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