Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_fingerprint.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_fingerprint.h"
10
11 #include "dng_assertions.h"
12 #include "dng_flags.h"
13
14 /*****************************************************************************/
15
16 dng_fingerprint::dng_fingerprint ()
17 {
18
19 for (uint32 j = 0; j < kDNGFingerprintSize; j++)
20 {
21
22 data [j] = 0;
23
24 }
25
26 }
27
28 /*****************************************************************************/
29
30 dng_fingerprint::dng_fingerprint (const char *hex)
31 {
32
33 if (!hex || strlen (hex) != kDNGFingerprintSize * 2 || !FromUtf8HexString (hex))
34 {
35
36 Clear ();
37
38 }
39
40 }
41
42 /*****************************************************************************/
43
44 bool dng_fingerprint::IsNull () const
45 {
46
47 for (uint32 j = 0; j < kDNGFingerprintSize; j++)
48 {
49
50 if (data [j] != 0)
51 {
52
53 return false;
54
55 }
56
57 }
58
59 return true;
60
61 }
62
63 /*****************************************************************************/
64
65 bool dng_fingerprint::operator== (const dng_fingerprint &print) const
66 {
67
68 for (uint32 j = 0; j < kDNGFingerprintSize; j++)
69 {
70
71 if (data [j] != print.data [j])
72 {
73
74 return false;
75
76 }
77
78 }
79
80 return true;
81
82 }
83
84 /******************************************************************************/
85
86 bool dng_fingerprint::operator< (const dng_fingerprint &print) const
87 {
88
89 for (uint32 j = 0; j < kDNGFingerprintSize; j++)
90 {
91
92 if (data [j] != print.data [j])
93 {
94
95 return data [j] < print.data [j];
96
97 }
98
99 }
100
101 return false;
102
103 }
104
105 /******************************************************************************/
106
107 uint32 dng_fingerprint::Collapse32 () const
108 {
109
110 uint32 x = 0;
111
112 for (uint32 j = 0; j < 4; j++)
113 {
114
115 uint32 y = 0;
116
117 for (uint32 k = 0; k < 4; k++)
118 {
119
120 y = (y << 8) + (uint32) data [j * 4 + k];
121
122 }
123
124 x = x ^ y;
125
126 }
127
128 return x;
129
130 }
131
132 /******************************************************************************/
133
134 static char NumToHexChar (unsigned int c)
135 {
136
137 if (c < 10)
138 {
139 return (char) ('0' + c);
140 }
141
142 else
143 {
144 return (char) ('A' + c - 10);
145 }
146
147 }
148
149 /*****************************************************************************/
150
151 void dng_fingerprint::ToUtf8HexString (char resultStr [2 * kDNGFingerprintSize + 1]) const
152 {
153
154 for (size_t i = 0; i < kDNGFingerprintSize; i++)
155 {
156
157 unsigned char c = data [i];
158
159 resultStr [i * 2 ] = NumToHexChar (c >> 4);
160 resultStr [i * 2 + 1] = NumToHexChar (c & 15);
161
162 }
163
164 resultStr [kDNGFingerprintSize * 2] = '\0';
165
166 }
167
168 /******************************************************************************/
169
170 dng_string dng_fingerprint::ToUtf8HexString () const
171 {
172
173 char buf [2 * kDNGFingerprintSize + 1];
174
175 ToUtf8HexString (buf);
176
177 return dng_string (buf);
178
179 }
180
181 /******************************************************************************/
182
183 static int HexCharToNum (char hexChar)
184 {
185
186 if (hexChar >= '0' && hexChar <= '9')
187 {
188 return hexChar - '0';
189 }
190
191 else if (hexChar >= 'A' && hexChar <= 'F')
192 {
193 return hexChar - 'A' + 10;
194 }
195
196 else if (hexChar >= 'a' && hexChar <= 'f')
197 {
198 return hexChar - 'a' + 10;
199 }
200
201 return -1;
202
203 }
204
205 /*****************************************************************************/
206
207 bool dng_fingerprint::FromUtf8HexString (const char inputStr [2 * kDNGFingerprintSize + 1])
208 {
209
210 for (size_t i = 0; i < kDNGFingerprintSize; i++)
211 {
212
213 int highNibble = HexCharToNum (inputStr [i * 2]);
214
215 if (highNibble < 0)
216 {
217 return false;
218 }
219
220 int lowNibble = HexCharToNum (inputStr [i * 2 + 1]);
221
222 if (lowNibble < 0)
223 {
224 return false;
225 }
226
227 data [i] = (uint8) ((highNibble << 4) + lowNibble);
228
229 }
230
231 return true;
232
233 }
234
235 /******************************************************************************/
236
237 bool dng_fingerprint::FromUtf8HexString (const dng_string &inputStr)
238 {
239
240 if (inputStr.Length () < kDNGFingerprintSize)
241 {
242 return false;
243 }
244
245 return FromUtf8HexString (inputStr.Get ());
246
247 }
248
249 /******************************************************************************/
250
251 // Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm
252
253 // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
254 // rights reserved.
255 //
256 // License to copy and use this software is granted provided that it
257 // is identified as the "RSA Data Security, Inc. MD5 Message-Digest
258 // Algorithm" in all material mentioning or referencing this software
259 // or this function.
260 //
261 // License is also granted to make and use derivative works provided
262 // that such works are identified as "derived from the RSA Data
263 // Security, Inc. MD5 Message-Digest Algorithm" in all material
264 // mentioning or referencing the derived work.
265 //
266 // RSA Data Security, Inc. makes no representations concerning either
267 // the merchantability of this software or the suitability of this
268 // software for any particular purpose. It is provided "as is"
269 // without express or implied warranty of any kind.
270 //
271 // These notices must be retained in any copies of any part of this
272 // documentation and/or software.
273
274 /******************************************************************************/
275
276 dng_md5_printer::dng_md5_printer ()
277
278 : final (false)
279 , result ()
280
281 {
282
283 Reset ();
284
285 }
286
287 /******************************************************************************/
288
289 void dng_md5_printer::Reset ()
290 {
291
292 // No bits processed yet.
293
294 count [0] = 0;
295 count [1] = 0;
296
297 // Load magic initialization constants.
298
299 state [0] = 0x67452301;
300 state [1] = 0xefcdab89;
301 state [2] = 0x98badcfe;
302 state [3] = 0x10325476;
303
304 // Not finalized yet.
305
306 final = false;
307
308 }
309
310 /******************************************************************************/
311
312 void dng_md5_printer::Process (const void *data,
313 uint32 inputLen)
314 {
315
316 DNG_ASSERT (!final, "Fingerprint already finalized!");
317
318 const uint8 *input = (const uint8 *) data;
319
320 // Compute number of bytes mod 64
321
322 uint32 index = (count [0] >> 3) & 0x3F;
323
324 // Update number of bits
325
326 if ((count [0] += inputLen << 3) < (inputLen << 3))
327 {
328 count [1]++;
329 }
330
331 count [1] += inputLen >> 29;
332
333 // Transform as many times as possible.
334
335 uint32 i = 0;
336
337 uint32 partLen = 64 - index;
338
339 if (inputLen >= partLen)
340 {
341
342 memcpy (&buffer [index],
343 input,
344 partLen);
345
346 MD5Transform (state, buffer);
347
348 for (i = partLen; i + 63 < inputLen; i += 64)
349 {
350
351 MD5Transform (state, &input [i]);
352
353 }
354
355 index = 0;
356
357 }
358
359 // Buffer remaining input
360
361 memcpy (&buffer [index],
362 &input [i],
363 inputLen - i);
364
365 }
366
367 /******************************************************************************/
368
369 const dng_fingerprint & dng_md5_printer::Result ()
370 {
371
372 if (!final)
373 {
374
375 static uint8 PADDING [64] =
376 {
377 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
378 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
380 };
381
382 // Save number of bits
383
384 uint8 bits [8];
385
386 Encode (bits, count, 8);
387
388 // Pad out to 56 mod 64.
389
390 uint32 index = (count [0] >> 3) & 0x3f;
391
392 uint32 padLen = (index < 56) ? (56 - index) : (120 - index);
393
394 Process (PADDING, padLen);
395
396 // Append length (before padding)
397
398 Process (bits, 8);
399
400 // Store state in digest
401
402 Encode (result.data, state, 16);
403
404 // We are now finalized.
405
406 final = true;
407
408 }
409
410 return result;
411
412 }
413
414 /******************************************************************************/
415
416 // Encodes input (uint32) into output (uint8). Assumes len is
417 // a multiple of 4.
418
419 void dng_md5_printer::Encode (uint8 *output,
420 const uint32 *input,
421 uint32 len)
422 {
423
424 uint32 i, j;
425
426 for (i = 0, j = 0; j < len; i++, j += 4)
427 {
428 output [j ] = (uint8) ((input [i] ) & 0xff);
429 output [j+1] = (uint8) ((input [i] >> 8) & 0xff);
430 output [j+2] = (uint8) ((input [i] >> 16) & 0xff);
431 output [j+3] = (uint8) ((input [i] >> 24) & 0xff);
432 }
433
434 }
435
436 /******************************************************************************/
437
438 // Decodes input (uint8) into output (uint32). Assumes len is
439 // a multiple of 4.
440
441 void dng_md5_printer::Decode (uint32 *output,
442 const uint8 *input,
443 uint32 len)
444 {
445
446 // Check for non-aligned case.
447
448 if (((uintptr) input) & 3)
449 {
450
451 uint32 i, j;
452
453 for (i = 0, j = 0; j < len; i++, j += 4)
454 {
455
456 output [i] = (((uint32) input [j ]) ) |
457 (((uint32) input [j+1]) << 8) |
458 (((uint32) input [j+2]) << 16) |
459 (((uint32) input [j+3]) << 24);
460
461 }
462
463 }
464
465 // Else use optimized code for aligned case.
466
467 else
468 {
469
470 len = len >> 2;
471
472 const uint32 *sPtr = (const uint32 *) input;
473
474 uint32 *dPtr = output;
475
476 while (len--)
477 {
478
479 #if qDNGBigEndian
480
481 uint32 data = *(sPtr++);
482
483 data = (data >> 24) |
484 ((data >> 8) & 0x0000FF00) |
485 ((data << 8) & 0x00FF0000) |
486 (data << 24);
487
488 *(dPtr++) = data;
489
490 #else
491
492 *(dPtr++) = *(sPtr++);
493
494 #endif
495
496 }
497
498 }
499
500 }
501
502 /******************************************************************************/
503
504 // MD5 basic transformation. Transforms state based on block.
505
506 DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow")
507 void dng_md5_printer::MD5Transform (uint32 state [4],
508 const uint8 block [64])
509 {
510
511 enum
512 {
513 S11 = 7,
514 S12 = 12,
515 S13 = 17,
516 S14 = 22,
517 S21 = 5,
518 S22 = 9,
519 S23 = 14,
520 S24 = 20,
521 S31 = 4,
522 S32 = 11,
523 S33 = 16,
524 S34 = 23,
525 S41 = 6,
526 S42 = 10,
527 S43 = 15,
528 S44 = 21
529 };
530
531 #if qDNGBigEndian
532
533 uint32 x [16];
534
535 Decode (x, block, 64);
536
537 #else
538
539 uint32 temp [16];
540
541 const uint32 *x;
542
543 if (((uintptr) block) & 3)
544 {
545
546 Decode (temp, block, 64);
547
548 x = temp;
549
550 }
551
552 else
553 x = (const uint32 *) block;
554
555 #endif
556
557 uint32 a = state [0];
558 uint32 b = state [1];
559 uint32 c = state [2];
560 uint32 d = state [3];
561
562 /* Round 1 */
563 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
564 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
565 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
566 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
567 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
568 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
569 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
570 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
571 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
572 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
573 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
574 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
575 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
576 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
577 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
578 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
579
580 /* Round 2 */
581 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
582 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
583 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
584 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
585 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
586 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
587 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
588 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
589 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
590 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
591 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
592 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
593 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
594 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
595 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
596 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
597
598 /* Round 3 */
599 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
600 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
601 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
602 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
603 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
604 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
605 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
606 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
607 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
608 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
609 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
610 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
611 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
612 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
613 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
614 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
615
616 /* Round 4 */
617 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
618 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
619 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
620 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
621 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
622 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
623 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
624 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
625 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
626 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
627 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
628 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
629 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
630 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
631 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
632 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
633
634 state [0] += a;
635 state [1] += b;
636 state [2] += c;
637 state [3] += d;
638
639 }
640
641 /*****************************************************************************/
642
643 // End of RSA Data Security, Inc. derived code.
644
645 /*****************************************************************************/