void callback(getdns_context *context, getdns_callback_type_t callback_type, getdns_dict *response, void *userarg, getdns_transaction_t trans_id) { char *response_str; if (callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */; if (!quiet && (response_str = json ? getdns_print_json_dict(response, json == 1) : getdns_pretty_print_dict(response))) { fprintf(stdout, "ASYNC response:\n%s\n", response_str); validate_chain(response); free(response_str); } fprintf(stdout, "Result: The callback with ID %llu was successfull.\n", (unsigned long long)trans_id); } else if (callback_type == GETDNS_CALLBACK_CANCEL) fprintf(stderr, "Result: The callback with ID %llu was cancelled. Exiting.\n", (unsigned long long)trans_id); else { fprintf(stderr, "Result: The callback got a callback_type of %d. Exiting.\n", callback_type); fprintf(stderr, "Error : '%s'\n", getdns_get_errorstr_by_id(callback_type)); } getdns_dict_destroy(response); response = NULL; }
extern lzma_ret lzma_raw_coder_init(lzma_next_coder *next, lzma_allocator *allocator, const lzma_filter *options, lzma_filter_find coder_find, bool is_encoder) { // Do some basic validation and get the number of filters. size_t count; return_if_error(validate_chain(options, &count)); // Set the filter functions and copy the options pointer. lzma_filter_info filters[LZMA_FILTERS_MAX + 1]; if (is_encoder) { for (size_t i = 0; i < count; ++i) { // The order of the filters is reversed in the // encoder. It allows more efficient handling // of the uncompressed data. const size_t j = count - i - 1; const lzma_filter_coder *const fc = coder_find(options[i].id); if (fc == NULL || fc->init == NULL) return LZMA_OPTIONS_ERROR; filters[j].id = options[i].id; filters[j].init = fc->init; filters[j].options = options[i].options; } } else { for (size_t i = 0; i < count; ++i) { const lzma_filter_coder *const fc = coder_find(options[i].id); if (fc == NULL || fc->init == NULL) return LZMA_OPTIONS_ERROR; filters[i].id = options[i].id; filters[i].init = fc->init; filters[i].options = options[i].options; } } // Terminate the array. filters[count].id = LZMA_VLI_UNKNOWN; filters[count].init = NULL; // Initialize the filters. const lzma_ret ret = lzma_next_filter_init(next, allocator, filters); if (ret != LZMA_OK) lzma_next_end(next, allocator); return ret; }
extern uint64_t lzma_raw_coder_memusage(lzma_filter_find coder_find, const lzma_filter *filters) { // The chain has to have at least one filter. { size_t tmp; if (validate_chain(filters, &tmp) != LZMA_OK) return UINT64_MAX; } uint64_t total = 0; size_t i = 0; do { const lzma_filter_coder *const fc = coder_find(filters[i].id); if (fc == NULL) return UINT64_MAX; // Unsupported Filter ID if (fc->memusage == NULL) { // This filter doesn't have a function to calculate // the memory usage and validate the options. Such // filters need only little memory, so we use 1 KiB // as a good estimate. They also accept all possible // options, so there's no need to worry about lack // of validation. total += 1024; } else { // Call the filter-specific memory usage calculation // function. const uint64_t usage = fc->memusage(filters[i].options); if (usage == UINT64_MAX) return UINT64_MAX; // Invalid options total += usage; } } while (filters[++i].id != LZMA_VLI_UNKNOWN); // Add some fixed amount of extra. It's to compensate memory usage // of Stream, Block etc. coders, malloc() overhead, stack etc. return total + LZMA_MEMUSAGE_BASE; }
int main(int argc, char **argv) { getdns_dict *response = NULL; char *response_str; getdns_return_t r; getdns_dict *address = NULL; FILE *fp = NULL; name = the_root; if ((r = getdns_context_create(&context, 1))) { fprintf(stderr, "Create context failed: %d\n", r); return r; } extensions = getdns_dict_create(); if (! extensions) { fprintf(stderr, "Could not create extensions dict\n"); r = GETDNS_RETURN_MEMORY_ERROR; goto done_destroy_context; } if ((r = parse_args(argc, argv))) goto done_destroy_context; if (query_file) { fp = fopen(query_file, "rt"); if (fp == NULL) { fprintf(stderr, "Could not open query file: %s\n", query_file); goto done_destroy_context; } } /* Make the call */ do { char line[1024], *token, *linev[256]; int linec; if (interactive) { if (!query_file) { fprintf(stdout, "> "); if (!fgets(line, 1024, stdin) || !*line) break; } else { if (!fgets(line, 1024, fp) || !*line) { fprintf(stdout,"End of file."); break; } fprintf(stdout,"Found query: %s", line); } linev[0] = argv[0]; linec = 1; if ( ! (token = strtok(line, " \t\f\n\r"))) continue; if (*token == '#') { fprintf(stdout,"Result: Skipping comment\n"); continue; } do linev[linec++] = token; while (linec < 256 && (token = strtok(NULL, " \t\f\n\r"))); if ((r = parse_args(linec, linev))) { if (r == CONTINUE || r == CONTINUE_ERROR) continue; else goto done_destroy_context; } } if (calltype == HOSTNAME && !(address = ipaddr_dict(context, name))) { fprintf(stderr, "Could not convert \"%s\" " "to an IP address", name); continue; } if (async) { switch (calltype) { case GENERAL: r = getdns_general(context, name, request_type, extensions, &response, NULL, callback); break; case ADDRESS: r = getdns_address(context, name, extensions, &response, NULL, callback); break; case HOSTNAME: r = getdns_hostname(context, address, extensions, &response, NULL, callback); break; case SERVICE: r = getdns_service(context, name, extensions, &response, NULL, callback); break; default: r = GETDNS_RETURN_GENERIC_ERROR; break; } if (r) goto done_destroy_extensions; if (!batch_mode) getdns_context_run(context); } else { switch (calltype) { case GENERAL: r = getdns_general_sync(context, name, request_type, extensions, &response); break; case ADDRESS: r = getdns_address_sync(context, name, extensions, &response); break; case HOSTNAME: r = getdns_hostname_sync(context, address, extensions, &response); break; case SERVICE: r = getdns_service_sync(context, name, extensions, &response); break; default: r = GETDNS_RETURN_GENERIC_ERROR; break; } if (response && !quiet) { if ((response_str = json ? getdns_print_json_dict(response, json == 1) : getdns_pretty_print_dict(response))) { fprintf( stdout, "SYNC response:\n%s\n" , response_str); validate_chain(response); free(response_str); } else { r = GETDNS_RETURN_MEMORY_ERROR; fprintf( stderr , "Could not print response\n"); } } if (r == GETDNS_RETURN_GOOD) { uint32_t status; getdns_dict_get_int(response, "status", &status); fprintf(stdout, "Response code was: GOOD. Status was: %s\n", getdns_get_errorstr_by_id(status)); } else fprintf(stderr, "An error occurred: %d '%s'\n", r, getdns_get_errorstr_by_id(r)); } } while (interactive); if (batch_mode) getdns_context_run(context); /* Clean up */ done_destroy_extensions: getdns_dict_destroy(extensions); done_destroy_context: if (response) getdns_dict_destroy(response); getdns_context_destroy(context); if (fp) fclose(fp); if (r == CONTINUE) return 0; else if (r == CONTINUE_ERROR) return 1; fprintf(stdout, "\nAll done.\n"); return r; }