|
|
1.1 root 1: /************************************************************************
2: * *
3: * reccnv.c converts a standard PT hexfile to a larger record for *
4: * format. Options allows input filename to be defined output *
5: * fle is always hexout. Checksums are set to "00" instead of *
6: * trying to calculate them. *
7: * This program assumes that each record is 44 chars long it then *
8: * combines two records affectively into one. *
9: * 88 is the standard 152 the second pass and is the third pass *
10: * *
11: ************************************************************************/
12:
13: #include <stdio.h>
14: #define mode 0777
15: #define TRUE 1;
16: #define FALSE 0;
17: static int lastflg; /* says working on last records */
18:
19: main(argc, argv)
20: int argc;
21: char **argv;
22:
23: {
24: static char record[1024]; /* actual data */
25: static char srecord[1024]; /* actual data */
26: register int fs, ss, rs; /* record size */
27: static char *ttrp, *ttrp2;
28: FILE *in;
29: int outf;
30: int cs, i, pr;
31: char *name = "hexout"; /* default filename for output */
32: char *name1 = "DEFAULT_LD_FL"; /* default filename */
33: char *rp; /* record pointer */
34: int l=88; /*length of record to fix */
35: int dl; /* length of data segment */
36: int fl=9; /* header length always the same */
37: char *dlc; /* data length character */
38: int aoff; /* address offset for checking */
39:
40: if(argc < 2)
41: printf("\n using default read file and record size 88\n");
42: else if(argc == 2)
43: {
44: name1 = *(++argv);
45: }
46: else if(argc == 3)
47: {
48: name1 = *(++argv);
49: l = atoi(*(++argv));
50: }
51:
52: /* fix the record parameters */
53: switch(l)
54: {
55: case 3:
56: l = 280; /* length of two records to be combined */
57: dlc = "8"; /* new data length character */
58: dl = 256; /* new data field length */
59: aoff = 0x40; /* address offset of two sequ. rec's. */
60: break;
61: case 2:
62: l = 152;
63: dlc = "4";
64: dl = 128;
65: aoff = 0x20;
66: break;
67: default : /* 88 two normal rows */
68: l = 88;
69: dlc = "2";
70: dl = 64;
71: aoff = 0x10;
72: break;
73: }
74:
75: rp = record; /* setup pointer to array to dump info */
76: ttrp2 = srecord; /* setup pointer to array to dump info */
77: if((in = fopen(name1, "r")) == (FILE *)NULL )
78: printf("can't fopen input file\n");
79: if((outf = creat(name, mode)) < 0)
80: printf("can't create hexout\n");
81: if((open(name, 2)) < 0)
82: printf("can't open hexout\n");
83:
84: /* read and convert the records */
85: while (((fs = getrec(in, srecord )) + (ss = getrec(in, record))) > 0)
86: {
87: rs = fs + ss;
88: if(rs == l)
89: {
90: ttrp = rp;
91: /* check to see if the two records have sequential addresses */
92: if (pr = acnv(record, srecord, aoff)) /* addreses sequential ?? */
93: {
94: *(ttrp + 1) = *dlc; /* double the data amount */
95: ttrp = rp + ((dl/2)+fl); /* where to add new data */
96:
97: /* copy from data field */
98: for(i=1, ttrp2 = srecord+fl ; i<((dl/2) +1);i++)
99: *ttrp++ = *ttrp2++; /* copy data */
100: *ttrp++ = '\0';
101: *ttrp++ = '\0';
102: *ttrp++ = '\n';
103: write(outf, record, (dl+fl+3));
104: }
105: else if(pr == 0) /* not sequential */
106: /* they don't ave sequential addresses so just write the record as is */
107: {
108: if(ss >0)
109: write(outf, record, ss);
110: if(fs >0)
111: write(outf, srecord, fs);
112: continue;
113: }
114: } /*end of large file if */
115: else if(rs < l) /* if less than expected read dump as is */
116: {
117: /* printf("< if\n");
118: */
119: if(ss >0)
120: write(outf, record, ss);
121: if(fs >0)
122: write(outf, srecord, fs);
123: continue;
124: }
125: } /* end of while loop */
126: exit(0);
127: }
128:
129: /************************************************************************
130: * *
131: * converts address field and compares to see if they're sequential *
132: * *
133: * *
134: ************************************************************************/
135:
136: int
137: acnv(a,b,aoff)
138: char *a, *b; /* pointers to the two records */
139: int aoff; /* address offset */
140:
141: {
142: char g[5], h[5];
143: int i, ch, cg;
144:
145: a=a+3; b=b+3;
146: for(i=0; i<4; i++,a++,b++)
147: {
148: g[i] = *a;
149: h[i] = *b;
150: }
151: g[4] = '\0';
152: h[4] = '\0';
153: cg = cnv(g);
154: ch = cnv(h) - aoff;
155: if( ch == cg)
156: {
157: /* printf("out of order \n");
158: */
159: return 1;
160: }
161: else if(ch != cg)
162: return 0;
163:
164: }
165:
166: /************************************************************************
167: * cnv - converts four ascii digits to an integer *
168: * returns - the integer value *
169: * *
170: ************************************************************************/
171: int
172: cnv(sp)
173: char *sp;
174:
175: {
176: register int c, d, i, b;
177:
178: b = c = 0;
179:
180: for(i=0 ; i<4; i++, sp++)
181: {
182: if (*sp >= '0' && *sp <= '9')
183: b = *sp - '0';
184: else if (*sp >= 'A' && *sp <= 'F')
185: b = *sp - 'A' + 10;
186: else if (*sp >= 'a' && *sp <= 'f')
187: b = *sp - 'a' + 10;
188: switch (i)
189: {
190: case 3:
191: c = b + c;
192: case 2:
193: c = (b * 16) + c;
194: break;
195: case 1:
196: c = (b * 256) + c;
197: break;
198: case 0:
199: c = (b * 4096) + c;
200: break;
201: }
202: }
203: return c;
204: }
205:
206: /************************************************************************
207: * *
208: * getrec - Get a record to transmit. *
209: * *
210: * Returns: *
211: * -1 Error in file format. *
212: * 0 End of file reached. *
213: * > 0 Size of record to transmit. *
214: * *
215: ************************************************************************/
216:
217: int
218: getrec (fd, rp)
219:
220: FILE * fd;
221: char * rp;
222:
223: {
224:
225: int c;
226: int nc; /* number of characters */
227: int l; /* length of record bytes */
228: int cc; /* checksum */
229: int i; /* counter */
230: int a; /* address piece */
231:
232: while ((c = getc (fd)) != ':') /* Look for start of record */
233: if (c == EOF)
234: return 0; /* End of file */
235:
236: /***** Start record off. */
237:
238: /* printf("At record start\n"); */
239: *rp++ = ':';
240: nc = 1;
241:
242: /***** Get number of hex bytes in record. */
243:
244: if ((l = getxb (fd, rp)) < 0)
245: return -1;
246:
247: if(l == 1)
248: {
249: /* printf("01 record \n");
250: */
251: rp += 2;
252: for(i=1; i < 11; i += 2, rp +=2)
253: {
254: /* printf(" %u\n", i);
255: */
256: getxb(fd,rp);
257: }
258: *(rp) = '\n';
259: return 14;
260: }
261:
262: else if(l == 0)
263: {
264: printf("00 record \n");
265: rp += 2;
266: for(i=1; i < 8; i += 2, rp += 2)
267: {
268: printf(" %u\n",i);
269: getxb(fd,rp);
270: }
271: *++rp = '\n';
272: return 10;
273: }
274:
275: else
276: {
277: /* printf("normal record \n");
278: printf("%d data bytes\n", l);
279: */
280: cc = l; /* Start checksum */
281: rp += 2; /* Past two characters */
282: nc += 2; /* Two more characters */
283:
284: /***** Get address portion of data record. */
285:
286: for (i = 2 ; i > 0 ; i--) /* Two hex bytes */
287: {
288: if ((a = getxb (fd, rp)) < 0)
289: return -1;
290:
291: cc += a; /* Figure address into checksum */
292: rp += 2; /* Past two characters */
293: nc += 2; /* Two more characters */
294: }
295:
296: /***** Read type byte, which should be zero. */
297:
298: if ((i = getxb (fd, rp)) != 0)
299: return -1;
300:
301: cc += i; /* Figure type into checksum */
302: rp += 2; /* Past two characters */
303: nc += 2; /* Two more characters */
304:
305: /***** Read in the data record. */
306:
307: while (l-- > 0) /* Count down */
308: {
309: if ((i = getxb (fd, rp)) < 0)
310: return -1;
311:
312: /* printf ("Read %d\n", i); */
313: cc += i; /* Checksum */
314: rp += 2; /* Pointer */
315: nc += 2; /* Number of characters */
316: }
317:
318: cc = (-cc) & 0377; /* Two's complement byte */
319:
320: /* printf ("Checksum = %x\n", cc); */
321: *rp = cc >> 4;
322: *rp += *rp < 10 ? '0' : 'A' - 10;
323: *++rp = cc & 0xF;
324: *rp += *rp < 10 ? '0' : 'A' - 10;
325:
326: /* printf ("record size = %d\n", nc + 2); */
327: *++rp = '\n';
328: return nc + 3;
329:
330: } /* normal records */
331: }
332:
333: /************************************************************************
334: * *
335: * getxb - Get a hex byte. *
336: * *
337: * Returns: *
338: * -1 Error in file format or end of file reached. *
339: * >= 0 Integer value of byte. *
340: * *
341: ************************************************************************/
342:
343: int
344: getxb (fd, sp)
345:
346: FILE * fd;
347: char * sp;
348:
349: {
350:
351: register int i, j;
352:
353: if ((i = getxn (fd, sp++)) < 0 || (j = getxn (fd, sp)) < 0)
354: return -1;
355:
356: return (i << 4) | j;
357:
358: }
359:
360: /************************************************************************
361: * *
362: * getxn - Get a hex nibble. *
363: * *
364: * Returns: *
365: * -1 Error in file format or end of file reached. *
366: * >= 0 Integer value of nibble. *
367: * *
368: ************************************************************************/
369:
370: int
371: getxn (fd, sp)
372:
373: FILE * fd;
374: char * sp;
375:
376: {
377:
378: register int c;
379:
380: *sp = c = getc (fd);
381:
382: if (c >= '0' && c <= '9')
383: return c - '0';
384: else if (c >= 'A' && c <= 'F')
385: return c - 'A' + 10;
386: else if (c >= 'a' && c <= 'f')
387: return c - 'a' + 10;
388:
389: return -1;
390:
391: }
392:
393:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.