Index: work/alien_formats/eagle/eagle_bin/test_parser/eagle_bin.c =================================================================== --- work/alien_formats/eagle/eagle_bin/test_parser/eagle_bin.c (revision 9175) +++ work/alien_formats/eagle/eagle_bin/test_parser/eagle_bin.c (revision 9176) @@ -682,8 +682,93 @@ return d; } +int read_drc(void *ctx, FILE *f, const char *fn) +{ + unsigned char sub_block[8]; + unsigned char block[8]; + unsigned char DRC_block[96]; + long sentinel_found = 0; + long magic_found = 0; + int extra_bytes = 0; + long offset = 0; + int index = 0; -int read_block(int level, void *ctx, FILE *f, const char *fn) + if (fread(block, 1, 8, f) != 8) { + printf("E: short DRC sentinel read. DRC start sentinel not found.\n"); + return -1; + } + + /* look for DRC sentinel */ + while (!sentinel_found) /* \x10\x04\x00\x20 */ + { + for (offset = 0; offset < 5; offset++) { + if (load_long(block, offset, 1) == 0x10 + && load_long(block, offset+1, 1) == 0x04 + && load_long(block, offset+2, 1) == 0x00 + && load_long(block, offset+3, 1) == 0x20) { + sentinel_found = 1; + } + } + if (fread(sub_block, 1, 4, f) != 4) { + printf("E: short DRC sentinel read. DRC start sentinel not found.\n"); + return -1; + } + for (index = 7; index > 3; index--) { + block[index-4] = block[index]; + block[index] = sub_block[index-4]; + } + } + printf("found DRC sentinel x10x04x00x20.\n"); + + while (!magic_found) /* \x78\x56\x34\x12 */ + { + for (offset = 0; offset < 5; offset++) { + if (load_long(block, offset, 1) == 0x78 + && load_long(block, offset+1, 1) == 0x56 + && load_long(block, offset+2, 1) == 0x34 + && load_long(block, offset+3, 1) == 0x12) { + magic_found = 1; + extra_bytes = 8 - 4 - offset; + } + } + if (fread(sub_block, 1, 4, f) != 4) { + printf("E: short DRC magic read. DRC magic not found.\n"); + return -1; + } + for (index = 7; index > 3; index--) { + block[index-4] = block[index]; + block[index] = sub_block[index-4]; + } + } + + printf("found DRC magic x78x56x34x12.\n"); + printf("readbuffer still contains %d DRC bytes\n", extra_bytes); + if (fread(DRC_block, 1, 96, f) != 96) { + printf("E: short DRC value block read. DRC section incomplete.\n"); + return -1; + } + for (index = 95; index >= extra_bytes; index--) { + DRC_block[index] = DRC_block[index-extra_bytes]; + } + for (index = 0; index < extra_bytes; index++) { + DRC_block[index] = block[7-index]; + } + /* following 92 bytes contain useful DRC stuff + # wire2wire wire2pad wire2via pad2pad pad2via via2via pad2smd via2smd smd2smd + self.clearances, data = _cut('<9I', data, 36) i.e. 9 integers, 4 bytes each + # restring order: padtop padinner padbottom viaouter viainner + (microviaouter microviainner) + restring_percentages = 7 doubles, 56 bytes total */ + printf("wire2wire: %ld\n", load_long(block, 0, 4)); + printf("wire2pad: %ld\n", load_long(block, 4, 4)); + printf("wire2via: %ld\n", load_long(block, 8, 4)); + printf("padtop: %f\n", load_double(block, 36, 8)); + printf("padinner: %f\n", load_double(block, 44, 8)); + return 0; +} + + +int read_block(long *numblocks, int level, void *ctx, FILE *f, const char *fn) { unsigned char block[24]; char tmp[25]; @@ -702,6 +787,11 @@ return -1; } + if (*numblocks < 0 && load_long(block, 0, 1) == 0x10) { + *numblocks = load_long(block, 4, 4); + printf("numblocks found in start block: %ld\n", *numblocks); + } + for(sc = pcb_eagle_script; sc->cmd != 0; sc++) { int match = 1; if (sc->cmd != block[0]) @@ -747,21 +837,43 @@ } } + *numblocks = *numblocks - 1; + for(ss = sc->subs; ss->offs != 0; ss++) { unsigned long int n, numch = load_ulong(block, ss->offs, ss->len); - for(n = 0; n < numch; n++) { - int res = read_block(level+1, ctx, f, fn); + printf("About to parse %ld sub-blocks\n", numch); + for(n = 0; n < numch && *numblocks != 0; n++) { + int res = read_block(numblocks, level+1, ctx, f, fn); if (res != 0) return res; } } - return 0; + printf("blocks remaining at end of read_block routine = %ld\n", *numblocks); + + return 0; } int pcb_egle_bin_load(void *ctx, FILE *f, const char *fn) { - return read_block(1, ctx, f, fn); + long *numblocks = NULL; + int res = 0; + long test = -1; + numblocks = &test; + + printf("blocks remaining prior to function call = %ld\n", *numblocks); + while (*numblocks != 0) { + res = read_block(numblocks, 1, ctx, f, fn); + if (res != 0) { + return res; + } + printf("blocks remaining after outer function call = %ld\n\n", *numblocks); + } + printf("Section blocks have been parsed. Next job is finding DRC.\n\n"); + + read_drc(ctx, f, fn); + + return 0; }