summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/irivertools
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/irivertools')
-rw-r--r--rbutil/rbutilqt/irivertools/checksums.h41
-rw-r--r--rbutil/rbutilqt/irivertools/irivertools.cpp527
-rw-r--r--rbutil/rbutilqt/irivertools/irivertools.h55
-rw-r--r--rbutil/rbutilqt/irivertools/md5sum.cpp295
-rw-r--r--rbutil/rbutilqt/irivertools/md5sum.h52
5 files changed, 0 insertions, 970 deletions
diff --git a/rbutil/rbutilqt/irivertools/checksums.h b/rbutil/rbutilqt/irivertools/checksums.h
deleted file mode 100644
index 6b66455f1f..0000000000
--- a/rbutil/rbutilqt/irivertools/checksums.h
+++ /dev/null
@@ -1,41 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: irivertools.h
10 *
11 * Copyright (C) 2007 Dominik Wenger
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22
23#ifndef CHECKSUMS_H
24#define CHECKSUMS_H
25
26/* precalculated checksums for H110/H115 */
27static struct sumpairs h100pairs[] = {
28#include "h100sums.h"
29};
30
31/* precalculated checksums for H120/H140 */
32static struct sumpairs h120pairs[] = {
33#include "h120sums.h"
34};
35
36/* precalculated checksums for H320/H340 */
37static struct sumpairs h300pairs[] = {
38#include "h300sums.h"
39};
40
41#endif
diff --git a/rbutil/rbutilqt/irivertools/irivertools.cpp b/rbutil/rbutilqt/irivertools/irivertools.cpp
deleted file mode 100644
index 2bcd9ffb80..0000000000
--- a/rbutil/rbutilqt/irivertools/irivertools.cpp
+++ /dev/null
@@ -1,527 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: irivertools.cpp
10 *
11 * Copyright (C) 2007 Dominik Wenger
12 *
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include <QtCore>
22#include "irivertools.h"
23
24const unsigned char munge[] = {
25 0x7a, 0x36, 0xc4, 0x43, 0x49, 0x6b, 0x35, 0x4e, 0xa3, 0x46, 0x25, 0x84,
26 0x4d, 0x73, 0x74, 0x61
27};
28
29const unsigned char header_modify[] = "* IHPFIRM-DECODED ";
30
31const char * const models[] = { "iHP-100", "iHP-120/iHP-140", "H300 series",
32 NULL };
33
34/* aligns with models array; expected min firmware size */
35const unsigned int firmware_minsize[] = { 0x100000, 0x100000, 0x200000 };
36/* aligns with models array; expected max firmware size */
37const unsigned int firmware_maxsize[] = { 0x200000, 0x200000, 0x400000 };
38
39const unsigned char header[][16] = {
40 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
41 { 0x20, 0x03, 0x08, 0x27, 0x24, 0x00, 0x02, 0x30, 0x19, 0x17, 0x65, 0x73,
42 0x85, 0x32, 0x83, 0x22 },
43 { 0x20, 0x04, 0x03, 0x27, 0x20, 0x50, 0x01, 0x70, 0x80, 0x30, 0x80, 0x06,
44 0x30, 0x19, 0x17, 0x65 }
45};
46
47/* begin mkboot.c excerpt */
48unsigned char image[0x400000 + 0x220 + 0x400000/0x200];
49
50int mkboot(QString infile, QString outfile, QString bootloader, int origin)
51{
52 int i;
53 int len,bllen;
54 int actual_length, total_length, binary_length, num_chksums;
55
56 memset(image, 0xff, sizeof(image));
57
58 /* First, read the iriver original firmware into the image */
59 QFile f(infile);
60 if(!f.open(QIODevice::ReadOnly))
61 {
62 // can't open input file
63 return -1;
64 }
65 i = f.read((char*)image,16);
66 if(i < 16) {
67 // reading header failed
68 return -2;
69 }
70
71 /* This is the length of the binary image without the scrambling
72 overhead (but including the ESTFBINR header) */
73 binary_length = image[4] + (image[5] << 8) +
74 (image[6] << 16) + (image[7] << 24);
75
76 /* Read the rest of the binary data, but not the checksum block */
77 len = binary_length+0x200-16;
78 i = f.read((char*)image+16, len);
79 if(i < len) {
80 // reading firmware failed
81 return -3;
82 }
83
84 f.close();
85 /* Now, read the boot loader into the image */
86 f.setFileName(bootloader);
87 if(!f.open(QIODevice::ReadOnly))
88 {
89 // can't open bootloader file
90 return -4;
91 }
92
93 bllen = f.size();
94
95 i = f.read((char*)image+0x220 + origin, bllen);
96 if(i < bllen) {
97 // reading bootloader file failed
98 return -5;
99 }
100
101 f.close();
102 f.setFileName(outfile);
103 if(!f.open(QIODevice::WriteOnly))
104 {
105 // can't open output file
106 return -6;
107 }
108
109 /* Patch the reset vector to start the boot loader */
110 image[0x220 + 4] = image[origin + 0x220 + 4];
111 image[0x220 + 5] = image[origin + 0x220 + 5];
112 image[0x220 + 6] = image[origin + 0x220 + 6];
113 image[0x220 + 7] = image[origin + 0x220 + 7];
114
115 /* This is the actual length of the binary, excluding all headers */
116 actual_length = origin + bllen;
117
118 /* Patch the ESTFBINR header */
119 image[0x20c] = (actual_length >> 24) & 0xff;
120 image[0x20d] = (actual_length >> 16) & 0xff;
121 image[0x20e] = (actual_length >> 8) & 0xff;
122 image[0x20f] = actual_length & 0xff;
123
124 image[0x21c] = (actual_length >> 24) & 0xff;
125 image[0x21d] = (actual_length >> 16) & 0xff;
126 image[0x21e] = (actual_length >> 8) & 0xff;
127 image[0x21f] = actual_length & 0xff;
128
129 /* This is the length of the binary, including the ESTFBINR header and
130 rounded up to the nearest 0x200 boundary */
131 binary_length = (actual_length + 0x20 + 0x1ff) & 0xfffffe00;
132
133 /* The number of checksums, i.e number of 0x200 byte blocks */
134 num_chksums = binary_length / 0x200;
135
136 /* The total file length, including all headers and checksums */
137 total_length = binary_length + num_chksums + 0x200;
138
139 /* Patch the scrambler header with the new length info */
140 image[0] = total_length & 0xff;
141 image[1] = (total_length >> 8) & 0xff;
142 image[2] = (total_length >> 16) & 0xff;
143 image[3] = (total_length >> 24) & 0xff;
144
145 image[4] = binary_length & 0xff;
146 image[5] = (binary_length >> 8) & 0xff;
147 image[6] = (binary_length >> 16) & 0xff;
148 image[7] = (binary_length >> 24) & 0xff;
149
150 image[8] = num_chksums & 0xff;
151 image[9] = (num_chksums >> 8) & 0xff;
152 image[10] = (num_chksums >> 16) & 0xff;
153 image[11] = (num_chksums >> 24) & 0xff;
154
155 i = f.write((char*)image,total_length);
156 if(i < total_length) {
157 // writing bootloader file failed
158 return -7;
159 }
160
161 f.close();
162
163 return 0;
164}
165
166/* end mkboot.c excerpt */
167
168
169int intable(char *md5, struct sumpairs *table, int len)
170{
171 int i;
172 for (i = 0; i < len; i++) {
173 if (strncmp(md5, table[i].unpatched, 32) == 0) {
174 return i;
175 }
176 }
177 return -1;
178}
179
180
181
182
183static int testheader( const unsigned char * const data )
184{
185 const unsigned char * const d = data+16;
186 const char * const * m = models;
187 int index = 0;
188 while( *m )
189 {
190 if( memcmp( header[ index ], d, 16 ) == 0 )
191 return index;
192 index++;
193 m++;
194 };
195 return -1;
196};
197
198static void modifyheader( unsigned char * data )
199{
200 const unsigned char * h = header_modify;
201 int i;
202 for( i=0; i<512; i++ )
203 {
204 if( *h == '\0' )
205 h = header_modify;
206 *data++ ^= *h++;
207 };
208};
209
210int iriver_decode(QString infile_name, QString outfile_name, unsigned int modify,
211 enum striptype stripmode)
212{
213 QFile infile(infile_name);
214 QFile outfile(outfile_name);
215 int i = -1;
216 unsigned char headerdata[512];
217 unsigned long dwLength1, dwLength2, dwLength3, fp = 0;
218 unsigned char blockdata[16+16];
219 unsigned char out[16];
220 unsigned char newmunge;
221 signed long lenread;
222 int s = 0;
223 unsigned char * pChecksums, * ppChecksums = 0;
224 unsigned char ck;
225
226
227 if(!infile.open(QIODevice::ReadOnly))
228 {
229 // can't open input file
230 return -1;
231 }
232 if(!outfile.open(QIODevice::WriteOnly))
233 {
234 // can't open output file
235 return -2;
236 }
237 lenread = infile.read( (char*)headerdata, 512);
238 if( lenread != 512 )
239 {
240 // header length doesn't match
241 infile.close();
242 outfile.close();
243 return -3;
244 };
245
246 i = testheader( headerdata );
247 if( i == -1 )
248 {
249 // header unknown
250 infile.close();
251 outfile.close();
252 return -4;
253 };
254 fprintf( stderr, "Model %s\n", models[ i ] );
255
256 dwLength1 = headerdata[0] | (headerdata[1]<<8) |
257 (headerdata[2]<<16) | (headerdata[3]<<24);
258 dwLength2 = headerdata[4] | (headerdata[5]<<8) |
259 (headerdata[6]<<16) | (headerdata[7]<<24);
260 dwLength3 = headerdata[8] | (headerdata[9]<<8) |
261 (headerdata[10]<<16) | (headerdata[11]<<24);
262
263 if( dwLength1 < firmware_minsize[ i ] ||
264 dwLength1 > firmware_maxsize[ i ] ||
265 dwLength2 < firmware_minsize[ i ] ||
266 dwLength2 > dwLength1 ||
267 dwLength3 > dwLength1 ||
268 dwLength2>>9 != dwLength3 ||
269 dwLength2+dwLength3+512 != dwLength1 )
270 {
271 // file 'length' data is wrong
272 infile.close();
273 outfile.close();
274 return -5;
275 };
276
277 pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) );
278
279 if( modify )
280 {
281 modifyheader( headerdata );
282 };
283
284 if( stripmode == STRIP_NONE )
285 outfile.write( (char*)headerdata, 512);
286
287 memset( blockdata, 0, 16 );
288
289 ck = 0;
290 while( ( fp < dwLength2 ) &&
291 ( lenread = infile.read( (char*)blockdata+16, 16) == 16) )
292 {
293 fp += 16;
294
295 for( i=0; i<16; ++i )
296 {
297 newmunge = blockdata[16+i] ^ munge[i];
298 out[i] = newmunge ^ blockdata[i];
299 blockdata[i] = newmunge;
300 ck += out[i];
301 }
302
303 if( fp > ESTF_SIZE || stripmode != STRIP_HEADER_CHECKSUM_ESTF )
304 {
305 outfile.write( (char*)out+4, 12);
306 outfile.write( (char*)out, 4);
307 }
308 else
309 {
310 if( ESTF_SIZE - fp < 16 )
311 {
312 memcpy( out+4, blockdata+16, 12 );
313 memcpy( out, blockdata+28, 4 );
314 outfile.write((char*) blockdata+16+ESTF_SIZE-fp, ESTF_SIZE-fp);
315 }
316 }
317
318
319 if( s == 496 )
320 {
321 s = 0;
322 memset( blockdata, 0, 16 );
323 *ppChecksums++ = ck;
324 ck = 0;
325 }
326 else
327 s+=16;
328 };
329
330 if( fp != dwLength2 )
331 {
332 // 'length2' field mismatch
333 infile.close();
334 outfile.close();
335 return -6;
336 };
337
338 fp = 0;
339 ppChecksums = pChecksums;
340 while( ( fp < dwLength3 ) &&
341 ( lenread = infile.read((char*) blockdata, 32 ) ) > 0 )
342 {
343 fp += lenread;
344 if( stripmode == STRIP_NONE )
345 outfile.write((char*) blockdata, lenread );
346 if( memcmp( ppChecksums, blockdata, lenread ) != 0 )
347 {
348 // file checksum wrong
349 infile.close();
350 outfile.close();
351 return -7;
352 };
353 ppChecksums += lenread;
354 };
355
356 if( fp != dwLength3 )
357 {
358 // 'length3' field mismatch
359 infile.close();
360 outfile.close();
361 return -8;
362 };
363
364
365 fprintf( stderr, "File decoded correctly and all checksums matched!\n" );
366 switch( stripmode )
367 {
368 default:
369 case STRIP_NONE:
370 fprintf(stderr, "Output file contains all headers and "
371 "checksums\n");
372 break;
373 case STRIP_HEADER_CHECKSUM:
374 fprintf( stderr, "NB: output file contains only ESTFBINR header"
375 " and decoded firmware code\n" );
376 break;
377 case STRIP_HEADER_CHECKSUM_ESTF:
378 fprintf( stderr, "NB: output file contains only raw decoded "
379 "firmware code\n" );
380 break;
381 };
382
383 infile.close();
384 outfile.close();
385 return 0;
386
387};
388
389int iriver_encode(QString infile_name, QString outfile_name, unsigned int modify)
390{
391 QFile infile(infile_name);
392 QFile outfile(outfile_name);
393 int i = -1;
394 unsigned char headerdata[512];
395 unsigned long dwLength1, dwLength2, dwLength3, fp = 0;
396 unsigned char blockdata[16+16];
397 unsigned char out[16];
398 unsigned char newmunge;
399 signed long lenread;
400 int s = 0;
401 unsigned char * pChecksums, * ppChecksums;
402 unsigned char ck;
403
404 if(!infile.open(QIODevice::ReadOnly))
405 {
406 // can't open input file
407 return -1;
408 }
409 if(!outfile.open(QIODevice::WriteOnly))
410 {
411 // can't open output file
412 infile.close();
413 return -2;
414 }
415
416 lenread = infile.read((char*) headerdata, 512 );
417 if( lenread != 512 )
418 {
419 // header length error
420 infile.close();
421 outfile.close();
422 return -3;
423 };
424
425 if( modify )
426 {
427 modifyheader( headerdata ); /* reversible */
428 };
429
430 i = testheader( headerdata );
431 if( i == -1 )
432 {
433 // header verification error
434 infile.close();
435 outfile.close();
436 return -4;
437 };
438 fprintf( stderr, "Model %s\n", models[ i ] );
439
440 dwLength1 = headerdata[0] | (headerdata[1]<<8) |
441 (headerdata[2]<<16) | (headerdata[3]<<24);
442 dwLength2 = headerdata[4] | (headerdata[5]<<8) |
443 (headerdata[6]<<16) | (headerdata[7]<<24);
444 dwLength3 = headerdata[8] | (headerdata[9]<<8) |
445 (headerdata[10]<<16) | (headerdata[11]<<24);
446
447 if( dwLength1 < firmware_minsize[i] ||
448 dwLength1 > firmware_maxsize[i] ||
449 dwLength2 < firmware_minsize[i] ||
450 dwLength2 > dwLength1 ||
451 dwLength3 > dwLength1 ||
452 dwLength2+dwLength3+512 != dwLength1 )
453 {
454 // file 'length' error
455 infile.close();
456 outfile.close();
457 return -5;
458 };
459
460 pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) );
461
462 outfile.write( (char*)headerdata, 512);
463
464 memset( blockdata, 0, 16 );
465 ck = 0;
466 while( ( fp < dwLength2 ) &&
467 ( lenread = infile.read((char*) blockdata+16, 16) ) == 16 )
468 {
469 fp += 16;
470 for( i=0; i<16; ++i )
471 {
472 newmunge = blockdata[16+((12+i)&0xf)] ^ blockdata[i];
473 out[i] = newmunge ^ munge[i];
474 ck += blockdata[16+i];
475 blockdata[i] = newmunge;
476 };
477 outfile.write( (char*)out, 16);
478
479 if( s == 496 )
480 {
481 s = 0;
482 memset( blockdata, 0, 16 );
483 *ppChecksums++ = ck;
484 ck = 0;
485 }
486 else
487 s+=16;
488 };
489
490 if( fp != dwLength2 )
491 {
492 // file 'length1' mismatch
493 infile.close();
494 outfile.close();
495 return -6;
496 };
497
498 /* write out remainder w/out applying descrambler */
499 fp = 0;
500 lenread = dwLength3;
501 ppChecksums = pChecksums;
502 while( ( fp < dwLength3) &&
503 ( lenread = outfile.write((char*) ppChecksums, lenread) ) > 0 )
504 {
505 fp += lenread;
506 ppChecksums += lenread;
507 lenread = dwLength3 - fp;
508 };
509
510 if( fp != dwLength3 )
511 {
512 // 'length2' field mismatch
513 infile.close();
514 outfile.close();
515 return -8;
516 };
517
518 fprintf( stderr, "File encoded successfully and checksum table built!\n" );
519
520 infile.close();
521 outfile.close();
522 return 0;
523
524};
525
526
527
diff --git a/rbutil/rbutilqt/irivertools/irivertools.h b/rbutil/rbutilqt/irivertools/irivertools.h
deleted file mode 100644
index 6d61300d79..0000000000
--- a/rbutil/rbutilqt/irivertools/irivertools.h
+++ /dev/null
@@ -1,55 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: irivertools.h
10 *
11 * Copyright (C) 2007 Dominik Wenger
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22
23
24#ifndef IRIVERTOOLS_H_INCLUDED
25#define IRIVERTOOLS_H_INCLUDED
26
27#include <QtCore>
28
29#include "md5sum.h"
30
31#define ESTF_SIZE 32
32
33struct sumpairs {
34 const char *unpatched;
35 const char *patched;
36};
37
38
39enum striptype
40{
41 STRIP_NONE,
42 STRIP_HEADER_CHECKSUM,
43 STRIP_HEADER_CHECKSUM_ESTF
44};
45
46/* protos for iriver.c */
47
48int intable(char *md5, struct sumpairs *table, int len);
49
50int mkboot(QString infile, QString outfile,QString bootloader,int origin);
51int iriver_decode(QString infile_name, QString outfile_name, unsigned int modify,
52 enum striptype stripmode);
53int iriver_encode(QString infile_name, QString outfile_name, unsigned int modify);
54
55#endif // IRIVERTOOLS_H_INCLUDED
diff --git a/rbutil/rbutilqt/irivertools/md5sum.cpp b/rbutil/rbutilqt/irivertools/md5sum.cpp
deleted file mode 100644
index f4d25e67ef..0000000000
--- a/rbutil/rbutilqt/irivertools/md5sum.cpp
+++ /dev/null
@@ -1,295 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: md5sum.cpp
10 *
11 * Copyright (C) 2007 Dominik Wenger
12 *
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21
22/*
23 * RFC 1321 compliant MD5 implementation
24 *
25 * Copyright (C) 2001-2003 Christophe Devine
26 *
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 */
41
42
43#include "md5sum.h"
44
45
46#define GET_UINT32(n,b,i) \
47{ \
48 (n) = ( (uint32) (b)[(i) ] ) \
49 | ( (uint32) (b)[(i) + 1] << 8 ) \
50 | ( (uint32) (b)[(i) + 2] << 16 ) \
51 | ( (uint32) (b)[(i) + 3] << 24 ); \
52}
53
54#define PUT_UINT32(n,b,i) \
55{ \
56 (b)[(i) ] = (uint8) ( (n) ); \
57 (b)[(i) + 1] = (uint8) ( (n) >> 8 ); \
58 (b)[(i) + 2] = (uint8) ( (n) >> 16 ); \
59 (b)[(i) + 3] = (uint8) ( (n) >> 24 ); \
60}
61
62void md5_starts( md5_context *ctx )
63{
64 ctx->total[0] = 0;
65 ctx->total[1] = 0;
66
67 ctx->state[0] = 0x67452301;
68 ctx->state[1] = 0xEFCDAB89;
69 ctx->state[2] = 0x98BADCFE;
70 ctx->state[3] = 0x10325476;
71}
72
73void md5_process( md5_context *ctx, uint8 data[64] )
74{
75 uint32 X[16], A, B, C, D;
76
77 GET_UINT32( X[0], data, 0 );
78 GET_UINT32( X[1], data, 4 );
79 GET_UINT32( X[2], data, 8 );
80 GET_UINT32( X[3], data, 12 );
81 GET_UINT32( X[4], data, 16 );
82 GET_UINT32( X[5], data, 20 );
83 GET_UINT32( X[6], data, 24 );
84 GET_UINT32( X[7], data, 28 );
85 GET_UINT32( X[8], data, 32 );
86 GET_UINT32( X[9], data, 36 );
87 GET_UINT32( X[10], data, 40 );
88 GET_UINT32( X[11], data, 44 );
89 GET_UINT32( X[12], data, 48 );
90 GET_UINT32( X[13], data, 52 );
91 GET_UINT32( X[14], data, 56 );
92 GET_UINT32( X[15], data, 60 );
93
94#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
95
96#define P(a,b,c,d,k,s,t) \
97{ \
98 a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
99}
100
101 A = ctx->state[0];
102 B = ctx->state[1];
103 C = ctx->state[2];
104 D = ctx->state[3];
105
106#define F(x,y,z) (z ^ (x & (y ^ z)))
107
108 P( A, B, C, D, 0, 7, 0xD76AA478 );
109 P( D, A, B, C, 1, 12, 0xE8C7B756 );
110 P( C, D, A, B, 2, 17, 0x242070DB );
111 P( B, C, D, A, 3, 22, 0xC1BDCEEE );
112 P( A, B, C, D, 4, 7, 0xF57C0FAF );
113 P( D, A, B, C, 5, 12, 0x4787C62A );
114 P( C, D, A, B, 6, 17, 0xA8304613 );
115 P( B, C, D, A, 7, 22, 0xFD469501 );
116 P( A, B, C, D, 8, 7, 0x698098D8 );
117 P( D, A, B, C, 9, 12, 0x8B44F7AF );
118 P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
119 P( B, C, D, A, 11, 22, 0x895CD7BE );
120 P( A, B, C, D, 12, 7, 0x6B901122 );
121 P( D, A, B, C, 13, 12, 0xFD987193 );
122 P( C, D, A, B, 14, 17, 0xA679438E );
123 P( B, C, D, A, 15, 22, 0x49B40821 );
124
125#undef F
126
127#define F(x,y,z) (y ^ (z & (x ^ y)))
128
129 P( A, B, C, D, 1, 5, 0xF61E2562 );
130 P( D, A, B, C, 6, 9, 0xC040B340 );
131 P( C, D, A, B, 11, 14, 0x265E5A51 );
132 P( B, C, D, A, 0, 20, 0xE9B6C7AA );
133 P( A, B, C, D, 5, 5, 0xD62F105D );
134 P( D, A, B, C, 10, 9, 0x02441453 );
135 P( C, D, A, B, 15, 14, 0xD8A1E681 );
136 P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
137 P( A, B, C, D, 9, 5, 0x21E1CDE6 );
138 P( D, A, B, C, 14, 9, 0xC33707D6 );
139 P( C, D, A, B, 3, 14, 0xF4D50D87 );
140 P( B, C, D, A, 8, 20, 0x455A14ED );
141 P( A, B, C, D, 13, 5, 0xA9E3E905 );
142 P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
143 P( C, D, A, B, 7, 14, 0x676F02D9 );
144 P( B, C, D, A, 12, 20, 0x8D2A4C8A );
145
146#undef F
147
148#define F(x,y,z) (x ^ y ^ z)
149
150 P( A, B, C, D, 5, 4, 0xFFFA3942 );
151 P( D, A, B, C, 8, 11, 0x8771F681 );
152 P( C, D, A, B, 11, 16, 0x6D9D6122 );
153 P( B, C, D, A, 14, 23, 0xFDE5380C );
154 P( A, B, C, D, 1, 4, 0xA4BEEA44 );
155 P( D, A, B, C, 4, 11, 0x4BDECFA9 );
156 P( C, D, A, B, 7, 16, 0xF6BB4B60 );
157 P( B, C, D, A, 10, 23, 0xBEBFBC70 );
158 P( A, B, C, D, 13, 4, 0x289B7EC6 );
159 P( D, A, B, C, 0, 11, 0xEAA127FA );
160 P( C, D, A, B, 3, 16, 0xD4EF3085 );
161 P( B, C, D, A, 6, 23, 0x04881D05 );
162 P( A, B, C, D, 9, 4, 0xD9D4D039 );
163 P( D, A, B, C, 12, 11, 0xE6DB99E5 );
164 P( C, D, A, B, 15, 16, 0x1FA27CF8 );
165 P( B, C, D, A, 2, 23, 0xC4AC5665 );
166
167#undef F
168
169#define F(x,y,z) (y ^ (x | ~z))
170
171 P( A, B, C, D, 0, 6, 0xF4292244 );
172 P( D, A, B, C, 7, 10, 0x432AFF97 );
173 P( C, D, A, B, 14, 15, 0xAB9423A7 );
174 P( B, C, D, A, 5, 21, 0xFC93A039 );
175 P( A, B, C, D, 12, 6, 0x655B59C3 );
176 P( D, A, B, C, 3, 10, 0x8F0CCC92 );
177 P( C, D, A, B, 10, 15, 0xFFEFF47D );
178 P( B, C, D, A, 1, 21, 0x85845DD1 );
179 P( A, B, C, D, 8, 6, 0x6FA87E4F );
180 P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
181 P( C, D, A, B, 6, 15, 0xA3014314 );
182 P( B, C, D, A, 13, 21, 0x4E0811A1 );
183 P( A, B, C, D, 4, 6, 0xF7537E82 );
184 P( D, A, B, C, 11, 10, 0xBD3AF235 );
185 P( C, D, A, B, 2, 15, 0x2AD7D2BB );
186 P( B, C, D, A, 9, 21, 0xEB86D391 );
187
188#undef F
189#undef S
190#undef P
191
192
193 ctx->state[0] += A;
194 ctx->state[1] += B;
195 ctx->state[2] += C;
196 ctx->state[3] += D;
197}
198
199void md5_update( md5_context *ctx, uint8 *input, uint32 length )
200{
201 uint32 left, fill;
202
203 if( ! length ) return;
204
205 left = ctx->total[0] & 0x3F;
206 fill = 64 - left;
207
208 ctx->total[0] += length;
209 ctx->total[0] &= 0xFFFFFFFF;
210
211 if( ctx->total[0] < length )
212 ctx->total[1]++;
213
214 if( left && length >= fill )
215 {
216 memcpy( (void *) (ctx->buffer + left),
217 (void *) input, fill );
218 md5_process( ctx, ctx->buffer );
219 length -= fill;
220 input += fill;
221 left = 0;
222 }
223
224 while( length >= 64 )
225 {
226 md5_process( ctx, input );
227 length -= 64;
228 input += 64;
229 }
230
231 if( length )
232 {
233 memcpy( (void *) (ctx->buffer + left),
234 (void *) input, length );
235 }
236}
237
238static uint8 md5_padding[64] =
239{
240 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
241 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
242 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
243 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
244};
245
246void md5_finish( md5_context *ctx, uint8 digest[16] )
247{
248 uint32 last, padn;
249 uint32 high, low;
250 uint8 msglen[8];
251
252 high = ( ctx->total[0] >> 29 )
253 | ( ctx->total[1] << 3 );
254 low = ( ctx->total[0] << 3 );
255
256 PUT_UINT32( low, msglen, 0 );
257 PUT_UINT32( high, msglen, 4 );
258
259 last = ctx->total[0] & 0x3F;
260 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
261
262 md5_update( ctx, md5_padding, padn );
263 md5_update( ctx, msglen, 8 );
264
265 PUT_UINT32( ctx->state[0], digest, 0 );
266 PUT_UINT32( ctx->state[1], digest, 4 );
267 PUT_UINT32( ctx->state[2], digest, 8 );
268 PUT_UINT32( ctx->state[3], digest, 12 );
269}
270
271int FileMD5(QString name, char *md5)
272{
273 int i, read;
274 md5_context ctx;
275 unsigned char md5sum[16];
276 unsigned char block[32768];
277
278 QFile file(name);
279
280 if (!file.open(QIODevice::ReadOnly)) {
281 return 0;
282 }
283 md5_starts(&ctx);
284 while ( !file.atEnd() ) {
285 read = file.read((char*)block, sizeof(block));
286 md5_update(&ctx, block, read);
287 }
288 file.close();
289 md5_finish(&ctx, md5sum);
290 for (i = 0; i < 16; ++i)
291 {
292 sprintf(md5 + 2*i, "%02x", md5sum[i]);
293 }
294 return 1;
295}
diff --git a/rbutil/rbutilqt/irivertools/md5sum.h b/rbutil/rbutilqt/irivertools/md5sum.h
deleted file mode 100644
index 45cc734034..0000000000
--- a/rbutil/rbutilqt/irivertools/md5sum.h
+++ /dev/null
@@ -1,52 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: md5sum.h
10 *
11 * Copyright (C) 2007 Dominik Wenger
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22
23
24#ifndef MD5SUM_H_INCLUDED
25#define MD5SUM_H_INCLUDED
26
27#ifndef uint8
28#define uint8 unsigned char
29#endif
30
31
32#ifndef uint32
33#define uint32 unsigned long int
34#endif
35
36#include <QtCore>
37
38typedef struct
39{
40 uint32 total[2];
41 uint32 state[4];
42 uint8 buffer[64];
43}
44md5_context;
45
46void md5_starts( md5_context *ctx );
47void md5_update( md5_context *ctx, uint8 *input, uint32 length );
48void md5_finish( md5_context *ctx, uint8 digest[16] );
49
50int FileMD5(QString name, char *md5);
51
52#endif // MD5SUM_H_INCLUDED