static int processor(uint8_t *f, int fd, size_t l) { uint8_t frag[33]; size_t start, off; ssize_t scan; int rc = EXIT_SUCCESS; int stop = 0; char ch = 0; if (stdin_nobuf()) { return EXIT_FAILURE; } while (1) { if (0xa != ch) { printf("\n> "); } ch = fgetc(stdin); printf("%c", ch); switch (ch) { case '+': if (readnum(&start)) { printf("\nINVALID START\n"); break; } if (readnum(&off)) { printf("\nINVALID LENGTH\n"); break; } printf("%lx\n", off+start); break; case '-': if (readnum(&start)) { printf("\nINVALID START\n"); break; } if (readnum(&off)) { printf("\nINVALID LENGTH\n"); break; } printf("%lx\n", start-off); break; case 'e': if (readnum(&off)) { printf("\nINVALID OFFSET\n"); break; } if (-1 == expand(&f, fd, l, off)) { stop = 1; rc = EXIT_FAILURE; break; } l += off; printf("\nEXTENDED %lu BYTES\n", (long unsigned)off); break; case 'f': if (readnum(&start) || (start >= l)) { printf("\nINVALID START\n"); break; } if (readnum(&off)) { printf("\nINVALID LENGTH\n"); break; } if (-1 == read_fragment(frag, off)) { printf("\nINVALID FRAGMENT\n"); break; } printf("\nFIND FRAG FROM %lx SIZE %lu\n", start, off); find_frag(f+start, l-start, start, frag, off); break; case 'i': if (readnum(&start) || (start >= l)) { printf("\nINVALID START\n"); break; } if (-1 == expand(&f, fd, l, 1)) { stop = 1; rc = EXIT_FAILURE; break; } l++; if (-1 == shift_by(f+start, l-start)) { stop = 1; rc = EXIT_FAILURE; break; } break; case 'l': printf(" %lx\n", l); break; case 0x4: case 'q': printf("\nQUIT\n"); stop++; break; case 'r': if (readnum(&start) || (start >=l)) { printf("\nINVALID START\n"); break; } if (readnum(&off) || ((start+off) > l)) { printf("\nINVALID OFFSET\n"); rc = EXIT_FAILURE; break; } if (0 == off) { off = (l - start); } printhex(f+start, off, start); break; case 's': if (readnum(&start) || (start >= l)) { printf("\nINVALID START\n"); break; } if (readnum(&off)) { printf("\nINVALID LENGTH\n"); break; } if (-1 == read_fragment(frag, off)) { printf("\nINVALID FRAGMENT\n"); break; } printf("\nSCAN FRAG FROM %lx SIZE %lu\n", start, off); while (-1 != (scan = find_frag(f+start, l-start, start, frag, off))) { start += scan + 1; } break; case 'w': if (readnum(&start) || (start >=l)) { printf("\nINVALID START\n"); break; } writehex(f+start, l-start); break; } if (stop) { break; } } return rc; }
/** * In order to help generate repeatable pseudo-random results for a * given set of input parameters, the master precalculates how many * results will be generated for each query. * * @param test_params_p Pointer to test_params. * @param query_frag_preresult_matrix Pointer to the matrix of (query,frag) * results. * @return 0 on success. */ static int precalculate_results( struct test_params_s *test_params_p, struct frag_preresult_s **query_frag_preresult_matrix) { int i, j; unsigned int seed; int cur_result_count = 0; /* 1. Generate the amount of results for each query * 2. Generate in which fragments will those results go */ for (i = 0; i < test_params_p->query_count; i++) { seed = i+1; cur_result_count = generate_int_range(&seed, test_params_p->result_count_min, test_params_p->result_count_max); for (j = 0; j < cur_result_count; j++) { query_frag_preresult_matrix[i] [find_frag(&seed, test_params_p->total_frags)].result_count++; } } /* Calculate the correct result_start location for each frag */ for (i = 0; i < test_params_p->query_count; i++) { for (j = 0; j < test_params_p->total_frags; j++) { if (j == 0) query_frag_preresult_matrix[i][j].result_start = 0; else query_frag_preresult_matrix[i][j].result_start = query_frag_preresult_matrix[i][j-1].result_start + query_frag_preresult_matrix[i][j-1].result_count; } } /* Print out the results */ #if 0 fprintf(stderr, "Results (query #, total result_count) (result_start,result_count) --- \n"); fprintf(stderr, "Query |"); for (i = 0; i < test_params_p->total_frags; i++) { fprintf(stderr, " Frag %2d ", i); } fprintf(stderr, "\n"); for (i = 0; i < test_params_p->query_count; i++) { int total_result_count = 0; for (j = 0; j < test_params_p->total_frags; j++) total_result_count += query_frag_preresult_matrix[i][j].result_count; fprintf(stderr, "(%3d,%3d) |", i, total_result_count); for (j = 0; j < test_params_p->total_frags; j++) { fprintf(stderr, "(%3d,%3d) ", query_frag_preresult_matrix[i][j].result_start, query_frag_preresult_matrix[i][j].result_count); } fprintf(stderr, "\n"); } fprintf(stderr, "\n"); #endif return 0; }