int parse_block(int fd1,int fd2) { char tag; read(fd1,&tag,1); switch(GET_TAG(tag)) { case 0x00: parse_titleblock(fd1); break; case 0x01: parse_sheet(fd1); break; case 0x02: parse_component(fd1,fd2); break; case 0x03: parse_wire(fd1); break; case 0x04: parse_bus(fd1); break; case 0x05: parse_junction(fd1); break; case 0x06: parse_port(fd1); break; case 0x07: parse_label(fd1); break; case 0x08: parse_entry(fd1); break; case 0x09: parse_dashed(fd1); break; case 0x0a: parse_power(fd1); break; case 0x0b: parse_text(fd1); break; case 0x0c: parse_marker(fd1); break; case 0x0f: return 0; break; default: fprintf(stderr,"\nUnknown Block Tag\n"); exit(-1); break; } return 1; }
/* loop points have been found hiding here */ void parse_adtl(off_t adtl_offset, off_t adtl_length, STREAMFILE *streamFile, long *loop_start, long *loop_end, int *loop_flag) { int loop_start_found = 0; int loop_end_found = 0; off_t current_chunk = adtl_offset+4; while (current_chunk < adtl_offset+adtl_length) { uint32_t chunk_type = read_32bitBE(current_chunk,streamFile); off_t chunk_size = read_32bitLE(current_chunk+4,streamFile); if (current_chunk+8+chunk_size > adtl_offset+adtl_length) return; switch(chunk_type) { case 0x6c61626c: /* labl */ { unsigned char *labelcontent; labelcontent = malloc(chunk_size-4); if (!labelcontent) return; if (read_streamfile(labelcontent,current_chunk+0xc, chunk_size-4,streamFile)!=chunk_size-4) { free(labelcontent); return; } switch (read_32bitLE(current_chunk+8,streamFile)) { case 1: if (!loop_start_found && (*loop_start = parse_marker(labelcontent))>=0) { loop_start_found = 1; } break; case 2: if (!loop_end_found && (*loop_end = parse_marker(labelcontent))>=0) { loop_end_found = 1; } break; default: break; } free(labelcontent); } break; default: break; } current_chunk += 8 + chunk_size; } if (loop_start_found && loop_end_found) *loop_flag = 1; /* labels don't seem to be consistently ordered */ if (*loop_start > *loop_end) { long temp = *loop_start; *loop_start = *loop_end; *loop_end = temp; } }
static int process_resources(const char* input_file_name, const char* specific_file_name, int inserting, int force_processing, int verbose) { char buffer[2048], tmp_file_name[PATH_MAX]; const char *res_file_name; time_t rc_last_update, res_last_update; FILE *fin, *fres, *ftmp = 0; struct stat st; int fd, c; if (!(fin = fopen(input_file_name, "r"))) return 0; if (stat(input_file_name, &st) < 0) return 0; rc_last_update = st.st_mtime; if (inserting) { strcpy(tmp_file_name, input_file_name); strcat(tmp_file_name, "-XXXXXX.temp"); if ((fd = mkstemps(tmp_file_name, 5)) == -1) { strcpy(tmp_file_name, "/tmp/bin2res-XXXXXX.temp"); if ((fd = mkstemps(tmp_file_name, 5)) == -1) return 0; } clean_file = tmp_file_name; if (!(ftmp = fdopen(fd, "w"))) return 0; } for (c = EOF; fgets(buffer, sizeof(buffer), fin); c = EOF) { if (inserting) fprintf(ftmp, "%s", buffer); if (!(res_file_name = parse_marker(buffer, &res_last_update))) continue; if ( (specific_file_name && strcmp(specific_file_name, res_file_name)) || (!force_processing && ((rc_last_update < res_last_update) == !inserting)) ) { if (verbose) printf("skipping '%s'\n", res_file_name); continue; } if (verbose) printf("processing '%s'\n", res_file_name); while ( (c = fgetc(fin)) != EOF && c != '{') if (inserting) fputc(c, ftmp); if (c == EOF) break; if (inserting) { if (!(fres = fopen(res_file_name, "rb"))) break; if (!insert_hexdump(ftmp, fres)) break; while ( (c = fgetc(fin)) != EOF && c != '}') /**/; fclose(fres); } else { clean_file = res_file_name; if (!(fres = fopen(res_file_name, "wb"))) break; if (!extract_hexdump(fres, fin)) break; fclose(fres); clean_file = NULL; } } fclose(fin); if (inserting) { fclose(ftmp); if (c == EOF) { if (rename(tmp_file_name, input_file_name) < 0) { /* try unlinking first, Windows rename is brain-damaged */ if (unlink(input_file_name) < 0 || rename(tmp_file_name, input_file_name) < 0) return 0; } clean_file = NULL; } } return c == EOF; }