[3ae31e9] | 1 | /*
|
---|
| 2 | ============================================================================
|
---|
| 3 | csrec.c -- Motorola S record format checker
|
---|
| 4 | Version 3 -- 1987-07-10 -- D.N. Lynx Crowe
|
---|
| 5 | ============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #define CHECKER 0 /* define non-zero for main */
|
---|
| 9 |
|
---|
| 10 | #if CHECKER
|
---|
| 11 |
|
---|
| 12 | #define MAXBUFLN 2048 /* big enough for the longest expected record */
|
---|
| 13 |
|
---|
| 14 | #include "stdio.h"
|
---|
| 15 |
|
---|
| 16 | char iobuf[MAXBUFLN+1];
|
---|
| 17 | char *fileid;
|
---|
| 18 |
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | #include <stddefs.h>
|
---|
| 22 | #include <ctype.h>
|
---|
| 23 |
|
---|
| 24 | #define SREC9 "04000000FB"
|
---|
| 25 |
|
---|
| 26 | static char ahex[] = "0123456789abcdefABCDEF";
|
---|
| 27 |
|
---|
| 28 | /* |
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | /*
|
---|
| 32 | ============================================================================
|
---|
| 33 | memcmpu -- compare two memory areas while ignoring case
|
---|
| 34 | ============================================================================
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | int
|
---|
| 38 | memcmpu(s1, s2, n)
|
---|
| 39 | register char *s1, *s2;
|
---|
| 40 | int n;
|
---|
| 41 | {
|
---|
| 42 | register int c1, c2;
|
---|
| 43 |
|
---|
| 44 | while (n) {
|
---|
| 45 |
|
---|
| 46 | c1 = 0377 & *s1++;
|
---|
| 47 | c2 = 0377 & *s2++;
|
---|
| 48 |
|
---|
| 49 | if (isascii(c1) && islower(c1))
|
---|
| 50 | c1 = _toupper(c1);
|
---|
| 51 |
|
---|
| 52 | if (isascii(c2) && islower(c2))
|
---|
| 53 | c2 = _toupper(c2);
|
---|
| 54 |
|
---|
| 55 | if (c1 < c2)
|
---|
| 56 | return(-1);
|
---|
| 57 |
|
---|
| 58 | if (c1 > c2)
|
---|
| 59 | return(1);
|
---|
| 60 |
|
---|
| 61 | n--;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | return(0);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /* |
---|
| 68 | */
|
---|
| 69 |
|
---|
| 70 | /*
|
---|
| 71 | ============================================================================
|
---|
| 72 | xdtoi -- convert hex ASCII to an int digit
|
---|
| 73 | ============================================================================
|
---|
| 74 | */
|
---|
| 75 |
|
---|
| 76 | static int
|
---|
| 77 | xdtoi(c)
|
---|
| 78 | register int c;
|
---|
| 79 | {
|
---|
| 80 | register int i;
|
---|
| 81 | register char *ap = &ahex[0];
|
---|
| 82 |
|
---|
| 83 | for (i = 0; i < 22; i++)
|
---|
| 84 | if (c EQ *ap++)
|
---|
| 85 | if (i >15)
|
---|
| 86 | return(i - 6);
|
---|
| 87 | else
|
---|
| 88 | return(i);
|
---|
| 89 |
|
---|
| 90 | return(-1);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /* |
---|
| 94 | */
|
---|
| 95 |
|
---|
| 96 | /*
|
---|
| 97 | ============================================================================
|
---|
| 98 | csrec -- check a Motorola S record for errors
|
---|
| 99 | ============================================================================
|
---|
| 100 | */
|
---|
| 101 |
|
---|
| 102 | int
|
---|
| 103 | csrec(line)
|
---|
| 104 | register char *line;
|
---|
| 105 | {
|
---|
| 106 | register int c, len, i;
|
---|
| 107 |
|
---|
| 108 | int csum, val;
|
---|
| 109 |
|
---|
| 110 | if ('S' NE (c = *line++))
|
---|
| 111 | return(-1);
|
---|
| 112 |
|
---|
| 113 | switch (c = *line++) {
|
---|
| 114 |
|
---|
| 115 | case '2':
|
---|
| 116 |
|
---|
| 117 | csum = 0;
|
---|
| 118 |
|
---|
| 119 | if (isxdigit(c = *line++))
|
---|
| 120 | len = xdtoi(c);
|
---|
| 121 | else
|
---|
| 122 | return(-1);
|
---|
| 123 |
|
---|
| 124 | if (isxdigit(c = *line++))
|
---|
| 125 | len = (len << 4) + xdtoi(c);
|
---|
| 126 | else
|
---|
| 127 | return(-1);
|
---|
| 128 |
|
---|
| 129 | csum += (len & 0377);
|
---|
| 130 | len -= 4;
|
---|
| 131 |
|
---|
| 132 | for (i = 0; i < 3; i++) {
|
---|
| 133 |
|
---|
| 134 | if (isxdigit(c = *line++))
|
---|
| 135 | val = xdtoi(c);
|
---|
| 136 | else
|
---|
| 137 | return(-1);
|
---|
| 138 |
|
---|
| 139 | if (isxdigit(c = *line++))
|
---|
| 140 | val = (val << 4) + xdtoi(c);
|
---|
| 141 | else
|
---|
| 142 | return(-1);
|
---|
| 143 |
|
---|
| 144 | csum += (val & 0377);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | for (i = 0; i < len; i++) {
|
---|
| 148 |
|
---|
| 149 | if (isxdigit(c = *line++))
|
---|
| 150 | val = xdtoi(c);
|
---|
| 151 | else
|
---|
| 152 | return(-1);
|
---|
| 153 |
|
---|
| 154 | if (isxdigit(c = *line++))
|
---|
| 155 | val = (val << 4) + xdtoi(c);
|
---|
| 156 | else
|
---|
| 157 | return(-1);
|
---|
| 158 |
|
---|
| 159 | csum += (val & 0377);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | csum = 0377 & ~csum;
|
---|
| 163 |
|
---|
| 164 | if (isxdigit(c = *line++))
|
---|
| 165 | val = xdtoi(c);
|
---|
| 166 | else
|
---|
| 167 | return(-1);
|
---|
| 168 |
|
---|
| 169 | if (isxdigit(c = *line++))
|
---|
| 170 | val = (val << 4) + xdtoi(c);
|
---|
| 171 | else
|
---|
| 172 | return(-1);
|
---|
| 173 |
|
---|
| 174 | if (csum NE (val & 0377))
|
---|
| 175 | return(-1);
|
---|
| 176 |
|
---|
| 177 | return(1);
|
---|
| 178 |
|
---|
| 179 | case '9':
|
---|
| 180 |
|
---|
| 181 | if (memcmpu(line, SREC9, 10) EQ 0)
|
---|
| 182 | return(0);
|
---|
| 183 | else
|
---|
| 184 | return(-1);
|
---|
| 185 |
|
---|
| 186 | default:
|
---|
| 187 |
|
---|
| 188 | return(-1);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | return(-1);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /* |
---|
| 195 | */
|
---|
| 196 |
|
---|
| 197 | #if CHECKER
|
---|
| 198 |
|
---|
| 199 | main(argc, argv)
|
---|
| 200 | int argc;
|
---|
| 201 | char **argv;
|
---|
| 202 | {
|
---|
| 203 | FILE *sfile;
|
---|
| 204 | int rc;
|
---|
| 205 | long recnum;
|
---|
| 206 |
|
---|
| 207 | if (argc < 2) {
|
---|
| 208 |
|
---|
| 209 | printf("csrec: No file specified on input line.\n");
|
---|
| 210 | printf("csrec: Using stdin for input.\n");
|
---|
| 211 | sfile = stdin;
|
---|
| 212 | fileid = "stdin";
|
---|
| 213 |
|
---|
| 214 | } else {
|
---|
| 215 |
|
---|
| 216 | ++argv;
|
---|
| 217 | fileid = *argv;
|
---|
| 218 |
|
---|
| 219 | if (NULL EQ (sfile = fopena(fileid, "r")) ) {
|
---|
| 220 |
|
---|
| 221 | printf("csrec: Unable to open [%s]\n", fileid);
|
---|
| 222 | printf("csrec: Processing aborted.\n");
|
---|
| 223 | exit(1);
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | recnum = 0L;
|
---|
| 228 |
|
---|
| 229 | do {
|
---|
| 230 |
|
---|
| 231 | recnum++;
|
---|
| 232 | memset(iobuf, ' ', MAXBUFLN+1);
|
---|
| 233 | rc = fgets(iobuf, MAXBUFLN, sfile);
|
---|
| 234 |
|
---|
| 235 | if (rc EQ NULL) {
|
---|
| 236 |
|
---|
| 237 | printf("csrec: Could not read record %ld on [%s]\n",
|
---|
| 238 | recnum, fileid);
|
---|
| 239 |
|
---|
| 240 | if (sfile NE stdin)
|
---|
| 241 | fclose(sfile);
|
---|
| 242 |
|
---|
| 243 | printf("csrec: Processing aborted.\n");
|
---|
| 244 | exit(1);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | if ((rc =csrec(iobuf)) < 0) {
|
---|
| 248 |
|
---|
| 249 | printf("csrec: Record %ld on [%s] is in error\n",
|
---|
| 250 | recnum, fileid);
|
---|
| 251 |
|
---|
| 252 | if (sfile NE stdin)
|
---|
| 253 | fclose(sfile);
|
---|
| 254 |
|
---|
| 255 | printf("csrec: Processing aborted.\n");
|
---|
| 256 | exit(1);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | } while (rc);
|
---|
| 260 |
|
---|
| 261 | if (sfile NE stdin)
|
---|
| 262 | fclose(sfile);
|
---|
| 263 |
|
---|
| 264 | printf("csrec: S-Record file [%s] is valid\n", fileid);
|
---|
| 265 | printf("csrec: Processing complete.\n");
|
---|
| 266 | exit(0);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | #endif
|
---|