Ruby 3.5.0dev (2025-01-10 revision 5fab31b15e32622c4b71d1d347a41937e9f9c212)
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 long olen, nlen = (long)len;
240
241 if (cp != INVALID_CODE_PAGE) {
242 if (len == -1) len = lstrlenW(ws);
243 nlen = WideCharToMultiByte(cp, 0, ws, len, NULL, 0, NULL, NULL);
244 olen = RSTRING_LEN(dst);
245 rb_str_modify_expand(dst, nlen);
246 WideCharToMultiByte(cp, 0, ws, len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
247 rb_enc_associate(dst, enc);
248 rb_str_set_len(dst, olen + nlen);
249 }
250 else {
251 const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE;
252 char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (int)len, &nlen);
253 rb_econv_t *ec = rb_econv_open("UTF-8", rb_enc_name(enc), replaceflags);
254 dst = rb_econv_append(ec, utf8str, nlen, dst, replaceflags);
255 rb_econv_close(ec);
256 free(utf8str);
257 }
258 return dst;
259}
260
261VALUE
262rb_default_home_dir(VALUE result)
263{
264 WCHAR *dir = rb_w32_home_dir();
265 if (!dir) {
266 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding '~'");
267 }
268 append_wstr(result, dir, -1,
269 CP_UTF8, rb_utf8_encoding());
270
271 xfree(dir);
272 return result;
273}
274
275VALUE
276rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
277{
278 size_t size = 0, whome_len = 0;
279 size_t buffer_len = 0;
280 long wpath_len = 0, wdir_len = 0;
281 wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
282 wchar_t *wdir = NULL, *wdir_pos = NULL;
283 wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
284 UINT path_cp, cp;
285 VALUE path = fname, dir = dname;
286 wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
287 wchar_t path_drive = L'\0', dir_drive = L'\0';
288 int ignore_dir = 0;
289 rb_encoding *path_encoding;
290
291 /* get path encoding */
292 if (NIL_P(dir)) {
293 path_encoding = rb_enc_get(path);
294 }
295 else {
296 path_encoding = rb_enc_check(path, dir);
297 }
298
299 cp = path_cp = code_page(path_encoding);
300
301 /* workaround invalid codepage */
302 if (path_cp == INVALID_CODE_PAGE) {
303 cp = CP_UTF8;
304 if (!NIL_P(path)) {
305 path = fix_string_encoding(path, path_encoding);
306 }
307 }
308
309 /* convert char * to wchar_t */
310 if (!NIL_P(path)) {
311 const long path_len = RSTRING_LEN(path);
312#if SIZEOF_INT < SIZEOF_LONG
313 if ((long)(int)path_len != path_len) {
314 rb_raise(rb_eRangeError, "path (%ld bytes) is too long",
315 path_len);
316 }
317#endif
318 wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
319 wpath_pos = wpath;
320 }
321
322 /* determine if we need the user's home directory */
323 /* expand '~' only if NOT rb_file_absolute_path() where `abs_mode` is 1 */
324 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L'~' &&
325 (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
326 whome = rb_w32_home_dir();
327 if (whome == NULL) {
328 free(wpath);
329 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding '~'");
330 }
331 whome_len = wcslen(whome);
332
333 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
334 free(wpath);
335 free(whome);
336 rb_raise(rb_eArgError, "non-absolute home");
337 }
338
339 if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
340 /* use filesystem encoding if expanding home dir */
341 path_encoding = rb_filesystem_encoding();
342 cp = path_cp = code_page(path_encoding);
343 }
344
345 /* ignores dir since we are expanding home */
346 ignore_dir = 1;
347
348 /* exclude ~ from the result */
349 wpath_pos++;
350 wpath_len--;
351
352 /* exclude separator if present */
353 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
354 wpath_pos++;
355 wpath_len--;
356 }
357 }
358 else if (wpath_len >= 2 && wpath_pos[1] == L':') {
359 if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
360 /* ignore dir since path contains a drive letter and a root slash */
361 ignore_dir = 1;
362 }
363 else {
364 /* determine if we ignore dir or not later */
365 path_drive = wpath_pos[0];
366 wpath_pos += 2;
367 wpath_len -= 2;
368 }
369 }
370 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L'~') {
371 result = rb_str_new_cstr("can't find user ");
372 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
373 path_cp, path_encoding);
374
375 free(wpath);
376
377 rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
378 }
379
380 /* convert dir */
381 if (!ignore_dir && !NIL_P(dir)) {
382 /* fix string encoding */
383 if (path_cp == INVALID_CODE_PAGE) {
384 dir = fix_string_encoding(dir, path_encoding);
385 }
386
387 /* convert char * to wchar_t */
388 if (!NIL_P(dir)) {
389 const long dir_len = RSTRING_LEN(dir);
390#if SIZEOF_INT < SIZEOF_LONG
391 if ((long)(int)dir_len != dir_len) {
392 free(wpath);
393 rb_raise(rb_eRangeError, "base directory (%ld bytes) is too long",
394 dir_len);
395 }
396#endif
397 wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
398 wdir_pos = wdir;
399 }
400
401 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L'~' &&
402 (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
403 whome = rb_w32_home_dir();
404 if (whome == NULL) {
405 free(wpath);
406 free(wdir);
407 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding '~'");
408 }
409 whome_len = wcslen(whome);
410
411 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
412 free(wpath);
413 free(wdir);
414 free(whome);
415 rb_raise(rb_eArgError, "non-absolute home");
416 }
417
418 /* exclude ~ from the result */
419 wdir_pos++;
420 wdir_len--;
421
422 /* exclude separator if present */
423 if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
424 wdir_pos++;
425 wdir_len--;
426 }
427 }
428 else if (wdir_len >= 2 && wdir[1] == L':') {
429 dir_drive = wdir[0];
430 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
431 wdir_len = 2;
432 }
433 }
434 else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
435 /* UNC path */
436 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
437 /* cut the UNC path tail to '//host/share' */
438 long separators = 0;
439 long pos = 2;
440 while (pos < wdir_len && separators < 2) {
441 if (IS_DIR_SEPARATOR_P(wdir[pos])) {
442 separators++;
443 }
444 pos++;
445 }
446 if (separators == 2)
447 wdir_len = pos - 1;
448 }
449 }
450 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L'~') {
451 result = rb_str_new_cstr("can't find user ");
452 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
453 path_cp, path_encoding);
454 free(wpath);
455 free(wdir);
456
457 rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
458 }
459 }
460
461 /* determine if we ignore dir or not */
462 if (!ignore_dir && path_drive && dir_drive) {
463 if (towupper(path_drive) != towupper(dir_drive)) {
464 /* ignore dir since path drive is different from dir drive */
465 ignore_dir = 1;
466 wdir_len = 0;
467 dir_drive = 0;
468 }
469 }
470
471 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
472 /* ignore dir since path has UNC root */
473 ignore_dir = 1;
474 wdir_len = 0;
475 }
476 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
477 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
478 /* ignore dir since path has root slash and dir doesn't have drive or UNC root */
479 ignore_dir = 1;
480 wdir_len = 0;
481 }
482
483 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
484
485 buffer = buffer_pos = ALLOC_N(wchar_t, (buffer_len + 1));
486
487 /* add home */
488 if (whome_len) {
489 wcsncpy(buffer_pos, whome, whome_len);
490 buffer_pos += whome_len;
491 }
492
493 /* Add separator if required */
494 if (whome_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
495 buffer_pos[0] = L'\\';
496 buffer_pos++;
497 }
498 else if (!dir_drive && path_drive) {
499 *buffer_pos++ = path_drive;
500 *buffer_pos++ = L':';
501 }
502
503 if (wdir_len) {
504 wcsncpy(buffer_pos, wdir_pos, wdir_len);
505 buffer_pos += wdir_len;
506 }
507
508 /* add separator if required */
509 if (wdir_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
510 buffer_pos[0] = L'\\';
511 buffer_pos++;
512 }
513
514 /* now deal with path */
515 if (wpath_len) {
516 wcsncpy(buffer_pos, wpath_pos, wpath_len);
517 buffer_pos += wpath_len;
518 }
519
520 /* GetFullPathNameW requires at least "." to determine current directory */
521 if (wpath_len == 0) {
522 buffer_pos[0] = L'.';
523 buffer_pos++;
524 }
525
526 /* Ensure buffer is NULL terminated */
527 buffer_pos[0] = L'\0';
528
529 /* FIXME: Make this more robust */
530 /* Determine require buffer size */
531 size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
532 if (size > PATH_BUFFER_SIZE) {
533 /* allocate more memory than allotted originally by PATH_BUFFER_SIZE */
534 wfullpath = ALLOC_N(wchar_t, size);
535 size = GetFullPathNameW(buffer, size, wfullpath, NULL);
536 }
537 else {
538 wfullpath = wfullpath_buffer;
539 }
540
541 /* Remove any trailing slashes */
542 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
543 wfullpath[size - 2] != L':' &&
544 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
545 size -= 1;
546 wfullpath[size] = L'\0';
547 }
548
549 /* Remove any trailing dot */
550 if (wfullpath[size - 1] == L'.') {
551 size -= 1;
552 wfullpath[size] = L'\0';
553 }
554
555 /* removes trailing invalid ':$DATA' */
556 size = remove_invalid_alternative_data(wfullpath, size);
557
558 /* Replace the trailing path to long name */
559 if (long_name) {
560 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
561 size = replace_to_long_name(&wfullpath, size, bufsize);
562 }
563
564 /* sanitize backslashes with forwardslashes */
565 replace_wchar(wfullpath, L'\\', L'/');
566
567 /* convert to VALUE and set the path encoding */
568 rb_str_set_len(result, 0);
569 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
570
571 /* TODO: better cleanup */
572 xfree(buffer);
573 free(wpath);
574 free(wdir);
575 free(whome);
576
577 if (wfullpath != wfullpath_buffer)
578 xfree(wfullpath);
579
580 rb_enc_associate(result, path_encoding);
581 return result;
582}
583
584int
585rb_file_load_ok(const char *path)
586{
587 DWORD attr;
588 int ret = 1;
589 long len;
590 wchar_t* wpath;
591
592 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
593 if (!wpath) return 0;
594
595 attr = GetFileAttributesW(wpath);
596 if (attr == INVALID_FILE_ATTRIBUTES ||
597 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
598 ret = 0;
599 }
600 else {
601 HANDLE h = CreateFileW(wpath, GENERIC_READ,
602 FILE_SHARE_READ | FILE_SHARE_WRITE,
603 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
604 if (h != INVALID_HANDLE_VALUE) {
605 CloseHandle(h);
606 }
607 else {
608 ret = 0;
609 }
610 }
611 free(wpath);
612 return ret;
613}
614
615int
616rb_freopen(VALUE fname, const char *mode, FILE *file)
617{
618 WCHAR *wname, wmode[4];
619 VALUE wtmp;
620 char *name;
621 long len;
622 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
623 if (n > numberof(wmode)) return EINVAL;
624 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
625 RSTRING_GETMEM(fname, name, len);
626 n = rb_long2int(len);
627 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
628 wname = ALLOCV_N(WCHAR, wtmp, len + 1);
629 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
630 wname[len] = L'\0';
631 RB_GC_GUARD(fname);
632#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
633 e = _wfreopen(wname, wmode, file) ? 0 : errno;
634#else
635 {
636 FILE *newfp = 0;
637 e = _wfreopen_s(&newfp, wname, wmode, file);
638 }
639#endif
640 ALLOCV_END(wtmp);
641 return e;
642}
643
644void
645Init_w32_codepage(void)
646{
647 if (rb_code_page.count) return;
648 rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
649}
#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
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1434
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:1481
Encoding relates APIs.
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition string.c:919
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:1098
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
Definition transcode.c:1731
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, long bytesize, VALUE dst, int flags)
Converts the passed C's pointer according to the passed converter, then append the conversion result ...
Definition transcode.c:1847
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3268
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2648
#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:1514
int len
Length of the buffer.
Definition io.h:8
#define rb_long2int
Just another name of rb_long2int_inline.
Definition long.h:62
#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:488
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40