Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
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) {
192  close(fd);
194  }
195 
196  close(fd);
197  *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
198  return PM_STRING_INIT_SUCCESS;
199 #else
200  return pm_string_file_init(string, filepath);
201 #endif
202 }
203 
210 pm_string_file_init(pm_string_t *string, const char *filepath) {
211 #ifdef _WIN32
212  // Open the file for reading.
213  pm_string_file_handle_t handle;
214  pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
215  if (result != PM_STRING_INIT_SUCCESS) return result;
216 
217  // Get the file size.
218  DWORD file_size = GetFileSize(handle.file, NULL);
219  if (file_size == INVALID_FILE_SIZE) {
220  pm_string_file_handle_close(&handle);
222  }
223 
224  // If the file is empty, then we don't need to do anything else, we'll set
225  // the source to a constant empty string and return.
226  if (file_size == 0) {
227  pm_string_file_handle_close(&handle);
228  const uint8_t source[] = "";
229  *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
230  return PM_STRING_INIT_SUCCESS;
231  }
232 
233  // Create a buffer to read the file into.
234  uint8_t *source = xmalloc(file_size);
235  if (source == NULL) {
236  pm_string_file_handle_close(&handle);
238  }
239 
240  // Read the contents of the file
241  DWORD bytes_read;
242  if (!ReadFile(handle.file, source, file_size, &bytes_read, NULL)) {
243  pm_string_file_handle_close(&handle);
245  }
246 
247  // Check the number of bytes read
248  if (bytes_read != file_size) {
249  xfree(source);
250  pm_string_file_handle_close(&handle);
252  }
253 
254  pm_string_file_handle_close(&handle);
255  *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = (size_t) file_size };
256  return PM_STRING_INIT_SUCCESS;
257 #elif defined(PRISM_HAS_FILESYSTEM)
258  // Open the file for reading
259  int fd = open(filepath, O_RDONLY);
260  if (fd == -1) {
262  }
263 
264  // Stat the file to get the file size
265  struct stat sb;
266  if (fstat(fd, &sb) == -1) {
267  close(fd);
269  }
270 
271  // Ensure it is a file and not a directory
272  if (S_ISDIR(sb.st_mode)) {
273  close(fd);
275  }
276 
277  // Check the size to see if it's empty
278  size_t size = (size_t) sb.st_size;
279  if (size == 0) {
280  close(fd);
281  const uint8_t source[] = "";
282  *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
283  return PM_STRING_INIT_SUCCESS;
284  }
285 
286  size_t length = (size_t) size;
287  uint8_t *source = xmalloc(length);
288  if (source == NULL) {
289  close(fd);
291  }
292 
293  long bytes_read = (long) read(fd, source, length);
294  close(fd);
295 
296  if (bytes_read == -1) {
297  xfree(source);
299  }
300 
301  *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
302  return PM_STRING_INIT_SUCCESS;
303 #else
304  (void) string;
305  (void) filepath;
306  perror("pm_string_file_init is not implemented for this platform");
308 #endif
309 }
310 
315 void
317  if (string->type == PM_STRING_OWNED) return;
318 
319  size_t length = pm_string_length(string);
320  const uint8_t *source = pm_string_source(string);
321 
322  uint8_t *memory = xmalloc(length);
323  if (!memory) return;
324 
325  pm_string_owned_init(string, memory, length);
326  memcpy((void *) string->source, source, length);
327 }
328 
335 int
336 pm_string_compare(const pm_string_t *left, const pm_string_t *right) {
337  size_t left_length = pm_string_length(left);
338  size_t right_length = pm_string_length(right);
339 
340  if (left_length < right_length) {
341  return -1;
342  } else if (left_length > right_length) {
343  return 1;
344  }
345 
346  return memcmp(pm_string_source(left), pm_string_source(right), left_length);
347 }
348 
354  return string->length;
355 }
356 
360 PRISM_EXPORTED_FUNCTION const uint8_t *
362  return string->source;
363 }
364 
370  void *memory = (void *) string->source;
371 
372  if (string->type == PM_STRING_OWNED) {
373  xfree(memory);
374 #ifdef PRISM_HAS_MMAP
375  } else if (string->type == PM_STRING_MAPPED && string->length) {
376 #if defined(_WIN32)
377  UnmapViewOfFile(memory);
378 #elif defined(_POSIX_MAPPED_FILES)
379  munmap(memory, string->length);
380 #endif
381 #endif /* PRISM_HAS_MMAP */
382  }
383 }
#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:353
void pm_string_ensure_owned(pm_string_t *string)
Ensure the string is owned.
Definition: pm_string.c:316
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:369
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:361
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:336
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:210
#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
size_t length
The length of the string in bytes of memory.
Definition: pm_string.h:38
enum pm_string_t::@98 type
The type of the string.