Ruby 4.1.0dev (2026-10-10 revision 2e236bfd72ef89adf28cabda5fd6738649183ff0)
file.c
1#if defined(__MINGW32__)
2/* before stdio.h in ruby/define.h */
3# define MINGW_HAS_SECURE_API 1
4#endif
5#include "ruby/ruby.h"
6#include "ruby/encoding.h"
7#include "internal.h"
8#include "internal/error.h"
9#include <winbase.h>
10#include <wchar.h>
11#include <shlwapi.h>
12#include "win32/file.h"
13
14#ifndef INVALID_FILE_ATTRIBUTES
15# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
16#endif
17
18/* cache 'encoding name' => 'code page' into a hash */
19static struct code_page_table {
20 USHORT *table;
21 unsigned int count;
22} rb_code_page;
23
24#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
25#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
26static int
27IS_ABSOLUTE_PATH_P(const WCHAR *path, size_t len)
28{
29 if (len < 2) return FALSE;
30 if (ISALPHA(path[0]))
31 return len > 2 && path[1] == L':' && IS_DIR_SEPARATOR_P(path[2]);
32 else
33 return IS_DIR_UNC_P(path);
34}
35
36/* MultiByteToWideChar() doesn't work with code page 51932 */
37#define INVALID_CODE_PAGE 51932
38#define PATH_BUFFER_SIZE MAX_PATH * 2
39
40/* defined in win32/win32.c */
41#define system_code_page rb_w32_filecp
42#define mbstr_to_wstr rb_w32_mbstr_to_wstr
43#define wstr_to_mbstr rb_w32_wstr_to_mbstr
44
45static inline void
46replace_wchar(wchar_t *s, int find, int replace)
47{
48 while (*s != 0) {
49 if (*s == find)
50 *s = replace;
51 s++;
52 }
53}
54
55/* Remove trailing invalid ':$DATA' of the path. */
56static inline size_t
57remove_invalid_alternative_data(wchar_t *wfullpath, size_t size)
58{
59 static const wchar_t prime[] = L":$DATA";
60 enum { prime_len = (sizeof(prime) / sizeof(wchar_t)) -1 };
61
62 if (size <= prime_len || _wcsnicmp(wfullpath + size - prime_len, prime, prime_len) != 0)
63 return size;
64
65 /* alias of stream */
66 /* get rid of a bug of x64 VC++ */
67 if (wfullpath[size - (prime_len + 1)] == ':') {
68 /* remove trailing '::$DATA' */
69 size -= prime_len + 1; /* prime */
70 wfullpath[size] = L'\0';
71 }
72 else {
73 /* remove trailing ':$DATA' of paths like '/aa:a:$DATA' */
74 wchar_t *pos = wfullpath + size - (prime_len + 1);
75 while (!IS_DIR_SEPARATOR_P(*pos) && pos != wfullpath) {
76 if (*pos == L':') {
77 size -= prime_len; /* alternative */
78 wfullpath[size] = L'\0';
79 break;
80 }
81 pos--;
82 }
83 }
84 return size;
85}
86
87void rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
88
89static int
90code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
91{
92 const char *n = (const char *)name;
93 if (strncmp("CP", n, 2) == 0) {
94 int code_page = atoi(n + 2);
95 if (code_page != 0) {
96 struct code_page_table *cp = (struct code_page_table *)arg;
97 unsigned int count = cp->count;
98 USHORT *table = cp->table;
99 if (count <= idx) {
100 unsigned int i = count;
101 count = (((idx + 4) & ~31) | 28);
102 table = realloc(table, count * sizeof(*table));
103 if (!table) return ST_CONTINUE;
104 cp->count = count;
105 cp->table = table;
106 while (i < count) table[i++] = INVALID_CODE_PAGE;
107 }
108 table[idx] = (USHORT)code_page;
109 }
110 }
111 return ST_CONTINUE;
112}
113
114/*
115 Return code page number of the encoding.
116 Cache code page into a hash for performance since finding the code page in
117 Encoding#names is slow.
118*/
119static UINT
120code_page(rb_encoding *enc)
121{
122 int enc_idx;
123
124 if (!enc)
125 return system_code_page();
126
127 enc_idx = rb_enc_to_index(enc);
128
129 /* map US-ASCII and ASCII-8bit as code page 1252 (us-ascii) */
130 if (enc_idx == rb_usascii_encindex() || enc_idx == rb_ascii8bit_encindex()) {
131 return 1252;
132 }
133 if (enc_idx == rb_utf8_encindex()) {
134 return CP_UTF8;
135 }
136
137 if (0 <= enc_idx && (unsigned int)enc_idx < rb_code_page.count)
138 return rb_code_page.table[enc_idx];
139
140 return INVALID_CODE_PAGE;
141}
142
143#define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
144
145/*
146 Replace the last part of the path to long name.
147 We try to avoid to call FindFirstFileW() since it takes long time.
148*/
149static inline size_t
150replace_to_long_name(wchar_t **wfullpath, size_t size, size_t buffer_size)
151{
152 WIN32_FIND_DATAW find_data;
153 HANDLE find_handle;
154
155 /*
156 Skip long name conversion if the path is already long name.
157 Short name is 8.3 format.
158 https://en.wikipedia.org/wiki/8.3_filename
159 This check can be skipped for directory components that have file
160 extensions longer than 3 characters, or total lengths longer than
161 12 characters.
162 https://msdn.microsoft.com/en-us/library/windows/desktop/aa364980(v=vs.85).aspx
163 */
164 size_t const max_short_name_size = 8 + 1 + 3;
165 size_t const max_extension_size = 3;
166 size_t path_len = 1, extension_len = 0;
167 wchar_t *pos = *wfullpath;
168
169 if (size == 3 && pos[1] == L':' && pos[2] == L'\\' && pos[3] == L'\0') {
170 /* root path doesn't need short name expansion */
171 return size;
172 }
173
174 /* skip long name conversion if path contains wildcard characters */
175 if (wcspbrk(pos, L"*?")) {
176 return size;
177 }
178
179 pos = *wfullpath + size - 1;
180 while (!IS_DIR_SEPARATOR_P(*pos) && pos != *wfullpath) {
181 if (!extension_len && *pos == L'.') {
182 extension_len = path_len - 1;
183 }
184 if (path_len > max_short_name_size || extension_len > max_extension_size) {
185 return size;
186 }
187 path_len++;
188 pos--;
189 }
190
191 if ((pos >= *wfullpath + 2) &&
192 (*wfullpath)[0] == L'\\' && (*wfullpath)[1] == L'\\') {
193 /* UNC path: no short file name, and needs Network Share
194 * Management functions instead of FindFirstFile. */
195 if (pos == *wfullpath + 2) {
196 /* //host only */
197 return size;
198 }
199 if (!wmemchr(*wfullpath + 2, L'\\', pos - *wfullpath - 2)) {
200 /* //host/share only */
201 return size;
202 }
203 }
204
205 find_handle = FindFirstFileW(*wfullpath, &find_data);
206 if (find_handle != INVALID_HANDLE_VALUE) {
207 size_t trail_pos = pos - *wfullpath + IS_DIR_SEPARATOR_P(*pos);
208 size_t file_len = wcslen(find_data.cFileName);
209 size_t oldsize = size;
210
211 FindClose(find_handle);
212 size = trail_pos + file_len;
213 if (size > (buffer_size ? buffer_size-1 : oldsize)) {
214 wchar_t *buf = ALLOC_N(wchar_t, (size + 1));
215 wcsncpy(buf, *wfullpath, trail_pos);
216 if (!buffer_size)
217 xfree(*wfullpath);
218 *wfullpath = buf;
219 }
220 wcsncpy(*wfullpath + trail_pos, find_data.cFileName, file_len + 1);
221 }
222 return size;
223}
224
225static inline size_t
226user_length_in_path(const wchar_t *wuser, size_t len)
227{
228 size_t i;
229
230 for (i = 0; i < len && !IS_DIR_SEPARATOR_P(wuser[i]); i++)
231 ;
232
233 return i;
234}
235
236static VALUE
237append_wstr(VALUE dst, const WCHAR *ws, ssize_t len, UINT cp, rb_encoding *enc)
238{
239 rb_len_t olen;
240 long nlen = (long)len;
241
242 if (cp != INVALID_CODE_PAGE) {
243 if (len == -1) len = lstrlenW(ws);
244 nlen = WideCharToMultiByte(cp, 0, ws, len, NULL, 0, NULL, NULL);
245 olen = RSTRING_LEN(dst);
246 rb_str_modify_expand(dst, nlen);
247 WideCharToMultiByte(cp, 0, ws, len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
248 rb_enc_associate(dst, enc);
249 rb_str_set_len(dst, olen + nlen);
250 }
251 else {
252 const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE;
253 char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (int)len, &nlen);
254 rb_econv_t *ec = rb_econv_open("UTF-8", rb_enc_name(enc), replaceflags);
255 dst = rb_econv_append(ec, utf8str, nlen, dst, replaceflags);
256 rb_econv_close(ec);
257 free(utf8str);
258 }
259 return dst;
260}
261
262VALUE
263rb_default_home_dir(VALUE result)
264{
265 WCHAR *dir = rb_w32_home_dir();
266 if (!dir) {
267 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding '~'");
268 }
269 append_wstr(result, dir, -1,
270 CP_UTF8, rb_utf8_encoding());
271
272 xfree(dir);
273 return result;
274}
275
276VALUE
277rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
278{
279 if (!result) {
280 result = rb_usascii_str_new(0, 1);
281 }
282
283 size_t size = 0, whome_len = 0;
284 size_t buffer_len = 0;
285 long wpath_len = 0, wdir_len = 0;
286 wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
287 wchar_t *wdir = NULL, *wdir_pos = NULL;
288 wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
289 UINT path_cp, cp;
290 VALUE path = fname, dir = dname;
291 wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
292 wchar_t path_drive = L'\0', dir_drive = L'\0';
293 int ignore_dir = 0;
294 rb_encoding *path_encoding;
295
296 /* get path encoding */
297 if (NIL_P(dir)) {
298 path_encoding = rb_enc_get(path);
299 }
300 else {
301 path_encoding = rb_enc_check(path, dir);
302 }
303
304 cp = path_cp = code_page(path_encoding);
305
306 /* workaround invalid codepage */
307 if (path_cp == INVALID_CODE_PAGE) {
308 cp = CP_UTF8;
309 if (!NIL_P(path)) {
310 path = fix_string_encoding(path, path_encoding);
311 }
312 }
313
314 /* convert char * to wchar_t */
315 if (!NIL_P(path)) {
316 const rb_len_t path_len = RSTRING_LEN(path);
317#if SIZEOF_INT < SIZEOF_RB_LEN_T
318 if ((rb_len_t)(int)path_len != path_len) {
319 rb_raise(rb_eRangeError, "path (%"PRIdLEN" bytes) is too long",
320 path_len);
321 }
322#endif
323 wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
324 wpath_pos = wpath;
325 }
326
327 /* determine if we need the user's home directory */
328 /* expand '~' only if NOT rb_file_absolute_path() where `abs_mode` is 1 */
329 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L'~' &&
330 (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
331 whome = rb_w32_home_dir();
332 if (whome == NULL) {
333 free(wpath);
334 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding '~'");
335 }
336 whome_len = wcslen(whome);
337
338 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
339 free(wpath);
340 free(whome);
341 rb_raise(rb_eArgError, "non-absolute home");
342 }
343
344 if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
345 /* use filesystem encoding if expanding home dir */
346 path_encoding = rb_filesystem_encoding();
347 cp = path_cp = code_page(path_encoding);
348 }
349
350 /* ignores dir since we are expanding home */
351 ignore_dir = 1;
352
353 /* exclude ~ from the result */
354 wpath_pos++;
355 wpath_len--;
356
357 /* exclude separator if present */
358 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
359 wpath_pos++;
360 wpath_len--;
361 }
362 }
363 else if (wpath_len >= 2 && wpath_pos[1] == L':') {
364 if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
365 /* ignore dir since path contains a drive letter and a root slash */
366 ignore_dir = 1;
367 }
368 else {
369 /* determine if we ignore dir or not later */
370 path_drive = wpath_pos[0];
371 wpath_pos += 2;
372 wpath_len -= 2;
373 }
374 }
375 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L'~') {
376 result = rb_str_new_cstr("can't find user ");
377 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
378 path_cp, path_encoding);
379
380 free(wpath);
381
382 rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
383 }
384
385 /* convert dir */
386 if (!ignore_dir && !NIL_P(dir)) {
387 /* fix string encoding */
388 if (path_cp == INVALID_CODE_PAGE) {
389 dir = fix_string_encoding(dir, path_encoding);
390 }
391
392 /* convert char * to wchar_t */
393 if (!NIL_P(dir)) {
394 const rb_len_t dir_len = RSTRING_LEN(dir);
395#if SIZEOF_INT < SIZEOF_RB_LEN_T
396 if ((rb_len_t)(int)dir_len != dir_len) {
397 free(wpath);
398 rb_raise(rb_eRangeError, "base directory (%"PRIdLEN" bytes) is too long",
399 dir_len);
400 }
401#endif
402 wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
403 wdir_pos = wdir;
404 }
405
406 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L'~' &&
407 (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
408 whome = rb_w32_home_dir();
409 if (whome == NULL) {
410 free(wpath);
411 free(wdir);
412 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding '~'");
413 }
414 whome_len = wcslen(whome);
415
416 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
417 free(wpath);
418 free(wdir);
419 free(whome);
420 rb_raise(rb_eArgError, "non-absolute home");
421 }
422
423 /* exclude ~ from the result */
424 wdir_pos++;
425 wdir_len--;
426
427 /* exclude separator if present */
428 if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
429 wdir_pos++;
430 wdir_len--;
431 }
432 }
433 else if (wdir_len >= 2 && wdir[1] == L':') {
434 dir_drive = wdir[0];
435 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
436 wdir_len = 2;
437 }
438 }
439 else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
440 /* UNC path */
441 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
442 /* cut the UNC path tail to '//host/share' */
443 long separators = 0;
444 long pos = 2;
445 while (pos < wdir_len && separators < 2) {
446 if (IS_DIR_SEPARATOR_P(wdir[pos])) {
447 separators++;
448 }
449 pos++;
450 }
451 if (separators == 2)
452 wdir_len = pos - 1;
453 }
454 }
455 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L'~') {
456 result = rb_str_new_cstr("can't find user ");
457 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
458 path_cp, path_encoding);
459 free(wpath);
460 free(wdir);
461
462 rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
463 }
464 }
465
466 /* determine if we ignore dir or not */
467 if (!ignore_dir && path_drive && dir_drive) {
468 if (towupper(path_drive) != towupper(dir_drive)) {
469 /* ignore dir since path drive is different from dir drive */
470 ignore_dir = 1;
471 wdir_len = 0;
472 dir_drive = 0;
473 }
474 }
475
476 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
477 /* ignore dir since path has UNC root */
478 ignore_dir = 1;
479 wdir_len = 0;
480 }
481 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
482 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
483 /* ignore dir since path has root slash and dir doesn't have drive or UNC root */
484 ignore_dir = 1;
485 wdir_len = 0;
486 }
487
488 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
489
490 buffer = buffer_pos = ALLOC_N(wchar_t, (buffer_len + 1));
491
492 /* add home */
493 if (whome_len) {
494 wcsncpy(buffer_pos, whome, whome_len);
495 buffer_pos += whome_len;
496 }
497
498 /* Add separator if required */
499 if (whome_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
500 buffer_pos[0] = L'\\';
501 buffer_pos++;
502 }
503 else if (!dir_drive && path_drive) {
504 *buffer_pos++ = path_drive;
505 *buffer_pos++ = L':';
506 }
507
508 if (wdir_len) {
509 wcsncpy(buffer_pos, wdir_pos, wdir_len);
510 buffer_pos += wdir_len;
511 }
512
513 /* add separator if required */
514 if (wdir_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
515 buffer_pos[0] = L'\\';
516 buffer_pos++;
517 }
518
519 /* now deal with path */
520 if (wpath_len) {
521 wcsncpy(buffer_pos, wpath_pos, wpath_len);
522 buffer_pos += wpath_len;
523 }
524
525 /* GetFullPathNameW requires at least "." to determine current directory */
526 if (wpath_len == 0) {
527 buffer_pos[0] = L'.';
528 buffer_pos++;
529 }
530
531 /* Ensure buffer is NULL terminated */
532 buffer_pos[0] = L'\0';
533
534 /* FIXME: Make this more robust */
535 /* Determine require buffer size */
536 size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
537 if (size > PATH_BUFFER_SIZE) {
538 /* allocate more memory than allotted originally by PATH_BUFFER_SIZE */
539 wfullpath = ALLOC_N(wchar_t, size);
540 size = GetFullPathNameW(buffer, size, wfullpath, NULL);
541 }
542 else {
543 wfullpath = wfullpath_buffer;
544 }
545
546 /* Remove any trailing slashes */
547 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
548 wfullpath[size - 2] != L':' &&
549 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
550 size -= 1;
551 wfullpath[size] = L'\0';
552 }
553
554 /* Remove any trailing dot */
555 if (wfullpath[size - 1] == L'.') {
556 size -= 1;
557 wfullpath[size] = L'\0';
558 }
559
560 /* removes trailing invalid ':$DATA' */
561 size = remove_invalid_alternative_data(wfullpath, size);
562
563 /* Replace the trailing path to long name */
564 if (long_name) {
565 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
566 size = replace_to_long_name(&wfullpath, size, bufsize);
567 }
568
569 /* sanitize backslashes with forwardslashes */
570 replace_wchar(wfullpath, L'\\', L'/');
571
572 /* convert to VALUE and set the path encoding */
573 rb_str_set_len(result, 0);
574 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
575
576 /* TODO: better cleanup */
577 xfree(buffer);
578 free(wpath);
579 free(wdir);
580 free(whome);
581
582 if (wfullpath != wfullpath_buffer)
583 xfree(wfullpath);
584
585 rb_enc_associate(result, path_encoding);
586 return result;
587}
588
589int
590rb_file_load_ok(const char *path)
591{
592 DWORD attr;
593 int ret = 1;
594 long len;
595 wchar_t* wpath;
596
597 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
598 if (!wpath) return 0;
599
600 attr = GetFileAttributesW(wpath);
601 if (attr == INVALID_FILE_ATTRIBUTES ||
602 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
603 ret = 0;
604 }
605 else {
606 HANDLE h = CreateFileW(wpath, GENERIC_READ,
607 FILE_SHARE_READ | FILE_SHARE_WRITE,
608 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
609 if (h != INVALID_HANDLE_VALUE) {
610 CloseHandle(h);
611 }
612 else {
613 ret = 0;
614 }
615 }
616 free(wpath);
617 return ret;
618}
619
620int
621rb_freopen(VALUE fname, const char *mode, FILE *file)
622{
623 WCHAR *wname, wmode[4];
624 VALUE wtmp;
625 char *name;
627 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
628 if (n > numberof(wmode)) return EINVAL;
629 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
630 RSTRING_GETMEM(fname, name, len);
631 n = rb_len2int(len);
632 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
633 wname = ALLOCV_N(WCHAR, wtmp, len + 1);
634 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
635 wname[len] = L'\0';
636 RB_GC_GUARD(fname);
637 {
638 FILE *newfp = 0;
639 e = _wfreopen_s(&newfp, wname, wmode, file);
640 }
641 ALLOCV_END(wtmp);
642 return e;
643}
644
645void
646Init_w32_codepage(void)
647{
648 if (rb_code_page.count) return;
649 rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
650}
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
Definition transcode.h:526
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
Definition transcode.h:524
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
#define ISALPHA
Old name of rb_isalpha.
Definition ctype.h:92
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition memory.h:405
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:406
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1461
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1508
Encoding relates APIs.
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition string.c:988
rb_econv_t * rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags)
Creates a new instance of struct rb_econv_t.
Definition transcode.c:1108
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, rb_len_t bytesize, VALUE dst, int flags)
Converts the passed C's pointer according to the passed converter, then append the conversion result ...
Definition transcode.c:1878
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
Definition transcode.c:1744
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2816
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
Definition string.h:1557
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
Definition string.c:3500
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1539
int len
Length of the buffer.
Definition io.h:8
#define rb_len2int
Just another name of rb_len2int_inline.
Definition len_t.h:32
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:450
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:131
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40