//box *get_next_box(unsigned char *mem, long int *pos) { box *get_next_box(FILE *fd) { println_start(INFO); //box *box = malloc(sizeof(box)); int read = 0; box *box = init_box(); if( (box->lbox = read_bytes(box->lbox, fd, 4)) == NULL) return NULL; box->length = hex_to_long(box->lbox, 4); if(box->length == 0) { return NULL; } if( (box->tbox = read_bytes(box->tbox, fd, 4)) == NULL) { println(INFO, "Corrupted JP2 file. Exitting."); return NULL; } read = 8; if(box->length == 1) { //there should be XLbox field present; box->xlbox = (unsigned char*)malloc(9 * sizeof(char)); box->xlbox = read_bytes(box->xlbox, fd, 8); box->length = hex_to_long(box->xlbox, 8); read += 8; } box->content_length = box->length - read; box->dbox = (unsigned char*)malloc((box->content_length + 1) * sizeof(char)); box->dbox = read_bytes(box->dbox, fd, box->content_length); println_end(INFO); return box; }
int h_image_header_box(box *b, type_image *img) { char *cheight = (char*)malloc(5 * sizeof(char)); cheight = sstrncpy(cheight, (const char*)b->dbox, 4); img->height = hex_to_long((unsigned char*)cheight, 4); char *cwidth = (char*)malloc(5 * sizeof(char)); cwidth = sstrncpy(cwidth, (const char*)&(b->dbox)[4], 4); img->width = (unsigned short)hex_to_long((unsigned char*)cwidth, 4); char *cnum_comp = (char*)malloc(3 * sizeof(char)); cnum_comp = sstrncpy(cnum_comp, (const char*)&(b->dbox)[8], 2); img->num_components = (unsigned short)hex_to_long((unsigned char*)cnum_comp, 2); //TODO: bpc if(b->dbox[11] != 7) { println_var(INFO, "Value of the 'Compression type' shall be 7, not: %i", b->dbox[11]); } if(b->dbox[12] == 0) { println(INFO, "Colorspace know. Specified in Colorspace box"); } else if(b->dbox[12] == 1) { println(INFO, "Colorspace UNKNOWN."); } return 0; }
int h_header_box(box *header_box, type_image *img) { println_start(INFO); header_box->read = 0; box *ihdr = get_next_box_char(header_box); //TODO: extract box from within b if(hex_to_long(ihdr->tbox, 4) != IMAGE_HEADER_BOX) { println(INFO, "Image Header Box should be the first one in Header superbox. Exitting!"); return 1; } else h_image_header_box(ihdr, img); box *b; while( (b = get_next_box_char(header_box)) != NULL ) { if(hex_to_long(b->tbox, 4) == BITS_PER_COMPONENT_BOX) { println(INFO, "Bits Per Component box"); } else if(hex_to_long(b->tbox,4) == COLOR_BOX) { println(INFO, "Color Specification Box"); } else if(hex_to_long(b->tbox,4) == PALETTE_BOX) { println(INFO, "Palette Box"); } else if(hex_to_long(b->tbox,4) == COMPONENT_MAPPING_BOX) { println(INFO, "Component Mapping Box"); } else if(hex_to_long(b->tbox,4) == CHANNEL_DEFINITION_BOX) { println(INFO, "Channel Definition Box"); } } return 0; }
/* * Break up ADDR_ALL into ADDR_P and ADDR_COUNT_P */ void _dmalloc_address_break(const char *addr_all, DMALLOC_PNT *addr_p, unsigned long *addr_count_p) { char *colon_p; SET_POINTER(addr_p, (DMALLOC_PNT)hex_to_long(addr_all)); if (addr_count_p != NULL) { colon_p = strchr(addr_all, ':'); if (colon_p != NULL) { *addr_count_p = loc_atoul(colon_p + 1); } } }
box *get_next_box_char(box *superbox) { if(superbox->read >= superbox->content_length) return NULL; char *content = (char*)superbox->dbox; box* nextBox = (box*)malloc(sizeof(box)); int read = superbox->read; nextBox->lbox = (unsigned char*)malloc(5 * sizeof(char)); nextBox->lbox = (unsigned char*)sstrncpy((char*)nextBox->lbox, &content[read], 4); read += 4; nextBox->length = hex_to_long(nextBox->lbox, 4); println_var(INFO, "lbox: %i", nextBox->length); nextBox->tbox = (unsigned char*)malloc(5 * sizeof(char)); nextBox->tbox = (unsigned char*)sstrncpy((char*)nextBox->tbox, &content[read], 4); read += 4; println(INFO, "tbox"); //box->length = hex_to_long(box->lbox, 4); // println_var(INFO, "length: %i", box->length); if(nextBox->length == 1) { //there should be XLbox field present; nextBox->xlbox = (unsigned char*)malloc(9 * sizeof(char)); nextBox->xlbox = (unsigned char*)sstrncpy((char*)nextBox->xlbox, &content[read], 8); nextBox->length = hex_to_long(nextBox->xlbox, 8); read += 8; } int size = (nextBox->length - (read - superbox->read)); nextBox->dbox = (unsigned char*)malloc((size+1) * sizeof(char)); nextBox->dbox = (unsigned char*)sstrncpy((char*)nextBox->dbox, &content[read], size); read += size; nextBox->content_length = nextBox->length - read; println(INFO, "dbox"); superbox->read = read; println_end(INFO); return nextBox; }
int h_filetype_box(box *b, type_image *img) { char *br = (char*)malloc(5 * sizeof(char)); br = sstrncpy(br, (const char*)b->dbox, 4); if(strcmp(br, "jp2\040")) { println(INFO, "DOSEN'T Conform to IS 15444-1. Exiting"); return 1; } else println(INFO, "Conforms to IS 15444-1"); char *minv = (char*)malloc(5 * sizeof(char)); minv = sstrncpy(minv, (const char*)&(b->dbox[4]), 4); if(hex_to_long( (unsigned char*)minv, 4) != 0) { println(INFO, "MinV should be 0"); } else println(INFO, "MinV OK"); int left = b->content_length - 8; //16 bytes already read from the box's contents: br,minv printf("left: %i\n", left); char *cl; int i = 1; while(left) { cl = (char*)malloc(5 * sizeof(char)); cl = sstrncpy(cl, (const char*)&(b->dbox)[4 + i*4], 4); left -= 4; i++; //TODO: filetype box: codestream profile restrictions if(!strcmp(cl, "J2P0")) { println(INFO, "First codestream is restricted: Profile-0"); } else if(!strcmp(cl, "J2P1")) { println(INFO, "First codestream is restricted: Profile-1"); } else if(!strcmp(cl, "jp2\040")) { println(INFO, "One CL field with correct fingerprint"); } else println_var(INFO, "CL: >%s<", cl); } return 0; }
int jp2_parse_boxes(FILE *fd, type_image *img) { box *sig = get_next_box(fd); if(hex_to_long(sig->tbox, 4) != JP2_SIGNATURE_BOX) println(INFO, "JP2 signature box should be very first box in the file: Header"); if(hex_to_long(sig->dbox, 4) != JP2_SIG_BOX_CONTENT) println(INFO, "JP2 signature box should be very first box in the file: Content"); //dispose_of(sig); //free(sig); box *ft = get_next_box(fd); if(hex_to_long(ft->tbox, 4) != JP2_FILETYPE_BOX) println(INFO, "JP2 filetype box should directly follow JP2 signature box"); if(h_filetype_box(ft, img)) return 1; box *b; while( (b = get_next_box(fd)) != NULL ) { if(hex_to_long(b->tbox, 4) == JP2_HEADER_BOX) { println(INFO, "Header Box"); h_header_box(b,img); } else if(hex_to_long(b->tbox,4) == CODE_STREAM_BOX) { println(INFO, "Contiguous Codestream Box"); h_contiguous_codestream_box(b,img); } else if(hex_to_long(b->tbox,4) == INTELLECTUAL_PROPERTY_BOX) { println(INFO, "Intellectual Property Box"); } } println_end(INFO); return 0; }
/* * Process the values of dmalloc environ variable(s) from ENVIRON * string. */ void _dmalloc_environ_process(const char *env_str, DMALLOC_PNT *addr_p, unsigned long *addr_count_p, unsigned int *debug_p, unsigned long *interval_p, int *lock_on_p, char **logpath_p, char **start_file_p, int *start_line_p, unsigned long *start_iter_p, unsigned long *start_size_p, unsigned long *limit_p) { char *env_p, *this_p; char buf[1024]; int len, done_b = 0; unsigned int flags = 0; attr_t *attr_p; SET_POINTER(addr_p, NULL); SET_POINTER(addr_count_p, 0); SET_POINTER(debug_p, 0); SET_POINTER(interval_p, 0); SET_POINTER(lock_on_p, 0); SET_POINTER(logpath_p, NULL); SET_POINTER(start_file_p, NULL); SET_POINTER(start_line_p, 0); SET_POINTER(start_iter_p, 0); SET_POINTER(start_size_p, 0); SET_POINTER(limit_p, 0); /* make a copy */ (void)strncpy(buf, env_str, sizeof(buf)); buf[sizeof(buf) - 1] = '\0'; /* handle each of tokens, in turn */ for (env_p = buf, this_p = buf; ! done_b; env_p++, this_p = env_p) { /* find the comma of end */ for (;; env_p++) { if (*env_p == '\0') { done_b = 1; break; } if (*env_p == ',' && (env_p == buf || *(env_p - 1) != '\\')) { break; } } /* should we strip ' ' or '\t' here? */ if (this_p == env_p) { continue; } *env_p = '\0'; len = strlen(ADDRESS_LABEL); if (strncmp(this_p, ADDRESS_LABEL, len) == 0 && *(this_p + len) == ASSIGNMENT_CHAR) { this_p += len + 1; _dmalloc_address_break(this_p, addr_p, addr_count_p); continue; } len = strlen(DEBUG_LABEL); if (strncmp(this_p, DEBUG_LABEL, len) == 0 && *(this_p + len) == ASSIGNMENT_CHAR) { this_p += len + 1; SET_POINTER(debug_p, hex_to_long(this_p)); continue; } len = strlen(INTERVAL_LABEL); if (strncmp(this_p, INTERVAL_LABEL, len) == 0 && *(this_p + len) == ASSIGNMENT_CHAR) { this_p += len + 1; SET_POINTER(interval_p, loc_atoul(this_p)); continue; } len = strlen(LOCK_ON_LABEL); if (strncmp(this_p, LOCK_ON_LABEL, len) == 0 && *(this_p + len) == ASSIGNMENT_CHAR) { this_p += len + 1; SET_POINTER(lock_on_p, atoi(this_p)); continue; } /* get the dmalloc logfile name into a holding variable */ len = strlen(LOGFILE_LABEL); if (strncmp(this_p, LOGFILE_LABEL, len) == 0 && *(this_p + len) == ASSIGNMENT_CHAR) { this_p += len + 1; (void)strncpy(log_path, this_p, sizeof(log_path)); log_path[sizeof(log_path) - 1] = '\0'; SET_POINTER(logpath_p, log_path); continue; } /* * start checking the heap after X iterations OR * start at a file:line combination */ len = strlen(START_LABEL); if (strncmp(this_p, START_LABEL, len) == 0 && *(this_p + len) == ASSIGNMENT_CHAR) { this_p += len + 1; _dmalloc_start_break(this_p, start_file_p, start_line_p, start_iter_p, start_size_p); continue; } /* set the memory limit to the library */ len = strlen(LIMIT_LABEL); if (strncmp(this_p, LIMIT_LABEL, len) == 0 && *(this_p + len) == ASSIGNMENT_CHAR) { this_p += len + 1; SET_POINTER(limit_p, loc_atoul(this_p)); continue; } /* need to check the short/long debug options */ for (attr_p = attributes; attr_p->at_string != NULL; attr_p++) { if (strcmp(this_p, attr_p->at_string) == 0) { flags |= attr_p->at_value; break; } } if (attr_p->at_string != NULL) { continue; } } /* append the token settings to the debug setting */ if (debug_p != NULL) { if (*debug_p == 0) { *debug_p = flags; } else { *debug_p |= flags; } } }