static void rval_evaluator_x_srs_free(rval_evaluator_t* pevaluator) { rval_evaluator_x_srs_state_t* pstate = pevaluator->pvstate; pstate->parg1->pfree_func(pstate->parg1); regfree(&pstate->regex); pstate->parg3->pfree_func(pstate->parg3); sb_free(pstate->psb); free(pstate); free(pevaluator); }
static void virgo_deinit(Virgo *v) { unsigned i; for (i=0; i<NUM_DESKTOPS; i++) { windows_show(&v->desktops[i]); sb_free(v->desktops[i].windows); } trayicon_deinit(&v->trayicon); }
static int _delete_bigram_generator(index_word_extractor_t *extractor) { bigram_t *handle = NULL; if (extractor->id != MY_EXTRACTOR_ID) return DECLINE; handle = extractor->handle; delete_bigram_generator(handle); sb_free(extractor); return SUCCESS; }
static int build_test(struct TestFile *tf, struct Config *conf) { struct StringBuffer sb; sb_init(&sb, 128); make_build_command(&sb, tf, conf); int ret = checked_system(sb.s); sb_free(&sb); return ret; }
// Find the file in bin independent on where the // program was called from. void get_path_to_file(char* dest, const char* path_from_bin, const char* called_from) { string_builder sb; sb_init(&sb); sb_append(&sb, called_from); int dash_i = sb_last_index_of(&sb, "/"); sb_cut(&sb, 0, dash_i + 1); char* path_to_dir = sb_give_control_of_str(&sb); strcpy(dest, path_to_dir); dest = strcat(dest, path_from_bin); free(path_to_dir); sb_free(&sb); }
// ---------------------------------------------------------------- static void lrec_reader_mmap_csv_free(lrec_reader_t* preader) { lrec_reader_mmap_csv_state_t* pstate = preader->pvstate; for (lhmslve_t* pe = pstate->pheader_keepers->phead; pe != NULL; pe = pe->pnext) { header_keeper_t* pheader_keeper = pe->pvvalue; header_keeper_free(pheader_keeper); } lhmslv_free(pstate->pheader_keepers); parse_trie_free(pstate->pno_dquote_parse_trie); parse_trie_free(pstate->pdquote_parse_trie); rslls_free(pstate->pfields); sb_free(pstate->psb); free(pstate->ifs_eof); free(pstate->dquote_irs); free(pstate->dquote_ifs); free(pstate); free(preader); }
// XXX: should be extern... bigram_t* new_bigram() { bigram_t *handle=NULL; handle = sb_calloc(1,sizeof(bigram_t)); if (handle == NULL) { crit("cannot allocate bigram handle"); return NULL; } handle->tokenizer = sb_run_new_tokenizer(); if (handle->tokenizer == NULL) { crit("cannot allocate tokenizer"); sb_free(handle); return NULL; } return handle; }
static int read_file_mmap_psb(char* filename, int do_write) { file_reader_mmap_state_t* ph = file_reader_mmap_open(NULL, filename); string_builder_t* psb = sb_alloc(STRING_BUILDER_INIT_SIZE); char* irs = "\n"; int bc = 0; while (TRUE) { char* line = read_line_mmap_psb(ph, psb, irs); if (line == NULL) break; if (do_write) { fputs(line, stdout); fputc('\n', stdout); } bc += strlen(line); } sb_free(psb); file_reader_mmap_close(ph, NULL); return bc; }
char* lrec_sprint(lrec_t* prec, char* ors, char* ofs, char* ops) { string_builder_t* psb = sb_alloc(SB_ALLOC_LENGTH); if (prec == NULL) { sb_append_string(psb, "NULL"); } else { int nf = 0; for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) { if (nf > 0) sb_append_string(psb, ofs); sb_append_string(psb, pe->key); sb_append_string(psb, ops); sb_append_string(psb, pe->value); nf++; } sb_append_string(psb, ors); } char* rv = sb_finish(psb); sb_free(psb); return rv; }
static int read_file_fgetc_psb(char* filename, int do_write) { FILE* fp = fopen_or_die(filename); string_builder_t* psb = sb_alloc(STRING_BUILDER_INIT_SIZE); char* irs = "\n"; int bc = 0; while (TRUE) { char* line = read_line_fgetc_psb(fp, psb, irs); if (line == NULL) break; if (do_write) { fputs(line, stdout); fputc('\n', stdout); } bc += strlen(line); free(line); } sb_free(psb); fclose(fp); return bc; }
int main(int arg, char **argv) { int i; int *arr = NULL; for (i=0; i < 1000000; ++i) sb_push(arr, i); assert(sb_count(arr) == 1000000); for (i=0; i < 1000000; ++i) assert(arr[i] == i); sb_free(arr); arr = NULL; for (i=0; i < 1000; ++i) sb_add(arr, 1000); assert(sb_count(arr) == 1000000); return 0; }
void sb_replace(string_builder *sb, const char* old_val, const char* new_val) { if (old_val == NULL || sb->count == 0) return; string_builder new_sb; sb_init(&new_sb); int old_val_len = strlen(old_val); for (size_t i = 0; i < sb->count; i++) { if (strncmp(sb->_str_p + i, old_val, old_val_len) == 0 && i <= sb->count - old_val_len) { sb_append(&new_sb, new_val); i += old_val_len - 1; } else { sb_append_char(&new_sb, sb->_str_p[i]); } } sb_free(sb); *sb = new_sb; }
int DAPI_put (DocObject *doc) { xmlChar *tmpChar; char *locate, *to_put; int iSize, iResult; DocId docId; VariableBuffer buf; /* xmlValidCtxt cvp;*/ // FIXME /* static int sdocId = 0;*/ /* cvp.userData = (void *)stderr;*/ /* cvp.error = (xmlValidityErrorFunc) fprintf;*/ /* cvp.warning = (xmlValidityWarningFunc) fprintf;*/ /* if (!xmlValidateDtd (&cvp, doc->doc, cannedDocDtd)) {*/ /* if (debug) {*/ /* fprintf (stderr, "%s : %d : (DAPI_put) invalid canned document\n", */ /* __FILE__, __LINE__);*/ /* fflush (stderr);*/ /* }*/ /* return FALSE;*/ /* }*/ // FIXME -- get document id /* sdocId++;*/ /* docId = sdocId;*/ iResult = sb_run_client_get_new_docid(doc->key, &docId); if (iResult < 0) { error("cannot get new document id of key: \"%s\"", doc->key); return FALSE; } xmlDocDumpMemory(doc->doc, &tmpChar, &iSize); // charset transform if ((to_put = UTF8_TO_EUCKR ((char *)tmpChar)) == NULL) { free (tmpChar); return FALSE; } free (tmpChar); // remove <?xml ... ?>\n locate = to_put; if (strstr(to_put, "<?xml")) { locate = strchr (to_put, '>'); locate += 2; iSize -= (int)(locate - to_put); } sb_run_buffer_initbuf(&buf); iResult = sb_run_buffer_append(&buf, iSize, (char *)locate); sb_free(to_put); if (iResult < 0) { error ("out of memory"); return FALSE; } iResult = sb_run_server_canneddoc_put(docId, &buf); /* iResult = sb_run_client_canneddoc_put(docId, &buf);*/ sb_run_buffer_freebuf (&buf); if (iResult < 0) { error ("cannot regist document : %ld", docId); return FALSE; } return TRUE; }
static int document_ma(request_rec *r, softbot_handler_rec *s) { int rv = 0; char* metadata = NULL; char* document = NULL; char* p = NULL; void *parser = NULL; char* field_value; int field_length; sb4_merge_buffer_t merge_buffer; memfile* request_body = NULL; char field_name[MAX_FIELD_NAME_LEN]; int ma_id = 0; int field_id = 0; int data_size =0; uint32_t buffer_size=0; char *buffer = NULL; int is_binary = 0; int is_plantext = 0; int is_raw_koma_text = 0; char* contenttype = NULL; char* rawkomatext = NULL; char content_type[SHORT_STRING_SIZE+1]; is_binary = equals_content_type(r, "x-softbotd/binary"); contenttype = apr_pstrdup(r->pool, apr_table_get(s->parameters_in, "contenttype")); if(contenttype == NULL) { is_plantext = 0; // xml } else if(strncasecmp("xml", contenttype, 3) == 0) { is_plantext = 0; // xml } else if(strncasecmp("text", contenttype, 4) == 0) { is_plantext = 1; // text } rawkomatext = apr_pstrdup(r->pool, apr_table_get(s->parameters_in, "rawkomatext")); if(rawkomatext == NULL) { is_raw_koma_text = 0; } else { is_raw_koma_text = 1; } metadata = apr_pstrdup(r->pool, apr_table_get(s->parameters_in, "metadata")); if( metadata == NULL || strlen(metadata) == 0 ) { MSG_RECORD(&s->msg, error, "metadata is null, get parameter exist with metadata."); return FAIL; } if (canned_doc == NULL) { canned_doc = (char *)sb_malloc(DOCUMENT_SIZE); if (canned_doc == NULL) { MSG_RECORD(&s->msg, crit, "out of memory: %s", strerror(errno)); return FAIL; } } if(metadata != NULL) decodencpy(metadata, metadata, strlen(metadata)); rv = sb_run_sbhandler_make_memfile(r, &request_body); if(rv != SUCCESS) { MSG_RECORD(&s->msg, error, "can not post data"); if(request_body != NULL) memfile_free(request_body); return FAIL; } if(memfile_getSize(request_body) >= DOCUMENT_SIZE) { MSG_RECORD(&s->msg, error, "can not insert document, max size[%d], current size[%ld]", DOCUMENT_SIZE, memfile_getSize(request_body)); if(request_body != NULL) memfile_free(request_body); return FAIL; } rv = memfile_read(request_body, canned_doc, memfile_getSize(request_body)); if(rv != memfile_getSize(request_body)) { MSG_RECORD(&s->msg, error, "can not read memfile"); if(request_body != NULL) memfile_free(request_body); return FAIL; } canned_doc[memfile_getSize(request_body)] = '\0'; decodencpy(canned_doc, canned_doc, strlen(canned_doc)); document = strchr(canned_doc, '='); if(document == NULL || document - canned_doc > 20) { MSG_RECORD(&s->msg, error, "can not find body"); if(request_body != NULL) memfile_free(request_body); return FAIL; } document++; parser = sb_run_xmlparser_parselen("CP949" , (char *)document, strlen(document)); if (parser == NULL) { MSG_RECORD(&s->msg, error, "cannot parse document"); if(request_body != NULL) memfile_free(request_body); return FAIL; } p = metadata; merge_buffer.data = NULL; merge_buffer.data_size = 0; merge_buffer.allocated_size = 0; do { char path[STRING_SIZE]; char *field_id_ptr=0x00; char fid[3]; int field_id_is_given = 0; void* tmp_data = NULL; p = get_field_and_ma_id_from_meta_data(p , field_name, &ma_id); field_id_ptr = strchr(field_name, '#'); if (field_id_ptr != NULL) field_id_is_given = 1; if (field_id_is_given == 1) { strcpy(fid, field_id_ptr+1); *field_id_ptr = '\0'; field_id = atol(fid); } info("fieldname[%s] id[%d]", field_name, field_id); sprintf(path, "/Document/%s", field_name); path[STRING_SIZE-1] = '\0'; rv = sb_run_xmlparser_retrieve_field(parser, path, &field_value, &field_length); if (field_id_is_given) { if (rv != SUCCESS) { warn("cannot retrieve field[%s]", path); continue; } if (field_length == 0) { continue; } } else { if (rv != SUCCESS) { warn("cannot retrieve field[%s]", path); field_id++; continue; } if (field_length == 0) { field_id++; continue; } } if ( field_length+1 > buffer_size ) { buffer = apr_palloc(r->pool, sizeof(char) * (field_length+1)); buffer_size = field_length + 1; } memcpy(buffer, field_value, field_length); buffer[field_length] = '\0'; tmp_data = NULL; if(is_raw_koma_text) ma_id = 100; rv = sb_run_rmas_morphological_analyzer(field_id, buffer, &tmp_data, &data_size, ma_id); if (rv == FAIL || rv == DECLINE) { MSG_RECORD(&s->msg, warn, "failed to do morphological analysis - rmas_morphological_analyzer()"); sb_run_xmlparser_free_parser(parser); return FAIL; } info("finished ma: data_size: %d", data_size); rv = sb_run_rmas_merge_index_word_array( &merge_buffer , tmp_data , data_size); if (rv == FAIL) { MSG_RECORD(&s->msg, warn, "failed to do morphological analysis - rmas_merge_index_word_array()"); sb_free(tmp_data); sb_run_xmlparser_free_parser(parser); return FAIL; } sb_free(tmp_data); field_id++; } while (*p); sb_run_xmlparser_free_parser(parser); if(is_binary == SUCCESS) { ap_rwrite(merge_buffer.data, merge_buffer.data_size, r); } else { int i; index_word_t *idx = NULL; int cnt = merge_buffer.data_size / sizeof(index_word_t); char tag[5]; if(is_plantext) { ap_rprintf(r, "%d\n", cnt); for(i = 0; i < cnt; i++) { idx = (index_word_t*)merge_buffer.data + i; memcpy(tag, &idx->attribute, 4); tag[4] = '\0'; ap_rprintf(r, "%s %d %s\n", idx->word, idx->pos, tag); } } else { snprintf( content_type, SHORT_STRING_SIZE, "text/xml; charset=%s", default_charset); ap_set_content_type(r, content_type); ap_rprintf(r, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", default_charset); ap_rprintf(r, "<items count=\"%d\">", cnt); for(i = 0; i < cnt; i++) { idx = (index_word_t*)merge_buffer.data + i; memcpy(tag, &idx->attribute, 4); tag[4] = '\0'; ap_rprintf(r, "<word pos = \"%d\" field=\"%d\" tag=\"%s\"><![CDATA[%s]]></word>\n", idx->pos, idx->field, tag, idx->word); } ap_rprintf(r, "</items>"); } } return SUCCESS; }
int DAPI_get (DocId docId, DocObject **pdoc) { DocObject *doc; int iResult, iSize; VariableBuffer buf; char *to_parse; xmlDocPtr xmldoc; int tmpChar[DOCUMENT_SIZE]; bzero(tmpChar, DOCUMENT_SIZE); doc = sb_malloc(sizeof(DocObject)); if (doc == NULL) { error("cannot allocate memory for DocObject: %s", strerror(errno)); return FALSE; } sb_run_buffer_initbuf(&buf); iResult = sb_run_server_canneddoc_get(docId, &buf); /* iResult = sb_run_client_canneddoc_get(docId, &buf);*/ if (iResult < 0) { error("cannot retreive canned document from server"); sb_free(doc); return FALSE; } doc->dtd = cannedDocDtd; iSize = sb_run_buffer_getsize(&buf); iResult = sb_run_buffer_get(&buf, 0, iSize, tmpChar); tmpChar[iSize] = '\0'; sb_run_buffer_freebuf(&buf); // charset transform if ((to_parse = EUCKR_TO_UTF8 ((char *)tmpChar)) == NULL) { sb_free(doc); error("cannot convert Euc-kr to UTF-8"); return FALSE; } /* iSize = strlen(to_parse);*/ /* xmldoc = xmlParseMemory (to_parse, iSize);*/ xmldoc = xmlParseDoc(to_parse); free (to_parse); if (xmldoc == NULL) { error("cannot parse document[%ld]", docId); sb_free(doc); return FALSE; } doc->doc = xmldoc; doc->root = xmlDocGetRootElement (xmldoc); if (doc->root == NULL) { error("cannot get root element of cannot document[%ld]", docId); xmlFreeDoc (doc->doc); sb_free(doc); return FALSE; } *pdoc = doc; return TRUE; }
/* * Returns: * 0 Success * 1 Incorrect usage (i.e., command-line syntax error). * 2 No such parameter or node. Error message written. * 3 System error. Error message written. */ int main( int argc, char* argv[]) { int status; const char* const progname = basename(argv[0]); (void)log_init(progname); if ((status = sb_new(&_valuePath, 80))) { log_error("Couldn't initialize utility"); status = SYSTEM_ERROR; } else { enum { UNKNOWN, CREATE, PRINT, PUT_BOOL, PUT_STRING, PUT_UINT, PUT_SIGNATURE, PUT_TIME, RESET, REMOVE } action = UNKNOWN; const char* string; signaturet signature; timestampt timestamp; unsigned long uint; int boolean; int ch; int quiet = 0; opterr = 0; /* supress getopt(3) error messages */ while (0 == status && (ch = getopt(argc, argv, ":b:cd:h:qRrs:t:u:vx")) != -1) { switch (ch) { case 'b': { if (strcasecmp(optarg, "TRUE") == 0) { boolean = 1; } else if (strcasecmp(optarg, "FALSE") == 0) { boolean = 0; } else { log_add("Not a boolean value: \"%s\"", optarg); status = COMMAND_SYNTAX; } if (status == 0) { if (CREATE == action) { log_error("Create option ignored"); } action = PUT_BOOL; } break; } case 'c': { if (UNKNOWN != action) { log_add("Can't mix create action with other actions"); status = COMMAND_SYNTAX; } else { action = CREATE; } break; } case 'd': { if ((status = reg_setDirectory(optarg))) status = SYSTEM_ERROR; break; } case 'h': { status = sigParse(optarg, &signature); if (0 > status || 0 != optarg[status]) { log_add("Not a signature: \"%s\"", optarg); status = COMMAND_SYNTAX; } else { if (CREATE == action) { log_info("Create action ignored"); } action = PUT_SIGNATURE; status = 0; } break; } case 'q': { quiet = 1; break; } case 'R': { if (UNKNOWN != action) { log_add("Can't mix reset action with other actions"); status = COMMAND_SYNTAX; } else { action = RESET; } break; } case 'r': { if (UNKNOWN != action) { log_add("Can't mix remove action with other actions"); status = COMMAND_SYNTAX; } else { action = REMOVE; } break; } case 's': { if (CREATE == action) { log_info("Create action ignored"); } string = optarg; action = PUT_STRING; break; } case 't': { status = tsParse(optarg, ×tamp); if (0 > status || 0 != optarg[status]) { log_add("Not a timestamp: \"%s\"", optarg); status = COMMAND_SYNTAX; } else { if (CREATE == action) { log_info("Create action ignored"); } action = PUT_TIME; status = 0; } break; } case 'u': { char* end; errno = 0; uint = strtoul(optarg, &end, 0); if (0 != *end || (0 == uint && 0 != errno)) { log_add("Not an unsigned integer: \"%s\"", optarg); status = COMMAND_SYNTAX; } else { if (CREATE == action) { log_info("Create option ignored"); } action = PUT_UINT; } break; } case 'v': { if (!log_is_enabled_info) (void)log_set_level(LOG_LEVEL_INFO); break; } case 'x': { (void)log_set_level(LOG_LEVEL_DEBUG); break; } case ':': { log_add("Option \"-%c\" requires an operand", optopt); status = COMMAND_SYNTAX; break; } default: log_add("Unknown option: \"%c\"", optopt); status = COMMAND_SYNTAX; /* no break */ } } /* options loop */ if (status) { log_flush_error(); if (COMMAND_SYNTAX == status) usage(progname); } else { const int argCount = argc - optind; if (UNKNOWN == action) action = PRINT; switch (action) { case CREATE: { if (0 < argCount) { log_error("Too many arguments"); usage(progname); status = COMMAND_SYNTAX; } else { status = createRegistry(); } break; } case RESET: { if (0 < argCount) { log_error("Too many arguments"); usage(progname); status = COMMAND_SYNTAX; } else { status = resetRegistry(); } break; } case REMOVE: { if (0 == argCount) { log_error( "Removal action requires absolute pathname(s)"); usage(progname); status = COMMAND_SYNTAX; } else { log_debug("Removing registry"); status = actUponPathList(argv + optind, deletePath, quiet); } break; } case PRINT: { log_debug("Printing registry"); status = (0 == argCount) ? printPath("/", quiet) : actUponPathList(argv + optind, printPath, quiet); break; } default: { /* * Must be some kind of "put". */ if (0 == argCount) { log_error("Put action requires value pathname"); usage(progname); status = COMMAND_SYNTAX; } else { switch (action) { case PUT_BOOL: status = reg_putBool(argv[optind], boolean); break; case PUT_UINT: status = reg_putUint(argv[optind], uint); break; case PUT_STRING: status = reg_putString(argv[optind], string); break; case PUT_TIME: status = reg_putTime(argv[optind], ×tamp); break; case PUT_SIGNATURE: status = reg_putSignature(argv[optind], signature); break; default: abort(); } if (status) { log_flush_error(); status = SYSTEM_ERROR; } } } /* put switch */ /* no break */ } /* "action" switch */ } /* decoded options */ sb_free(_valuePath); } /* "_valuePath" allocated */ return status; }
int main(int argc, char **argv) { bool raw_mode = false; const char *loopback = NULL; while(1) { static struct option long_options[] = { {"help", no_argument, 0, '?'}, {"debug", no_argument, 0, 'd'}, {"add-key", required_argument, 0, 'a'}, {"no-color", no_argument, 0, 'n'}, {"loopback", required_argument, 0, 'l'}, {0, 0, 0, 0} }; int c = getopt_long(argc, argv, "?do:k:zra:nl:", long_options, NULL); if(c == -1) break; switch(c) { case -1: break; case 'l': if(loopback) bug("Only one loopback file can be specified !\n"); loopback = optarg; break; case 'n': enable_color(false); break; case 'd': g_debug = true; break; case '?': usage(); break; case 'o': g_out_prefix = optarg; break; case 'k': { add_keys_from_file(optarg); break; } case 'z': { add_keys(&g_zero_key, 1); break; } case 'r': raw_mode = true; break; case 'a': { struct crypto_key_t key; char *s = optarg; if(!parse_key(&s, &key)) bug("Invalid key specified as argument"); if(*s != 0) bug("Trailing characters after key specified as argument"); add_keys(&key, 1); break; } default: abort(); } } if(argc - optind != 1) { usage(); return 1; } const char *sb_filename = argv[optind]; enum sb_error_t err; struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, sb_printf, &err); if(file == NULL) { color(OFF); printf("SB read failed: %d\n", err); return 1; } color(OFF); if(g_out_prefix) extract_sb_file(file); if(g_debug) { color(GREY); printf("[Debug output]\n"); sb_dump(file, NULL, sb_printf); } if(loopback) { /* sb_read_file will fill real key and IV but we don't want to override * them when looping back otherwise the output will be inconsistent and * garbage */ file->override_real_key = false; file->override_crypto_iv = false; sb_write_file(file, loopback); } sb_free(file); clear_keys(); return 0; }
void delete_bigram_generator(bigram_t *handle) { sb_run_delete_tokenizer(handle->tokenizer); sb_free(handle); }
int main(int argc, const char **argv) { if (argc < 3) return syntax_error(argv, 5); int csize = atoi(argv[1]), climit0 = atoi(argv[2]); if (csize < 1 || csize > 4) return syntax_error(argv, 6); if (climit0 < 0) return syntax_error(argv, 7); int exit_code = 0; size_t count = 0; size_t cmax = csize == 4 ? 0xffffffff : 0xffffffff & ((1 << (csize * 8)) - 1); size_t climit = climit0 ? S_MIN((size_t)climit0, cmax) : cmax; #ifdef COUNTER_USE_BITSET #define COUNTER_SET(val) sb_set(&bs, val) #define COUNTER_POPCOUNT sb_popcount(bs) sb_t *bs = sb_alloc(0); sb_eval(&bs, cmax); #else #define COUNTER_SET(val) sm_uu32_insert(&m, val, 1) #define COUNTER_POPCOUNT sm_size(m) sm_t *m = sm_alloc(SM_U32U32, 0); #endif unsigned char buf[3 * 4 * 128]; int i; ssize_t l; for (;;) { l = read(0, buf, sizeof(buf)); l = (l / csize) * csize; if (l <= 0) break; #define CNTLOOP(inc, val) \ for (i = 0; i < l; i += inc) { \ COUNTER_SET(val); \ count++; \ if (COUNTER_POPCOUNT >= climit) \ goto done; \ } switch (csize) { case 1: CNTLOOP(1, buf[i]); break; case 2: CNTLOOP(2, (size_t)(buf[i] << 8 | buf[i + 1])); break; case 3: CNTLOOP(3, (size_t)(buf[i] << 16 | buf[i + 1] << 8 | buf[i + 2])); break; case 4: CNTLOOP(4, (size_t)buf[i] << 24 | (size_t)buf[i + 1] << 16 | (size_t)buf[i + 2] << 8 | (size_t)buf[i + 3]); break; default: goto done; } #undef CNTLOOP } done: printf(FMT_ZU ", " FMT_ZU, count, COUNTER_POPCOUNT); #ifdef COUNTER_USE_BITSET sb_free(&bs); #else sm_free(&m); #endif return exit_code; }
void process_block(const char * block, int block_size) { static enum { HTTP_HEADER, HEADER_NAME, HEADER_VALUE, BODY } state = HTTP_HEADER; static string_buffer buffer; static int sb_initialized = 0; if(!sb_initialized) { sb_initialized = 1; buffer = sb_alloc(); } if(block == NULL) { state = HTTP_HEADER; } int i = -1; switch(state) { case HTTP_HEADER: goto _0; case HEADER_NAME: goto _1; case HEADER_VALUE: goto _2; case BODY: goto _3; } _0: while(++i < block_size) { if(block[i] == '\n') { state = HEADER_NAME; printf("Got HTTP header: \"%s\"\n", sb_tostring(buffer)); sb_free(buffer); buffer = sb_alloc(); goto _1; } if(block[i] != '\r') /* Hm... */ sb_append(&buffer, block[i]); } return; _1: while(++i < block_size) { if(block[i] == '\n') { /* TODO: Check that current header name is empty */ state = BODY; goto _3; } else if(block[i] == ':') { state = HEADER_VALUE; printf("Got header name: \"%s\"\n", sb_tostring(buffer)); sb_free(buffer); buffer = sb_alloc(); goto _2; } if(block[i] != '\r') /* Hm... */ sb_append(&buffer, block[i]); } return; _2: while(++i < block_size) { if(block[i] == '\n') { state = HEADER_NAME; printf("Got header value: \"%s\"\n", sb_tostring(buffer)); sb_free(buffer); buffer = sb_alloc(); goto _1; } if(block[i] != '\r') /* Hm... */ sb_append(&buffer, block[i]); } return; _3: while(++i < block_size) { sb_append(&buffer, block[i]); } printf("Got request body: \"%s\"\n", sb_tostring(buffer)); sb_free(buffer); buffer = sb_alloc(); }
int main(int argc, char **argv) { bool raw_mode = false; const char *loopback = NULL; bool force_sb1 = false; bool force_sb2 = false; bool brute_force = false; while(1) { static struct option long_options[] = { {"help", no_argument, 0, '?'}, {"debug", no_argument, 0, 'd'}, {"add-key", required_argument, 0, 'a'}, {"no-color", no_argument, 0, 'n'}, {"loopback", required_argument, 0, 'l'}, {"force", no_argument, 0, 'f'}, {"v1", no_argument, 0, '1'}, {"v2", no_argument, 0, '2'}, {"no-simpl", no_argument, 0, 's'}, {0, 0, 0, 0} }; int c = getopt_long(argc, argv, "?do:k:zra:nl:f12xsb", long_options, NULL); if(c == -1) break; switch(c) { case -1: break; case 'l': if(loopback) bug("Only one loopback file can be specified !\n"); loopback = optarg; break; case 'n': enable_color(false); break; case 'd': g_debug = true; break; case '?': usage(); break; case 'o': g_out_prefix = optarg; break; case 'f': g_force = true; break; case 'k': { if(!add_keys_from_file(optarg)) bug("Cannot add keys from %s\n", optarg); break; } case 'z': { struct crypto_key_t g_zero_key; sb_get_zero_key(&g_zero_key); add_keys(&g_zero_key, 1); break; } case 'x': { struct crypto_key_t key; sb1_get_default_key(&key); add_keys(&key, 1); break; } case 'r': raw_mode = true; break; case 'a': { struct crypto_key_t key; char *s = optarg; if(!parse_key(&s, &key)) bug("Invalid key specified as argument\n"); if(*s != 0) bug("Trailing characters after key specified as argument\n"); add_keys(&key, 1); break; } case '1': force_sb1 = true; break; case '2': force_sb2 = true; break; case 's': g_elf_simplify = false; break; case 'b': brute_force = true; break; default: bug("Internal error: unknown option '%c'\n", c); } } if(force_sb1 && force_sb2) bug("You cannot force both version 1 and 2\n"); if(argc - optind != 1) { usage(); return 1; } const char *sb_filename = argv[optind]; enum sb_version_guess_t ver = guess_sb_version(sb_filename); if(ver == SB_VERSION_ERR) { printf("Cannot open/read SB file: %m\n"); return 1; } if(force_sb2 || ver == SB_VERSION_2) { enum sb_error_t err; struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, generic_std_printf, &err); if(file == NULL) { color(OFF); printf("SB read failed: %d\n", err); return 1; } color(OFF); if(g_out_prefix) extract_sb_file(file); if(g_debug) { color(GREY); printf("[Debug output]\n"); sb_dump(file, NULL, generic_std_printf); } if(loopback) { /* sb_read_file will fill real key and IV but we don't want to override * them when looping back otherwise the output will be inconsistent and * garbage */ file->override_real_key = false; file->override_crypto_iv = false; sb_write_file(file, loopback, 0, generic_std_printf); } sb_free(file); } else if(force_sb1 || ver == SB_VERSION_1) { if(brute_force) { struct crypto_key_t key; enum sb1_error_t err; if(!sb1_brute_force(sb_filename, NULL, generic_std_printf, &err, &key)) { color(OFF); printf("Brute force failed: %d\n", err); return 1; } color(RED); printf("Key found:"); color(YELLOW); for(int i = 0; i < 32; i++) printf(" %08x", key.u.xor_key[i / 16].k[i % 16]); color(OFF); printf("\n"); color(RED); printf("Key: "); color(YELLOW); for(int i = 0; i < 128; i++) printf("%02x", key.u.xor_key[i / 64].key[i % 64]); color(OFF); printf("\n"); add_keys(&key, 1); } enum sb1_error_t err; struct sb1_file_t *file = sb1_read_file(sb_filename, NULL, generic_std_printf, &err); if(file == NULL) { color(OFF); printf("SB read failed: %d\n", err); return 1; } color(OFF); if(g_out_prefix) extract_sb1_file(file); if(g_debug) { color(GREY); printf("[Debug output]\n"); sb1_dump(file, NULL, generic_std_printf); } if(loopback) sb1_write_file(file, loopback); sb1_free(file); } else { color(OFF); printf("Cannot guess file type, are you sure it's a valid image ?\n"); return 1; } clear_keys(); return 0; }
sb_void_t sb_string_destroy(sb_string_t str) { if (str) { sb_free(sb_string_of(str)); } }
void DestroyRequestParser(RequestParser* p) { if(p->state == REQUEST_STATE_HEADER_VALUE) { sb_free(p->curHeader.value); } }
int DAPI_get (uint32_t docId, DocObject **pdoc) { DocObject *doc; int iResult, iSize, size; VariableBuffer buf; static char *tmpChar = NULL; if (tmpChar == NULL) { tmpChar = (char *)sb_malloc(DOCUMENT_SIZE); if (tmpChar == NULL) { crit("out of memory: %s", strerror(errno)); return FALSE; } } doc = sb_malloc(sizeof(DocObject)); if (doc == NULL) { error("cannot allocate memory for DocObject: %s", strerror(errno)); sb_free(tmpChar); tmpChar = NULL; return FALSE; } /* get document */ sb_run_buffer_initbuf(&buf); iResult = sb_run_server_canneddoc_get(docId, &buf); if (iResult == CDM_DELETED) { info("deleted document[%u]",(uint32_t)docId); sb_free(doc); return DOCAPI_DELETED; } if (iResult < 0) { error("cannot retrieve canned document from server"); sb_free(doc); return FALSE; } iSize = sb_run_buffer_getsize(&buf); iResult = sb_run_buffer_get(&buf, 0, iSize, tmpChar); if (iResult < 0) { error("cannot get document[%u] from variable buffer", docId); sb_free(doc); return FALSE; } tmpChar[iSize] = '\0'; size = sb_run_buffer_getsize(&buf); sb_run_buffer_freebuf(&buf); /* parse */ doc->p = sb_run_xmlparser_parselen("CP949", tmpChar, size); if (doc->p == NULL) { error("cannot parse document[%u]", docId); sb_free(doc); return FALSE; } *pdoc = doc; return TRUE; }
int main(int argc, char **argv) { char *cmd_filename = NULL; char *output_filename = NULL; struct crypto_key_t real_key; struct crypto_key_t crypto_iv; real_key.method = CRYPTO_NONE; crypto_iv.method = CRYPTO_NONE; while(1) { static struct option long_options[] = { {"help", no_argument, 0, '?'}, {"debug", no_argument, 0, 'd'}, {"add-key", required_argument, 0, 'a'}, {"real-key", required_argument, 0, 'r'}, {"crypto-iv", required_argument, 0, 'i'}, {0, 0, 0, 0} }; int c = getopt_long(argc, argv, "?do:c:k:za:", long_options, NULL); if(c == -1) break; switch(c) { case 'd': g_debug = true; break; case '?': usage(); break; case 'o': output_filename = optarg; break; case 'c': cmd_filename = optarg; break; case 'k': { if(!add_keys_from_file(optarg)) bug("Cannot keys from %s\n", optarg); break; } case 'z': { add_keys(&g_zero_key, 1); break; } case 'a': case 'r': case 'i': { struct crypto_key_t key; char *s = optarg; if(!parse_key(&s, &key)) bug("Invalid key/iv specified as argument"); if(*s != 0) bug("Trailing characters after key/iv specified as argument"); if(c == 'r') memcpy(&real_key, &key, sizeof(key)); else if(c == 'i') memcpy(&crypto_iv, &key, sizeof(key)); else add_keys(&key, 1); break; } default: abort(); } } if(!cmd_filename) bug("You must specify a command file\n"); if(!output_filename) bug("You must specify an output file\n"); g_extern = &argv[optind]; g_extern_count = argc - optind; if(g_debug) { printf("key: %d\n", g_nr_keys); for(int i = 0; i < g_nr_keys; i++) { printf(" "); print_key(&g_key_array[i], true); } for(int i = 0; i < g_extern_count; i++) printf("extern(%d)=%s\n", i, g_extern[i]); } struct cmd_file_t *cmd_file = db_parse_file(cmd_filename); struct sb_file_t *sb_file = apply_cmd_file(cmd_file); db_free(cmd_file); if(real_key.method == CRYPTO_KEY) { sb_file->override_real_key = true; memcpy(sb_file->real_key, real_key.u.key, 16); } if(crypto_iv.method == CRYPTO_KEY) { sb_file->override_crypto_iv = true; memcpy(sb_file->crypto_iv, crypto_iv.u.key, 16); } /* fill with default parameters since there is no command file support for them */ sb_file->drive_tag = 0; sb_file->first_boot_sec_id = sb_file->sections[0].identifier; sb_file->flags = 0; sb_file->minor_version = 1; sb_write_file(sb_file, output_filename); sb_free(sb_file); clear_keys(); return 0; }
int main(int argc, char **argv) { char *cmd_filename = NULL; char *output_filename = NULL; struct crypto_key_t real_key; struct crypto_key_t crypto_iv; memset(&real_key, 0, sizeof(real_key)); memset(&crypto_iv, 0, sizeof(crypto_iv)); real_key.method = CRYPTO_NONE; crypto_iv.method = CRYPTO_NONE; if(argc == 1) usage(); while(1) { static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"debug", no_argument, 0, 'd'}, {"add-key", required_argument, 0, 'a'}, {"real-key", required_argument, 0, 'r'}, {"crypto-iv", required_argument, 0, 'i'}, {0, 0, 0, 0} }; int c = getopt_long(argc, argv, "hdo:c:k:za:", long_options, NULL); if(c == -1) break; switch(c) { case 'd': g_debug = true; break; case 'h': usage(); break; case 'o': output_filename = optarg; break; case 'c': cmd_filename = optarg; break; case 'k': { if(!add_keys_from_file(optarg)) bug("Cannot keys from %s\n", optarg); break; } case 'z': { struct crypto_key_t g_zero_key; sb_get_zero_key(&g_zero_key); add_keys(&g_zero_key, 1); break; } case 'a': case 'r': case 'i': { struct crypto_key_t key; char *s = optarg; if(!parse_key(&s, &key)) bug("Invalid key/iv specified as argument"); if(*s != 0) bug("Trailing characters after key/iv specified as argument"); if(c == 'r') memcpy(&real_key, &key, sizeof(key)); else if(c == 'i') memcpy(&crypto_iv, &key, sizeof(key)); else add_keys(&key, 1); break; } default: bug("Internal error: unknown option '%c'\n", c); } } if(!cmd_filename) bug("You must specify a command file\n"); if(!output_filename) bug("You must specify an output file\n"); g_extern = &argv[optind]; g_extern_count = argc - optind; if(g_debug) { printf("key: %d\n", g_nr_keys); for(int i = 0; i < g_nr_keys; i++) { printf(" "); print_key(NULL, misc_std_printf, &g_key_array[i], true); } for(int i = 0; i < g_extern_count; i++) printf("extern(%d)=%s\n", i, g_extern[i]); } struct cmd_file_t *cmd_file = db_parse_file(cmd_filename); if(cmd_file == NULL) bug("Error parsing command file\n"); struct sb_file_t *sb_file = apply_cmd_file(cmd_file); db_free(cmd_file); if(real_key.method == CRYPTO_KEY) { sb_file->override_real_key = true; memcpy(sb_file->real_key, real_key.u.key, 16); } if(crypto_iv.method == CRYPTO_KEY) { sb_file->override_crypto_iv = true; memcpy(sb_file->crypto_iv, crypto_iv.u.key, 16); } sb_write_file(sb_file, output_filename, 0, generic_std_printf); sb_free(sb_file); clear_keys(); return 0; }