4 * Tool to dump the contents of a jigdo template file
6 * Copyright (c) 2004 Steve McIntyre <steve@einval.com>
17 #include <sys/types.h>
24 typedef unsigned long long UINT64;
25 typedef long long INT64;
26 typedef unsigned long UINT32;
28 #define BUF_SIZE 65536
40 INT64 find_string(unsigned char *buf, size_t buf_size, char *search)
42 size_t length = strlen(search);
45 for (result = 0; result < (buf_size - length); result++)
47 if (!memcmp(&buf[result], search, length))
53 INT64 parse_data_block(INT64 offset, unsigned char *buf, size_t buf_size)
55 /* Parse the contents of this data block... */
59 printf("\nDATA block found at offset %lld\n", offset);
60 dataLen = (UINT64)buf[4];
61 dataLen |= (UINT64)buf[5] << 8;
62 dataLen |= (UINT64)buf[6] << 16;
63 dataLen |= (UINT64)buf[7] << 24;
64 dataLen |= (UINT64)buf[8] << 32;
65 dataLen |= (UINT64)buf[9] << 40;
66 printf(" compressed block size %llu bytes\n", dataLen);
68 dataUnc = (UINT64)buf[10];
69 dataUnc |= (UINT64)buf[11] << 8;
70 dataUnc |= (UINT64)buf[12] << 16;
71 dataUnc |= (UINT64)buf[13] << 24;
72 dataUnc |= (UINT64)buf[14] << 32;
73 dataUnc |= (UINT64)buf[15] << 40;
74 printf(" uncompressed block size %llu bytes\n", dataUnc);
79 INT64 parse_desc_block(INT64 offset, unsigned char *buf, size_t buf_size)
81 /* Parse the contents of this data block... */
84 printf("\nDESC block found at offset %lld\n", offset);
85 descLen = (UINT64)buf[4];
86 descLen |= (UINT64)buf[5] << 8;
87 descLen |= (UINT64)buf[6] << 16;
88 descLen |= (UINT64)buf[7] << 24;
89 descLen |= (UINT64)buf[8] << 32;
90 descLen |= (UINT64)buf[9] << 40;
91 printf(" DESC block size is %llu bytes\n", descLen);
96 INT64 parse_desc_data(INT64 offset, unsigned char *buf, size_t buf_size)
99 printf(" DESC entry: block type %d\n", type);
106 skipLen = (UINT64)buf[1];
107 skipLen |= (UINT64)buf[2] << 8;
108 skipLen |= (UINT64)buf[3] << 16;
109 skipLen |= (UINT64)buf[4] << 24;
110 skipLen |= (UINT64)buf[5] << 32;
111 skipLen |= (UINT64)buf[6] << 40;
112 printf(" Unmatched data, %llu bytes\n", skipLen);
121 imglen = (UINT64)buf[1];
122 imglen |= (UINT64)buf[2] << 8;
123 imglen |= (UINT64)buf[3] << 16;
124 imglen |= (UINT64)buf[4] << 24;
125 imglen |= (UINT64)buf[5] << 32;
126 imglen |= (UINT64)buf[6] << 40;
128 blocklen = (UINT32)buf[23];
129 blocklen |= (UINT32)buf[24] << 8;
130 blocklen |= (UINT32)buf[25] << 16;
131 blocklen |= (UINT32)buf[26] << 24;
133 printf(" Original image length %llu bytes\n", imglen);
134 printf(" Image MD5: ");
135 for (i = 7; i < 23; i++)
136 printf("%2.2x", buf[i]);
137 printf(" (%s)\n", base64_dump(&buf[7], 16));
138 printf(" Rsync block length %lu bytes\n", blocklen);
139 return 0; /* i.e. we're finished! */
146 fileLen = (UINT64)buf[1];
147 fileLen |= (UINT64)buf[2] << 8;
148 fileLen |= (UINT64)buf[3] << 16;
149 fileLen |= (UINT64)buf[4] << 24;
150 fileLen |= (UINT64)buf[5] << 32;
151 fileLen |= (UINT64)buf[6] << 40;
153 printf(" File, length %llu bytes\n", fileLen);
154 printf(" file rsyncsum: ");
155 for (i = 7; i < 15; i++)
156 printf("%2.2x", buf[i]);
157 printf(" (%s)\n", base64_dump(&buf[7], 8));
158 printf(" file md5: ");
159 for (i = 15; i < 31; i++)
160 printf("%2.2x", buf[i]);
161 printf(" (%s)\n", base64_dump(&buf[15], 16));
171 int main(int argc, char **argv)
173 char *filename = NULL;
175 unsigned char *buf = NULL;
178 e_state state = STARTING;
182 printf("No filename specified! Try again...\n");
188 fd = open(filename, O_RDONLY);
191 printf("Failed to open file %s, error %d!. Try again...\n", filename, errno);
195 buf = malloc(BUF_SIZE);
198 printf("Failed to malloc %d bytes. Abort!\n", BUF_SIZE);
202 /* Find the beginning of the data - read the first chunk, including the header */
203 while (STARTING == state)
205 INT64 start_offset = -1;
207 bytes = read(fd, buf, BUF_SIZE);
213 start_offset = find_string(buf, bytes, "DATA");
214 if (start_offset >= 0)
216 offset += start_offset;
223 while (DONE != state && ERROR != state)
225 INT64 start_offset = -1;
226 lseek(fd, offset, SEEK_SET);
227 bytes = read(fd, buf, BUF_SIZE);
233 if (IN_DATA == state)
235 if (!find_string(buf, bytes, "DATA"))
237 if (!find_string(buf, bytes, "DESC"))
243 start_offset = parse_data_block(offset, buf, bytes);
244 offset += start_offset;
247 start_offset = parse_desc_block(offset, buf, bytes);
248 offset += start_offset;
252 start_offset = parse_desc_data(offset, buf, bytes);
253 offset += start_offset;
254 if (0 == start_offset)