MagickCore  7.0.7
Convert, Edit, Or Compose Bitmap Images
signature.c
Go to the documentation of this file.
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % SSSSS IIIII GGGG N N AAA TTTTT U U RRRR EEEEE %
6 % SS I G NN N A A T U U R R E %
7 % SSS I G GG N N N AAAAA T U U RRRR EEE %
8 % SS I G G N NN A A T U U R R E %
9 % SSSSS IIIII GGG N N A A T UUU R R EEEEE %
10 % %
11 % %
12 % MagickCore Methods to Compute a Message Digest for an Image %
13 % %
14 % Software Design %
15 % Cristy %
16 % December 1992 %
17 % %
18 % %
19 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
21 % %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
24 % %
25 % https://www.imagemagick.org/script/license.php %
26 % %
27 % Unless required by applicable law or agreed to in writing, software %
28 % distributed under the License is distributed on an "AS IS" BASIS, %
29 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30 % See the License for the specific language governing permissions and %
31 % limitations under the License. %
32 % %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %
35 %
36 %
37 */
38 
39 /*
40  Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/cache.h"
44 #include "MagickCore/exception.h"
46 #include "MagickCore/property.h"
47 #include "MagickCore/image.h"
48 #include "MagickCore/memory_.h"
51 #include "MagickCore/quantum.h"
53 #include "MagickCore/signature.h"
55 #include "MagickCore/string_.h"
56 /*
57  Define declarations.
58 */
59 #define SignatureBlocksize 64
60 #define SignatureDigestsize 32
61 
62 /*
63  Typedef declarations.
64 */
66 {
67  unsigned int
69  blocksize;
70 
73  *message;
74 
75  unsigned int
77  low_order,
78  high_order;
79 
80  size_t
82 
85 
86  ssize_t
88 
89  size_t
91 };
92 
93 /*
94  Forward declarations.
95 */
96 static void
98 
99 /*
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 % %
102 % %
103 % %
104 + A c q u i r e S i g n a t u r e I n f o %
105 % %
106 % %
107 % %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 %
110 % AcquireSignatureInfo() allocate the SignatureInfo structure.
111 %
112 % The format of the AcquireSignatureInfo method is:
113 %
114 % SignatureInfo *AcquireSignatureInfo(void)
115 %
116 */
118 {
120  *signature_info;
121 
122  unsigned long
123  lsb_first;
124 
125  signature_info=(SignatureInfo *) AcquireCriticalMemory(sizeof(*signature_info));
126  (void) ResetMagickMemory(signature_info,0,sizeof(*signature_info));
127  signature_info->digestsize=SignatureDigestsize;
128  signature_info->blocksize=SignatureBlocksize;
131  signature_info->accumulator=(unsigned int *) AcquireQuantumMemory(
132  SignatureBlocksize,sizeof(*signature_info->accumulator));
133  if (signature_info->accumulator == (unsigned int *) NULL)
134  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
135  lsb_first=1;
136  signature_info->lsb_first=(int) (*(char *) &lsb_first) == 1 ? MagickTrue :
137  MagickFalse;
138  signature_info->timestamp=(ssize_t) time((time_t *) NULL);
139  signature_info->signature=MagickCoreSignature;
140  InitializeSignature(signature_info);
141  return(signature_info);
142 }
143 
144 /*
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 % %
147 % %
148 % %
149 + D e s t r o y S i g n a t u r e I n f o %
150 % %
151 % %
152 % %
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 %
155 % DestroySignatureInfo() zeros memory associated with the SignatureInfo
156 % structure.
157 %
158 % The format of the DestroySignatureInfo method is:
159 %
160 % SignatureInfo *DestroySignatureInfo(SignatureInfo *signature_info)
161 %
162 % A description of each parameter follows:
163 %
164 % o signature_info: the cipher signature_info.
165 %
166 */
168 {
169  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
170  assert(signature_info != (SignatureInfo *) NULL);
171  assert(signature_info->signature == MagickCoreSignature);
172  if (signature_info->accumulator != (unsigned int *) NULL)
173  signature_info->accumulator=(unsigned int *) RelinquishMagickMemory(
174  signature_info->accumulator);
175  if (signature_info->message != (StringInfo *) NULL)
176  signature_info->message=DestroyStringInfo(signature_info->message);
177  if (signature_info->digest != (StringInfo *) NULL)
178  signature_info->digest=DestroyStringInfo(signature_info->digest);
179  signature_info->signature=(~MagickCoreSignature);
180  signature_info=(SignatureInfo *) RelinquishMagickMemory(signature_info);
181  return(signature_info);
182 }
183 
184 /*
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 % %
187 % %
188 % %
189 + F i n a l i z e S i g n a t u r e %
190 % %
191 % %
192 % %
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 %
195 % FinalizeSignature() finalizes the Signature message accumulator computation.
196 %
197 % The format of the FinalizeSignature method is:
198 %
199 % FinalizeSignature(SignatureInfo *signature_info)
200 %
201 % A description of each parameter follows:
202 %
203 % o signature_info: the address of a structure of type SignatureInfo.
204 %
205 */
207 {
208  register ssize_t
209  i;
210 
211  register unsigned char
212  *q;
213 
214  register unsigned int
215  *p;
216 
217  size_t
218  extent;
219 
220  unsigned char
221  *datum;
222 
223  unsigned int
224  high_order,
225  low_order;
226 
227  /*
228  Add padding and return the message accumulator.
229  */
230  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
231  assert(signature_info != (SignatureInfo *) NULL);
232  assert(signature_info->signature == MagickCoreSignature);
233  low_order=signature_info->low_order;
234  high_order=signature_info->high_order;
235  extent=((low_order >> 3) & 0x3f);
236  datum=GetStringInfoDatum(signature_info->message);
237  datum[extent++]=(unsigned char) 0x80;
238  if (extent <= (unsigned int) (GetStringInfoLength(signature_info->message)-8))
239  (void) ResetMagickMemory(datum+extent,0,GetStringInfoLength(
240  signature_info->message)-8-extent);
241  else
242  {
243  (void) ResetMagickMemory(datum+extent,0,GetStringInfoLength(
244  signature_info->message)-extent);
245  TransformSignature(signature_info);
246  (void) ResetMagickMemory(datum,0,GetStringInfoLength(
247  signature_info->message)-8);
248  }
249  datum[56]=(unsigned char) (high_order >> 24);
250  datum[57]=(unsigned char) (high_order >> 16);
251  datum[58]=(unsigned char) (high_order >> 8);
252  datum[59]=(unsigned char) high_order;
253  datum[60]=(unsigned char) (low_order >> 24);
254  datum[61]=(unsigned char) (low_order >> 16);
255  datum[62]=(unsigned char) (low_order >> 8);
256  datum[63]=(unsigned char) low_order;
257  TransformSignature(signature_info);
258  p=signature_info->accumulator;
259  q=GetStringInfoDatum(signature_info->digest);
260  for (i=0; i < (SignatureDigestsize/4); i++)
261  {
262  *q++=(unsigned char) ((*p >> 24) & 0xff);
263  *q++=(unsigned char) ((*p >> 16) & 0xff);
264  *q++=(unsigned char) ((*p >> 8) & 0xff);
265  *q++=(unsigned char) (*p & 0xff);
266  p++;
267  }
268 }
269 
270 /*
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 % %
273 % %
274 % %
275 + G e t S i g n a t u r e B l o c k s i z e %
276 % %
277 % %
278 % %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 %
281 % GetSignatureBlocksize() returns the Signature blocksize.
282 %
283 % The format of the GetSignatureBlocksize method is:
284 %
285 % unsigned int *GetSignatureBlocksize(const SignatureInfo *signature_info)
286 %
287 % A description of each parameter follows:
288 %
289 % o signature_info: the signature info.
290 %
291 */
293  const SignatureInfo *signature_info)
294 {
295  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
296  assert(signature_info != (SignatureInfo *) NULL);
297  assert(signature_info->signature == MagickCoreSignature);
298  return(signature_info->blocksize);
299 }
300 
301 /*
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % %
304 % %
305 % %
306 + G e t S i g n a t u r e D i g e s t %
307 % %
308 % %
309 % %
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 %
312 % GetSignatureDigest() returns the signature digest.
313 %
314 % The format of the GetSignatureDigest method is:
315 %
316 % const StringInfo *GetSignatureDigest(const SignatureInfo *signature_info)
317 %
318 % A description of each parameter follows:
319 %
320 % o signature_info: the signature info.
321 %
322 */
324  const SignatureInfo *signature_info)
325 {
326  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
327  assert(signature_info != (SignatureInfo *) NULL);
328  assert(signature_info->signature == MagickCoreSignature);
329  return(signature_info->digest);
330 }
331 
332 /*
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 % %
335 % %
336 % %
337 + G e t S i g n a t u r e D i g e s t s i z e %
338 % %
339 % %
340 % %
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 %
343 % GetSignatureDigestsize() returns the Signature digest size.
344 %
345 % The format of the GetSignatureDigestsize method is:
346 %
347 % unsigned int *GetSignatureDigestsize(const SignatureInfo *signature_info)
348 %
349 % A description of each parameter follows:
350 %
351 % o signature_info: the signature info.
352 %
353 */
355  const SignatureInfo *signature_info)
356 {
357  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
358  assert(signature_info != (SignatureInfo *) NULL);
359  assert(signature_info->signature == MagickCoreSignature);
360  return(signature_info->digestsize);
361 }
362 
363 /*
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 % %
366 % %
367 % %
368 + I n i t i a l i z e S i g n a t u r e %
369 % %
370 % %
371 % %
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 %
374 % InitializeSignature() initializes the Signature accumulator.
375 %
376 % The format of the DestroySignatureInfo method is:
377 %
378 % void InitializeSignatureInfo(SignatureInfo *signature_info)
379 %
380 % A description of each parameter follows:
381 %
382 % o signature_info: the cipher signature_info.
383 %
384 */
386 {
387  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
388  assert(signature_info != (SignatureInfo *) NULL);
389  assert(signature_info->signature == MagickCoreSignature);
390  signature_info->accumulator[0]=0x6a09e667U;
391  signature_info->accumulator[1]=0xbb67ae85U;
392  signature_info->accumulator[2]=0x3c6ef372U;
393  signature_info->accumulator[3]=0xa54ff53aU;
394  signature_info->accumulator[4]=0x510e527fU;
395  signature_info->accumulator[5]=0x9b05688cU;
396  signature_info->accumulator[6]=0x1f83d9abU;
397  signature_info->accumulator[7]=0x5be0cd19U;
398  signature_info->low_order=0;
399  signature_info->high_order=0;
400  signature_info->extent=0;
401 }
402 
403 /*
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % %
406 % %
407 % %
408 + S e t S i g n a t u r e D i g e s t %
409 % %
410 % %
411 % %
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413 %
414 % SetSignatureDigest() set the signature digest.
415 %
416 % The format of the SetSignatureDigest method is:
417 %
418 % SetSignatureDigest(SignatureInfo *signature_info,
419 % const StringInfo *digest)
420 %
421 % A description of each parameter follows:
422 %
423 % o signature_info: the signature info.
424 %
425 % o digest: the digest.
426 %
427 */
429  const StringInfo *digest)
430 {
431  /*
432  Set the signature accumulator.
433  */
434  assert(signature_info != (SignatureInfo *) NULL);
435  assert(signature_info->signature == MagickCoreSignature);
436  SetStringInfo(signature_info->digest,digest);
437 }
438 
439 /*
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
441 % %
442 % %
443 % %
444 % S i g n a t u r e I m a g e %
445 % %
446 % %
447 % %
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449 %
450 % SignatureImage() computes a message digest from an image pixel stream with
451 % an implementation of the NIST SHA-256 Message Digest algorithm. This
452 % signature uniquely identifies the image and is convenient for determining
453 % if an image has been modified or whether two images are identical.
454 %
455 % The format of the SignatureImage method is:
456 %
457 % MagickBooleanType SignatureImage(Image *image,ExceptionInfo *exception)
458 %
459 % A description of each parameter follows:
460 %
461 % o image: the image.
462 %
463 % o exception: return any errors or warnings in this structure.
464 %
465 */
467  ExceptionInfo *exception)
468 {
469  CacheView
470  *image_view;
471 
472  char
473  *hex_signature;
474 
475  double
476  pixel;
477 
478  register const Quantum
479  *p;
480 
482  *signature_info;
483 
484  ssize_t
485  y;
486 
487  StringInfo
488  *signature;
489 
490  unsigned char
491  *pixels;
492 
493  /*
494  Compute image digital signature.
495  */
496  assert(image != (Image *) NULL);
497  assert(image->signature == MagickCoreSignature);
498  if (image->debug != MagickFalse)
499  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
500  signature_info=AcquireSignatureInfo();
501  signature=AcquireStringInfo(GetPixelChannels(image)*image->columns*
502  sizeof(pixel));
503  image_view=AcquireVirtualCacheView(image,exception);
504  for (y=0; y < (ssize_t) image->rows; y++)
505  {
506  register ssize_t
507  x;
508 
509  register unsigned char
510  *q;
511 
512  p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
513  if (p == (const Quantum *) NULL)
514  break;
515  SetStringInfoLength(signature,GetPixelChannels(image)*image->columns*
516  sizeof(pixel));
517  pixels=GetStringInfoDatum(signature);
518  q=pixels;
519  for (x=0; x < (ssize_t) image->columns; x++)
520  {
521  register ssize_t
522  i;
523 
524  if (GetPixelReadMask(image,p) <= (QuantumRange/2))
525  {
526  p+=GetPixelChannels(image);
527  continue;
528  }
529  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
530  {
531  register ssize_t
532  j;
533 
534  PixelChannel channel = GetPixelChannelChannel(image,i);
535  PixelTrait traits = GetPixelChannelTraits(image,channel);
536  if (traits == UndefinedPixelTrait)
537  continue;
538  pixel=QuantumScale*p[i];
539  if (signature_info->lsb_first == MagickFalse)
540  for (j=(ssize_t) sizeof(pixel)-1; j >= 0; j--)
541  *q++=(unsigned char) ((unsigned char *) &pixel)[j];
542  else
543  for (j=0; j < (ssize_t) sizeof(pixel); j++)
544  *q++=(unsigned char) ((unsigned char *) &pixel)[j];
545  }
546  p+=GetPixelChannels(image);
547  }
548  SetStringInfoLength(signature,(size_t) (q-pixels));
549  UpdateSignature(signature_info,signature);
550  }
551  image_view=DestroyCacheView(image_view);
552  FinalizeSignature(signature_info);
553  hex_signature=StringInfoToHexString(GetSignatureDigest(signature_info));
554  (void) DeleteImageProperty(image,"signature");
555  (void) SetImageProperty(image,"signature",hex_signature,exception);
556  /*
557  Free resources.
558  */
559  hex_signature=DestroyString(hex_signature);
560  signature=DestroyStringInfo(signature);
561  signature_info=DestroySignatureInfo(signature_info);
562  return(MagickTrue);
563 }
564 
565 /*
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 % %
568 % %
569 % %
570 + T r a n s f o r m S i g n a t u r e %
571 % %
572 % %
573 % %
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 %
576 % TransformSignature() transforms the Signature message accumulator.
577 %
578 % The format of the TransformSignature method is:
579 %
580 % TransformSignature(SignatureInfo *signature_info)
581 %
582 % A description of each parameter follows:
583 %
584 % o signature_info: the address of a structure of type SignatureInfo.
585 %
586 */
587 
588 static inline unsigned int Ch(unsigned int x,unsigned int y,unsigned int z)
589 {
590  return((x & y) ^ (~x & z));
591 }
592 
593 static inline unsigned int Maj(unsigned int x,unsigned int y,unsigned int z)
594 {
595  return((x & y) ^ (x & z) ^ (y & z));
596 }
597 
598 static inline unsigned int Trunc32(unsigned int x)
599 {
600  return((unsigned int) (x & 0xffffffffU));
601 }
602 
603 static unsigned int RotateRight(unsigned int x,unsigned int n)
604 {
605  return(Trunc32((x >> n) | (x << (32-n))));
606 }
607 
608 static void TransformSignature(SignatureInfo *signature_info)
609 {
610 #define Sigma0(x) (RotateRight(x,7) ^ RotateRight(x,18) ^ Trunc32((x) >> 3))
611 #define Sigma1(x) (RotateRight(x,17) ^ RotateRight(x,19) ^ Trunc32((x) >> 10))
612 #define Suma0(x) (RotateRight(x,2) ^ RotateRight(x,13) ^ RotateRight(x,22))
613 #define Suma1(x) (RotateRight(x,6) ^ RotateRight(x,11) ^ RotateRight(x,25))
614 
615  register ssize_t
616  i;
617 
618  register unsigned char
619  *p;
620 
621  ssize_t
622  j;
623 
624  static unsigned int
625  K[64] =
626  {
627  0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU,
628  0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U, 0xd807aa98U, 0x12835b01U,
629  0x243185beU, 0x550c7dc3U, 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U,
630  0xc19bf174U, 0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU,
631  0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU, 0x983e5152U,
632  0xa831c66dU, 0xb00327c8U, 0xbf597fc7U, 0xc6e00bf3U, 0xd5a79147U,
633  0x06ca6351U, 0x14292967U, 0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU,
634  0x53380d13U, 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U,
635  0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, 0xd192e819U,
636  0xd6990624U, 0xf40e3585U, 0x106aa070U, 0x19a4c116U, 0x1e376c08U,
637  0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU,
638  0x682e6ff3U, 0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U,
639  0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U
640  }; /* 32-bit fractional part of the cube root of the first 64 primes */
641 
642  unsigned int
643  A,
644  B,
645  C,
646  D,
647  E,
648  F,
649  G,
650  H,
651  shift,
652  T,
653  T1,
654  T2,
655  W[64];
656 
657  shift=32;
658  p=GetStringInfoDatum(signature_info->message);
659  if (signature_info->lsb_first == MagickFalse)
660  {
661 DisableMSCWarning(4127)
662  if (sizeof(unsigned int) <= 4)
664  for (i=0; i < 16; i++)
665  {
666  T=(*((unsigned int *) p));
667  p+=4;
668  W[i]=Trunc32(T);
669  }
670  else
671  for (i=0; i < 16; i+=2)
672  {
673  T=(*((unsigned int *) p));
674  p+=8;
675  W[i]=Trunc32(T >> shift);
676  W[i+1]=Trunc32(T);
677  }
678  }
679  else
680 DisableMSCWarning(4127)
681  if (sizeof(unsigned int) <= 4)
683  for (i=0; i < 16; i++)
684  {
685  T=(*((unsigned int *) p));
686  p+=4;
687  W[i]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
688  ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
689  }
690  else
691  for (i=0; i < 16; i+=2)
692  {
693  T=(*((unsigned int *) p));
694  p+=8;
695  W[i]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
696  ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
697  T>>=shift;
698  W[i+1]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
699  ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
700  }
701  /*
702  Copy accumulator to registers.
703  */
704  A=signature_info->accumulator[0];
705  B=signature_info->accumulator[1];
706  C=signature_info->accumulator[2];
707  D=signature_info->accumulator[3];
708  E=signature_info->accumulator[4];
709  F=signature_info->accumulator[5];
710  G=signature_info->accumulator[6];
711  H=signature_info->accumulator[7];
712  for (i=16; i < 64; i++)
713  W[i]=Trunc32(Sigma1(W[i-2])+W[i-7]+Sigma0(W[i-15])+W[i-16]);
714  for (j=0; j < 64; j++)
715  {
716  T1=Trunc32(H+Suma1(E)+Ch(E,F,G)+K[j]+W[j]);
717  T2=Trunc32(Suma0(A)+Maj(A,B,C));
718  H=G;
719  G=F;
720  F=E;
721  E=Trunc32(D+T1);
722  D=C;
723  C=B;
724  B=A;
725  A=Trunc32(T1+T2);
726  }
727  /*
728  Add registers back to accumulator.
729  */
730  signature_info->accumulator[0]=Trunc32(signature_info->accumulator[0]+A);
731  signature_info->accumulator[1]=Trunc32(signature_info->accumulator[1]+B);
732  signature_info->accumulator[2]=Trunc32(signature_info->accumulator[2]+C);
733  signature_info->accumulator[3]=Trunc32(signature_info->accumulator[3]+D);
734  signature_info->accumulator[4]=Trunc32(signature_info->accumulator[4]+E);
735  signature_info->accumulator[5]=Trunc32(signature_info->accumulator[5]+F);
736  signature_info->accumulator[6]=Trunc32(signature_info->accumulator[6]+G);
737  signature_info->accumulator[7]=Trunc32(signature_info->accumulator[7]+H);
738  /*
739  Reset working registers.
740  */
741  A=0;
742  B=0;
743  C=0;
744  D=0;
745  E=0;
746  F=0;
747  G=0;
748  H=0;
749  T=0;
750  T1=0;
751  T2=0;
752  (void) ResetMagickMemory(W,0,sizeof(W));
753 }
754 
755 /*
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % %
758 % %
759 % %
760 + U p d a t e S i g n a t u r e %
761 % %
762 % %
763 % %
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 %
766 % UpdateSignature() updates the Signature message accumulator.
767 %
768 % The format of the UpdateSignature method is:
769 %
770 % UpdateSignature(SignatureInfo *signature_info,const StringInfo *message)
771 %
772 % A description of each parameter follows:
773 %
774 % o signature_info: the address of a structure of type SignatureInfo.
775 %
776 % o message: the message.
777 %
778 */
780  const StringInfo *message)
781 {
782  register size_t
783  i;
784 
785  register unsigned char
786  *p;
787 
788  size_t
789  n;
790 
791  unsigned int
792  length;
793 
794  /*
795  Update the Signature accumulator.
796  */
797  assert(signature_info != (SignatureInfo *) NULL);
798  assert(signature_info->signature == MagickCoreSignature);
799  n=GetStringInfoLength(message);
800  length=Trunc32((unsigned int) (signature_info->low_order+(n << 3)));
801  if (length < signature_info->low_order)
802  signature_info->high_order++;
803  signature_info->low_order=length;
804  signature_info->high_order+=(unsigned int) n >> 29;
805  p=GetStringInfoDatum(message);
806  if (signature_info->extent != 0)
807  {
808  i=GetStringInfoLength(signature_info->message)-signature_info->extent;
809  if (i > n)
810  i=n;
811  (void) CopyMagickMemory(GetStringInfoDatum(signature_info->message)+
812  signature_info->extent,p,i);
813  n-=i;
814  p+=i;
815  signature_info->extent+=i;
816  if (signature_info->extent != GetStringInfoLength(signature_info->message))
817  return;
818  TransformSignature(signature_info);
819  }
820  while (n >= GetStringInfoLength(signature_info->message))
821  {
822  SetStringInfoDatum(signature_info->message,p);
823  p+=GetStringInfoLength(signature_info->message);
824  n-=GetStringInfoLength(signature_info->message);
825  TransformSignature(signature_info);
826  }
827  (void) CopyMagickMemory(GetStringInfoDatum(signature_info->message),p,n);
828  signature_info->extent=n;
829 }
size_t rows
Definition: image.h:172
MagickExport CacheView * DestroyCacheView(CacheView *cache_view)
Definition: cache-view.c:252
MagickPrivate void InitializeSignature(SignatureInfo *signature_info)
Definition: signature.c:385
#define Suma1(x)
static unsigned int RotateRight(unsigned int x, unsigned int n)
Definition: signature.c:603
#define DisableMSCWarning(nr)
Definition: studio.h:345
unsigned int high_order
Definition: signature.c:76
unsigned int low_order
Definition: signature.c:76
size_t extent
Definition: signature.c:81
static void TransformSignature(SignatureInfo *)
Definition: signature.c:608
#define ThrowFatalException(severity, tag)
static unsigned int Ch(unsigned int x, unsigned int y, unsigned int z)
Definition: signature.c:588
static PixelTrait GetPixelChannelTraits(const Image *magick_restrict image, const PixelChannel channel)
static Quantum GetPixelReadMask(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
MagickExport MagickBooleanType SignatureImage(Image *image, ExceptionInfo *exception)
Definition: signature.c:466
MagickExport const Quantum * GetCacheViewVirtualPixels(const CacheView *cache_view, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: cache-view.c:651
size_t signature
Definition: signature.c:90
#define SignatureBlocksize
Definition: signature.c:59
StringInfo * digest
Definition: signature.c:72
StringInfo * message
Definition: signature.c:72
unsigned int * accumulator
Definition: signature.c:76
MagickPrivate SignatureInfo * AcquireSignatureInfo(void)
Definition: signature.c:117
Definition: log.h:52
#define Sigma1(x)
Definition: image.h:151
MagickPrivate void FinalizeSignature(SignatureInfo *signature_info)
Definition: signature.c:206
#define MagickCoreSignature
#define SignatureDigestsize
Definition: signature.c:60
MagickExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition: string.c:1283
MagickBooleanType
Definition: magick-type.h:156
static unsigned int Maj(unsigned int x, unsigned int y, unsigned int z)
Definition: signature.c:593
MagickExport void * ResetMagickMemory(void *memory, int byte, const size_t size)
Definition: memory.c:1164
MagickExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition: string.c:837
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:529
#define RestoreMSCWarning
Definition: studio.h:346
MagickExport MagickBooleanType SetImageProperty(Image *image, const char *property, const char *value, ExceptionInfo *exception)
Definition: property.c:4004
MagickExport MagickBooleanType static void * AcquireCriticalMemory(const size_t size)
MagickPrivate unsigned int GetSignatureDigestsize(const SignatureInfo *signature_info)
Definition: signature.c:354
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1397
size_t signature
Definition: image.h:354
#define QuantumScale
Definition: magick-type.h:113
size_t columns
Definition: image.h:172
PixelChannel
Definition: pixel.h:66
unsigned int blocksize
Definition: signature.c:68
MagickExport StringInfo * AcquireStringInfo(const size_t length)
Definition: string.c:170
static size_t GetPixelChannels(const Image *magick_restrict image)
unsigned int digestsize
Definition: signature.c:68
MagickPrivate unsigned int GetSignatureBlocksize(const SignatureInfo *signature_info)
Definition: signature.c:292
char filename[MagickPathExtent]
Definition: image.h:319
#define GetMagickModule()
Definition: log.h:28
MagickPrivate void UpdateSignature(SignatureInfo *signature_info, const StringInfo *message)
Definition: signature.c:779
static PixelChannel GetPixelChannelChannel(const Image *magick_restrict image, const ssize_t offset)
MagickExport CacheView * AcquireVirtualCacheView(const Image *image, ExceptionInfo *exception)
Definition: cache-view.c:149
MagickBooleanType lsb_first
Definition: signature.c:84
unsigned short Quantum
Definition: magick-type.h:82
ssize_t timestamp
Definition: signature.c:87
MagickExport void SetStringInfoLength(StringInfo *string_info, const size_t length)
Definition: string.c:1784
MagickExport char * DestroyString(char *string)
Definition: string.c:810
MagickExport MagickBooleanType DeleteImageProperty(Image *image, const char *property)
Definition: property.c:279
static unsigned int Trunc32(unsigned int x)
Definition: signature.c:598
MagickPrivate const StringInfo * GetSignatureDigest(const SignatureInfo *signature_info)
Definition: signature.c:323
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1038
#define MagickPrivate
#define MagickExport
MagickPrivate void SetSignatureDigest(SignatureInfo *signature_info, const StringInfo *digest)
Definition: signature.c:428
MagickExport char * StringInfoToHexString(const StringInfo *string_info)
Definition: string.c:1940
#define Suma0(x)
PixelTrait
Definition: pixel.h:132
MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
Definition: string.c:1312
MagickExport void * CopyMagickMemory(void *destination, const void *source, const size_t size)
Definition: memory.c:721
#define Sigma0(x)
#define QuantumRange
Definition: magick-type.h:83
MagickExport void SetStringInfoDatum(StringInfo *string_info, const unsigned char *source)
Definition: string.c:1751
MagickBooleanType debug
Definition: image.h:334
MagickExport void SetStringInfo(StringInfo *string_info, const StringInfo *source)
Definition: string.c:1711
MagickPrivate SignatureInfo * DestroySignatureInfo(SignatureInfo *signature_info)
Definition: signature.c:167