| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
. |
| 18 |
|
| 19 |
|
| 20 |
#pragma once |
| 21 |
|
| 22 |
#include <ctype.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#define MAX_MARKER_LEN 50 |
| 27 |
#define NUM_OF_KATS 100 |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static inline int FindMarker(FILE *infile, const char *marker) |
| 34 |
{ |
| 35 |
int8_t line[MAX_MARKER_LEN]; |
| 36 |
uint32_t i, len; |
| 37 |
|
| 38 |
len = (int) strlen(marker); |
| 39 |
if (len > (MAX_MARKER_LEN - 1)) { |
| 40 |
len = MAX_MARKER_LEN - 1; |
| 41 |
} |
| 42 |
|
| 43 |
for (i = 0; i < len; i++) { |
| 44 |
if ((line[i] = fgetc(infile)) == EOF) { |
| 45 |
return -1; |
| 46 |
} |
| 47 |
} |
| 48 |
line[len] = '\0'; |
| 49 |
|
| 50 |
while (1) { |
| 51 |
if (!strncmp((char *) line, marker, len)) { |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
for (i = 0; i < len - 1; i++) { |
| 56 |
line[i] = line[i + 1]; |
| 57 |
} |
| 58 |
if ((line[len - 1] = fgetc(infile)) == EOF) { |
| 59 |
return -1; |
| 60 |
} |
| 61 |
line[len] = '\0'; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static inline int ReadHex(FILE *infile, uint8_t *buf, uint32_t len, const char *str) |
| 70 |
{ |
| 71 |
int ch; |
| 72 |
int started = 0; |
| 73 |
uint8_t ich; |
| 74 |
|
| 75 |
if (0 == len) { |
| 76 |
buf[0] = 0x00; |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
memset(buf, 0x00, len); |
| 81 |
|
| 82 |
if (FindMarker(infile, str) == -1) { |
| 83 |
return -1; |
| 84 |
} |
| 85 |
|
| 86 |
while ((ch = fgetc(infile)) != EOF) { |
| 87 |
if (!isxdigit(ch)) { |
| 88 |
if (!started) { |
| 89 |
if (ch == '\n') |
| 90 |
break; |
| 91 |
else |
| 92 |
continue; |
| 93 |
} else |
| 94 |
break; |
| 95 |
} |
| 96 |
started = 1; |
| 97 |
|
| 98 |
if ((ch >= '0') && (ch <= '9')) { |
| 99 |
ich = ch - '0'; |
| 100 |
} else if ((ch >= 'A') && (ch <= 'F')) { |
| 101 |
ich = ch - 'A' + 10; |
| 102 |
} else if ((ch >= 'a') && (ch <= 'f')) { |
| 103 |
ich = ch - 'a' + 10; |
| 104 |
} else { |
| 105 |
|
| 106 |
ich = 0; |
| 107 |
} |
| 108 |
|
| 109 |
for (uint32_t i = 0; i < len - 1; i++) { |
| 110 |
buf[i] = (buf[i] << 4) | (buf[i + 1] >> 4); |
| 111 |
} |
| 112 |
buf[len - 1] = (buf[len - 1] << 4) | ich; |
| 113 |
} |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |