Ruby 4.1.0dev (2026-11-16 revision 3fdd6e8fcd4f63b56b4ae6ac3c04e4604a6053ac)
source.c
1#include "prism/internal/source.h"
2
3#include "prism/internal/allocator.h"
4#include "prism/internal/buffer.h"
5#include "prism/internal/oom.h"
6
7#include <stdlib.h>
8#include <string.h>
9
10/* The following headers are necessary to read files using demand paging. */
11#ifdef _WIN32
12#include <windows.h>
13#elif defined(_POSIX_MAPPED_FILES)
14#include <fcntl.h>
15#include <sys/mman.h>
16#include <sys/stat.h>
17#elif defined(PRISM_HAS_FILESYSTEM)
18#include <fcntl.h>
19#include <sys/stat.h>
20#endif
21
22static const uint8_t empty_source[] = "";
23
27static pm_source_t *
28pm_source_alloc(const uint8_t *source, size_t length, pm_source_type_t type) {
29 pm_source_t *result = xmalloc(sizeof(pm_source_t));
30 if (result == NULL) PRISM_OOM();
31
32 *result = (struct pm_source_t) {
33 .source = source,
34 .length = length,
35 .type = type
36 };
37
38 return result;
39}
40
45pm_source_constant_new(const uint8_t *data, size_t length) {
46 return pm_source_alloc(data, length, PM_SOURCE_CONSTANT);
47}
48
53pm_source_shared_new(const uint8_t *data, size_t length) {
54 return pm_source_alloc(data, length, PM_SOURCE_SHARED);
55}
56
61pm_source_owned_new(uint8_t *data, size_t length) {
62 return pm_source_alloc(data, length, PM_SOURCE_OWNED);
63}
64
65#ifdef _WIN32
70typedef struct {
72 WCHAR *path;
73
75 size_t path_size;
76
78 HANDLE file;
79} pm_source_file_handle_t;
80
85pm_source_file_handle_open(pm_source_file_handle_t *handle, const char *filepath) {
86 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
87 if (length == 0) return PM_SOURCE_INIT_ERROR_GENERIC;
88
89 handle->path_size = sizeof(WCHAR) * ((size_t) length);
90 handle->path = xmalloc(handle->path_size);
91 if ((handle->path == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, handle->path, length) == 0)) {
92 xfree_sized(handle->path, handle->path_size);
94 }
95
96 handle->file = CreateFileW(handle->path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
97 if (handle->file == INVALID_HANDLE_VALUE) {
99
100 if (GetLastError() == ERROR_ACCESS_DENIED) {
101 DWORD attributes = GetFileAttributesW(handle->path);
102 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
104 }
105 }
106
107 xfree_sized(handle->path, handle->path_size);
108 return result;
109 }
110
112}
113
117static void
118pm_source_file_handle_close(pm_source_file_handle_t *handle) {
119 xfree_sized(handle->path, handle->path_size);
120 CloseHandle(handle->file);
121}
122#endif
123
128pm_source_mapped_new(const char *filepath, int open_flags, pm_source_init_result_t *result) {
129#ifdef _WIN32
130 (void) open_flags;
131
132 /* Open the file for reading. */
133 pm_source_file_handle_t handle;
134 *result = pm_source_file_handle_open(&handle, filepath);
135 if (*result != PM_SOURCE_INIT_SUCCESS) return NULL;
136
137 /* Get the file size. */
138 DWORD file_size = GetFileSize(handle.file, NULL);
139 if (file_size == INVALID_FILE_SIZE) {
140 pm_source_file_handle_close(&handle);
142 return NULL;
143 }
144
145 /* If the file is empty, then return a constant source. */
146 if (file_size == 0) {
147 pm_source_file_handle_close(&handle);
148 *result = PM_SOURCE_INIT_SUCCESS;
149 return pm_source_alloc(empty_source, 0, PM_SOURCE_CONSTANT);
150 }
151
152 /* Create a mapping of the file. */
153 HANDLE mapping = CreateFileMapping(handle.file, NULL, PAGE_READONLY, 0, 0, NULL);
154 if (mapping == NULL) {
155 pm_source_file_handle_close(&handle);
157 return NULL;
158 }
159
160 /* Map the file into memory. */
161 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
162 CloseHandle(mapping);
163 pm_source_file_handle_close(&handle);
164
165 if (source == NULL) {
167 return NULL;
168 }
169
170 *result = PM_SOURCE_INIT_SUCCESS;
171 return pm_source_alloc(source, (size_t) file_size, PM_SOURCE_MAPPED);
172#elif defined(_POSIX_MAPPED_FILES)
173 /* Open the file for reading. */
174 int fd = open(filepath, O_RDONLY | open_flags);
175 if (fd == -1) {
177 return NULL;
178 }
179
180 /* Stat the file to get the file size. */
181 struct stat sb;
182 if (fstat(fd, &sb) == -1) {
183 close(fd);
185 return NULL;
186 }
187
188 /* Ensure it is a file and not a directory. */
189 if (S_ISDIR(sb.st_mode)) {
190 close(fd);
192 return NULL;
193 }
194
195 /*
196 * For non-regular files (pipes, character devices), return a specific
197 * error so the caller can handle reading through their own I/O layer.
198 */
199 if (!S_ISREG(sb.st_mode)) {
200 close(fd);
202 return NULL;
203 }
204
205 /* mmap the file descriptor to virtually get the contents. */
206 size_t size = (size_t) sb.st_size;
207
208 if (size == 0) {
209 close(fd);
210 *result = PM_SOURCE_INIT_SUCCESS;
211 return pm_source_alloc(empty_source, 0, PM_SOURCE_CONSTANT);
212 }
213
214 uint8_t *source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
215 if (source == MAP_FAILED) {
216 close(fd);
218 return NULL;
219 }
220
221 close(fd);
222 *result = PM_SOURCE_INIT_SUCCESS;
223 return pm_source_alloc(source, size, PM_SOURCE_MAPPED);
224#else
225 (void) open_flags;
226 return pm_source_file_new(filepath, result);
227#endif
228}
229
234pm_source_file_new(const char *filepath, pm_source_init_result_t *result) {
235#ifdef _WIN32
236 /* Open the file for reading. */
237 pm_source_file_handle_t handle;
238 *result = pm_source_file_handle_open(&handle, filepath);
239 if (*result != PM_SOURCE_INIT_SUCCESS) return NULL;
240
241 /* Get the file size. */
242 const DWORD file_size = GetFileSize(handle.file, NULL);
243 if (file_size == INVALID_FILE_SIZE) {
244 pm_source_file_handle_close(&handle);
246 return NULL;
247 }
248
249 /* If the file is empty, return a constant source. */
250 if (file_size == 0) {
251 pm_source_file_handle_close(&handle);
252 *result = PM_SOURCE_INIT_SUCCESS;
253 return pm_source_alloc(empty_source, 0, PM_SOURCE_CONSTANT);
254 }
255
256 /* Create a buffer to read the file into. */
257 uint8_t *source = xmalloc(file_size);
258 if (source == NULL) {
259 pm_source_file_handle_close(&handle);
261 return NULL;
262 }
263
264 /* Read the contents of the file. */
265 DWORD bytes_read;
266 if (!ReadFile(handle.file, source, file_size, &bytes_read, NULL)) {
267 xfree_sized(source, file_size);
268 pm_source_file_handle_close(&handle);
270 return NULL;
271 }
272
273 /* Check the number of bytes read. */
274 if (bytes_read != file_size) {
275 xfree_sized(source, file_size);
276 pm_source_file_handle_close(&handle);
278 return NULL;
279 }
280
281 pm_source_file_handle_close(&handle);
282 *result = PM_SOURCE_INIT_SUCCESS;
283 return pm_source_alloc(source, (size_t) file_size, PM_SOURCE_OWNED);
284#elif defined(PRISM_HAS_FILESYSTEM)
285 /* Open the file for reading. */
286 int fd = open(filepath, O_RDONLY);
287 if (fd == -1) {
289 return NULL;
290 }
291
292 /* Stat the file to get the file size. */
293 struct stat sb;
294 if (fstat(fd, &sb) == -1) {
295 close(fd);
297 return NULL;
298 }
299
300 /* Ensure it is a file and not a directory. */
301 if (S_ISDIR(sb.st_mode)) {
302 close(fd);
304 return NULL;
305 }
306
307 /* Check the size to see if it's empty. */
308 size_t size = (size_t) sb.st_size;
309 if (size == 0) {
310 close(fd);
311 *result = PM_SOURCE_INIT_SUCCESS;
312 return pm_source_alloc(empty_source, 0, PM_SOURCE_CONSTANT);
313 }
314
315 const size_t length = (size_t) size;
316 uint8_t *source = xmalloc(length);
317 if (source == NULL) {
318 close(fd);
320 return NULL;
321 }
322
323 ssize_t bytes_read = read(fd, source, length);
324 close(fd);
325
326 if (bytes_read == -1 || (size_t) bytes_read != length) {
327 xfree_sized(source, length);
329 return NULL;
330 }
331
332 *result = PM_SOURCE_INIT_SUCCESS;
333 return pm_source_alloc(source, length, PM_SOURCE_OWNED);
334#else
335 (void) filepath;
337 perror("pm_source_file_new is not implemented for this platform");
338 return NULL;
339#endif
340}
341
348pm_source_stream_new(void *stream, pm_source_stream_fgets_t *fgets, pm_source_stream_feof_t *feof) {
349 pm_source_t *source = pm_source_alloc(NULL, 0, PM_SOURCE_STREAM);
350 source->stream.buffer = pm_buffer_new();
351 source->stream.stream = stream;
352 source->stream.fgets = fgets;
353 source->stream.feof = feof;
354 source->stream.eof = false;
355
356 return source;
357}
358
365bool
366pm_source_stream_read(pm_source_t *source) {
367 pm_buffer_t *buffer = source->stream.buffer;
368
369#define LINE_SIZE 4096
370 char line[LINE_SIZE];
371
372 /*
373 * A chunk read from the stream may legitimately contain embedded NUL bytes,
374 * so the NUL terminator written by fgets cannot be located with strlen.
375 * Instead we prefill the buffer with a non-NUL sentinel before each read;
376 * the terminator is then the only NUL at or after the data, so it can be
377 * found by scanning from the end. The value only needs to differ from '\0'.
378 */
379#define LINE_FILL 0xff
380
381 /*
382 * The fgets and feof members share their names with standard library
383 * functions, which <stdio.h> may define as function-like macros (newlib on
384 * Cygwin does this for feof). Parenthesizing the member access keeps those
385 * macros from expanding at the call sites.
386 */
387 while (memset(line, LINE_FILL, LINE_SIZE), (source->stream.fgets)(line, LINE_SIZE, source->stream.stream) != NULL) {
388 /*
389 * Locate the NUL terminator written by fgets (the last NUL in the
390 * buffer) to recover the length of the chunk, then drop the terminator.
391 */
392 size_t length = LINE_SIZE;
393 while (length > 0 && line[length - 1] != '\0') length--;
394 if (length > 0) length--;
395
396 /*
397 * An empty chunk means the stream returned an empty string instead of
398 * signaling EOF (a well-behaved stream never does this). We can't make
399 * progress on it, so stop reading rather than spinning forever.
400 */
401 if (length == 0) {
402 break;
403 }
404
405 /* Append the chunk to the buffer. */
406 pm_buffer_append_string(buffer, line, length);
407
408 bool newline = (line[length - 1] == '\n');
409
410 /*
411 * A chunk with no trailing newline is either a line longer than the
412 * read buffer (keep reading the rest of it) or the final line at EOF.
413 * This is the only place the stream's EOF state changes what we do
414 * next, so it is the only place we ask the stream whether it has hit
415 * EOF. A newline-terminated line needs no such check: if it happens to
416 * be the last line, the next fgets returns NULL and ends the loop.
417 */
418 if (!newline && !(source->stream.feof)(source->stream.stream)) {
419 continue;
420 }
421
422 /*
423 * Check if the line matches the __END__ marker. If it does, then stop
424 * reading and return false. In most circumstances, this means we should
425 * stop reading from the stream so that the DATA constant can pick it
426 * up.
427 */
428 switch (length) {
429 case 7:
430 if (memcmp(line, "__END__", 7) == 0) {
431 source->source = (const uint8_t *) pm_buffer_value(buffer);
432 source->length = pm_buffer_length(buffer);
433 return false;
434 }
435 break;
436 case 8:
437 if (memcmp(line, "__END__\n", 8) == 0) {
438 source->source = (const uint8_t *) pm_buffer_value(buffer);
439 source->length = pm_buffer_length(buffer);
440 return false;
441 }
442 break;
443 case 9:
444 if (memcmp(line, "__END__\r\n", 9) == 0) {
445 source->source = (const uint8_t *) pm_buffer_value(buffer);
446 source->length = pm_buffer_length(buffer);
447 return false;
448 }
449 break;
450 }
451
452 /*
453 * A chunk that reached here without a trailing newline is the final
454 * line at EOF (the check above would have continued otherwise), so
455 * there is nothing more to read.
456 */
457 if (!newline) {
458 break;
459 }
460 }
461
462#undef LINE_FILL
463#undef LINE_SIZE
464
465 source->stream.eof = true;
466 source->source = (const uint8_t *) pm_buffer_value(buffer);
467 source->length = pm_buffer_length(buffer);
468 return true;
469}
470
474bool
475pm_source_stream_eof(const pm_source_t *source) {
476 return source->stream.eof;
477}
478
482void
483pm_source_free(pm_source_t *source) {
484 switch (source->type) {
485 case PM_SOURCE_CONSTANT:
486 case PM_SOURCE_SHARED:
487 /* No cleanup needed for the data. */
488 break;
489 case PM_SOURCE_OWNED:
490 xfree_sized((void *) source->source, source->length);
491 break;
492 case PM_SOURCE_MAPPED:
493#if defined(_WIN32)
494 if (source->length > 0) {
495 UnmapViewOfFile((void *) source->source);
496 }
497#elif defined(_POSIX_MAPPED_FILES)
498 if (source->length > 0) {
499 munmap((void *) source->source, source->length);
500 }
501#endif
502 break;
503 case PM_SOURCE_STREAM:
504 pm_buffer_free(source->stream.buffer);
505 break;
506 }
507
508 xfree_sized(source, sizeof(pm_source_t));
509}
510
514size_t
515pm_source_length(const pm_source_t *source) {
516 return source->length;
517}
518
522const uint8_t *
523pm_source_source(const pm_source_t *source) {
524 return source->source;
525}
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
VALUE type(ANYARGS)
ANYARGS-ed function type.
int() pm_source_stream_feof_t(void *stream)
This function is used to check whether a stream is at EOF.
Definition source.h:45
char *() pm_source_stream_fgets_t(char *string, int size, void *stream)
This function is used to retrieve a line of input from a stream.
Definition source.h:39
pm_source_init_result_t
Represents the result of initializing a source from a file.
Definition source.h:50
@ PM_SOURCE_INIT_ERROR_GENERIC
Indicates a generic error from a source init function, where the type of error should be read from er...
Definition source.h:58
@ PM_SOURCE_INIT_SUCCESS
Indicates that the source was successfully initialized.
Definition source.h:52
@ PM_SOURCE_INIT_ERROR_NON_REGULAR
Indicates that the file is not a regular file (e.g.
Definition source.h:69
@ PM_SOURCE_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
Definition source.h:63