73
static int bin_to_file(void *buf, int len, FILE *f)
75
static int bytes_on_line;
76
unsigned char *p = buf;
78
if (bytes_on_line != 0) {
79
if (fprintf(f, "\n") != 1)
86
for (i = 0; i < len; i++) {
87
if (fprintf(f, "%02x", *p++) != 2)
90
if (bytes_on_line >= 16) {
91
if (fprintf(f, "\n") != 1)
99
static int write_to_file(struct eeprom_table *eeprom_table, struct capabilities *caps, struct capkey *key, FILE *f)
101
fprintf(f, "-----BEGIN XORCOM LICENSE BLOCK-----\n");
102
fprintf(f, "Version: 1.0\n");
103
fprintf(f, "Timestamp: %u\n", caps->timestamp);
104
fprintf(f, "Serial: %.*s\n", LABEL_SIZE, eeprom_table->label);
105
fprintf(f, "Capabilities.Port.FXS: %d\n", caps->ports_fxs);
106
fprintf(f, "Capabilities.Port.FXO: %d\n", caps->ports_fxo);
107
fprintf(f, "Capabilities.Port.BRI: %d\n", caps->ports_bri);
108
fprintf(f, "Capabilities.Port.PRI: %d\n", caps->ports_pri);
109
fprintf(f, "Capabilities.Twinstar: %d\n", CAP_EXTRA_TWINSTAR(caps));
110
fprintf(f, "Data:\n");
111
bin_to_file(eeprom_table, sizeof(*eeprom_table), f);
112
bin_to_file(caps, sizeof(*caps), f);
113
bin_to_file(key, sizeof(*key), f);
114
bin_to_file(NULL, 0, f);
115
fprintf(f, "-----END XORCOM LICENSE BLOCK-----\n");
120
* Removes whitespace on both sizes of the string.
121
* Returns a pointer to the first non-space char. The string
122
* is modified in place to trim trailing whitespace.
123
* If the whole string is whitespace, returns NULL.
128
while (len > 0 && isspace(s[len-1])) {
136
/* *s is not a space, since in this case we'd return NULL above */
140
int get_key_value(char *line, char **key, char **value)
142
char *p = strchr(line, ':');
147
*value = trim(p + 1);
151
static int hex_digit_to_int(char c)
153
if (c >= '0' && c <= '9')
155
else if (c >= 'a' && c <= 'f')
161
static int str_to_bin(char *line, void *buf, int maxlen)
164
unsigned char *p = buf;
165
if (strlen(line) % 2 != 0)
167
while (offset < maxlen && *line) {
169
char c = hex_digit_to_int(*line++);
170
if (c < 0 || *line == '\0')
173
c = hex_digit_to_int(*line++);
179
if (offset == maxlen && *line)
184
static int read_from_file(struct eeprom_table *eeprom_table, struct capabilities *caps, struct capkey *capkey, FILE *f)
187
char *line, *key, *value;
191
struct eeprom_table eeprom_table;
192
struct capabilities capabilities;
193
struct capkey capkey;
196
memset(&table, 0, sizeof(struct table));
199
* 0: start - before BEGIN_LICENSE_BLOCK line. on BEGIN_LICENSE_BLOCK line goto 1.
200
* 1: read Version, goto 2. if not version line then error.
201
* 2: after BEGIN line. split line into key:value. if line is Data:, goto 3.
202
* 3: read binary data. if line is END_LICENSE_BLOCK goto 4.
203
* 4: END_LICENSE_BLOCK - ignore lines.
205
while (fgets(buf, 256, f) != NULL) {
207
int len = strlen(buf);
208
if (len > 0 && buf[len-1] != '\n') {
209
ERR("Line %d: Line too long\n", lineno);
214
if (state > 0 && state < 4) {
215
ERR("Line %d: Empty line\n", lineno);
223
if (strcmp(line, "-----BEGIN XORCOM LICENSE BLOCK-----") == 0)
226
ERR("Line %d: Invalid license begin block\n", lineno);
231
if (get_key_value(line, &key, &value) < 0) {
232
ERR("Line %d: Can't parse line\n", lineno);
235
if (strcmp(key, "Version") == 0) {
236
if (strcmp(value, "1.0") == 0) {
239
ERR("Line %d: Unknown license file version '%s', need version '1.0'\n", lineno, value);
243
ERR("Line %d: No license file version\n", lineno);
248
if (get_key_value(line, &key, &value) < 0) {
249
ERR("Line %d: Can't parse line\n", lineno);
252
if (strcmp(key, "Data") == 0) {
258
if (strcmp(line, "-----END XORCOM LICENSE BLOCK-----") == 0) {
262
if (str_to_bin(line, &table, sizeof(table)) < 0) {
263
ERR("Line %d: Error in data block\n", lineno);
273
ERR("Invalid license file\n");
276
memcpy(eeprom_table, &table.eeprom_table, sizeof(*eeprom_table));
277
memcpy(caps, &table.capabilities, sizeof(*caps));
278
memcpy(capkey, &table.capkey, sizeof(*capkey));
282
76
int main(int argc, char *argv[])
284
78
char *devpath = NULL;