void checkTreeContents(BPlusTree *tree) { // reset counter for (int32 i = 0;i < gNum;i++) gKeys[i].count = 0; TreeIterator iterator(tree); char key[B_FILE_NAME_LENGTH]; uint16 length,duplicate; off_t value; status_t status; while ((status = iterator.GetNextEntry(key,&length,B_FILE_NAME_LENGTH,&value,&duplicate)) == B_OK) { if (value < 0 || value >= gNum) { iterator.Dump(); printf("\ninvalid value %Ld in tree: ",value); bailOutWithKey(key,length); } if (gKeys[value].value != value) { iterator.Dump(); printf("\nkey pointing to the wrong value %Ld (should be %Ld)\n",value,gKeys[value].value); bailOutWithKey(key,length); } if (length != gKeys[value].length || memcmp(key,gKeys[value].data,length)) { iterator.Dump(); printf("\nkeys don't match (key index = %Ld, %ld times in tree, %ld. occassion):\n\tfound: ",value,gKeys[value].in,gKeys[value].count + 1); dumpKey(key,length); printf("\n\texpected: "); dumpKey(gKeys[value].data,gKeys[value].length); putchar('\n'); bailOut(); } gKeys[value].count++; } if (status != B_ENTRY_NOT_FOUND) { printf("TreeIterator::GetNext() returned: %s\n",strerror(status)); iterator.Dump(); bailOut(); } for (int32 i = 0;i < gNum;i++) { if (gKeys[i].in != gKeys[i].count) { printf("Key "); dumpKey(gKeys[i].data,gKeys[i].length); printf(" found only %ld from %ld\n",gKeys[i].count,gKeys[i].in); } } }
void bailOutWithKey(void *key, uint16 length) { dumpKey(key, length); putchar('\n'); bailOut(); }
void printValue(const REGF_VK_REC* vk, char* prefix) { char* quoted_value = NULL; char* quoted_name = NULL; char* conv_error = NULL; const char* str_type = NULL; uint32 size = vk->data_size; /* Microsoft's documentation indicates that "available memory" is * the limit on value sizes. Annoying. We limit it to 1M which * should rarely be exceeded, unless the file is corrupt or * malicious. For more info, see: * http://msdn2.microsoft.com/en-us/library/ms724872.aspx */ if(size > VK_MAX_DATA_LENGTH) { fprintf(stderr, "WARNING: value data size %d larger than " "%d, truncating...\n", size, VK_MAX_DATA_LENGTH); size = VK_MAX_DATA_LENGTH; } quoted_name = quote_string(vk->valuename, key_special_chars); if (quoted_name == NULL) { /* Value names are NULL when we're looking at the "(default)" value. * Currently we just return a 0-length string to try an eliminate * ambiguity with a literal "(default)" value. The data type of a line * in the output allows one to differentiate between the parent key and * this value. */ quoted_name = talloc_size(vk, 1); if(quoted_name == NULL) bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n"); quoted_name[0] = '\0'; } quoted_value = data_to_ascii(vk, vk->data, size, vk->type, &conv_error); if(quoted_value == NULL) { if(conv_error == NULL) fprintf(stderr, "WARNING: Could not quote value for '%s/%s'. " "Memory allocation failure likely.\n", prefix, quoted_name); else if(print_verbose) fprintf(stderr, "WARNING: Could not quote value for '%s/%s'. " "Returned error: %s\n", prefix, quoted_name, conv_error); } /* XXX: should these always be printed? */ else if(conv_error != NULL && print_verbose) fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', " "warning returned: %s\n", prefix, quoted_name, conv_error); str_type = regfi_type_val2str(vk->type); if(print_security) { if(str_type == NULL) printf("%s/%s,0x%.8X,%s,,,,,\n", prefix, quoted_name, vk->type, quoted_value); else printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name, str_type, quoted_value); } else { if(str_type == NULL) printf("%s/%s,0x%.8X,%s,\n", prefix, quoted_name, vk->type, quoted_value); else printf("%s/%s,%s,%s,\n", prefix, quoted_name, str_type, quoted_value); } }
bool Tracter::LPCepstrum::UnaryFetch(IndexType iIndex, float* oData) { assert(iIndex >= 0); CacheArea inputArea; // Read the input frame if (mInput->Read(inputArea, iIndex) < 1) return false; // Copy the frame though a compression function float* p = mInput->GetPointer(inputArea.offset); for (int i=0; i<mNCompressed; i++) mCompressed[i] = powf(p[i], mCompressionPower); // Do the DCT mFourier.Transform(); // Levinson / Durbin recursion // Indexes are C style from 0, but the books use 1 mAlpha0.assign(mOrder, 0.0f); mAlpha1.assign(mOrder, 0.0f); float* a0 = &mAlpha0.front(); // Current alphas float* a1 = &mAlpha1.front(); // Previous alphas float error = mAutoCorrelation[0] * (mRidge + 1.0f); if (error < 1e-8f) { Verbose(2, "error too small at index %ld\n", iIndex); return bailOut(oData); } for (int i=0; i<mOrder; i++) { float* tmp = a0; a0 = a1; a1 = tmp; // Swap a1 and a0 float sum = mAutoCorrelation[i+1]; for (int j=0; j<i; j++) sum -= a1[j] * mAutoCorrelation[i-j]; a0[i] = sum / error; if (!std::isfinite(a0[i])) { Verbose(2, "a0[%d] = %f at index %ld\n", i, a0[i], iIndex); return bailOut(oData); } error *= 1.0f - a0[i] * a0[i]; assert(std::isfinite(error)); assert(error != 0.0f); for (int j=0; j<i; j++) a0[j] = a1[j] - a0[i] * a1[i-j-1]; } // Gain (squared) float gain = mAutoCorrelation[0]; for (int j=0; j<mOrder; j++) gain -= a0[j] * mAutoCorrelation[j+1]; #if 0 // Compute LP power spectrum for (int i=0; i<mNCepstra; i++) { float omega = (float)M_PI * i/mNCepstra; float c = 0; float s = 0; for (int j=0; j<mOrder; j++) { c += a0[j] * cosf(omega*(j+1)); s += a0[j] * sinf(omega*(j+1)); } c = 1.0f - c; //oData[i] = gain / (s*s + c*c); oData[i] = 1.0f / (s*s + c*c); } #else // Compute LP cepstrum replacing unknown coeffs with 0 for (int i=0; i<mNCepstra; i++) { float sum = 0.0f; for (int k=0; k<i; k++) { int index = i-k-1; if (index < mOrder) sum += a0[i-k-1] * oData[k] * (k+1); } oData[i] = sum / (i+1); if (i < mOrder) oData[i] += a0[i]; assert(std::isfinite(oData[i])); } if (mC0) oData[mNCepstra] = logf(std::max(gain, 1e-8f)); #endif return true; }
int main(int argc,char **argv) { char *program = argv[0]; while (*++argv) { char *arg = *argv; if (*arg == '-') { if (arg[1] == '-') usage(program); while (*++arg && isalpha(*arg)) { switch (*arg) { case 'v': gVerbose = true; break; case 'e': gExcessive = true; break; case 't': if (*++argv == NULL) usage(program); if (!strcmp(*argv,"string")) gType = S_STR_INDEX; else if (!strcmp(*argv,"int32") || !strcmp(*argv,"int")) gType = S_INT_INDEX; else if (!strcmp(*argv,"uint32") || !strcmp(*argv,"uint")) gType = S_UINT_INDEX; else if (!strcmp(*argv,"int64") || !strcmp(*argv,"llong")) gType = S_LONG_LONG_INDEX; else if (!strcmp(*argv,"uint64") || !strcmp(*argv,"ullong")) gType = S_ULONG_LONG_INDEX; else if (!strcmp(*argv,"float")) gType = S_FLOAT_INDEX; else if (!strcmp(*argv,"double")) gType = S_DOUBLE_INDEX; else usage(program); break; case 'n': if (*++argv == NULL || !isdigit(**argv)) usage(program); gNum = atoi(*argv); if (gNum < 1) gNum = 1; break; case 'h': if (*++argv == NULL || !isdigit(**argv)) usage(program); gHard = atoi(*argv); if (gHard < 1) gHard = 1; break; case 'i': if (*++argv == NULL || !isdigit(**argv)) usage(program); gIterations = atoi(*argv); if (gIterations < 1) gIterations = 1; break; case 'r': if (*++argv == NULL || !isdigit(**argv)) usage(program); gSeed = atoi(*argv); break; } } } else break; } // we do want to have reproducible random keys if (gVerbose) printf("Set seed to %ld\n",gSeed); srand(gSeed); Inode inode("tree.data",gType | S_ALLOW_DUPS); gVolume = inode.GetVolume(); Transaction transaction(gVolume,0); init_cache(gVolume->Device(),gVolume->BlockSize()); // // Create the tree, the keys, and add all keys to the tree initially // BPlusTree tree(&transaction,&inode); status_t status; if ((status = tree.InitCheck()) < B_OK) { fprintf(stderr,"creating tree failed: %s\n",strerror(status)); bailOut(); } printf("*** Creating %ld keys...\n",gNum); if ((status = createKeys()) < B_OK) { fprintf(stderr,"creating keys failed: %s\n",strerror(status)); bailOut(); } if (gVerbose) dumpKeys(); for (int32 j = 0; j < gHard; j++ ) { addAllKeys(&transaction, &tree); // // Run the tests (they will exit the app, if an error occurs) // for (int32 i = 0;i < gIterations;i++) { printf("---------- Test iteration %ld ---------------------------------\n",i+1); addRandomSet(&transaction,&tree,int32(1.0 * gNum * rand() / RAND_MAX)); removeRandomSet(&transaction,&tree,int32(1.0 * gNum * rand() / RAND_MAX)); duplicateTest(&transaction,&tree); } removeAllKeys(&transaction, &tree); } // of course, we would have to free all our memory in a real application here... // write the cache back to the tree shutdown_cache(gVolume->Device(),gVolume->BlockSize()); return 0; }
status_t createKeys() { gKeys = (key *)malloc(gNum * sizeof(key)); if (gKeys == NULL) return B_NO_MEMORY; if (gType == S_STR_INDEX) { for (int32 i = 0;i < gNum;i++) { char name[B_FILE_NAME_LENGTH]; int32 length,tries = 0; bool last; // create unique keys! do { generateName(i,name,&length); } while ((last = findKey(name,length,i)) && tries++ < 100); if (last) { printf("Couldn't create unique key list!\n"); dumpKeys(); bailOut(); } gKeys[i].data = malloc(length + 1); memcpy(gKeys[i].data,name,length + 1); gKeys[i].length = length; gKeys[i].in = 0; gKeys[i].count = 0; gKeys[i].value = i; } } else { int32 length; int32 start = 0; switch (gType) { case S_FLOAT_INDEX: case S_INT_INDEX: start = -gNum / 2; case S_UINT_INDEX: length = 4; break; case S_DOUBLE_INDEX: case S_LONG_LONG_INDEX: start = -gNum / 2; case S_ULONG_LONG_INDEX: length = 8; break; default: return B_BAD_VALUE; } uint8 *buffer = (uint8 *)malloc(length * gNum); if (buffer == NULL) return B_NO_MEMORY; for (int32 i = 0;i < gNum;i++) { gKeys[i].data = (void *)(buffer + i * length); gKeys[i].length = length; gKeys[i].in = 0; gKeys[i].count = 0; } fillBuffer(buffer,start); } return B_OK; }