forked from mattstevens/dmap-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmapprint.c
164 lines (134 loc) · 3.36 KB
/
dmapprint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "dmap_parser.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
char prefix[64] = {};
static const char hexchars[] = "0123456789abcdef";
void indent() {
strcat(prefix, " ");
}
void outdent() {
size_t len = strlen(prefix);
if (len >= 2) {
prefix[len - 2] = '\0';
}
}
void append(const char* line, ...) {
va_list args;
va_start(args, line);
printf("%s", prefix);
vprintf(line, args);
printf("\n");
va_end(args);
}
void on_dict_start(void *ctx, const char *code, const char *name) {
append("%s:", name);
indent();
}
void on_dict_end(void *ctx, const char *code, const char *name) {
outdent();
}
void on_int32(void *ctx, const char *code, const char *name, int32_t value) {
append("%s: %d", name, value);
}
void on_int64(void *ctx, const char *code, const char *name, int64_t value) {
append("%s: %lld", name, value);
}
void on_uint32(void *ctx, const char *code, const char *name, uint32_t value) {
if (strcmp(code, "mcnm") == 0) {
char buf[5] = {
(value >> 24) & 0xff,
(value >> 16) & 0xff,
(value >> 8) & 0xff,
value & 0xff,
0
};
append("%s: %s", name, buf);
return;
}
append("%s: %u", name, value);
}
void on_uint64(void *ctx, const char *code, const char *name, uint64_t value) {
append("%s: %llu", name, value);
}
void on_date(void *ctx, const char *code, const char *name, uint32_t value) {
char buf[32];
time_t timeval = value;
struct tm* timestruct = gmtime(&timeval);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S +0000", timestruct);
append("%s: %s", name, buf);
}
void on_string(void *ctx, const char *code, const char *name, const char *buf, size_t len) {
char *str = (char *)malloc(len + 1);
strncpy(str, buf, len);
str[len] = '\0';
append("%s: %s", name, str);
free(str);
}
void on_data(void *ctx, const char *code, const char *name, const char *buf, size_t len) {
size_t i;
printf("%s%s: ", prefix, name);
for (i = 0; i < len; i++) {
putc(hexchars[buf[i] >> 4], stdout);
putc(hexchars[buf[i] & 0x0f], stdout);
putc(' ', stdout);
}
printf("\n");
}
int main(int argc, char* argv[]) {
dmap_settings settings = {
.on_dict_start = on_dict_start,
.on_dict_end = on_dict_end,
.on_int32 = on_int32,
.on_int64 = on_int64,
.on_uint32 = on_uint32,
.on_uint64 = on_uint64,
.on_date = on_date,
.on_string = on_string,
.on_data = on_data,
.ctx = 0
};
char *buf = NULL;
size_t size = 0;
ssize_t result = 0;
if (argc >= 2) {
if (strcmp(argv[1], "--help") == 0) {
printf("Usage: dmapprint [file]\n\n"
"Accepts Digital Media Access Protocol input from a file or "
"standard input and\nprints it in a human-readable format.\n");
return 0;
}
const char *path = argv[1];
struct stat s;
if (stat(path, &s) != 0) {
return 1;
}
size = s.st_size;
buf = malloc(size);
int fd = open(path, O_RDONLY);
if (fd == -1)
return 1;
do {
result = read(fd, buf, size);
} while (result == -1 && errno == EINTR);
close(fd);
if (result == -1)
return 1;
} else {
size_t bufIncrement = 60 * 1024;
do {
size += result;
buf = realloc(buf, size + bufIncrement);
result = read(fileno(stdin), &buf[size], bufIncrement);
} while (result > 0);
}
dmap_parse(&settings, buf, size);
free(buf);
return 0;
}