Debian Codegraph @

src/util/pm_string.c

raw symbols SCIP
1 #cxx . . $ `<file>/src/util/pm_string.c`/include "prism/util/pm_string.h"cxx . . $ `<file>/include/prism/util/pm_string.h`/
2
3 static const uint8_tcxx . . $ uint8_t# empty_sourcecxx . . $ empty_source.[] = "";
4
5 /**
6 * Returns the size of the pm_string_t struct. This is necessary to allocate the
7 * correct amount of memory in the FFI backend.
8 */
9 PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! size_tcxx . . $ size_t#
10 pm_string_sizeofcxx . . $ pm_string_sizeof(f708dce9a8856c70).(void) {
11 return sizeof(pm_string_tcxx . . $ pm_string_t#);
12 }
13
14 /**
15 * Initialize a shared string that is based on initial input.
16 */
17 void
18 pm_string_shared_initcxx . . $ pm_string_shared_init(3931a8f5f74a1622).(pm_string_tcxx . . $ pm_string_t# *string, const uint8_tcxx . . $ uint8_t# *start, const uint8_tcxx . . $ uint8_t# *end) {
19 assertcxx . . $ `<external>/4398592474888995393/assert.h:108:11`!(start <= end);
20
21 *string = (pm_string_tcxx . . $ pm_string_t#) {
22 .type = PM_STRING_SHAREDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_SHARED.,
23 .source = start,
24 .length = (size_tcxx . . $ size_t#) (end - start)
25 };
26 }
27
28 /**
29 * Initialize an owned string that is responsible for freeing allocated memory.
30 */
31 void
32 pm_string_owned_initcxx . . $ pm_string_owned_init(15e507396f0067cc).(pm_string_tcxx . . $ pm_string_t# *string, uint8_tcxx . . $ uint8_t# *source, size_tcxx . . $ size_t# length) {
33 *string = (pm_string_tcxx . . $ pm_string_t#) {
34 .type = PM_STRING_OWNEDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_OWNED.,
35 .source = source,
36 .length = length
37 };
38 }
39
40 /**
41 * Initialize a constant string that doesn't own its memory source.
42 */
43 void
44 pm_string_constant_initcxx . . $ pm_string_constant_init(501226d9fb5d86b1).(pm_string_tcxx . . $ pm_string_t# *string, const char *source, size_tcxx . . $ size_t# length) {
45 *string = (pm_string_tcxx . . $ pm_string_t#) {
46 .type = PM_STRING_CONSTANTcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_CONSTANT.,
47 .source = (const uint8_tcxx . . $ uint8_t# *) source,
48 .length = length
49 };
50 }
51
52 #ifdef _WIN32
53 /**
54 * Represents a file handle on Windows, where the path will need to be freed
55 * when the file is closed.
56 */
57 typedef struct {
58 /** The path to the file, which will become allocated memory. */
59 WCHAR *path;
60
61 /** The handle to the file, which will start as uninitialized memory. */
62 HANDLE file;
63 } pm_string_file_handle_t;
64
65 /**
66 * Open the file indicated by the filepath parameter for reading on Windows.
67 * Perform any kind of normalization that needs to happen on the filepath.
68 */
69 static pm_string_init_result_t
70 pm_string_file_handle_open(pm_string_file_handle_t *handle, const char *filepath) {
71 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
72 if (length == 0) return PM_STRING_INIT_ERROR_GENERIC;
73
74 handle->path = xmalloc(sizeof(WCHAR) * ((size_t) length));
75 if ((handle->path == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, handle->path, length) == 0)) {
76 xfree(handle->path);
77 return PM_STRING_INIT_ERROR_GENERIC;
78 }
79
80 handle->file = CreateFileW(handle->path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
81 if (handle->file == INVALID_HANDLE_VALUE) {
82 pm_string_init_result_t result = PM_STRING_INIT_ERROR_GENERIC;
83
84 if (GetLastError() == ERROR_ACCESS_DENIED) {
85 DWORD attributes = GetFileAttributesW(handle->path);
86 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
87 result = PM_STRING_INIT_ERROR_DIRECTORY;
88 }
89 }
90
91 xfree(handle->path);
92 return result;
93 }
94
95 return PM_STRING_INIT_SUCCESS;
96 }
97
98 /**
99 * Close the file handle and free the path.
100 */
101 static void
102 pm_string_file_handle_close(pm_string_file_handle_t *handle) {
103 xfree(handle->path);
104 CloseHandle(handle->file);
105 }
106 #endif
107
108 /**
109 * Read the file indicated by the filepath parameter into source and load its
110 * contents and size into the given `pm_string_t`. The given `pm_string_t`
111 * should be freed using `pm_string_free` when it is no longer used.
112 *
113 * We want to use demand paging as much as possible in order to avoid having to
114 * read the entire file into memory (which could be detrimental to performance
115 * for large files). This means that if we're on windows we'll use
116 * `MapViewOfFile`, on POSIX systems that have access to `mmap` we'll use
117 * `mmap`, and on other POSIX systems we'll use `read`.
118 */
119 PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! pm_string_init_result_tcxx . . $ pm_string_init_result_t#
120 pm_string_mapped_initcxx . . $ pm_string_mapped_init(16ac81ad46c85296).(pm_string_tcxx . . $ pm_string_t# *string, const char *filepath) {
121 #ifdef _WIN32
122 // Open the file for reading.
123 pm_string_file_handle_t handle;
124 pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
125 if (result != PM_STRING_INIT_SUCCESS) return result;
126
127 // Get the file size.
128 DWORD file_size = GetFileSize(handle.file, NULL);
129 if (file_size == INVALID_FILE_SIZE) {
130 pm_string_file_handle_close(&handle);
131 return PM_STRING_INIT_ERROR_GENERIC;
132 }
133
134 // If the file is empty, then we don't need to do anything else, we'll set
135 // the source to a constant empty string and return.
136 if (file_size == 0) {
137 pm_string_file_handle_close(&handle);
138 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
139 return PM_STRING_INIT_SUCCESS;
140 }
141
142 // Create a mapping of the file.
143 HANDLE mapping = CreateFileMapping(handle.file, NULL, PAGE_READONLY, 0, 0, NULL);
144 if (mapping == NULL) {
145 pm_string_file_handle_close(&handle);
146 return PM_STRING_INIT_ERROR_GENERIC;
147 }
148
149 // Map the file into memory.
150 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
151 CloseHandle(mapping);
152 pm_string_file_handle_close(&handle);
153
154 if (source == NULL) {
155 return PM_STRING_INIT_ERROR_GENERIC;
156 }
157
158 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
159 return PM_STRING_INIT_SUCCESS;
160 #elif defined(_POSIX_MAPPED_FILEScxx . . $ `<external>/4533468968537266721/posix_opt.h:41:9`!)
161 // Open the file for reading
162 int fd = opencxx . . $ open(e156e3aa1e5c4ad1).(filepath, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
163 if (fd == -1) {
164 return PM_STRING_INIT_ERROR_GENERICcxx . . $ PM_STRING_INIT_ERROR_GENERIC.;
165 }
166
167 // Stat the file to get the file size
168 struct statcxx . . $ stat# sb;
169 if (fstatcxx . . $ fstat(a43660ce2f600cb5).(fd, &sb) == -1) {
170 closecxx . . $ close(7864480464b09eea).(fd);
171 return PM_STRING_INIT_ERROR_GENERICcxx . . $ PM_STRING_INIT_ERROR_GENERIC.;
172 }
173
174 // Ensure it is a file and not a directory
175 if (S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`!(sb.st_mode)) {
176 closecxx . . $ close(7864480464b09eea).(fd);
177 return PM_STRING_INIT_ERROR_DIRECTORYcxx . . $ PM_STRING_INIT_ERROR_DIRECTORY.;
178 }
179
180 // mmap the file descriptor to virtually get the contents
181 size_tcxx . . $ size_t# size = (size_tcxx . . $ size_t#) sb.st_sizecxx . . $ stat#st_size.;
182 uint8_tcxx . . $ uint8_t# *source = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
183
184 if (size == 0) {
185 closecxx . . $ close(7864480464b09eea).(fd);
186 *string = (pm_string_tcxx . . $ pm_string_t#) { .type = PM_STRING_CONSTANTcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_CONSTANT., .source = empty_sourcecxx . . $ empty_source., .length = 0 };
187 return PM_STRING_INIT_SUCCESScxx . . $ PM_STRING_INIT_SUCCESS.;
188 }
189
190 source = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, size, PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!, MAP_PRIVATEcxx . . $ `<external>/17226767460805440860/mman-linux.h:43:9`!, fd, 0);
191 if (source == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) {
192 closecxx . . $ close(7864480464b09eea).(fd);
193 return PM_STRING_INIT_ERROR_GENERICcxx . . $ PM_STRING_INIT_ERROR_GENERIC.;
194 }
195
196 closecxx . . $ close(7864480464b09eea).(fd);
197 *string = (pm_string_tcxx . . $ pm_string_t#) { .type = PM_STRING_MAPPEDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_MAPPED., .source = source, .length = size };
198 return PM_STRING_INIT_SUCCESScxx . . $ PM_STRING_INIT_SUCCESS.;
199 #else
200 return pm_string_file_init(string, filepath);
201 #endif
202 }
203
204 /**
205 * Read the file indicated by the filepath parameter into source and load its
206 * contents and size into the given `pm_string_t`. The given `pm_string_t`
207 * should be freed using `pm_string_free` when it is no longer used.
208 */
209 PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! pm_string_init_result_tcxx . . $ pm_string_init_result_t#
210 pm_string_file_initcxx . . $ pm_string_file_init(16ac81ad46c85296).(pm_string_tcxx . . $ 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);
221 return PM_STRING_INIT_ERROR_GENERIC;
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 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_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);
236 return PM_STRING_INIT_ERROR_GENERIC;
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);
243 return PM_STRING_INIT_ERROR_GENERIC;
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);
250 return PM_STRING_INIT_ERROR_GENERIC;
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_FILESYSTEMcxx . . $ `include/prism/defines.h:139:12`!)
257 // Open the file for reading
258 int fd = opencxx . . $ open(e156e3aa1e5c4ad1).(filepath, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
259 if (fd == -1) {
260 return PM_STRING_INIT_ERROR_GENERICcxx . . $ PM_STRING_INIT_ERROR_GENERIC.;
261 }
262
263 // Stat the file to get the file size
264 struct statcxx . . $ stat# sb;
265 if (fstatcxx . . $ fstat(a43660ce2f600cb5).(fd, &sb) == -1) {
266 closecxx . . $ close(7864480464b09eea).(fd);
267 return PM_STRING_INIT_ERROR_GENERICcxx . . $ PM_STRING_INIT_ERROR_GENERIC.;
268 }
269
270 // Ensure it is a file and not a directory
271 if (S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`!(sb.st_mode)) {
272 closecxx . . $ close(7864480464b09eea).(fd);
273 return PM_STRING_INIT_ERROR_DIRECTORYcxx . . $ PM_STRING_INIT_ERROR_DIRECTORY.;
274 }
275
276 // Check the size to see if it's empty
277 size_tcxx . . $ size_t# size = (size_tcxx . . $ size_t#) sb.st_sizecxx . . $ stat#st_size.;
278 if (size == 0) {
279 closecxx . . $ close(7864480464b09eea).(fd);
280 *string = (pm_string_tcxx . . $ pm_string_t#) { .type = PM_STRING_CONSTANTcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_CONSTANT., .source = empty_sourcecxx . . $ empty_source., .length = 0 };
281 return PM_STRING_INIT_SUCCESScxx . . $ PM_STRING_INIT_SUCCESS.;
282 }
283
284 size_tcxx . . $ size_t# length = (size_tcxx . . $ size_t#) size;
285 uint8_tcxx . . $ uint8_t# *source = xmalloccxx . . $ `include/prism/defines.h:179:17`!(length);
286 if (source == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
287 closecxx . . $ close(7864480464b09eea).(fd);
288 return PM_STRING_INIT_ERROR_GENERICcxx . . $ PM_STRING_INIT_ERROR_GENERIC.;
289 }
290
291 long bytes_read = (long) readcxx . . $ read(7e34d0a36f355389).(fd, source, length);
292 closecxx . . $ close(7864480464b09eea).(fd);
293
294 if (bytes_read == -1) {
295 xfreecxx . . $ `include/prism/defines.h:203:17`!(source);
296 return PM_STRING_INIT_ERROR_GENERICcxx . . $ PM_STRING_INIT_ERROR_GENERIC.;
297 }
298
299 *string = (pm_string_tcxx . . $ pm_string_t#) { .type = PM_STRING_OWNEDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_OWNED., .source = source, .length = length };
300 return PM_STRING_INIT_SUCCESScxx . . $ PM_STRING_INIT_SUCCESS.;
301 #else
302 (void) string;
303 (void) filepath;
304 perror("pm_string_file_init is not implemented for this platform");
305 return PM_STRING_INIT_ERROR_GENERIC;
306 #endif
307 }
308
309 /**
310 * Ensure the string is owned. If it is not, then reinitialize it as owned and
311 * copy over the previous source.
312 */
313 void
314 pm_string_ensure_ownedcxx . . $ pm_string_ensure_owned(c15d128c68b0cb72).(pm_string_tcxx . . $ pm_string_t# *string) {
315 if (string->typecxx . . $ $anonymous_type_cbb08f63caf075c2_0#type. == PM_STRING_OWNEDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_OWNED.) return;
316
317 size_tcxx . . $ size_t# length = pm_string_lengthcxx . . $ pm_string_length(576a13ceb66f8a15).(string);
318 const uint8_tcxx . . $ uint8_t# *source = pm_string_sourcecxx . . $ pm_string_source(37c9bd456e9b3712).(string);
319
320 uint8_tcxx . . $ uint8_t# *memory = xmalloccxx . . $ `include/prism/defines.h:179:17`!(length);
321 if (!memory) return;
322
323 pm_string_owned_initcxx . . $ pm_string_owned_init(15e507396f0067cc).(string, memory, length);
324 memcpycxx . . $ memcpy(cc817cb175f282d).((void *) string->sourcecxx . . $ $anonymous_type_cbb08f63caf075c2_0#source., source, length);
325 }
326
327 /**
328 * Compare the underlying lengths and bytes of two strings. Returns 0 if the
329 * strings are equal, a negative number if the left string is less than the
330 * right string, and a positive number if the left string is greater than the
331 * right string.
332 */
333 int
334 pm_string_comparecxx . . $ pm_string_compare(89247a2b4ba54e22).(const pm_string_tcxx . . $ pm_string_t# *left, const pm_string_tcxx . . $ pm_string_t# *right) {
335 size_tcxx . . $ size_t# left_length = pm_string_lengthcxx . . $ pm_string_length(576a13ceb66f8a15).(left);
336 size_tcxx . . $ size_t# right_length = pm_string_lengthcxx . . $ pm_string_length(576a13ceb66f8a15).(right);
337
338 if (left_length < right_length) {
339 return -1;
340 } else if (left_length > right_length) {
341 return 1;
342 }
343
344 return memcmpcxx . . $ memcmp(36f949851efa3055).(pm_string_sourcecxx . . $ pm_string_source(37c9bd456e9b3712).(left), pm_string_sourcecxx . . $ pm_string_source(37c9bd456e9b3712).(right), left_length);
345 }
346
347 /**
348 * Returns the length associated with the string.
349 */
350 PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! size_tcxx . . $ size_t#
351 pm_string_lengthcxx . . $ pm_string_length(576a13ceb66f8a15).(const pm_string_tcxx . . $ pm_string_t# *string) {
352 return string->lengthcxx . . $ $anonymous_type_cbb08f63caf075c2_0#length.;
353 }
354
355 /**
356 * Returns the start pointer associated with the string.
357 */
358 PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! const uint8_tcxx . . $ uint8_t# *
359 pm_string_sourcecxx . . $ pm_string_source(37c9bd456e9b3712).(const pm_string_tcxx . . $ pm_string_t# *string) {
360 return string->sourcecxx . . $ $anonymous_type_cbb08f63caf075c2_0#source.;
361 }
362
363 /**
364 * Free the associated memory of the given string.
365 */
366 PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! void
367 pm_string_freecxx . . $ pm_string_free(c15d128c68b0cb72).(pm_string_tcxx . . $ pm_string_t# *string) {
368 void *memory = (void *) string->sourcecxx . . $ $anonymous_type_cbb08f63caf075c2_0#source.;
369
370 if (string->typecxx . . $ $anonymous_type_cbb08f63caf075c2_0#type. == PM_STRING_OWNEDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_OWNED.) {
371 xfreecxx . . $ `include/prism/defines.h:203:17`!(memory);
372 #ifdef PRISM_HAS_MMAPcxx . . $ `include/prism/defines.h:129:16`!
373 } else if (string->typecxx . . $ $anonymous_type_cbb08f63caf075c2_0#type. == PM_STRING_MAPPEDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_MAPPED. && string->lengthcxx . . $ $anonymous_type_cbb08f63caf075c2_0#length.) {
374 #if defined(_WIN32)
375 UnmapViewOfFile(memory);
376 #elif defined(_POSIX_MAPPED_FILEScxx . . $ `<external>/4533468968537266721/posix_opt.h:41:9`!)
377 munmapcxx . . $ munmap(c0a5f96701d3f89).(memory, string->lengthcxx . . $ $anonymous_type_cbb08f63caf075c2_0#length.);
378 #endif
379 #endif /* PRISM_HAS_MMAP */
380 }
381 }