summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/keytab.h0
-rw-r--r--src/include/parser.h6
-rw-r--r--src/keytab.c0
-rw-r--r--src/main.c19
-rw-r--r--src/parser.c35
5 files changed, 50 insertions, 10 deletions
diff --git a/src/include/keytab.h b/src/include/keytab.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/include/keytab.h
diff --git a/src/include/parser.h b/src/include/parser.h
index f4a6c77..25045ae 100644
--- a/src/include/parser.h
+++ b/src/include/parser.h
@@ -5,11 +5,9 @@ extern int blen;
enum btype {
BEGIN = 1,
- END, OBJ,
+ END, PAIR,
- COLON,
-
- IDENTIFIER, VALUE
+ ERROR
};
struct byte {
diff --git a/src/keytab.c b/src/keytab.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/keytab.c
diff --git a/src/main.c b/src/main.c
index 64c5de2..56253a6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -15,11 +15,26 @@ int main(int argc, char **argv)
if (buf == NULL)
exit(0);
struct byte *bytes = parse(buf);
+ free(buf);
+
for (int i = 0; i < blen; ++i) {
- printf("%d\n", bytes[i].type);
+ switch (bytes[i].type) {
+ case BEGIN:
+ printf("BEGIN\n");
+ break;
+ case END:
+ printf("END\n");
+ break;
+ case PAIR:
+ printf("PAIR: %s\n", bytes[i].value);
+ free(bytes[i].value);
+ break;
+ case ERROR:
+ fprintf(stderr, "%s\n", bytes[i].value);
+ exit(1);
+ }
}
free(bytes);
- free(buf);
exit(0);
}
diff --git a/src/parser.c b/src/parser.c
index 4cd26fa..54e7ecc 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -7,9 +7,11 @@
int blen;
static void addbyte(struct byte *bytes, int *pos, enum btype type, char *value);
+static char *getpair(uint8_t *buf, int *pos);
struct byte *parse(uint8_t *buf)
{
+ char *value;
int len = 2;
struct byte *bytes = calloc(len, sizeof(struct byte));
for (int i = 0, j = 0; buf[i]; ++i) {
@@ -17,12 +19,21 @@ struct byte *parse(uint8_t *buf)
len *= 2;
bytes = realloc(bytes, len * sizeof(struct byte));
switch (buf[i]) {
- case '\xb':
+ case 250:
+ value = getpair(buf, &i);
+ if (value == NULL)
+ addbyte(bytes, &j, ERROR, "Invalid Key-Value pair");
+ else
+ addbyte(bytes, &j, PAIR, value);
+ break;
+ case 251:
addbyte(bytes, &j, BEGIN, NULL);
break;
- case '\xe':
+ case 254:
addbyte(bytes, &j, END, NULL);
break;
+ default:
+ addbyte(bytes, &j, ERROR, "Invalid chunk");
}
blen = j;
}
@@ -34,11 +45,27 @@ static void addbyte(struct byte *bytes, int *pos, enum btype type, char *value)
bytes[*pos].type = type;
if (value != NULL) {
- bytes[*pos].value = calloc(strlen(value) + 1, sizeof(char));
- strcpy(bytes[*pos].value, value);
+ bytes[*pos].value = value;
}
else
bytes[*pos].value = NULL;
(*pos)++;
}
+
+static char *getpair(uint8_t *buf, int *pos)
+{
+ int i;
+ for (i = *pos + 1; buf[i] != 250 && buf[i] != 254 && buf[i]; ++i)
+ ;
+ if (buf[i] != 250 && buf[i] != 254)
+ return NULL;
+
+ char *pair = calloc(i - *pos, sizeof(char));
+ int j;
+ for (j = *pos + 1; j < i; ++j)
+ pair[j - (*pos + 1)] = buf[j];
+ pair[j - (*pos + 1)] = '\0';
+ *pos = i - 1;
+ return pair;
+}