-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode-backup.c
194 lines (184 loc) · 3.91 KB
/
decode-backup.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
static write_record (char *par, char *val, FILE *f) {
if (NULL != f) {
if (NULL == val || strlen (val) < 1) {
fprintf (f, "%s |\n", par);
}
else {
fprintf (f, "%s | (%s)\n", par, val);
}
}
else {
printf ("\t");
}
if (NULL == val || strlen (val) < 1) {
printf ("%s |\n", par);
}
else {
printf ("%s | (%s)\n", par, val);
}
}
static int read_record (int fd, FILE* f) {
char lenpar;
char param[256];
unsigned short lenval;
char value[65536];
int x;
x = read (fd, &lenpar, 1);
if (1 != x) {
if (NULL != f) {
if (errno != 0) {
printf ("error reading file descriptor %d : %s\n", fd, strerror(errno));
}
else {
printf ("error reading file descriptor %d @ %ld\n", fd, lseek (fd, 0, SEEK_CUR));
}
}
return errno == 0 ? -1 : 1;
}
bzero (param, sizeof(param));
x = read (fd, param, lenpar);
if (lenpar != x) {
if (NULL != f) {
printf ("error reading parameter : %d <> %d : %s\n", x, (int)lenpar, strerror(errno));
}
return 2;
}
x = read (fd, &lenval, 2);
if (2 != x) {
if (NULL != f) {
printf ("error reading value length : %d <> %d : %s\n", x, (int)lenval, strerror(errno));
}
return 3;
}
if (0 == lenval) {
write_record (param, "", f);
/*
if (NULL != f) {
printf ("no value for parameter '%s'\n", param);
} */
return 0;
}
bzero (value, sizeof(value));
if (lenval != read (fd, value, lenval)) {
if (NULL != f) {
printf ("error reading value of parameter '%s' : %d <> %d : %s\n", param, x, (int)lenval, strerror(errno));
}
return 4;
}
/*
if (value[lenval-1] == '\n') {
value[lenval-1] = 0;
}*/
write_record (param, value, f);
return 0;
}
/**
* http://www.dd-wrt.com/phpBB2/viewtopic.php?t=33164&highlight=nvram+csv
*/
int main (int ac, char* av[]) {
char* fisier;
FILE* outf = NULL;
int fdi;
struct stat buf;
char header[8];
unsigned short nbrecords = 0;
int i, j;
if (ac < 2) {
printf ("Usage : %s <in file> [<out file>]\n", av[0]);
exit (1);
}
fisier = av[1];
if ((fdi = open (fisier, O_RDONLY)) == -1) {
printf ("error opening '%s' : %s\n", fisier, strerror(errno));
exit (2);
}
if (fstat (fdi, &buf)) {
printf ("error reading status of '%s' : %s\n", fisier, strerror(errno));
close (fdi);
exit (2);
}
if (buf.st_size < 8) {
printf ("error reading size of '%s' : %ld bytes\n", fisier, buf.st_size);
close (fdi);
exit (3);
}
if (ac > 2) {
outf = fopen (av[2], "w");
printf ("decoding '%s' in '%s'\n", fisier, av[2]);
if (NULL == outf) {
printf ("error creating file '%s'\n", av[2]);
close (fdi);
exit (4);
}
}
if (read (fdi, header, sizeof (header)) < sizeof (header)) {
printf ("error reading header of '%s'\n", fisier);
close (fdi);
if (NULL != outf) {
fclose (outf);
unlink (av[2]);
}
exit (5);
}
if (0 != strncmp (header, "DD-WRT", 6)) {
header[6] = 0;
printf ("error reading header of '%s' : %s\n", fisier, header);
close (fdi);
if (NULL != outf) {
fclose (outf);
unlink (av[2]);
}
exit (6);
}
nbrecords = (unsigned char)header[6]; /* ((unsigned short)header[6]) << 8 | header[7]; */
if (4 != header[7]) {
printf ("error reading header of '%s' : %d\n", fisier, (int)header[7]);
close (fdi);
if (NULL != outf) {
fclose (outf);
unlink (av[2]);
}
exit (7);
}
if (NULL != outf) {
printf ("%d saved records in '%s'\n", nbrecords, fisier);
}
/*
for (i = 0; i < nbrecords; i++) {
int j = read_record (fdi, outf);
if (j) {
if (NULL != outf) {
printf ("read %d records\n", i);
}
close (fdi);
if (j > 0) {
fclose (outf);
unlink (av[2]);
}
exit ( j > 0 ? 8 : 0);
}
} */
i = 0;
do {
j = read_record (fdi, outf);
if (j) {
printf ("read %d records\n", i);
}
else {
++i;
}
} while (!j);
if (NULL != outf) {
fclose (outf);
}
close (fdi);
return 0;
}