/************************************************************************** d i c t C r e a t e W o r d l i s t ** Create and initialize an anonymous wordlist **************************************************************************/ FICL_HASH *dictCreateWordlist(FICL_DICT *dp, int nBuckets) { FICL_HASH *pHash; dictAlign(dp); pHash = (FICL_HASH *)dp->here; dictAllot(dp, sizeof (FICL_HASH) + (nBuckets-1) * sizeof (FICL_WORD *)); pHash->size = nBuckets; hashReset(pHash); return pHash; }
/************************************************************************** d i c t E m p t y ** Empty the dictionary, reset its hash table, and reset its search order. ** Clears and (re-)creates the hash table with the size specified by nHash. **************************************************************************/ void dictEmpty(FICL_DICT *pDict, unsigned nHash) { FICL_HASH *pHash; pDict->here = pDict->dict; dictAlign(pDict); pHash = (FICL_HASH *)pDict->here; dictAllot(pDict, sizeof (FICL_HASH) + (nHash - 1) * sizeof (FICL_WORD *)); pHash->size = nHash; hashReset(pHash); pDict->pForthWords = pHash; pDict->smudge = NULL; dictResetSearchOrder(pDict); return; }
void hashCreate(void){ guint dSize = sizeof(HashData); hFree = hSize = (hashSize * 1048576)/dSize; data = (HashData*)g_malloc(hSize*dSize); hashReset(); }
int compress(FILE* inputFile, FILE* outputFile, int compressionLevel) { struct BitwiseBufferedFile* w = openBitwiseBufferedFile(NULL, 1, -1, outputFile); uint8_t readByte[LOCAL_BYTE_BUFFER_LENGTH]; size_t indexLength = INITIAL_INDEX_LENGTH; size_t bufferedBytes; int byteIndex = 0; struct LZ78HashTableEntry* hashTable; uint32_t childIndex = 257; uint32_t lookupIndex = ROOT_INDEX; uint32_t indexLengthMask = (1 << INITIAL_INDEX_LENGTH) - 1; uint32_t child; if((compressionLevel < MIN_COMPRESSION_LEVEL || compressionLevel > MAX_COMPRESSION_LEVEL) || inputFile == NULL || w == NULL) { errno = EINVAL; if(w != NULL) closeBitwiseBufferedFile(w); return -1; } uint32_t hashTableEntries = compressionParameters[compressionLevel - MIN_COMPRESSION_LEVEL].hashTableEntries; uint32_t moduloMask = hashTableEntries - 1; uint32_t maxChild = compressionParameters[compressionLevel - MIN_COMPRESSION_LEVEL].maxChild; hashTable = hashCreate(hashTableEntries, moduloMask); if(hashTable == NULL) { closeBitwiseBufferedFile(w); return -1; } if(writeBitBuffer(w, (uint32_t)compressionLevel, 3) == -1) goto exceptionHandler; while(!feof(inputFile) && !ferror(inputFile)) { bufferedBytes = fread(readByte, 1, LOCAL_BYTE_BUFFER_LENGTH, inputFile); for(byteIndex = 0; byteIndex < bufferedBytes; byteIndex++) { child = hashLookup(hashTable, lookupIndex, readByte[byteIndex], moduloMask); if(child != ROOT_INDEX) lookupIndex = child; //ROOT_INDEX means NOT FOUND else { if(writeBitBuffer(w, lookupIndex, indexLength) == -1 || hashInsert(hashTable, lookupIndex, readByte[byteIndex], childIndex, moduloMask) == -1) goto exceptionHandler; childIndex++; if((childIndex & indexLengthMask) == 0) //A power of 2 is reached { //The length of the transmitted index is incremented indexLength++; //The next power of 2 mask is set indexLengthMask = (indexLengthMask << 1) | 1; } //readByte value is also the right index to start with next time //because you have to start from the last character recognized lookupIndex = readByte[byteIndex] + 1; //ROOT_INDEX = 0 so ASCII characters are indexed in [1, 257] if (childIndex == maxChild) //hash table is full { if(hashReset(hashTable, hashTableEntries, moduloMask) == NULL) goto exceptionHandler; //hash table was not successfully created childIndex = FIRST_CHILD; //starts from the beginning indexLength = INITIAL_INDEX_LENGTH; indexLengthMask = (1 << INITIAL_INDEX_LENGTH) - 1; } } } } if(ferror(inputFile)) { errno = EBADFD; goto exceptionHandler; } if(writeBitBuffer(w, lookupIndex, indexLength) == -1 || writeBitBuffer(w, ROOT_INDEX, indexLength) == -1) goto exceptionHandler; hashDestroy(hashTable, hashTableEntries); return closeBitwiseBufferedFile(w); exceptionHandler: hashDestroy(hashTable, hashTableEntries); closeBitwiseBufferedFile(w); return -1; }