void get_keys(rb_tree * tree, rb_node * n, uint32_t height, uint32_t cutoff, linked_list * ll){ if (height > cutoff) linked_list_add_tail(n->key, ll); if (n->left != tree->nil) get_keys(tree, n->left, height+1, cutoff, ll); if (n->right != tree->nil) get_keys(tree, n->right, height+1, cutoff, ll); }
void wait_keypress(void) { key_data_t keys, no_keys; get_keys(&no_keys); while (1) { get_keys(&keys); if (keys.key2 != no_keys.key2) break; } }
static bool foreach_key_run_request(struct key_visitor_state *state, bool internal, bool toggles) { struct keymap *keymap = state->keymap; const char *group = !internal ? "External commands:" : toggles ? "Option toggling:" : "Internal commands:"; enum request request = REQ_RUN_REQUESTS + 1; for (; true; request++) { struct run_request *req = get_run_request(request); const char *key; if (!req) break; if (req->flags.internal != !!internal || req->keymap != keymap || !*(key = get_keys(keymap, request, true))) continue; if (toggles != !strcmp(req->argv[0], "toggle")) continue; if (!foreach_key_visit(state, group, request, NULL, req)) return false; } return true; }
static void get_args(int argc, char *argv[]) { u32 i; if (argc != 9 && argc != 10) fail("usage: makeself [-c] [type] [version suffix] [version] [vendor id] [auth id] [sdk type] [elf] [self]"); i = 1; if (argc == 10) { if (strcmp(argv[1], "-c") != 0) fail("invalid option: %s", argv[1]); compression = 1; i++; } get_type(argv[i++]); get_keys(argv[i++]); get_version(argv[i++]); get_vendor(argv[i++]); get_auth(argv[i++]); get_sdktype(argv[i++]); elf_name = argv[i++]; self_name = argv[i++]; if (compression) { if (type == KEY_ISO) fail("no compression support for isolated modules"); if (type == KEY_LDR) fail("no compression support for secure loaders"); } }
int misc_init_r(void) { char *s, buf[32]; #ifdef CONFIG_PREBOOT struct kbd_data_t kbd_data; /* Decode keys */ char *str = strdup(key_match(get_keys(&kbd_data))); /* Set or delete definition */ setenv("preboot", str); free(str); #endif /* CONFIG_PREBOOT */ /* count all restarts, and save this in an environment var */ s = getenv("restartcount"); if (s) sprintf(buf, "%ld", simple_strtoul(s, NULL, 10) + 1); else strcpy(buf, "1"); setenv("restartcount", buf); saveenv(); #ifdef CONFIG_HW_WATCHDOG davinci_hw_watchdog_enable(); #endif return 0; }
void union_oper(char *ptr, char key[MAX_KEY], entry *entryHead) { int keysLength = 1; char *keys = get_keys(ptr, key, &keysLength); if (keysLength == 1) { printf("invalid input\n"); free(keys); return; } entry *lastEntry = get_entry(entryHead, &keys[MAX_KEY*(keysLength - 1)]); if (lastEntry == NULL) { printf("no such key\n"); free(keys); return; } int *processedValues = get_values_copy(lastEntry->values, lastEntry->length); int valuesLength = lastEntry->length; for (int i = keysLength - 2; i >= 0; i--) { entry *first = get_entry(entryHead, &keys[MAX_KEY*i]); if (first == NULL) { printf("no such key\n"); return; } processedValues = (int *) realloc(processedValues, sizeof(int)*(valuesLength + first->length)); int counter = 0; for (int i = valuesLength; i < valuesLength + first->length; i++) { processedValues[i] = first->values[counter]; counter++; } valuesLength = valuesLength + first->length; } free(keys); if (valuesLength == 0) { printf("[]\n"); free(processedValues); return; } sort_array(processedValues, valuesLength); int tempLength = 0; int *uniqProcessedValues = get_unique_array(processedValues, valuesLength, &tempLength); printf("["); for (int i = 0; i < tempLength - 1; i++) { printf("%d ", uniqProcessedValues[i]); } printf("%d]\n", uniqProcessedValues[tempLength - 1]); free(uniqProcessedValues); free(processedValues); }
int dmsg_puts(char *__s) { int i; for (i=0; i<256; i++) { if (__s[i] == '\0') break; if (__s[i] == '\n') { dmsg_cursor_y += 8; dmsg_cursor_x = 0; } else { dmsg_putchar(dmsg_cursor_x, dmsg_cursor_y, __s[i]); dmsg_cursor_x += 8; } if (dmsg_cursor_x > (dmsg_buff_width-8)) { dmsg_cursor_x = 0; dmsg_cursor_y += 8; } if (dmsg_cursor_y > (dmsg_buff_height - 8)) { if (wait_scroll == 1) { key_data_t keys; // wait for 'O' press while (1) { get_keys(&keys); if (keys.key2 & KEY_O) break; } // wait for 'O' release while (1) { get_keys(&keys); if ((keys.key2 & KEY_O) == 0) break; } } dmsg_clear(); dmsg_cursor_y = 0; } } dmsg_update(); return 0; }
/** * returns -1 if o is greater * returns 0 may be identical, or may not be * returns 1 if o is less */ int compare(const vclock& o) const { bool l = false; bool g = false; auto keys = merge_keys(get_keys(_c), get_keys(o._c)); for(const auto& k : keys) { auto lvi = _c.find(k); auto rvi = o._c.find(k); size_t lv = lvi != std::end(_c) ? lvi->second : 0; size_t rv = rvi != std::end(o._c) ? rvi->second : 0; auto diff = static_cast<ptrdiff_t>(lv) - static_cast<ptrdiff_t>(rv); if(diff > 0) g = true; if(diff < 0) l = true; } if(l && g) return 0; if(l) return -1; if(g) return 1; return 0; }
void start () { if (!kmgr) kmgr = New sfskeymgr (); if (opts & KM_ALL) expand_list (); get_keys (); if (!(opts & KM_NOSRP) && !kmgr->getsrp (srpfile)) fatal << "No changes made\nCannot find suitable SRP parameters\n"; ncb = keys.size (); for (u_int i = 0; i < ncb; i++) keys[i]->k->key->init (wrap (this, &schnorr2edit::initcb, keys[i])); }
static void curses_output (void) { allocate_data (); if (!conf.skip_term_resolver) gdns_thread_create (); render_screens (); get_keys (); /* restore tty modes and reset * terminal into non-visual mode */ endwin (); }
void Parser::create_table(){ string t1 = tokenizer.pop(); string t2 = tokenizer.pop(); if (t1 == "CREATE" && t2 == "TABLE"){ string table_name = tokenizer.pop(); pair<vector<string>, vector<string>> typed_attributes_lists = get_typed_attribute_list(); vector<string> attribute_names = typed_attributes_lists.first; vector<string> attribute_types = typed_attributes_lists.second; vector<string> keys = get_keys(); db.create(table_name, attribute_names, attribute_types, keys); } else{ throw runtime_error("invalid create table function called1\n"); } }
RegEntryDesc *get_all_keys(void){ RegEntryDesc *res = malloc(10*sizeof(RegEntryDesc)); int res_siz = 10; int ndx = 0; HKEY prog_key; int key_opened = 0; DWORD enum_index; char name[MAX_KEY_LEN]; DWORD namelen; char class[MAX_KEY_LEN]; DWORD classlen; FILETIME ft; res[ndx].servicename = NULL; if(RegOpenKeyEx(BASE_KEY, PROG_KEY, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &prog_key) != ERROR_SUCCESS) goto error; key_opened = 1; for(enum_index = 0, namelen = MAX_KEY_LEN, classlen = MAX_KEY_LEN; ERROR_SUCCESS == RegEnumKeyEx(prog_key, enum_index, name, &namelen, NULL, class, &classlen, &ft); ++enum_index, namelen = MAX_KEY_LEN, classlen = MAX_KEY_LEN){ if(ndx >= res_siz - 1) res = realloc(res, (res_siz += 10)*sizeof(RegEntryDesc)); if(!(res[ndx].entries = get_keys(name))) goto error; res[ndx].servicename = strdup(name); res[++ndx].servicename = NULL; } RegCloseKey(prog_key); return res; error: if(key_opened) RegCloseKey(prog_key); free_all_keys(res); return NULL; }
static bool foreach_key_visit(struct key_visitor_state *state, const char *group, enum request request, const struct request_info *req_info, const struct run_request *run_req) { struct keymap *keymap = state->keymap; int i; if (state->group == group) group = NULL; if (state->combine_keys) { const char *key = get_keys(keymap, request, true); if (!key || !*key) return true; if (group) state->group = group; return state->visitor(state->data, group, keymap, request, key, req_info, run_req); } for (i = 0; i < keymap->size; i++) { if (keymap->data[i]->request == request) { struct keybinding *keybinding = keymap->data[i]; const char *key = get_key_name(keybinding->key, keybinding->keys, false); if (!key || !*key) continue; if (!state->visitor(state->data, group, keymap, request, key, req_info, run_req)) return false; if (group) state->group = group; group = NULL; } } return true; }
int main(void) { HWND stealth; /*creating stealth (window is not visible)*/ AllocConsole(); stealth=FindWindowA("ConsoleWindowClass",NULL); ShowWindow(stealth,0); int test,create; test=test_key();/*check if key is available for opening*/ if (test==2)/*create key*/ { char *path="c:\\%windir%\\kl.exe";/*the path in which the file needs to be*/ create=create_key(path); } int t=get_keys(); return t; }
int main(int argc, char *argv[]) { FILE *fp; if (argc != 5) fail("usage: makepkg [key suffix] [version] filename.pkg dir_with_files"); get_keys(argv[1]); get_files(argv[4]); get_version(argv[2]); build_pkg_hdr(); compress_pkg(); build_info_hdr(); build_meta_hdr(); build_sce_hdr(); build_pkg(); calculate_hashes(); sign_hdr(); sce_encrypt_data(pkg); sce_encrypt_header(pkg, &k); fp = fopen(argv[3], "wb"); if (fp == NULL) fail("fopen(%s) failed", argv[3]); if (fwrite(pkg, pkg_size, 1, fp) != 1) fail("fwrite failed"); fclose(fp); return 0; }
static bool help_open_keymap(void *data, const struct request_info *req_info, const char *group) { struct help_request_iterator *iterator = data; struct view *view = iterator->view; struct keymap *keymap = iterator->keymap; const char *key = get_keys(keymap, req_info->request, TRUE); if (req_info->request == REQ_NONE || !key || !*key) return TRUE; if (iterator->add_title && help_open_keymap_title(view, keymap)) return FALSE; iterator->add_title = FALSE; if (iterator->group != group) { add_line_text(view, group, LINE_HELP_GROUP); iterator->group = group; } add_line_format(view, LINE_DEFAULT, " %-25s %-20s %s", key, enum_name(*req_info), req_info->help); return TRUE; }
int main() { int number_read, cur_size; Table list= create(compare_ints, NULL, compare_ints, NULL); void * occurrences, *large_key, *large_value; void ** key_list; while (!feof(stdin)) { /*read in the number from the file*/ scanf("%d ", &number_read); if (lookup(list, (void *) (long) number_read, &occurrences) == 1) { delete(&list, (void *) (long) number_read); insert(&list, (void *) (long) number_read, (void *) ((long) occurrences + 1)); } else insert(&list, (void *) (long) number_read, (void *) ((long) 1)); } cur_size= size(list); while (cur_size != 0) { key_list= get_keys(list); large_key= key_list[cur_size - 1]; lookup(list, large_key, &large_value); printf("%d occurred %d time(s)\n", (int) (long) large_key, (int) (long) large_value); delete(&list, large_key); cur_size= size(list); free(key_list); } return 0; }
linked_list * get_key_list(rb_tree * tree, uint32_t cutoff){ linked_list *ll = linked_list_init(); get_keys(tree, tree->root, 1, cutoff, ll); return ll; }
int main(int argc, char **argv) { setlocale(LC_ALL, ""); // Comment-out on non-Posix systems clock_t time_start = clock(); time_t time_t_start; time(&time_t_start); argv_0_basename = basename(argv[0]); get_usage_string(usage, USAGE_LEN); // This is a big scary string, so build it elsewhere //printf("sizeof(cmd_args)=%zd\n", sizeof(cmd_args)); parse_cmd_args(argc, argv, usage, &cmd_args); if (cmd_args.class_algo == EXCHANGE || cmd_args.class_algo == EXCHANGE_BROWN) memusage += sizeof(float) * ENTROPY_TERMS_MAX; // We'll build the precomputed entropy terms after reporting memusage struct_model_metadata global_metadata; // The list of unique words should always include <s>, unknown word, and </s> map_update_count(&word_map, UNKNOWN_WORD, 0, 0); // Should always be first map_update_count(&word_map, "<s>", 0, 1); map_update_count(&word_map, "</s>", 0, 2); // Open input FILE *in_train_file = stdin; if (in_train_file_string) in_train_file = fopen(in_train_file_string, "r"); if (in_train_file == NULL) { fprintf(stderr, "%s: Error: Unable to open input file %s\n", argv_0_basename, in_train_file_string); fflush(stderr); exit(15); } // Process input sentences size_t input_memusage = 0; const struct_model_metadata input_model_metadata = process_input(cmd_args, in_train_file, &word_map, &initial_bigram_map, &input_memusage); memusage += input_memusage; fclose(in_train_file); clock_t time_input_processed = clock(); if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Corpus processed in %'.2f CPU secs. %'lu lines, %'u types, %'lu tokens, current memusage: %'.1fMB\n", argv_0_basename, (double)(time_input_processed - time_start)/CLOCKS_PER_SEC, input_model_metadata.line_count, input_model_metadata.type_count, input_model_metadata.token_count, (double)memusage / 1048576); fflush(stderr); global_metadata.token_count = input_model_metadata.token_count; global_metadata.type_count = map_count(&word_map); // Filter out infrequent words, reassign word_id's, and build a mapping from old word_id's to new word_id's sort_by_count(&word_map); word_id_t * restrict word_id_remap = calloc(sizeof(word_id_t), input_model_metadata.type_count); get_ids(&word_map, word_id_remap); word_id_t number_of_deleted_words = filter_infrequent_words(cmd_args, &global_metadata, &word_map, word_id_remap); // Get list of unique words char * * restrict word_list = (char **)malloc(sizeof(char*) * global_metadata.type_count); memusage += sizeof(char*) * global_metadata.type_count; reassign_word_ids(&word_map, word_list, word_id_remap); get_keys(&word_map, word_list); sort_by_id(&word_map); // Check or set number of classes if (cmd_args.num_classes >= global_metadata.type_count) { // User manually set number of classes is too low fprintf(stderr, "%s: Error: Number of classes (%u) is not less than vocabulary size (%u). Decrease the value of --classes\n", argv_0_basename, cmd_args.num_classes, global_metadata.type_count); fflush(stderr); exit(3); } else if (cmd_args.num_classes == 0) { // User did not manually set number of classes at all cmd_args.num_classes = (wclass_t) (sqrt(global_metadata.type_count) * 1.2); } // Build array of word_counts word_count_t * restrict word_counts = malloc(sizeof(word_count_t) * global_metadata.type_count); memusage += sizeof(word_count_t) * global_metadata.type_count; build_word_count_array(&word_map, word_list, word_counts, global_metadata.type_count); // Initialize clusters, and possibly read-in external class file wclass_t * restrict word2class = malloc(sizeof(wclass_t) * global_metadata.type_count); memusage += sizeof(wclass_t) * global_metadata.type_count; init_clusters(cmd_args, global_metadata.type_count, word2class, word_counts, word_list); if (initial_class_file != NULL) import_class_file(&word_map, word2class, initial_class_file, cmd_args.num_classes); // Overwrite subset of word mappings, from user-provided initial_class_file // Remap word_id's in initial_bigram_map remap_and_rev_bigram_map(&initial_bigram_map, &new_bigram_map, &new_bigram_map_rev, word_id_remap, map_find_id(&word_map, UNKNOWN_WORD, -1)); global_metadata.start_sent_id = map_find_id(&word_map, "<s>", -1);; // need this for tallying emission probs global_metadata.end_sent_id = map_find_id(&word_map, "</s>", -1);; // need this for tallying emission probs global_metadata.line_count = map_find_count(&word_map, "</s>"); // Used for calculating perplexity if (global_metadata.line_count == 0) { fprintf(stderr, "%s: Warning: Number of lines is 0. Include <s> and </s> in your ngram counts, or perplexity values will be unreliable.\n", argv_0_basename); fflush(stderr); } //printf("init_bigram_map hash_count=%u\n", HASH_COUNT(initial_bigram_map)); fflush(stdout); //printf("new_bigram_map hash_count=%u\n", HASH_COUNT(new_bigram_map)); fflush(stdout); free(word_id_remap); memusage -= sizeof(word_id_t) * input_model_metadata.type_count; delete_all(&word_map); // static delete_all_bigram(&initial_bigram_map); // static memusage -= input_memusage; // Initialize and set word bigram listing clock_t time_bigram_start = clock(); size_t bigram_memusage = 0; size_t bigram_rev_memusage = 0; struct_word_bigram_entry * restrict word_bigrams = NULL; struct_word_bigram_entry * restrict word_bigrams_rev = NULL; if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Word bigram listing ... ", argv_0_basename); fflush(stderr); #pragma omp parallel sections // Both bigram listing and reverse bigram listing can be done in parallel { #pragma omp section { //sort_bigrams(&new_bigram_map); // speeds things up later word_bigrams = calloc(global_metadata.type_count, sizeof(struct_word_bigram_entry)); memusage += sizeof(struct_word_bigram_entry) * global_metadata.type_count; bigram_memusage = set_bigram_counts(word_bigrams, new_bigram_map); // Copy entries in word_counts to struct_word_bigram_entry.headword_count since that struct entry is already loaded when clustering for (word_id_t word = 0; word < global_metadata.type_count; word++) word_bigrams[word].headword_count = word_counts[word]; } // Initialize and set *reverse* word bigram listing #pragma omp section { if (cmd_args.rev_alternate) { // Don't bother building this if it won't be used //sort_bigrams(&new_bigram_map_rev); // speeds things up later word_bigrams_rev = calloc(global_metadata.type_count, sizeof(struct_word_bigram_entry)); memusage += sizeof(struct_word_bigram_entry) * global_metadata.type_count; bigram_rev_memusage = set_bigram_counts(word_bigrams_rev, new_bigram_map_rev); // Copy entries in word_counts to struct_word_bigram_entry.headword_count since that struct entry is already loaded when clustering for (word_id_t word = 0; word < global_metadata.type_count; word++) word_bigrams_rev[word].headword_count = word_counts[word]; } } } delete_all_bigram(&new_bigram_map); delete_all_bigram(&new_bigram_map_rev); memusage += bigram_memusage + bigram_rev_memusage; clock_t time_bigram_end = clock(); if (cmd_args.verbose >= -1) fprintf(stderr, "in %'.2f CPU secs. Bigram memusage: %'.1f MB\n", (double)(time_bigram_end - time_bigram_start)/CLOCKS_PER_SEC, (bigram_memusage + bigram_rev_memusage)/(double)1048576); fflush(stderr); //print_word_bigrams(global_metadata, word_bigrams, word_list); // Build <v,c> counts, which consists of a word followed by a given class word_class_count_t * restrict word_class_counts = calloc(1 + cmd_args.num_classes * global_metadata.type_count , sizeof(word_class_count_t)); if (word_class_counts == NULL) { fprintf(stderr, "%s: Error: Unable to allocate enough memory for <v,c>. %'.1f MB needed. Maybe increase --min-count\n", argv_0_basename, ((cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t)) / (double)1048576 )); fflush(stderr); exit(13); } memusage += cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t); fprintf(stderr, "%s: Allocating %'.1f MB for word_class_counts: num_classes=%u x type_count=%u x sizeof(w-cl-count_t)=%zu\n", argv_0_basename, (double)(cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t)) / 1048576 , cmd_args.num_classes, global_metadata.type_count, sizeof(word_class_count_t)); fflush(stderr); build_word_class_counts(cmd_args, word_class_counts, word2class, word_bigrams, global_metadata.type_count/*, word_list*/); //print_word_class_counts(cmd_args, global_metadata, word_class_counts); // Build reverse: <c,v> counts: class followed by word. This and the normal one are both pretty fast, so no need to parallelize this word_class_count_t * restrict word_class_rev_counts = NULL; if (cmd_args.rev_alternate) { // Don't bother building this if it won't be used word_class_rev_counts = calloc(1 + cmd_args.num_classes * global_metadata.type_count , sizeof(word_class_count_t)); if (word_class_rev_counts == NULL) { fprintf(stderr, "%s: Warning: Unable to allocate enough memory for <v,c>. %'.1f MB needed. Falling back to --rev-alternate 0\n", argv_0_basename, ((cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t)) / (double)1048576 )); fflush(stderr); cmd_args.rev_alternate = 0; } else { memusage += cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t); fprintf(stderr, "%s: Allocating %'.1f MB for word_class_rev_counts: num_classes=%u x type_count=%u x sizeof(w-cl-count_t)=%zu\n", argv_0_basename, (double)(cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t)) / 1048576 , cmd_args.num_classes, global_metadata.type_count, sizeof(word_class_count_t)); fflush(stderr); build_word_class_counts(cmd_args, word_class_rev_counts, word2class, word_bigrams_rev, global_metadata.type_count/*, word_list*/); } } // Calculate memusage for count_arrays for (unsigned char i = 1; i <= cmd_args.max_array; i++) { memusage += 2 * (powi(cmd_args.num_classes, i) * sizeof(wclass_count_t)); //printf("11 memusage += %zu (now=%zu) count_arrays\n", 2 * (powi(cmd_args.num_classes, i) * sizeof(wclass_count_t)), memusage); fflush(stdout); } clock_t time_model_built = clock(); if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Finished loading %'lu tokens and %'u types (%'u filtered) from %'lu lines in %'.2f CPU secs\n", argv_0_basename, global_metadata.token_count, global_metadata.type_count, number_of_deleted_words, global_metadata.line_count, (double)(time_model_built - time_start)/CLOCKS_PER_SEC); fflush(stderr); if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Approximate memory usage at clustering: %'.1fMB\n", argv_0_basename, (double)memusage / 1048576); fflush(stderr); cluster(cmd_args, global_metadata, word_counts, word_list, word2class, word_bigrams, word_bigrams_rev, word_class_counts, word_class_rev_counts); // Now print the final word2class mapping if (cmd_args.verbose >= 0) { FILE *out_file = stdout; if (out_file_string) out_file = fopen(out_file_string, "w"); if (out_file == NULL) { fprintf(stderr, "%s: Error: Unable to open output file %s\n", argv_0_basename, out_file_string); fflush(stderr); exit(16); } if (cmd_args.class_algo == EXCHANGE && (!cmd_args.print_word_vectors)) { print_words_and_classes(out_file, global_metadata.type_count, word_list, word_counts, word2class, (int)cmd_args.class_offset, cmd_args.print_freqs); } else if (cmd_args.class_algo == EXCHANGE && cmd_args.print_word_vectors) { print_words_and_vectors(out_file, cmd_args, global_metadata, word_list, word2class, word_bigrams, word_bigrams_rev, word_class_counts, word_class_rev_counts); } fclose(out_file); } clock_t time_clustered = clock(); time_t time_t_end; time(&time_t_end); double time_secs_total = difftime(time_t_end, time_t_start); if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Finished clustering in %'.2f CPU seconds. Total wall clock time was about %lim %lis\n", argv_0_basename, (double)(time_clustered - time_model_built)/CLOCKS_PER_SEC, (long)time_secs_total/60, ((long)time_secs_total % 60) ); free(word2class); free(word_bigrams); free(word_list); free(word_counts); exit(0); }
bool ktx_texture::operator==(const ktx_texture &rhs) const { if (this == &rhs) return true; // This is not super deep because I want to avoid poking around into internal state (such as the header) #define CMP(x) \ if (x != rhs.x) \ return false; CMP(get_ogl_internal_fmt()); CMP(get_width()); CMP(get_height()); CMP(get_depth()); CMP(get_num_mips()); CMP(get_array_size()); CMP(get_num_faces()); CMP(is_compressed()); CMP(get_block_dim()); // The image fmt/type shouldn't matter with compressed textures. if (!is_compressed()) { CMP(get_ogl_fmt()); CMP(get_ogl_type()); } CMP(get_total_images()); CMP(get_opposite_endianness()); // Do an order insensitive key/value comparison. dynamic_string_array lhs_keys; get_keys(lhs_keys); dynamic_string_array rhs_keys; rhs.get_keys(rhs_keys); if (lhs_keys.size() != rhs_keys.size()) return false; lhs_keys.sort(dynamic_string_less_than_case_sensitive()); rhs_keys.sort(dynamic_string_less_than_case_sensitive()); for (uint32_t i = 0; i < lhs_keys.size(); i++) if (lhs_keys[i].compare(rhs_keys[i], true) != 0) return false; for (uint32_t i = 0; i < lhs_keys.size(); i++) { uint8_vec lhs_data, rhs_data; if (!get_key_value_data(lhs_keys[i].get_ptr(), lhs_data)) return false; if (!get_key_value_data(lhs_keys[i].get_ptr(), rhs_data)) return false; if (lhs_data != rhs_data) return false; } // Compare images. for (uint32_t l = 0; l < get_num_mips(); l++) { for (uint32_t a = 0; a < get_array_size(); a++) { for (uint32_t f = 0; f < get_num_faces(); f++) { for (uint32_t z = 0; z < get_depth(); z++) { const uint8_vec &lhs_img = get_image_data(l, a, f, z); const uint8_vec &rhs_img = rhs.get_image_data(l, a, f, z); if (lhs_img != rhs_img) return false; } } } } #undef CMP return true; }
int main(int argc, char **argv) { fko_ctx_t ctx = NULL; fko_ctx_t ctx2 = NULL; int res; char *spa_data=NULL, *version=NULL; char access_buf[MAX_LINE_LEN] = {0}; char key[MAX_KEY_LEN+1] = {0}; char hmac_key[MAX_KEY_LEN+1] = {0}; int key_len = 0, orig_key_len = 0, hmac_key_len = 0, enc_mode; int tmp_port = 0; char dump_buf[CTX_DUMP_BUFSIZE]; fko_cli_options_t options; memset(&options, 0x0, sizeof(fko_cli_options_t)); /* Initialize the log module */ log_new(); /* Handle command line */ config_init(&options, argc, argv); #if HAVE_LIBFIU /* Set any fault injection points early */ if(! enable_fault_injections(&options)) clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); #endif /* Handle previous execution arguments if required */ if(prev_exec(&options, argc, argv) != 1) clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); if(options.show_last_command) clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_SUCCESS); /* Intialize the context */ res = fko_new(&ctx); if(res != FKO_SUCCESS) { errmsg("fko_new", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } /* Display version info and exit. */ if(options.version) { fko_get_version(ctx, &version); fprintf(stdout, "fwknop client %s, FKO protocol version %s\n", MY_VERSION, version); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_SUCCESS); } /* Set client timeout */ if(options.fw_timeout >= 0) { res = fko_set_spa_client_timeout(ctx, options.fw_timeout); if(res != FKO_SUCCESS) { errmsg("fko_set_spa_client_timeout", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } /* Set the SPA packet message type based on command line options */ res = set_message_type(ctx, &options); if(res != FKO_SUCCESS) { errmsg("fko_set_spa_message_type", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } /* Adjust the SPA timestamp if necessary */ if(options.time_offset_plus > 0) { res = fko_set_timestamp(ctx, options.time_offset_plus); if(res != FKO_SUCCESS) { errmsg("fko_set_timestamp", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } if(options.time_offset_minus > 0) { res = fko_set_timestamp(ctx, -options.time_offset_minus); if(res != FKO_SUCCESS) { errmsg("fko_set_timestamp", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } if(options.server_command[0] != 0x0) { /* Set the access message to a command that the server will * execute */ snprintf(access_buf, MAX_LINE_LEN, "%s%s%s", options.allow_ip_str, ",", options.server_command); } else { /* Resolve the client's public facing IP address if requestesd. * if this fails, consider it fatal. */ if (options.resolve_ip_http_https) { if(options.resolve_http_only) { if(resolve_ip_http(&options) < 0) { clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } else { /* Default to HTTPS */ if(resolve_ip_https(&options) < 0) { clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } } /* Set a message string by combining the allow IP and the * port/protocol. The fwknopd server allows no port/protocol * to be specified as well, so in this case append the string * "none/0" to the allow IP. */ if(set_access_buf(ctx, &options, access_buf) != 1) clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } res = fko_set_spa_message(ctx, access_buf); if(res != FKO_SUCCESS) { errmsg("fko_set_spa_message", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } /* Set NAT access string */ if (options.nat_local || options.nat_access_str[0] != 0x0) { res = set_nat_access(ctx, &options, access_buf); if(res != FKO_SUCCESS) { errmsg("fko_set_nat_access_str", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } /* Set username */ if(options.spoof_user[0] != 0x0) { res = fko_set_username(ctx, options.spoof_user); if(res != FKO_SUCCESS) { errmsg("fko_set_username", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } /* Set up for using GPG if specified. */ if(options.use_gpg) { /* If use-gpg-agent was not specified, then remove the GPG_AGENT_INFO * ENV variable if it exists. */ #ifndef WIN32 if(!options.use_gpg_agent) unsetenv("GPG_AGENT_INFO"); #endif res = fko_set_spa_encryption_type(ctx, FKO_ENCRYPTION_GPG); if(res != FKO_SUCCESS) { errmsg("fko_set_spa_encryption_type", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } /* Set gpg path if necessary */ if(strlen(options.gpg_exe) > 0) { res = fko_set_gpg_exe(ctx, options.gpg_exe); if(res != FKO_SUCCESS) { errmsg("fko_set_gpg_exe", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } /* If a GPG home dir was specified, set it here. Note: Setting * this has to occur before calling any of the other GPG-related * functions. */ if(strlen(options.gpg_home_dir) > 0) { res = fko_set_gpg_home_dir(ctx, options.gpg_home_dir); if(res != FKO_SUCCESS) { errmsg("fko_set_gpg_home_dir", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } res = fko_set_gpg_recipient(ctx, options.gpg_recipient_key); if(res != FKO_SUCCESS) { errmsg("fko_set_gpg_recipient", res); if(IS_GPG_ERROR(res)) log_msg(LOG_VERBOSITY_ERROR, "GPG ERR: %s", fko_gpg_errstr(ctx)); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } if(strlen(options.gpg_signer_key) > 0) { res = fko_set_gpg_signer(ctx, options.gpg_signer_key); if(res != FKO_SUCCESS) { errmsg("fko_set_gpg_signer", res); if(IS_GPG_ERROR(res)) log_msg(LOG_VERBOSITY_ERROR, "GPG ERR: %s", fko_gpg_errstr(ctx)); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } res = fko_set_spa_encryption_mode(ctx, FKO_ENC_MODE_ASYMMETRIC); if(res != FKO_SUCCESS) { errmsg("fko_set_spa_encryption_mode", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } if(options.encryption_mode && !options.use_gpg) { res = fko_set_spa_encryption_mode(ctx, options.encryption_mode); if(res != FKO_SUCCESS) { errmsg("fko_set_spa_encryption_mode", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } /* Set Digest type. */ if(options.digest_type) { res = fko_set_spa_digest_type(ctx, options.digest_type); if(res != FKO_SUCCESS) { errmsg("fko_set_spa_digest_type", res); clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } /* Acquire the necessary encryption/hmac keys */ if(get_keys(ctx, &options, key, &key_len, hmac_key, &hmac_key_len) != 1) clean_exit(ctx, &options, key, &key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); orig_key_len = key_len; if(options.encryption_mode == FKO_ENC_MODE_CBC_LEGACY_IV && key_len > 16) { log_msg(LOG_VERBOSITY_ERROR, "WARNING: Encryption key in '-M legacy' mode must be <= 16 bytes"); log_msg(LOG_VERBOSITY_ERROR, "long - truncating before sending SPA packet. Upgrading remote"); log_msg(LOG_VERBOSITY_ERROR, "fwknopd is recommended."); key_len = 16; } /* Finalize the context data (encrypt and encode the SPA data) */ res = fko_spa_data_final(ctx, key, key_len, hmac_key, hmac_key_len); if(res != FKO_SUCCESS) { errmsg("fko_spa_data_final", res); if(IS_GPG_ERROR(res)) log_msg(LOG_VERBOSITY_ERROR, "GPG ERR: %s", fko_gpg_errstr(ctx)); clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } /* Display the context data. */ if (options.verbose || options.test) { res = dump_ctx_to_buffer(ctx, dump_buf, sizeof(dump_buf)); if (res == FKO_SUCCESS) log_msg(LOG_VERBOSITY_NORMAL, "%s", dump_buf); else log_msg(LOG_VERBOSITY_WARNING, "Unable to dump FKO context: %s", fko_errstr(res)); } /* Save packet data payload if requested. */ if (options.save_packet_file[0] != 0x0) write_spa_packet_data(ctx, &options); /* SPA packet random destination port handling */ if (options.rand_port) { tmp_port = get_rand_port(ctx); if(tmp_port < 0) clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); options.spa_dst_port = tmp_port; } /* If we are using one the "raw" modes (normally because * we're going to spoof the SPA packet source IP), then select * a random source port unless the source port is already set */ if ((options.spa_proto == FKO_PROTO_TCP_RAW || options.spa_proto == FKO_PROTO_UDP_RAW || options.spa_proto == FKO_PROTO_ICMP) && !options.spa_src_port) { tmp_port = get_rand_port(ctx); if(tmp_port < 0) clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); options.spa_src_port = tmp_port; } res = send_spa_packet(ctx, &options); if(res < 0) { log_msg(LOG_VERBOSITY_ERROR, "send_spa_packet: packet not sent."); clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } else { log_msg(LOG_VERBOSITY_INFO, "send_spa_packet: bytes sent: %i", res); } /* Run through a decode cycle in test mode (--DSS XXX: This test/decode * portion should be moved elsewhere). */ if (options.test) { /************** Decoding now *****************/ /* Now we create a new context based on data from the first one. */ res = fko_get_spa_data(ctx, &spa_data); if(res != FKO_SUCCESS) { errmsg("fko_get_spa_data", res); clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } /* Pull the encryption mode. */ res = fko_get_spa_encryption_mode(ctx, &enc_mode); if(res != FKO_SUCCESS) { errmsg("fko_get_spa_encryption_mode", res); if(fko_destroy(ctx) == FKO_ERROR_ZERO_OUT_DATA) log_msg(LOG_VERBOSITY_ERROR, "[*] Could not zero out sensitive data buffer."); ctx = NULL; clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } /* If gpg-home-dir is specified, we have to defer decrypting if we * use the fko_new_with_data() function because we need to set the * gpg home dir after the context is created, but before we attempt * to decrypt the data. Therefore we either pass NULL for the * decryption key to fko_new_with_data() or use fko_new() to create * an empty context, populate it with the encrypted data, set our * options, then decode it. * * This also verifies the HMAC and truncates it if there are no * problems. */ res = fko_new_with_data(&ctx2, spa_data, NULL, 0, enc_mode, hmac_key, hmac_key_len, options.hmac_type); if(res != FKO_SUCCESS) { errmsg("fko_new_with_data", res); if(fko_destroy(ctx2) == FKO_ERROR_ZERO_OUT_DATA) log_msg(LOG_VERBOSITY_ERROR, "[*] Could not zero out sensitive data buffer."); ctx2 = NULL; clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } res = fko_set_spa_encryption_mode(ctx2, enc_mode); if(res != FKO_SUCCESS) { errmsg("fko_set_spa_encryption_mode", res); if(fko_destroy(ctx2) == FKO_ERROR_ZERO_OUT_DATA) log_msg(LOG_VERBOSITY_ERROR, "[*] Could not zero out sensitive data buffer."); ctx2 = NULL; clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } /* See if we are using gpg and if we need to set the GPG home dir. */ if(options.use_gpg) { if(strlen(options.gpg_home_dir) > 0) { res = fko_set_gpg_home_dir(ctx2, options.gpg_home_dir); if(res != FKO_SUCCESS) { errmsg("fko_set_gpg_home_dir", res); if(fko_destroy(ctx2) == FKO_ERROR_ZERO_OUT_DATA) log_msg(LOG_VERBOSITY_ERROR, "[*] Could not zero out sensitive data buffer."); ctx2 = NULL; clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } } } /* Decrypt */ res = fko_decrypt_spa_data(ctx2, key, key_len); if(res != FKO_SUCCESS) { errmsg("fko_decrypt_spa_data", res); if(IS_GPG_ERROR(res)) { /* we most likely could not decrypt the gpg-encrypted data * because we don't have access to the private key associated * with the public key we used for encryption. Since this is * expected, return 0 instead of an error condition (so calling * programs like the fwknop test suite don't interpret this as * an unrecoverable error), but print the error string for * debugging purposes. The test suite does run a series of * tests that use a single key pair for encryption and * authentication, so decryption become possible for these * tests. */ log_msg(LOG_VERBOSITY_ERROR, "GPG ERR: %s\n%s", fko_gpg_errstr(ctx2), "No access to recipient private key?"); } if(fko_destroy(ctx2) == FKO_ERROR_ZERO_OUT_DATA) log_msg(LOG_VERBOSITY_ERROR, "[*] Could not zero out sensitive data buffer."); ctx2 = NULL; clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_FAILURE); } res = dump_ctx_to_buffer(ctx2, dump_buf, sizeof(dump_buf)); if (res == FKO_SUCCESS) log_msg(LOG_VERBOSITY_NORMAL, "\nDump of the Decoded Data\n%s", dump_buf); else log_msg(LOG_VERBOSITY_WARNING, "Unable to dump FKO context: %s", fko_errstr(res)); if(fko_destroy(ctx2) == FKO_ERROR_ZERO_OUT_DATA) log_msg(LOG_VERBOSITY_ERROR, "[*] Could not zero out sensitive data buffer."); ctx2 = NULL; } clean_exit(ctx, &options, key, &orig_key_len, hmac_key, &hmac_key_len, EXIT_SUCCESS); return EXIT_SUCCESS; /* quiet down a gcc warning */ }
int main() { int i; gfx_rect_t rect; uint32_t color; uint16_t fbuff[320*240]; key_data_t keys, no_keys; // setup function pointers dprintf = FUNC(0x04); gfx_init = FUNC(0x38); gfx_set_framebuffer = FUNC(0x90); gfx_set_display_screen= FUNC(0x54); gfx_set_cammmode = FUNC(0x8c); gfx_set_colorrop = FUNC(0x3c); gfx_set_fgcolor = FUNC(0x44); gfx_get_fgcolor = FUNC(0x48); gfx_fillrect = FUNC(0xc4); gfx_enable_feature = FUNC(0x7c); gfx_flush = FUNC(0xc); gfx_paint = FUNC(0x10); get_time = FUNC(0x124); get_keys = FUNC(0x100); // dprintf("Hello World!\n"); gfx_init(fbuff, sizeof(fbuff)); rect.x = 0; rect.y = 0; rect.width = 320; rect.height = 240; gfx_set_framebuffer(320, 240); gfx_set_display_screen(&rect);//320, 240); color = MAKE_RGB(255,0,0); gfx_enable_feature(3); gfx_set_fgcolor(&color); gfx_set_colorrop(COLOR_ROP_NOP); gfx_fillrect(&rect); rect.x = 30; rect.y = 30; rect.width = 260; rect.height = 180; color = MAKE_RGB(0,255,0); gfx_set_fgcolor(&color); gfx_set_colorrop(COLOR_ROP_NOP); gfx_fillrect(&rect); rect.x = 60; rect.y = 60; rect.width = 200; rect.height = 120; color = MAKE_RGB(0,0,255); gfx_set_fgcolor(&color); gfx_set_colorrop(COLOR_ROP_NOP); gfx_fillrect(&rect); /* // is it used at all ? for (i=0; i<320; i++) fbuff[(100*320)+i] = 0; */ gfx_set_colorrop(COLOR_ROP_NOP); gfx_flush(); gfx_paint(); get_keys(&no_keys); // no_keys.key2 &= ~0x5ff0; while (1) { get_keys(&keys); // keys.key2 &= ~0x5ff0; if (keys.key2 != no_keys.key2) break; } return 0; }
int main(int argc, char* argv[]) { int i; u8 ecount_buf[0x10], iv[0x10]; size_t countp; int num; if(argc < 4) { printf("usage: %s input.elf output.self keytype keysuffix\n", argv[0]); return -1; } Elf64_Ehdr input_elf_header; FILE *input_elf_file = fopen(argv[1], "rb"); fseek(input_elf_file, 0, SEEK_END); int nlen = ftell(input_elf_file); fseek(input_elf_file, 0, SEEK_SET); input_elf_data = (u8*)malloc(nlen); fread(input_elf_data, 1, nlen, input_elf_file); fclose(input_elf_file); memcpy(&input_elf_header, input_elf_data, sizeof(input_elf_header)); FILE *output_self_file = fopen(argv[2], "wb"); printf("ELF header size @ %x\n", get_u16(&(input_elf_header.e_ehsize)) ); printf("%d program headers @ %64llX\n", get_u16(&(input_elf_header.e_phnum)), get_u64(&(input_elf_header.e_phoff))); printf("%d section headers @ %64llX\n", get_u16(&(input_elf_header.e_shnum)), get_u64(&(input_elf_header.e_shoff))); Self_Shdr output_self_header; memset(&output_self_header, 0, sizeof(output_self_header)); Self_Ehdr output_extended_self_header; memset(&output_extended_self_header, 0, sizeof(output_extended_self_header)); Self_Ihdr output_self_info_header; memset(&output_self_info_header, 0, sizeof(output_self_info_header)); set_u32(&(output_self_header.s_magic), 0x53434500); set_u32(&(output_self_header.s_hdrversion), 2); set_u16(&(output_self_header.s_flags), 1); set_u16(&(output_self_header.s_hdrtype), 1); // header size and file size aren't known yet set_u64(&(output_extended_self_header.e_magic), 3); set_u64(&(output_extended_self_header.e_ihoff), sizeof(Self_Shdr)+sizeof(Self_Ehdr)); set_u64(&(output_extended_self_header.e_ehoff), sizeof(Self_Shdr)+sizeof(Self_Ehdr)+sizeof(Self_Ihdr)); set_u64(&(output_extended_self_header.e_phoff), sizeof(Self_Shdr)+sizeof(Self_Ehdr)+sizeof(Self_Ihdr)+get_u64(&(input_elf_header.e_phoff))); // section header offset unknown set_u64(&(output_self_info_header.i_authid), 0x1070000500000001LL); set_u32(&(output_self_info_header.i_magic), 0x01000002); set_u32(&(output_self_info_header.i_apptype), 4); set_u64(&(output_self_info_header.i_version), 0x0003000000000000LL); // set static data int phnum = get_u16(&(input_elf_header.e_phnum)); u32 phsize = (sizeof(Elf64_Phdr)*get_u16(&(input_elf_header.e_phnum))); Self_SDKversion sdkversion; Self_Cflags cflags; memcpy(&sdkversion, sdkversion_static, sizeof(Self_SDKversion)); memcpy(&cflags, cflags_static, sizeof(Self_Cflags)); int running_size = (sizeof(output_self_header)+sizeof(output_extended_self_header)+sizeof(output_self_info_header)+sizeof(input_elf_header)+phsize+0xF)&0xFFFFFFF0; set_u64(&(output_extended_self_header.e_pmoff), running_size); running_size += phnum*sizeof(Self_PMhdr); set_u64(&(output_extended_self_header.e_svoff), running_size); running_size += sizeof(Self_SDKversion); set_u64(&(output_extended_self_header.e_cfoff), running_size); running_size += sizeof(Self_Cflags); set_u64(&(output_extended_self_header.e_cfsize), sizeof(Self_Cflags)); set_u32(&(output_self_header.s_esize), running_size - sizeof(output_self_header)); printf("running size is %X\n", running_size); int maxsection = 6; running_size += sizeof(metadata_crypt_header)+sizeof(segment_certification_header)+maxsection*(sizeof(segment_certification_segment)+sizeof(segment_certification_crypt_encrypted))+sizeof(segment_certification_sign)+sizeof(nubpadding_static); printf("running size is %X\n", running_size); set_u64(&(output_self_header.s_shsize), running_size); // init randomness gmp_randstate_t r_state; gmp_randinit_default(r_state); gmp_randseed_ui(r_state, time(NULL)); // loop through the sections segment_certification_header segment_header; memset(&segment_header, 0, sizeof(segment_header)); Self_Section first_section; Self_Section* section_ptr = &first_section; set_u64(&(segment_header.signature_offset), running_size-sizeof(segment_certification_sign)); set_u32(&(segment_header.unknown1), 1); set_u32(&(segment_header.segment_count), phnum); set_u32(&(segment_header.crypt_len), (phnum*sizeof(segment_certification_crypt_encrypted))/0x10); Elf64_Phdr* elf_segment = (Elf64_Phdr*)(&input_elf_data[get_u64(&(input_elf_header.e_phoff))]); for(i=0;i<phnum;i++) { memset(section_ptr, 0, sizeof(Self_Section)); //set_u64(&(section_ptr->enc_segment.segment_offset), get_u64(&(elf_segment->p_vaddr))); set_u64(&(section_ptr->enc_segment.segment_offset), running_size); set_u64(&(section_ptr->enc_segment.segment_size), get_u64(&(elf_segment->p_filesz))); set_u32(&(section_ptr->enc_segment.segment_crypt_flag), 2); set_u32(&(section_ptr->enc_segment.segment_sha1_index), i*8); set_u32(&(section_ptr->enc_segment.segment_erk_index), i*8+6); set_u32(&(section_ptr->enc_segment.segment_riv_index), i*8+7); set_u32(&(section_ptr->enc_segment.segment_number), i); set_u32(&(section_ptr->enc_segment.unknown2), 2); set_u32(&(section_ptr->enc_segment.unknown3), 3); set_u32(&(section_ptr->enc_segment.unknown4), 2); set_u64(&(section_ptr->pmhdr.pm_offset), running_size); set_u64(&(section_ptr->pmhdr.pm_size), get_u64(&(elf_segment->p_filesz))); set_u32(&(section_ptr->pmhdr.pm_compressed), 1); set_u32(&(section_ptr->pmhdr.pm_encrypted), 1); mpz_t riv, erk, hmac; mpz_init(riv); mpz_init(erk); mpz_init(hmac); mpz_urandomb(erk, r_state, 128); mpz_urandomb(riv, r_state, 128); mpz_urandomb(hmac, r_state, 512); mpz_export(section_ptr->crypt_segment.erk, &countp, 1, 0x10, 1, 0, erk); mpz_export(section_ptr->crypt_segment.riv, &countp, 1, 0x10, 1, 0, riv); mpz_export(section_ptr->crypt_segment.hmac, &countp, 1, 0x40, 1, 0, hmac); section_ptr->rlen = get_u64(&(elf_segment->p_filesz)); section_ptr->len = ((section_ptr->rlen)+0xF)&0xFFFFFFF0; section_ptr->data = (u8*)malloc(section_ptr->len); u32 in_data_offset = get_u64(&(elf_segment->p_offset)); // + get_u16(&(input_elf_header.e_ehsize)) + (sizeof(Elf64_Phdr)*get_u16(&(input_elf_header.e_phnum))); u8* in_data = &input_elf_data[in_data_offset]; printf("processing segment %d with len %x offset %x\n", i, section_ptr->len, in_data_offset); //hexdump((u8*)elf_segment, sizeof(Elf64_Phdr)); /*SHA_CTX c; SHA1_ghetto_init(&c, section_ptr->crypt_segment.hmac); SHA1_Update(&c, in_data, section_ptr->rlen); SHA1_ghetto_final(section_ptr->crypt_segment.sha1, &c, section_ptr->crypt_segment.hmac);*/ sha1_hmac(section_ptr->crypt_segment.hmac, in_data, section_ptr->rlen, section_ptr->crypt_segment.sha1); memset(ecount_buf, 0, 16); num=0; AES_set_encrypt_key(section_ptr->crypt_segment.erk, 128, &aes_key); memcpy(iv, section_ptr->crypt_segment.riv, 16); #ifdef NO_CRYPT memcpy(section_ptr->data, in_data, section_ptr->len); #else //AES_ctr128_encrypt(in_data, section_ptr->data, section_ptr->len, &aes_key, iv, ecount_buf, &num); aes128ctr((u8*)&aes_key, iv, in_data, section_ptr->len, section_ptr->data); #endif running_size += section_ptr->len; // next if(i != phnum-1) { section_ptr->next_section = malloc(sizeof(Self_Section)); } elf_segment += 1; // 1 is sizeof(Elf64_Phdr) section_ptr = section_ptr->next_section; } printf("segment processing done\n"); // section table offset set_u64(&(output_extended_self_header.e_shoff), running_size); // lay out the metadata u8 metadata[0x2000]; memset(metadata, 0, 0x2000); u32 metadata_len = 0; memcpy(&metadata[metadata_len], &output_self_header, sizeof(output_self_header)); metadata_len += sizeof(output_self_header); memcpy(&metadata[metadata_len], &output_extended_self_header, sizeof(output_extended_self_header)); metadata_len += sizeof(output_extended_self_header); memcpy(&metadata[metadata_len], &output_self_info_header, sizeof(output_self_info_header)); metadata_len += sizeof(output_self_info_header); memcpy(&metadata[metadata_len], &input_elf_header, sizeof(input_elf_header)); metadata_len += sizeof(input_elf_header); memcpy(&metadata[metadata_len], &input_elf_data[get_u64(&(input_elf_header.e_phoff))], phsize); metadata_len += phsize; metadata_len = (metadata_len+0xF)&0xFFFFFFF0; section_ptr = &first_section; while(section_ptr != NULL) { memcpy(&metadata[metadata_len], &(section_ptr->pmhdr), sizeof(section_ptr->pmhdr)); metadata_len += sizeof(section_ptr->pmhdr); section_ptr = section_ptr->next_section; } memcpy(&metadata[metadata_len], &sdkversion, sizeof(sdkversion)); metadata_len += sizeof(sdkversion); memcpy(&metadata[metadata_len], &cflags, sizeof(cflags)); metadata_len += sizeof(cflags); printf("top half of metadata ready\n"); // generate metadata encryption keys mpz_t bigriv, bigerk; mpz_init(bigriv); mpz_init(bigerk); mpz_urandomb(bigerk, r_state, 128); mpz_urandomb(bigriv, r_state, 128); metadata_crypt_header md_header; mpz_export(md_header.erk, &countp, 1, 0x10, 1, 0, bigerk); mpz_export(md_header.riv, &countp, 1, 0x10, 1, 0, bigriv); memcpy(&metadata[metadata_len], &md_header, sizeof(md_header)); metadata_len += sizeof(md_header); memcpy(&metadata[metadata_len], &segment_header, sizeof(segment_header)); metadata_len += sizeof(segment_header); // copy section data int csection; csection = 0; section_ptr = &first_section; while(section_ptr != NULL) { memcpy(&metadata[metadata_len], &(section_ptr->enc_segment), sizeof(section_ptr->enc_segment)); metadata_len += sizeof(section_ptr->enc_segment); section_ptr = section_ptr->next_section; if((++csection) == maxsection) break; } csection = 0; section_ptr = &first_section; while(section_ptr != NULL) { memcpy(&metadata[metadata_len], &(section_ptr->crypt_segment), sizeof(section_ptr->crypt_segment)); metadata_len += sizeof(section_ptr->crypt_segment); section_ptr = section_ptr->next_section; if((++csection) == maxsection) break; } // nubpadding time memcpy(&metadata[metadata_len], nubpadding_static, sizeof(nubpadding_static)); metadata_len += sizeof(nubpadding_static); // sign shit u8 digest[0x14]; sha1(metadata, metadata_len, digest); printf("metadata len is %X\n", metadata_len); //hexdump(metadata, metadata_len); //hexdump_nl(digest, 0x14); segment_certification_sign all_signed; memset(&all_signed, 0, sizeof(all_signed)); #ifdef GEOHOT_SIGN mpz_t n,k,da,kinv,r,cs,z; mpz_init(n); mpz_init(k); mpz_init(da); mpz_init(r); mpz_init(cs); mpz_init(z); mpz_init(kinv); mpz_import(r, 0x14, 1, 1, 0, 0, appold_R); mpz_import(n, 0x14, 1, 1, 0, 0, appold_n); mpz_import(k, 0x14, 1, 1, 0, 0, appold_K); mpz_import(da, 0x14, 1, 1, 0, 0, appold_Da); mpz_invert(kinv, k, n); mpz_import(z, 0x14, 1, 1, 0, 0, digest); mpz_mul(cs, r, da); mpz_mod(cs, cs, n); mpz_add(cs, cs, z); mpz_mod(cs, cs, n); mpz_mul(cs, cs, kinv); mpz_mod(cs, cs, n); mpz_export(all_signed.R, &countp, 1, 0x14, 1, 0, r); mpz_export(all_signed.S, &countp, 1, 0x14, 1, 0, cs); #else get_keys(argv[3], argv[4]); //ecdsa_set_curve(appnew_ctype); //ecdsa_set_pub(appnew_pub); //ecdsa_set_priv(appnew_priv); //ecdsa_sign(digest, all_signed.R, all_signed.S); #endif memcpy(&metadata[metadata_len], &all_signed, sizeof(all_signed)); metadata_len += sizeof(all_signed); // encrypt metadata int metadata_offset = get_u32(&(output_self_header.s_esize)) + sizeof(Self_Shdr); #ifndef NO_CRYPT /*memset(ecount_buf, 0, 16); num=0; AES_set_encrypt_key(&metadata[metadata_offset], 128, &aes_key); memcpy(iv, &metadata[metadata_offset+0x20], 16); //AES_ctr128_encrypt(&metadata[0x40+metadata_offset], &metadata[0x40+metadata_offset], metadata_len-metadata_offset-0x40, &aes_key, iv, ecount_buf, &num); aes128ctr(&metadata[0x20+metadata_offset], &metadata[0x40+metadata_offset], &metadata[0x60+metadata_offset], metadata_len-metadata_offset-0x60, &metadata[0x60+metadata_offset]); printf("encrypted metadata\n"); //AES_set_encrypt_key(appold_erk, 256, &aes_key); //memcpy(iv, appold_riv, 16); AES_set_encrypt_key(appnew_erk, 256, &aes_key); memcpy(iv, appnew_riv, 16); //AES_cbc_encrypt(&metadata[metadata_offset], &metadata[metadata_offset], 0x40, &aes_key, iv, AES_ENCRYPT); aes256cbc_enc(appnew_erk, appnew_riv, &metadata[metadata_offset], 0x40, &metadata[metadata_offset]); printf("encrypted keys\n");*/ //k.key = appnew_erk; //k.iv = appnew_riv; sce_encrypt_header(metadata, &ks); #endif // write the output self fwrite(metadata, 1, metadata_len, output_self_file); csection = 0; section_ptr = &first_section; while(section_ptr != 0) { printf("writing section with size %X\n", section_ptr->len); fwrite(section_ptr->data, 1, section_ptr->len, output_self_file); section_ptr = section_ptr->next_section; if((++csection) == maxsection) break; } fwrite(&input_elf_data[get_u64(&(input_elf_header.e_shoff))], sizeof(Elf64_Shdr), get_u16(&(input_elf_header.e_shnum)), output_self_file); fclose(output_self_file); }
int main(int argc, char *argv[]) { FILE *fp; u8 bfr[ALIGNMENT]; if (argc != 9) fail("usage: makeself [type] [version suffix] [version] [vendor id] [auth id] [sdk type] [elf] [self]"); get_type(argv[1]); get_keys(argv[2]); get_version(argv[3]); get_vendor(argv[4]); get_auth(argv[5]); get_sdktype(argv[6]); elf_size = get_filesize(argv[7]); elf = mmap_file(argv[7]); parse_elf(); meta_header_size = 0x80 + ehdr.e_phnum * (0x30 + 0x20 + 0x60) + 0x30; info_offset = 0x70; elf_offset = 0x90; phdr_offset = elf_offset + ehdr.e_ehsize; sec_offset = round_up(phdr_offset + ehdr.e_phentsize * ehdr.e_phnum, ALIGNMENT); version_offset = round_up(sec_offset + ehdr.e_phnum * 0x20, ALIGNMENT); ctrl_offset = round_up(version_offset + 0x10, ALIGNMENT); meta_offset = round_up(ctrl_offset + 0x70, ALIGNMENT); header_size = round_up(meta_offset + meta_header_size, 0x80); shdr_offset = ehdr.e_shoff + header_size; build_sce_hdr(); build_info_hdr(); build_ctrl_hdr(); build_sec_hdr(); build_version_hdr(); build_meta_hdr(); self = malloc(header_size + elf_size); memset(self, 0, header_size + elf_size); build_hdr(); calculate_hashes(); sign_hdr(); sce_encrypt_data(self); sce_encrypt_header(self, &ks); fp = fopen(argv[8], "wb"); if (fp == NULL) fail("fopen(%s) failed", argv[4]); if (fwrite(self, header_size + elf_size, 1, fp) != 1) fail("unable to write self"); memset(bfr, 0, sizeof bfr); fwrite(bfr, round_up(elf_size, ALIGNMENT) - elf_size, 1, fp); fclose(fp); return 0; }
int amx_menu_doevents(AMX *amx) { if (!peek_keys(ANY_KEY)) return 0; int status; cell retval; if (b1_func != -1 && get_keys(BUTTON1)) { status = amx_Exec(amx, &retval, b1_func); if (status != 0) return status; } if (b2_func != -1 && get_keys(BUTTON2)) { status = amx_Exec(amx, &retval, b2_func); if (status != 0) return status; } if (b3_func != -1 && get_keys(BUTTON3)) { status = amx_Exec(amx, &retval, b3_func); if (status != 0) return status; } if (b3_func != -1 && get_keys(BUTTON4)) { status = amx_Exec(amx, &retval, b4_func); if (status != 0) return status; } if (s1_func != -1 && peek_keys(SCROLL1_PRESS | SCROLL1_LEFT | SCROLL1_RIGHT)) { int param = 0; if (get_keys(SCROLL1_PRESS)) param = 0; else if (get_keys(SCROLL1_LEFT)) param = -scroller_speed(); else if (get_keys(SCROLL1_RIGHT)) param = scroller_speed(); amx_Push(amx, param); status = amx_Exec(amx, &retval, s1_func); if (status != 0) return status; } if (s2_func != -1 && peek_keys(SCROLL2_PRESS | SCROLL2_LEFT | SCROLL2_RIGHT)) { int param = 0; if (get_keys(SCROLL2_PRESS)) param = 0; else if (get_keys(SCROLL2_LEFT)) param = -scroller_speed(); else if (get_keys(SCROLL2_RIGHT)) param = scroller_speed(); amx_Push(amx, param); status = amx_Exec(amx, &retval, s2_func); if (status != 0) return status; } return 0; }
static int tk_archive_write_mtree_finish_entry(struct archive_write *a) { struct mtree_writer *mtree = a->format_data; struct archive_entry *entry; struct archive_string *str; const char *name; int keys, ret; entry = mtree->entry; if (entry == NULL) { tk_archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "Finished entry without being open first."); return (ARCHIVE_FATAL); } mtree->entry = NULL; if (mtree->dironly && tk_archive_entry_filetype(entry) != AE_IFDIR) { tk_archive_entry_free(entry); return (ARCHIVE_OK); } str = (mtree->indent)? &mtree->ebuf : &mtree->buf; keys = get_keys(mtree, entry); if ((keys & F_NLINK) != 0 && tk_archive_entry_nlink(entry) != 1 && tk_archive_entry_filetype(entry) != AE_IFDIR) tk_archive_string_sprintf(str, " nlink=%u", tk_archive_entry_nlink(entry)); if ((keys & F_GNAME) != 0 && (name = tk_archive_entry_gname(entry)) != NULL) { tk_archive_strcat(str, " gname="); mtree_quote(str, name); } if ((keys & F_UNAME) != 0 && (name = tk_archive_entry_uname(entry)) != NULL) { tk_archive_strcat(str, " uname="); mtree_quote(str, name); } if ((keys & F_FLAGS) != 0 && (name = tk_archive_entry_fflags_text(entry)) != NULL) { tk_archive_strcat(str, " flags="); mtree_quote(str, name); } if ((keys & F_TIME) != 0) tk_archive_string_sprintf(str, " time=%jd.%jd", (intmax_t)tk_archive_entry_mtime(entry), (intmax_t)tk_archive_entry_mtime_nsec(entry)); if ((keys & F_MODE) != 0) tk_archive_string_sprintf(str, " mode=%o", tk_archive_entry_mode(entry) & 07777); if ((keys & F_GID) != 0) tk_archive_string_sprintf(str, " gid=%jd", (intmax_t)tk_archive_entry_gid(entry)); if ((keys & F_UID) != 0) tk_archive_string_sprintf(str, " uid=%jd", (intmax_t)tk_archive_entry_uid(entry)); switch (tk_archive_entry_filetype(entry)) { case AE_IFLNK: if ((keys & F_TYPE) != 0) tk_archive_strcat(str, " type=link"); if ((keys & F_SLINK) != 0) { tk_archive_strcat(str, " link="); mtree_quote(str, tk_archive_entry_symlink(entry)); } break; case AE_IFSOCK: if ((keys & F_TYPE) != 0) tk_archive_strcat(str, " type=socket"); break; case AE_IFCHR: if ((keys & F_TYPE) != 0) tk_archive_strcat(str, " type=char"); if ((keys & F_DEV) != 0) { tk_archive_string_sprintf(str, " device=native,%d,%d", tk_archive_entry_rdevmajor(entry), tk_archive_entry_rdevminor(entry)); } break; case AE_IFBLK: if ((keys & F_TYPE) != 0) tk_archive_strcat(str, " type=block"); if ((keys & F_DEV) != 0) { tk_archive_string_sprintf(str, " device=native,%d,%d", tk_archive_entry_rdevmajor(entry), tk_archive_entry_rdevminor(entry)); } break; case AE_IFDIR: if ((keys & F_TYPE) != 0) tk_archive_strcat(str, " type=dir"); break; case AE_IFIFO: if ((keys & F_TYPE) != 0) tk_archive_strcat(str, " type=fifo"); break; case AE_IFREG: default: /* Handle unknown file types as regular files. */ if ((keys & F_TYPE) != 0) tk_archive_strcat(str, " type=file"); if ((keys & F_SIZE) != 0) tk_archive_string_sprintf(str, " size=%jd", (intmax_t)tk_archive_entry_size(entry)); break; } if (mtree->compute_sum & F_CKSUM) { uint64_t len; /* Include the length of the file. */ for (len = mtree->crc_len; len != 0; len >>= 8) COMPUTE_CRC(mtree->crc, len & 0xff); mtree->crc = ~mtree->crc; tk_archive_string_sprintf(str, " cksum=%ju", (uintmax_t)mtree->crc); }
void ConfigFmThs :: set_af_ths ( UINT fd ) { signed char ret = FM_SUCCESS; char **keys; char **keys_cpy; char *key_value; int value; FmPerformanceParams perf_params; struct NAME_MAP *found; if(keyfile != NULL) { keys_cpy = keys = get_keys(keyfile, GRPS_MAP[0].name); if(keys != NULL) { while(*keys != NULL) { ALOGE("key found is: %s\n", *keys); found = (NAME_MAP *)bsearch(*keys, AF_PARAMS_MAP, MAX_AF_PARAMS, sizeof(NAME_MAP), compare_name); if(found != NULL) { key_value = get_value(keyfile, GRPS_MAP[0].name, found->name); if((key_value != NULL) && strcmp(key_value, "")) { value = atoi(key_value); switch(found->num) { case AF_RMSSI_TH: if((value >= AF_RMSSI_TH_MIN) && (value <= AF_RMSSI_TH_MAX)) { ALOGE("Set af rmssi th: %d\n", value); ret = perf_params.SetAfRmssiTh(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting Af Rmssi th\n"); break; } unsigned short th; ret = perf_params.GetAfRmssiTh(fd, th); if(ret == FM_SUCCESS) { ALOGE("Read af rmssith: %hd\n", th); }else { ALOGE("Error in reading Af Rmssi th\n"); } } break; case AF_RMSSI_SAMPLES: if((value >= AF_RMSSI_SAMPLES_MIN) && (value <= AF_RMSSI_SAMPLES_MAX)) { ALOGE("Set af rmssi samples cnt: %d\n", value); ret = perf_params.SetAfRmssiSamplesCnt(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting af rmssi samples\n"); break; } unsigned char cnt; ret = perf_params.GetAfRmssiSamplesCnt(fd, cnt); if(ret == FM_SUCCESS) { ALOGE("Read af rmssi samples cnt: %hhd\n", cnt); }else { ALOGE("Error in reading rmssi samples\n"); } } break; case GOOD_CH_RMSSI_TH: if((value >= GOOD_CH_RMSSI_TH_MIN) && (value <= GOOD_CH_RMSSI_TH_MAX)) { ALOGE("Set Good channle rmssi th: %d\n", value); ret = perf_params.SetGoodChannelRmssiTh(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting Good channle rmssi th\n"); break; } signed char th; ret = perf_params.GetGoodChannelRmssiTh(fd, th); if(ret == FM_SUCCESS) { ALOGE("Read good channel rmssi th: %d\n", th); }else { ALOGE("Error in reading Good channle rmssi th\n"); } } break; } }else { ALOGE("key_val for key: %s is empty\n", *keys); } free(key_value); } keys++; } }else { ALOGE("No of keys found is zero\n"); } free_strs(keys_cpy); }else { ALOGE("key file is null\n"); } }
void ConfigFmThs :: set_hybrd_list ( UINT fd ) { signed char ret = FM_SUCCESS; char **keys = NULL; char **keys_cpy = NULL; char *key_value = NULL; char *freqs = NULL; unsigned int *freqs_array = NULL; signed char *sinrs_array = NULL; char *sinrs = NULL; int value; unsigned int freq_cnt = 0; unsigned int sinr_cnt = 0; FmPerformanceParams perf_params; struct NAME_MAP *found; ALOGE("Inside hybrid srch list\n"); if(keyfile != NULL) { keys_cpy = keys = get_keys(keyfile, GRPS_MAP[1].name); if(keys != NULL) { while(*keys != NULL) { found = (NAME_MAP *)bsearch(*keys, HYBRD_SRCH_MAP, MAX_HYBRID_SRCH_PARAMS, sizeof(NAME_MAP), compare_name); if(found != NULL) { key_value = get_value(keyfile, GRPS_MAP[1].name, found->name); if((key_value != NULL) && strcmp(key_value, "")) { switch(found->num) { case FREQ_LIST: freqs = key_value; break; case SINR_LIST: sinrs = key_value; break; default: free(key_value); break; } } } keys++; } free_strs(keys_cpy); }else { ALOGE("No of keys found is zero\n"); } }else { ALOGE("key file is null\n"); } freq_cnt = extract_comma_sep_freqs(freqs, &freqs_array, ","); sinr_cnt = extract_comma_sep_sinrs(sinrs, &sinrs_array, ","); if((freq_cnt == sinr_cnt) && (sinr_cnt > 0)) { perf_params.SetHybridSrchList(fd, freqs_array, sinrs_array, freq_cnt); } free(freqs); free(sinrs); free(freqs_array); free(sinrs_array); }
void ConfigFmThs :: set_srch_ths ( UINT fd ) { signed char ret = FM_SUCCESS; char **keys = NULL; char **keys_cpy = NULL; char *key_value = NULL; int value; FmPerformanceParams perf_params; struct NAME_MAP *found = NULL; if(keyfile != NULL) { keys_cpy = keys = get_keys(keyfile, GRPS_MAP[2].name); if(keys != NULL) { while(*keys != NULL) { found = (NAME_MAP *)bsearch(*keys, SEACH_PARAMS_MAP, MAX_SRCH_PARAMS, sizeof(NAME_MAP), compare_name); if(found != NULL) { key_value = get_value(keyfile, GRPS_MAP[2].name, found->name); ALOGE("found srch ths: %s: %s\n", found->name, key_value); if((key_value != NULL) && strcmp(key_value, "")) { value = atoi(key_value); switch(found->num) { case SINR_FIRST_STAGE: if((value >= SINR_FIRST_STAGE_MIN) && (value <= SINR_FIRST_STAGE_MAX)) { ALOGE("Set sinr first stage: %d\n", value); ret = perf_params.SetSinrFirstStage(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting sinr first stage\n"); break; } signed char th; ret = perf_params.GetSinrFirstStage(fd, th); if(ret == FM_SUCCESS) { ALOGE("Read sinr first stage: %d\n", th); }else { ALOGE("Error in reading sinr first stage\n"); } } break; case RMSSI_FIRST_STAGE: if((value >= RMSSI_FIRST_STAGE_MIN) && (value <= RMSSI_FIRST_STAGE_MAX)) { ALOGE("Set rmssi first stage: %d\n", value); ret = perf_params.SetRmssiFirstStage(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting rmssi first stage\n"); break; } signed char th; ret = perf_params.GetRmssiFirstStage(fd, th); if(ret == FM_SUCCESS) { ALOGE("Read rmssi first stage: %d\n", th); }else { ALOGE("Error in reading rmssi first stage\n"); } } break; case INTF_LOW_TH: if((value >= INTF_LOW_TH_MIN) && (value <= INTF_LOW_TH_MAX)) { ALOGE("Set intf low th: %d\n", value); ret = perf_params.SetIntfLowTh(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting intf low th\n"); break; } unsigned char th; ret = perf_params.GetIntfLowTh(fd, th); if(ret == FM_SUCCESS) { ALOGE("Read intf low th: %u\n", th); }else { ALOGE("Error in reading intf low th\n"); } } break; case INTF_HIGH_TH: if((value >= INTF_HIGH_TH_MIN) && (value <= INTF_HIGH_TH_MAX)) { ALOGE("Set intf high th: %d\n", value); ret = perf_params.SetIntfHighTh(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting intf high th\n"); break; } unsigned char th; ret = perf_params.GetIntfHighTh(fd, th); if(ret == FM_SUCCESS) { ALOGE("Read intf high th: %u\n", th); }else { ALOGE("Error in reading intf high th\n"); } } break; case CF0_TH: ALOGE("Set cf0 th: %d\n", value); ret = perf_params.SetCf0Th12(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting cf0 th\n"); break; } int th; ret = perf_params.GetCf0Th12(fd, th); if(ret == FM_SUCCESS) { ALOGE("Read CF012 th: %d\n", th); }else { ALOGE("Error in reading cf0 th\n"); } break; case SRCH_ALGO_TYPE: if((value >= SRCH_ALGO_TYPE_MIN) && (value <= SRCH_ALGO_TYPE_MAX)) { ALOGE("Set search algo type: %d\n", value); ret = perf_params.SetSrchAlgoType(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting search algo type\n"); break; } unsigned char algo; ret = perf_params.GetSrchAlgoType(fd, algo); if(ret == FM_SUCCESS) { ALOGE("Read algo type: %u\n", algo); }else { ALOGE("Error in reading search algo type\n"); } } break; case SINR_SAMPLES: if((value >= SINR_SAMPLES_CNT_MIN) && (value <= SINR_SAMPLES_CNT_MAX)) { ALOGE("Set sinr samples count: %d\n", value); ret = perf_params.SetSinrSamplesCnt(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting sinr samples count\n"); break; } unsigned char cnt; ret = perf_params.GetSinrSamplesCnt(fd, cnt); if(ret == FM_SUCCESS) { ALOGE("Read sinr samples cnt: %u\n", cnt); }else { ALOGE("Error in reading sinr samples count\n"); } } break; case SINR: if((value >= SINR_FINAL_STAGE_MIN) && (value <= SINR_FINAL_STAGE_MAX)) { ALOGE("Set final stage sinr: %d\n", value); ret = perf_params.SetSinrFinalStage(fd, value); if(ret == FM_FAILURE) { ALOGE("Error in setting final stage sinr\n"); break; } signed char th; ret = perf_params.GetSinrFinalStage(fd, th); if(ret == FM_SUCCESS) { ALOGE("Read final stage sinr: %d\n", th); }else { ALOGE("Error in reading final stage sinr\n"); } } break; } }else { ALOGE("key_value for key: %s is empty\n", *keys); } free(key_value); } keys++; } }else { ALOGE("No of keys found is zero\n"); } free_strs(keys_cpy); }else { ALOGE("key file is null\n"); } }
void inter(char *ptr, char key[MAX_KEY], entry *entryHead) { int keysLength = 1; char *keys = get_keys(ptr, key, &keysLength); if (keysLength == 1) { printf("invalid input\n"); free(keys); return; } entry *lastEntry = get_entry(entryHead, &keys[MAX_KEY*(keysLength - 1)]); if (lastEntry == NULL) { printf("no such key\n"); free(keys); return; } int *processedValues = get_values_copy(lastEntry->values, lastEntry->length); int valuesLength = lastEntry->length; for (int i = keysLength - 2; i >= 0; i--) { entry *first = get_entry(entryHead, &keys[MAX_KEY*i]); if (first == NULL) { printf("no such key\n"); return; } int firstLength = first->length; int *firstValues = get_values_copy(first->values, firstLength); sort_array(firstValues, firstLength); int firstUniqLength = 0; int *uniqFirst = get_unique_array(firstValues, firstLength, &firstUniqLength); free(firstValues); if (firstUniqLength == 0) { free(processedValues); processedValues = NULL; valuesLength = 0; continue; } else if (valuesLength == 0) { processedValues = NULL; valuesLength = 0; free(uniqFirst); continue; } int * newValues = NULL; int newLength = 0; for (int i = 0; i < firstUniqLength; i++) { int exists = 0; for (int j = 0; j < valuesLength; j++) { if (processedValues[j] == uniqFirst[i]) { exists = 1; } } if (exists == 1) { newLength++; newValues = (int *) realloc(newValues, newLength*sizeof(int)); newValues[newLength - 1] = uniqFirst[i]; } } free(uniqFirst); free(processedValues); processedValues = newValues; valuesLength = newLength; } free(keys); if (valuesLength == 0) { printf("[]\n"); return; } printf("["); for (int i = 0; i < valuesLength - 1; i++) { printf("%d ", processedValues[i]); } printf("%d]\n", processedValues[valuesLength - 1]); free(processedValues); }