| 1 | #include "prism/util/pm_string.h" |
| 2 | |
| 3 | static const uint8_t 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_FUNCTION size_t |
| 10 | pm_string_sizeof(void) { |
| 11 | return sizeof(pm_string_t); |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Initialize a shared string that is based on initial input. |
| 16 | */ |
| 17 | void |
| 18 | pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end) { |
| 19 | assert(start <= end); |
| 20 | |
| 21 | *string = (pm_string_t) { |
| 22 | .type = PM_STRING_SHARED, |
| 23 | .source = start, |
| 24 | .length = (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_init(pm_string_t *string, uint8_t *source, size_t length) { |
| 33 | *string = (pm_string_t) { |
| 34 | .type = 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_init(pm_string_t *string, const char *source, size_t length) { |
| 45 | *string = (pm_string_t) { |
| 46 | .type = PM_STRING_CONSTANT, |
| 47 | .source = (const 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_FUNCTION pm_string_init_result_t |
| 120 | pm_string_mapped_init(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_FILES) |
| 161 | // Open the file for reading |
| 162 | int fd = open(filepath, O_RDONLY); |
| 163 | if (fd == -1) { |
| 164 | return PM_STRING_INIT_ERROR_GENERIC; |
| 165 | } |
| 166 | |
| 167 | // Stat the file to get the file size |
| 168 | struct stat sb; |
| 169 | if (fstat(fd, &sb) == -1) { |
| 170 | close(fd); |
| 171 | return PM_STRING_INIT_ERROR_GENERIC; |
| 172 | } |
| 173 | |
| 174 | // Ensure it is a file and not a directory |
| 175 | if (S_ISDIR(sb.st_mode)) { |
| 176 | close(fd); |
| 177 | return PM_STRING_INIT_ERROR_DIRECTORY; |
| 178 | } |
| 179 | |
| 180 | // mmap the file descriptor to virtually get the contents |
| 181 | size_t size = (size_t) sb.st_size; |
| 182 | uint8_t *source = NULL; |
| 183 | |
| 184 | if (size == 0) { |
| 185 | close(fd); |
| 186 | *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_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); |
| 193 | return PM_STRING_INIT_ERROR_GENERIC; |
| 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 | |
| 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_FUNCTION pm_string_init_result_t |
| 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); |
| 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_FILESYSTEM) |
| 257 | // Open the file for reading |
| 258 | int fd = open(filepath, O_RDONLY); |
| 259 | if (fd == -1) { |
| 260 | return PM_STRING_INIT_ERROR_GENERIC; |
| 261 | } |
| 262 | |
| 263 | // Stat the file to get the file size |
| 264 | struct stat sb; |
| 265 | if (fstat(fd, &sb) == -1) { |
| 266 | close(fd); |
| 267 | return PM_STRING_INIT_ERROR_GENERIC; |
| 268 | } |
| 269 | |
| 270 | // Ensure it is a file and not a directory |
| 271 | if (S_ISDIR(sb.st_mode)) { |
| 272 | close(fd); |
| 273 | return PM_STRING_INIT_ERROR_DIRECTORY; |
| 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 | *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 }; |
| 281 | return PM_STRING_INIT_SUCCESS; |
| 282 | } |
| 283 | |
| 284 | size_t length = (size_t) size; |
| 285 | uint8_t *source = xmalloc(length); |
| 286 | if (source == NULL) { |
| 287 | close(fd); |
| 288 | return PM_STRING_INIT_ERROR_GENERIC; |
| 289 | } |
| 290 | |
| 291 | long bytes_read = (long) read(fd, source, length); |
| 292 | close(fd); |
| 293 | |
| 294 | if (bytes_read == -1) { |
| 295 | xfree(source); |
| 296 | return PM_STRING_INIT_ERROR_GENERIC; |
| 297 | } |
| 298 | |
| 299 | *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length }; |
| 300 | return 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_owned(pm_string_t *string) { |
| 315 | if (string->type == PM_STRING_OWNED) return; |
| 316 | |
| 317 | size_t length = pm_string_length(string); |
| 318 | const uint8_t *source = pm_string_source(string); |
| 319 | |
| 320 | uint8_t *memory = xmalloc(length); |
| 321 | if (!memory) return; |
| 322 | |
| 323 | pm_string_owned_init(string, memory, length); |
| 324 | memcpy((void *) string->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_compare(const pm_string_t *left, const pm_string_t *right) { |
| 335 | size_t left_length = pm_string_length(left); |
| 336 | size_t right_length = pm_string_length(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 memcmp(pm_string_source(left), pm_string_source(right), left_length); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Returns the length associated with the string. |
| 349 | */ |
| 350 | PRISM_EXPORTED_FUNCTION size_t |
| 351 | pm_string_length(const pm_string_t *string) { |
| 352 | return string->length; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Returns the start pointer associated with the string. |
| 357 | */ |
| 358 | PRISM_EXPORTED_FUNCTION const uint8_t * |
| 359 | pm_string_source(const pm_string_t *string) { |
| 360 | return string->source; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Free the associated memory of the given string. |
| 365 | */ |
| 366 | PRISM_EXPORTED_FUNCTION void |
| 367 | pm_string_free(pm_string_t *string) { |
| 368 | void *memory = (void *) string->source; |
| 369 | |
| 370 | if (string->type == PM_STRING_OWNED) { |
| 371 | xfree(memory); |
| 372 | #ifdef PRISM_HAS_MMAP |
| 373 | } else if (string->type == PM_STRING_MAPPED && string->length) { |
| 374 | #if defined(_WIN32) |
| 375 | UnmapViewOfFile(memory); |
| 376 | #elif defined(_POSIX_MAPPED_FILES) |
| 377 | munmap(memory, string->length); |
| 378 | #endif |
| 379 | #endif /* PRISM_HAS_MMAP */ |
| 380 | } |
| 381 | } |