PUBLIC int websOpenAuth(int minimal) { char sbuf[64]; assert(minimal == 0 || minimal == 1); if ((users = hashCreate(-1)) < 0) { return -1; } if ((roles = hashCreate(-1)) < 0) { return -1; } if (!minimal) { fmt(sbuf, sizeof(sbuf), "%x:%x", rand(), OSTimeGet()); secret = websMD5(sbuf); #if ME_GOAHEAD_JAVASCRIPT && FUTURE websJsDefine("can", jsCan); #endif websDefineAction("login", loginServiceProc); websDefineAction("logout", logoutServiceProc); } if (smatch(ME_GOAHEAD_AUTH_STORE, "file")) { verifyPassword = websVerifyPasswordFromFile; #if ME_COMPILER_HAS_PAM } else if (smatch(ME_GOAHEAD_AUTH_STORE, "pam")) { verifyPassword = websVerifyPasswordFromPam; #endif } return 0; }
extern BREthereumStatus ewmAnnounceTransaction(BREthereumEWM ewm, int id, const char *hashString, const char *from, const char *to, const char *contract, const char *strAmount, // value const char *strGasLimit, const char *strGasPrice, const char *data, const char *strNonce, const char *strGasUsed, const char *strBlockNumber, const char *strBlockHash, const char *strBlockConfirmations, const char *strBlockTransactionIndex, const char *strBlockTimestamp, const char *isError) { BRCoreParseStatus parseStatus; BREthereumEWMClientAnnounceTransactionBundle *bundle = malloc(sizeof (BREthereumEWMClientAnnounceTransactionBundle)); bundle->hash = hashCreate(hashString); bundle->from = addressCreate(from); bundle->to = addressCreate(to); bundle->contract = (NULL == contract || '\0' == contract[0] ? EMPTY_ADDRESS_INIT : addressCreate(contract)); bundle->amount = createUInt256Parse(strAmount, 0, &parseStatus); bundle->gasLimit = strtoull(strGasLimit, NULL, 0); bundle->gasPrice = createUInt256Parse(strGasPrice, 0, &parseStatus); bundle->data = strdup(data); bundle->nonce = strtoull(strNonce, NULL, 0); // TODO: Assumes `nonce` is uint64_t; which it is for now bundle->gasUsed = strtoull(strGasUsed, NULL, 0); bundle->blockNumber = strtoull(strBlockNumber, NULL, 0); bundle->blockHash = hashCreate (strBlockHash); bundle->blockConfirmations = strtoull(strBlockConfirmations, NULL, 0); bundle->blockTransactionIndex = (unsigned int) strtoul(strBlockTransactionIndex, NULL, 0); bundle->blockTimestamp = strtoull(strBlockTimestamp, NULL, 0); bundle->isError = AS_ETHEREUM_BOOLEAN(0 != strcmp (isError, "0")); ewmSignalAnnounceTransaction(ewm, bundle, id); return SUCCESS; }
/************************************************************** * * initCmdParser - Initialize the command hash table. * * This routine initializes the command hash table. * * RETURNS: * OK , or ERROR on error. * * Author Greg Brissey 8/4/94 */ int initCmdParser() { cmd *table; int i, size; /* size = sizeof(table); */ size = sizeOfCmdTable(); cmdtbl = hashCreate(size, NULL, NULL, "Cmd Hash Table"); if (cmdtbl == NULL) errLogSysQuit(LOGOPT,debugInfo,"initCmdParser: hashCreate() failed"); i = 0; table = addrOfCmdTable(); while(table[i].n) { hashPut(cmdtbl, table[i].n, (char *) table[i].f); i++; } #ifdef DEBUG if (DebugLevel >= 3) hashShow(cmdtbl,2); #endif return(0); }
PUBLIC int websJstOpen() { websJstFunctions = hashCreate(WEBS_HASH_INIT * 2); websDefineJst("write", websJstWrite); websDefineHandler("jst", 0, jstHandler, closeJst, 0); return 0; }
static void computeUserAbilities(WebsUser *user) { char *ability, *roles, *tok; assure(user); if ((user->abilities = hashCreate(-1)) == 0) { return; } roles = sclone(user->roles); for (ability = stok(roles, " \t,", &tok); ability; ability = stok(NULL, " \t,", &tok)) { computeAbilities(user->abilities, ability, 0); } #if BIT_DEBUG { WebsKey *key; trace(5, "User \"%s\" has abilities: ", user->name); for (key = hashFirst(user->abilities); key; key = hashNext(user->abilities, key)) { trace(5, "%s ", key->name.value.string); ability = key->name.value.string; } trace(5, "\n"); } #endif wfree(roles); }
extern BREthereumStatus ewmAnnounceLog (BREthereumEWM ewm, int id, const char *strHash, const char *strContract, int topicCount, const char **arrayTopics, const char *strData, const char *strGasPrice, const char *strGasUsed, const char *strLogIndex, const char *strBlockNumber, const char *strBlockTransactionIndex, const char *strBlockTimestamp) { BRCoreParseStatus parseStatus; BREthereumEWMClientAnnounceLogBundle *bundle = malloc(sizeof (BREthereumEWMClientAnnounceLogBundle)); bundle->hash = hashCreate(strHash); bundle->contract = addressCreate(strContract); bundle->topicCount = topicCount; bundle->arrayTopics = calloc (topicCount, sizeof (char *)); for (int i = 0; i < topicCount; i++) bundle->arrayTopics[i] = strdup (arrayTopics[i]); bundle->data = strdup (strData); bundle->gasPrice = createUInt256Parse(strGasPrice, 0, &parseStatus); bundle->gasUsed = strtoull(strGasUsed, NULL, 0); bundle->logIndex = strtoull(strLogIndex, NULL, 0); bundle->blockNumber = strtoull(strBlockNumber, NULL, 0); bundle->blockTransactionIndex = strtoull(strBlockTransactionIndex, NULL, 0); bundle->blockTimestamp = strtoull(strBlockTimestamp, NULL, 0); ewmSignalAnnounceLog(ewm, bundle, id); return SUCCESS; }
PUBLIC int websTimeOpen() { TimeToken *tt; timeTokens = hashCreate(59); for (tt = days; tt->name; tt++) { hashEnter(timeTokens, tt->name, valueSymbol(tt), 0); } for (tt = fullDays; tt->name; tt++) { hashEnter(timeTokens, tt->name, valueSymbol(tt), 0); } for (tt = months; tt->name; tt++) { hashEnter(timeTokens, tt->name, valueSymbol(tt), 0); } for (tt = fullMonths; tt->name; tt++) { hashEnter(timeTokens, tt->name, valueSymbol(tt), 0); } for (tt = ampm; tt->name; tt++) { hashEnter(timeTokens, tt->name, valueSymbol(tt), 0); } for (tt = zones; tt->name; tt++) { hashEnter(timeTokens, tt->name, valueSymbol(tt), 0); } for (tt = offsets; tt->name; tt++) { hashEnter(timeTokens, tt->name, valueSymbol(tt), 0); } return 0; }
bool Preferences_init(Preferences * self) { call_super(init, self); self->private_ivar(hashTable) = hashCreate(); self->eventDispatcher = EventDispatcher_create(); self->dispose = Preferences_dispose; return true; }
PUBLIC int websOpenAuth(int minimal) { char sbuf[64]; assure(minimal == 0 || minimal == 1); if ((users = hashCreate(-1)) < 0) { return -1; } if ((roles = hashCreate(-1)) < 0) { return -1; } if (!minimal) { fmt(sbuf, sizeof(sbuf), "%x:%x", rand(), time(0)); secret = websMD5(sbuf); #if BIT_JAVASCRIPT && FUTURE websJsDefine("can", jsCan); #endif websDefineAction("login", loginServiceProc); websDefineAction("logout", logoutServiceProc); } return 0; }
static int initUpload(Webs *wp) { char *boundary; if (wp->uploadState == 0) { wp->uploadState = UPLOAD_BOUNDARY; if ((boundary = strstr(wp->contentType, "boundary=")) != 0) { boundary += 9; wp->boundary = sfmt("--%s", boundary); wp->boundaryLen = strlen(wp->boundary); } if (wp->boundaryLen == 0 || *wp->boundary == '\0') { websError(wp, HTTP_CODE_BAD_REQUEST, "Bad boundary"); return -1; } websSetVar(wp, "UPLOAD_DIR", uploadDir); wp->files = hashCreate(11); } return 0; }
PUBLIC int websFsOpen() { #if BIT_ROM WebsRomIndex *wip; char name[BIT_GOAHEAD_LIMIT_FILENAME]; ssize len; romFs = hashCreate(WEBS_HASH_INIT); for (wip = websRomIndex; wip->path; wip++) { strncpy(name, wip->path, BIT_GOAHEAD_LIMIT_FILENAME); len = strlen(name) - 1; if (len > 0 && (name[len] == '/' || name[len] == '\\')) { name[len] = '\0'; } hashEnter(romFs, name, valueSymbol(wip), 0); } #endif return 0; }
extern BREthereumStatus ewmAnnounceSubmitTransfer(BREthereumEWM ewm, BREthereumWallet wallet, BREthereumTransfer transfer, const char *strHash, int errorCode, const char *errorMessage, int id) { if (NULL == wallet) { return ERROR_UNKNOWN_WALLET; } if (NULL == transfer) { return ERROR_UNKNOWN_TRANSACTION; } if (NULL != strHash) { BREthereumHash hash = hashCreate(strHash); // We announce a submitted transfer => there is an originating transaction. if (ETHEREUM_BOOLEAN_IS_TRUE (hashEqual (hash, EMPTY_HASH_INIT)) || ETHEREUM_BOOLEAN_IS_FALSE (hashEqual (hash, transferGetOriginatingTransactionHash (transfer)))) return ERROR_TRANSACTION_HASH_MISMATCH; } ewmSignalAnnounceSubmitTransfer (ewm, wallet, transfer, errorCode, errorMessage, id); return SUCCESS; }
int main(int argc, char const *argv[]) { // default to a table of 1,000 elements. size_t size = 1000; if (argc > 1) { size = atoi(argv[1]); if (!size) { // couldn't convert argv[1] to a number fprintf(stderr, USAGE); exit(EXIT_FAILURE); } } // create a new hash table Hash *h = hashCreate(size); if (!h) { // failed to create hash table fprintf(stderr, "failed to create hash table\n"); exit(EXIT_FAILURE); } else { printf("Successfully created a hash table of size %zu.\n",size); FILE *opt; if ((opt = fopen(OPTIONS_FILE,"r"))) { char c; while((c = fgetc(opt)) != EOF) { putchar(c); } fclose(opt); } else { fprintf(stderr, "failed to open %s\n", OPTIONS_FILE); exit(EXIT_FAILURE); } } // create some variables char *command, *key, *obj, *res; bool hasSet,hasGet,onOwn; hasSet = hasGet = onOwn = false; bool keepGoing = true; while(keepGoing) { // initialize the variables and print the prompt command = key = obj = res = NULL; // encourage the user to try out the different commands if (!hasSet) { printf("%% (type 'set') "); } else if (!hasGet) { printf("%% (type 'get') "); } else if (!onOwn) { onOwn = true; printf("You're on your own now... available commands: set, get, delete, load\n"); printf("%% "); } else { printf("%% "); } // read the command command = readString(); // process a set command if (!strcasecmp(command,"set")) { hasSet = true; printf("\tkey: "); key = readString(); printf("\tvalue: "); obj = readString(); printf("\tresult: "); printf(hashSet(h,key,obj) ? "SUCCESS\n" : "FAILURE\n"); // process a get command } else if (!strcasecmp(command,"get")) { hasGet = true; printf("\tkey: "); key = readString(); if ((res = (char *) hashGet(h,key))){ printf("\tresult: %s\n", res); } else { printf("\tresult: NULL\n"); } // process a delete command } else if (!strcasecmp(command,"delete")) { printf("\tkey: "); key = readString(); if ((res = (char *) hashDelete(h,key))){ printf("\tresult: %s\n", res); free(res); } else { printf("\tresult: NULL\n"); } // process a load command } else if (!strcasecmp(command,"load")) { printf("\tresult: %f\n", hashLoad(h)); // let the user exit } else if (!strcasecmp(command,"exit") || !strcasecmp(command,"quit")) { keepGoing = false; // invalid command } else if (strlen(command) > 0) { printf("invalid command\n"); } // free the command and key, as necessary if (command) { free(command); } if (key) { free(key); } } hashDestroy(h,destroy); return EXIT_SUCCESS; }
PUBLIC void websActionOpen() { formSymtab = hashCreate(WEBS_HASH_INIT); websDefineHandler("action", actionHandler, closeAction, 0); }
static void testGet() { DataValue value; bool boolean; int8_t int8; uint8_t uint8; int16_t int16; uint16_t uint16; int32_t int32; uint32_t uint32; int64_t int64; uint64_t uint64; float float32; double float64; fixed16_16 fixed; void * pointer; const char * string; size_t length; const void * blob; HashTable * hashTable, * hashTableResult; DataArray * array, * arrayResult; AssociativeArray * assArray, * assArrayResult; value = valueCreateBoolean(false); boolean = valueGetBoolean(&value); TestCase_assert(!boolean, "Expected false but got true"); valueDispose(&value); value = valueCreateBoolean(true); boolean = valueGetBoolean(&value); TestCase_assert(boolean, "Expected true but got false"); valueDispose(&value); value = valueCreateInt8(0); int8 = valueGetInt8(&value); TestCase_assert(int8 == 0, "Expected 0 but got %d", int8); valueDispose(&value); value = valueCreateInt8(INT8_MIN); int8 = valueGetInt8(&value); TestCase_assert(int8 == INT8_MIN, "Expected -128 but got %d", int8); valueDispose(&value); value = valueCreateUInt8(0); uint8 = valueGetUInt8(&value); TestCase_assert(uint8 == 0, "Expected 0 but got %u", uint8); valueDispose(&value); value = valueCreateUInt8(UINT8_MAX); uint8 = valueGetUInt8(&value); TestCase_assert(uint8 == UINT8_MAX, "Expected 255 but got %u", uint8); valueDispose(&value); value = valueCreateInt16(0); int16 = valueGetInt16(&value); TestCase_assert(int16 == 0, "Expected 0 but got %d", int16); valueDispose(&value); value = valueCreateInt16(INT16_MIN); int16 = valueGetInt16(&value); TestCase_assert(int16 == INT16_MIN, "Expected -32768 but got %d", int16); valueDispose(&value); value = valueCreateUInt16(0); uint16 = valueGetUInt16(&value); TestCase_assert(uint16 == 0, "Expected 0 but got %u", uint16); valueDispose(&value); value = valueCreateUInt16(UINT16_MAX); uint16 = valueGetUInt16(&value); TestCase_assert(uint16 == UINT16_MAX, "Expected 65535 but got %u", uint16); valueDispose(&value); value = valueCreateInt32(0); int32 = valueGetInt32(&value); TestCase_assert(int32 == 0, "Expected 0 but got %d", int32); valueDispose(&value); value = valueCreateInt32(INT32_MIN); int32 = valueGetInt32(&value); TestCase_assert(int32 == INT32_MIN, "Expected -214783648 but got %d", int32); valueDispose(&value); value = valueCreateUInt32(0); uint32 = valueGetUInt32(&value); TestCase_assert(uint32 == 0, "Expected 0 but got %u", uint32); valueDispose(&value); value = valueCreateUInt32(UINT32_MAX); uint32 = valueGetUInt32(&value); TestCase_assert(uint32 == UINT32_MAX, "Expected 4294967295 but got %u", uint32); valueDispose(&value); value = valueCreateInt64(0); int64 = valueGetInt64(&value); TestCase_assert(int64 == 0, "Expected 0 but got " INT64_FORMAT, int64); valueDispose(&value); value = valueCreateInt64(INT64_MIN); int64 = valueGetInt64(&value); TestCase_assert(int64 == INT64_MIN, "Expected -9223372036854775808 but got " INT64_FORMAT, int64); valueDispose(&value); value = valueCreateUInt64(0); uint64 = valueGetUInt64(&value); TestCase_assert(uint64 == 0, "Expected 0 but got " UINT64_FORMAT, uint64); valueDispose(&value); value = valueCreateUInt64(UINT64_MAX); uint64 = valueGetUInt64(&value); TestCase_assert(uint64 == UINT64_MAX, "Expected 18446744073709551615 but got " UINT64_FORMAT, uint64); valueDispose(&value); value = valueCreateFloat(0.0f); float32 = valueGetFloat(&value); TestCase_assert(float32 == 0.0f, "Expected 0.0 but got %f", float32); valueDispose(&value); value = valueCreateFloat(FLT_MAX); float32 = valueGetFloat(&value); TestCase_assert(float32 == FLT_MAX, "Expected %f but got %f", FLT_MAX, float32); valueDispose(&value); value = valueCreateDouble(0.0); float64 = valueGetDouble(&value); TestCase_assert(float64 == 0.0, "Expected 0.0 but got %f", float64); valueDispose(&value); value = valueCreateDouble(DBL_MAX); float64 = valueGetDouble(&value); TestCase_assert(float64 == DBL_MAX, "Expected %f but got %f", DBL_MAX, float64); valueDispose(&value); value = valueCreateFixed16_16(0x00000); fixed = valueGetFixed16_16(&value); TestCase_assert(fixed == 0x00000, "Expected 0x00000 but got 0x%05X", fixed); valueDispose(&value); value = valueCreateFixed16_16(FIXED_16_16_MIN); fixed = valueGetFixed16_16(&value); TestCase_assert(fixed == FIXED_16_16_MIN, "Expected 0x%05X but got 0x%05X", FIXED_16_16_MIN, fixed); valueDispose(&value); value = valueCreatePointer(NULL); pointer = valueGetPointer(&value); TestCase_assert(pointer == NULL, "Expected NULL but got %p", pointer); valueDispose(&value); value = valueCreatePointer((void *) 0xFFFFFFFF); pointer = valueGetPointer(&value); TestCase_assert(pointer == (void *) 0xFFFFFFFF, "Expected 0xffffffff but got %p", pointer); valueDispose(&value); value = valueCreateString("", DATA_USE_STRLEN, false, false); string = valueGetString(&value); TestCase_assert(string != NULL, "Expected non-NULL but got NULL"); TestCase_assert(!strcmp(string, ""), "Expected \"\" but got \"%s\"", string); valueDispose(&value); value = valueCreateString("abc", DATA_USE_STRLEN, false, false); string = valueGetString(&value); TestCase_assert(string != NULL, "Expected non-NULL but got NULL"); TestCase_assert(!strcmp(string, "abc"), "Expected \"abc\" but got \"%s\"", string); valueDispose(&value); value = valueCreateBlob("", 0, false, false); blob = valueGetBlob(&value, &length); TestCase_assert(blob != NULL, "Expected non-NULL but got NULL"); TestCase_assert(length == 0, "Expected 0 but got " SIZE_T_FORMAT, length); valueDispose(&value); value = valueCreateBlob("\x00\xFF", 2, false, false); blob = valueGetBlob(&value, &length); TestCase_assert(blob != NULL, "Expected non-NULL but got NULL"); TestCase_assert(length == 2, "Expected 2 but got " SIZE_T_FORMAT, length); TestCase_assert(!memcmp(blob, "\x00\xFF", 2), "Expected {0x00, 0xFF} but got {0x%02X, 0x%02X}", ((char *) blob)[0], ((char *) blob)[1]); valueDispose(&value); value = valueCreateHashTable(hashTable = hashCreate(), true, false); hashTableResult = valueGetHashTable(&value); TestCase_assert(hashTableResult == hashTable, "Expected %p but got %p", hashTable, hashTableResult); valueDispose(&value); value = valueCreateArray(array = arrayCreate(), true, false); arrayResult = valueGetArray(&value); TestCase_assert(arrayResult == array, "Expected %p but got %p", array, arrayResult); valueDispose(&value); value = valueCreateAssociativeArray(assArray = associativeArrayCreate(), true, false); assArrayResult = valueGetAssociativeArray(&value); TestCase_assert(assArrayResult == assArray, "Expected %p but got %p", assArray, assArrayResult); valueDispose(&value); }
static void testCopy() { DataValue value, copy; HashTable * hashTable; DataArray * array; AssociativeArray * assArray; value = valueCreateBoolean(false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, copy.type); TestCase_assert(!copy.value.boolean, "Expected false but got true"); valueDispose(&value); valueDispose(©); value = valueCreateBoolean(true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, copy.type); TestCase_assert(copy.value.boolean, "Expected true but got false"); valueDispose(&value); valueDispose(©); value = valueCreateInt8(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, copy.type); TestCase_assert(copy.value.int8 == 0, "Expected 0 but got %d", copy.value.int8); valueDispose(&value); valueDispose(©); value = valueCreateInt8(INT8_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, copy.type); TestCase_assert(copy.value.int8 == INT8_MIN, "Expected -128 but got %d", copy.value.int8); valueDispose(&value); valueDispose(©); value = valueCreateUInt8(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, copy.type); TestCase_assert(copy.value.uint8 == 0, "Expected 0 but got %u", copy.value.uint8); valueDispose(&value); valueDispose(©); value = valueCreateUInt8(UINT8_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, copy.type); TestCase_assert(copy.value.uint8 == UINT8_MAX, "Expected 255 but got %u", copy.value.uint8); valueDispose(&value); valueDispose(©); value = valueCreateInt16(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, copy.type); TestCase_assert(copy.value.int16 == 0, "Expected 0 but got %d", copy.value.int16); valueDispose(&value); valueDispose(©); value = valueCreateInt16(INT16_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, copy.type); TestCase_assert(copy.value.int16 == INT16_MIN, "Expected -32768 but got %d", copy.value.int16); valueDispose(&value); valueDispose(©); value = valueCreateUInt16(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, copy.type); TestCase_assert(copy.value.uint16 == 0, "Expected 0 but got %u", copy.value.uint16); valueDispose(&value); valueDispose(©); value = valueCreateUInt16(UINT16_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, copy.type); TestCase_assert(copy.value.uint16 == UINT16_MAX, "Expected 65535 but got %u", copy.value.uint16); valueDispose(&value); valueDispose(©); value = valueCreateInt32(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, copy.type); TestCase_assert(copy.value.int32 == 0, "Expected 0 but got %d", copy.value.int32); valueDispose(&value); valueDispose(©); value = valueCreateInt32(INT32_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, copy.type); TestCase_assert(copy.value.int32 == INT32_MIN, "Expected -214783648 but got %d", copy.value.int32); valueDispose(&value); valueDispose(©); value = valueCreateUInt32(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, copy.type); TestCase_assert(copy.value.uint32 == 0, "Expected 0 but got %u", copy.value.uint32); valueDispose(&value); valueDispose(©); value = valueCreateUInt32(UINT32_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, copy.type); TestCase_assert(copy.value.uint32 == UINT32_MAX, "Expected 4294967295 but got %u", copy.value.uint32); valueDispose(&value); valueDispose(©); value = valueCreateInt64(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, copy.type); TestCase_assert(copy.value.int64 == 0, "Expected 0 but got " INT64_FORMAT, copy.value.int64); valueDispose(&value); valueDispose(©); value = valueCreateInt64(INT64_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, copy.type); TestCase_assert(copy.value.int64 == INT64_MIN, "Expected -9223372036854775808 but got " INT64_FORMAT, copy.value.int64); valueDispose(&value); valueDispose(©); value = valueCreateUInt64(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, copy.type); TestCase_assert(copy.value.uint64 == 0, "Expected 0 but got " UINT64_FORMAT, copy.value.uint64); valueDispose(&value); valueDispose(©); value = valueCreateUInt64(UINT64_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, copy.type); TestCase_assert(copy.value.uint64 == UINT64_MAX, "Expected 18446744073709551615 but got " UINT64_FORMAT, copy.value.uint64); valueDispose(&value); valueDispose(©); value = valueCreateFloat(0.0f); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, copy.type); TestCase_assert(copy.value.float32 == 0.0f, "Expected 0.0 but got %f", copy.value.float32); valueDispose(&value); valueDispose(©); value = valueCreateFloat(FLT_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, copy.type); TestCase_assert(copy.value.float32 == FLT_MAX, "Expected %f but got %f", FLT_MAX, copy.value.float32); valueDispose(&value); valueDispose(©); value = valueCreateDouble(0.0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, copy.type); TestCase_assert(copy.value.float64 == 0.0, "Expected 0.0 but got %f", copy.value.float64); valueDispose(&value); valueDispose(©); value = valueCreateDouble(DBL_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, copy.type); TestCase_assert(copy.value.float64 == DBL_MAX, "Expected %f but got %f", DBL_MAX, copy.value.float64); valueDispose(&value); valueDispose(©); value = valueCreateFixed16_16(0x00000); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, copy.type); TestCase_assert(copy.value.fixed == 0.0, "Expected 0.0 but got %f", copy.value.fixed); valueDispose(&value); valueDispose(©); value = valueCreateFixed16_16(FIXED_16_16_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, copy.type); TestCase_assert(copy.value.fixed == FIXED_16_16_MIN, "Expected 0x%05X but got 0x%05X", FIXED_16_16_MIN, copy.value.fixed); valueDispose(&value); valueDispose(©); value = valueCreatePointer(NULL); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, copy.type); TestCase_assert(copy.value.pointer == NULL, "Expected NULL but got %p", copy.value.pointer); valueDispose(&value); valueDispose(©); value = valueCreatePointer((void *) 0xFFFFFFFF); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, copy.type); TestCase_assert(copy.value.pointer == (void *) 0xFFFFFFFF, "Expected 0xffffffff but got %p", copy.value.pointer); valueDispose(&value); valueDispose(©); value = valueCreateString("", DATA_USE_STRLEN, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, copy.type); TestCase_assert(copy.value.string != NULL, "Expected non-NULL but got NULL"); TestCase_assert(!strcmp(copy.value.string, ""), "Expected \"\" but got \"%s\"", copy.value.string); valueDispose(&value); valueDispose(©); value = valueCreateString("abc", DATA_USE_STRLEN, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, copy.type); TestCase_assert(copy.value.string != NULL, "Expected non-NULL but got NULL"); TestCase_assert(!strcmp(copy.value.string, "abc"), "Expected \"abc\" but got \"%s\"", copy.value.string); TestCase_assert(copy.value.string == value.value.string, "Expected %p but got %p", value.value.string, copy.value.string); valueDispose(&value); valueDispose(©); value = valueCreateString("abc", DATA_USE_STRLEN, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, copy.type); TestCase_assert(copy.value.string != NULL, "Expected non-NULL but got NULL"); TestCase_assert(!strcmp(copy.value.string, "abc"), "Expected \"abc\" but got \"%s\"", copy.value.string); TestCase_assert(copy.value.string != value.value.string, "Expected pointers to differ, but both are %p", copy.value.string); valueDispose(&value); valueDispose(©); value = valueCreateBlob("", 0, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, copy.type); TestCase_assert(copy.value.blob.bytes != NULL, "Expected non-NULL but got NULL"); TestCase_assert(copy.value.blob.length == 0, "Expected 0 but got " SIZE_T_FORMAT, copy.value.blob.length); valueDispose(&value); valueDispose(©); value = valueCreateBlob("\x00\xFF", 2, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, copy.type); TestCase_assert(copy.value.blob.bytes != NULL, "Expected non-NULL but got NULL"); TestCase_assert(copy.value.blob.length == 2, "Expected 2 but got " SIZE_T_FORMAT, copy.value.blob.length); TestCase_assert(!memcmp(copy.value.blob.bytes, "\x00\xFF", 2), "Expected {0x00, 0xFF} but got {0x%02X, 0x%02X}", ((char *) copy.value.blob.bytes)[0], ((char *) copy.value.blob.bytes)[1]); TestCase_assert(copy.value.blob.bytes == value.value.blob.bytes, "Expected %p but got %p", value.value.blob.bytes, copy.value.blob.bytes); valueDispose(&value); valueDispose(©); value = valueCreateBlob("\x00\xFF", 2, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, copy.type); TestCase_assert(copy.value.blob.bytes != NULL, "Expected non-NULL but got NULL"); TestCase_assert(copy.value.blob.length == 2, "Expected 2 but got " SIZE_T_FORMAT, copy.value.blob.length); TestCase_assert(!memcmp(copy.value.blob.bytes, "\x00\xFF", 2), "Expected {0x00, 0xFF} but got {0x%02X, 0x%02X}", ((char *) copy.value.blob.bytes)[0], ((char *) copy.value.blob.bytes)[1]); TestCase_assert(copy.value.blob.bytes != value.value.blob.bytes, "Expected pointers to differ, but both are %p", copy.value.blob.bytes); valueDispose(&value); valueDispose(©); hashTable = hashCreate(); hashSet(hashTable, "a", valueCreateBoolean(false)); value = valueCreateHashTable(hashTable, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, copy.type); TestCase_assert(copy.value.hashTable == value.value.hashTable, "Expected %p but got %p", value.value.hashTable, copy.value.hashTable); valueDispose(&value); valueDispose(©); value = valueCreateHashTable(hashTable, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, copy.type); TestCase_assert(copy.value.hashTable->count == 1, "Expected 1 but got " SIZE_T_FORMAT, copy.value.hashTable->count); TestCase_assert(copy.value.hashTable != value.value.hashTable, "Expected pointers to differ, but both are %p", copy.value.hashTable); valueDispose(&value); valueDispose(©); hashDispose(hashTable); array = arrayCreate(); arrayAppend(array, valueCreateBoolean(false)); value = valueCreateArray(array, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, copy.type); TestCase_assert(copy.value.array == value.value.array, "Expected %p but got %p", value.value.array, copy.value.array); valueDispose(&value); valueDispose(©); value = valueCreateArray(array, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, copy.type); TestCase_assert(copy.value.array->count == 1, "Expected 1 but got " SIZE_T_FORMAT, copy.value.array->count); TestCase_assert(copy.value.array != value.value.array, "Expected pointers to differ, but both are %p", copy.value.array); valueDispose(&value); valueDispose(©); arrayDispose(array); assArray = associativeArrayCreate(); associativeArrayAppend(assArray, "a", valueCreateBoolean(false)); value = valueCreateAssociativeArray(assArray, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, copy.type); TestCase_assert(copy.value.associativeArray == value.value.associativeArray, "Expected %p but got %p", value.value.associativeArray, copy.value.associativeArray); valueDispose(&value); valueDispose(©); value = valueCreateAssociativeArray(assArray, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, copy.type); TestCase_assert(copy.value.associativeArray->count == 1, "Expected 1 but got " SIZE_T_FORMAT, copy.value.associativeArray->count); TestCase_assert(copy.value.associativeArray != value.value.associativeArray, "Expected pointers to differ, but both are %p", copy.value.associativeArray); valueDispose(&value); valueDispose(©); associativeArrayDispose(assArray); }
static void testConversions() { DataValue value; bool boolean; int8_t int8; uint8_t uint8; int16_t int16; uint16_t uint16; int32_t int32; uint32_t uint32; int64_t int64; uint64_t uint64; float float32; double float64; fixed16_16 fixed; void * pointer; const char * string; const void * blob; HashTable * hashTable; DataArray * array; AssociativeArray * assArray; value = valueCreateBoolean(false); getAllValues(value); assertAllTypes(0, 0, 0.0, 0x00000, NULL); value = valueCreateBoolean(true); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateInt8(0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateInt8(1); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateUInt8(0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateUInt8(1); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateInt16(0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateInt16(1); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateUInt16(0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateUInt16(1); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateInt32(0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateInt32(1); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateUInt32(0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateUInt32(1); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateInt64(0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateInt64(1); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateUInt64(0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateUInt64(1); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateFloat(0.0f); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateFloat(1.0f); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateFloat(1.25f); getAllValues(value); assertAllTypes(true, 1, 1.25, 0x14000, NULL); value = valueCreateDouble(0.0); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateDouble(1.0); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateDouble(1.25); getAllValues(value); assertAllTypes(true, 1, 1.25, 0x14000, NULL); value = valueCreateFixed16_16(0x00000); getAllValues(value); assertAllTypes(false, 0, 0.0, 0x00000, NULL); value = valueCreateFixed16_16(0x10000); getAllValues(value); assertAllTypes(true, 1, 1.0, 0x10000, NULL); value = valueCreateFixed16_16(0x14000); getAllValues(value); assertAllTypes(true, 1, 1.25, 0x14000, NULL); value = valueCreatePointer(NULL); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypes(NULL); value = valueCreatePointer((void *) 0x1); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypesSeparate((void *) 0x1, NULL, NULL, NULL, NULL, NULL); value = valueCreateString(NULL, 0, false, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypes(NULL); value = valueCreateString("abcd", 4, false, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypesSeparate(value.value.string, value.value.string, value.value.string, NULL, NULL, NULL); value = valueCreateBlob(NULL, 0, false, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypes(NULL); value = valueCreateBlob("abcd", 4, false, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypesSeparate(value.value.blob.bytes, NULL, value.value.blob.bytes, NULL, NULL, NULL); value = valueCreateHashTable(NULL, false, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypes(NULL); value = valueCreateHashTable(hashCreate(), true, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypesSeparate(value.value.hashTable, NULL, NULL, value.value.hashTable, NULL, NULL); valueDispose(&value); value = valueCreateArray(NULL, false, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypes(NULL); value = valueCreateArray(arrayCreate(), true, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypesSeparate(value.value.array, NULL, NULL, NULL, value.value.array, NULL); valueDispose(&value); value = valueCreateAssociativeArray(NULL, false, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypes(NULL); value = valueCreateAssociativeArray(associativeArrayCreate(), true, false); getAllValues(value); assertAllPrimitiveTypes(false, 0, 0.0, 0x00000); assertAllPointerTypesSeparate(value.value.associativeArray, NULL, NULL, NULL, NULL, value.value.associativeArray); valueDispose(&value); }
static void deserializeContainer(DataValue * container, DeserializationContext * context) { size_t index, count; const char * key = NULL; enum DataValueType type; DataValue value; const void * blob; size_t length = 0; count = context->beginArray(context, NULL); if (context->status != SERIALIZATION_ERROR_OK) { return; } for (index = 0; index < count; index++) { if (container->type == DATA_TYPE_HASH_TABLE || container->type == DATA_TYPE_ASSOCIATIVE_ARRAY) { key = context->readString(context, NULL); if (context->status != SERIALIZATION_ERROR_OK) { return; } index++; } type = context->readEnumeration(context, NULL, ALL_DATA_TYPE_ENUM_KEYS_AND_VALUES, NULL); if (context->status != SERIALIZATION_ERROR_OK) { return; } index++; switch (type) { case DATA_TYPE_BOOLEAN: value = valueCreateBoolean(context->readBoolean(context, NULL)); break; case DATA_TYPE_INT8: value = valueCreateInt8(context->readInt8(context, NULL)); break; case DATA_TYPE_UINT8: value = valueCreateUInt8(context->readUInt8(context, NULL)); break; case DATA_TYPE_INT16: value = valueCreateInt16(context->readInt16(context, NULL)); break; case DATA_TYPE_UINT16: value = valueCreateUInt16(context->readUInt16(context, NULL)); break; case DATA_TYPE_INT32: value = valueCreateInt32(context->readInt32(context, NULL)); break; case DATA_TYPE_UINT32: value = valueCreateUInt32(context->readUInt32(context, NULL)); break; case DATA_TYPE_INT64: value = valueCreateInt64(context->readInt64(context, NULL)); break; case DATA_TYPE_UINT64: value = valueCreateUInt64(context->readUInt64(context, NULL)); break; case DATA_TYPE_FLOAT: value = valueCreateFloat(context->readFloat(context, NULL)); break; case DATA_TYPE_DOUBLE: value = valueCreateDouble(context->readDouble(context, NULL)); break; case DATA_TYPE_FIXED_16_16: value = valueCreateFixed16_16(context->readFixed16_16(context, NULL)); break; case DATA_TYPE_STRING: value = valueCreateString(context->readString(context, NULL), DATA_USE_STRLEN, true, true); break; case DATA_TYPE_BLOB: blob = context->readBlob(context, NULL, &length); if (context->status != SERIALIZATION_ERROR_OK) { return; } value = valueCreateBlob(blob, length, true, true); break; case DATA_TYPE_ARRAY: value.type = DATA_TYPE_ARRAY; value.value.array = arrayCreate(); deserializeContainer(&value, context); break; case DATA_TYPE_HASH_TABLE: value.type = DATA_TYPE_HASH_TABLE; value.value.hashTable = hashCreate(); deserializeContainer(&value, context); break; case DATA_TYPE_ASSOCIATIVE_ARRAY: value.type = DATA_TYPE_ASSOCIATIVE_ARRAY; value.value.associativeArray = associativeArrayCreate(); deserializeContainer(&value, context); break; case DATA_TYPE_POINTER: break; } if (context->status != SERIALIZATION_ERROR_OK) { return; } switch (container->type) { case DATA_TYPE_ARRAY: arrayAppend(container->value.array, value); break; case DATA_TYPE_HASH_TABLE: hashSet(container->value.hashTable, key, value); break; case DATA_TYPE_ASSOCIATIVE_ARRAY: associativeArrayAppend(container->value.associativeArray, key, value); break; default: break; } } context->endArray(context); }
PUBLIC void websActionOpen() { actionTable = hashCreate(WEBS_HASH_INIT); websDefineHandler("action", 0, actionHandler, closeAction, 0); }
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; }
static void testCreate() { DataValue value; char * string1 = "abc", * string2 = "foo"; char blob1[] = {0x00, 0x01}, blob2[] = {0x03, 0x05, 0x07}; HashTable * hashTable1, * hashTable2; DataArray * array1, * array2; AssociativeArray * assArray1, * assArray2; value = valueCreateBoolean(false); TestCase_assert(value.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value.type); TestCase_assert(!value.value.boolean, "Expected false but got true"); valueDispose(&value); value = valueCreateBoolean(true); TestCase_assert(value.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value.type); TestCase_assert(value.value.boolean, "Expected true but got false"); valueDispose(&value); value = valueCreateInt8(0); TestCase_assert(value.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, value.type); TestCase_assert(value.value.int8 == 0, "Expected 0 but got %d", value.value.int8); valueDispose(&value); value = valueCreateInt8(1); TestCase_assert(value.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, value.type); TestCase_assert(value.value.int8 == 1, "Expected 1 but got %d", value.value.int8); valueDispose(&value); value = valueCreateUInt8(0); TestCase_assert(value.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, value.type); TestCase_assert(value.value.uint8 == 0, "Expected 0 but got %d", value.value.uint8); valueDispose(&value); value = valueCreateUInt8(1); TestCase_assert(value.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, value.type); TestCase_assert(value.value.uint8 == 1, "Expected 1 but got %d", value.value.uint8); valueDispose(&value); value = valueCreateInt16(0); TestCase_assert(value.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, value.type); TestCase_assert(value.value.int16 == 0, "Expected 0 but got %d", value.value.int16); valueDispose(&value); value = valueCreateInt16(1); TestCase_assert(value.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, value.type); TestCase_assert(value.value.int16 == 1, "Expected 1 but got %d", value.value.int16); valueDispose(&value); value = valueCreateUInt16(0); TestCase_assert(value.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, value.type); TestCase_assert(value.value.uint16 == 0, "Expected 0 but got %d", value.value.uint16); valueDispose(&value); value = valueCreateUInt16(1); TestCase_assert(value.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, value.type); TestCase_assert(value.value.uint16 == 1, "Expected 1 but got %d", value.value.uint16); valueDispose(&value); value = valueCreateInt32(0); TestCase_assert(value.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, value.type); TestCase_assert(value.value.int32 == 0, "Expected 0 but got %d", value.value.int32); valueDispose(&value); value = valueCreateInt32(1); TestCase_assert(value.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, value.type); TestCase_assert(value.value.int32 == 1, "Expected 1 but got %d", value.value.int32); valueDispose(&value); value = valueCreateUInt32(0); TestCase_assert(value.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, value.type); TestCase_assert(value.value.uint32 == 0, "Expected 0 but got %d", value.value.uint32); valueDispose(&value); value = valueCreateUInt32(1); TestCase_assert(value.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, value.type); TestCase_assert(value.value.uint32 == 1, "Expected 1 but got %d", value.value.uint32); valueDispose(&value); value = valueCreateInt64(0); TestCase_assert(value.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, value.type); TestCase_assert(value.value.int64 == 0, "Expected 0 but got " INT64_FORMAT, value.value.int64); valueDispose(&value); value = valueCreateInt64(1); TestCase_assert(value.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, value.type); TestCase_assert(value.value.int64 == 1, "Expected 1 but got " INT64_FORMAT, value.value.int64); valueDispose(&value); value = valueCreateUInt64(0); TestCase_assert(value.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, value.type); TestCase_assert(value.value.uint64 == 0, "Expected 0 but got " UINT64_FORMAT, value.value.uint64); valueDispose(&value); value = valueCreateUInt64(1); TestCase_assert(value.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, value.type); TestCase_assert(value.value.uint64 == 1, "Expected 1 but got " UINT64_FORMAT, value.value.uint64); valueDispose(&value); value = valueCreateFloat(0.0f); TestCase_assert(value.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, value.type); TestCase_assert(value.value.float32 == 0.0f, "Expected 0.0 but got %f", value.value.float32); valueDispose(&value); value = valueCreateFloat(1.0f); TestCase_assert(value.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, value.type); TestCase_assert(value.value.float32 == 1.0f, "Expected 1.0 but got %f", value.value.float32); valueDispose(&value); value = valueCreateDouble(0.0); TestCase_assert(value.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, value.type); TestCase_assert(value.value.float64 == 0.0, "Expected 0.0 but got %f", value.value.float64); valueDispose(&value); value = valueCreateDouble(1.0); TestCase_assert(value.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, value.type); TestCase_assert(value.value.float64 == 1.0, "Expected 1.0 but got %f", value.value.float64); valueDispose(&value); value = valueCreateFixed16_16(0x00000); TestCase_assert(value.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, value.type); TestCase_assert(value.value.fixed == 0x00000, "Expected 0x00000 but got 0x%05X", value.value.fixed); valueDispose(&value); value = valueCreateFixed16_16(0x10000); TestCase_assert(value.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, value.type); TestCase_assert(value.value.fixed == 0x10000, "Expected 0x10000 but got 0x%05X", value.value.fixed); valueDispose(&value); value = valueCreatePointer((void *) 0x0); TestCase_assert(value.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, value.type); TestCase_assert(value.value.pointer == (void *) 0x0, "Expected 0x0 but got %p", value.value.pointer); valueDispose(&value); value = valueCreatePointer((void *) 0x1); TestCase_assert(value.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, value.type); TestCase_assert(value.value.pointer == (void *) 0x1, "Expected 0x1 but got %p", value.value.pointer); valueDispose(&value); value = valueCreateString(string1, 2, true, true); TestCase_assert(value.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, value.type); TestCase_assert(!strcmp(value.value.string, "ab"), "Expected \"ab\" but got \"%s\"", value.value.string); TestCase_assert(value.value.string != string1, "Expected differing pointers, but both are %p", string1); valueDispose(&value); value = valueCreateString(string2, DATA_USE_STRLEN, false, false); TestCase_assert(value.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, value.type); TestCase_assert(value.value.string == string2, "Expected %p but got %p", string2, value.value.string); valueDispose(&value); value = valueCreateBlob(blob1, sizeof(blob1), true, true); TestCase_assert(value.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, value.type); TestCase_assert(value.value.blob.length == 2, "Expected 2 but got " SIZE_T_FORMAT, value.value.blob.length); TestCase_assert(!memcmp(value.value.blob.bytes, blob1, sizeof(blob1)), "Expected {0x%02X, 0x%02X} but got {0x%02X, 0x%02X}", blob1[0], blob1[1], ((char *) value.value.blob.bytes)[0], ((char *) value.value.blob.bytes)[1]); TestCase_assert(value.value.blob.bytes != blob1, "Expected differing pointers, but both are %p", blob1); valueDispose(&value); value = valueCreateBlob(blob2, sizeof(blob2), false, false); TestCase_assert(value.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, value.type); TestCase_assert(value.value.blob.length == 3, "Expected 3 but got " SIZE_T_FORMAT, value.value.blob.length); TestCase_assert(value.value.blob.bytes == blob2, "Expected %p but got %p", blob2, value.value.blob.bytes); valueDispose(&value); hashTable1 = hashCreate(); hashSet(hashTable1, "a", valueCreateBoolean(false)); hashTable2 = hashCreate(); value = valueCreateHashTable(hashTable1, true, true); TestCase_assert(value.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, value.type); TestCase_assert(value.value.hashTable != hashTable1, "Expected differing pointers, but both are %p", hashTable1); TestCase_assert(value.value.hashTable->count == hashTable1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, hashTable1->count, value.value.hashTable->count); TestCase_assert(hashGet(value.value.hashTable, "a") != NULL, "Expected non-NULL but got NULL"); TestCase_assert(hashGet(value.value.hashTable, "a")->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, hashGet(value.value.hashTable, "a")->type); TestCase_assert(!hashGet(value.value.hashTable, "a")->value.boolean, "Expected false but got true"); valueDispose(&value); value = valueCreateHashTable(hashTable2, true, false); TestCase_assert(value.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, value.type); TestCase_assert(value.value.hashTable == hashTable2, "Expected %p but got %p", hashTable2, value.value.hashTable); valueDispose(&value); array1 = arrayCreate(); arrayAppend(array1, valueCreateBoolean(false)); array2 = arrayCreate(); value = valueCreateArray(array1, true, true); TestCase_assert(value.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, value.type); TestCase_assert(value.value.array != array1, "Expected differing pointers, but both are %p", array1); TestCase_assert(value.value.array->count == array1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, array1->count, value.value.array->count); TestCase_assert(arrayGet(value.value.array, 0) != NULL, "Expected non-NULL but got NULL"); TestCase_assert(arrayGet(value.value.array, 0)->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, arrayGet(value.value.array, 0)->type); TestCase_assert(!arrayGet(value.value.array, 0)->value.boolean, "Expected false but got true"); valueDispose(&value); value = valueCreateArray(array2, true, false); TestCase_assert(value.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, value.type); TestCase_assert(value.value.array == array2, "Expected %p but got %p", array2, value.value.array); valueDispose(&value); assArray1 = associativeArrayCreate(); associativeArrayAppend(assArray1, "a", valueCreateBoolean(false)); assArray2 = associativeArrayCreate(); value = valueCreateAssociativeArray(assArray1, true, true); TestCase_assert(value.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, value.type); TestCase_assert(value.value.associativeArray != assArray1, "Expected differing pointers, but both are %p", assArray1); TestCase_assert(value.value.associativeArray->count == assArray1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, assArray1->count, value.value.associativeArray->count); TestCase_assert(associativeArrayGetValueAtIndex(value.value.associativeArray, 0) != NULL, "Expected non-NULL but got NULL"); TestCase_assert(associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->type); TestCase_assert(!associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->value.boolean, "Expected false but got true"); TestCase_assert(!strcmp(associativeArrayGetKeyAtIndex(value.value.associativeArray, 0), "a"), "Expected \"a\" but got \"%s\"", associativeArrayGetKeyAtIndex(value.value.associativeArray, 0)); valueDispose(&value); value = valueCreateAssociativeArray(assArray2, true, false); TestCase_assert(value.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, value.type); TestCase_assert(value.value.associativeArray == assArray2, "Expected %p but got %p", assArray2, value.value.associativeArray); valueDispose(&value); }