! FIO_createDictBuffer() : * creates a buffer, pointed by `*bufferPtr`, * loads `filename` content into it, up to DICTSIZE_MAX bytes. * @return : loaded size * if fileName==NULL, returns 0 and a NULL pointer */
| 695 | * if fileName==NULL, returns 0 and a NULL pointer |
| 696 | */ |
| 697 | static size_t FIO_createDictBuffer(void** bufferPtr, const char* fileName, FIO_prefs_t* const prefs) |
| 698 | { |
| 699 | FILE* fileHandle; |
| 700 | U64 fileSize; |
| 701 | |
| 702 | assert(bufferPtr != NULL); |
| 703 | *bufferPtr = NULL; |
| 704 | if (fileName == NULL) return 0; |
| 705 | |
| 706 | DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName); |
| 707 | fileHandle = fopen(fileName, "rb"); |
| 708 | if (fileHandle==NULL) EXM_THROW(31, "%s: %s", fileName, strerror(errno)); |
| 709 | |
| 710 | fileSize = UTIL_getFileSize(fileName); |
| 711 | { |
| 712 | size_t const dictSizeMax = prefs->patchFromMode ? prefs->memLimit : DICTSIZE_MAX; |
| 713 | if (fileSize > dictSizeMax) { |
| 714 | EXM_THROW(32, "Dictionary file %s is too large (> %u bytes)", |
| 715 | fileName, (unsigned)dictSizeMax); /* avoid extreme cases */ |
| 716 | } |
| 717 | } |
| 718 | *bufferPtr = malloc((size_t)fileSize); |
| 719 | if (*bufferPtr==NULL) EXM_THROW(34, "%s", strerror(errno)); |
| 720 | { size_t const readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle); |
| 721 | if (readSize != fileSize) |
| 722 | EXM_THROW(35, "Error reading dictionary file %s : %s", |
| 723 | fileName, strerror(errno)); |
| 724 | } |
| 725 | fclose(fileHandle); |
| 726 | return (size_t)fileSize; |
| 727 | } |
| 728 | |
| 729 | |
| 730 |
no test coverage detected