static void do_glyphs(deark *c, lctx *d) { struct de_bitmap_font *font = NULL; de_byte *font_data = NULL; de_int64 i; de_int64 glyph_rowspan; font = de_create_bitmap_font(c); font->has_nonunicode_codepoints = 1; font->nominal_width = (int)d->glyph_width; font->nominal_height = (int)d->glyph_height; font->num_chars = d->num_glyphs; // This may increase later glyph_rowspan = (d->glyph_width+7)/8; d->num_chars_alloc = d->num_glyphs; if(d->read_extra_codepoints) d->num_chars_alloc += MAX_EXTRA_CODEPOINTS; d->index_of_first_extra_codepoint = d->num_glyphs; d->num_extra_codepoints = 0; font->char_array = de_malloc(c, d->num_chars_alloc * sizeof(struct de_bitmap_font_char)); font_data = de_malloc(c, d->font_data_size); de_read(font_data, d->headersize, d->font_data_size); for(i=0; i<d->num_chars_alloc; i++) { font->char_array[i].width = font->nominal_width; font->char_array[i].height = font->nominal_height; font->char_array[i].rowspan = glyph_rowspan; if(i<d->num_glyphs) font->char_array[i].codepoint_nonunicode = (de_int32)i; else font->char_array[i].codepoint_nonunicode = DE_INVALID_CODEPOINT; font->char_array[i].codepoint_unicode = DE_INVALID_CODEPOINT; if(i<d->num_glyphs) font->char_array[i].bitmap = &font_data[i*d->bytes_per_glyph]; } if(d->has_unicode_table) { if(d->version==2) do_psf2_unicode_table(c, d, font); else do_psf1_unicode_table(c, d, font); } if(d->num_extra_codepoints>0) { font->num_chars = d->index_of_first_extra_codepoint + d->num_extra_codepoints; de_dbg(c, "codepoints aliases: %d\n", (int)d->num_extra_codepoints); de_dbg(c, "total characters: %d\n", (int)font->num_chars); } de_font_bitmap_font_to_image(c, font, NULL, 0); if(font) { de_free(c, font->char_array); de_destroy_bitmap_font(c, font); } de_free(c, font_data); }
static void de_run_pnm(deark *c, de_module_params *mparams) { lctx *d = NULL; de_int64 pos; int ret; int pagenum = 0; d = de_malloc(c, sizeof(lctx)); pos = 0; while(1) { if(c->infile->len - pos < 8) break; d->last_fmt = 0; d->last_bytesused = 0; ret = do_page(c, d, pagenum, pos); if(!ret) break; if(d->last_bytesused<8) break; if(!fmt_is_binary(d->last_fmt)) { break; // ASCII formats don't support multiple images } pos += d->last_bytesused; pagenum++; } de_free(c, d); }
static struct de_linereader *de_linereader_create(deark *c, dbuf *f) { struct de_linereader *lr; lr = de_malloc(c, sizeof(struct de_linereader)); lr->f = f; return lr; }
void de_print_module_list(deark *c) { int i, k; struct sort_data_struct *sort_data = NULL; de_register_modules(c); // An index to the modules. Will be sorted by name. sort_data = de_malloc(c, c->num_modules * sizeof(struct sort_data_struct)); for(k=0; k<c->num_modules; k++) { sort_data[k].c = c; sort_data[k].module_index = k; } qsort((void*)sort_data, (size_t)c->num_modules, sizeof(struct sort_data_struct), module_compare_fn); for(k=0; k<c->num_modules; k++) { i = sort_data[k].module_index; if(c->module_info[i].id && !(c->module_info[i].flags&DE_MODFLAG_HIDDEN) && !(c->module_info[i].flags&DE_MODFLAG_NONWORKING) ) { if(c->module_info[i].desc) de_printf(c, DE_MSGTYPE_MESSAGE, "%-14s %s\n", c->module_info[i].id, c->module_info[i].desc); else de_printf(c, DE_MSGTYPE_MESSAGE, "%s\n", c->module_info[i].id); } } de_free(c, sort_data); }
struct de_bitmap_font *de_create_bitmap_font(deark *c) { struct de_bitmap_font *font; font = de_malloc(c, sizeof(struct de_bitmap_font)); font->index_of_replacement_char = -1; return font; }
static void de_run_gemmeta(deark *c, de_module_params *mparams) { lctx *d = NULL; de_int64 pos; de_int64 hdrlen_words; de_int64 version; de_int64 imgflag; de_int64 bytesused; d = de_malloc(c, sizeof(lctx)); de_msg(c, "Note: GEM VDI Metafiles can be parsed, but no files can be extracted from them.\n"); pos = 0; hdrlen_words = de_getui16le(pos+2); de_dbg(c, "header length: %d words\n", (int)hdrlen_words); version = de_getui16le(pos+4); de_dbg(c, "version number: %d\n", (int)version); // TODO: Read more header fields. imgflag = de_getui16le(pos+28); de_dbg(c, "image flag: %d\n", (int)imgflag); pos += hdrlen_words*2; while(1) { if(pos >= c->infile->len) break; if(!do_record(c, d, pos, &bytesused)) break; if(bytesused<=0) break; pos += bytesused; } de_free(c, d); }
static void de_run_wri(deark *c, de_module_params *mparams) { lctx *d = NULL; i64 pos; d = de_malloc(c, sizeof(lctx)); if(c->input_encoding==DE_ENCODING_UNKNOWN) d->input_encoding = DE_ENCODING_WINDOWS1252; else d->input_encoding = c->input_encoding; d->extract_text = de_get_ext_option_bool(c, "wri:extracttext", 1); d->extract_ole = de_get_ext_option_bool(c, "wri:extractole", (c->extract_level>=2)?1:0); pos = 0; if(!do_header(c, d, pos)) goto done; if(d->extract_text) { do_html_begin(c, d); } do_para_info(c, d); done: do_html_end(c, d); de_free(c, d); }
// de_ucstring is a Unicode (utf-32) string object. de_ucstring *ucstring_create(deark *c) { de_ucstring *s; s = de_malloc(c, sizeof(de_ucstring)); s->c = c; return s; }
scoredMafLine_t* newScoredMafLine(void) { scoredMafLine_t *m = (scoredMafLine_t *) de_malloc(sizeof(*m)); m->mafLine = NULL; m->next = NULL; m->score = 0.0; return m; }
static void de_run_gzip(deark *c, de_module_params *mparams) { lctx *d = NULL; i64 pos; i64 member_size; d = de_malloc(c, sizeof(lctx)); d->crco = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE); pos = 0; while(1) { if(pos >= c->infile->len) break; if(!do_gzip_read_member(c, d, pos, &member_size)) { break; } if(member_size<=0) break; pos += member_size; } dbuf_close(d->output_file); if(d) { de_crcobj_destroy(d->crco); de_free(c, d); } }
static void de_run_t64(deark *c, de_module_params *mparams) { lctx *d = NULL; de_int64 pos; de_int64 i; d = de_malloc(c, sizeof(lctx)); pos = 32; d->version = de_getui16le(pos); de_dbg(c, "version: 0x%04x\n", (int)d->version); if(d->version!=0x100 && d->version!=0x101) { de_warn(c, "Unexpected version number. This might not be a T64 file.\n"); } d->max_dir_entries = de_getui16le(pos+2); d->used_dir_entries = de_getui16le(pos+4); de_dbg(c, "max dir entries = %d, files = %d\n", (int)d->max_dir_entries, (int)d->used_dir_entries); pos += 32; for(i=0; i<d->max_dir_entries; i++) { do_dir_entry(c, d, i, pos+32*i); } de_free(c, d); }
void findBestDupes(duplicate_t *head, char *consensus) { // For each duplicate list, go through its mafline list and find the best line and move it // to the head of the list. duplicate_t *d = head; scoredMafLine_t *sml = NULL; while (d != NULL) { if (d->numSequences < 2) { // if there's only one sequence, who cares d = d->next; continue; } // score all the maf lines sml = d->headScoredMaf; while (sml != NULL) { sml->score = scoreSequence(consensus, maf_mafLine_getSequence(sml->mafLine)); sml = sml->next; } // sort on scores scoredMafLine_t **mafLineArray = (scoredMafLine_t**) de_malloc(sizeof(*mafLineArray) * d->numSequences); populateMafLineArray(d->headScoredMaf, mafLineArray); qsort(mafLineArray, d->numSequences, sizeof(scoredMafLine_t *), cmp_by_score); // move the top score to the head of the list d->headScoredMaf = mafLineArray[0]; sml = d->headScoredMaf; for (unsigned i = 1; i < d->numSequences; ++i) { // rebuild the linked list sml->next = mafLineArray[i]; sml = sml->next; } sml->next = NULL; d = d->next; free(mafLineArray); } }
static void de_run_cardfile(deark *c, de_module_params *mparams) { lctx *d = NULL; de_byte b; de_int64 pos; de_int64 n; d = de_malloc(c, sizeof(lctx)); pos = 0; b = de_getbyte(pos); if(b=='R') d->fmt=DE_CRDFMT_RRG; else d->fmt=DE_CRDFMT_MGC; if(d->fmt==DE_CRDFMT_RRG) { de_err(c, "CardFile RRG format is not supported\n"); goto done; } pos+=3; d->numcards = de_getui16le(pos); de_dbg(c, "number of cards: %d\n", (int)d->numcards); pos+=2; for(n=0; n<d->numcards; n++) { do_card_index(c, d, n, pos); pos+=52; } done: de_free(c, d); }
void parseOptions(int argc, char **argv, char *filename) { int c; int setMName = 0; while (1) { static struct option longOptions[] = { {"debug", no_argument, 0, 'd'}, {"verbose", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 0}, {"maf", required_argument, 0, 'm'}, {0, 0, 0, 0} }; int longIndex = 0; c = getopt_long(argc, argv, "d:m:h:v", longOptions, &longIndex); if (c == -1) break; switch (c) { case 0: if (strcmp("version", longOptions[longIndex].name) == 0) { version(); exit(EXIT_SUCCESS); } break; case 'm': setMName = 1; sscanf(optarg, "%s", filename); break; case 'v': g_verbose_flag++; break; case 'd': g_debug_flag = 1; break; case 'h': case '?': usage(); break; default: abort(); } } if (!setMName) { fprintf(stderr, "specify --maf\n"); usage(); } // Check there's nothing left over on the command line if (optind < argc) { char *errorString = de_malloc(kMaxStringLength); strcpy(errorString, "Unexpected arguments:"); while (optind < argc) { strcat(errorString, " "); strcat(errorString, argv[optind++]); } fprintf(stderr, "%s\n", errorString); free(errorString); usage(); } }
mafFileApi_t* maf_newMfa(const char *filename, char const *mode) { mafFileApi_t *mfa = (mafFileApi_t *) de_malloc(sizeof(*mfa)); mfa->lineNumber = 0; mfa->lastLine = NULL; mfa->mfp = de_fopen(filename, mode); mfa->filename = de_strdup(filename); return mfa; }
duplicate_t* newDuplicate(void) { duplicate_t *d = (duplicate_t *) de_malloc(sizeof(*d)); d->species = NULL; d->headScoredMaf = NULL; d->reported = false; d->next = NULL; return d; }
static void handler_attachedfile_start(deark *c, lctx *d, struct handler_params *hp) { if(d->attachmentctx) { destroy_attachment_data(c, d); } d->attachmentctx = de_malloc(c, sizeof(struct attachmentctx_struct)); }
static void de_run_psf(deark *c, de_module_params *mparams) { lctx *d = NULL; de_byte b; const char *s; d = de_malloc(c, sizeof(lctx)); s = de_get_ext_option(c, "font:noaliases"); if(s) d->read_extra_codepoints = 0; else d->read_extra_codepoints = 1; b = de_getbyte(0); if(b==0x36) { d->version=1; } else if(b==0x72) { d->version=2; } else { de_err(c, "Not a PSF file\n"); goto done; } de_dbg(c, "PSF version: %d\n", (int)d->version); if(d->version==2) do_psf2_header(c, d); else do_psf1_header(c, d); d->font_data_size = d->bytes_per_glyph * d->num_glyphs; if(d->has_unicode_table) { d->unicode_table_pos = d->headersize + d->font_data_size; if(d->unicode_table_pos >= c->infile->len) { d->has_unicode_table = 0; } } if((d->headersize+d->font_data_size > c->infile->len) || d->bytes_per_glyph<1 || d->glyph_width<1 || d->glyph_width>256 || d->glyph_height<1 || d->glyph_height>256 || d->num_glyphs<1 || d->num_glyphs>2000000) { de_err(c, "Invalid or unsupported PSF file\n"); goto done; } do_glyphs(c, d); done: de_free(c, d); }
mafBlock_t* maf_newMafBlockFromString(const char *s, uint64_t lineNumber) { if (s[0] != 'a') { char *error = de_malloc(kMaxStringLength); sprintf(error, "Unable to create maf block from input, " "first line does not start with 'a': %s", s); maf_failBadFormat(lineNumber, error); } mafBlock_t* mb = maf_newMafBlock(); mafLine_t* ml = NULL; maf_mafBlock_setLineNumber(mb, lineNumber); char *cline = (char *) de_malloc(strlen(s) + 1); strcpy(cline, s); char *cline_orig = cline; char **ptr = &cline; char *tkn = NULL; tkn = de_strtok(ptr, '\n'); maf_mafBlock_incrementLineNumber(mb); maf_mafBlock_incrementNumberOfLines(mb); ml = maf_newMafLineFromString(tkn, lineNumber++); maf_mafBlock_setHeadLine(mb, ml); free(tkn); tkn = NULL; tkn = de_strtok(ptr, '\n'); while (tkn != NULL) { maf_mafBlock_incrementLineNumber(mb); maf_mafBlock_incrementNumberOfLines(mb); maf_mafLine_setNext(ml, maf_newMafLineFromString(tkn, lineNumber++)); ml = maf_mafLine_getNext(ml); if (maf_mafLine_getType(ml) == 's') { maf_mafBlock_incrementNumberOfSequences(mb); mb->sequenceFieldLength = maf_mafLine_getSequenceFieldLength(ml); } maf_mafBlock_setTailLine(mb, ml); free(tkn); tkn = NULL; tkn = de_strtok(ptr, '\n'); } free(cline_orig); cline_orig = NULL; return mb; }
static de_uint32 x_dbuf_crc32(dbuf *f, de_int64 pos, de_int64 len) { de_uint32 crc; de_byte *buf; buf = de_malloc(f->c, len); dbuf_read(f, buf, pos, len); crc = de_crc32(buf, len); de_free(f->c, buf); return crc; }
void maf_mafBlock_appendToAlignmentBlock(mafBlock_t *m, char *s) { mafLine_t *ml = maf_mafBlock_getHeadLine(m); char *line = maf_mafLine_getLine(ml); assert(line[0] == 'a'); char *newline = (char*) de_malloc(strlen(line) + strlen(s) + 1); newline[0] = '\0'; strcat(newline, line); strcat(newline, s); free(ml->line); ml->line = newline; }
mafBlock_t* maf_newMafBlock(void) { mafBlock_t *mb = (mafBlock_t *) de_malloc(sizeof(*mb)); mb->next = NULL; mb->headLine = NULL; mb->tailLine = NULL; mb->lineNumber = 0; mb->numberOfSequences = 0; mb->numberOfLines = 0; mb->sequenceFieldLength = 0; return mb; }
// Put the actual codepont to use in the font->char_array[].codepoint_tmp field. static void fixup_codepoints(deark *c, struct font_render_ctx *fctx) { i64 i; i32 c1; i64 num_uncoded_chars = 0; u8 *used_codepoint_map = NULL; u8 codepoint_already_used; if(!fctx->render_as_unicode) { for(i=0; i<fctx->font->num_chars; i++) { fctx->codepoint_tmp[i] = fctx->font->char_array[i].codepoint_nonunicode; } goto done; } // An array of bits to remember if we've seen a codepoint before (BMP only). // A character with a duplicate codepoint will be moved to another // location, so that it doesn't get painted over the previous one. used_codepoint_map = de_malloc(c, 65536/8); for(i=0; i<fctx->font->num_chars; i++) { if(!is_valid_char(&fctx->font->char_array[i])) continue; c1 = fctx->font->char_array[i].codepoint_unicode; codepoint_already_used = 0; if(c1>=0 && c1<65536) { // Check if we've seen this codepoint before. codepoint_already_used = used_codepoint_map[c1/8] & (1<<(c1%8)); // Remember that we've seen this codepoint. used_codepoint_map[c1/8] |= 1<<(c1%8); } if(codepoint_already_used || c1==DE_CODEPOINT_INVALID) { if(codepoint_already_used) { de_dbg2(c, "moving duplicate codepoint U+%04x at index %d to private use area", (unsigned int)c1, (int)i); } // Move uncoded characters to a Private Use area. // (Supplementary Private Use Area-A = U+F0000 - U+FFFFD) if(DE_CODEPOINT_MOVED + num_uncoded_chars <= DE_CODEPOINT_MOVED_MAX) { fctx->codepoint_tmp[i] = (i32)(DE_CODEPOINT_MOVED + num_uncoded_chars); num_uncoded_chars++; } } else { fctx->codepoint_tmp[i] = c1; } } done: de_free(c, used_codepoint_map); }
static void de_run_woz(deark *c, de_module_params *mparams) { lctx *d = NULL; struct de_iffctx *ictx = NULL; u32 crc; i64 pos = 0; // WOZ has a 12-byte header, then sequence of chunks that are basically the // same format as RIFF. d = de_malloc(c, sizeof(lctx)); ictx = de_malloc(c, sizeof(struct de_iffctx)); ictx->userdata = (void*)d; ictx->preprocess_chunk_fn = my_preprocess_woz_chunk_fn; ictx->handle_chunk_fn = my_woz_chunk_handler; ictx->f = c->infile; ictx->is_le = 1; ictx->reversed_4cc = 0; if(ictx->f->len<12) goto done; de_dbg(c, "header at %d", (int)pos); de_dbg_indent(c, 1); pos += 3; // "WOZ" part of signature d->wozver = dbuf_getbyte_p(ictx->f, &pos); de_dbg(c, "format version: '%c'", de_byte_to_printable_char(d->wozver)); if(d->wozver<'1' || d->wozver>'2') { de_err(c, "Unsupported WOZ format version"); goto done; } pos += 4; // rest of signature crc = (u32)dbuf_getu32le_p(ictx->f, &pos); de_dbg(c, "crc: 0x%08x", (unsigned int)crc); de_dbg_indent(c, -1); de_fmtutil_read_iff_format(c, ictx, pos, ictx->f->len-pos); done: de_free(c, ictx); de_free(c, d); }
mafBlock_t* maf_newMafBlockListFromString(const char *s, uint64_t lineNumber) { // given a string, walk through and create a mafBlock_t linked list // for all maf blocks in the string mafBlock_t *head = NULL, *mb = NULL, *tmp = NULL; char *block_s = NULL; size_t len, start = 0, stop; len = strlen(s); for (stop = start + 1; stop < len; ++stop) { if (s[stop] == 'a' && s[stop - 1] == '\n') { block_s = (char *) de_malloc(stop - start + 2); strncpy(block_s, s + start, stop - start + 1); block_s[stop - start + 1] = '\0'; tmp = maf_newMafBlockFromString(block_s, lineNumber); if (head == NULL) { mb = tmp; head = mb; } else { maf_mafBlock_setNext(mb, tmp); mb = maf_mafBlock_getNext(mb); } free(block_s); block_s = NULL; start = stop; } } block_s = (char *) de_malloc(stop - start + 2); strncpy(block_s, s + start, stop - start + 1); block_s[stop - start + 1] = '\0'; tmp = maf_newMafBlockFromString(block_s, lineNumber); if (head == NULL) { mb = tmp; head = mb; } else { maf_mafBlock_setNext(mb, tmp); mb = maf_mafBlock_getNext(mb); } free(block_s); block_s = NULL; return head; }
static void de_run_fnt(deark *c, de_module_params *mparams) { lctx *d = NULL; d = de_malloc(c, sizeof(lctx)); if(!do_read_header(c, d)) goto done; read_face_name(c, d); do_make_image(c, d); done: de_finfo_destroy(c, d->fi); de_free(c, d); }
dbuf *dbuf_create_membuf(deark *c, i64 initialsize, unsigned int flags) { dbuf *f; f = de_malloc(c, sizeof(dbuf)); f->c = c; f->btype = DBUF_TYPE_MEMBUF; f->max_len_hard = DE_MAX_MEMBUF_SIZE; if(initialsize>0) { if(initialsize > f->max_len_hard) { do_on_dbuf_size_exceeded(f); } f->membuf_buf = de_malloc(c, initialsize); f->membuf_alloc = initialsize; } if(flags&0x01) { dbuf_set_length_limit(f, initialsize); } return f; }
mafBlock_t* maf_readBlockBody(mafFileApi_t *mfa) { extern const int kMaxStringLength; mafBlock_t *thisBlock = maf_newMafBlock(); if (mfa->lastLine != NULL) { // this is only invoked when the header is not followed by a blank line mafLine_t *ml = maf_newMafLineFromString(mfa->lastLine, mfa->lineNumber); if (ml->type == 's') { ++(thisBlock->numberOfSequences); if (thisBlock->sequenceFieldLength == 0) { thisBlock->sequenceFieldLength = maf_mafLine_getSequenceFieldLength(ml); } } ++(thisBlock->numberOfLines); thisBlock->headLine = ml; thisBlock->tailLine = ml; free(mfa->lastLine); mfa->lastLine = NULL; } int64_t n = kMaxStringLength; char *line = (char*) de_malloc(n); thisBlock->lineNumber = mfa->lineNumber; while(de_getline(&line, &n, mfa->mfp) != -1) { ++(mfa->lineNumber); if (maf_isBlankLine(line)) { if (thisBlock->headLine == NULL) { // this handles multiple blank lines in a row continue; } else { break; } } mafLine_t *ml = maf_newMafLineFromString(line, mfa->lineNumber); if (thisBlock->headLine == NULL) { thisBlock->headLine = ml; thisBlock->tailLine = ml; } else { thisBlock->tailLine->next = ml; thisBlock->tailLine = ml; } if (ml->type == 's') { ++(thisBlock->numberOfSequences); if (thisBlock->sequenceFieldLength == 0) { thisBlock->sequenceFieldLength = maf_mafLine_getSequenceFieldLength(ml); } } ++(thisBlock->numberOfLines); } free(line); line = NULL; return thisBlock; }
static void de_run_macpaint(deark *c, de_module_params *mparams) { lctx *d; de_int64 pos; const char *s; d = de_malloc(c, sizeof(lctx)); d->has_macbinary_header = -1; s = de_get_ext_option(c, "macpaint:macbinary"); if(s) d->has_macbinary_header = de_atoi(s); if(d->has_macbinary_header == -1) { int v512; int v640; de_dbg(c, "trying to determine if file has a MacBinary header\n"); v512 = valid_file_at(c, d, 0); v640 = valid_file_at(c, d, 128); if(v512 > v640) { de_dbg(c, "assuming it has no MacBinary header\n"); d->has_macbinary_header = 0; } else if(v640 > v512) { de_dbg(c, "assuming it has a MacBinary header\n"); d->has_macbinary_header = 1; } else if(v512 && v640) { de_warn(c, "Can't determine if this file has a MacBinary header. " "Try \"-opt macpaint:macbinary=0\".\n"); d->has_macbinary_header = 1; } else { de_warn(c, "This is probably not a MacPaint file.\n"); d->has_macbinary_header = 1; } } if(d->has_macbinary_header) de_declare_fmt(c, "MacPaint with MacBinary header"); else de_declare_fmt(c, "MacPaint without MacBinary header"); pos = d->has_macbinary_header ? 128 : 0; do_read_bitmap(c, d, pos); do_read_patterns(c, d, pos); de_free(c, d); }
char *copyChromosomeName(const char *s) { // return a copy of the string, minus species information // hg18.chr1 -> chr1 unsigned n, l = 0; n = strlen(s); for (l = 0; l < n; ++l) { // find the first instance of a `.' character if (s[l] == '.') { l += 1; break; } } if (l < n) { char *copy = (char *) de_malloc(n - l + 1); strncpy(copy, s + l, n - l); copy[n - l] = '\0'; return copy; } else { char *copy = (char *) de_malloc(1); copy[0] = '\0'; return copy; } }