Debian Codegraph @

src/hex2dbus.cc

raw
1 /* hex2dbus - an D-BUS packet decompiler
2 * Copyright (C) 2002-2007 Romain Liévin
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24
25 #include "ticables.h"
26 #include "internal.h"
27
28 static const unsigned char machine_id[] =
29 {
30 0x00, 0x02, 0x03, 0x05, 0x06, 0x07, 0x08, 0x08, 0x09, 0x23,
31 0x00, 0x82, 0x83, 0x85, 0x86, 0x74, 0x98, 0x88, 0x89, 0x73,
32 0xff
33 };
34
35 static const char* machine_way[] =
36 {
37 "PC>TI", "PC>TI", "PC>TI", "PC>TI", "PC>TI", "PC>TI", "PC>TI", "PC>TI", "PC>TI", "PC>TI",
38 "TI>PC", "TI>PC", "TI>PC", "TI>PC", "TI>PC", "TI>PC", "TI>PC", "TI>PC", "TI>PC", "TI>PC",
39 "",
40 };
41
42 static const unsigned char command_id[] =
43 {
44 0x06, 0x09, 0x15, 0x2d, 0x36, 0x47, 0x56, 0x5A,
45 0x68, 0x6D, 0x74, 0x78, 0x87, 0x88, 0x92, 0xA2,
46 0xB7, 0xC9,
47 0xff
48 };
49
50 static const char command_name[][8] =
51 {
52 "VAR", "CTS", "XDP", "VER", "SKP", "SID", "ACK", "ERR",
53 "RDY", "SCR", "RID", "CNT", "KEY", "DEL", "EOT", "REQ",
54 "IND", "RTS",
55 ""
56 };
57
58 static const int cmd_with_data[] =
59 {
60 !0, 0, !0, 0, !0, !0, 0, !0,
61 0, 0, 0, 0, 0, !0, 0, !0,
62 !0, !0, -1
63 };
64
65 static int is_a_machine_id(unsigned char id)
66 {
67 int i;
68
69 for (i=0; machine_id[i] != 0xff; i++)
70 {
71 if (id == machine_id[i])
72 {
73 break;
74 }
75 }
76
77 return i;
78 }
79
80 static int is_a_command_id(unsigned char id)
81 {
82 int i;
83
84 for (i=0; command_id[i] != 0xff; i++)
85 {
86 if (id == command_id[i])
87 {
88 break;
89 }
90 }
91
92 return i;
93 }
94
95 #define WIDTH 12
96
97 static int fill_buf(FILE *f, char data, int flush)
98 {
99 static char buf[WIDTH];
100 static unsigned int cnt = 0;
101 unsigned int i, j;
102
103 if (!flush)
104 {
105 buf[cnt++] = data;
106 }
107
108 if ((cnt >= WIDTH) || flush)
109 {
110 //printf(".");
111 fprintf(f, " ");
112 for (i = 0; i < cnt; i++)
113 {
114 fprintf(f, "%02X ", 0xff & buf[i]);
115 }
116
117 if (flush)
118 {
119 for (j = i; j < WIDTH; j++)
120 {
121 fprintf(f, " ");
122 }
123 }
124
125 fprintf(f, "| ");
126 for (i = 0; i < cnt; i++)
127 {
128 fprintf(f, "%c", isalnum(buf[i]) ? buf[i] : '.');
129 }
130
131 fprintf(f, "\n");
132 cnt = 0;
133 }
134
135 return 0;
136 }
137
138 /*
139 Format of data: 8 hexadecimal numbers with spaces
140 */
141 int dbus_decomp(const char *filename, int resync)
142 {
143 char src_name[1024];
144 char dst_name[1024];
145 FILE *fi = NULL, *fo = NULL;
146 long file_size;
147 unsigned char *buffer;
148 int i;
149 unsigned int j;
150 int num_bytes;
151 char str[256];
152 unsigned char mid, cid;
153 unsigned int length;
154 int idx;
155 int ret = 0;
156
157 // build filenames
158 snprintf(src_name, sizeof(src_name) - 1, "%s.hex", filename);
159 src_name[sizeof(src_name) - 1] = 0;
160
161 snprintf(dst_name, sizeof(dst_name) - 1, "%s.pkt", filename);
162 dst_name[sizeof(dst_name) - 1] = 0;
163
164 // open files
165 fi = fopen(src_name, "rt");
166 if (fi == NULL)
167 {
168 fprintf(stderr, "Unable to open input file: %s\n", src_name);
169 return -1;
170 }
171
172 if (fseek(fi, 0, SEEK_END) < 0)
173 {
174 fclose(fi);
175 return -1;
176 }
177 file_size = ftell(fi);
178 if (file_size < 0)
179 {
180 fclose(fi);
181 return -1;
182 }
183 if (fseek(fi, 0, SEEK_SET) < 0)
184 {
185 fclose(fi);
186 return -1;
187 }
188
189 // allocate buffer
190 buffer = (unsigned char*)calloc(file_size < 131072 ? 65536 : file_size / 2, 1);
191 if (buffer == NULL)
192 {
193 fprintf(stderr, "calloc error.\n");
194 fclose(fi);
195 return -1;
196 }
197 memset(buffer, 0xff, file_size/2);
198
199 fo = fopen(dst_name, "wt");
200 if (fo == NULL)
201 {
202 fprintf(stderr, "Unable to open output file: %s\n", dst_name);
203 free(buffer);
204 fclose(fi);
205 return -1;
206 }
207
208 fprintf(fo, "TI packet decompiler for D-BUS, version 1.2\n");
209
210 // skip comments
211 if ( fgets(str, sizeof(str), fi) == NULL
212 || fgets(str, sizeof(str), fi) == NULL
213 || fgets(str, sizeof(str), fi) == NULL)
214 {
215 goto exit;
216 }
217
218 // read source file
219 for (i = 0; !feof(fi);)
220 {
221 for (j = 0; j < 16 && !feof(fi); j++)
222 {
223 if (fscanf(fi, "%02X", (unsigned int *)&(buffer[i+j])) < 1)
224 {
225 ret = -1;
226 goto exit;
227 }
228 if (fgetc(fi) < 0)
229 {
230 goto exit;
231 }
232 }
233 i += j;
234
235 for (j=0; j<18 && !feof(fi); j++)
236 {
237 if (fgetc(fi) < 0)
238 {
239 goto exit;
240 }
241 }
242 }
243 num_bytes = i-1; // -1 due to EOF char
244 printf("%i bytes read.\n", num_bytes);
245
246 // process data
247 for (i = 0; i < num_bytes;)
248 {
249 restart:
250 mid = buffer[i+0];
251 cid = buffer[i+1];
252 length = buffer[i+2];
253 length |= ((int)(buffer[i+3])) << 8;
254
255 // check for valid packet
256 if (is_a_machine_id(mid) == -1)
257 {
258 ret = -1;
259 goto exit;
260 }
261
262 // check for valid packet
263 idx = is_a_command_id(cid);
264 if (idx == -1)
265 {
266 ret = -2;
267 goto exit;
268 }
269
270 fprintf(fo, "%02X %02X %02X %02X", mid, cid, length >> 8, length & 0xff);
271 for (j = 4; j <= WIDTH; j++)
272 {
273 fprintf(fo, " ");
274 }
275 fprintf(fo, " | ");
276 fprintf(fo, "%s: %s\n", machine_way[is_a_machine_id(mid)], command_name[is_a_command_id(cid)]);
277
278 i += 4;
279
280 // get data & checksum
281 if (cmd_with_data[idx] && length > 0)
282 {
283 // data
284 for(j = 0; j < length; j++, i++)
285 {
286 if (resync && buffer[i] == 0x98 && (buffer[i+1] == 0x15 || buffer[i+1] == 0x56))
287 {
288 printf("Warning: there is packets in data !\n");
289 fprintf(fo, "Beware : length of previous packet is wrong !\n");
290 goto restart;
291 }
292
293 fill_buf(fo, buffer[i], 0);
294 }
295
296 fill_buf(fo, 0, !0);
297 //fprintf(fo, "\n");
298
299 // write checksum
300 fprintf(fo, " ");
301 fprintf(fo, "%02X ", buffer[i++]);
302 fprintf(fo, "%02X ", buffer[i++]);
303 fprintf(fo, "\n");
304 }
305 }
306
307 exit:
308 if (ret < 0)
309 {
310 printf("Error %i\n", -ret);
311 }
312
313 free(buffer);
314 fclose(fo);
315 fclose(fi);
316
317 return ret;
318 }
319
320 #if 0
321 int main(int argc, char **argv)
322 {
323 int resync = 0;
324
325 if (argc < 2)
326 {
327 fprintf(stderr, "Usage: hex2dbus [file]\n");
328 exit(0);
329 }
330
331 if (argc > 2)
332 {
333 resync = !0;
334 }
335
336 return dbus_decomp(argv[1], resync);
337 }
338 #endif