MagickCore  6.9.12-64
Convert, Edit, Or Compose Bitmap Images
 All Data Structures
blob.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % BBBB L OOO BBBB %
7 % B B L O O B B %
8 % BBBB L O O BBBB %
9 % B B L O O B B %
10 % BBBB LLLLL OOO BBBB %
11 % %
12 % %
13 % MagickCore Binary Large OBjectS Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % July 1999 %
18 % %
19 % %
20 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 %
38 */
39 
40 /*
41  Include declarations.
42 */
43 #ifdef __VMS
44 #include <types.h>
45 #include <mman.h>
46 #endif
47 #include "magick/studio.h"
48 #include "magick/blob.h"
49 #include "magick/blob-private.h"
50 #include "magick/cache.h"
51 #include "magick/client.h"
52 #include "magick/constitute.h"
53 #include "magick/delegate.h"
54 #include "magick/exception.h"
55 #include "magick/exception-private.h"
56 #include "magick/geometry.h"
57 #include "magick/image-private.h"
58 #include "magick/list.h"
59 #include "magick/locale_.h"
60 #include "magick/log.h"
61 #include "magick/magick.h"
62 #include "magick/memory_.h"
63 #include "magick/nt-base-private.h"
64 #include "magick/option.h"
65 #include "magick/policy.h"
66 #include "magick/resource_.h"
67 #include "magick/semaphore.h"
68 #include "magick/string_.h"
69 #include "magick/string-private.h"
70 #include "magick/timer-private.h"
71 #include "magick/token.h"
72 #include "magick/utility.h"
73 #include "magick/utility-private.h"
74 #if defined(MAGICKCORE_ZLIB_DELEGATE)
75 #include "zlib.h"
76 #endif
77 #if defined(MAGICKCORE_BZLIB_DELEGATE)
78 #include "bzlib.h"
79 #endif
80 
81 /*
82  Define declarations.
83 */
84 #define MagickMaxBlobExtent (8*8192)
85 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
86 # define MAP_ANONYMOUS MAP_ANON
87 #endif
88 #if !defined(MAP_FAILED)
89 #define MAP_FAILED ((void *) -1)
90 #endif
91 #if defined(__OS2__)
92 #include <io.h>
93 #define _O_BINARY O_BINARY
94 #endif
95 
96 /*
97  Typedef declarations.
98 */
99 typedef union FileInfo
100 {
101  FILE
102  *file;
103 
104 #if defined(MAGICKCORE_ZLIB_DELEGATE)
105  gzFile
106  gzfile;
107 #endif
108 
109 #if defined(MAGICKCORE_BZLIB_DELEGATE)
110  BZFILE
111  *bzfile;
112 #endif
113 } FileInfo;
114 
115 struct _BlobInfo
116 {
117  size_t
118  length,
119  extent,
120  quantum;
121 
122  BlobMode
123  mode;
124 
125  MagickBooleanType
126  mapped,
127  eof;
128 
129  int
130  error,
131  error_number;
132 
133  MagickOffsetType
134  offset;
135 
136  MagickSizeType
137  size;
138 
139  MagickBooleanType
140  exempt,
141  synchronize,
142  status,
143  temporary;
144 
145  StreamType
146  type;
147 
148  FileInfo
149  file_info;
150 
151  struct stat
152  properties;
153 
154  StreamHandler
155  stream;
156 
157  unsigned char
158  *data;
159 
160  MagickBooleanType
161  debug;
162 
164  *semaphore;
165 
166  ssize_t
167  reference_count;
168 
169  size_t
170  signature;
171 };
172 
173 /*
174  Forward declarations.
175 */
176 static int
177  SyncBlob(Image *);
178 
179 /*
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 % %
182 % %
183 % %
184 + A t t a c h B l o b %
185 % %
186 % %
187 % %
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 %
190 % AttachBlob() attaches a blob to the BlobInfo structure.
191 %
192 % The format of the AttachBlob method is:
193 %
194 % void AttachBlob(BlobInfo *blob_info,const void *blob,const size_t length)
195 %
196 % A description of each parameter follows:
197 %
198 % o blob_info: Specifies a pointer to a BlobInfo structure.
199 %
200 % o blob: the address of a character stream in one of the image formats
201 % understood by ImageMagick.
202 %
203 % o length: This size_t integer reflects the length in bytes of the blob.
204 %
205 */
206 MagickExport void AttachBlob(BlobInfo *blob_info,const void *blob,
207  const size_t length)
208 {
209  assert(blob_info != (BlobInfo *) NULL);
210  if (IsEventLogging() != MagickFalse)
211  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
212  blob_info->length=length;
213  blob_info->extent=length;
214  blob_info->quantum=(size_t) MagickMaxBlobExtent;
215  blob_info->offset=0;
216  blob_info->type=BlobStream;
217  blob_info->file_info.file=(FILE *) NULL;
218  blob_info->data=(unsigned char *) blob;
219  blob_info->mapped=MagickFalse;
220 }
221 
222 /*
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % %
225 % %
226 % %
227 + B l o b T o F i l e %
228 % %
229 % %
230 % %
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 %
233 % BlobToFile() writes a blob to a file. It returns MagickFalse if an error
234 % occurs otherwise MagickTrue.
235 %
236 % The format of the BlobToFile method is:
237 %
238 % MagickBooleanType BlobToFile(char *filename,const void *blob,
239 % const size_t length,ExceptionInfo *exception)
240 %
241 % A description of each parameter follows:
242 %
243 % o filename: Write the blob to this file.
244 %
245 % o blob: the address of a blob.
246 %
247 % o length: This length in bytes of the blob.
248 %
249 % o exception: return any errors or warnings in this structure.
250 %
251 */
252 MagickExport MagickBooleanType BlobToFile(char *filename,const void *blob,
253  const size_t length,ExceptionInfo *exception)
254 {
255  int
256  file;
257 
258  size_t
259  i;
260 
261  ssize_t
262  count;
263 
264  assert(filename != (const char *) NULL);
265  assert(blob != (const void *) NULL);
266  if (IsEventLogging() != MagickFalse)
267  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
268  if (*filename == '\0')
269  file=AcquireUniqueFileResource(filename);
270  else
271  file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
272  if (file == -1)
273  {
274  ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
275  return(MagickFalse);
276  }
277  for (i=0; i < length; i+=count)
278  {
279  count=write(file,(const char *) blob+i,MagickMin(length-i,(size_t)
280  MAGICK_SSIZE_MAX));
281  if (count <= 0)
282  {
283  count=0;
284  if (errno != EINTR)
285  break;
286  }
287  }
288  file=close(file);
289  if ((file == -1) || (i < length))
290  {
291  ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
292  return(MagickFalse);
293  }
294  return(MagickTrue);
295 }
296 
297 /*
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 % %
300 % %
301 % %
302 % B l o b T o I m a g e %
303 % %
304 % %
305 % %
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 %
308 % BlobToImage() implements direct to memory image formats. It returns the
309 % blob as an image.
310 %
311 % The format of the BlobToImage method is:
312 %
313 % Image *BlobToImage(const ImageInfo *image_info,const void *blob,
314 % const size_t length,ExceptionInfo *exception)
315 %
316 % A description of each parameter follows:
317 %
318 % o image_info: the image info.
319 %
320 % o blob: the address of a character stream in one of the image formats
321 % understood by ImageMagick.
322 %
323 % o length: This size_t integer reflects the length in bytes of the blob.
324 %
325 % o exception: return any errors or warnings in this structure.
326 %
327 */
328 MagickExport Image *BlobToImage(const ImageInfo *image_info,const void *blob,
329  const size_t length,ExceptionInfo *exception)
330 {
331  const MagickInfo
332  *magick_info;
333 
334  Image
335  *image;
336 
337  ImageInfo
338  *blob_info,
339  *clone_info;
340 
341  MagickBooleanType
342  status;
343 
344  assert(image_info != (ImageInfo *) NULL);
345  assert(image_info->signature == MagickCoreSignature);
346  assert(exception != (ExceptionInfo *) NULL);
347  if (IsEventLogging() != MagickFalse)
348  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
349  image_info->filename);
350  if ((blob == (const void *) NULL) || (length == 0))
351  {
352  (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
353  "ZeroLengthBlobNotPermitted","`%s'",image_info->filename);
354  return((Image *) NULL);
355  }
356  blob_info=CloneImageInfo(image_info);
357  blob_info->blob=(void *) blob;
358  blob_info->length=length;
359  if (*blob_info->magick == '\0')
360  (void) SetImageInfo(blob_info,0,exception);
361  magick_info=GetMagickInfo(blob_info->magick,exception);
362  if (magick_info == (const MagickInfo *) NULL)
363  {
364  (void) ThrowMagickException(exception,GetMagickModule(),
365  MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
366  blob_info->magick);
367  blob_info=DestroyImageInfo(blob_info);
368  return((Image *) NULL);
369  }
370  if (GetMagickBlobSupport(magick_info) != MagickFalse)
371  {
372  char
373  filename[MagickPathExtent];
374 
375  /*
376  Native blob support for this image format.
377  */
378  (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
379  (void) FormatLocaleString(blob_info->filename,MagickPathExtent,"%s:%s",
380  blob_info->magick,filename);
381  image=ReadImage(blob_info,exception);
382  if (image != (Image *) NULL)
383  (void) DetachBlob(image->blob);
384  blob_info=DestroyImageInfo(blob_info);
385  return(image);
386  }
387  /*
388  Write blob to a temporary file on disk.
389  */
390  blob_info->blob=(void *) NULL;
391  blob_info->length=0;
392  *blob_info->filename='\0';
393  status=BlobToFile(blob_info->filename,blob,length,exception);
394  if (status == MagickFalse)
395  {
396  (void) RelinquishUniqueFileResource(blob_info->filename);
397  blob_info=DestroyImageInfo(blob_info);
398  return((Image *) NULL);
399  }
400  clone_info=CloneImageInfo(blob_info);
401  (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:%s",
402  blob_info->magick,blob_info->filename);
403  image=ReadImage(clone_info,exception);
404  if (image != (Image *) NULL)
405  {
406  Image
407  *images;
408 
409  /*
410  Restore original filenames and image format.
411  */
412  for (images=GetFirstImageInList(image); images != (Image *) NULL; )
413  {
414  (void) CopyMagickString(images->filename,image_info->filename,
415  MagickPathExtent);
416  (void) CopyMagickString(images->magick_filename,image_info->filename,
417  MagickPathExtent);
418  (void) CopyMagickString(images->magick,magick_info->name,
419  MagickPathExtent);
420  images=GetNextImageInList(images);
421  }
422  }
423  clone_info=DestroyImageInfo(clone_info);
424  (void) RelinquishUniqueFileResource(blob_info->filename);
425  blob_info=DestroyImageInfo(blob_info);
426  return(image);
427 }
428 
429 /*
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % %
432 % %
433 % %
434 + C l o n e B l o b I n f o %
435 % %
436 % %
437 % %
438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439 %
440 % CloneBlobInfo() makes a duplicate of the given blob info structure, or if
441 % blob info is NULL, a new one.
442 %
443 % The format of the CloneBlobInfo method is:
444 %
445 % BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
446 %
447 % A description of each parameter follows:
448 %
449 % o blob_info: the blob info.
450 %
451 */
452 MagickExport BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
453 {
454  BlobInfo
455  *clone_info;
456 
458  *semaphore;
459 
460  clone_info=(BlobInfo *) AcquireCriticalMemory(sizeof(*clone_info));
461  GetBlobInfo(clone_info);
462  if (blob_info == (BlobInfo *) NULL)
463  return(clone_info);
464  semaphore=clone_info->semaphore;
465  (void) memcpy(clone_info,blob_info,sizeof(*clone_info));
466  if (blob_info->mapped != MagickFalse)
467  (void) AcquireMagickResource(MapResource,blob_info->length);
468  clone_info->semaphore=semaphore;
469  LockSemaphoreInfo(clone_info->semaphore);
470  clone_info->reference_count=1;
471  UnlockSemaphoreInfo(clone_info->semaphore);
472  return(clone_info);
473 }
474 
475 /*
476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 % %
478 % %
479 % %
480 + C l o s e B l o b %
481 % %
482 % %
483 % %
484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485 %
486 % CloseBlob() closes a stream associated with the image.
487 %
488 % The format of the CloseBlob method is:
489 %
490 % MagickBooleanType CloseBlob(Image *image)
491 %
492 % A description of each parameter follows:
493 %
494 % o image: the image.
495 %
496 */
497 
498 static inline void ThrowBlobException(BlobInfo *blob_info)
499 {
500  if ((blob_info->status == MagickFalse) && (errno != 0))
501  blob_info->error_number=errno;
502  blob_info->status=MagickTrue;
503 }
504 
505 MagickExport MagickBooleanType CloseBlob(Image *image)
506 {
507  BlobInfo
508  *magick_restrict blob_info;
509 
510  int
511  status;
512 
513  /*
514  Close image file.
515  */
516  assert(image != (Image *) NULL);
517  assert(image->signature == MagickCoreSignature);
518  if (IsEventLogging() != MagickFalse)
519  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
520  blob_info=image->blob;
521  if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
522  return(MagickTrue);
523  status=SyncBlob(image);
524  switch (blob_info->type)
525  {
526  case UndefinedStream:
527  case StandardStream:
528  break;
529  case FileStream:
530  case PipeStream:
531  {
532  if (blob_info->synchronize != MagickFalse)
533  {
534  status=fflush(blob_info->file_info.file);
535  if (status != 0)
536  ThrowBlobException(blob_info);
537  status=fsync(fileno(blob_info->file_info.file));
538  if (status != 0)
539  ThrowBlobException(blob_info);
540  }
541  if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
542  ThrowBlobException(blob_info);
543  break;
544  }
545  case ZipStream:
546  {
547 #if defined(MAGICKCORE_ZLIB_DELEGATE)
548  status=Z_OK;
549  (void) gzerror(blob_info->file_info.gzfile,&status);
550  if (status != Z_OK)
551  ThrowBlobException(blob_info);
552 #endif
553  break;
554  }
555  case BZipStream:
556  {
557 #if defined(MAGICKCORE_BZLIB_DELEGATE)
558  status=BZ_OK;
559  (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
560  if (status != BZ_OK)
561  ThrowBlobException(blob_info);
562 #endif
563  break;
564  }
565  case FifoStream:
566  break;
567  case BlobStream:
568  {
569  if (blob_info->file_info.file != (FILE *) NULL)
570  {
571  if (blob_info->synchronize != MagickFalse)
572  {
573  status=fflush(blob_info->file_info.file);
574  if (status != 0)
575  ThrowBlobException(blob_info);
576  status=fsync(fileno(blob_info->file_info.file));
577  if (status != 0)
578  ThrowBlobException(blob_info);
579  }
580  if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
581  ThrowBlobException(blob_info);
582  }
583  break;
584  }
585  }
586  blob_info->size=GetBlobSize(image);
587  image->extent=blob_info->size;
588  blob_info->eof=MagickFalse;
589  blob_info->error=0;
590  blob_info->mode=UndefinedBlobMode;
591  if (blob_info->exempt != MagickFalse)
592  {
593  blob_info->type=UndefinedStream;
594  return(blob_info->status);
595  }
596  switch (blob_info->type)
597  {
598  case UndefinedStream:
599  case StandardStream:
600  break;
601  case FileStream:
602  {
603  if (blob_info->file_info.file != (FILE *) NULL)
604  {
605  status=fclose(blob_info->file_info.file);
606  if (status != 0)
607  ThrowBlobException(blob_info);
608  }
609  break;
610  }
611  case PipeStream:
612  {
613 #if defined(MAGICKCORE_HAVE_PCLOSE)
614  status=pclose(blob_info->file_info.file);
615  if (status != 0)
616  ThrowBlobException(blob_info);
617 #endif
618  break;
619  }
620  case ZipStream:
621  {
622 #if defined(MAGICKCORE_ZLIB_DELEGATE)
623  status=gzclose(blob_info->file_info.gzfile);
624  if (status != Z_OK)
625  ThrowBlobException(blob_info);
626 #endif
627  break;
628  }
629  case BZipStream:
630  {
631 #if defined(MAGICKCORE_BZLIB_DELEGATE)
632  BZ2_bzclose(blob_info->file_info.bzfile);
633 #endif
634  break;
635  }
636  case FifoStream:
637  break;
638  case BlobStream:
639  {
640  if (blob_info->file_info.file != (FILE *) NULL)
641  {
642  status=fclose(blob_info->file_info.file);
643  if (status != 0)
644  ThrowBlobException(blob_info);
645  }
646  break;
647  }
648  }
649  (void) DetachBlob(blob_info);
650  return(blob_info->status);
651 }
652 
653 /*
654 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % %
656 % %
657 % %
658 + D e s t r o y B l o b %
659 % %
660 % %
661 % %
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 %
664 % DestroyBlob() deallocates memory associated with a blob.
665 %
666 % The format of the DestroyBlob method is:
667 %
668 % void DestroyBlob(Image *image)
669 %
670 % A description of each parameter follows:
671 %
672 % o image: the image.
673 %
674 */
675 MagickExport void DestroyBlob(Image *image)
676 {
677  BlobInfo
678  *magick_restrict blob_info;
679 
680  MagickBooleanType
681  destroy;
682 
683  assert(image != (Image *) NULL);
684  assert(image->signature == MagickCoreSignature);
685  assert(image->blob != (BlobInfo *) NULL);
686  assert(image->blob->signature == MagickCoreSignature);
687  if (IsEventLogging() != MagickFalse)
688  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
689  blob_info=image->blob;
690  destroy=MagickFalse;
691  LockSemaphoreInfo(blob_info->semaphore);
692  blob_info->reference_count--;
693  assert(blob_info->reference_count >= 0);
694  if (blob_info->reference_count == 0)
695  destroy=MagickTrue;
696  UnlockSemaphoreInfo(blob_info->semaphore);
697  if (destroy == MagickFalse)
698  {
699  image->blob=(BlobInfo *) NULL;
700  return;
701  }
702  (void) CloseBlob(image);
703  if (blob_info->mapped != MagickFalse)
704  {
705  (void) UnmapBlob(blob_info->data,blob_info->length);
706  RelinquishMagickResource(MapResource,blob_info->length);
707  }
708  if (blob_info->semaphore != (SemaphoreInfo *) NULL)
709  DestroySemaphoreInfo(&blob_info->semaphore);
710  blob_info->signature=(~MagickCoreSignature);
711  image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
712 }
713 
714 /*
715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716 % %
717 % %
718 % %
719 + D e t a c h B l o b %
720 % %
721 % %
722 % %
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724 %
725 % DetachBlob() detaches a blob from the BlobInfo structure.
726 %
727 % The format of the DetachBlob method is:
728 %
729 % unsigned char *DetachBlob(BlobInfo *blob_info)
730 %
731 % A description of each parameter follows:
732 %
733 % o blob_info: Specifies a pointer to a BlobInfo structure.
734 %
735 */
736 MagickExport unsigned char *DetachBlob(BlobInfo *blob_info)
737 {
738  unsigned char
739  *data;
740 
741  assert(blob_info != (BlobInfo *) NULL);
742  if (IsEventLogging() != MagickFalse)
743  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
744  if (blob_info->mapped != MagickFalse)
745  {
746  (void) UnmapBlob(blob_info->data,blob_info->length);
747  blob_info->data=NULL;
748  RelinquishMagickResource(MapResource,blob_info->length);
749  }
750  blob_info->mapped=MagickFalse;
751  blob_info->length=0;
752  blob_info->offset=0;
753  blob_info->eof=MagickFalse;
754  blob_info->error=0;
755  blob_info->exempt=MagickFalse;
756  blob_info->type=UndefinedStream;
757  blob_info->file_info.file=(FILE *) NULL;
758  data=blob_info->data;
759  blob_info->data=(unsigned char *) NULL;
760  blob_info->stream=(StreamHandler) NULL;
761  return(data);
762 }
763 
764 /*
765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % %
767 % %
768 % %
769 + D i s a s s o c i a t e B l o b %
770 % %
771 % %
772 % %
773 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 %
775 % DisassociateBlob() disassociates the image stream. It checks if the
776 % blob of the specified image is referenced by other images. If the reference
777 % count is higher then 1 a new blob is assigned to the specified image.
778 %
779 % The format of the DisassociateBlob method is:
780 %
781 % void DisassociateBlob(const Image *image)
782 %
783 % A description of each parameter follows:
784 %
785 % o image: the image.
786 %
787 */
788 MagickPrivate void DisassociateBlob(Image *image)
789 {
790  BlobInfo
791  *magick_restrict blob_info,
792  *clone_info;
793 
794  MagickBooleanType
795  clone;
796 
797  assert(image != (Image *) NULL);
798  assert(image->signature == MagickCoreSignature);
799  assert(image->blob != (BlobInfo *) NULL);
800  assert(image->blob->signature == MagickCoreSignature);
801  if (IsEventLogging() != MagickFalse)
802  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
803  blob_info=image->blob;
804  clone=MagickFalse;
805  LockSemaphoreInfo(blob_info->semaphore);
806  assert(blob_info->reference_count >= 0);
807  if (blob_info->reference_count > 1)
808  clone=MagickTrue;
809  UnlockSemaphoreInfo(blob_info->semaphore);
810  if (clone == MagickFalse)
811  return;
812  clone_info=CloneBlobInfo(blob_info);
813  DestroyBlob(image);
814  image->blob=clone_info;
815 }
816 
817 /*
818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
819 % %
820 % %
821 % %
822 + D i s c a r d B l o b B y t e s %
823 % %
824 % %
825 % %
826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 %
828 % DiscardBlobBytes() discards bytes in a blob.
829 %
830 % The format of the DiscardBlobBytes method is:
831 %
832 % MagickBooleanType DiscardBlobBytes(Image *image,
833 % const MagickSizeType length)
834 %
835 % A description of each parameter follows.
836 %
837 % o image: the image.
838 %
839 % o length: the number of bytes to skip.
840 %
841 */
842 MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
843  const MagickSizeType length)
844 {
845  MagickOffsetType
846  i;
847 
848  size_t
849  quantum;
850 
851  ssize_t
852  count;
853 
854  unsigned char
855  buffer[MagickMinBufferExtent >> 1];
856 
857  assert(image != (Image *) NULL);
858  assert(image->signature == MagickCoreSignature);
859  if (length != (MagickSizeType) ((MagickOffsetType) length))
860  return(MagickFalse);
861  count=0;
862  for (i=0; i < (MagickOffsetType) length; i+=count)
863  {
864  quantum=(size_t) MagickMin(length-i,sizeof(buffer));
865  (void) ReadBlobStream(image,quantum,buffer,&count);
866  if (count <= 0)
867  {
868  count=0;
869  if (errno != EINTR)
870  break;
871  }
872  }
873  return(i < (MagickOffsetType) length ? MagickFalse : MagickTrue);
874 }
875 
876 /*
877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 % %
879 % %
880 % %
881 + D u p l i c a t e s B l o b %
882 % %
883 % %
884 % %
885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886 %
887 % DuplicateBlob() duplicates a blob descriptor.
888 %
889 % The format of the DuplicateBlob method is:
890 %
891 % void DuplicateBlob(Image *image,const Image *duplicate)
892 %
893 % A description of each parameter follows:
894 %
895 % o image: the image.
896 %
897 % o duplicate: the duplicate image.
898 %
899 */
900 MagickExport void DuplicateBlob(Image *image,const Image *duplicate)
901 {
902  assert(image != (Image *) NULL);
903  assert(image->signature == MagickCoreSignature);
904  assert(duplicate != (Image *) NULL);
905  assert(duplicate->signature == MagickCoreSignature);
906  if (IsEventLogging() != MagickFalse)
907  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
908  DestroyBlob(image);
909  image->blob=ReferenceBlob(duplicate->blob);
910 }
911 
912 /*
913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
914 % %
915 % %
916 % %
917 + E O F B l o b %
918 % %
919 % %
920 % %
921 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
922 %
923 % EOFBlob() returns a non-zero value when EOF has been detected reading from
924 % a blob or file.
925 %
926 % The format of the EOFBlob method is:
927 %
928 % int EOFBlob(const Image *image)
929 %
930 % A description of each parameter follows:
931 %
932 % o image: the image.
933 %
934 */
935 MagickExport int EOFBlob(const Image *image)
936 {
937  BlobInfo
938  *magick_restrict blob_info;
939 
940  assert(image != (Image *) NULL);
941  assert(image->signature == MagickCoreSignature);
942  assert(image->blob != (BlobInfo *) NULL);
943  assert(image->blob->type != UndefinedStream);
944  if (IsEventLogging() != MagickFalse)
945  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
946  blob_info=image->blob;
947  switch (blob_info->type)
948  {
949  case UndefinedStream:
950  case StandardStream:
951  break;
952  case FileStream:
953  case PipeStream:
954  {
955  blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
956  MagickFalse;
957  break;
958  }
959  case ZipStream:
960  {
961 #if defined(MAGICKCORE_ZLIB_DELEGATE)
962  blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
963  MagickFalse;
964 #endif
965  break;
966  }
967  case BZipStream:
968  {
969 #if defined(MAGICKCORE_BZLIB_DELEGATE)
970  int
971  status;
972 
973  status=0;
974  (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
975  blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
976 #endif
977  break;
978  }
979  case FifoStream:
980  {
981  blob_info->eof=MagickFalse;
982  break;
983  }
984  case BlobStream:
985  break;
986  }
987  return((int) blob_info->eof);
988 }
989 
990 /*
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 % %
993 % %
994 % %
995 + E r r o r B l o b %
996 % %
997 % %
998 % %
999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 %
1001 % ErrorBlob() returns a non-zero value when an error has been detected reading
1002 % from a blob or file.
1003 %
1004 % The format of the ErrorBlob method is:
1005 %
1006 % int ErrorBlob(const Image *image)
1007 %
1008 % A description of each parameter follows:
1009 %
1010 % o image: the image.
1011 %
1012 */
1013 MagickExport int ErrorBlob(const Image *image)
1014 {
1015  BlobInfo
1016  *magick_restrict blob_info;
1017 
1018  assert(image != (Image *) NULL);
1019  assert(image->signature == MagickCoreSignature);
1020  assert(image->blob != (BlobInfo *) NULL);
1021  assert(image->blob->type != UndefinedStream);
1022  if (IsEventLogging() != MagickFalse)
1023  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1024  blob_info=image->blob;
1025  switch (blob_info->type)
1026  {
1027  case UndefinedStream:
1028  case StandardStream:
1029  break;
1030  case FileStream:
1031  case PipeStream:
1032  {
1033  blob_info->error=ferror(blob_info->file_info.file);
1034  break;
1035  }
1036  case ZipStream:
1037  {
1038 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1039  (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1040 #endif
1041  break;
1042  }
1043  case BZipStream:
1044  {
1045 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1046  (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1047 #endif
1048  break;
1049  }
1050  case FifoStream:
1051  {
1052  blob_info->error=0;
1053  break;
1054  }
1055  case BlobStream:
1056  break;
1057  }
1058  return(blob_info->error);
1059 }
1060 
1061 /*
1062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1063 % %
1064 % %
1065 % %
1066 % F i l e T o B l o b %
1067 % %
1068 % %
1069 % %
1070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1071 %
1072 % FileToBlob() returns the contents of a file as a buffer terminated with
1073 % the '\0' character. The length of the buffer (not including the extra
1074 % terminating '\0' character) is returned via the 'length' parameter. Free
1075 % the buffer with RelinquishMagickMemory().
1076 %
1077 % The format of the FileToBlob method is:
1078 %
1079 % unsigned char *FileToBlob(const char *filename,const size_t extent,
1080 % size_t *length,ExceptionInfo *exception)
1081 %
1082 % A description of each parameter follows:
1083 %
1084 % o blob: FileToBlob() returns the contents of a file as a blob. If
1085 % an error occurs NULL is returned.
1086 %
1087 % o filename: the filename.
1088 %
1089 % o extent: The maximum length of the blob.
1090 %
1091 % o length: On return, this reflects the actual length of the blob.
1092 %
1093 % o exception: return any errors or warnings in this structure.
1094 %
1095 */
1096 MagickExport unsigned char *FileToBlob(const char *filename,const size_t extent,
1097  size_t *length,ExceptionInfo *exception)
1098 {
1099  int
1100  file;
1101 
1102  MagickBooleanType
1103  status;
1104 
1105  MagickOffsetType
1106  offset;
1107 
1108  size_t
1109  i;
1110 
1111  ssize_t
1112  count;
1113 
1114  struct stat
1115  attributes;
1116 
1117  unsigned char
1118  *blob;
1119 
1120  void
1121  *map;
1122 
1123  assert(filename != (const char *) NULL);
1124  assert(exception != (ExceptionInfo *) NULL);
1125  assert(exception->signature == MagickCoreSignature);
1126  if (IsEventLogging() != MagickFalse)
1127  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1128  *length=0;
1129  status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1130  if (status == MagickFalse)
1131  {
1132  errno=EPERM;
1133  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1134  "NotAuthorized","`%s'",filename);
1135  return(NULL);
1136  }
1137  file=fileno(stdin);
1138  if (LocaleCompare(filename,"-") != 0)
1139  {
1140  status=GetPathAttributes(filename,&attributes);
1141  if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1142  {
1143  ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1144  return(NULL);
1145  }
1146  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1147  }
1148  if (file == -1)
1149  {
1150  ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
1151  return((unsigned char *) NULL);
1152  }
1153  offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1154  count=0;
1155  if ((file == fileno(stdin)) || (offset < 0) ||
1156  (offset != (MagickOffsetType) ((ssize_t) offset)))
1157  {
1158  size_t
1159  quantum;
1160 
1161  struct stat
1162  file_stats;
1163 
1164  /*
1165  Stream is not seekable.
1166  */
1167  offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1168  quantum=(size_t) MagickMaxBufferExtent;
1169  if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1170  quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1171  blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1172  for (i=0; blob != (unsigned char *) NULL; i+=count)
1173  {
1174  count=read(file,blob+i,quantum);
1175  if (count <= 0)
1176  {
1177  count=0;
1178  if (errno != EINTR)
1179  break;
1180  }
1181  if (~((size_t) i) < (count+quantum+1))
1182  {
1183  blob=(unsigned char *) RelinquishMagickMemory(blob);
1184  break;
1185  }
1186  blob=(unsigned char *) ResizeQuantumMemory(blob,i+count+quantum+1,
1187  sizeof(*blob));
1188  if ((size_t) (i+count) >= extent)
1189  break;
1190  }
1191  if (LocaleCompare(filename,"-") != 0)
1192  file=close(file);
1193  if (blob == (unsigned char *) NULL)
1194  {
1195  (void) ThrowMagickException(exception,GetMagickModule(),
1196  ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1197  return((unsigned char *) NULL);
1198  }
1199  if (file == -1)
1200  {
1201  blob=(unsigned char *) RelinquishMagickMemory(blob);
1202  ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1203  return((unsigned char *) NULL);
1204  }
1205  *length=(size_t) MagickMin(i+count,extent);
1206  blob[*length]='\0';
1207  return(blob);
1208  }
1209  *length=(size_t) MagickMin(offset,(MagickOffsetType)
1210  MagickMin(extent,(size_t) MAGICK_SSIZE_MAX));
1211  blob=(unsigned char *) NULL;
1212  if (~(*length) >= (MagickPathExtent-1))
1213  blob=(unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1214  sizeof(*blob));
1215  if (blob == (unsigned char *) NULL)
1216  {
1217  file=close(file);
1218  (void) ThrowMagickException(exception,GetMagickModule(),
1219  ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1220  return((unsigned char *) NULL);
1221  }
1222  map=MapBlob(file,ReadMode,0,*length);
1223  if (map != (unsigned char *) NULL)
1224  {
1225  (void) memcpy(blob,map,*length);
1226  (void) UnmapBlob(map,*length);
1227  }
1228  else
1229  {
1230  (void) lseek(file,0,SEEK_SET);
1231  for (i=0; i < *length; i+=count)
1232  {
1233  count=read(file,blob+i,(size_t) MagickMin(*length-i,(size_t)
1234  MAGICK_SSIZE_MAX));
1235  if (count <= 0)
1236  {
1237  count=0;
1238  if (errno != EINTR)
1239  break;
1240  }
1241  }
1242  if (i < *length)
1243  {
1244  file=close(file)-1;
1245  blob=(unsigned char *) RelinquishMagickMemory(blob);
1246  ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1247  return((unsigned char *) NULL);
1248  }
1249  }
1250  blob[*length]='\0';
1251  if (LocaleCompare(filename,"-") != 0)
1252  file=close(file);
1253  if (file == -1)
1254  {
1255  blob=(unsigned char *) RelinquishMagickMemory(blob);
1256  ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1257  }
1258  return(blob);
1259 }
1260 
1261 /*
1262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1263 % %
1264 % %
1265 % %
1266 % F i l e T o I m a g e %
1267 % %
1268 % %
1269 % %
1270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1271 %
1272 % FileToImage() write the contents of a file to an image.
1273 %
1274 % The format of the FileToImage method is:
1275 %
1276 % MagickBooleanType FileToImage(Image *,const char *filename)
1277 %
1278 % A description of each parameter follows:
1279 %
1280 % o image: the image.
1281 %
1282 % o filename: the filename.
1283 %
1284 */
1285 
1286 static inline ssize_t WriteBlobStream(Image *image,const size_t length,
1287  const unsigned char *magick_restrict data)
1288 {
1289  BlobInfo
1290  *magick_restrict blob_info;
1291 
1292  MagickSizeType
1293  extent;
1294 
1295  unsigned char
1296  *magick_restrict q;
1297 
1298  assert(image->blob != (BlobInfo *) NULL);
1299  assert(image->blob->type != UndefinedStream);
1300  assert(data != (void *) NULL);
1301  blob_info=image->blob;
1302  if (blob_info->type != BlobStream)
1303  return(WriteBlob(image,length,data));
1304  extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1305  if (extent >= blob_info->extent)
1306  {
1307  extent=blob_info->extent+blob_info->quantum+length;
1308  blob_info->quantum<<=1;
1309  if (SetBlobExtent(image,extent) == MagickFalse)
1310  return(0);
1311  }
1312  q=blob_info->data+blob_info->offset;
1313  (void) memcpy(q,data,length);
1314  blob_info->offset+=length;
1315  if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1316  blob_info->length=(size_t) blob_info->offset;
1317  return((ssize_t) length);
1318 }
1319 
1320 MagickExport MagickBooleanType FileToImage(Image *image,const char *filename)
1321 {
1322  int
1323  file;
1324 
1325  MagickBooleanType
1326  status;
1327 
1328  size_t
1329  length,
1330  quantum;
1331 
1332  ssize_t
1333  count;
1334 
1335  struct stat
1336  file_stats;
1337 
1338  unsigned char
1339  *blob;
1340 
1341  assert(image != (const Image *) NULL);
1342  assert(image->signature == MagickCoreSignature);
1343  assert(filename != (const char *) NULL);
1344  if (IsEventLogging() != MagickFalse)
1345  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1346  status=IsRightsAuthorized(PathPolicyDomain,WritePolicyRights,filename);
1347  if (status == MagickFalse)
1348  {
1349  errno=EPERM;
1350  (void) ThrowMagickException(&image->exception,GetMagickModule(),
1351  PolicyError,"NotAuthorized","`%s'",filename);
1352  return(MagickFalse);
1353  }
1354  file=fileno(stdin);
1355  if (LocaleCompare(filename,"-") != 0)
1356  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1357  if (file == -1)
1358  {
1359  ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob",
1360  filename);
1361  return(MagickFalse);
1362  }
1363  quantum=(size_t) MagickMaxBufferExtent;
1364  if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1365  quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1366  blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1367  if (blob == (unsigned char *) NULL)
1368  {
1369  file=close(file);
1370  ThrowFileException(&image->exception,ResourceLimitError,
1371  "MemoryAllocationFailed",filename);
1372  return(MagickFalse);
1373  }
1374  for ( ; ; )
1375  {
1376  count=read(file,blob,quantum);
1377  if (count <= 0)
1378  {
1379  count=0;
1380  if (errno != EINTR)
1381  break;
1382  }
1383  length=(size_t) count;
1384  count=WriteBlobStream(image,length,blob);
1385  if (count != (ssize_t) length)
1386  {
1387  ThrowFileException(&image->exception,BlobError,"UnableToWriteBlob",
1388  filename);
1389  break;
1390  }
1391  }
1392  file=close(file);
1393  if (file == -1)
1394  ThrowFileException(&image->exception,BlobError,"UnableToWriteBlob",
1395  filename);
1396  blob=(unsigned char *) RelinquishMagickMemory(blob);
1397  return(MagickTrue);
1398 }
1399 
1400 /*
1401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402 % %
1403 % %
1404 % %
1405 + G e t B l o b E r r o r %
1406 % %
1407 % %
1408 % %
1409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 %
1411 % GetBlobError() returns MagickTrue if the blob associated with the specified
1412 % image encountered an error.
1413 %
1414 % The format of the GetBlobError method is:
1415 %
1416 % MagickBooleanType GetBlobError(const Image *image)
1417 %
1418 % A description of each parameter follows:
1419 %
1420 % o image: the image.
1421 %
1422 */
1423 MagickExport MagickBooleanType GetBlobError(const Image *image)
1424 {
1425  assert(image != (const Image *) NULL);
1426  assert(image->signature == MagickCoreSignature);
1427  if (IsEventLogging() != MagickFalse)
1428  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1429  if ((image->blob->status != MagickFalse) && (image->blob->error_number != 0))
1430  errno=image->blob->error_number;
1431  return(image->blob->status);
1432 }
1433 
1434 /*
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 % %
1437 % %
1438 % %
1439 + G e t B l o b F i l e H a n d l e %
1440 % %
1441 % %
1442 % %
1443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1444 %
1445 % GetBlobFileHandle() returns the file handle associated with the image blob.
1446 %
1447 % The format of the GetBlobFile method is:
1448 %
1449 % FILE *GetBlobFileHandle(const Image *image)
1450 %
1451 % A description of each parameter follows:
1452 %
1453 % o image: the image.
1454 %
1455 */
1456 MagickExport FILE *GetBlobFileHandle(const Image *image)
1457 {
1458  assert(image != (const Image *) NULL);
1459  assert(image->signature == MagickCoreSignature);
1460  return(image->blob->file_info.file);
1461 }
1462 
1463 /*
1464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 % %
1466 % %
1467 % %
1468 + G e t B l o b I n f o %
1469 % %
1470 % %
1471 % %
1472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1473 %
1474 % GetBlobInfo() initializes the BlobInfo structure.
1475 %
1476 % The format of the GetBlobInfo method is:
1477 %
1478 % void GetBlobInfo(BlobInfo *blob_info)
1479 %
1480 % A description of each parameter follows:
1481 %
1482 % o blob_info: Specifies a pointer to a BlobInfo structure.
1483 %
1484 */
1485 MagickExport void GetBlobInfo(BlobInfo *blob_info)
1486 {
1487  assert(blob_info != (BlobInfo *) NULL);
1488  (void) memset(blob_info,0,sizeof(*blob_info));
1489  blob_info->type=UndefinedStream;
1490  blob_info->quantum=(size_t) MagickMaxBlobExtent;
1491  blob_info->properties.st_mtime=GetMagickTime();
1492  blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1493  blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1494  blob_info->reference_count=1;
1495  blob_info->semaphore=AllocateSemaphoreInfo();
1496  blob_info->signature=MagickCoreSignature;
1497 }
1498 
1499 /*
1500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1501 % %
1502 % %
1503 % %
1504 % G e t B l o b P r o p e r t i e s %
1505 % %
1506 % %
1507 % %
1508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1509 %
1510 % GetBlobProperties() returns information about an image blob.
1511 %
1512 % The format of the GetBlobProperties method is:
1513 %
1514 % const struct stat *GetBlobProperties(const Image *image)
1515 %
1516 % A description of each parameter follows:
1517 %
1518 % o image: the image.
1519 %
1520 */
1521 MagickExport const struct stat *GetBlobProperties(const Image *image)
1522 {
1523  assert(image != (Image *) NULL);
1524  assert(image->signature == MagickCoreSignature);
1525  if (IsEventLogging() != MagickFalse)
1526  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1527  return(&image->blob->properties);
1528 }
1529 
1530 /*
1531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1532 % %
1533 % %
1534 % %
1535 + G e t B l o b S i z e %
1536 % %
1537 % %
1538 % %
1539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1540 %
1541 % GetBlobSize() returns the current length of the image file or blob; zero is
1542 % returned if the size cannot be determined.
1543 %
1544 % The format of the GetBlobSize method is:
1545 %
1546 % MagickSizeType GetBlobSize(const Image *image)
1547 %
1548 % A description of each parameter follows:
1549 %
1550 % o image: the image.
1551 %
1552 */
1553 MagickExport MagickSizeType GetBlobSize(const Image *image)
1554 {
1555  BlobInfo
1556  *magick_restrict blob_info;
1557 
1558  MagickSizeType
1559  extent;
1560 
1561  assert(image != (Image *) NULL);
1562  assert(image->signature == MagickCoreSignature);
1563  assert(image->blob != (BlobInfo *) NULL);
1564  if (IsEventLogging() != MagickFalse)
1565  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1566  blob_info=image->blob;
1567  extent=0;
1568  switch (blob_info->type)
1569  {
1570  case UndefinedStream:
1571  case StandardStream:
1572  {
1573  extent=blob_info->size;
1574  break;
1575  }
1576  case FileStream:
1577  {
1578  int
1579  file_descriptor;
1580 
1581  extent=(MagickSizeType) blob_info->properties.st_size;
1582  if (extent == 0)
1583  extent=blob_info->size;
1584  file_descriptor=fileno(blob_info->file_info.file);
1585  if (file_descriptor == -1)
1586  break;
1587  if (fstat(file_descriptor,&blob_info->properties) == 0)
1588  extent=(MagickSizeType) blob_info->properties.st_size;
1589  break;
1590  }
1591  case PipeStream:
1592  {
1593  extent=blob_info->size;
1594  break;
1595  }
1596  case ZipStream:
1597  case BZipStream:
1598  {
1599  MagickBooleanType
1600  status;
1601 
1602  status=GetPathAttributes(image->filename,&blob_info->properties);
1603  if (status != MagickFalse)
1604  extent=(MagickSizeType) blob_info->properties.st_size;
1605  break;
1606  }
1607  case FifoStream:
1608  break;
1609  case BlobStream:
1610  {
1611  extent=(MagickSizeType) blob_info->length;
1612  break;
1613  }
1614  }
1615  return(extent);
1616 }
1617 
1618 /*
1619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1620 % %
1621 % %
1622 % %
1623 + G e t B l o b S t r e a m D a t a %
1624 % %
1625 % %
1626 % %
1627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1628 %
1629 % GetBlobStreamData() returns the stream data for the image.
1630 %
1631 % The format of the GetBlobStreamData method is:
1632 %
1633 % unsigned char *GetBlobStreamData(const Image *image)
1634 %
1635 % A description of each parameter follows:
1636 %
1637 % o image: the image.
1638 %
1639 */
1640 MagickExport unsigned char *GetBlobStreamData(const Image *image)
1641 {
1642  assert(image != (const Image *) NULL);
1643  assert(image->signature == MagickCoreSignature);
1644  return(image->blob->data);
1645 }
1646 
1647 /*
1648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1649 % %
1650 % %
1651 % %
1652 + G e t B l o b S t r e a m H a n d l e r %
1653 % %
1654 % %
1655 % %
1656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1657 %
1658 % GetBlobStreamHandler() returns the stream handler for the image.
1659 %
1660 % The format of the GetBlobStreamHandler method is:
1661 %
1662 % StreamHandler GetBlobStreamHandler(const Image *image)
1663 %
1664 % A description of each parameter follows:
1665 %
1666 % o image: the image.
1667 %
1668 */
1669 MagickExport StreamHandler GetBlobStreamHandler(const Image *image)
1670 {
1671  assert(image != (const Image *) NULL);
1672  assert(image->signature == MagickCoreSignature);
1673  if (IsEventLogging() != MagickFalse)
1674  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1675  return(image->blob->stream);
1676 }
1677 
1678 /*
1679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1680 % %
1681 % %
1682 % %
1683 % I m a g e T o B l o b %
1684 % %
1685 % %
1686 % %
1687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1688 %
1689 % ImageToBlob() implements direct to memory image formats. It returns the
1690 % image as a formatted blob and its length. The magick member of the Image
1691 % structure determines the format of the returned blob (GIF, JPEG, PNG,
1692 % etc.). This method is the equivalent of WriteImage(), but writes the
1693 % formatted "file" to a memory buffer rather than to an actual file.
1694 %
1695 % The format of the ImageToBlob method is:
1696 %
1697 % unsigned char *ImageToBlob(const ImageInfo *image_info,Image *image,
1698 % size_t *length,ExceptionInfo *exception)
1699 %
1700 % A description of each parameter follows:
1701 %
1702 % o image_info: the image info.
1703 %
1704 % o image: the image.
1705 %
1706 % o length: return the actual length of the blob.
1707 %
1708 % o exception: return any errors or warnings in this structure.
1709 %
1710 */
1711 MagickExport unsigned char *ImageToBlob(const ImageInfo *image_info,
1712  Image *image,size_t *length,ExceptionInfo *exception)
1713 {
1714  const MagickInfo
1715  *magick_info;
1716 
1717  ImageInfo
1718  *blob_info;
1719 
1720  MagickBooleanType
1721  status;
1722 
1723  unsigned char
1724  *blob;
1725 
1726  assert(image_info != (const ImageInfo *) NULL);
1727  assert(image_info->signature == MagickCoreSignature);
1728  assert(image != (Image *) NULL);
1729  assert(image->signature == MagickCoreSignature);
1730  assert(exception != (ExceptionInfo *) NULL);
1731  assert(exception->signature == MagickCoreSignature);
1732  if (IsEventLogging() != MagickFalse)
1733  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1734  image_info->filename);
1735  *length=0;
1736  blob=(unsigned char *) NULL;
1737  blob_info=CloneImageInfo(image_info);
1738  blob_info->adjoin=MagickFalse;
1739  (void) SetImageInfo(blob_info,1,exception);
1740  if (*blob_info->magick != '\0')
1741  (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
1742  magick_info=GetMagickInfo(image->magick,exception);
1743  if (magick_info == (const MagickInfo *) NULL)
1744  {
1745  (void) ThrowMagickException(exception,GetMagickModule(),
1746  MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
1747  image->magick);
1748  blob_info=DestroyImageInfo(blob_info);
1749  return(blob);
1750  }
1751  (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
1752  if (GetMagickBlobSupport(magick_info) != MagickFalse)
1753  {
1754  /*
1755  Native blob support for this image format.
1756  */
1757  blob_info->length=0;
1758  blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1759  sizeof(unsigned char));
1760  if (blob_info->blob == NULL)
1761  (void) ThrowMagickException(exception,GetMagickModule(),
1762  ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1763  else
1764  {
1765  (void) CloseBlob(image);
1766  image->blob->exempt=MagickTrue;
1767  *image->filename='\0';
1768  status=WriteImage(blob_info,image);
1769  InheritException(exception,&image->exception);
1770  *length=image->blob->length;
1771  blob=DetachBlob(image->blob);
1772  if (blob != (void *) NULL)
1773  {
1774  if (status == MagickFalse)
1775  blob=RelinquishMagickMemory(blob);
1776  else
1777  blob=ResizeQuantumMemory(blob,*length+1,sizeof(unsigned char));
1778  }
1779  }
1780  }
1781  else
1782  {
1783  char
1784  unique[MagickPathExtent];
1785 
1786  int
1787  file;
1788 
1789  /*
1790  Write file to disk in blob image format.
1791  */
1792  file=AcquireUniqueFileResource(unique);
1793  if (file == -1)
1794  {
1795  ThrowFileException(exception,BlobError,"UnableToWriteBlob",
1796  image_info->filename);
1797  }
1798  else
1799  {
1800  blob_info->file=fdopen(file,"wb");
1801  if (blob_info->file != (FILE *) NULL)
1802  {
1803  (void) FormatLocaleString(image->filename,MagickPathExtent,
1804  "%s:%s",image->magick,unique);
1805  status=WriteImage(blob_info,image);
1806  (void) CloseBlob(image);
1807  (void) fclose(blob_info->file);
1808  if (status == MagickFalse)
1809  InheritException(exception,&image->exception);
1810  else
1811  blob=FileToBlob(unique,~0UL,length,exception);
1812  }
1813  (void) RelinquishUniqueFileResource(unique);
1814  }
1815  }
1816  blob_info=DestroyImageInfo(blob_info);
1817  return(blob);
1818 }
1819 
1820 /*
1821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1822 % %
1823 % %
1824 % %
1825 % I m a g e T o F i l e %
1826 % %
1827 % %
1828 % %
1829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1830 %
1831 % ImageToFile() writes an image to a file. It returns MagickFalse if an error
1832 % occurs otherwise MagickTrue.
1833 %
1834 % The format of the ImageToFile method is:
1835 %
1836 % MagickBooleanType ImageToFile(Image *image,char *filename,
1837 % ExceptionInfo *exception)
1838 %
1839 % A description of each parameter follows:
1840 %
1841 % o image: the image.
1842 %
1843 % o filename: Write the image to this file.
1844 %
1845 % o exception: return any errors or warnings in this structure.
1846 %
1847 */
1848 MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
1849  ExceptionInfo *exception)
1850 {
1851  int
1852  file;
1853 
1854  const unsigned char
1855  *p;
1856 
1857  size_t
1858  i;
1859 
1860  size_t
1861  length,
1862  quantum;
1863 
1864  ssize_t
1865  count;
1866 
1867  struct stat
1868  file_stats;
1869 
1870  unsigned char
1871  *buffer;
1872 
1873  assert(image != (Image *) NULL);
1874  assert(image->signature == MagickCoreSignature);
1875  assert(image->blob != (BlobInfo *) NULL);
1876  assert(image->blob->type != UndefinedStream);
1877  assert(filename != (const char *) NULL);
1878  if (IsEventLogging() != MagickFalse)
1879  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1880  if (*filename == '\0')
1881  file=AcquireUniqueFileResource(filename);
1882  else
1883  if (LocaleCompare(filename,"-") == 0)
1884  file=fileno(stdout);
1885  else
1886  file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1887  if (file == -1)
1888  {
1889  ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1890  return(MagickFalse);
1891  }
1892  quantum=(size_t) MagickMaxBufferExtent;
1893  if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1894  quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1895  buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1896  if (buffer == (unsigned char *) NULL)
1897  {
1898  file=close(file)-1;
1899  (void) ThrowMagickException(exception,GetMagickModule(),
1900  ResourceLimitError,"MemoryAllocationError","`%s'",filename);
1901  return(MagickFalse);
1902  }
1903  length=0;
1904  p=(const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1905  for (i=0; count > 0; )
1906  {
1907  length=(size_t) count;
1908  for (i=0; i < length; i+=count)
1909  {
1910  count=write(file,p+i,(size_t) (length-i));
1911  if (count <= 0)
1912  {
1913  count=0;
1914  if (errno != EINTR)
1915  break;
1916  }
1917  }
1918  if (i < length)
1919  break;
1920  p=(const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1921  }
1922  if (LocaleCompare(filename,"-") != 0)
1923  file=close(file);
1924  buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1925  if ((file == -1) || (i < length))
1926  {
1927  if (file != -1)
1928  file=close(file);
1929  ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1930  return(MagickFalse);
1931  }
1932  return(MagickTrue);
1933 }
1934 
1935 /*
1936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1937 % %
1938 % %
1939 % %
1940 % I m a g e s T o B l o b %
1941 % %
1942 % %
1943 % %
1944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1945 %
1946 % ImagesToBlob() implements direct to memory image formats. It returns the
1947 % image sequence as a blob and its length. The magick member of the ImageInfo
1948 % structure determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1949 %
1950 % Note, some image formats do not permit multiple images to the same image
1951 % stream (e.g. JPEG). in this instance, just the first image of the
1952 % sequence is returned as a blob.
1953 %
1954 % The format of the ImagesToBlob method is:
1955 %
1956 % unsigned char *ImagesToBlob(const ImageInfo *image_info,Image *images,
1957 % size_t *length,ExceptionInfo *exception)
1958 %
1959 % A description of each parameter follows:
1960 %
1961 % o image_info: the image info.
1962 %
1963 % o images: the image list.
1964 %
1965 % o length: return the actual length of the blob.
1966 %
1967 % o exception: return any errors or warnings in this structure.
1968 %
1969 */
1970 MagickExport unsigned char *ImagesToBlob(const ImageInfo *image_info,
1971  Image *images,size_t *length,ExceptionInfo *exception)
1972 {
1973  const MagickInfo
1974  *magick_info;
1975 
1976  ImageInfo
1977  *blob_info;
1978 
1979  MagickBooleanType
1980  status;
1981 
1982  unsigned char
1983  *blob;
1984 
1985  assert(image_info != (const ImageInfo *) NULL);
1986  assert(image_info->signature == MagickCoreSignature);
1987  assert(images != (Image *) NULL);
1988  assert(images->signature == MagickCoreSignature);
1989  assert(exception != (ExceptionInfo *) NULL);
1990  if (IsEventLogging() != MagickFalse)
1991  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1992  image_info->filename);
1993  *length=0;
1994  blob=(unsigned char *) NULL;
1995  blob_info=CloneImageInfo(image_info);
1996  (void) SetImageInfo(blob_info,(unsigned int) GetImageListLength(images),
1997  exception);
1998  if (*blob_info->magick != '\0')
1999  (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2000  magick_info=GetMagickInfo(images->magick,exception);
2001  if (magick_info == (const MagickInfo *) NULL)
2002  {
2003  (void) ThrowMagickException(exception,GetMagickModule(),
2004  MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
2005  images->magick);
2006  blob_info=DestroyImageInfo(blob_info);
2007  return(blob);
2008  }
2009  if (GetMagickAdjoin(magick_info) == MagickFalse)
2010  {
2011  blob_info=DestroyImageInfo(blob_info);
2012  return(ImageToBlob(image_info,images,length,exception));
2013  }
2014  (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2015  if (GetMagickBlobSupport(magick_info) != MagickFalse)
2016  {
2017  /*
2018  Native blob support for this images format.
2019  */
2020  blob_info->length=0;
2021  blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2022  sizeof(unsigned char));
2023  if (blob_info->blob == (void *) NULL)
2024  (void) ThrowMagickException(exception,GetMagickModule(),
2025  ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
2026  else
2027  {
2028  (void) CloseBlob(images);
2029  images->blob->exempt=MagickTrue;
2030  *images->filename='\0';
2031  status=WriteImages(blob_info,images,images->filename,exception);
2032  *length=images->blob->length;
2033  blob=DetachBlob(images->blob);
2034  if (blob != (void *) NULL)
2035  {
2036  if (status == MagickFalse)
2037  blob=RelinquishMagickMemory(blob);
2038  else
2039  blob=ResizeQuantumMemory(blob,*length+1,sizeof(unsigned char));
2040  }
2041  }
2042  }
2043  else
2044  {
2045  char
2046  filename[MagickPathExtent],
2047  unique[MagickPathExtent];
2048 
2049  int
2050  file;
2051 
2052  /*
2053  Write file to disk in blob images format.
2054  */
2055  file=AcquireUniqueFileResource(unique);
2056  if (file == -1)
2057  {
2058  ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
2059  image_info->filename);
2060  }
2061  else
2062  {
2063  blob_info->file=fdopen(file,"wb");
2064  if (blob_info->file != (FILE *) NULL)
2065  {
2066  (void) FormatLocaleString(filename,MagickPathExtent,"%s:%s",
2067  images->magick,unique);
2068  status=WriteImages(blob_info,images,filename,exception);
2069  (void) CloseBlob(images);
2070  (void) fclose(blob_info->file);
2071  if (status == MagickFalse)
2072  InheritException(exception,&images->exception);
2073  else
2074  blob=FileToBlob(unique,~0UL,length,exception);
2075  }
2076  (void) RelinquishUniqueFileResource(unique);
2077  }
2078  }
2079  blob_info=DestroyImageInfo(blob_info);
2080  return(blob);
2081 }
2082 /*
2083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2084 % %
2085 % %
2086 % %
2087 % I n j e c t I m a g e B l o b %
2088 % %
2089 % %
2090 % %
2091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2092 %
2093 % InjectImageBlob() injects the image with a copy of itself in the specified
2094 % format (e.g. inject JPEG into a PDF image).
2095 %
2096 % The format of the InjectImageBlob method is:
2097 %
2098 % MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
2099 % Image *image,Image *inject_image,const char *format,
2100 % ExceptionInfo *exception)
2101 %
2102 % A description of each parameter follows:
2103 %
2104 % o image_info: the image info..
2105 %
2106 % o image: the image.
2107 %
2108 % o inject_image: inject into the image stream.
2109 %
2110 % o format: the image format.
2111 %
2112 % o exception: return any errors or warnings in this structure.
2113 %
2114 */
2115 MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
2116  Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
2117 {
2118  char
2119  filename[MagickPathExtent];
2120 
2121  FILE
2122  *unique_file;
2123 
2124  Image
2125  *byte_image;
2126 
2127  ImageInfo
2128  *write_info;
2129 
2130  int
2131  file;
2132 
2133  MagickBooleanType
2134  status;
2135 
2136  size_t
2137  quantum;
2138 
2139  struct stat
2140  file_stats;
2141 
2142  unsigned char
2143  *buffer;
2144 
2145  /*
2146  Write inject image to a temporary file.
2147  */
2148  assert(image_info != (ImageInfo *) NULL);
2149  assert(image_info->signature == MagickCoreSignature);
2150  assert(image != (Image *) NULL);
2151  assert(image->signature == MagickCoreSignature);
2152  assert(inject_image != (Image *) NULL);
2153  assert(inject_image->signature == MagickCoreSignature);
2154  assert(exception != (ExceptionInfo *) NULL);
2155  if (IsEventLogging() != MagickFalse)
2156  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2157  unique_file=(FILE *) NULL;
2158  file=AcquireUniqueFileResource(filename);
2159  if (file != -1)
2160  unique_file=fdopen(file,"wb");
2161  if ((file == -1) || (unique_file == (FILE *) NULL))
2162  {
2163  (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2164  ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2165  image->filename);
2166  return(MagickFalse);
2167  }
2168  byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2169  if (byte_image == (Image *) NULL)
2170  {
2171  (void) fclose(unique_file);
2172  (void) RelinquishUniqueFileResource(filename);
2173  return(MagickFalse);
2174  }
2175  (void) FormatLocaleString(byte_image->filename,MagickPathExtent,"%s:%s",
2176  format,filename);
2177  DestroyBlob(byte_image);
2178  byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2179  write_info=CloneImageInfo(image_info);
2180  SetImageInfoFile(write_info,unique_file);
2181  status=WriteImage(write_info,byte_image);
2182  write_info=DestroyImageInfo(write_info);
2183  byte_image=DestroyImage(byte_image);
2184  (void) fclose(unique_file);
2185  if (status == MagickFalse)
2186  {
2187  (void) RelinquishUniqueFileResource(filename);
2188  return(MagickFalse);
2189  }
2190  /*
2191  Inject into image stream.
2192  */
2193  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2194  if (file == -1)
2195  {
2196  (void) RelinquishUniqueFileResource(filename);
2197  ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2198  image_info->filename);
2199  return(MagickFalse);
2200  }
2201  quantum=(size_t) MagickMaxBufferExtent;
2202  if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2203  quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2204  buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
2205  if (buffer == (unsigned char *) NULL)
2206  {
2207  (void) RelinquishUniqueFileResource(filename);
2208  file=close(file);
2209  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2210  image->filename);
2211  }
2212  for ( ; ; )
2213  {
2214  ssize_t count = read(file,buffer,quantum);
2215  if (count <= 0)
2216  {
2217  count=0;
2218  if (errno != EINTR)
2219  break;
2220  }
2221  status=WriteBlobStream(image,(size_t) count,buffer) == count ? MagickTrue :
2222  MagickFalse;
2223  }
2224  file=close(file);
2225  if (file == -1)
2226  ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",filename);
2227  (void) RelinquishUniqueFileResource(filename);
2228  buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2229  return(status);
2230 }
2231 
2232 /*
2233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2234 % %
2235 % %
2236 % %
2237 % I s B l o b E x e m p t %
2238 % %
2239 % %
2240 % %
2241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2242 %
2243 % IsBlobExempt() returns true if the blob is exempt.
2244 %
2245 % The format of the IsBlobExempt method is:
2246 %
2247 % MagickBooleanType IsBlobExempt(const Image *image)
2248 %
2249 % A description of each parameter follows:
2250 %
2251 % o image: the image.
2252 %
2253 */
2254 MagickExport MagickBooleanType IsBlobExempt(const Image *image)
2255 {
2256  assert(image != (const Image *) NULL);
2257  assert(image->signature == MagickCoreSignature);
2258  if (IsEventLogging() != MagickFalse)
2259  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2260  return(image->blob->exempt);
2261 }
2262 
2263 /*
2264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2265 % %
2266 % %
2267 % %
2268 + I s B l o b S e e k a b l e %
2269 % %
2270 % %
2271 % %
2272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2273 %
2274 % IsBlobSeekable() returns true if the blob is seekable.
2275 %
2276 % The format of the IsBlobSeekable method is:
2277 %
2278 % MagickBooleanType IsBlobSeekable(const Image *image)
2279 %
2280 % A description of each parameter follows:
2281 %
2282 % o image: the image.
2283 %
2284 */
2285 MagickExport MagickBooleanType IsBlobSeekable(const Image *image)
2286 {
2287  BlobInfo
2288  *magick_restrict blob_info;
2289 
2290  assert(image != (const Image *) NULL);
2291  assert(image->signature == MagickCoreSignature);
2292  if (IsEventLogging() != MagickFalse)
2293  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2294  blob_info=image->blob;
2295  switch (blob_info->type)
2296  {
2297  case BlobStream:
2298  return(MagickTrue);
2299  case FileStream:
2300  {
2301  int
2302  status;
2303 
2304  if (blob_info->file_info.file == (FILE *) NULL)
2305  return(MagickFalse);
2306  status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2307  return(status == -1 ? MagickFalse : MagickTrue);
2308  }
2309  case ZipStream:
2310  {
2311 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2312  MagickOffsetType
2313  offset;
2314 
2315  if (blob_info->file_info.gzfile == (gzFile) NULL)
2316  return(MagickFalse);
2317  offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2318  return(offset < 0 ? MagickFalse : MagickTrue);
2319 #else
2320  break;
2321 #endif
2322  }
2323  case UndefinedStream:
2324  case BZipStream:
2325  case FifoStream:
2326  case PipeStream:
2327  case StandardStream:
2328  break;
2329  default:
2330  break;
2331  }
2332  return(MagickFalse);
2333 }
2334 
2335 /*
2336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2337 % %
2338 % %
2339 % %
2340 % I s B l o b T e m p o r a r y %
2341 % %
2342 % %
2343 % %
2344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2345 %
2346 % IsBlobTemporary() returns true if the blob is temporary.
2347 %
2348 % The format of the IsBlobTemporary method is:
2349 %
2350 % MagickBooleanType IsBlobTemporary(const Image *image)
2351 %
2352 % A description of each parameter follows:
2353 %
2354 % o image: the image.
2355 %
2356 */
2357 MagickExport MagickBooleanType IsBlobTemporary(const Image *image)
2358 {
2359  assert(image != (const Image *) NULL);
2360  assert(image->signature == MagickCoreSignature);
2361  if (IsEventLogging() != MagickFalse)
2362  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2363  return(image->blob->temporary);
2364 }
2365 
2366 /*
2367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2368 % %
2369 % %
2370 % %
2371 + M a p B l o b %
2372 % %
2373 % %
2374 % %
2375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2376 %
2377 % MapBlob() creates a mapping from a file to a binary large object.
2378 %
2379 % The format of the MapBlob method is:
2380 %
2381 % unsigned char *MapBlob(int file,const MapMode mode,
2382 % const MagickOffsetType offset,const size_t length)
2383 %
2384 % A description of each parameter follows:
2385 %
2386 % o file: map this file descriptor.
2387 %
2388 % o mode: ReadMode, WriteMode, or IOMode.
2389 %
2390 % o offset: starting at this offset within the file.
2391 %
2392 % o length: the length of the mapping is returned in this pointer.
2393 %
2394 */
2395 MagickExport unsigned char *MapBlob(int file,const MapMode mode,
2396  const MagickOffsetType offset,const size_t length)
2397 {
2398 #if defined(MAGICKCORE_HAVE_MMAP)
2399  int
2400  flags,
2401  protection;
2402 
2403  unsigned char
2404  *map;
2405 
2406  /*
2407  Map file.
2408  */
2409  flags=0;
2410  if (file == -1)
2411 #if defined(MAP_ANONYMOUS)
2412  flags|=MAP_ANONYMOUS;
2413 #else
2414  return((unsigned char *) NULL);
2415 #endif
2416  switch (mode)
2417  {
2418  case ReadMode:
2419  default:
2420  {
2421  protection=PROT_READ;
2422  flags|=MAP_PRIVATE;
2423  break;
2424  }
2425  case WriteMode:
2426  {
2427  protection=PROT_WRITE;
2428  flags|=MAP_SHARED;
2429  break;
2430  }
2431  case IOMode:
2432  {
2433  protection=PROT_READ | PROT_WRITE;
2434  flags|=MAP_SHARED;
2435  break;
2436  }
2437  }
2438 #if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2439  map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,offset);
2440 #else
2441  map=(unsigned char *) mmap((char *) NULL,length,protection,flags |
2442  MAP_HUGETLB,file,offset);
2443  if (map == (unsigned char *) MAP_FAILED)
2444  map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2445  offset);
2446 #endif
2447  if (map == (unsigned char *) MAP_FAILED)
2448  return((unsigned char *) NULL);
2449  return(map);
2450 #else
2451  (void) file;
2452  (void) mode;
2453  (void) offset;
2454  (void) length;
2455  return((unsigned char *) NULL);
2456 #endif
2457 }
2458 
2459 /*
2460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2461 % %
2462 % %
2463 % %
2464 + M S B O r d e r L o n g %
2465 % %
2466 % %
2467 % %
2468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2469 %
2470 % MSBOrderLong() converts a least-significant byte first buffer of integers to
2471 % most-significant byte first.
2472 %
2473 % The format of the MSBOrderLong method is:
2474 %
2475 % void MSBOrderLong(unsigned char *buffer,const size_t length)
2476 %
2477 % A description of each parameter follows.
2478 %
2479 % o buffer: Specifies a pointer to a buffer of integers.
2480 %
2481 % o length: Specifies the length of the buffer.
2482 %
2483 */
2484 MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
2485 {
2486  int
2487  c;
2488 
2489  unsigned char
2490  *p,
2491  *q;
2492 
2493  assert(buffer != (unsigned char *) NULL);
2494  q=buffer+length;
2495  while (buffer < q)
2496  {
2497  p=buffer+3;
2498  c=(int) (*p);
2499  *p=(*buffer);
2500  *buffer++=(unsigned char) c;
2501  p=buffer+1;
2502  c=(int) (*p);
2503  *p=(*buffer);
2504  *buffer++=(unsigned char) c;
2505  buffer+=2;
2506  }
2507 }
2508 
2509 /*
2510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2511 % %
2512 % %
2513 % %
2514 + M S B O r d e r S h o r t %
2515 % %
2516 % %
2517 % %
2518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2519 %
2520 % MSBOrderShort() converts a least-significant byte first buffer of integers
2521 % to most-significant byte first.
2522 %
2523 % The format of the MSBOrderShort method is:
2524 %
2525 % void MSBOrderShort(unsigned char *p,const size_t length)
2526 %
2527 % A description of each parameter follows.
2528 %
2529 % o p: Specifies a pointer to a buffer of integers.
2530 %
2531 % o length: Specifies the length of the buffer.
2532 %
2533 */
2534 MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
2535 {
2536  int
2537  c;
2538 
2539  unsigned char
2540  *q;
2541 
2542  assert(p != (unsigned char *) NULL);
2543  q=p+length;
2544  while (p < q)
2545  {
2546  c=(int) (*p);
2547  *p=(*(p+1));
2548  p++;
2549  *p++=(unsigned char) c;
2550  }
2551 }
2552 
2553 /*
2554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2555 % %
2556 % %
2557 % %
2558 + O p e n B l o b %
2559 % %
2560 % %
2561 % %
2562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2563 %
2564 % OpenBlob() opens a file associated with the image. A file name of '-' sets
2565 % the file to stdin for type 'r' and stdout for type 'w'. If the filename
2566 % suffix is '.gz' or '.Z', the image is decompressed for type 'r' and
2567 % compressed for type 'w'. If the filename prefix is '|', it is piped to or
2568 % from a system command.
2569 %
2570 % The format of the OpenBlob method is:
2571 %
2572 % MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
2573 % const BlobMode mode,ExceptionInfo *exception)
2574 %
2575 % A description of each parameter follows:
2576 %
2577 % o image_info: the image info.
2578 %
2579 % o image: the image.
2580 %
2581 % o mode: the mode for opening the file.
2582 %
2583 */
2584 
2585 static inline MagickBooleanType SetStreamBuffering(const ImageInfo *image_info,
2586  Image *image)
2587 {
2588  const char
2589  *option;
2590 
2591  int
2592  status;
2593 
2594  size_t
2595  size;
2596 
2597  size=MagickMinBufferExtent;
2598  option=GetImageOption(image_info,"stream:buffer-size");
2599  if (option != (const char *) NULL)
2600  size=StringToUnsignedLong(option);
2601  status=setvbuf(image->blob->file_info.file,(char *) NULL,size == 0 ?
2602  _IONBF : _IOFBF,size);
2603  return(status == 0 ? MagickTrue : MagickFalse);
2604 }
2605 
2606 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2607 static inline gzFile gzopen_utf8(const char *path,const char *mode)
2608 {
2609 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2610  return(gzopen(path,mode));
2611 #else
2612  gzFile
2613  file;
2614 
2615  wchar_t
2616  *path_wide;
2617 
2618  path_wide=create_wchar_path(path);
2619  if (path_wide == (wchar_t *) NULL)
2620  return((gzFile) NULL);
2621  file=gzopen_w(path_wide,mode);
2622  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
2623  return(file);
2624 #endif
2625 }
2626 #endif
2627 
2628 MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
2629  Image *image,const BlobMode mode,ExceptionInfo *exception)
2630 {
2631  BlobInfo
2632  *magick_restrict blob_info;
2633 
2634  char
2635  extension[MagickPathExtent],
2636  filename[MagickPathExtent];
2637 
2638  const char
2639  *type;
2640 
2641  MagickBooleanType
2642  status;
2643 
2644  PolicyRights
2645  rights;
2646 
2647  assert(image_info != (ImageInfo *) NULL);
2648  assert(image_info->signature == MagickCoreSignature);
2649  assert(image != (Image *) NULL);
2650  assert(image->signature == MagickCoreSignature);
2651  if (IsEventLogging() != MagickFalse)
2652  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2653  image_info->filename);
2654  blob_info=image->blob;
2655  if (image_info->blob != (void *) NULL)
2656  {
2657  if (image_info->stream != (StreamHandler) NULL)
2658  blob_info->stream=(StreamHandler) image_info->stream;
2659  AttachBlob(blob_info,image_info->blob,image_info->length);
2660  return(MagickTrue);
2661  }
2662  (void) DetachBlob(blob_info);
2663  blob_info->mode=mode;
2664  switch (mode)
2665  {
2666  default: type="r"; break;
2667  case ReadBlobMode: type="r"; break;
2668  case ReadBinaryBlobMode: type="rb"; break;
2669  case WriteBlobMode: type="w"; break;
2670  case WriteBinaryBlobMode: type="w+b"; break;
2671  case AppendBlobMode: type="a"; break;
2672  case AppendBinaryBlobMode: type="a+b"; break;
2673  }
2674  if (*type != 'r')
2675  blob_info->synchronize=image_info->synchronize;
2676  if (image_info->stream != (StreamHandler) NULL)
2677  {
2678  blob_info->stream=(StreamHandler) image_info->stream;
2679  if (*type == 'w')
2680  {
2681  blob_info->type=FifoStream;
2682  return(MagickTrue);
2683  }
2684  }
2685  /*
2686  Open image file.
2687  */
2688  *filename='\0';
2689  (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2690  rights=ReadPolicyRights;
2691  if (*type == 'w')
2692  rights=WritePolicyRights;
2693  if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2694  {
2695  errno=EPERM;
2696  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2697  "NotAuthorized","`%s'",filename);
2698  return(MagickFalse);
2699  }
2700  if ((LocaleCompare(filename,"-") == 0) ||
2701  ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
2702  {
2703  blob_info->file_info.file=(*type == 'r') ? stdin : stdout;
2704 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2705  if (strchr(type,'b') != (char *) NULL)
2706  (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2707 #endif
2708  blob_info->type=StandardStream;
2709  blob_info->exempt=MagickTrue;
2710  return(SetStreamBuffering(image_info,image));
2711  }
2712  if ((LocaleNCompare(filename,"fd:",3) == 0) &&
2713  (IsGeometry(filename+3) != MagickFalse))
2714  {
2715  char
2716  fileMode[MagickPathExtent];
2717 
2718  *fileMode=(*type);
2719  fileMode[1]='\0';
2720  blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2721  if (blob_info->file_info.file == (FILE *) NULL)
2722  {
2723  ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2724  return(MagickFalse);
2725  }
2726 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2727  if (strchr(type,'b') != (char *) NULL)
2728  (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2729 #endif
2730  blob_info->type=FileStream;
2731  blob_info->exempt=MagickTrue;
2732  return(SetStreamBuffering(image_info,image));
2733  }
2734 #if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2735  if (*filename == '|')
2736  {
2737  char
2738  fileMode[MagickPathExtent],
2739  *sanitize_command;
2740 
2741  /*
2742  Pipe image to or from a system command.
2743  */
2744 #if defined(SIGPIPE)
2745  if (*type == 'w')
2746  (void) signal(SIGPIPE,SIG_IGN);
2747 #endif
2748  *fileMode=(*type);
2749  fileMode[1]='\0';
2750  sanitize_command=SanitizeString(filename+1);
2751  blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2752  fileMode);
2753  sanitize_command=DestroyString(sanitize_command);
2754  if (blob_info->file_info.file == (FILE *) NULL)
2755  {
2756  ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2757  return(MagickFalse);
2758  }
2759  blob_info->type=PipeStream;
2760  blob_info->exempt=MagickTrue;
2761  return(SetStreamBuffering(image_info,image));
2762  }
2763 #endif
2764  status=GetPathAttributes(filename,&blob_info->properties);
2765 #if defined(S_ISFIFO)
2766  if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2767  {
2768  blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2769  if (blob_info->file_info.file == (FILE *) NULL)
2770  {
2771  ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2772  return(MagickFalse);
2773  }
2774  blob_info->type=FileStream;
2775  blob_info->exempt=MagickTrue;
2776  return(SetStreamBuffering(image_info,image));
2777  }
2778 #endif
2779  GetPathComponent(image->filename,ExtensionPath,extension);
2780  if (*type == 'w')
2781  {
2782  (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2783  if ((image_info->adjoin == MagickFalse) ||
2784  (strchr(filename,'%') != (char *) NULL))
2785  {
2786  /*
2787  Form filename for multi-part images.
2788  */
2789  (void) InterpretImageFilename(image_info,image,image->filename,(int)
2790  image->scene,filename);
2791  if ((LocaleCompare(filename,image->filename) == 0) &&
2792  ((GetPreviousImageInList(image) != (Image *) NULL) ||
2793  (GetNextImageInList(image) != (Image *) NULL)))
2794  {
2795  char
2796  path[MagickPathExtent];
2797 
2798  GetPathComponent(image->filename,RootPath,path);
2799  if (*extension == '\0')
2800  (void) FormatLocaleString(filename,MagickPathExtent,"%s-%.20g",
2801  path,(double) image->scene);
2802  else
2803  (void) FormatLocaleString(filename,MagickPathExtent,
2804  "%s-%.20g.%s",path,(double) image->scene,extension);
2805  }
2806  (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2807 #if defined(macintosh)
2808  SetApplicationType(filename,image_info->magick,'8BIM');
2809 #endif
2810  }
2811  }
2812  if (image_info->file != (FILE *) NULL)
2813  {
2814  blob_info->file_info.file=image_info->file;
2815  blob_info->type=FileStream;
2816  blob_info->exempt=MagickTrue;
2817  }
2818  else
2819  if (*type == 'r')
2820  {
2821  blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2822  if (blob_info->file_info.file != (FILE *) NULL)
2823  {
2824  size_t
2825  count;
2826 
2827  unsigned char
2828  magick[3];
2829 
2830  blob_info->type=FileStream;
2831  (void) fstat(fileno(blob_info->file_info.file),
2832  &blob_info->properties);
2833  (void) SetStreamBuffering(image_info,image);
2834  (void) memset(magick,0,sizeof(magick));
2835  count=fread(magick,1,sizeof(magick),blob_info->file_info.file);
2836  (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2837 #if defined(MAGICKCORE_POSIX_SUPPORT)
2838  (void) fflush(blob_info->file_info.file);
2839 #endif
2840  (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2841  " read %.20g magic header bytes",(double) count);
2842 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2843  if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
2844  ((int) magick[2] == 0x08))
2845  {
2846  gzFile
2847  gzfile = gzopen_utf8(filename,"rb");
2848 
2849  if (gzfile != (gzFile) NULL)
2850  {
2851  if (blob_info->file_info.file != (FILE *) NULL)
2852  (void) fclose(blob_info->file_info.file);
2853  blob_info->file_info.file=(FILE *) NULL;
2854  blob_info->file_info.gzfile=gzfile;
2855  blob_info->type=ZipStream;
2856  }
2857  }
2858 #endif
2859 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2860  if (strncmp((char *) magick,"BZh",3) == 0)
2861  {
2862  BZFILE
2863  *bzfile = BZ2_bzopen(filename,"r");
2864 
2865  if (bzfile != (BZFILE *) NULL)
2866  {
2867  if (blob_info->file_info.file != (FILE *) NULL)
2868  (void) fclose(blob_info->file_info.file);
2869  blob_info->file_info.file=(FILE *) NULL;
2870  blob_info->file_info.bzfile=bzfile;
2871  blob_info->type=BZipStream;
2872  }
2873  }
2874 #endif
2875  if (blob_info->type == FileStream)
2876  {
2877  const MagickInfo
2878  *magick_info;
2879 
2881  *sans_exception;
2882 
2883  size_t
2884  length;
2885 
2886  sans_exception=AcquireExceptionInfo();
2887  magick_info=GetMagickInfo(image_info->magick,sans_exception);
2888  sans_exception=DestroyExceptionInfo(sans_exception);
2889  length=(size_t) blob_info->properties.st_size;
2890  if ((magick_info != (const MagickInfo *) NULL) &&
2891  (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2892  (length > MagickMaxBufferExtent) &&
2893  (AcquireMagickResource(MapResource,length) != MagickFalse))
2894  {
2895  void
2896  *blob;
2897 
2898  blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
2899  length);
2900  if (blob == (void *) NULL)
2901  RelinquishMagickResource(MapResource,length);
2902  else
2903  {
2904  /*
2905  Format supports blobs-- use memory-mapped I/O.
2906  */
2907  if (image_info->file != (FILE *) NULL)
2908  blob_info->exempt=MagickFalse;
2909  else
2910  {
2911  (void) fclose(blob_info->file_info.file);
2912  blob_info->file_info.file=(FILE *) NULL;
2913  }
2914  AttachBlob(blob_info,blob,length);
2915  blob_info->mapped=MagickTrue;
2916  }
2917  }
2918  }
2919  }
2920  }
2921  else
2922 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2923  if ((LocaleCompare(extension,"Z") == 0) ||
2924  (LocaleCompare(extension,"gz") == 0) ||
2925  (LocaleCompare(extension,"wmz") == 0) ||
2926  (LocaleCompare(extension,"svgz") == 0))
2927  {
2928  blob_info->file_info.gzfile=gzopen_utf8(filename,"wb");
2929  if (blob_info->file_info.gzfile != (gzFile) NULL)
2930  blob_info->type=ZipStream;
2931  }
2932  else
2933 #endif
2934 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2935  if (LocaleCompare(extension,"bz2") == 0)
2936  {
2937  if (mode == WriteBinaryBlobMode)
2938  type="w";
2939  blob_info->file_info.bzfile=BZ2_bzopen(filename,"w");
2940  if (blob_info->file_info.bzfile != (BZFILE *) NULL)
2941  blob_info->type=BZipStream;
2942  }
2943  else
2944 #endif
2945  {
2946  blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2947  if (blob_info->file_info.file != (FILE *) NULL)
2948  {
2949  blob_info->type=FileStream;
2950  (void) SetStreamBuffering(image_info,image);
2951  }
2952  }
2953  blob_info->status=MagickFalse;
2954  blob_info->error_number=0;
2955  if (blob_info->type != UndefinedStream)
2956  blob_info->size=GetBlobSize(image);
2957  else
2958  {
2959  ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2960  return(MagickFalse);
2961  }
2962  return(MagickTrue);
2963 }
2964 
2965 /*
2966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2967 % %
2968 % %
2969 % %
2970 + P i n g B l o b %
2971 % %
2972 % %
2973 % %
2974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2975 %
2976 % PingBlob() returns all the attributes of an image or image sequence except
2977 % for the pixels. It is much faster and consumes far less memory than
2978 % BlobToImage(). On failure, a NULL image is returned and exception
2979 % describes the reason for the failure.
2980 %
2981 % The format of the PingBlob method is:
2982 %
2983 % Image *PingBlob(const ImageInfo *image_info,const void *blob,
2984 % const size_t length,ExceptionInfo *exception)
2985 %
2986 % A description of each parameter follows:
2987 %
2988 % o image_info: the image info.
2989 %
2990 % o blob: the address of a character stream in one of the image formats
2991 % understood by ImageMagick.
2992 %
2993 % o length: This size_t integer reflects the length in bytes of the blob.
2994 %
2995 % o exception: return any errors or warnings in this structure.
2996 %
2997 */
2998 
2999 #if defined(__cplusplus) || defined(c_plusplus)
3000 extern "C" {
3001 #endif
3002 
3003 static size_t PingStream(const Image *magick_unused(image),
3004  const void *magick_unused(pixels),const size_t columns)
3005 {
3006  magick_unreferenced(image);
3007  magick_unreferenced(pixels);
3008 
3009  return(columns);
3010 }
3011 
3012 #if defined(__cplusplus) || defined(c_plusplus)
3013 }
3014 #endif
3015 
3016 MagickExport Image *PingBlob(const ImageInfo *image_info,const void *blob,
3017  const size_t length,ExceptionInfo *exception)
3018 {
3019  const MagickInfo
3020  *magick_info;
3021 
3022  Image
3023  *image;
3024 
3025  ImageInfo
3026  *clone_info,
3027  *ping_info;
3028 
3029  MagickBooleanType
3030  status;
3031 
3032  assert(image_info != (ImageInfo *) NULL);
3033  assert(image_info->signature == MagickCoreSignature);
3034  assert(exception != (ExceptionInfo *) NULL);
3035  if (IsEventLogging() != MagickFalse)
3036  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3037  image_info->filename);
3038  if ((blob == (const void *) NULL) || (length == 0))
3039  {
3040  (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3041  "ZeroLengthBlobNotPermitted","`%s'",image_info->filename);
3042  return((Image *) NULL);
3043  }
3044  ping_info=CloneImageInfo(image_info);
3045  ping_info->blob=(void *) blob;
3046  ping_info->length=length;
3047  ping_info->ping=MagickTrue;
3048  if (*ping_info->magick == '\0')
3049  (void) SetImageInfo(ping_info,0,exception);
3050  magick_info=GetMagickInfo(ping_info->magick,exception);
3051  if (magick_info == (const MagickInfo *) NULL)
3052  {
3053  (void) ThrowMagickException(exception,GetMagickModule(),
3054  MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
3055  ping_info->magick);
3056  ping_info=DestroyImageInfo(ping_info);
3057  return((Image *) NULL);
3058  }
3059  if (GetMagickBlobSupport(magick_info) != MagickFalse)
3060  {
3061  char
3062  filename[MagickPathExtent];
3063 
3064  /*
3065  Native blob support for this image format.
3066  */
3067  (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3068  (void) FormatLocaleString(ping_info->filename,MagickPathExtent,"%s:%s",
3069  ping_info->magick,filename);
3070  image=ReadStream(ping_info,&PingStream,exception);
3071  if (image != (Image *) NULL)
3072  (void) DetachBlob(image->blob);
3073  ping_info=DestroyImageInfo(ping_info);
3074  return(image);
3075  }
3076  /*
3077  Write blob to a temporary file on disk.
3078  */
3079  ping_info->blob=(void *) NULL;
3080  ping_info->length=0;
3081  *ping_info->filename='\0';
3082  status=BlobToFile(ping_info->filename,blob,length,exception);
3083  if (status == MagickFalse)
3084  {
3085  (void) RelinquishUniqueFileResource(ping_info->filename);
3086  ping_info=DestroyImageInfo(ping_info);
3087  return((Image *) NULL);
3088  }
3089  clone_info=CloneImageInfo(ping_info);
3090  (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:%s",
3091  ping_info->magick,ping_info->filename);
3092  image=ReadStream(clone_info,&PingStream,exception);
3093  if (image != (Image *) NULL)
3094  {
3095  Image
3096  *images;
3097 
3098  /*
3099  Restore original filenames and image format.
3100  */
3101  for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3102  {
3103  (void) CopyMagickString(images->filename,image_info->filename,
3104  MagickPathExtent);
3105  (void) CopyMagickString(images->magick_filename,image_info->filename,
3106  MagickPathExtent);
3107  (void) CopyMagickString(images->magick,magick_info->name,
3108  MagickPathExtent);
3109  images=GetNextImageInList(images);
3110  }
3111  }
3112  clone_info=DestroyImageInfo(clone_info);
3113  (void) RelinquishUniqueFileResource(ping_info->filename);
3114  ping_info=DestroyImageInfo(ping_info);
3115  return(image);
3116 }
3117 
3118 /*
3119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3120 % %
3121 % %
3122 % %
3123 + R e a d B l o b %
3124 % %
3125 % %
3126 % %
3127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3128 %
3129 % ReadBlob() reads data from the blob or image file and returns it. It
3130 % returns the number of bytes read. If length is zero, ReadBlob() returns
3131 % zero and has no other results. If length is greater than MAGICK_SSIZE_MAX, the
3132 % result is unspecified.
3133 %
3134 % The format of the ReadBlob method is:
3135 %
3136 % ssize_t ReadBlob(Image *image,const size_t length,unsigned char *data)
3137 %
3138 % A description of each parameter follows:
3139 %
3140 % o image: the image.
3141 %
3142 % o length: Specifies an integer representing the number of bytes to read
3143 % from the file.
3144 %
3145 % o data: Specifies an area to place the information requested from the
3146 % file.
3147 %
3148 */
3149 MagickExport ssize_t ReadBlob(Image *image,const size_t length,
3150  unsigned char *data)
3151 {
3152  BlobInfo
3153  *magick_restrict blob_info;
3154 
3155  int
3156  c;
3157 
3158  unsigned char
3159  *q;
3160 
3161  ssize_t
3162  count;
3163 
3164  assert(image != (Image *) NULL);
3165  assert(image->signature == MagickCoreSignature);
3166  assert(image->blob != (BlobInfo *) NULL);
3167  assert(image->blob->type != UndefinedStream);
3168  if (length == 0)
3169  return(0);
3170  assert(data != (void *) NULL);
3171  blob_info=image->blob;
3172  count=0;
3173  q=data;
3174  switch (blob_info->type)
3175  {
3176  case UndefinedStream:
3177  break;
3178  case StandardStream:
3179  case FileStream:
3180  case PipeStream:
3181  {
3182  switch (length)
3183  {
3184  default:
3185  {
3186  count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3187  break;
3188  }
3189  case 4:
3190  {
3191  c=getc(blob_info->file_info.file);
3192  if (c == EOF)
3193  break;
3194  *q++=(unsigned char) c;
3195  count++;
3196  }
3197  case 3:
3198  {
3199  c=getc(blob_info->file_info.file);
3200  if (c == EOF)
3201  break;
3202  *q++=(unsigned char) c;
3203  count++;
3204  }
3205  case 2:
3206  {
3207  c=getc(blob_info->file_info.file);
3208  if (c == EOF)
3209  break;
3210  *q++=(unsigned char) c;
3211  count++;
3212  }
3213  case 1:
3214  {
3215  c=getc(blob_info->file_info.file);
3216  if (c == EOF)
3217  break;
3218  *q++=(unsigned char) c;
3219  count++;
3220  }
3221  case 0:
3222  break;
3223  }
3224  if ((count != (ssize_t) length) &&
3225  (ferror(blob_info->file_info.file) != 0))
3226  ThrowBlobException(blob_info);
3227  break;
3228  }
3229  case ZipStream:
3230  {
3231 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3232  int
3233  status;
3234 
3235  switch (length)
3236  {
3237  default:
3238  {
3239  ssize_t
3240  i;
3241 
3242  for (i=0; i < (ssize_t) length; i+=count)
3243  {
3244  count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3245  (unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3246  if (count <= 0)
3247  {
3248  count=0;
3249  if (errno != EINTR)
3250  break;
3251  }
3252  }
3253  count=i;
3254  break;
3255  }
3256  case 4:
3257  {
3258  c=gzgetc(blob_info->file_info.gzfile);
3259  if (c == EOF)
3260  break;
3261  *q++=(unsigned char) c;
3262  count++;
3263  }
3264  case 3:
3265  {
3266  c=gzgetc(blob_info->file_info.gzfile);
3267  if (c == EOF)
3268  break;
3269  *q++=(unsigned char) c;
3270  count++;
3271  }
3272  case 2:
3273  {
3274  c=gzgetc(blob_info->file_info.gzfile);
3275  if (c == EOF)
3276  break;
3277  *q++=(unsigned char) c;
3278  count++;
3279  }
3280  case 1:
3281  {
3282  c=gzgetc(blob_info->file_info.gzfile);
3283  if (c == EOF)
3284  break;
3285  *q++=(unsigned char) c;
3286  count++;
3287  }
3288  case 0:
3289  break;
3290  }
3291  status=Z_OK;
3292  (void) gzerror(blob_info->file_info.gzfile,&status);
3293  if ((count != (ssize_t) length) && (status != Z_OK))
3294  ThrowBlobException(blob_info);
3295  if (blob_info->eof == MagickFalse)
3296  blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3297  MagickFalse;
3298 #endif
3299  break;
3300  }
3301  case BZipStream:
3302  {
3303 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3304  int
3305  status;
3306 
3307  ssize_t
3308  i;
3309 
3310  for (i=0; i < (ssize_t) length; i+=count)
3311  {
3312  count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3313  (unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3314  if (count <= 0)
3315  {
3316  count=0;
3317  if (errno != EINTR)
3318  break;
3319  }
3320  }
3321  count=i;
3322  status=BZ_OK;
3323  (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3324  if ((count != (ssize_t) length) && (status != BZ_OK))
3325  ThrowBlobException(blob_info);
3326 #endif
3327  break;
3328  }
3329  case FifoStream:
3330  break;
3331  case BlobStream:
3332  {
3333  const unsigned char
3334  *p;
3335 
3336  if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3337  {
3338  blob_info->eof=MagickTrue;
3339  break;
3340  }
3341  p=blob_info->data+blob_info->offset;
3342  count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3343  blob_info->length-blob_info->offset);
3344  blob_info->offset+=count;
3345  if (count != (ssize_t) length)
3346  blob_info->eof=MagickTrue;
3347  (void) memcpy(q,p,(size_t) count);
3348  break;
3349  }
3350  }
3351  return(count);
3352 }
3353 
3354 /*
3355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3356 % %
3357 % %
3358 % %
3359 + R e a d B l o b B y t e %
3360 % %
3361 % %
3362 % %
3363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3364 %
3365 % ReadBlobByte() reads a single byte from the image file and returns it.
3366 %
3367 % The format of the ReadBlobByte method is:
3368 %
3369 % int ReadBlobByte(Image *image)
3370 %
3371 % A description of each parameter follows.
3372 %
3373 % o image: the image.
3374 %
3375 */
3376 MagickExport int ReadBlobByte(Image *image)
3377 {
3378  BlobInfo
3379  *magick_restrict blob_info;
3380 
3381  const unsigned char
3382  *p;
3383 
3384  unsigned char
3385  buffer[1];
3386 
3387  assert(image != (Image *) NULL);
3388  assert(image->signature == MagickCoreSignature);
3389  assert(image->blob != (BlobInfo *) NULL);
3390  assert(image->blob->type != UndefinedStream);
3391  blob_info=image->blob;
3392  switch (blob_info->type)
3393  {
3394  case StandardStream:
3395  case FileStream:
3396  case PipeStream:
3397  {
3398  int
3399  c;
3400 
3401  p=(const unsigned char *) buffer;
3402  c=getc(blob_info->file_info.file);
3403  if (c == EOF)
3404  return(EOF);
3405  *buffer=(unsigned char) c;
3406  break;
3407  }
3408  default:
3409  {
3410  ssize_t
3411  count;
3412 
3413  p=(const unsigned char *) ReadBlobStream(image,1,buffer,&count);
3414  if (count != 1)
3415  return(EOF);
3416  break;
3417  }
3418  }
3419  return((int) (*p));
3420 }
3421 
3422 /*
3423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3424 % %
3425 % %
3426 % %
3427 + R e a d B l o b D o u b l e %
3428 % %
3429 % %
3430 % %
3431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3432 %
3433 % ReadBlobDouble() reads a double value as a 64-bit quantity in the byte-order
3434 % specified by the endian member of the image structure.
3435 %
3436 % The format of the ReadBlobDouble method is:
3437 %
3438 % double ReadBlobDouble(Image *image)
3439 %
3440 % A description of each parameter follows.
3441 %
3442 % o image: the image.
3443 %
3444 */
3445 MagickExport double ReadBlobDouble(Image *image)
3446 {
3447  union
3448  {
3449  MagickSizeType
3450  unsigned_value;
3451 
3452  double
3453  double_value;
3454  } quantum;
3455 
3456  quantum.double_value=0.0;
3457  quantum.unsigned_value=ReadBlobLongLong(image);
3458  return(quantum.double_value);
3459 }
3460 
3461 /*
3462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3463 % %
3464 % %
3465 % %
3466 + R e a d B l o b F l o a t %
3467 % %
3468 % %
3469 % %
3470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3471 %
3472 % ReadBlobFloat() reads a float value as a 32-bit quantity in the byte-order
3473 % specified by the endian member of the image structure.
3474 %
3475 % The format of the ReadBlobFloat method is:
3476 %
3477 % float ReadBlobFloat(Image *image)
3478 %
3479 % A description of each parameter follows.
3480 %
3481 % o image: the image.
3482 %
3483 */
3484 MagickExport float ReadBlobFloat(Image *image)
3485 {
3486  union
3487  {
3488  unsigned int
3489  unsigned_value;
3490 
3491  float
3492  float_value;
3493  } quantum;
3494 
3495  quantum.float_value=0.0;
3496  quantum.unsigned_value=ReadBlobLong(image);
3497  return(quantum.float_value);
3498 }
3499 
3500 /*
3501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3502 % %
3503 % %
3504 % %
3505 + R e a d B l o b L o n g %
3506 % %
3507 % %
3508 % %
3509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3510 %
3511 % ReadBlobLong() reads a unsigned int value as a 32-bit quantity in the
3512 % byte-order specified by the endian member of the image structure.
3513 %
3514 % The format of the ReadBlobLong method is:
3515 %
3516 % unsigned int ReadBlobLong(Image *image)
3517 %
3518 % A description of each parameter follows.
3519 %
3520 % o image: the image.
3521 %
3522 */
3523 MagickExport unsigned int ReadBlobLong(Image *image)
3524 {
3525  const unsigned char
3526  *p;
3527 
3528  ssize_t
3529  count;
3530 
3531  unsigned char
3532  buffer[4];
3533 
3534  unsigned int
3535  value;
3536 
3537  assert(image != (Image *) NULL);
3538  assert(image->signature == MagickCoreSignature);
3539  *buffer='\0';
3540  p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3541  if (count != 4)
3542  return(0UL);
3543  if (image->endian == LSBEndian)
3544  {
3545  value=(unsigned int) (*p++);
3546  value|=(unsigned int) (*p++) << 8;
3547  value|=(unsigned int) (*p++) << 16;
3548  value|=(unsigned int) (*p++) << 24;
3549  return(value);
3550  }
3551  value=(unsigned int) (*p++) << 24;
3552  value|=(unsigned int) (*p++) << 16;
3553  value|=(unsigned int) (*p++) << 8;
3554  value|=(unsigned int) (*p++);
3555  return(value);
3556 }
3557 
3558 /*
3559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3560 % %
3561 % %
3562 % %
3563 + R e a d B l o b L o n g L o n g %
3564 % %
3565 % %
3566 % %
3567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3568 %
3569 % ReadBlobLongLong() reads a long long value as a 64-bit quantity in the
3570 % byte-order specified by the endian member of the image structure.
3571 %
3572 % The format of the ReadBlobLongLong method is:
3573 %
3574 % MagickSizeType ReadBlobLongLong(Image *image)
3575 %
3576 % A description of each parameter follows.
3577 %
3578 % o image: the image.
3579 %
3580 */
3581 MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3582 {
3583  MagickSizeType
3584  value;
3585 
3586  const unsigned char
3587  *p;
3588 
3589  ssize_t
3590  count;
3591 
3592  unsigned char
3593  buffer[8];
3594 
3595  assert(image != (Image *) NULL);
3596  assert(image->signature == MagickCoreSignature);
3597  *buffer='\0';
3598  p=(const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3599  if (count != 8)
3600  return(MagickULLConstant(0));
3601  if (image->endian == LSBEndian)
3602  {
3603  value=(MagickSizeType) (*p++);
3604  value|=(MagickSizeType) (*p++) << 8;
3605  value|=(MagickSizeType) (*p++) << 16;
3606  value|=(MagickSizeType) (*p++) << 24;
3607  value|=(MagickSizeType) (*p++) << 32;
3608  value|=(MagickSizeType) (*p++) << 40;
3609  value|=(MagickSizeType) (*p++) << 48;
3610  value|=(MagickSizeType) (*p++) << 56;
3611  return(value);
3612  }
3613  value=(MagickSizeType) (*p++) << 56;
3614  value|=(MagickSizeType) (*p++) << 48;
3615  value|=(MagickSizeType) (*p++) << 40;
3616  value|=(MagickSizeType) (*p++) << 32;
3617  value|=(MagickSizeType) (*p++) << 24;
3618  value|=(MagickSizeType) (*p++) << 16;
3619  value|=(MagickSizeType) (*p++) << 8;
3620  value|=(MagickSizeType) (*p++);
3621  return(value);
3622 }
3623 
3624 /*
3625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3626 % %
3627 % %
3628 % %
3629 + R e a d B l o b S h o r t %
3630 % %
3631 % %
3632 % %
3633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3634 %
3635 % ReadBlobShort() reads a short value as a 16-bit quantity in the byte-order
3636 % specified by the endian member of the image structure.
3637 %
3638 % The format of the ReadBlobShort method is:
3639 %
3640 % unsigned short ReadBlobShort(Image *image)
3641 %
3642 % A description of each parameter follows.
3643 %
3644 % o image: the image.
3645 %
3646 */
3647 MagickExport unsigned short ReadBlobShort(Image *image)
3648 {
3649  const unsigned char
3650  *p;
3651 
3652  unsigned short
3653  value;
3654 
3655  ssize_t
3656  count;
3657 
3658  unsigned char
3659  buffer[2];
3660 
3661  assert(image != (Image *) NULL);
3662  assert(image->signature == MagickCoreSignature);
3663  *buffer='\0';
3664  p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3665  if (count != 2)
3666  return((unsigned short) 0U);
3667  if (image->endian == LSBEndian)
3668  {
3669  value=(unsigned short) (*p++);
3670  value|=(unsigned short) (*p++) << 8;
3671  return(value);
3672  }
3673  value=(unsigned short) ((unsigned short) (*p++) << 8);
3674  value|=(unsigned short) (*p++);
3675  return(value);
3676 }
3677 
3678 /*
3679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3680 % %
3681 % %
3682 % %
3683 + R e a d B l o b L S B L o n g %
3684 % %
3685 % %
3686 % %
3687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3688 %
3689 % ReadBlobLSBLong() reads a unsigned int value as a 32-bit quantity in
3690 % least-significant byte first order.
3691 %
3692 % The format of the ReadBlobLSBLong method is:
3693 %
3694 % unsigned int ReadBlobLSBLong(Image *image)
3695 %
3696 % A description of each parameter follows.
3697 %
3698 % o image: the image.
3699 %
3700 */
3701 MagickExport unsigned int ReadBlobLSBLong(Image *image)
3702 {
3703  const unsigned char
3704  *p;
3705 
3706  unsigned int
3707  value;
3708 
3709  ssize_t
3710  count;
3711 
3712  unsigned char
3713  buffer[4];
3714 
3715  assert(image != (Image *) NULL);
3716  assert(image->signature == MagickCoreSignature);
3717  *buffer='\0';
3718  p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3719  if (count != 4)
3720  return(0U);
3721  value=(unsigned int) (*p++);
3722  value|=(unsigned int) (*p++) << 8;
3723  value|=(unsigned int) (*p++) << 16;
3724  value|=(unsigned int) (*p++) << 24;
3725  return(value);
3726 }
3727 
3728 /*
3729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3730 % %
3731 % %
3732 % %
3733 + R e a d B l o b L S B S i g n e d L o n g %
3734 % %
3735 % %
3736 % %
3737 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3738 %
3739 % ReadBlobLSBSignedLong() reads a signed int value as a 32-bit quantity in
3740 % least-significant byte first order.
3741 %
3742 % The format of the ReadBlobLSBSignedLong method is:
3743 %
3744 % signed int ReadBlobLSBSignedLong(Image *image)
3745 %
3746 % A description of each parameter follows.
3747 %
3748 % o image: the image.
3749 %
3750 */
3751 MagickExport signed int ReadBlobLSBSignedLong(Image *image)
3752 {
3753  union
3754  {
3755  unsigned int
3756  unsigned_value;
3757 
3758  signed int
3759  signed_value;
3760  } quantum;
3761 
3762  quantum.unsigned_value=ReadBlobLSBLong(image);
3763  return(quantum.signed_value);
3764 }
3765 
3766 /*
3767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3768 % %
3769 % %
3770 % %
3771 + R e a d B l o b L S B S h o r t %
3772 % %
3773 % %
3774 % %
3775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3776 %
3777 % ReadBlobLSBShort() reads a short value as a 16-bit quantity in
3778 % least-significant byte first order.
3779 %
3780 % The format of the ReadBlobLSBShort method is:
3781 %
3782 % unsigned short ReadBlobLSBShort(Image *image)
3783 %
3784 % A description of each parameter follows.
3785 %
3786 % o image: the image.
3787 %
3788 */
3789 MagickExport unsigned short ReadBlobLSBShort(Image *image)
3790 {
3791  const unsigned char
3792  *p;
3793 
3794  unsigned short
3795  value;
3796 
3797  ssize_t
3798  count;
3799 
3800  unsigned char
3801  buffer[2];
3802 
3803  assert(image != (Image *) NULL);
3804  assert(image->signature == MagickCoreSignature);
3805  *buffer='\0';
3806  p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3807  if (count != 2)
3808  return((unsigned short) 0U);
3809  value=(unsigned short) (*p++);
3810  value|=(unsigned short) (*p++) << 8;
3811  return(value);
3812 }
3813 
3814 /*
3815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3816 % %
3817 % %
3818 % %
3819 + R e a d B l o b L S B S i g n e d S h o r t %
3820 % %
3821 % %
3822 % %
3823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3824 %
3825 % ReadBlobLSBSignedShort() reads a signed short value as a 16-bit quantity in
3826 % least-significant byte-order.
3827 %
3828 % The format of the ReadBlobLSBSignedShort method is:
3829 %
3830 % signed short ReadBlobLSBSignedShort(Image *image)
3831 %
3832 % A description of each parameter follows.
3833 %
3834 % o image: the image.
3835 %
3836 */
3837 MagickExport signed short ReadBlobLSBSignedShort(Image *image)
3838 {
3839  union
3840  {
3841  unsigned short
3842  unsigned_value;
3843 
3844  signed short
3845  signed_value;
3846  } quantum;
3847 
3848  quantum.unsigned_value=ReadBlobLSBShort(image);
3849  return(quantum.signed_value);
3850 }
3851 
3852 /*
3853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3854 % %
3855 % %
3856 % %
3857 + R e a d B l o b M S B L o n g %
3858 % %
3859 % %
3860 % %
3861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3862 %
3863 % ReadBlobMSBLong() reads a unsigned int value as a 32-bit quantity in
3864 % most-significant byte first order.
3865 %
3866 % The format of the ReadBlobMSBLong method is:
3867 %
3868 % unsigned int ReadBlobMSBLong(Image *image)
3869 %
3870 % A description of each parameter follows.
3871 %
3872 % o image: the image.
3873 %
3874 */
3875 MagickExport unsigned int ReadBlobMSBLong(Image *image)
3876 {
3877  const unsigned char
3878  *p;
3879 
3880  unsigned int
3881  value;
3882 
3883  ssize_t
3884  count;
3885 
3886  unsigned char
3887  buffer[4];
3888 
3889  assert(image != (Image *) NULL);
3890  assert(image->signature == MagickCoreSignature);
3891  *buffer='\0';
3892  p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3893  if (count != 4)
3894  return(0UL);
3895  value=(unsigned int) (*p++) << 24;
3896  value|=(unsigned int) (*p++) << 16;
3897  value|=(unsigned int) (*p++) << 8;
3898  value|=(unsigned int) (*p++);
3899  return(value);
3900 }
3901 
3902 /*
3903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3904 % %
3905 % %
3906 % %
3907 + R e a d B l o b M S B L o n g L o n g %
3908 % %
3909 % %
3910 % %
3911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3912 %
3913 % ReadBlobMSBLongLong() reads a unsigned long int value as a 64-bit quantity
3914 % in most-significant byte first order.
3915 %
3916 % The format of the ReadBlobMSBLongLong method is:
3917 %
3918 % unsigned int ReadBlobMSBLongLong(Image *image)
3919 %
3920 % A description of each parameter follows.
3921 %
3922 % o image: the image.
3923 %
3924 */
3925 MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
3926 {
3927  const unsigned char
3928  *p;
3929 
3930  MagickSizeType
3931  value;
3932 
3933  ssize_t
3934  count;
3935 
3936  unsigned char
3937  buffer[8];
3938 
3939  assert(image != (Image *) NULL);
3940  assert(image->signature == MagickCoreSignature);
3941  *buffer='\0';
3942  p=(const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3943  if (count != 8)
3944  return(MagickULLConstant(0));
3945  value=(MagickSizeType) (*p++) << 56;
3946  value|=(MagickSizeType) (*p++) << 48;
3947  value|=(MagickSizeType) (*p++) << 40;
3948  value|=(MagickSizeType) (*p++) << 32;
3949  value|=(MagickSizeType) (*p++) << 24;
3950  value|=(MagickSizeType) (*p++) << 16;
3951  value|=(MagickSizeType) (*p++) << 8;
3952  value|=(MagickSizeType) (*p++);
3953  return(value);
3954 }
3955 
3956 /*
3957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3958 % %
3959 % %
3960 % %
3961 + R e a d B l o b M S B S h o r t %
3962 % %
3963 % %
3964 % %
3965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3966 %
3967 % ReadBlobMSBShort() reads a short value as a 16-bit quantity in
3968 % most-significant byte first order.
3969 %
3970 % The format of the ReadBlobMSBShort method is:
3971 %
3972 % unsigned short ReadBlobMSBShort(Image *image)
3973 %
3974 % A description of each parameter follows.
3975 %
3976 % o image: the image.
3977 %
3978 */
3979 MagickExport unsigned short ReadBlobMSBShort(Image *image)
3980 {
3981  const unsigned char
3982  *p;
3983 
3984  unsigned short
3985  value;
3986 
3987  ssize_t
3988  count;
3989 
3990  unsigned char
3991  buffer[2];
3992 
3993  assert(image != (Image *) NULL);
3994  assert(image->signature == MagickCoreSignature);
3995  *buffer='\0';
3996  p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3997  if (count != 2)
3998  return((unsigned short) 0U);
3999  value=(unsigned short) ((*p++) << 8);
4000  value|=(unsigned short) (*p++);
4001  return(value);
4002 }
4003 
4004 /*
4005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4006 % %
4007 % %
4008 % %
4009 + R e a d B l o b M S B S i g n e d L o n g %
4010 % %
4011 % %
4012 % %
4013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4014 %
4015 % ReadBlobMSBSignedLong() reads a signed int value as a 32-bit quantity in
4016 % most-significant byte-order.
4017 %
4018 % The format of the ReadBlobMSBSignedLong method is:
4019 %
4020 % signed int ReadBlobMSBSignedLong(Image *image)
4021 %
4022 % A description of each parameter follows.
4023 %
4024 % o image: the image.
4025 %
4026 */
4027 MagickExport signed int ReadBlobMSBSignedLong(Image *image)
4028 {
4029  union
4030  {
4031  unsigned int
4032  unsigned_value;
4033 
4034  signed int
4035  signed_value;
4036  } quantum;
4037 
4038  quantum.unsigned_value=ReadBlobMSBLong(image);
4039  return(quantum.signed_value);
4040 }
4041 
4042 /*
4043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4044 % %
4045 % %
4046 % %
4047 + R e a d B l o b M S B S i g n e d S h o r t %
4048 % %
4049 % %
4050 % %
4051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4052 %
4053 % ReadBlobMSBSignedShort() reads a signed short value as a 16-bit quantity in
4054 % most-significant byte-order.
4055 %
4056 % The format of the ReadBlobMSBSignedShort method is:
4057 %
4058 % signed short ReadBlobMSBSignedShort(Image *image)
4059 %
4060 % A description of each parameter follows.
4061 %
4062 % o image: the image.
4063 %
4064 */
4065 MagickExport signed short ReadBlobMSBSignedShort(Image *image)
4066 {
4067  union
4068  {
4069  unsigned short
4070  unsigned_value;
4071 
4072  signed short
4073  signed_value;
4074  } quantum;
4075 
4076  quantum.unsigned_value=ReadBlobMSBShort(image);
4077  return(quantum.signed_value);
4078 }
4079 
4080 /*
4081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4082 % %
4083 % %
4084 % %
4085 + R e a d B l o b S i g n e d L o n g %
4086 % %
4087 % %
4088 % %
4089 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4090 %
4091 % ReadBlobSignedLong() reads a signed int value as a 32-bit quantity in the
4092 % byte-order specified by the endian member of the image structure.
4093 %
4094 % The format of the ReadBlobSignedLong method is:
4095 %
4096 % signed int ReadBlobSignedLong(Image *image)
4097 %
4098 % A description of each parameter follows.
4099 %
4100 % o image: the image.
4101 %
4102 */
4103 MagickExport signed int ReadBlobSignedLong(Image *image)
4104 {
4105  union
4106  {
4107  unsigned int
4108  unsigned_value;
4109 
4110  signed int
4111  signed_value;
4112  } quantum;
4113 
4114  quantum.unsigned_value=ReadBlobLong(image);
4115  return(quantum.signed_value);
4116 }
4117 
4118 /*
4119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4120 % %
4121 % %
4122 % %
4123 + R e a d B l o b S i g n e d S h o r t %
4124 % %
4125 % %
4126 % %
4127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4128 %
4129 % ReadBlobSignedShort() reads a signed short value as a 16-bit quantity in the
4130 % byte-order specified by the endian member of the image structure.
4131 %
4132 % The format of the ReadBlobSignedShort method is:
4133 %
4134 % signed short ReadBlobSignedShort(Image *image)
4135 %
4136 % A description of each parameter follows.
4137 %
4138 % o image: the image.
4139 %
4140 */
4141 MagickExport signed short ReadBlobSignedShort(Image *image)
4142 {
4143  union
4144  {
4145  unsigned short
4146  unsigned_value;
4147 
4148  signed short
4149  signed_value;
4150  } quantum;
4151 
4152  quantum.unsigned_value=ReadBlobShort(image);
4153  return(quantum.signed_value);
4154 }
4155 
4156 /*
4157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4158 % %
4159 % %
4160 % %
4161 + R e a d B l o b S t r e a m %
4162 % %
4163 % %
4164 % %
4165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4166 %
4167 % ReadBlobStream() reads data from the blob or image file and returns it. It
4168 % returns a pointer to the data buffer you supply or to the image memory
4169 % buffer if its supported (zero-copy). If length is zero, ReadBlobStream()
4170 % returns a count of zero and has no other results. If length is greater than
4171 % MAGICK_SSIZE_MAX, the result is unspecified.
4172 %
4173 % The format of the ReadBlobStream method is:
4174 %
4175 % const void *ReadBlobStream(Image *image,const size_t length,
4176 % void *magick_restrict data,ssize_t *count)
4177 %
4178 % A description of each parameter follows:
4179 %
4180 % o image: the image.
4181 %
4182 % o length: Specifies an integer representing the number of bytes to read
4183 % from the file.
4184 %
4185 % o count: returns the number of bytes read.
4186 %
4187 % o data: Specifies an area to place the information requested from the
4188 % file.
4189 %
4190 */
4191 MagickExport magick_hot_spot const void *ReadBlobStream(Image *image,
4192  const size_t length,void *magick_restrict data,ssize_t *count)
4193 {
4194  BlobInfo
4195  *magick_restrict blob_info;
4196 
4197  assert(image != (Image *) NULL);
4198  assert(image->signature == MagickCoreSignature);
4199  assert(image->blob != (BlobInfo *) NULL);
4200  assert(image->blob->type != UndefinedStream);
4201  assert(count != (ssize_t *) NULL);
4202  blob_info=image->blob;
4203  if (blob_info->type != BlobStream)
4204  {
4205  assert(data != NULL);
4206  *count=ReadBlob(image,length,(unsigned char *) data);
4207  return(data);
4208  }
4209  if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4210  {
4211  *count=0;
4212  blob_info->eof=MagickTrue;
4213  return(data);
4214  }
4215  data=blob_info->data+blob_info->offset;
4216  *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4217  blob_info->length-blob_info->offset);
4218  blob_info->offset+=(*count);
4219  if (*count != (ssize_t) length)
4220  blob_info->eof=MagickTrue;
4221  return(data);
4222 }
4223 
4224 /*
4225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4226 % %
4227 % %
4228 % %
4229 + R e a d B l o b S t r i n g %
4230 % %
4231 % %
4232 % %
4233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4234 %
4235 % ReadBlobString() reads characters from a blob or file until a newline
4236 % character is read or an end-of-file condition is encountered.
4237 %
4238 % The format of the ReadBlobString method is:
4239 %
4240 % char *ReadBlobString(Image *image,char *string)
4241 %
4242 % A description of each parameter follows:
4243 %
4244 % o image: the image.
4245 %
4246 % o string: the address of a character buffer.
4247 %
4248 */
4249 MagickExport char *ReadBlobString(Image *image,char *string)
4250 {
4251  int
4252  c;
4253 
4254  ssize_t
4255  i;
4256 
4257  assert(image != (Image *) NULL);
4258  assert(image->signature == MagickCoreSignature);
4259  for (i=0; i < (MagickPathExtent-1L); i++)
4260  {
4261  c=ReadBlobByte(image);
4262  if (c == EOF)
4263  {
4264  if (i == 0)
4265  return((char *) NULL);
4266  break;
4267  }
4268  string[i]=c;
4269  if (c == '\n')
4270  {
4271  if ((i > 0) && (string[i-1] == '\r'))
4272  i--;
4273  break;
4274  }
4275  }
4276  string[i]='\0';
4277  return(string);
4278 }
4279 
4280 /*
4281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4282 % %
4283 % %
4284 % %
4285 + R e f e r e n c e B l o b %
4286 % %
4287 % %
4288 % %
4289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4290 %
4291 % ReferenceBlob() increments the reference count associated with the pixel
4292 % blob returning a pointer to the blob.
4293 %
4294 % The format of the ReferenceBlob method is:
4295 %
4296 % BlobInfo ReferenceBlob(BlobInfo *blob_info)
4297 %
4298 % A description of each parameter follows:
4299 %
4300 % o blob_info: the blob_info.
4301 %
4302 */
4303 MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4304 {
4305  assert(blob != (BlobInfo *) NULL);
4306  assert(blob->signature == MagickCoreSignature);
4307  if (IsEventLogging() != MagickFalse)
4308  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4309  LockSemaphoreInfo(blob->semaphore);
4310  blob->reference_count++;
4311  UnlockSemaphoreInfo(blob->semaphore);
4312  return(blob);
4313 }
4314 
4315 /*
4316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4317 % %
4318 % %
4319 % %
4320 + S e e k B l o b %
4321 % %
4322 % %
4323 % %
4324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4325 %
4326 % SeekBlob() sets the offset in bytes from the beginning of a blob or file
4327 % and returns the resulting offset.
4328 %
4329 % The format of the SeekBlob method is:
4330 %
4331 % MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
4332 % const int whence)
4333 %
4334 % A description of each parameter follows:
4335 %
4336 % o image: the image.
4337 %
4338 % o offset: Specifies an integer representing the offset in bytes.
4339 %
4340 % o whence: Specifies an integer representing how the offset is
4341 % treated relative to the beginning of the blob as follows:
4342 %
4343 % SEEK_SET Set position equal to offset bytes.
4344 % SEEK_CUR Set position to current location plus offset.
4345 % SEEK_END Set position to EOF plus offset.
4346 %
4347 */
4348 MagickExport MagickOffsetType SeekBlob(Image *image,
4349  const MagickOffsetType offset,const int whence)
4350 {
4351  BlobInfo
4352  *magick_restrict blob_info;
4353 
4354  assert(image != (Image *) NULL);
4355  assert(image->signature == MagickCoreSignature);
4356  assert(image->blob != (BlobInfo *) NULL);
4357  assert(image->blob->type != UndefinedStream);
4358  if (IsEventLogging() != MagickFalse)
4359  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4360  blob_info=image->blob;
4361  switch (blob_info->type)
4362  {
4363  case UndefinedStream:
4364  break;
4365  case StandardStream:
4366  case PipeStream:
4367  return(-1);
4368  case FileStream:
4369  {
4370  if ((offset < 0) && (whence == SEEK_SET))
4371  return(-1);
4372  if (fseek(blob_info->file_info.file,offset,whence) < 0)
4373  return(-1);
4374  blob_info->offset=TellBlob(image);
4375  break;
4376  }
4377  case ZipStream:
4378  {
4379 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4380  if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4381  return(-1);
4382 #endif
4383  blob_info->offset=TellBlob(image);
4384  break;
4385  }
4386  case BZipStream:
4387  return(-1);
4388  case FifoStream:
4389  return(-1);
4390  case BlobStream:
4391  {
4392  switch (whence)
4393  {
4394  case SEEK_SET:
4395  default:
4396  {
4397  if (offset < 0)
4398  return(-1);
4399  blob_info->offset=offset;
4400  break;
4401  }
4402  case SEEK_CUR:
4403  {
4404  if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4405  ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4406  {
4407  errno=EOVERFLOW;
4408  return(-1);
4409  }
4410  if ((blob_info->offset+offset) < 0)
4411  return(-1);
4412  blob_info->offset+=offset;
4413  break;
4414  }
4415  case SEEK_END:
4416  {
4417  if (((MagickOffsetType) blob_info->length+offset) < 0)
4418  return(-1);
4419  blob_info->offset=blob_info->length+offset;
4420  break;
4421  }
4422  }
4423  if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4424  {
4425  blob_info->eof=MagickFalse;
4426  break;
4427  }
4428  if (blob_info->offset >= (MagickOffsetType) ((off_t) blob_info->extent))
4429  return(-1);
4430  break;
4431  }
4432  }
4433  return(blob_info->offset);
4434 }
4435 
4436 /*
4437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4438 % %
4439 % %
4440 % %
4441 + S e t B l o b E x e m p t %
4442 % %
4443 % %
4444 % %
4445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4446 %
4447 % SetBlobExempt() sets the blob exempt status.
4448 %
4449 % The format of the SetBlobExempt method is:
4450 %
4451 % MagickBooleanType SetBlobExempt(const Image *image,
4452 % const MagickBooleanType exempt)
4453 %
4454 % A description of each parameter follows:
4455 %
4456 % o image: the image.
4457 %
4458 % o exempt: Set to true if this blob is exempt from being closed.
4459 %
4460 */
4461 MagickExport void SetBlobExempt(Image *image,const MagickBooleanType exempt)
4462 {
4463  assert(image != (const Image *) NULL);
4464  assert(image->signature == MagickCoreSignature);
4465  if (IsEventLogging() != MagickFalse)
4466  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4467  image->blob->exempt=exempt;
4468 }
4469 
4470 /*
4471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4472 % %
4473 % %
4474 % %
4475 + S e t B l o b E x t e n t %
4476 % %
4477 % %
4478 % %
4479 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4480 %
4481 % SetBlobExtent() ensures enough space is allocated for the blob. If the
4482 % method is successful, subsequent writes to bytes in the specified range are
4483 % guaranteed not to fail.
4484 %
4485 % The format of the SetBlobExtent method is:
4486 %
4487 % MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
4488 %
4489 % A description of each parameter follows:
4490 %
4491 % o image: the image.
4492 %
4493 % o extent: the blob maximum extent.
4494 %
4495 */
4496 MagickExport MagickBooleanType SetBlobExtent(Image *image,
4497  const MagickSizeType extent)
4498 {
4499  BlobInfo
4500  *magick_restrict blob_info;
4501 
4502  assert(image != (Image *) NULL);
4503  assert(image->signature == MagickCoreSignature);
4504  assert(image->blob != (BlobInfo *) NULL);
4505  assert(image->blob->type != UndefinedStream);
4506  if (IsEventLogging() != MagickFalse)
4507  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4508  blob_info=image->blob;
4509  switch (blob_info->type)
4510  {
4511  case UndefinedStream:
4512  break;
4513  case StandardStream:
4514  return(MagickFalse);
4515  case FileStream:
4516  {
4517  MagickOffsetType
4518  offset;
4519 
4520  ssize_t
4521  count;
4522 
4523  if (extent != (MagickSizeType) ((off_t) extent))
4524  return(MagickFalse);
4525  offset=SeekBlob(image,0,SEEK_END);
4526  if (offset < 0)
4527  return(MagickFalse);
4528  if ((MagickSizeType) offset >= extent)
4529  break;
4530  offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4531  if (offset < 0)
4532  break;
4533  count=(ssize_t) fwrite((const unsigned char *) "",1,1,
4534  blob_info->file_info.file);
4535 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4536  if (blob_info->synchronize != MagickFalse)
4537  {
4538  int
4539  file;
4540 
4541  file=fileno(blob_info->file_info.file);
4542  if ((file == -1) || (offset < 0))
4543  return(MagickFalse);
4544  (void) posix_fallocate(file,offset,extent-offset);
4545  }
4546 #endif
4547  offset=SeekBlob(image,offset,SEEK_SET);
4548  if (count != 1)
4549  return(MagickFalse);
4550  break;
4551  }
4552  case PipeStream:
4553  case ZipStream:
4554  return(MagickFalse);
4555  case BZipStream:
4556  return(MagickFalse);
4557  case FifoStream:
4558  return(MagickFalse);
4559  case BlobStream:
4560  {
4561  if (extent != (MagickSizeType) ((size_t) extent))
4562  return(MagickFalse);
4563  if (blob_info->mapped != MagickFalse)
4564  {
4565  MagickOffsetType
4566  offset;
4567 
4568  ssize_t
4569  count;
4570 
4571  (void) UnmapBlob(blob_info->data,blob_info->length);
4572  RelinquishMagickResource(MapResource,blob_info->length);
4573  if (extent != (MagickSizeType) ((off_t) extent))
4574  return(MagickFalse);
4575  offset=SeekBlob(image,0,SEEK_END);
4576  if (offset < 0)
4577  return(MagickFalse);
4578  if ((MagickSizeType) offset >= extent)
4579  break;
4580  offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4581  count=(ssize_t) fwrite((const unsigned char *) "",1,1,
4582  blob_info->file_info.file);
4583 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4584  if (blob_info->synchronize != MagickFalse)
4585  {
4586  int
4587  file;
4588 
4589  file=fileno(blob_info->file_info.file);
4590  if ((file == -1) || (offset < 0))
4591  return(MagickFalse);
4592  (void) posix_fallocate(file,offset,extent-offset);
4593  }
4594 #endif
4595  offset=SeekBlob(image,offset,SEEK_SET);
4596  if (count != 1)
4597  return(MagickFalse);
4598  (void) AcquireMagickResource(MapResource,extent);
4599  blob_info->data=(unsigned char*) MapBlob(fileno(
4600  blob_info->file_info.file),WriteMode,0,(size_t) extent);
4601  blob_info->extent=(size_t) extent;
4602  blob_info->length=(size_t) extent;
4603  (void) SyncBlob(image);
4604  break;
4605  }
4606  blob_info->extent=(size_t) extent;
4607  blob_info->data=(unsigned char *) ResizeQuantumMemory(blob_info->data,
4608  blob_info->extent+1,sizeof(*blob_info->data));
4609  (void) SyncBlob(image);
4610  if (blob_info->data == (unsigned char *) NULL)
4611  {
4612  (void) DetachBlob(blob_info);
4613  return(MagickFalse);
4614  }
4615  break;
4616  }
4617  }
4618  return(MagickTrue);
4619 }
4620 
4621 /*
4622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4623 % %
4624 % %
4625 % %
4626 + S y n c B l o b %
4627 % %
4628 % %
4629 % %
4630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4631 %
4632 % SyncBlob() flushes the datastream if it is a file or synchronizes the data
4633 % attributes if it is an blob.
4634 %
4635 % The format of the SyncBlob method is:
4636 %
4637 % int SyncBlob(Image *image)
4638 %
4639 % A description of each parameter follows:
4640 %
4641 % o image: the image.
4642 %
4643 */
4644 static int SyncBlob(Image *image)
4645 {
4646  BlobInfo
4647  *magick_restrict blob_info;
4648 
4649  int
4650  status;
4651 
4652  assert(image != (Image *) NULL);
4653  assert(image->signature == MagickCoreSignature);
4654  assert(image->blob != (BlobInfo *) NULL);
4655  assert(image->blob->type != UndefinedStream);
4656  if (IsEventLogging() != MagickFalse)
4657  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4658  blob_info=image->blob;
4659  status=0;
4660  switch (blob_info->type)
4661  {
4662  case UndefinedStream:
4663  case StandardStream:
4664  break;
4665  case FileStream:
4666  case PipeStream:
4667  {
4668  status=fflush(blob_info->file_info.file);
4669  break;
4670  }
4671  case ZipStream:
4672  {
4673 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4674  status=gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4675 #endif
4676  break;
4677  }
4678  case BZipStream:
4679  {
4680 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4681  status=BZ2_bzflush(blob_info->file_info.bzfile);
4682 #endif
4683  break;
4684  }
4685  case FifoStream:
4686  break;
4687  case BlobStream:
4688  break;
4689  }
4690  return(status);
4691 }
4692 
4693 /*
4694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4695 % %
4696 % %
4697 % %
4698 + T e l l B l o b %
4699 % %
4700 % %
4701 % %
4702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4703 %
4704 % TellBlob() obtains the current value of the blob or file position.
4705 %
4706 % The format of the TellBlob method is:
4707 %
4708 % MagickOffsetType TellBlob(const Image *image)
4709 %
4710 % A description of each parameter follows:
4711 %
4712 % o image: the image.
4713 %
4714 */
4715 MagickExport MagickOffsetType TellBlob(const Image *image)
4716 {
4717  BlobInfo
4718  *magick_restrict blob_info;
4719 
4720  MagickOffsetType
4721  offset;
4722 
4723  assert(image != (Image *) NULL);
4724  assert(image->signature == MagickCoreSignature);
4725  assert(image->blob != (BlobInfo *) NULL);
4726  assert(image->blob->type != UndefinedStream);
4727  if (IsEventLogging() != MagickFalse)
4728  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4729  blob_info=image->blob;
4730  offset=(-1);
4731  switch (blob_info->type)
4732  {
4733  case UndefinedStream:
4734  case StandardStream:
4735  break;
4736  case FileStream:
4737  {
4738  offset=ftell(blob_info->file_info.file);
4739  break;
4740  }
4741  case PipeStream:
4742  break;
4743  case ZipStream:
4744  {
4745 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4746  offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4747 #endif
4748  break;
4749  }
4750  case BZipStream:
4751  break;
4752  case FifoStream:
4753  break;
4754  case BlobStream:
4755  {
4756  offset=blob_info->offset;
4757  break;
4758  }
4759  }
4760  return(offset);
4761 }
4762 
4763 /*
4764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4765 % %
4766 % %
4767 % %
4768 + U n m a p B l o b %
4769 % %
4770 % %
4771 % %
4772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4773 %
4774 % UnmapBlob() deallocates the binary large object previously allocated with
4775 % the MapBlob method.
4776 %
4777 % The format of the UnmapBlob method is:
4778 %
4779 % MagickBooleanType UnmapBlob(void *map,const size_t length)
4780 %
4781 % A description of each parameter follows:
4782 %
4783 % o map: the address of the binary large object.
4784 %
4785 % o length: the length of the binary large object.
4786 %
4787 */
4788 MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
4789 {
4790 #if defined(MAGICKCORE_HAVE_MMAP)
4791  int
4792  status;
4793 
4794  status=munmap(map,length);
4795  return(status == -1 ? MagickFalse : MagickTrue);
4796 #else
4797  (void) map;
4798  (void) length;
4799  return(MagickFalse);
4800 #endif
4801 }
4802 
4803 /*
4804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4805 % %
4806 % %
4807 % %
4808 + W r i t e B l o b %
4809 % %
4810 % %
4811 % %
4812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4813 %
4814 % WriteBlob() writes data to a blob or image file. It returns the number of
4815 % bytes written.
4816 %
4817 % The format of the WriteBlob method is:
4818 %
4819 % ssize_t WriteBlob(Image *image,const size_t length,
4820 % const unsigned char *data)
4821 %
4822 % A description of each parameter follows:
4823 %
4824 % o image: the image.
4825 %
4826 % o length: Specifies an integer representing the number of bytes to
4827 % write to the file.
4828 %
4829 % o data: The address of the data to write to the blob or file.
4830 %
4831 */
4832 MagickExport ssize_t WriteBlob(Image *image,const size_t length,
4833  const unsigned char *data)
4834 {
4835  BlobInfo
4836  *magick_restrict blob_info;
4837 
4838  int
4839  c;
4840 
4841  const unsigned char
4842  *p;
4843 
4844  unsigned char
4845  *q;
4846 
4847  ssize_t
4848  count;
4849 
4850  assert(image != (Image *) NULL);
4851  assert(image->signature == MagickCoreSignature);
4852  assert(image->blob != (BlobInfo *) NULL);
4853  assert(image->blob->type != UndefinedStream);
4854  if (length == 0)
4855  return(0);
4856  assert(data != (const unsigned char *) NULL);
4857  blob_info=image->blob;
4858  count=0;
4859  p=(const unsigned char *) data;
4860  q=(unsigned char *) data;
4861  switch (blob_info->type)
4862  {
4863  case UndefinedStream:
4864  break;
4865  case StandardStream:
4866  case FileStream:
4867  case PipeStream:
4868  {
4869  switch (length)
4870  {
4871  default:
4872  {
4873  count=(ssize_t) fwrite((const char *) data,1,length,
4874  blob_info->file_info.file);
4875  break;
4876  }
4877  case 4:
4878  {
4879  c=putc((int) *p++,blob_info->file_info.file);
4880  if (c == EOF)
4881  break;
4882  count++;
4883  }
4884  case 3:
4885  {
4886  c=putc((int) *p++,blob_info->file_info.file);
4887  if (c == EOF)
4888  break;
4889  count++;
4890  }
4891  case 2:
4892  {
4893  c=putc((int) *p++,blob_info->file_info.file);
4894  if (c == EOF)
4895  break;
4896  count++;
4897  }
4898  case 1:
4899  {
4900  c=putc((int) *p++,blob_info->file_info.file);
4901  if (c == EOF)
4902  break;
4903  count++;
4904  }
4905  case 0:
4906  break;
4907  }
4908  if ((count != (ssize_t) length) &&
4909  (ferror(blob_info->file_info.file) != 0))
4910  ThrowBlobException(blob_info);
4911  break;
4912  }
4913  case ZipStream:
4914  {
4915 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4916  int
4917  status;
4918 
4919  switch (length)
4920  {
4921  default:
4922  {
4923  ssize_t
4924  i;
4925 
4926  for (i=0; i < (ssize_t) length; i+=count)
4927  {
4928  count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
4929  (unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4930  if (count <= 0)
4931  {
4932  count=0;
4933  if (errno != EINTR)
4934  break;
4935  }
4936  }
4937  count=i;
4938  break;
4939  }
4940  case 4:
4941  {
4942  c=gzputc(blob_info->file_info.gzfile,(int) *p++);
4943  if (c == EOF)
4944  break;
4945  count++;
4946  }
4947  case 3:
4948  {
4949  c=gzputc(blob_info->file_info.gzfile,(int) *p++);
4950  if (c == EOF)
4951  break;
4952  count++;
4953  }
4954  case 2:
4955  {
4956  c=gzputc(blob_info->file_info.gzfile,(int) *p++);
4957  if (c == EOF)
4958  break;
4959  count++;
4960  }
4961  case 1:
4962  {
4963  c=gzputc(blob_info->file_info.gzfile,(int) *p++);
4964  if (c == EOF)
4965  break;
4966  count++;
4967  }
4968  case 0:
4969  break;
4970  }
4971  status=Z_OK;
4972  (void) gzerror(blob_info->file_info.gzfile,&status);
4973  if ((count != (ssize_t) length) && (status != Z_OK))
4974  ThrowBlobException(blob_info);
4975 #endif
4976  break;
4977  }
4978  case BZipStream:
4979  {
4980 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4981  int
4982  status;
4983 
4984  ssize_t
4985  i;
4986 
4987  for (i=0; i < (ssize_t) length; i+=count)
4988  {
4989  count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
4990  (int) MagickMin(length-i,MagickMaxBufferExtent));
4991  if (count <= 0)
4992  {
4993  count=0;
4994  if (errno != EINTR)
4995  break;
4996  }
4997  }
4998  count=i;
4999  status=BZ_OK;
5000  (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5001  if ((count != (ssize_t) length) && (status != BZ_OK))
5002  ThrowBlobException(blob_info);
5003 #endif
5004  break;
5005  }
5006  case FifoStream:
5007  {
5008  count=(ssize_t) blob_info->stream(image,data,length);
5009  break;
5010  }
5011  case BlobStream:
5012  {
5013  if ((blob_info->offset+(MagickOffsetType) length) >=
5014  (MagickOffsetType) blob_info->extent)
5015  {
5016  if (blob_info->mapped != MagickFalse)
5017  return(0);
5018  blob_info->extent+=length+blob_info->quantum;
5019  blob_info->quantum<<=1;
5020  blob_info->data=(unsigned char *) ResizeQuantumMemory(
5021  blob_info->data,blob_info->extent+1,sizeof(*blob_info->data));
5022  (void) SyncBlob(image);
5023  if (blob_info->data == (unsigned char *) NULL)
5024  {
5025  (void) DetachBlob(blob_info);
5026  return(0);
5027  }
5028  }
5029  q=blob_info->data+blob_info->offset;
5030  (void) memcpy(q,p,length);
5031  blob_info->offset+=length;
5032  if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5033  blob_info->length=(size_t) blob_info->offset;
5034  count=(ssize_t) length;
5035  }
5036  }
5037  if (count != (ssize_t) length)
5038  ThrowBlobException(blob_info);
5039  return(count);
5040 }
5041 
5042 /*
5043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5044 % %
5045 % %
5046 % %
5047 + W r i t e B l o b B y t e %
5048 % %
5049 % %
5050 % %
5051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5052 %
5053 % WriteBlobByte() write an integer to a blob. It returns the number of bytes
5054 % written (either 0 or 1);
5055 %
5056 % The format of the WriteBlobByte method is:
5057 %
5058 % ssize_t WriteBlobByte(Image *image,const unsigned char value)
5059 %
5060 % A description of each parameter follows.
5061 %
5062 % o image: the image.
5063 %
5064 % o value: Specifies the value to write.
5065 %
5066 */
5067 MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
5068 {
5069  BlobInfo
5070  *magick_restrict blob_info;
5071 
5072  ssize_t
5073  count;
5074 
5075  assert(image != (Image *) NULL);
5076  assert(image->signature == MagickCoreSignature);
5077  assert(image->blob != (BlobInfo *) NULL);
5078  assert(image->blob->type != UndefinedStream);
5079  blob_info=image->blob;
5080  count=0;
5081  switch (blob_info->type)
5082  {
5083  case StandardStream:
5084  case FileStream:
5085  case PipeStream:
5086  {
5087  int
5088  c;
5089 
5090  c=putc((int) value,blob_info->file_info.file);
5091  if (c == EOF)
5092  {
5093  if (ferror(blob_info->file_info.file) != 0)
5094  ThrowBlobException(blob_info);
5095  break;
5096  }
5097  count++;
5098  break;
5099  }
5100  default:
5101  {
5102  count=WriteBlobStream(image,1,&value);
5103  break;
5104  }
5105  }
5106  return(count);
5107 }
5108 
5109 /*
5110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5111 % %
5112 % %
5113 % %
5114 + W r i t e B l o b F l o a t %
5115 % %
5116 % %
5117 % %
5118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5119 %
5120 % WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
5121 % specified by the endian member of the image structure.
5122 %
5123 % The format of the WriteBlobFloat method is:
5124 %
5125 % ssize_t WriteBlobFloat(Image *image,const float value)
5126 %
5127 % A description of each parameter follows.
5128 %
5129 % o image: the image.
5130 %
5131 % o value: Specifies the value to write.
5132 %
5133 */
5134 MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
5135 {
5136  union
5137  {
5138  unsigned int
5139  unsigned_value;
5140 
5141  float
5142  float_value;
5143  } quantum;
5144 
5145  quantum.unsigned_value=0U;
5146  quantum.float_value=value;
5147  return(WriteBlobLong(image,quantum.unsigned_value));
5148 }
5149 
5150 /*
5151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5152 % %
5153 % %
5154 % %
5155 + W r i t e B l o b L o n g %
5156 % %
5157 % %
5158 % %
5159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5160 %
5161 % WriteBlobLong() writes a unsigned int value as a 32-bit quantity in the
5162 % byte-order specified by the endian member of the image structure.
5163 %
5164 % The format of the WriteBlobLong method is:
5165 %
5166 % ssize_t WriteBlobLong(Image *image,const unsigned int value)
5167 %
5168 % A description of each parameter follows.
5169 %
5170 % o image: the image.
5171 %
5172 % o value: Specifies the value to write.
5173 %
5174 */
5175 MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
5176 {
5177  unsigned char
5178  buffer[4];
5179 
5180  assert(image != (Image *) NULL);
5181  assert(image->signature == MagickCoreSignature);
5182  if (image->endian == LSBEndian)
5183  {
5184  buffer[0]=(unsigned char) value;
5185  buffer[1]=(unsigned char) (value >> 8);
5186  buffer[2]=(unsigned char) (value >> 16);
5187  buffer[3]=(unsigned char) (value >> 24);
5188  return(WriteBlobStream(image,4,buffer));
5189  }
5190  buffer[0]=(unsigned char) (value >> 24);
5191  buffer[1]=(unsigned char) (value >> 16);
5192  buffer[2]=(unsigned char) (value >> 8);
5193  buffer[3]=(unsigned char) value;
5194  return(WriteBlobStream(image,4,buffer));
5195 }
5196 
5197 /*
5198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5199 % %
5200 % %
5201 % %
5202 + W r i t e B l o b S h o r t %
5203 % %
5204 % %
5205 % %
5206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5207 %
5208 % WriteBlobShort() writes a short value as a 16-bit quantity in the
5209 % byte-order specified by the endian member of the image structure.
5210 %
5211 % The format of the WriteBlobShort method is:
5212 %
5213 % ssize_t WriteBlobShort(Image *image,const unsigned short value)
5214 %
5215 % A description of each parameter follows.
5216 %
5217 % o image: the image.
5218 %
5219 % o value: Specifies the value to write.
5220 %
5221 */
5222 MagickExport ssize_t WriteBlobShort(Image *image,const unsigned short value)
5223 {
5224  unsigned char
5225  buffer[2];
5226 
5227  assert(image != (Image *) NULL);
5228  assert(image->signature == MagickCoreSignature);
5229  if (image->endian == LSBEndian)
5230  {
5231  buffer[0]=(unsigned char) value;
5232  buffer[1]=(unsigned char) (value >> 8);
5233  return(WriteBlobStream(image,2,buffer));
5234  }
5235  buffer[0]=(unsigned char) (value >> 8);
5236  buffer[1]=(unsigned char) value;
5237  return(WriteBlobStream(image,2,buffer));
5238 }
5239 
5240 /*
5241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5242 % %
5243 % %
5244 % %
5245 + W r i t e B l o b L S B L o n g %
5246 % %
5247 % %
5248 % %
5249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5250 %
5251 % WriteBlobLSBLong() writes a unsigned int value as a 32-bit quantity in
5252 % least-significant byte first order.
5253 %
5254 % The format of the WriteBlobLSBLong method is:
5255 %
5256 % ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
5257 %
5258 % A description of each parameter follows.
5259 %
5260 % o image: the image.
5261 %
5262 % o value: Specifies the value to write.
5263 %
5264 */
5265 MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
5266 {
5267  unsigned char
5268  buffer[4];
5269 
5270  assert(image != (Image *) NULL);
5271  assert(image->signature == MagickCoreSignature);
5272  buffer[0]=(unsigned char) value;
5273  buffer[1]=(unsigned char) (value >> 8);
5274  buffer[2]=(unsigned char) (value >> 16);
5275  buffer[3]=(unsigned char) (value >> 24);
5276  return(WriteBlobStream(image,4,buffer));
5277 }
5278 
5279 /*
5280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5281 % %
5282 % %
5283 % %
5284 + W r i t e B l o b L S B S h o r t %
5285 % %
5286 % %
5287 % %
5288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5289 %
5290 % WriteBlobLSBShort() writes a unsigned short value as a 16-bit quantity in
5291 % least-significant byte first order.
5292 %
5293 % The format of the WriteBlobLSBShort method is:
5294 %
5295 % ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
5296 %
5297 % A description of each parameter follows.
5298 %
5299 % o image: the image.
5300 %
5301 % o value: Specifies the value to write.
5302 %
5303 */
5304 MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
5305 {
5306  unsigned char
5307  buffer[2];
5308 
5309  assert(image != (Image *) NULL);
5310  assert(image->signature == MagickCoreSignature);
5311  buffer[0]=(unsigned char) value;
5312  buffer[1]=(unsigned char) (value >> 8);
5313  return(WriteBlobStream(image,2,buffer));
5314 }
5315 
5316 /*
5317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5318 % %
5319 % %
5320 % %
5321 + W r i t e B l o b L S B S i g n e d L o n g %
5322 % %
5323 % %
5324 % %
5325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5326 %
5327 % WriteBlobLSBSignedLong() writes a signed value as a 32-bit quantity in
5328 % least-significant byte first order.
5329 %
5330 % The format of the WriteBlobLSBSignedLong method is:
5331 %
5332 % ssize_t WriteBlobLSBSignedLong(Image *image,const signed int value)
5333 %
5334 % A description of each parameter follows.
5335 %
5336 % o image: the image.
5337 %
5338 % o value: Specifies the value to write.
5339 %
5340 */
5341 MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,const signed int value)
5342 {
5343  union
5344  {
5345  unsigned int
5346  unsigned_value;
5347 
5348  signed int
5349  signed_value;
5350  } quantum;
5351 
5352  unsigned char
5353  buffer[4];
5354 
5355  assert(image != (Image *) NULL);
5356  assert(image->signature == MagickCoreSignature);
5357  quantum.signed_value=value;
5358  buffer[0]=(unsigned char) quantum.unsigned_value;
5359  buffer[1]=(unsigned char) (quantum.unsigned_value >> 8);
5360  buffer[2]=(unsigned char) (quantum.unsigned_value >> 16);
5361  buffer[3]=(unsigned char) (quantum.unsigned_value >> 24);
5362  return(WriteBlobStream(image,4,buffer));
5363 }
5364 
5365 /*
5366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5367 % %
5368 % %
5369 % %
5370 + W r i t e B l o b L S B S i g n e d S h o r t %
5371 % %
5372 % %
5373 % %
5374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5375 %
5376 % WriteBlobLSBSignedShort() writes a signed short value as a 16-bit quantity
5377 % in least-significant byte first order.
5378 %
5379 % The format of the WriteBlobLSBSignedShort method is:
5380 %
5381 % ssize_t WriteBlobLSBSignedShort(Image *image,const signed short value)
5382 %
5383 % A description of each parameter follows.
5384 %
5385 % o image: the image.
5386 %
5387 % o value: Specifies the value to write.
5388 %
5389 */
5390 MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5391  const signed short value)
5392 {
5393  union
5394  {
5395  unsigned short
5396  unsigned_value;
5397 
5398  signed short
5399  signed_value;
5400  } quantum;
5401 
5402  unsigned char
5403  buffer[2];
5404 
5405  assert(image != (Image *) NULL);
5406  assert(image->signature == MagickCoreSignature);
5407  quantum.signed_value=value;
5408  buffer[0]=(unsigned char) quantum.unsigned_value;
5409  buffer[1]=(unsigned char) (quantum.unsigned_value >> 8);
5410  return(WriteBlobStream(image,2,buffer));
5411 }
5412 
5413 /*
5414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5415 % %
5416 % %
5417 % %
5418 + W r i t e B l o b M S B L o n g %
5419 % %
5420 % %
5421 % %
5422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5423 %
5424 % WriteBlobMSBLong() writes a unsigned int value as a 32-bit quantity in
5425 % most-significant byte first order.
5426 %
5427 % The format of the WriteBlobMSBLong method is:
5428 %
5429 % ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
5430 %
5431 % A description of each parameter follows.
5432 %
5433 % o value: Specifies the value to write.
5434 %
5435 % o image: the image.
5436 %
5437 */
5438 MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
5439 {
5440  unsigned char
5441  buffer[4];
5442 
5443  assert(image != (Image *) NULL);
5444  assert(image->signature == MagickCoreSignature);
5445  buffer[0]=(unsigned char) (value >> 24);
5446  buffer[1]=(unsigned char) (value >> 16);
5447  buffer[2]=(unsigned char) (value >> 8);
5448  buffer[3]=(unsigned char) value;
5449  return(WriteBlobStream(image,4,buffer));
5450 }
5451 
5452 /*
5453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5454 % %
5455 % %
5456 % %
5457 + W r i t e B l o b M S B L o n g L o n g %
5458 % %
5459 % %
5460 % %
5461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5462 %
5463 % WriteBlobMSBLongLong() writes a long long value as a 64-bit quantity in
5464 % most-significant byte first order.
5465 %
5466 % The format of the WriteBlobMSBLongLong method is:
5467 %
5468 % ssize_t WriteBlobMSBLongLong(Image *image,const MagickSizeType value)
5469 %
5470 % A description of each parameter follows.
5471 %
5472 % o value: Specifies the value to write.
5473 %
5474 % o image: the image.
5475 %
5476 */
5477 MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5478  const MagickSizeType value)
5479 {
5480  unsigned char
5481  buffer[8];
5482 
5483  assert(image != (Image *) NULL);
5484  assert(image->signature == MagickCoreSignature);
5485  buffer[0]=(unsigned char) (value >> 56);
5486  buffer[1]=(unsigned char) (value >> 48);
5487  buffer[2]=(unsigned char) (value >> 40);
5488  buffer[3]=(unsigned char) (value >> 32);
5489  buffer[4]=(unsigned char) (value >> 24);
5490  buffer[5]=(unsigned char) (value >> 16);
5491  buffer[6]=(unsigned char) (value >> 8);
5492  buffer[7]=(unsigned char) value;
5493  return(WriteBlobStream(image,8,buffer));
5494 }
5495 
5496 /*
5497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5498 % %
5499 % %
5500 % %
5501 + W r i t e B l o b M S B S h o r t %
5502 % %
5503 % %
5504 % %
5505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5506 %
5507 % WriteBlobMSBShort() writes a unsigned short value as a 16-bit quantity in
5508 % most-significant byte first order.
5509 %
5510 % The format of the WriteBlobMSBShort method is:
5511 %
5512 % ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
5513 %
5514 % A description of each parameter follows.
5515 %
5516 % o value: Specifies the value to write.
5517 %
5518 % o file: Specifies the file to write the data to.
5519 %
5520 */
5521 MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
5522 {
5523  unsigned char
5524  buffer[2];
5525 
5526  assert(image != (Image *) NULL);
5527  assert(image->signature == MagickCoreSignature);
5528  buffer[0]=(unsigned char) (value >> 8);
5529  buffer[1]=(unsigned char) value;
5530  return(WriteBlobStream(image,2,buffer));
5531 }
5532 
5533 /*
5534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5535 % %
5536 % %
5537 % %
5538 + W r i t e B l o b M S B S i g n e d L o n g %
5539 % %
5540 % %
5541 % %
5542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5543 %
5544 % WriteBlobMSBSignedLong() writes a signed value as a 32-bit quantity in
5545 % most-significant byte first order.
5546 %
5547 % The format of the WriteBlobMSBSignedLong method is:
5548 %
5549 % ssize_t WriteBlobMSBSignedLong(Image *image,const signed int value)
5550 %
5551 % A description of each parameter follows.
5552 %
5553 % o image: the image.
5554 %
5555 % o value: Specifies the value to write.
5556 %
5557 */
5558 MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,const signed int value)
5559 {
5560  union
5561  {
5562  unsigned int
5563  unsigned_value;
5564 
5565  signed int
5566  signed_value;
5567  } quantum;
5568 
5569  unsigned char
5570  buffer[4];
5571 
5572  assert(image != (Image *) NULL);
5573  assert(image->signature == MagickCoreSignature);
5574  quantum.signed_value=value;
5575  buffer[0]=(unsigned char) (quantum.unsigned_value >> 24);
5576  buffer[1]=(unsigned char) (quantum.unsigned_value >> 16);
5577  buffer[2]=(unsigned char) (quantum.unsigned_value >> 8);
5578  buffer[3]=(unsigned char) quantum.unsigned_value;
5579  return(WriteBlobStream(image,4,buffer));
5580 }
5581 
5582 /*
5583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5584 % %
5585 % %
5586 % %
5587 + W r i t e B l o b M S B S i g n e d S h o r t %
5588 % %
5589 % %
5590 % %
5591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5592 %
5593 % WriteBlobMSBSignedShort() writes a signed short value as a 16-bit quantity
5594 % in most-significant byte first order.
5595 %
5596 % The format of the WriteBlobMSBSignedShort method is:
5597 %
5598 % ssize_t WriteBlobMSBSignedShort(Image *image,const signed short value)
5599 %
5600 % A description of each parameter follows.
5601 %
5602 % o image: the image.
5603 %
5604 % o value: Specifies the value to write.
5605 %
5606 */
5607 MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5608  const signed short value)
5609 {
5610  union
5611  {
5612  unsigned short
5613  unsigned_value;
5614 
5615  signed short
5616  signed_value;
5617  } quantum;
5618 
5619  unsigned char
5620  buffer[2];
5621 
5622  assert(image != (Image *) NULL);
5623  assert(image->signature == MagickCoreSignature);
5624  quantum.signed_value=value;
5625  buffer[0]=(unsigned char) (quantum.unsigned_value >> 8);
5626  buffer[1]=(unsigned char) quantum.unsigned_value;
5627  return(WriteBlobStream(image,2,buffer));
5628 }
5629 
5630 /*
5631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5632 % %
5633 % %
5634 % %
5635 + W r i t e B l o b S t r i n g %
5636 % %
5637 % %
5638 % %
5639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5640 %
5641 % WriteBlobString() write a string to a blob. It returns the number of
5642 % characters written.
5643 %
5644 % The format of the WriteBlobString method is:
5645 %
5646 % ssize_t WriteBlobString(Image *image,const char *string)
5647 %
5648 % A description of each parameter follows.
5649 %
5650 % o image: the image.
5651 %
5652 % o string: Specifies the string to write.
5653 %
5654 */
5655 MagickExport ssize_t WriteBlobString(Image *image,const char *string)
5656 {
5657  assert(image != (Image *) NULL);
5658  assert(image->signature == MagickCoreSignature);
5659  assert(string != (const char *) NULL);
5660  return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));
5661 }
Definition: image.h:152
Definition: blob.c:99