Ruby  3.4.0dev (2024-11-05 revision 348a53415339076afc4a02fcd09f3ae36e9c4c61)
pm_string.c
1 #include "prism/util/pm_string.h"
2 
9  return sizeof(pm_string_t);
10 }
11 
15 void
16 pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end) {
17  assert(start <= end);
18 
19  *string = (pm_string_t) {
20  .type = PM_STRING_SHARED,
21  .source = start,
22  .length = (size_t) (end - start)
23  };
24 }
25 
29 void
30 pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length) {
31  *string = (pm_string_t) {
32  .type = PM_STRING_OWNED,
33  .source = source,
34  .length = length
35  };
36 }
37 
41 void
42 pm_string_constant_init(pm_string_t *string, const char *source, size_t length) {
43  *string = (pm_string_t) {
44  .type = PM_STRING_CONSTANT,
45  .source = (const uint8_t *) source,
46  .length = length
47  };
48 }
49 
50 #ifdef _WIN32
55 typedef struct {
57  WCHAR *path;
58 
60  HANDLE file;
61 } pm_string_file_handle_t;
62 
68 pm_string_file_handle_open(pm_string_file_handle_t *handle, const char *filepath) {
69  int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
70  if (length == 0) return PM_STRING_INIT_ERROR_GENERIC;
71 
72  handle->path = xmalloc(sizeof(WCHAR) * ((size_t) length));
73  if ((handle->path == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, handle->path, length) == 0)) {
74  xfree(handle->path);
76  }
77 
78  handle->file = CreateFileW(handle->path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
79  if (handle->file == INVALID_HANDLE_VALUE) {
81 
82  if (GetLastError() == ERROR_ACCESS_DENIED) {
83  DWORD attributes = GetFileAttributesW(handle->path);
84  if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
86  }
87  }
88 
89  xfree(handle->path);
90  return result;
91  }
92 
94 }
95 
99 static void
100 pm_string_file_handle_close(pm_string_file_handle_t *handle) {
101  xfree(handle->path);
102  CloseHandle(handle->file);
103 }
104 #endif
105 
118 pm_string_mapped_init(pm_string_t *string, const char *filepath) {
119 #ifdef _WIN32
120  // Open the file for reading.
121  pm_string_file_handle_t handle;
122  pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
123  if (result != PM_STRING_INIT_SUCCESS) return result;
124 
125  // Get the file size.
126  DWORD file_size = GetFileSize(handle.file, NULL);
127  if (file_size == INVALID_FILE_SIZE) {
128  pm_string_file_handle_close(&handle);
130  }
131 
132  // If the file is empty, then we don't need to do anything else, we'll set
133  // the source to a constant empty string and return.
134  if (file_size == 0) {
135  pm_string_file_handle_close(&handle);
136  const uint8_t source[] = "";
137  *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
138  return PM_STRING_INIT_SUCCESS;
139  }
140 
141  // Create a mapping of the file.
142  HANDLE mapping = CreateFileMapping(handle.file, NULL, PAGE_READONLY, 0, 0, NULL);
143  if (mapping == NULL) {
144  pm_string_file_handle_close(&handle);
146  }
147 
148  // Map the file into memory.
149  uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
150  CloseHandle(mapping);
151  pm_string_file_handle_close(&handle);
152 
153  if (source == NULL) {
155  }
156 
157  *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
158  return PM_STRING_INIT_SUCCESS;
159 #elif defined(_POSIX_MAPPED_FILES)
160  // Open the file for reading
161  int fd = open(filepath, O_RDONLY);
162  if (fd == -1) {
164  }
165 
166  // Stat the file to get the file size
167  struct stat sb;
168  if (fstat(fd, &sb) == -1) {
169  close(fd);
171  }
172 
173  // Ensure it is a file and not a directory
174  if (S_ISDIR(sb.st_mode)) {
175  close(fd);
177  }
178 
179  // mmap the file descriptor to virtually get the contents
180  size_t size = (size_t) sb.st_size;
181  uint8_t *source = NULL;
182 
183  if (size == 0) {
184  close(fd);
185  const uint8_t source[] = "";
186  *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
187  return PM_STRING_INIT_SUCCESS;
188  }
189 
190  source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
191  if (source == MAP_FAILED) {
193  }
194 
195  close(fd);
196  *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
197  return PM_STRING_INIT_SUCCESS;
198 #else
199  return pm_string_file_init(string, filepath);
200 #endif
201 }
202 
209 pm_string_file_init(pm_string_t *string, const char *filepath) {
210 #ifdef _WIN32
211  // Open the file for reading.
212  pm_string_file_handle_t handle;
213  pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
214  if (result != PM_STRING_INIT_SUCCESS) return result;
215 
216  // Get the file size.
217  DWORD file_size = GetFileSize(handle.file, NULL);
218  if (file_size == INVALID_FILE_SIZE) {
219  pm_string_file_handle_close(&handle);
221  }
222 
223  // If the file is empty, then we don't need to do anything else, we'll set
224  // the source to a constant empty string and return.
225  if (file_size == 0) {
226  pm_string_file_handle_close(&handle);
227  const uint8_t source[] = "";
228  *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
229  return PM_STRING_INIT_SUCCESS;
230  }
231 
232  // Create a buffer to read the file into.
233  uint8_t *source = xmalloc(file_size);
234  if (source == NULL) {
235  pm_string_file_handle_close(&handle);
237  }
238 
239  // Read the contents of the file
240  DWORD bytes_read;
241  if (!ReadFile(handle.file, source, file_size, &bytes_read, NULL)) {
242  pm_string_file_handle_close(&handle);
244  }
245 
246  // Check the number of bytes read
247  if (bytes_read != file_size) {
248  xfree(source);
249  pm_string_file_handle_close(&handle);
251  }
252 
253  pm_string_file_handle_close(&handle);
254  *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = (size_t) file_size };
255  return PM_STRING_INIT_SUCCESS;
256 #elif defined(PRISM_HAS_FILESYSTEM)
257  // Open the file for reading
258  int fd = open(filepath, O_RDONLY);
259  if (fd == -1) {
261  }
262 
263  // Stat the file to get the file size
264  struct stat sb;
265  if (fstat(fd, &sb) == -1) {
266  close(fd);
268  }
269 
270  // Ensure it is a file and not a directory
271  if (S_ISDIR(sb.st_mode)) {
272  close(fd);
274  }
275 
276  // Check the size to see if it's empty
277  size_t size = (size_t) sb.st_size;
278  if (size == 0) {
279  close(fd);
280  const uint8_t source[] = "";
281  *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
282  return PM_STRING_INIT_SUCCESS;
283  }
284 
285  size_t length = (size_t) size;
286  uint8_t *source = xmalloc(length);
287  if (source == NULL) {
288  close(fd);
290  }
291 
292  long bytes_read = (long) read(fd, source, length);
293  close(fd);
294 
295  if (bytes_read == -1) {
296  xfree(source);
298  }
299 
300  *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
301  return PM_STRING_INIT_SUCCESS;
302 #else
303  (void) string;
304  (void) filepath;
305  perror("pm_string_file_init is not implemented for this platform");
307 #endif
308 }
309 
314 void
316  if (string->type == PM_STRING_OWNED) return;
317 
318  size_t length = pm_string_length(string);
319  const uint8_t *source = pm_string_source(string);
320 
321  uint8_t *memory = xmalloc(length);
322  if (!memory) return;
323 
324  pm_string_owned_init(string, memory, length);
325  memcpy((void *) string->source, source, length);
326 }
327 
334 int
335 pm_string_compare(const pm_string_t *left, const pm_string_t *right) {
336  size_t left_length = pm_string_length(left);
337  size_t right_length = pm_string_length(right);
338 
339  if (left_length < right_length) {
340  return -1;
341  } else if (left_length > right_length) {
342  return 1;
343  }
344 
345  return memcmp(pm_string_source(left), pm_string_source(right), left_length);
346 }
347 
353  return string->length;
354 }
355 
359 PRISM_EXPORTED_FUNCTION const uint8_t *
361  return string->source;
362 }
363 
369  void *memory = (void *) string->source;
370 
371  if (string->type == PM_STRING_OWNED) {
372  xfree(memory);
373 #ifdef PRISM_HAS_MMAP
374  } else if (string->type == PM_STRING_MAPPED && string->length) {
375 #if defined(_WIN32)
376  UnmapViewOfFile(memory);
377 #elif defined(_POSIX_MAPPED_FILES)
378  munmap(memory, string->length);
379 #endif
380 #endif /* PRISM_HAS_MMAP */
381  }
382 }
#define xfree
Old name of ruby_xfree.
Definition: xmalloc.h:58
#define xmalloc
Old name of ruby_xmalloc.
Definition: xmalloc.h:53
A generic string type that can have various ownership semantics.
PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string)
Returns the length associated with the string.
Definition: pm_string.c:352
void pm_string_ensure_owned(pm_string_t *string)
Ensure the string is owned.
Definition: pm_string.c:315
void pm_string_constant_init(pm_string_t *string, const char *source, size_t length)
Initialize a constant string that doesn't own its memory source.
Definition: pm_string.c:42
void pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length)
Initialize an owned string that is responsible for freeing allocated memory.
Definition: pm_string.c:30
pm_string_init_result_t
Represents the result of calling pm_string_mapped_init or pm_string_file_init.
Definition: pm_string.h:105
@ PM_STRING_INIT_SUCCESS
Indicates that the string was successfully initialized.
Definition: pm_string.h:107
@ PM_STRING_INIT_ERROR_GENERIC
Indicates a generic error from a string_*_init function, where the type of error should be read from ...
Definition: pm_string.h:112
@ PM_STRING_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
Definition: pm_string.h:116
void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end)
Initialize a shared string that is based on initial input.
Definition: pm_string.c:16
PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init(pm_string_t *string, const char *filepath)
Read the file indicated by the filepath parameter into source and load its contents and size into the...
Definition: pm_string.c:118
PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string)
Free the associated memory of the given string.
Definition: pm_string.c:368
PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void)
Returns the size of the pm_string_t struct.
Definition: pm_string.c:8
PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string)
Returns the start pointer associated with the string.
Definition: pm_string.c:360
int pm_string_compare(const pm_string_t *left, const pm_string_t *right)
Compare the underlying lengths and bytes of two strings.
Definition: pm_string.c:335
PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t *string, const char *filepath)
Read the file indicated by the filepath parameter into source and load its contents and size into the...
Definition: pm_string.c:209
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition: defines.h:50
A generic string type that can have various ownership semantics.
Definition: pm_string.h:33
const uint8_t * source
A pointer to the start of the string.
Definition: pm_string.h:35
enum pm_string_t::@94 type
The type of the string.
size_t length
The length of the string in bytes of memory.
Definition: pm_string.h:38