/*Gets copurchases then adds to hash*/ void addCopurchase(HashRef h, long bookId, ListRef * customers, int custNum){ BookRef list = getElement(h, bookId); if(list==NULL) list = insert(h, bookId, NULL); addToHash(h, bookId, customers, custNum, list);//add symmetrically, need for loop to add moveFirst(customers[custNum]); while(!offEnd(customers[custNum])){ long id = getCurrent(customers[custNum]); BookRef l = getElement(h, id); if(l==NULL) l = insert(h, id, NULL); addToHash(h, id, customers, custNum, l); if(atLast(customers[custNum])) break; moveNext(customers[custNum]); } }
HashSet<RefPtr<SecurityOrigin>> MediaPlayer::originsInMediaCache(const String& path) { HashSet<RefPtr<SecurityOrigin>> origins; for (auto& engine : installedMediaEngines()) { if (!engine.originsInMediaCache) continue; addToHash(origins, engine.originsInMediaCache(path)); } return origins; }
unsigned FontDescription::styleHashWithoutFamilyList() const { unsigned hash = 0; StringHasher stringHasher; const FontFeatureSettings* settings = featureSettings(); if (settings) { unsigned numFeatures = settings->size(); for (unsigned i = 0; i < numFeatures; ++i) { const AtomicString& tag = settings->at(i).tag(); for (unsigned j = 0; j < tag.length(); j++) stringHasher.addCharacter(tag[j]); addToHash(hash, settings->at(i).value()); } } const FontVariationSettings* varSettings = variationSettings(); if (varSettings) { unsigned numFeatures = varSettings->size(); for (unsigned i = 0; i < numFeatures; ++i) { const AtomicString& tag = varSettings->at(i).tag(); for (unsigned j = 0; j < tag.length(); j++) stringHasher.addCharacter(tag[j]); addToHash(hash, varSettings->at(i).value()); } } if (m_locale) { const AtomicString& locale = m_locale->localeString(); for (unsigned i = 0; i < locale.length(); i++) stringHasher.addCharacter(locale[i]); } addToHash(hash, stringHasher.hash()); addFloatToHash(hash, m_specifiedSize); addFloatToHash(hash, m_computedSize); addFloatToHash(hash, m_adjustedSize); addFloatToHash(hash, m_sizeAdjust); addFloatToHash(hash, m_letterSpacing); addFloatToHash(hash, m_wordSpacing); addToHash(hash, m_fieldsAsUnsigned.parts[0]); addToHash(hash, m_fieldsAsUnsigned.parts[1]); return hash; }
/* * Parses the Zip archive's Central Directory. Allocates and populates the * hash table. * * Returns 0 on success. */ static int parseZipArchive(ZipArchive* pArchive) { int result = -1; const u1* cdPtr = (const u1*)pArchive->mDirectoryMap.addr; size_t cdLength = pArchive->mDirectoryMap.length; int numEntries = pArchive->mNumEntries; /* * Create hash table. We have a minimum 75% load factor, possibly as * low as 50% after we round off to a power of 2. There must be at * least one unused entry to avoid an infinite loop during creation. */ pArchive->mHashTableSize = dexRoundUpPower2(1 + (numEntries * 4) / 3); pArchive->mHashTable = (ZipHashEntry*) calloc(pArchive->mHashTableSize, sizeof(ZipHashEntry)); /* * Walk through the central directory, adding entries to the hash * table and verifying values. */ const u1* ptr = cdPtr; int i; for (i = 0; i < numEntries; i++) { if (get4LE(ptr) != kCDESignature) { LOGW("Zip: missed a central dir sig (at %d)\n", i); goto bail; } if (ptr + kCDELen > cdPtr + cdLength) { LOGW("Zip: ran off the end (at %d)\n", i); goto bail; } long localHdrOffset = (long) get4LE(ptr + kCDELocalOffset); if (localHdrOffset >= pArchive->mDirectoryOffset) { LOGW("Zip: bad LFH offset %ld at entry %d\n", localHdrOffset, i); goto bail; } unsigned int fileNameLen, extraLen, commentLen, hash; fileNameLen = get2LE(ptr + kCDENameLen); extraLen = get2LE(ptr + kCDEExtraLen); commentLen = get2LE(ptr + kCDECommentLen); /* add the CDE filename to the hash table */ hash = computeHash((const char*)ptr + kCDELen, fileNameLen); addToHash(pArchive, (const char*)ptr + kCDELen, fileNameLen, hash); ptr += kCDELen + fileNameLen + extraLen + commentLen; if ((size_t)(ptr - cdPtr) > cdLength) { LOGW("Zip: bad CD advance (%d vs %zd) at entry %d\n", (int) (ptr - cdPtr), cdLength, i); goto bail; } } LOGV("+++ zip good scan %d entries\n", numEntries); result = 0; bail: return result; }
/* * Parse the Zip archive, verifying its contents and initializing internal * data structures. */ static bool parseZipArchive(ZipArchive* pArchive, const MemMapping* pMap) { #define CHECK_OFFSET(_off) { \ if ((unsigned int) (_off) >= maxOffset) { \ LOGE("ERROR: bad offset %u (max %d): %s\n", \ (unsigned int) (_off), maxOffset, #_off); \ goto bail; \ } \ } bool result = false; const unsigned char* basePtr = (const unsigned char*)pMap->addr; const unsigned char* ptr; size_t length = pMap->length; unsigned int i, numEntries, cdOffset; unsigned int val; /* * The first 4 bytes of the file will either be the local header * signature for the first file (kLFHSignature) or, if the archive doesn't * have any files in it, the end-of-central-directory signature * (kEOCDSignature). */ val = get4LE(basePtr); if (val == kEOCDSignature) { LOGI("Found Zip archive, but it looks empty\n"); goto bail; } else if (val != kLFHSignature) { LOGV("Not a Zip archive (found 0x%08x)\n", val); goto bail; } /* * Find the EOCD. We'll find it immediately unless they have a file * comment. */ ptr = basePtr + length - kEOCDLen; while (ptr >= basePtr) { if (*ptr == (kEOCDSignature & 0xff) && get4LE(ptr) == kEOCDSignature) break; ptr--; } if (ptr < basePtr) { LOGI("Could not find end-of-central-directory in Zip\n"); goto bail; } /* * There are two interesting items in the EOCD block: the number of * entries in the file, and the file offset of the start of the * central directory. * * (There's actually a count of the #of entries in this file, and for * all files which comprise a spanned archive, but for our purposes * we're only interested in the current file. Besides, we expect the * two to be equivalent for our stuff.) */ numEntries = get2LE(ptr + kEOCDNumEntries); cdOffset = get4LE(ptr + kEOCDFileOffset); /* valid offsets are [0,EOCD] */ unsigned int maxOffset; maxOffset = (ptr - basePtr) +1; LOGV("+++ numEntries=%d cdOffset=%d\n", numEntries, cdOffset); if (numEntries == 0 || cdOffset >= length) { LOGW("Invalid entries=%d offset=%d (len=%zd)\n", numEntries, cdOffset, length); goto bail; } /* * Create hash table. We have a minimum 75% load factor, possibly as * low as 50% after we round off to a power of 2. There must be at * least one unused entry to avoid an infinite loop during creation. */ pArchive->mNumEntries = numEntries; pArchive->mHashTableSize = dexRoundUpPower2(1 + (numEntries * 4) / 3); pArchive->mHashTable = (ZipHashEntry*) calloc(pArchive->mHashTableSize, sizeof(ZipHashEntry)); /* * Walk through the central directory, adding entries to the hash * table. */ ptr = basePtr + cdOffset; for (i = 0; i < numEntries; i++) { unsigned int fileNameLen, extraLen, commentLen, localHdrOffset; const unsigned char* localHdr; unsigned int hash; if (get4LE(ptr) != kCDESignature) { LOGW("Missed a central dir sig (at %d)\n", i); goto bail; } if (ptr + kCDELen > basePtr + length) { LOGW("Ran off the end (at %d)\n", i); goto bail; } localHdrOffset = get4LE(ptr + kCDELocalOffset); CHECK_OFFSET(localHdrOffset); fileNameLen = get2LE(ptr + kCDENameLen); extraLen = get2LE(ptr + kCDEExtraLen); commentLen = get2LE(ptr + kCDECommentLen); //LOGV("+++ %d: localHdr=%d fnl=%d el=%d cl=%d\n", // i, localHdrOffset, fileNameLen, extraLen, commentLen); //LOGV(" '%.*s'\n", fileNameLen, ptr + kCDELen); /* add the CDE filename to the hash table */ hash = computeHash((const char*)ptr + kCDELen, fileNameLen); addToHash(pArchive, (const char*)ptr + kCDELen, fileNameLen, hash); localHdr = basePtr + localHdrOffset; if (get4LE(localHdr) != kLFHSignature) { LOGW("Bad offset to local header: %d (at %d)\n", localHdrOffset, i); goto bail; } ptr += kCDELen + fileNameLen + extraLen + commentLen; CHECK_OFFSET(ptr - basePtr); } result = true; bail: return result; #undef CHECK_OFFSET }
int main(void) { HashTable t; initHashTable(&t); char a1[5] = "X"; char a2[5] = "xy"; char a3[5] = "xx"; char a4[5] = "a"; char a5[5] = "b"; char a6[5] = "A"; char a7[5] = "Z"; char a8[5] = "inc"; char a9[5] = "lsj"; char a10[5] = "sdf"; char a11[5] = "grx"; char a12[5] = "yfg"; char a13[5] = "dsf"; char a14[5] = "inc"; // notice a variable already exits with this name, // the variable with this name in the list will have the // newest value, i.e. old values are overwritten addToHash(&t, a1, 5.0); addToHash(&t, a2, 6.0); addToHash(&t, a3, 7.0); addToHash(&t, a4, 8.0); addToHash(&t, a5, 9.0); addToHash(&t, a6, 10.0); addToHash(&t, a7, 11.0); addToHash(&t, a8, 12.0); addToHash(&t, a9, 13.0); addToHash(&t, a10, 14.0); addToHash(&t, a11, 15.0); addToHash(&t, a12, 16.0); addToHash(&t, a13, 17.0); addToHash(&t, a14, 18.0); printf("HashTable:\n%s", tableToString(&t)); return 0; }
void addVariableToHash(HashTable* t, Variable v) { addToHash(t, v.name, v.value); }
static inline void addFloatToHash(unsigned& hash, float value) { addToHash(hash, StringHasher::hashMemory(&value, sizeof(value))); }
/* * Parses the Zip archive's Central Directory. Allocates and populates the * hash table. * * Returns 0 on success. */ static int parseZipArchive(ZipArchive* pArchive) { int result = -1; const u1* cdPtr = (const u1*)pArchive->mDirectoryMap.addr; size_t cdLength = pArchive->mDirectoryMap.length; int numEntries = pArchive->mNumEntries; /* * Create hash table. We have a minimum 75% load factor, possibly as * low as 50% after we round off to a power of 2. There must be at * least one unused entry to avoid an infinite loop during creation. */ pArchive->mHashTableSize = dexRoundUpPower2(1 + (numEntries * 4) / 3); pArchive->mHashTable = (ZipHashEntry*) calloc(pArchive->mHashTableSize, sizeof(ZipHashEntry)); /* * Walk through the central directory, adding entries to the hash * table and verifying values. */ const u1* ptr = cdPtr; int i; for (i = 0; i < numEntries; i++) { if (get4LE(ptr) != kCDESignature) { ALOGW("Zip: missed a central dir sig (at %d)", i); goto bail; } if (ptr + kCDELen > cdPtr + cdLength) { ALOGW("Zip: ran off the end (at %d)", i); goto bail; } long localHdrOffset = (long) get4LE(ptr + kCDELocalOffset); if (localHdrOffset >= pArchive->mDirectoryOffset) { ALOGW("Zip: bad LFH offset %ld at entry %d", localHdrOffset, i); goto bail; } unsigned int gpbf = get2LE(ptr + kCDEGPBFlags); if ((gpbf & kGPFUnsupportedMask) != 0) { ALOGW("Invalid General Purpose Bit Flag: %d", gpbf); goto bail; } unsigned int nameLen, extraLen, commentLen, hash; nameLen = get2LE(ptr + kCDENameLen); extraLen = get2LE(ptr + kCDEExtraLen); commentLen = get2LE(ptr + kCDECommentLen); const char *name = (const char *) ptr + kCDELen; /* Check name for NULL characters */ if (memchr(name, 0, nameLen) != NULL) { ALOGW("Filename contains NUL byte"); goto bail; } /* add the CDE filename to the hash table */ hash = computeHash(name, nameLen); addToHash(pArchive, name, nameLen, hash); /* We don't care about the comment or extra data. */ ptr += kCDELen + nameLen + extraLen + commentLen; if ((size_t)(ptr - cdPtr) > cdLength) { ALOGW("Zip: bad CD advance (%d vs %zd) at entry %d", (int) (ptr - cdPtr), cdLength, i); goto bail; } } ALOGV("+++ zip good scan %d entries", numEntries); result = 0; bail: return result; }