MagickCore  6.9.12-76
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=(unsigned char *) RelinquishMagickMemory(blob);
1776  else
1777  blob=(unsigned char *) ResizeQuantumMemory(blob,*length+1,
1778  sizeof(unsigned char));
1779  }
1780  }
1781  }
1782  else
1783  {
1784  char
1785  unique[MagickPathExtent];
1786 
1787  int
1788  file;
1789 
1790  /*
1791  Write file to disk in blob image format.
1792  */
1793  file=AcquireUniqueFileResource(unique);
1794  if (file == -1)
1795  {
1796  ThrowFileException(exception,BlobError,"UnableToWriteBlob",
1797  image_info->filename);
1798  }
1799  else
1800  {
1801  blob_info->file=fdopen(file,"wb");
1802  if (blob_info->file != (FILE *) NULL)
1803  {
1804  (void) FormatLocaleString(image->filename,MagickPathExtent,
1805  "%s:%s",image->magick,unique);
1806  status=WriteImage(blob_info,image);
1807  (void) CloseBlob(image);
1808  (void) fclose(blob_info->file);
1809  if (status == MagickFalse)
1810  InheritException(exception,&image->exception);
1811  else
1812  blob=FileToBlob(unique,~0UL,length,exception);
1813  }
1814  (void) RelinquishUniqueFileResource(unique);
1815  }
1816  }
1817  blob_info=DestroyImageInfo(blob_info);
1818  return(blob);
1819 }
1820 
1821 /*
1822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1823 % %
1824 % %
1825 % %
1826 % I m a g e T o F i l e %
1827 % %
1828 % %
1829 % %
1830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1831 %
1832 % ImageToFile() writes an image to a file. It returns MagickFalse if an error
1833 % occurs otherwise MagickTrue.
1834 %
1835 % The format of the ImageToFile method is:
1836 %
1837 % MagickBooleanType ImageToFile(Image *image,char *filename,
1838 % ExceptionInfo *exception)
1839 %
1840 % A description of each parameter follows:
1841 %
1842 % o image: the image.
1843 %
1844 % o filename: Write the image to this file.
1845 %
1846 % o exception: return any errors or warnings in this structure.
1847 %
1848 */
1849 MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
1850  ExceptionInfo *exception)
1851 {
1852  int
1853  file;
1854 
1855  const unsigned char
1856  *p;
1857 
1858  size_t
1859  i;
1860 
1861  size_t
1862  length,
1863  quantum;
1864 
1865  ssize_t
1866  count;
1867 
1868  struct stat
1869  file_stats;
1870 
1871  unsigned char
1872  *buffer;
1873 
1874  assert(image != (Image *) NULL);
1875  assert(image->signature == MagickCoreSignature);
1876  assert(image->blob != (BlobInfo *) NULL);
1877  assert(image->blob->type != UndefinedStream);
1878  assert(filename != (const char *) NULL);
1879  if (IsEventLogging() != MagickFalse)
1880  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1881  if (*filename == '\0')
1882  file=AcquireUniqueFileResource(filename);
1883  else
1884  if (LocaleCompare(filename,"-") == 0)
1885  file=fileno(stdout);
1886  else
1887  file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1888  if (file == -1)
1889  {
1890  ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1891  return(MagickFalse);
1892  }
1893  quantum=(size_t) MagickMaxBufferExtent;
1894  if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1895  quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1896  buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1897  if (buffer == (unsigned char *) NULL)
1898  {
1899  file=close(file)-1;
1900  (void) ThrowMagickException(exception,GetMagickModule(),
1901  ResourceLimitError,"MemoryAllocationError","`%s'",filename);
1902  return(MagickFalse);
1903  }
1904  length=0;
1905  p=(const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1906  for (i=0; count > 0; )
1907  {
1908  length=(size_t) count;
1909  for (i=0; i < length; i+=count)
1910  {
1911  count=write(file,p+i,(size_t) (length-i));
1912  if (count <= 0)
1913  {
1914  count=0;
1915  if (errno != EINTR)
1916  break;
1917  }
1918  }
1919  if (i < length)
1920  break;
1921  p=(const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1922  }
1923  if (LocaleCompare(filename,"-") != 0)
1924  file=close(file);
1925  buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1926  if ((file == -1) || (i < length))
1927  {
1928  if (file != -1)
1929  file=close(file);
1930  ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1931  return(MagickFalse);
1932  }
1933  return(MagickTrue);
1934 }
1935 
1936 /*
1937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1938 % %
1939 % %
1940 % %
1941 % I m a g e s T o B l o b %
1942 % %
1943 % %
1944 % %
1945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1946 %
1947 % ImagesToBlob() implements direct to memory image formats. It returns the
1948 % image sequence as a blob and its length. The magick member of the ImageInfo
1949 % structure determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1950 %
1951 % Note, some image formats do not permit multiple images to the same image
1952 % stream (e.g. JPEG). in this instance, just the first image of the
1953 % sequence is returned as a blob.
1954 %
1955 % The format of the ImagesToBlob method is:
1956 %
1957 % unsigned char *ImagesToBlob(const ImageInfo *image_info,Image *images,
1958 % size_t *length,ExceptionInfo *exception)
1959 %
1960 % A description of each parameter follows:
1961 %
1962 % o image_info: the image info.
1963 %
1964 % o images: the image list.
1965 %
1966 % o length: return the actual length of the blob.
1967 %
1968 % o exception: return any errors or warnings in this structure.
1969 %
1970 */
1971 MagickExport unsigned char *ImagesToBlob(const ImageInfo *image_info,
1972  Image *images,size_t *length,ExceptionInfo *exception)
1973 {
1974  const MagickInfo
1975  *magick_info;
1976 
1977  ImageInfo
1978  *blob_info;
1979 
1980  MagickBooleanType
1981  status;
1982 
1983  unsigned char
1984  *blob;
1985 
1986  assert(image_info != (const ImageInfo *) NULL);
1987  assert(image_info->signature == MagickCoreSignature);
1988  assert(images != (Image *) NULL);
1989  assert(images->signature == MagickCoreSignature);
1990  assert(exception != (ExceptionInfo *) NULL);
1991  if (IsEventLogging() != MagickFalse)
1992  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1993  image_info->filename);
1994  *length=0;
1995  blob=(unsigned char *) NULL;
1996  blob_info=CloneImageInfo(image_info);
1997  (void) SetImageInfo(blob_info,(unsigned int) GetImageListLength(images),
1998  exception);
1999  if (*blob_info->magick != '\0')
2000  (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2001  magick_info=GetMagickInfo(images->magick,exception);
2002  if (magick_info == (const MagickInfo *) NULL)
2003  {
2004  (void) ThrowMagickException(exception,GetMagickModule(),
2005  MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
2006  images->magick);
2007  blob_info=DestroyImageInfo(blob_info);
2008  return(blob);
2009  }
2010  if (GetMagickAdjoin(magick_info) == MagickFalse)
2011  {
2012  blob_info=DestroyImageInfo(blob_info);
2013  return(ImageToBlob(image_info,images,length,exception));
2014  }
2015  (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2016  if (GetMagickBlobSupport(magick_info) != MagickFalse)
2017  {
2018  /*
2019  Native blob support for this images format.
2020  */
2021  blob_info->length=0;
2022  blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2023  sizeof(unsigned char));
2024  if (blob_info->blob == (void *) NULL)
2025  (void) ThrowMagickException(exception,GetMagickModule(),
2026  ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
2027  else
2028  {
2029  (void) CloseBlob(images);
2030  images->blob->exempt=MagickTrue;
2031  *images->filename='\0';
2032  status=WriteImages(blob_info,images,images->filename,exception);
2033  *length=images->blob->length;
2034  blob=DetachBlob(images->blob);
2035  if (blob != (void *) NULL)
2036  {
2037  if (status == MagickFalse)
2038  blob=(unsigned char *) RelinquishMagickMemory(blob);
2039  else
2040  blob=(unsigned char *) ResizeQuantumMemory(blob,*length+1,
2041  sizeof(unsigned char));
2042  }
2043  }
2044  }
2045  else
2046  {
2047  char
2048  filename[MagickPathExtent],
2049  unique[MagickPathExtent];
2050 
2051  int
2052  file;
2053 
2054  /*
2055  Write file to disk in blob images format.
2056  */
2057  file=AcquireUniqueFileResource(unique);
2058  if (file == -1)
2059  {
2060  ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
2061  image_info->filename);
2062  }
2063  else
2064  {
2065  blob_info->file=fdopen(file,"wb");
2066  if (blob_info->file != (FILE *) NULL)
2067  {
2068  (void) FormatLocaleString(filename,MagickPathExtent,"%s:%s",
2069  images->magick,unique);
2070  status=WriteImages(blob_info,images,filename,exception);
2071  (void) CloseBlob(images);
2072  (void) fclose(blob_info->file);
2073  if (status == MagickFalse)
2074  InheritException(exception,&images->exception);
2075  else
2076  blob=FileToBlob(unique,~0UL,length,exception);
2077  }
2078  (void) RelinquishUniqueFileResource(unique);
2079  }
2080  }
2081  blob_info=DestroyImageInfo(blob_info);
2082  return(blob);
2083 }
2084 /*
2085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2086 % %
2087 % %
2088 % %
2089 % I n j e c t I m a g e B l o b %
2090 % %
2091 % %
2092 % %
2093 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2094 %
2095 % InjectImageBlob() injects the image with a copy of itself in the specified
2096 % format (e.g. inject JPEG into a PDF image).
2097 %
2098 % The format of the InjectImageBlob method is:
2099 %
2100 % MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
2101 % Image *image,Image *inject_image,const char *format,
2102 % ExceptionInfo *exception)
2103 %
2104 % A description of each parameter follows:
2105 %
2106 % o image_info: the image info..
2107 %
2108 % o image: the image.
2109 %
2110 % o inject_image: inject into the image stream.
2111 %
2112 % o format: the image format.
2113 %
2114 % o exception: return any errors or warnings in this structure.
2115 %
2116 */
2117 MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
2118  Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
2119 {
2120  char
2121  filename[MagickPathExtent];
2122 
2123  FILE
2124  *unique_file;
2125 
2126  Image
2127  *byte_image;
2128 
2129  ImageInfo
2130  *write_info;
2131 
2132  int
2133  file;
2134 
2135  MagickBooleanType
2136  status;
2137 
2138  size_t
2139  quantum;
2140 
2141  struct stat
2142  file_stats;
2143 
2144  unsigned char
2145  *buffer;
2146 
2147  /*
2148  Write inject image to a temporary file.
2149  */
2150  assert(image_info != (ImageInfo *) NULL);
2151  assert(image_info->signature == MagickCoreSignature);
2152  assert(image != (Image *) NULL);
2153  assert(image->signature == MagickCoreSignature);
2154  assert(inject_image != (Image *) NULL);
2155  assert(inject_image->signature == MagickCoreSignature);
2156  assert(exception != (ExceptionInfo *) NULL);
2157  if (IsEventLogging() != MagickFalse)
2158  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2159  unique_file=(FILE *) NULL;
2160  file=AcquireUniqueFileResource(filename);
2161  if (file != -1)
2162  unique_file=fdopen(file,"wb");
2163  if ((file == -1) || (unique_file == (FILE *) NULL))
2164  {
2165  (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2166  ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2167  image->filename);
2168  return(MagickFalse);
2169  }
2170  byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2171  if (byte_image == (Image *) NULL)
2172  {
2173  (void) fclose(unique_file);
2174  (void) RelinquishUniqueFileResource(filename);
2175  return(MagickFalse);
2176  }
2177  (void) FormatLocaleString(byte_image->filename,MagickPathExtent,"%s:%s",
2178  format,filename);
2179  DestroyBlob(byte_image);
2180  byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2181  write_info=CloneImageInfo(image_info);
2182  SetImageInfoFile(write_info,unique_file);
2183  status=WriteImage(write_info,byte_image);
2184  write_info=DestroyImageInfo(write_info);
2185  byte_image=DestroyImage(byte_image);
2186  (void) fclose(unique_file);
2187  if (status == MagickFalse)
2188  {
2189  (void) RelinquishUniqueFileResource(filename);
2190  return(MagickFalse);
2191  }
2192  /*
2193  Inject into image stream.
2194  */
2195  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2196  if (file == -1)
2197  {
2198  (void) RelinquishUniqueFileResource(filename);
2199  ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2200  image_info->filename);
2201  return(MagickFalse);
2202  }
2203  quantum=(size_t) MagickMaxBufferExtent;
2204  if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2205  quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2206  buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
2207  if (buffer == (unsigned char *) NULL)
2208  {
2209  (void) RelinquishUniqueFileResource(filename);
2210  file=close(file);
2211  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2212  image->filename);
2213  }
2214  for ( ; ; )
2215  {
2216  ssize_t count = read(file,buffer,quantum);
2217  if (count <= 0)
2218  {
2219  count=0;
2220  if (errno != EINTR)
2221  break;
2222  }
2223  status=WriteBlobStream(image,(size_t) count,buffer) == count ? MagickTrue :
2224  MagickFalse;
2225  }
2226  file=close(file);
2227  if (file == -1)
2228  ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",filename);
2229  (void) RelinquishUniqueFileResource(filename);
2230  buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2231  return(status);
2232 }
2233 
2234 /*
2235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2236 % %
2237 % %
2238 % %
2239 % I s B l o b E x e m p t %
2240 % %
2241 % %
2242 % %
2243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2244 %
2245 % IsBlobExempt() returns true if the blob is exempt.
2246 %
2247 % The format of the IsBlobExempt method is:
2248 %
2249 % MagickBooleanType IsBlobExempt(const Image *image)
2250 %
2251 % A description of each parameter follows:
2252 %
2253 % o image: the image.
2254 %
2255 */
2256 MagickExport MagickBooleanType IsBlobExempt(const Image *image)
2257 {
2258  assert(image != (const Image *) NULL);
2259  assert(image->signature == MagickCoreSignature);
2260  if (IsEventLogging() != MagickFalse)
2261  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2262  return(image->blob->exempt);
2263 }
2264 
2265 /*
2266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2267 % %
2268 % %
2269 % %
2270 + I s B l o b S e e k a b l e %
2271 % %
2272 % %
2273 % %
2274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2275 %
2276 % IsBlobSeekable() returns true if the blob is seekable.
2277 %
2278 % The format of the IsBlobSeekable method is:
2279 %
2280 % MagickBooleanType IsBlobSeekable(const Image *image)
2281 %
2282 % A description of each parameter follows:
2283 %
2284 % o image: the image.
2285 %
2286 */
2287 MagickExport MagickBooleanType IsBlobSeekable(const Image *image)
2288 {
2289  BlobInfo
2290  *magick_restrict blob_info;
2291 
2292  assert(image != (const Image *) NULL);
2293  assert(image->signature == MagickCoreSignature);
2294  if (IsEventLogging() != MagickFalse)
2295  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2296  blob_info=image->blob;
2297  switch (blob_info->type)
2298  {
2299  case BlobStream:
2300  return(MagickTrue);
2301  case FileStream:
2302  {
2303  int
2304  status;
2305 
2306  if (blob_info->file_info.file == (FILE *) NULL)
2307  return(MagickFalse);
2308  status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2309  return(status == -1 ? MagickFalse : MagickTrue);
2310  }
2311  case ZipStream:
2312  {
2313 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2314  MagickOffsetType
2315  offset;
2316 
2317  if (blob_info->file_info.gzfile == (gzFile) NULL)
2318  return(MagickFalse);
2319  offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2320  return(offset < 0 ? MagickFalse : MagickTrue);
2321 #else
2322  break;
2323 #endif
2324  }
2325  case UndefinedStream:
2326  case BZipStream:
2327  case FifoStream:
2328  case PipeStream:
2329  case StandardStream:
2330  break;
2331  default:
2332  break;
2333  }
2334  return(MagickFalse);
2335 }
2336 
2337 /*
2338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2339 % %
2340 % %
2341 % %
2342 % I s B l o b T e m p o r a r y %
2343 % %
2344 % %
2345 % %
2346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2347 %
2348 % IsBlobTemporary() returns true if the blob is temporary.
2349 %
2350 % The format of the IsBlobTemporary method is:
2351 %
2352 % MagickBooleanType IsBlobTemporary(const Image *image)
2353 %
2354 % A description of each parameter follows:
2355 %
2356 % o image: the image.
2357 %
2358 */
2359 MagickExport MagickBooleanType IsBlobTemporary(const Image *image)
2360 {
2361  assert(image != (const Image *) NULL);
2362  assert(image->signature == MagickCoreSignature);
2363  if (IsEventLogging() != MagickFalse)
2364  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2365  return(image->blob->temporary);
2366 }
2367 
2368 /*
2369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2370 % %
2371 % %
2372 % %
2373 + M a p B l o b %
2374 % %
2375 % %
2376 % %
2377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2378 %
2379 % MapBlob() creates a mapping from a file to a binary large object.
2380 %
2381 % The format of the MapBlob method is:
2382 %
2383 % unsigned char *MapBlob(int file,const MapMode mode,
2384 % const MagickOffsetType offset,const size_t length)
2385 %
2386 % A description of each parameter follows:
2387 %
2388 % o file: map this file descriptor.
2389 %
2390 % o mode: ReadMode, WriteMode, or IOMode.
2391 %
2392 % o offset: starting at this offset within the file.
2393 %
2394 % o length: the length of the mapping is returned in this pointer.
2395 %
2396 */
2397 MagickExport unsigned char *MapBlob(int file,const MapMode mode,
2398  const MagickOffsetType offset,const size_t length)
2399 {
2400 #if defined(MAGICKCORE_HAVE_MMAP)
2401  int
2402  flags,
2403  protection;
2404 
2405  unsigned char
2406  *map;
2407 
2408  /*
2409  Map file.
2410  */
2411  flags=0;
2412  if (file == -1)
2413 #if defined(MAP_ANONYMOUS)
2414  flags|=MAP_ANONYMOUS;
2415 #else
2416  return((unsigned char *) NULL);
2417 #endif
2418  switch (mode)
2419  {
2420  case ReadMode:
2421  default:
2422  {
2423  protection=PROT_READ;
2424  flags|=MAP_PRIVATE;
2425  break;
2426  }
2427  case WriteMode:
2428  {
2429  protection=PROT_WRITE;
2430  flags|=MAP_SHARED;
2431  break;
2432  }
2433  case IOMode:
2434  {
2435  protection=PROT_READ | PROT_WRITE;
2436  flags|=MAP_SHARED;
2437  break;
2438  }
2439  }
2440 #if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2441  map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,offset);
2442 #else
2443  map=(unsigned char *) mmap((char *) NULL,length,protection,flags |
2444  MAP_HUGETLB,file,offset);
2445  if (map == (unsigned char *) MAP_FAILED)
2446  map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2447  offset);
2448 #endif
2449  if (map == (unsigned char *) MAP_FAILED)
2450  return((unsigned char *) NULL);
2451  return(map);
2452 #else
2453  (void) file;
2454  (void) mode;
2455  (void) offset;
2456  (void) length;
2457  return((unsigned char *) NULL);
2458 #endif
2459 }
2460 
2461 /*
2462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2463 % %
2464 % %
2465 % %
2466 + M S B O r d e r L o n g %
2467 % %
2468 % %
2469 % %
2470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2471 %
2472 % MSBOrderLong() converts a least-significant byte first buffer of integers to
2473 % most-significant byte first.
2474 %
2475 % The format of the MSBOrderLong method is:
2476 %
2477 % void MSBOrderLong(unsigned char *buffer,const size_t length)
2478 %
2479 % A description of each parameter follows.
2480 %
2481 % o buffer: Specifies a pointer to a buffer of integers.
2482 %
2483 % o length: Specifies the length of the buffer.
2484 %
2485 */
2486 MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
2487 {
2488  int
2489  c;
2490 
2491  unsigned char
2492  *p,
2493  *q;
2494 
2495  assert(buffer != (unsigned char *) NULL);
2496  q=buffer+length;
2497  while (buffer < q)
2498  {
2499  p=buffer+3;
2500  c=(int) (*p);
2501  *p=(*buffer);
2502  *buffer++=(unsigned char) c;
2503  p=buffer+1;
2504  c=(int) (*p);
2505  *p=(*buffer);
2506  *buffer++=(unsigned char) c;
2507  buffer+=2;
2508  }
2509 }
2510 
2511 /*
2512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2513 % %
2514 % %
2515 % %
2516 + M S B O r d e r S h o r t %
2517 % %
2518 % %
2519 % %
2520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2521 %
2522 % MSBOrderShort() converts a least-significant byte first buffer of integers
2523 % to most-significant byte first.
2524 %
2525 % The format of the MSBOrderShort method is:
2526 %
2527 % void MSBOrderShort(unsigned char *p,const size_t length)
2528 %
2529 % A description of each parameter follows.
2530 %
2531 % o p: Specifies a pointer to a buffer of integers.
2532 %
2533 % o length: Specifies the length of the buffer.
2534 %
2535 */
2536 MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
2537 {
2538  int
2539  c;
2540 
2541  unsigned char
2542  *q;
2543 
2544  assert(p != (unsigned char *) NULL);
2545  q=p+length;
2546  while (p < q)
2547  {
2548  c=(int) (*p);
2549  *p=(*(p+1));
2550  p++;
2551  *p++=(unsigned char) c;
2552  }
2553 }
2554 
2555 /*
2556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2557 % %
2558 % %
2559 % %
2560 + O p e n B l o b %
2561 % %
2562 % %
2563 % %
2564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2565 %
2566 % OpenBlob() opens a file associated with the image. A file name of '-' sets
2567 % the file to stdin for type 'r' and stdout for type 'w'. If the filename
2568 % suffix is '.gz', the image is decompressed for type 'r' and compressed
2569 % for type 'w'. If the filename prefix is '|', it is piped to or from a
2570 % system command.
2571 %
2572 % The format of the OpenBlob method is:
2573 %
2574 % MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
2575 % const BlobMode mode,ExceptionInfo *exception)
2576 %
2577 % A description of each parameter follows:
2578 %
2579 % o image_info: the image info.
2580 %
2581 % o image: the image.
2582 %
2583 % o mode: the mode for opening the file.
2584 %
2585 */
2586 
2587 static inline MagickBooleanType SetStreamBuffering(const ImageInfo *image_info,
2588  Image *image)
2589 {
2590  const char
2591  *option;
2592 
2593  int
2594  status;
2595 
2596  size_t
2597  size;
2598 
2599  size=MagickMinBufferExtent;
2600  option=GetImageOption(image_info,"stream:buffer-size");
2601  if (option != (const char *) NULL)
2602  size=StringToUnsignedLong(option);
2603  status=setvbuf(image->blob->file_info.file,(char *) NULL,size == 0 ?
2604  _IONBF : _IOFBF,size);
2605  return(status == 0 ? MagickTrue : MagickFalse);
2606 }
2607 
2608 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2609 static inline gzFile gzopen_utf8(const char *path,const char *mode)
2610 {
2611 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2612  return(gzopen(path,mode));
2613 #else
2614  gzFile
2615  file;
2616 
2617  wchar_t
2618  *path_wide;
2619 
2620  path_wide=create_wchar_path(path);
2621  if (path_wide == (wchar_t *) NULL)
2622  return((gzFile) NULL);
2623  file=gzopen_w(path_wide,mode);
2624  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
2625  return(file);
2626 #endif
2627 }
2628 #endif
2629 
2630 MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
2631  Image *image,const BlobMode mode,ExceptionInfo *exception)
2632 {
2633  BlobInfo
2634  *magick_restrict blob_info;
2635 
2636  char
2637  extension[MagickPathExtent],
2638  filename[MagickPathExtent];
2639 
2640  const char
2641  *type;
2642 
2643  MagickBooleanType
2644  status;
2645 
2646  PolicyRights
2647  rights;
2648 
2649  assert(image_info != (ImageInfo *) NULL);
2650  assert(image_info->signature == MagickCoreSignature);
2651  assert(image != (Image *) NULL);
2652  assert(image->signature == MagickCoreSignature);
2653  if (IsEventLogging() != MagickFalse)
2654  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2655  image_info->filename);
2656  blob_info=image->blob;
2657  if (image_info->blob != (void *) NULL)
2658  {
2659  if (image_info->stream != (StreamHandler) NULL)
2660  blob_info->stream=(StreamHandler) image_info->stream;
2661  AttachBlob(blob_info,image_info->blob,image_info->length);
2662  return(MagickTrue);
2663  }
2664  (void) DetachBlob(blob_info);
2665  blob_info->mode=mode;
2666  switch (mode)
2667  {
2668  default: type="r"; break;
2669  case ReadBlobMode: type="r"; break;
2670  case ReadBinaryBlobMode: type="rb"; break;
2671  case WriteBlobMode: type="w"; break;
2672  case WriteBinaryBlobMode: type="w+b"; break;
2673  case AppendBlobMode: type="a"; break;
2674  case AppendBinaryBlobMode: type="a+b"; break;
2675  }
2676  if (*type != 'r')
2677  blob_info->synchronize=image_info->synchronize;
2678  if (image_info->stream != (StreamHandler) NULL)
2679  {
2680  blob_info->stream=(StreamHandler) image_info->stream;
2681  if (*type == 'w')
2682  {
2683  blob_info->type=FifoStream;
2684  return(MagickTrue);
2685  }
2686  }
2687  /*
2688  Open image file.
2689  */
2690  *filename='\0';
2691  (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2692  rights=ReadPolicyRights;
2693  if (*type == 'w')
2694  rights=WritePolicyRights;
2695  if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2696  {
2697  errno=EPERM;
2698  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2699  "NotAuthorized","`%s'",filename);
2700  return(MagickFalse);
2701  }
2702  if ((LocaleCompare(filename,"-") == 0) ||
2703  ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
2704  {
2705  blob_info->file_info.file=(*type == 'r') ? stdin : stdout;
2706 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2707  if (strchr(type,'b') != (char *) NULL)
2708  (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2709 #endif
2710  blob_info->type=StandardStream;
2711  blob_info->exempt=MagickTrue;
2712  return(SetStreamBuffering(image_info,image));
2713  }
2714  if ((LocaleNCompare(filename,"fd:",3) == 0) &&
2715  (IsGeometry(filename+3) != MagickFalse))
2716  {
2717  char
2718  fileMode[MagickPathExtent];
2719 
2720  *fileMode=(*type);
2721  fileMode[1]='\0';
2722  blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2723  if (blob_info->file_info.file == (FILE *) NULL)
2724  {
2725  ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2726  return(MagickFalse);
2727  }
2728 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2729  if (strchr(type,'b') != (char *) NULL)
2730  (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2731 #endif
2732  blob_info->type=FileStream;
2733  blob_info->exempt=MagickTrue;
2734  return(SetStreamBuffering(image_info,image));
2735  }
2736 #if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2737  if (*filename == '|')
2738  {
2739  char
2740  fileMode[MagickPathExtent],
2741  *sanitize_command;
2742 
2743  /*
2744  Pipe image to or from a system command.
2745  */
2746 #if defined(SIGPIPE)
2747  if (*type == 'w')
2748  (void) signal(SIGPIPE,SIG_IGN);
2749 #endif
2750  *fileMode=(*type);
2751  fileMode[1]='\0';
2752  sanitize_command=SanitizeString(filename+1);
2753  blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2754  fileMode);
2755  sanitize_command=DestroyString(sanitize_command);
2756  if (blob_info->file_info.file == (FILE *) NULL)
2757  {
2758  ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2759  return(MagickFalse);
2760  }
2761  blob_info->type=PipeStream;
2762  blob_info->exempt=MagickTrue;
2763  return(SetStreamBuffering(image_info,image));
2764  }
2765 #endif
2766  status=GetPathAttributes(filename,&blob_info->properties);
2767 #if defined(S_ISFIFO)
2768  if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2769  {
2770  blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2771  if (blob_info->file_info.file == (FILE *) NULL)
2772  {
2773  ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2774  return(MagickFalse);
2775  }
2776  blob_info->type=FileStream;
2777  blob_info->exempt=MagickTrue;
2778  return(SetStreamBuffering(image_info,image));
2779  }
2780 #endif
2781  GetPathComponent(image->filename,ExtensionPath,extension);
2782  if (*type == 'w')
2783  {
2784  (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2785  if ((image_info->adjoin == MagickFalse) ||
2786  (strchr(filename,'%') != (char *) NULL))
2787  {
2788  /*
2789  Form filename for multi-part images.
2790  */
2791  (void) InterpretImageFilename(image_info,image,image->filename,(int)
2792  image->scene,filename);
2793  if ((LocaleCompare(filename,image->filename) == 0) &&
2794  ((GetPreviousImageInList(image) != (Image *) NULL) ||
2795  (GetNextImageInList(image) != (Image *) NULL)))
2796  {
2797  char
2798  path[MagickPathExtent];
2799 
2800  GetPathComponent(image->filename,RootPath,path);
2801  if (*extension == '\0')
2802  (void) FormatLocaleString(filename,MagickPathExtent,"%s-%.20g",
2803  path,(double) image->scene);
2804  else
2805  (void) FormatLocaleString(filename,MagickPathExtent,
2806  "%s-%.20g.%s",path,(double) image->scene,extension);
2807  }
2808  (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2809 #if defined(macintosh)
2810  SetApplicationType(filename,image_info->magick,'8BIM');
2811 #endif
2812  }
2813  }
2814  if (image_info->file != (FILE *) NULL)
2815  {
2816  blob_info->file_info.file=image_info->file;
2817  blob_info->type=FileStream;
2818  blob_info->exempt=MagickTrue;
2819  }
2820  else
2821  if (*type == 'r')
2822  {
2823  blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2824  if (blob_info->file_info.file != (FILE *) NULL)
2825  {
2826  size_t
2827  count;
2828 
2829  unsigned char
2830  magick[3];
2831 
2832  blob_info->type=FileStream;
2833  (void) fstat(fileno(blob_info->file_info.file),
2834  &blob_info->properties);
2835  (void) SetStreamBuffering(image_info,image);
2836  (void) memset(magick,0,sizeof(magick));
2837  count=fread(magick,1,sizeof(magick),blob_info->file_info.file);
2838  (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2839 #if defined(MAGICKCORE_POSIX_SUPPORT)
2840  (void) fflush(blob_info->file_info.file);
2841 #endif
2842  (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2843  " read %.20g magic header bytes",(double) count);
2844 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2845  if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
2846  ((int) magick[2] == 0x08))
2847  {
2848  gzFile
2849  gzfile = gzopen_utf8(filename,"rb");
2850 
2851  if (gzfile != (gzFile) NULL)
2852  {
2853  if (blob_info->file_info.file != (FILE *) NULL)
2854  (void) fclose(blob_info->file_info.file);
2855  blob_info->file_info.file=(FILE *) NULL;
2856  blob_info->file_info.gzfile=gzfile;
2857  blob_info->type=ZipStream;
2858  }
2859  }
2860 #endif
2861 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2862  if (strncmp((char *) magick,"BZh",3) == 0)
2863  {
2864  BZFILE
2865  *bzfile = BZ2_bzopen(filename,"r");
2866 
2867  if (bzfile != (BZFILE *) NULL)
2868  {
2869  if (blob_info->file_info.file != (FILE *) NULL)
2870  (void) fclose(blob_info->file_info.file);
2871  blob_info->file_info.file=(FILE *) NULL;
2872  blob_info->file_info.bzfile=bzfile;
2873  blob_info->type=BZipStream;
2874  }
2875  }
2876 #endif
2877  if (blob_info->type == FileStream)
2878  {
2879  const MagickInfo
2880  *magick_info;
2881 
2883  *sans_exception;
2884 
2885  size_t
2886  length;
2887 
2888  sans_exception=AcquireExceptionInfo();
2889  magick_info=GetMagickInfo(image_info->magick,sans_exception);
2890  sans_exception=DestroyExceptionInfo(sans_exception);
2891  length=(size_t) blob_info->properties.st_size;
2892  if ((magick_info != (const MagickInfo *) NULL) &&
2893  (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2894  (length > MagickMaxBufferExtent) &&
2895  (AcquireMagickResource(MapResource,length) != MagickFalse))
2896  {
2897  void
2898  *blob;
2899 
2900  blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
2901  length);
2902  if (blob == (void *) NULL)
2903  RelinquishMagickResource(MapResource,length);
2904  else
2905  {
2906  /*
2907  Format supports blobs-- use memory-mapped I/O.
2908  */
2909  if (image_info->file != (FILE *) NULL)
2910  blob_info->exempt=MagickFalse;
2911  else
2912  {
2913  (void) fclose(blob_info->file_info.file);
2914  blob_info->file_info.file=(FILE *) NULL;
2915  }
2916  AttachBlob(blob_info,blob,length);
2917  blob_info->mapped=MagickTrue;
2918  }
2919  }
2920  }
2921  }
2922  }
2923  else
2924 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2925  if ((LocaleCompare(extension,"gz") == 0) ||
2926  (LocaleCompare(extension,"wmz") == 0) ||
2927  (LocaleCompare(extension,"svgz") == 0))
2928  {
2929  blob_info->file_info.gzfile=gzopen_utf8(filename,"wb");
2930  if (blob_info->file_info.gzfile != (gzFile) NULL)
2931  blob_info->type=ZipStream;
2932  }
2933  else
2934 #endif
2935 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2936  if (LocaleCompare(extension,"bz2") == 0)
2937  {
2938  if (mode == WriteBinaryBlobMode)
2939  type="w";
2940  blob_info->file_info.bzfile=BZ2_bzopen(filename,"w");
2941  if (blob_info->file_info.bzfile != (BZFILE *) NULL)
2942  blob_info->type=BZipStream;
2943  }
2944  else
2945 #endif
2946  {
2947  blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2948  if (blob_info->file_info.file != (FILE *) NULL)
2949  {
2950  blob_info->type=FileStream;
2951  (void) SetStreamBuffering(image_info,image);
2952  }
2953  }
2954  blob_info->status=MagickFalse;
2955  blob_info->error_number=0;
2956  if (blob_info->type != UndefinedStream)
2957  blob_info->size=GetBlobSize(image);
2958  else
2959  {
2960  ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2961  return(MagickFalse);
2962  }
2963  return(MagickTrue);
2964 }
2965 
2966 /*
2967 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2968 % %
2969 % %
2970 % %
2971 + P i n g B l o b %
2972 % %
2973 % %
2974 % %
2975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2976 %
2977 % PingBlob() returns all the attributes of an image or image sequence except
2978 % for the pixels. It is much faster and consumes far less memory than
2979 % BlobToImage(). On failure, a NULL image is returned and exception
2980 % describes the reason for the failure.
2981 %
2982 % The format of the PingBlob method is:
2983 %
2984 % Image *PingBlob(const ImageInfo *image_info,const void *blob,
2985 % const size_t length,ExceptionInfo *exception)
2986 %
2987 % A description of each parameter follows:
2988 %
2989 % o image_info: the image info.
2990 %
2991 % o blob: the address of a character stream in one of the image formats
2992 % understood by ImageMagick.
2993 %
2994 % o length: This size_t integer reflects the length in bytes of the blob.
2995 %
2996 % o exception: return any errors or warnings in this structure.
2997 %
2998 */
2999 
3000 #if defined(__cplusplus) || defined(c_plusplus)
3001 extern "C" {
3002 #endif
3003 
3004 static size_t PingStream(const Image *magick_unused(image),
3005  const void *magick_unused(pixels),const size_t columns)
3006 {
3007  magick_unreferenced(image);
3008  magick_unreferenced(pixels);
3009 
3010  return(columns);
3011 }
3012 
3013 #if defined(__cplusplus) || defined(c_plusplus)
3014 }
3015 #endif
3016 
3017 MagickExport Image *PingBlob(const ImageInfo *image_info,const void *blob,
3018  const size_t length,ExceptionInfo *exception)
3019 {
3020  const MagickInfo
3021  *magick_info;
3022 
3023  Image
3024  *image;
3025 
3026  ImageInfo
3027  *clone_info,
3028  *ping_info;
3029 
3030  MagickBooleanType
3031  status;
3032 
3033  assert(image_info != (ImageInfo *) NULL);
3034  assert(image_info->signature == MagickCoreSignature);
3035  assert(exception != (ExceptionInfo *) NULL);
3036  if (IsEventLogging() != MagickFalse)
3037  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3038  image_info->filename);
3039  if ((blob == (const void *) NULL) || (length == 0))
3040  {
3041  (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3042  "ZeroLengthBlobNotPermitted","`%s'",image_info->filename);
3043  return((Image *) NULL);
3044  }
3045  ping_info=CloneImageInfo(image_info);
3046  ping_info->blob=(void *) blob;
3047  ping_info->length=length;
3048  ping_info->ping=MagickTrue;
3049  if (*ping_info->magick == '\0')
3050  (void) SetImageInfo(ping_info,0,exception);
3051  magick_info=GetMagickInfo(ping_info->magick,exception);
3052  if (magick_info == (const MagickInfo *) NULL)
3053  {
3054  (void) ThrowMagickException(exception,GetMagickModule(),
3055  MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
3056  ping_info->magick);
3057  ping_info=DestroyImageInfo(ping_info);
3058  return((Image *) NULL);
3059  }
3060  if (GetMagickBlobSupport(magick_info) != MagickFalse)
3061  {
3062  char
3063  filename[MagickPathExtent];
3064 
3065  /*
3066  Native blob support for this image format.
3067  */
3068  (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3069  (void) FormatLocaleString(ping_info->filename,MagickPathExtent,"%s:%s",
3070  ping_info->magick,filename);
3071  image=ReadStream(ping_info,&PingStream,exception);
3072  if (image != (Image *) NULL)
3073  (void) DetachBlob(image->blob);
3074  ping_info=DestroyImageInfo(ping_info);
3075  return(image);
3076  }
3077  /*
3078  Write blob to a temporary file on disk.
3079  */
3080  ping_info->blob=(void *) NULL;
3081  ping_info->length=0;
3082  *ping_info->filename='\0';
3083  status=BlobToFile(ping_info->filename,blob,length,exception);
3084  if (status == MagickFalse)
3085  {
3086  (void) RelinquishUniqueFileResource(ping_info->filename);
3087  ping_info=DestroyImageInfo(ping_info);
3088  return((Image *) NULL);
3089  }
3090  clone_info=CloneImageInfo(ping_info);
3091  (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:%s",
3092  ping_info->magick,ping_info->filename);
3093  image=ReadStream(clone_info,&PingStream,exception);
3094  if (image != (Image *) NULL)
3095  {
3096  Image
3097  *images;
3098 
3099  /*
3100  Restore original filenames and image format.
3101  */
3102  for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3103  {
3104  (void) CopyMagickString(images->filename,image_info->filename,
3105  MagickPathExtent);
3106  (void) CopyMagickString(images->magick_filename,image_info->filename,
3107  MagickPathExtent);
3108  (void) CopyMagickString(images->magick,magick_info->name,
3109  MagickPathExtent);
3110  images=GetNextImageInList(images);
3111  }
3112  }
3113  clone_info=DestroyImageInfo(clone_info);
3114  (void) RelinquishUniqueFileResource(ping_info->filename);
3115  ping_info=DestroyImageInfo(ping_info);
3116  return(image);
3117 }
3118 
3119 /*
3120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3121 % %
3122 % %
3123 % %
3124 + R e a d B l o b %
3125 % %
3126 % %
3127 % %
3128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3129 %
3130 % ReadBlob() reads data from the blob or image file and returns it. It
3131 % returns the number of bytes read. If length is zero, ReadBlob() returns
3132 % zero and has no other results. If length is greater than MAGICK_SSIZE_MAX, the
3133 % result is unspecified.
3134 %
3135 % The format of the ReadBlob method is:
3136 %
3137 % ssize_t ReadBlob(Image *image,const size_t length,unsigned char *data)
3138 %
3139 % A description of each parameter follows:
3140 %
3141 % o image: the image.
3142 %
3143 % o length: Specifies an integer representing the number of bytes to read
3144 % from the file.
3145 %
3146 % o data: Specifies an area to place the information requested from the
3147 % file.
3148 %
3149 */
3150 MagickExport ssize_t ReadBlob(Image *image,const size_t length,
3151  unsigned char *data)
3152 {
3153  BlobInfo
3154  *magick_restrict blob_info;
3155 
3156  int
3157  c;
3158 
3159  unsigned char
3160  *q;
3161 
3162  ssize_t
3163  count;
3164 
3165  assert(image != (Image *) NULL);
3166  assert(image->signature == MagickCoreSignature);
3167  assert(image->blob != (BlobInfo *) NULL);
3168  assert(image->blob->type != UndefinedStream);
3169  if (length == 0)
3170  return(0);
3171  assert(data != (void *) NULL);
3172  blob_info=image->blob;
3173  count=0;
3174  q=data;
3175  switch (blob_info->type)
3176  {
3177  case UndefinedStream:
3178  break;
3179  case StandardStream:
3180  case FileStream:
3181  case PipeStream:
3182  {
3183  switch (length)
3184  {
3185  default:
3186  {
3187  count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3188  break;
3189  }
3190  case 4:
3191  {
3192  c=getc(blob_info->file_info.file);
3193  if (c == EOF)
3194  break;
3195  *q++=(unsigned char) c;
3196  count++;
3197  }
3198  case 3:
3199  {
3200  c=getc(blob_info->file_info.file);
3201  if (c == EOF)
3202  break;
3203  *q++=(unsigned char) c;
3204  count++;
3205  }
3206  case 2:
3207  {
3208  c=getc(blob_info->file_info.file);
3209  if (c == EOF)
3210  break;
3211  *q++=(unsigned char) c;
3212  count++;
3213  }
3214  case 1:
3215  {
3216  c=getc(blob_info->file_info.file);
3217  if (c == EOF)
3218  break;
3219  *q++=(unsigned char) c;
3220  count++;
3221  }
3222  case 0:
3223  break;
3224  }
3225  if ((count != (ssize_t) length) &&
3226  (ferror(blob_info->file_info.file) != 0))
3227  ThrowBlobException(blob_info);
3228  break;
3229  }
3230  case ZipStream:
3231  {
3232 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3233  int
3234  status;
3235 
3236  switch (length)
3237  {
3238  default:
3239  {
3240  ssize_t
3241  i;
3242 
3243  for (i=0; i < (ssize_t) length; i+=count)
3244  {
3245  count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3246  (unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3247  if (count <= 0)
3248  {
3249  count=0;
3250  if (errno != EINTR)
3251  break;
3252  }
3253  }
3254  count=i;
3255  break;
3256  }
3257  case 4:
3258  {
3259  c=gzgetc(blob_info->file_info.gzfile);
3260  if (c == EOF)
3261  break;
3262  *q++=(unsigned char) c;
3263  count++;
3264  }
3265  case 3:
3266  {
3267  c=gzgetc(blob_info->file_info.gzfile);
3268  if (c == EOF)
3269  break;
3270  *q++=(unsigned char) c;
3271  count++;
3272  }
3273  case 2:
3274  {
3275  c=gzgetc(blob_info->file_info.gzfile);
3276  if (c == EOF)
3277  break;
3278  *q++=(unsigned char) c;
3279  count++;
3280  }
3281  case 1:
3282  {
3283  c=gzgetc(blob_info->file_info.gzfile);
3284  if (c == EOF)
3285  break;
3286  *q++=(unsigned char) c;
3287  count++;
3288  }
3289  case 0:
3290  break;
3291  }
3292  status=Z_OK;
3293  (void) gzerror(blob_info->file_info.gzfile,&status);
3294  if ((count != (ssize_t) length) && (status != Z_OK))
3295  ThrowBlobException(blob_info);
3296  if (blob_info->eof == MagickFalse)
3297  blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3298  MagickFalse;
3299 #endif
3300  break;
3301  }
3302  case BZipStream:
3303  {
3304 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3305  int
3306  status;
3307 
3308  ssize_t
3309  i;
3310 
3311  for (i=0; i < (ssize_t) length; i+=count)
3312  {
3313  count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3314  (unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3315  if (count <= 0)
3316  {
3317  count=0;
3318  if (errno != EINTR)
3319  break;
3320  }
3321  }
3322  count=i;
3323  status=BZ_OK;
3324  (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3325  if ((count != (ssize_t) length) && (status != BZ_OK))
3326  ThrowBlobException(blob_info);
3327 #endif
3328  break;
3329  }
3330  case FifoStream:
3331  break;
3332  case BlobStream:
3333  {
3334  const unsigned char
3335  *p;
3336 
3337  if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3338  {
3339  blob_info->eof=MagickTrue;
3340  break;
3341  }
3342  p=blob_info->data+blob_info->offset;
3343  count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3344  blob_info->length-blob_info->offset);
3345  blob_info->offset+=count;
3346  if (count != (ssize_t) length)
3347  blob_info->eof=MagickTrue;
3348  (void) memcpy(q,p,(size_t) count);
3349  break;
3350  }
3351  }
3352  return(count);
3353 }
3354 
3355 /*
3356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3357 % %
3358 % %
3359 % %
3360 + R e a d B l o b B y t e %
3361 % %
3362 % %
3363 % %
3364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3365 %
3366 % ReadBlobByte() reads a single byte from the image file and returns it.
3367 %
3368 % The format of the ReadBlobByte method is:
3369 %
3370 % int ReadBlobByte(Image *image)
3371 %
3372 % A description of each parameter follows.
3373 %
3374 % o image: the image.
3375 %
3376 */
3377 MagickExport int ReadBlobByte(Image *image)
3378 {
3379  BlobInfo
3380  *magick_restrict blob_info;
3381 
3382  int
3383  c;
3384 
3385  assert(image != (Image *) NULL);
3386  assert(image->signature == MagickCoreSignature);
3387  assert(image->blob != (BlobInfo *) NULL);
3388  assert(image->blob->type != UndefinedStream);
3389  blob_info=image->blob;
3390  switch (blob_info->type)
3391  {
3392  case StandardStream:
3393  case FileStream:
3394  case PipeStream:
3395  {
3396  c=getc(blob_info->file_info.file);
3397  if (c == EOF)
3398  {
3399  if (ferror(blob_info->file_info.file) != 0)
3400  ThrowBlobException(blob_info);
3401  return(EOF);
3402  }
3403  break;
3404  }
3405  case BlobStream:
3406  {
3407  if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3408  {
3409  blob_info->eof=MagickTrue;
3410  return(EOF);
3411  }
3412  c=(int) (*((unsigned char *) blob_info->data+blob_info->offset));
3413  blob_info->offset++;
3414  break;
3415  }
3416  default:
3417  {
3418  ssize_t
3419  count;
3420 
3421  unsigned char
3422  buffer[1];
3423 
3424  count=ReadBlob(image,1,buffer);
3425  if (count != 1)
3426  return(EOF);
3427  c=(int) *buffer;
3428  break;
3429  }
3430  }
3431  return(c);
3432 }
3433 
3434 /*
3435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3436 % %
3437 % %
3438 % %
3439 + R e a d B l o b D o u b l e %
3440 % %
3441 % %
3442 % %
3443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3444 %
3445 % ReadBlobDouble() reads a double value as a 64-bit quantity in the byte-order
3446 % specified by the endian member of the image structure.
3447 %
3448 % The format of the ReadBlobDouble method is:
3449 %
3450 % double ReadBlobDouble(Image *image)
3451 %
3452 % A description of each parameter follows.
3453 %
3454 % o image: the image.
3455 %
3456 */
3457 MagickExport double ReadBlobDouble(Image *image)
3458 {
3459  union
3460  {
3461  MagickSizeType
3462  unsigned_value;
3463 
3464  double
3465  double_value;
3466  } quantum;
3467 
3468  quantum.double_value=0.0;
3469  quantum.unsigned_value=ReadBlobLongLong(image);
3470  return(quantum.double_value);
3471 }
3472 
3473 /*
3474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3475 % %
3476 % %
3477 % %
3478 + R e a d B l o b F l o a t %
3479 % %
3480 % %
3481 % %
3482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3483 %
3484 % ReadBlobFloat() reads a float value as a 32-bit quantity in the byte-order
3485 % specified by the endian member of the image structure.
3486 %
3487 % The format of the ReadBlobFloat method is:
3488 %
3489 % float ReadBlobFloat(Image *image)
3490 %
3491 % A description of each parameter follows.
3492 %
3493 % o image: the image.
3494 %
3495 */
3496 MagickExport float ReadBlobFloat(Image *image)
3497 {
3498  union
3499  {
3500  unsigned int
3501  unsigned_value;
3502 
3503  float
3504  float_value;
3505  } quantum;
3506 
3507  quantum.float_value=0.0;
3508  quantum.unsigned_value=ReadBlobLong(image);
3509  return(quantum.float_value);
3510 }
3511 
3512 /*
3513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3514 % %
3515 % %
3516 % %
3517 + R e a d B l o b L o n g %
3518 % %
3519 % %
3520 % %
3521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3522 %
3523 % ReadBlobLong() reads a unsigned int value as a 32-bit quantity in the
3524 % byte-order specified by the endian member of the image structure.
3525 %
3526 % The format of the ReadBlobLong method is:
3527 %
3528 % unsigned int ReadBlobLong(Image *image)
3529 %
3530 % A description of each parameter follows.
3531 %
3532 % o image: the image.
3533 %
3534 */
3535 MagickExport unsigned int ReadBlobLong(Image *image)
3536 {
3537  const unsigned char
3538  *p;
3539 
3540  ssize_t
3541  count;
3542 
3543  unsigned char
3544  buffer[4];
3545 
3546  unsigned int
3547  value;
3548 
3549  assert(image != (Image *) NULL);
3550  assert(image->signature == MagickCoreSignature);
3551  *buffer='\0';
3552  p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3553  if (count != 4)
3554  return(0UL);
3555  if (image->endian == LSBEndian)
3556  {
3557  value=(unsigned int) (*p++);
3558  value|=(unsigned int) (*p++) << 8;
3559  value|=(unsigned int) (*p++) << 16;
3560  value|=(unsigned int) (*p++) << 24;
3561  return(value);
3562  }
3563  value=(unsigned int) (*p++) << 24;
3564  value|=(unsigned int) (*p++) << 16;
3565  value|=(unsigned int) (*p++) << 8;
3566  value|=(unsigned int) (*p++);
3567  return(value);
3568 }
3569 
3570 /*
3571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3572 % %
3573 % %
3574 % %
3575 + R e a d B l o b L o n g L o n g %
3576 % %
3577 % %
3578 % %
3579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3580 %
3581 % ReadBlobLongLong() reads a long long value as a 64-bit quantity in the
3582 % byte-order specified by the endian member of the image structure.
3583 %
3584 % The format of the ReadBlobLongLong method is:
3585 %
3586 % MagickSizeType ReadBlobLongLong(Image *image)
3587 %
3588 % A description of each parameter follows.
3589 %
3590 % o image: the image.
3591 %
3592 */
3593 MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3594 {
3595  MagickSizeType
3596  value;
3597 
3598  const unsigned char
3599  *p;
3600 
3601  ssize_t
3602  count;
3603 
3604  unsigned char
3605  buffer[8];
3606 
3607  assert(image != (Image *) NULL);
3608  assert(image->signature == MagickCoreSignature);
3609  *buffer='\0';
3610  p=(const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3611  if (count != 8)
3612  return(MagickULLConstant(0));
3613  if (image->endian == LSBEndian)
3614  {
3615  value=(MagickSizeType) (*p++);
3616  value|=(MagickSizeType) (*p++) << 8;
3617  value|=(MagickSizeType) (*p++) << 16;
3618  value|=(MagickSizeType) (*p++) << 24;
3619  value|=(MagickSizeType) (*p++) << 32;
3620  value|=(MagickSizeType) (*p++) << 40;
3621  value|=(MagickSizeType) (*p++) << 48;
3622  value|=(MagickSizeType) (*p++) << 56;
3623  return(value);
3624  }
3625  value=(MagickSizeType) (*p++) << 56;
3626  value|=(MagickSizeType) (*p++) << 48;
3627  value|=(MagickSizeType) (*p++) << 40;
3628  value|=(MagickSizeType) (*p++) << 32;
3629  value|=(MagickSizeType) (*p++) << 24;
3630  value|=(MagickSizeType) (*p++) << 16;
3631  value|=(MagickSizeType) (*p++) << 8;
3632  value|=(MagickSizeType) (*p++);
3633  return(value);
3634 }
3635 
3636 /*
3637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3638 % %
3639 % %
3640 % %
3641 + R e a d B l o b S h o r t %
3642 % %
3643 % %
3644 % %
3645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3646 %
3647 % ReadBlobShort() reads a short value as a 16-bit quantity in the byte-order
3648 % specified by the endian member of the image structure.
3649 %
3650 % The format of the ReadBlobShort method is:
3651 %
3652 % unsigned short ReadBlobShort(Image *image)
3653 %
3654 % A description of each parameter follows.
3655 %
3656 % o image: the image.
3657 %
3658 */
3659 MagickExport unsigned short ReadBlobShort(Image *image)
3660 {
3661  const unsigned char
3662  *p;
3663 
3664  unsigned short
3665  value;
3666 
3667  ssize_t
3668  count;
3669 
3670  unsigned char
3671  buffer[2];
3672 
3673  assert(image != (Image *) NULL);
3674  assert(image->signature == MagickCoreSignature);
3675  *buffer='\0';
3676  p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3677  if (count != 2)
3678  return((unsigned short) 0U);
3679  if (image->endian == LSBEndian)
3680  {
3681  value=(unsigned short) (*p++);
3682  value|=(unsigned short) (*p++) << 8;
3683  return(value);
3684  }
3685  value=(unsigned short) ((unsigned short) (*p++) << 8);
3686  value|=(unsigned short) (*p++);
3687  return(value);
3688 }
3689 
3690 /*
3691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3692 % %
3693 % %
3694 % %
3695 + R e a d B l o b L S B L o n g %
3696 % %
3697 % %
3698 % %
3699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3700 %
3701 % ReadBlobLSBLong() reads a unsigned int value as a 32-bit quantity in
3702 % least-significant byte first order.
3703 %
3704 % The format of the ReadBlobLSBLong method is:
3705 %
3706 % unsigned int ReadBlobLSBLong(Image *image)
3707 %
3708 % A description of each parameter follows.
3709 %
3710 % o image: the image.
3711 %
3712 */
3713 MagickExport unsigned int ReadBlobLSBLong(Image *image)
3714 {
3715  const unsigned char
3716  *p;
3717 
3718  unsigned int
3719  value;
3720 
3721  ssize_t
3722  count;
3723 
3724  unsigned char
3725  buffer[4];
3726 
3727  assert(image != (Image *) NULL);
3728  assert(image->signature == MagickCoreSignature);
3729  *buffer='\0';
3730  p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3731  if (count != 4)
3732  return(0U);
3733  value=(unsigned int) (*p++);
3734  value|=(unsigned int) (*p++) << 8;
3735  value|=(unsigned int) (*p++) << 16;
3736  value|=(unsigned int) (*p++) << 24;
3737  return(value);
3738 }
3739 
3740 /*
3741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3742 % %
3743 % %
3744 % %
3745 + R e a d B l o b L S B S i g n e d L o n g %
3746 % %
3747 % %
3748 % %
3749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3750 %
3751 % ReadBlobLSBSignedLong() reads a signed int value as a 32-bit quantity in
3752 % least-significant byte first order.
3753 %
3754 % The format of the ReadBlobLSBSignedLong method is:
3755 %
3756 % signed int ReadBlobLSBSignedLong(Image *image)
3757 %
3758 % A description of each parameter follows.
3759 %
3760 % o image: the image.
3761 %
3762 */
3763 MagickExport signed int ReadBlobLSBSignedLong(Image *image)
3764 {
3765  union
3766  {
3767  unsigned int
3768  unsigned_value;
3769 
3770  signed int
3771  signed_value;
3772  } quantum;
3773 
3774  quantum.unsigned_value=ReadBlobLSBLong(image);
3775  return(quantum.signed_value);
3776 }
3777 
3778 /*
3779 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3780 % %
3781 % %
3782 % %
3783 + R e a d B l o b L S B S h o r t %
3784 % %
3785 % %
3786 % %
3787 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3788 %
3789 % ReadBlobLSBShort() reads a short value as a 16-bit quantity in
3790 % least-significant byte first order.
3791 %
3792 % The format of the ReadBlobLSBShort method is:
3793 %
3794 % unsigned short ReadBlobLSBShort(Image *image)
3795 %
3796 % A description of each parameter follows.
3797 %
3798 % o image: the image.
3799 %
3800 */
3801 MagickExport unsigned short ReadBlobLSBShort(Image *image)
3802 {
3803  const unsigned char
3804  *p;
3805 
3806  unsigned short
3807  value;
3808 
3809  ssize_t
3810  count;
3811 
3812  unsigned char
3813  buffer[2];
3814 
3815  assert(image != (Image *) NULL);
3816  assert(image->signature == MagickCoreSignature);
3817  *buffer='\0';
3818  p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3819  if (count != 2)
3820  return((unsigned short) 0U);
3821  value=(unsigned short) (*p++);
3822  value|=(unsigned short) (*p++) << 8;
3823  return(value);
3824 }
3825 
3826 /*
3827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3828 % %
3829 % %
3830 % %
3831 + R e a d B l o b L S B S i g n e d S h o r t %
3832 % %
3833 % %
3834 % %
3835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3836 %
3837 % ReadBlobLSBSignedShort() reads a signed short value as a 16-bit quantity in
3838 % least-significant byte-order.
3839 %
3840 % The format of the ReadBlobLSBSignedShort method is:
3841 %
3842 % signed short ReadBlobLSBSignedShort(Image *image)
3843 %
3844 % A description of each parameter follows.
3845 %
3846 % o image: the image.
3847 %
3848 */
3849 MagickExport signed short ReadBlobLSBSignedShort(Image *image)
3850 {
3851  union
3852  {
3853  unsigned short
3854  unsigned_value;
3855 
3856  signed short
3857  signed_value;
3858  } quantum;
3859 
3860  quantum.unsigned_value=ReadBlobLSBShort(image);
3861  return(quantum.signed_value);
3862 }
3863 
3864 /*
3865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3866 % %
3867 % %
3868 % %
3869 + R e a d B l o b M S B L o n g %
3870 % %
3871 % %
3872 % %
3873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3874 %
3875 % ReadBlobMSBLong() reads a unsigned int value as a 32-bit quantity in
3876 % most-significant byte first order.
3877 %
3878 % The format of the ReadBlobMSBLong method is:
3879 %
3880 % unsigned int ReadBlobMSBLong(Image *image)
3881 %
3882 % A description of each parameter follows.
3883 %
3884 % o image: the image.
3885 %
3886 */
3887 MagickExport unsigned int ReadBlobMSBLong(Image *image)
3888 {
3889  const unsigned char
3890  *p;
3891 
3892  unsigned int
3893  value;
3894 
3895  ssize_t
3896  count;
3897 
3898  unsigned char
3899  buffer[4];
3900 
3901  assert(image != (Image *) NULL);
3902  assert(image->signature == MagickCoreSignature);
3903  *buffer='\0';
3904  p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3905  if (count != 4)
3906  return(0UL);
3907  value=(unsigned int) (*p++) << 24;
3908  value|=(unsigned int) (*p++) << 16;
3909  value|=(unsigned int) (*p++) << 8;
3910  value|=(unsigned int) (*p++);
3911  return(value);
3912 }
3913 
3914 /*
3915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3916 % %
3917 % %
3918 % %
3919 + R e a d B l o b M S B L o n g L o n g %
3920 % %
3921 % %
3922 % %
3923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3924 %
3925 % ReadBlobMSBLongLong() reads a unsigned long int value as a 64-bit quantity
3926 % in most-significant byte first order.
3927 %
3928 % The format of the ReadBlobMSBLongLong method is:
3929 %
3930 % unsigned int ReadBlobMSBLongLong(Image *image)
3931 %
3932 % A description of each parameter follows.
3933 %
3934 % o image: the image.
3935 %
3936 */
3937 MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
3938 {
3939  const unsigned char
3940  *p;
3941 
3942  MagickSizeType
3943  value;
3944 
3945  ssize_t
3946  count;
3947 
3948  unsigned char
3949  buffer[8];
3950 
3951  assert(image != (Image *) NULL);
3952  assert(image->signature == MagickCoreSignature);
3953  *buffer='\0';
3954  p=(const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3955  if (count != 8)
3956  return(MagickULLConstant(0));
3957  value=(MagickSizeType) (*p++) << 56;
3958  value|=(MagickSizeType) (*p++) << 48;
3959  value|=(MagickSizeType) (*p++) << 40;
3960  value|=(MagickSizeType) (*p++) << 32;
3961  value|=(MagickSizeType) (*p++) << 24;
3962  value|=(MagickSizeType) (*p++) << 16;
3963  value|=(MagickSizeType) (*p++) << 8;
3964  value|=(MagickSizeType) (*p++);
3965  return(value);
3966 }
3967 
3968 /*
3969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3970 % %
3971 % %
3972 % %
3973 + R e a d B l o b M S B S h o r t %
3974 % %
3975 % %
3976 % %
3977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3978 %
3979 % ReadBlobMSBShort() reads a short value as a 16-bit quantity in
3980 % most-significant byte first order.
3981 %
3982 % The format of the ReadBlobMSBShort method is:
3983 %
3984 % unsigned short ReadBlobMSBShort(Image *image)
3985 %
3986 % A description of each parameter follows.
3987 %
3988 % o image: the image.
3989 %
3990 */
3991 MagickExport unsigned short ReadBlobMSBShort(Image *image)
3992 {
3993  const unsigned char
3994  *p;
3995 
3996  unsigned short
3997  value;
3998 
3999  ssize_t
4000  count;
4001 
4002  unsigned char
4003  buffer[2];
4004 
4005  assert(image != (Image *) NULL);
4006  assert(image->signature == MagickCoreSignature);
4007  *buffer='\0';
4008  p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4009  if (count != 2)
4010  return((unsigned short) 0U);
4011  value=(unsigned short) ((*p++) << 8);
4012  value|=(unsigned short) (*p++);
4013  return(value);
4014 }
4015 
4016 /*
4017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4018 % %
4019 % %
4020 % %
4021 + R e a d B l o b M S B S i g n e d L o n g %
4022 % %
4023 % %
4024 % %
4025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4026 %
4027 % ReadBlobMSBSignedLong() reads a signed int value as a 32-bit quantity in
4028 % most-significant byte-order.
4029 %
4030 % The format of the ReadBlobMSBSignedLong method is:
4031 %
4032 % signed int ReadBlobMSBSignedLong(Image *image)
4033 %
4034 % A description of each parameter follows.
4035 %
4036 % o image: the image.
4037 %
4038 */
4039 MagickExport signed int ReadBlobMSBSignedLong(Image *image)
4040 {
4041  union
4042  {
4043  unsigned int
4044  unsigned_value;
4045 
4046  signed int
4047  signed_value;
4048  } quantum;
4049 
4050  quantum.unsigned_value=ReadBlobMSBLong(image);
4051  return(quantum.signed_value);
4052 }
4053 
4054 /*
4055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4056 % %
4057 % %
4058 % %
4059 + R e a d B l o b M S B S i g n e d S h o r t %
4060 % %
4061 % %
4062 % %
4063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4064 %
4065 % ReadBlobMSBSignedShort() reads a signed short value as a 16-bit quantity in
4066 % most-significant byte-order.
4067 %
4068 % The format of the ReadBlobMSBSignedShort method is:
4069 %
4070 % signed short ReadBlobMSBSignedShort(Image *image)
4071 %
4072 % A description of each parameter follows.
4073 %
4074 % o image: the image.
4075 %
4076 */
4077 MagickExport signed short ReadBlobMSBSignedShort(Image *image)
4078 {
4079  union
4080  {
4081  unsigned short
4082  unsigned_value;
4083 
4084  signed short
4085  signed_value;
4086  } quantum;
4087 
4088  quantum.unsigned_value=ReadBlobMSBShort(image);
4089  return(quantum.signed_value);
4090 }
4091 
4092 /*
4093 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4094 % %
4095 % %
4096 % %
4097 + R e a d B l o b S i g n e d L o n g %
4098 % %
4099 % %
4100 % %
4101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4102 %
4103 % ReadBlobSignedLong() reads a signed int value as a 32-bit quantity in the
4104 % byte-order specified by the endian member of the image structure.
4105 %
4106 % The format of the ReadBlobSignedLong method is:
4107 %
4108 % signed int ReadBlobSignedLong(Image *image)
4109 %
4110 % A description of each parameter follows.
4111 %
4112 % o image: the image.
4113 %
4114 */
4115 MagickExport signed int ReadBlobSignedLong(Image *image)
4116 {
4117  union
4118  {
4119  unsigned int
4120  unsigned_value;
4121 
4122  signed int
4123  signed_value;
4124  } quantum;
4125 
4126  quantum.unsigned_value=ReadBlobLong(image);
4127  return(quantum.signed_value);
4128 }
4129 
4130 /*
4131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4132 % %
4133 % %
4134 % %
4135 + R e a d B l o b S i g n e d S h o r t %
4136 % %
4137 % %
4138 % %
4139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4140 %
4141 % ReadBlobSignedShort() reads a signed short value as a 16-bit quantity in the
4142 % byte-order specified by the endian member of the image structure.
4143 %
4144 % The format of the ReadBlobSignedShort method is:
4145 %
4146 % signed short ReadBlobSignedShort(Image *image)
4147 %
4148 % A description of each parameter follows.
4149 %
4150 % o image: the image.
4151 %
4152 */
4153 MagickExport signed short ReadBlobSignedShort(Image *image)
4154 {
4155  union
4156  {
4157  unsigned short
4158  unsigned_value;
4159 
4160  signed short
4161  signed_value;
4162  } quantum;
4163 
4164  quantum.unsigned_value=ReadBlobShort(image);
4165  return(quantum.signed_value);
4166 }
4167 
4168 /*
4169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4170 % %
4171 % %
4172 % %
4173 + R e a d B l o b S t r e a m %
4174 % %
4175 % %
4176 % %
4177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4178 %
4179 % ReadBlobStream() reads data from the blob or image file and returns it. It
4180 % returns a pointer to the data buffer you supply or to the image memory
4181 % buffer if its supported (zero-copy). If length is zero, ReadBlobStream()
4182 % returns a count of zero and has no other results. If length is greater than
4183 % MAGICK_SSIZE_MAX, the result is unspecified.
4184 %
4185 % The format of the ReadBlobStream method is:
4186 %
4187 % const void *ReadBlobStream(Image *image,const size_t length,
4188 % void *magick_restrict data,ssize_t *count)
4189 %
4190 % A description of each parameter follows:
4191 %
4192 % o image: the image.
4193 %
4194 % o length: Specifies an integer representing the number of bytes to read
4195 % from the file.
4196 %
4197 % o count: returns the number of bytes read.
4198 %
4199 % o data: Specifies an area to place the information requested from the
4200 % file.
4201 %
4202 */
4203 MagickExport magick_hot_spot const void *ReadBlobStream(Image *image,
4204  const size_t length,void *magick_restrict data,ssize_t *count)
4205 {
4206  BlobInfo
4207  *magick_restrict blob_info;
4208 
4209  assert(image != (Image *) NULL);
4210  assert(image->signature == MagickCoreSignature);
4211  assert(image->blob != (BlobInfo *) NULL);
4212  assert(image->blob->type != UndefinedStream);
4213  assert(count != (ssize_t *) NULL);
4214  blob_info=image->blob;
4215  if (blob_info->type != BlobStream)
4216  {
4217  assert(data != NULL);
4218  *count=ReadBlob(image,length,(unsigned char *) data);
4219  return(data);
4220  }
4221  if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4222  {
4223  *count=0;
4224  blob_info->eof=MagickTrue;
4225  return(data);
4226  }
4227  data=blob_info->data+blob_info->offset;
4228  *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4229  blob_info->length-blob_info->offset);
4230  blob_info->offset+=(*count);
4231  if (*count != (ssize_t) length)
4232  blob_info->eof=MagickTrue;
4233  return(data);
4234 }
4235 
4236 /*
4237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4238 % %
4239 % %
4240 % %
4241 + R e a d B l o b S t r i n g %
4242 % %
4243 % %
4244 % %
4245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4246 %
4247 % ReadBlobString() reads characters from a blob or file until a newline
4248 % character is read or an end-of-file condition is encountered.
4249 %
4250 % The format of the ReadBlobString method is:
4251 %
4252 % char *ReadBlobString(Image *image,char *string)
4253 %
4254 % A description of each parameter follows:
4255 %
4256 % o image: the image.
4257 %
4258 % o string: the address of a character buffer.
4259 %
4260 */
4261 MagickExport char *ReadBlobString(Image *image,char *string)
4262 {
4263  BlobInfo
4264  *magick_restrict blob_info;
4265 
4266  int
4267  c = -1;
4268 
4269  ssize_t
4270  i = 0;
4271 
4272  assert(image != (Image *) NULL);
4273  assert(image->signature == MagickCoreSignature);
4274  assert(image->blob != (BlobInfo *) NULL);
4275  assert(image->blob->type != UndefinedStream);
4276  if (IsEventLogging() != MagickFalse)
4277  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4278  *string='\0';
4279  blob_info=image->blob;
4280  switch (blob_info->type)
4281  {
4282  case UndefinedStream:
4283  break;
4284  case StandardStream:
4285  case FileStream:
4286  {
4287  char *p = fgets(string,MagickPathExtent,blob_info->file_info.file);
4288  if (p == (char *) NULL)
4289  {
4290  if (ferror(blob_info->file_info.file) != 0)
4291  ThrowBlobException(blob_info);
4292  return((char *) NULL);
4293  }
4294  i=strlen(string);
4295  break;
4296  }
4297  case ZipStream:
4298  {
4299 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4300  char *p = gzgets(blob_info->file_info.gzfile,string,MagickPathExtent);
4301  if (p == (char *) NULL)
4302  {
4303  int status = Z_OK;
4304  (void) gzerror(blob_info->file_info.gzfile,&status);
4305  if (status != Z_OK)
4306  ThrowBlobException(blob_info);
4307  return((char *) NULL);
4308  }
4309  i=strlen(string);
4310  break;
4311 #endif
4312  }
4313  default:
4314  {
4315  do
4316  {
4317  c=ReadBlobByte(image);
4318  if (c == EOF)
4319  {
4320  blob_info->eof=MagickTrue;
4321  break;
4322  }
4323  string[i++]=c;
4324  if (c == '\n')
4325  break;
4326  } while (i < (MaxTextExtent-2));
4327  string[i]='\0';
4328  break;
4329  }
4330  }
4331  /*
4332  Strip trailing newline.
4333  */
4334  if ((string[i] == '\r') || (string[i] == '\n'))
4335  string[i]='\0';
4336  if (i >= 1)
4337  if ((string[i-1] == '\r') || (string[i-1] == '\n'))
4338  string[i-1]='\0';
4339  if ((*string == '\0') && (blob_info->eof != MagickFalse))
4340  return((char *) NULL);
4341  return(string);
4342 }
4343 
4344 /*
4345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4346 % %
4347 % %
4348 % %
4349 + R e f e r e n c e B l o b %
4350 % %
4351 % %
4352 % %
4353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4354 %
4355 % ReferenceBlob() increments the reference count associated with the pixel
4356 % blob returning a pointer to the blob.
4357 %
4358 % The format of the ReferenceBlob method is:
4359 %
4360 % BlobInfo ReferenceBlob(BlobInfo *blob_info)
4361 %
4362 % A description of each parameter follows:
4363 %
4364 % o blob_info: the blob_info.
4365 %
4366 */
4367 MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4368 {
4369  assert(blob != (BlobInfo *) NULL);
4370  assert(blob->signature == MagickCoreSignature);
4371  if (IsEventLogging() != MagickFalse)
4372  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4373  LockSemaphoreInfo(blob->semaphore);
4374  blob->reference_count++;
4375  UnlockSemaphoreInfo(blob->semaphore);
4376  return(blob);
4377 }
4378 
4379 /*
4380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4381 % %
4382 % %
4383 % %
4384 + S e e k B l o b %
4385 % %
4386 % %
4387 % %
4388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4389 %
4390 % SeekBlob() sets the offset in bytes from the beginning of a blob or file
4391 % and returns the resulting offset.
4392 %
4393 % The format of the SeekBlob method is:
4394 %
4395 % MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
4396 % const int whence)
4397 %
4398 % A description of each parameter follows:
4399 %
4400 % o image: the image.
4401 %
4402 % o offset: Specifies an integer representing the offset in bytes.
4403 %
4404 % o whence: Specifies an integer representing how the offset is
4405 % treated relative to the beginning of the blob as follows:
4406 %
4407 % SEEK_SET Set position equal to offset bytes.
4408 % SEEK_CUR Set position to current location plus offset.
4409 % SEEK_END Set position to EOF plus offset.
4410 %
4411 */
4412 MagickExport MagickOffsetType SeekBlob(Image *image,
4413  const MagickOffsetType offset,const int whence)
4414 {
4415  BlobInfo
4416  *magick_restrict blob_info;
4417 
4418  assert(image != (Image *) NULL);
4419  assert(image->signature == MagickCoreSignature);
4420  assert(image->blob != (BlobInfo *) NULL);
4421  assert(image->blob->type != UndefinedStream);
4422  if (IsEventLogging() != MagickFalse)
4423  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4424  blob_info=image->blob;
4425  switch (blob_info->type)
4426  {
4427  case UndefinedStream:
4428  break;
4429  case StandardStream:
4430  case PipeStream:
4431  return(-1);
4432  case FileStream:
4433  {
4434  if ((offset < 0) && (whence == SEEK_SET))
4435  return(-1);
4436  if (fseek(blob_info->file_info.file,offset,whence) < 0)
4437  return(-1);
4438  blob_info->offset=TellBlob(image);
4439  break;
4440  }
4441  case ZipStream:
4442  {
4443 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4444  if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4445  return(-1);
4446 #endif
4447  blob_info->offset=TellBlob(image);
4448  break;
4449  }
4450  case BZipStream:
4451  return(-1);
4452  case FifoStream:
4453  return(-1);
4454  case BlobStream:
4455  {
4456  switch (whence)
4457  {
4458  case SEEK_SET:
4459  default:
4460  {
4461  if (offset < 0)
4462  return(-1);
4463  blob_info->offset=offset;
4464  break;
4465  }
4466  case SEEK_CUR:
4467  {
4468  if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4469  ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4470  {
4471  errno=EOVERFLOW;
4472  return(-1);
4473  }
4474  if ((blob_info->offset+offset) < 0)
4475  return(-1);
4476  blob_info->offset+=offset;
4477  break;
4478  }
4479  case SEEK_END:
4480  {
4481  if (((MagickOffsetType) blob_info->length+offset) < 0)
4482  return(-1);
4483  blob_info->offset=blob_info->length+offset;
4484  break;
4485  }
4486  }
4487  if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4488  {
4489  blob_info->eof=MagickFalse;
4490  break;
4491  }
4492  if (blob_info->offset >= (MagickOffsetType) ((off_t) blob_info->extent))
4493  return(-1);
4494  break;
4495  }
4496  }
4497  return(blob_info->offset);
4498 }
4499 
4500 /*
4501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4502 % %
4503 % %
4504 % %
4505 + S e t B l o b E x e m p t %
4506 % %
4507 % %
4508 % %
4509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4510 %
4511 % SetBlobExempt() sets the blob exempt status.
4512 %
4513 % The format of the SetBlobExempt method is:
4514 %
4515 % MagickBooleanType SetBlobExempt(const Image *image,
4516 % const MagickBooleanType exempt)
4517 %
4518 % A description of each parameter follows:
4519 %
4520 % o image: the image.
4521 %
4522 % o exempt: Set to true if this blob is exempt from being closed.
4523 %
4524 */
4525 MagickExport void SetBlobExempt(Image *image,const MagickBooleanType exempt)
4526 {
4527  assert(image != (const Image *) NULL);
4528  assert(image->signature == MagickCoreSignature);
4529  if (IsEventLogging() != MagickFalse)
4530  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4531  image->blob->exempt=exempt;
4532 }
4533 
4534 /*
4535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4536 % %
4537 % %
4538 % %
4539 + S e t B l o b E x t e n t %
4540 % %
4541 % %
4542 % %
4543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4544 %
4545 % SetBlobExtent() ensures enough space is allocated for the blob. If the
4546 % method is successful, subsequent writes to bytes in the specified range are
4547 % guaranteed not to fail.
4548 %
4549 % The format of the SetBlobExtent method is:
4550 %
4551 % MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
4552 %
4553 % A description of each parameter follows:
4554 %
4555 % o image: the image.
4556 %
4557 % o extent: the blob maximum extent.
4558 %
4559 */
4560 MagickExport MagickBooleanType SetBlobExtent(Image *image,
4561  const MagickSizeType extent)
4562 {
4563  BlobInfo
4564  *magick_restrict blob_info;
4565 
4566  assert(image != (Image *) NULL);
4567  assert(image->signature == MagickCoreSignature);
4568  assert(image->blob != (BlobInfo *) NULL);
4569  assert(image->blob->type != UndefinedStream);
4570  if (IsEventLogging() != MagickFalse)
4571  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4572  blob_info=image->blob;
4573  switch (blob_info->type)
4574  {
4575  case UndefinedStream:
4576  break;
4577  case StandardStream:
4578  return(MagickFalse);
4579  case FileStream:
4580  {
4581  MagickOffsetType
4582  offset;
4583 
4584  ssize_t
4585  count;
4586 
4587  if (extent != (MagickSizeType) ((off_t) extent))
4588  return(MagickFalse);
4589  offset=SeekBlob(image,0,SEEK_END);
4590  if (offset < 0)
4591  return(MagickFalse);
4592  if ((MagickSizeType) offset >= extent)
4593  break;
4594  offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4595  if (offset < 0)
4596  break;
4597  count=(ssize_t) fwrite((const unsigned char *) "",1,1,
4598  blob_info->file_info.file);
4599 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4600  if (blob_info->synchronize != MagickFalse)
4601  {
4602  int
4603  file;
4604 
4605  file=fileno(blob_info->file_info.file);
4606  if ((file == -1) || (offset < 0))
4607  return(MagickFalse);
4608  (void) posix_fallocate(file,offset,extent-offset);
4609  }
4610 #endif
4611  offset=SeekBlob(image,offset,SEEK_SET);
4612  if (count != 1)
4613  return(MagickFalse);
4614  break;
4615  }
4616  case PipeStream:
4617  case ZipStream:
4618  return(MagickFalse);
4619  case BZipStream:
4620  return(MagickFalse);
4621  case FifoStream:
4622  return(MagickFalse);
4623  case BlobStream:
4624  {
4625  if (extent != (MagickSizeType) ((size_t) extent))
4626  return(MagickFalse);
4627  if (blob_info->mapped != MagickFalse)
4628  {
4629  MagickOffsetType
4630  offset;
4631 
4632  ssize_t
4633  count;
4634 
4635  (void) UnmapBlob(blob_info->data,blob_info->length);
4636  RelinquishMagickResource(MapResource,blob_info->length);
4637  if (extent != (MagickSizeType) ((off_t) extent))
4638  return(MagickFalse);
4639  offset=SeekBlob(image,0,SEEK_END);
4640  if (offset < 0)
4641  return(MagickFalse);
4642  if ((MagickSizeType) offset >= extent)
4643  break;
4644  offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4645  count=(ssize_t) fwrite((const unsigned char *) "",1,1,
4646  blob_info->file_info.file);
4647 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4648  if (blob_info->synchronize != MagickFalse)
4649  {
4650  int
4651  file;
4652 
4653  file=fileno(blob_info->file_info.file);
4654  if ((file == -1) || (offset < 0))
4655  return(MagickFalse);
4656  (void) posix_fallocate(file,offset,extent-offset);
4657  }
4658 #endif
4659  offset=SeekBlob(image,offset,SEEK_SET);
4660  if (count != 1)
4661  return(MagickFalse);
4662  (void) AcquireMagickResource(MapResource,extent);
4663  blob_info->data=(unsigned char*) MapBlob(fileno(
4664  blob_info->file_info.file),WriteMode,0,(size_t) extent);
4665  blob_info->extent=(size_t) extent;
4666  blob_info->length=(size_t) extent;
4667  (void) SyncBlob(image);
4668  break;
4669  }
4670  blob_info->extent=(size_t) extent;
4671  blob_info->data=(unsigned char *) ResizeQuantumMemory(blob_info->data,
4672  blob_info->extent+1,sizeof(*blob_info->data));
4673  (void) SyncBlob(image);
4674  if (blob_info->data == (unsigned char *) NULL)
4675  {
4676  (void) DetachBlob(blob_info);
4677  return(MagickFalse);
4678  }
4679  break;
4680  }
4681  }
4682  return(MagickTrue);
4683 }
4684 
4685 /*
4686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4687 % %
4688 % %
4689 % %
4690 + S y n c B l o b %
4691 % %
4692 % %
4693 % %
4694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4695 %
4696 % SyncBlob() flushes the datastream if it is a file or synchronizes the data
4697 % attributes if it is an blob.
4698 %
4699 % The format of the SyncBlob method is:
4700 %
4701 % int SyncBlob(Image *image)
4702 %
4703 % A description of each parameter follows:
4704 %
4705 % o image: the image.
4706 %
4707 */
4708 static int SyncBlob(Image *image)
4709 {
4710  BlobInfo
4711  *magick_restrict blob_info;
4712 
4713  int
4714  status;
4715 
4716  assert(image != (Image *) NULL);
4717  assert(image->signature == MagickCoreSignature);
4718  assert(image->blob != (BlobInfo *) NULL);
4719  assert(image->blob->type != UndefinedStream);
4720  if (IsEventLogging() != MagickFalse)
4721  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4722  blob_info=image->blob;
4723  status=0;
4724  switch (blob_info->type)
4725  {
4726  case UndefinedStream:
4727  case StandardStream:
4728  break;
4729  case FileStream:
4730  case PipeStream:
4731  {
4732  status=fflush(blob_info->file_info.file);
4733  break;
4734  }
4735  case ZipStream:
4736  {
4737 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4738  status=gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4739 #endif
4740  break;
4741  }
4742  case BZipStream:
4743  {
4744 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4745  status=BZ2_bzflush(blob_info->file_info.bzfile);
4746 #endif
4747  break;
4748  }
4749  case FifoStream:
4750  break;
4751  case BlobStream:
4752  break;
4753  }
4754  return(status);
4755 }
4756 
4757 /*
4758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4759 % %
4760 % %
4761 % %
4762 + T e l l B l o b %
4763 % %
4764 % %
4765 % %
4766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4767 %
4768 % TellBlob() obtains the current value of the blob or file position.
4769 %
4770 % The format of the TellBlob method is:
4771 %
4772 % MagickOffsetType TellBlob(const Image *image)
4773 %
4774 % A description of each parameter follows:
4775 %
4776 % o image: the image.
4777 %
4778 */
4779 MagickExport MagickOffsetType TellBlob(const Image *image)
4780 {
4781  BlobInfo
4782  *magick_restrict blob_info;
4783 
4784  MagickOffsetType
4785  offset;
4786 
4787  assert(image != (Image *) NULL);
4788  assert(image->signature == MagickCoreSignature);
4789  assert(image->blob != (BlobInfo *) NULL);
4790  assert(image->blob->type != UndefinedStream);
4791  if (IsEventLogging() != MagickFalse)
4792  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4793  blob_info=image->blob;
4794  offset=(-1);
4795  switch (blob_info->type)
4796  {
4797  case UndefinedStream:
4798  case StandardStream:
4799  break;
4800  case FileStream:
4801  {
4802  offset=ftell(blob_info->file_info.file);
4803  break;
4804  }
4805  case PipeStream:
4806  break;
4807  case ZipStream:
4808  {
4809 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4810  offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4811 #endif
4812  break;
4813  }
4814  case BZipStream:
4815  break;
4816  case FifoStream:
4817  break;
4818  case BlobStream:
4819  {
4820  offset=blob_info->offset;
4821  break;
4822  }
4823  }
4824  return(offset);
4825 }
4826 
4827 /*
4828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4829 % %
4830 % %
4831 % %
4832 + U n m a p B l o b %
4833 % %
4834 % %
4835 % %
4836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4837 %
4838 % UnmapBlob() deallocates the binary large object previously allocated with
4839 % the MapBlob method.
4840 %
4841 % The format of the UnmapBlob method is:
4842 %
4843 % MagickBooleanType UnmapBlob(void *map,const size_t length)
4844 %
4845 % A description of each parameter follows:
4846 %
4847 % o map: the address of the binary large object.
4848 %
4849 % o length: the length of the binary large object.
4850 %
4851 */
4852 MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
4853 {
4854 #if defined(MAGICKCORE_HAVE_MMAP)
4855  int
4856  status;
4857 
4858  status=munmap(map,length);
4859  return(status == -1 ? MagickFalse : MagickTrue);
4860 #else
4861  (void) map;
4862  (void) length;
4863  return(MagickFalse);
4864 #endif
4865 }
4866 
4867 /*
4868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4869 % %
4870 % %
4871 % %
4872 + W r i t e B l o b %
4873 % %
4874 % %
4875 % %
4876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4877 %
4878 % WriteBlob() writes data to a blob or image file. It returns the number of
4879 % bytes written.
4880 %
4881 % The format of the WriteBlob method is:
4882 %
4883 % ssize_t WriteBlob(Image *image,const size_t length,
4884 % const unsigned char *data)
4885 %
4886 % A description of each parameter follows:
4887 %
4888 % o image: the image.
4889 %
4890 % o length: Specifies an integer representing the number of bytes to
4891 % write to the file.
4892 %
4893 % o data: The address of the data to write to the blob or file.
4894 %
4895 */
4896 MagickExport ssize_t WriteBlob(Image *image,const size_t length,
4897  const unsigned char *data)
4898 {
4899  BlobInfo
4900  *magick_restrict blob_info;
4901 
4902  int
4903  c;
4904 
4905  const unsigned char
4906  *p;
4907 
4908  unsigned char
4909  *q;
4910 
4911  ssize_t
4912  count;
4913 
4914  assert(image != (Image *) NULL);
4915  assert(image->signature == MagickCoreSignature);
4916  assert(image->blob != (BlobInfo *) NULL);
4917  assert(image->blob->type != UndefinedStream);
4918  if (length == 0)
4919  return(0);
4920  assert(data != (const unsigned char *) NULL);
4921  blob_info=image->blob;
4922  count=0;
4923  p=(const unsigned char *) data;
4924  q=(unsigned char *) data;
4925  switch (blob_info->type)
4926  {
4927  case UndefinedStream:
4928  break;
4929  case StandardStream:
4930  case FileStream:
4931  case PipeStream:
4932  {
4933  switch (length)
4934  {
4935  default:
4936  {
4937  count=(ssize_t) fwrite((const char *) data,1,length,
4938  blob_info->file_info.file);
4939  break;
4940  }
4941  case 4:
4942  {
4943  c=putc((int) *p++,blob_info->file_info.file);
4944  if (c == EOF)
4945  break;
4946  count++;
4947  }
4948  case 3:
4949  {
4950  c=putc((int) *p++,blob_info->file_info.file);
4951  if (c == EOF)
4952  break;
4953  count++;
4954  }
4955  case 2:
4956  {
4957  c=putc((int) *p++,blob_info->file_info.file);
4958  if (c == EOF)
4959  break;
4960  count++;
4961  }
4962  case 1:
4963  {
4964  c=putc((int) *p++,blob_info->file_info.file);
4965  if (c == EOF)
4966  break;
4967  count++;
4968  }
4969  case 0:
4970  break;
4971  }
4972  if ((count != (ssize_t) length) &&
4973  (ferror(blob_info->file_info.file) != 0))
4974  ThrowBlobException(blob_info);
4975  break;
4976  }
4977  case ZipStream:
4978  {
4979 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4980  int
4981  status;
4982 
4983  switch (length)
4984  {
4985  default:
4986  {
4987  ssize_t
4988  i;
4989 
4990  for (i=0; i < (ssize_t) length; i+=count)
4991  {
4992  count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
4993  (unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4994  if (count <= 0)
4995  {
4996  count=0;
4997  if (errno != EINTR)
4998  break;
4999  }
5000  }
5001  count=i;
5002  break;
5003  }
5004  case 4:
5005  {
5006  c=gzputc(blob_info->file_info.gzfile,(int) *p++);
5007  if (c == EOF)
5008  break;
5009  count++;
5010  }
5011  case 3:
5012  {
5013  c=gzputc(blob_info->file_info.gzfile,(int) *p++);
5014  if (c == EOF)
5015  break;
5016  count++;
5017  }
5018  case 2:
5019  {
5020  c=gzputc(blob_info->file_info.gzfile,(int) *p++);
5021  if (c == EOF)
5022  break;
5023  count++;
5024  }
5025  case 1:
5026  {
5027  c=gzputc(blob_info->file_info.gzfile,(int) *p++);
5028  if (c == EOF)
5029  break;
5030  count++;
5031  }
5032  case 0:
5033  break;
5034  }
5035  status=Z_OK;
5036  (void) gzerror(blob_info->file_info.gzfile,&status);
5037  if ((count != (ssize_t) length) && (status != Z_OK))
5038  ThrowBlobException(blob_info);
5039 #endif
5040  break;
5041  }
5042  case BZipStream:
5043  {
5044 #if defined(MAGICKCORE_BZLIB_DELEGATE)
5045  int
5046  status;
5047 
5048  ssize_t
5049  i;
5050 
5051  for (i=0; i < (ssize_t) length; i+=count)
5052  {
5053  count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5054  (int) MagickMin(length-i,MagickMaxBufferExtent));
5055  if (count <= 0)
5056  {
5057  count=0;
5058  if (errno != EINTR)
5059  break;
5060  }
5061  }
5062  count=i;
5063  status=BZ_OK;
5064  (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5065  if ((count != (ssize_t) length) && (status != BZ_OK))
5066  ThrowBlobException(blob_info);
5067 #endif
5068  break;
5069  }
5070  case FifoStream:
5071  {
5072  count=(ssize_t) blob_info->stream(image,data,length);
5073  break;
5074  }
5075  case BlobStream:
5076  {
5077  if ((blob_info->offset+(MagickOffsetType) length) >=
5078  (MagickOffsetType) blob_info->extent)
5079  {
5080  if (blob_info->mapped != MagickFalse)
5081  return(0);
5082  blob_info->extent+=length+blob_info->quantum;
5083  blob_info->quantum<<=1;
5084  blob_info->data=(unsigned char *) ResizeQuantumMemory(
5085  blob_info->data,blob_info->extent+1,sizeof(*blob_info->data));
5086  (void) SyncBlob(image);
5087  if (blob_info->data == (unsigned char *) NULL)
5088  {
5089  (void) DetachBlob(blob_info);
5090  return(0);
5091  }
5092  }
5093  q=blob_info->data+blob_info->offset;
5094  (void) memcpy(q,p,length);
5095  blob_info->offset+=length;
5096  if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5097  blob_info->length=(size_t) blob_info->offset;
5098  count=(ssize_t) length;
5099  }
5100  }
5101  if (count != (ssize_t) length)
5102  ThrowBlobException(blob_info);
5103  return(count);
5104 }
5105 
5106 /*
5107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5108 % %
5109 % %
5110 % %
5111 + W r i t e B l o b B y t e %
5112 % %
5113 % %
5114 % %
5115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5116 %
5117 % WriteBlobByte() write an integer to a blob. It returns the number of bytes
5118 % written (either 0 or 1);
5119 %
5120 % The format of the WriteBlobByte method is:
5121 %
5122 % ssize_t WriteBlobByte(Image *image,const unsigned char value)
5123 %
5124 % A description of each parameter follows.
5125 %
5126 % o image: the image.
5127 %
5128 % o value: Specifies the value to write.
5129 %
5130 */
5131 MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
5132 {
5133  BlobInfo
5134  *magick_restrict blob_info;
5135 
5136  ssize_t
5137  count;
5138 
5139  assert(image != (Image *) NULL);
5140  assert(image->signature == MagickCoreSignature);
5141  assert(image->blob != (BlobInfo *) NULL);
5142  assert(image->blob->type != UndefinedStream);
5143  blob_info=image->blob;
5144  count=0;
5145  switch (blob_info->type)
5146  {
5147  case StandardStream:
5148  case FileStream:
5149  case PipeStream:
5150  {
5151  int
5152  c;
5153 
5154  c=putc((int) value,blob_info->file_info.file);
5155  if (c == EOF)
5156  {
5157  if (ferror(blob_info->file_info.file) != 0)
5158  ThrowBlobException(blob_info);
5159  break;
5160  }
5161  count++;
5162  break;
5163  }
5164  default:
5165  {
5166  count=WriteBlobStream(image,1,&value);
5167  break;
5168  }
5169  }
5170  return(count);
5171 }
5172 
5173 /*
5174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5175 % %
5176 % %
5177 % %
5178 + W r i t e B l o b F l o a t %
5179 % %
5180 % %
5181 % %
5182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5183 %
5184 % WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
5185 % specified by the endian member of the image structure.
5186 %
5187 % The format of the WriteBlobFloat method is:
5188 %
5189 % ssize_t WriteBlobFloat(Image *image,const float value)
5190 %
5191 % A description of each parameter follows.
5192 %
5193 % o image: the image.
5194 %
5195 % o value: Specifies the value to write.
5196 %
5197 */
5198 MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
5199 {
5200  union
5201  {
5202  unsigned int
5203  unsigned_value;
5204 
5205  float
5206  float_value;
5207  } quantum;
5208 
5209  quantum.unsigned_value=0U;
5210  quantum.float_value=value;
5211  return(WriteBlobLong(image,quantum.unsigned_value));
5212 }
5213 
5214 /*
5215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5216 % %
5217 % %
5218 % %
5219 + W r i t e B l o b L o n g %
5220 % %
5221 % %
5222 % %
5223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5224 %
5225 % WriteBlobLong() writes a unsigned int value as a 32-bit quantity in the
5226 % byte-order specified by the endian member of the image structure.
5227 %
5228 % The format of the WriteBlobLong method is:
5229 %
5230 % ssize_t WriteBlobLong(Image *image,const unsigned int value)
5231 %
5232 % A description of each parameter follows.
5233 %
5234 % o image: the image.
5235 %
5236 % o value: Specifies the value to write.
5237 %
5238 */
5239 MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
5240 {
5241  unsigned char
5242  buffer[4];
5243 
5244  assert(image != (Image *) NULL);
5245  assert(image->signature == MagickCoreSignature);
5246  if (image->endian == LSBEndian)
5247  {
5248  buffer[0]=(unsigned char) value;
5249  buffer[1]=(unsigned char) (value >> 8);
5250  buffer[2]=(unsigned char) (value >> 16);
5251  buffer[3]=(unsigned char) (value >> 24);
5252  return(WriteBlobStream(image,4,buffer));
5253  }
5254  buffer[0]=(unsigned char) (value >> 24);
5255  buffer[1]=(unsigned char) (value >> 16);
5256  buffer[2]=(unsigned char) (value >> 8);
5257  buffer[3]=(unsigned char) value;
5258  return(WriteBlobStream(image,4,buffer));
5259 }
5260 
5261 /*
5262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5263 % %
5264 % %
5265 % %
5266 + W r i t e B l o b S h o r t %
5267 % %
5268 % %
5269 % %
5270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5271 %
5272 % WriteBlobShort() writes a short value as a 16-bit quantity in the
5273 % byte-order specified by the endian member of the image structure.
5274 %
5275 % The format of the WriteBlobShort method is:
5276 %
5277 % ssize_t WriteBlobShort(Image *image,const unsigned short value)
5278 %
5279 % A description of each parameter follows.
5280 %
5281 % o image: the image.
5282 %
5283 % o value: Specifies the value to write.
5284 %
5285 */
5286 MagickExport ssize_t WriteBlobShort(Image *image,const unsigned short value)
5287 {
5288  unsigned char
5289  buffer[2];
5290 
5291  assert(image != (Image *) NULL);
5292  assert(image->signature == MagickCoreSignature);
5293  if (image->endian == LSBEndian)
5294  {
5295  buffer[0]=(unsigned char) value;
5296  buffer[1]=(unsigned char) (value >> 8);
5297  return(WriteBlobStream(image,2,buffer));
5298  }
5299  buffer[0]=(unsigned char) (value >> 8);
5300  buffer[1]=(unsigned char) value;
5301  return(WriteBlobStream(image,2,buffer));
5302 }
5303 
5304 /*
5305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5306 % %
5307 % %
5308 % %
5309 + W r i t e B l o b L S B L o n g %
5310 % %
5311 % %
5312 % %
5313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5314 %
5315 % WriteBlobLSBLong() writes a unsigned int value as a 32-bit quantity in
5316 % least-significant byte first order.
5317 %
5318 % The format of the WriteBlobLSBLong method is:
5319 %
5320 % ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
5321 %
5322 % A description of each parameter follows.
5323 %
5324 % o image: the image.
5325 %
5326 % o value: Specifies the value to write.
5327 %
5328 */
5329 MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
5330 {
5331  unsigned char
5332  buffer[4];
5333 
5334  assert(image != (Image *) NULL);
5335  assert(image->signature == MagickCoreSignature);
5336  buffer[0]=(unsigned char) value;
5337  buffer[1]=(unsigned char) (value >> 8);
5338  buffer[2]=(unsigned char) (value >> 16);
5339  buffer[3]=(unsigned char) (value >> 24);
5340  return(WriteBlobStream(image,4,buffer));
5341 }
5342 
5343 /*
5344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5345 % %
5346 % %
5347 % %
5348 + W r i t e B l o b L S B S h o r t %
5349 % %
5350 % %
5351 % %
5352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5353 %
5354 % WriteBlobLSBShort() writes a unsigned short value as a 16-bit quantity in
5355 % least-significant byte first order.
5356 %
5357 % The format of the WriteBlobLSBShort method is:
5358 %
5359 % ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
5360 %
5361 % A description of each parameter follows.
5362 %
5363 % o image: the image.
5364 %
5365 % o value: Specifies the value to write.
5366 %
5367 */
5368 MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
5369 {
5370  unsigned char
5371  buffer[2];
5372 
5373  assert(image != (Image *) NULL);
5374  assert(image->signature == MagickCoreSignature);
5375  buffer[0]=(unsigned char) value;
5376  buffer[1]=(unsigned char) (value >> 8);
5377  return(WriteBlobStream(image,2,buffer));
5378 }
5379 
5380 /*
5381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5382 % %
5383 % %
5384 % %
5385 + W r i t e B l o b L S B S i g n e d L o n g %
5386 % %
5387 % %
5388 % %
5389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5390 %
5391 % WriteBlobLSBSignedLong() writes a signed value as a 32-bit quantity in
5392 % least-significant byte first order.
5393 %
5394 % The format of the WriteBlobLSBSignedLong method is:
5395 %
5396 % ssize_t WriteBlobLSBSignedLong(Image *image,const signed int value)
5397 %
5398 % A description of each parameter follows.
5399 %
5400 % o image: the image.
5401 %
5402 % o value: Specifies the value to write.
5403 %
5404 */
5405 MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,const signed int value)
5406 {
5407  union
5408  {
5409  unsigned int
5410  unsigned_value;
5411 
5412  signed int
5413  signed_value;
5414  } quantum;
5415 
5416  unsigned char
5417  buffer[4];
5418 
5419  assert(image != (Image *) NULL);
5420  assert(image->signature == MagickCoreSignature);
5421  quantum.signed_value=value;
5422  buffer[0]=(unsigned char) quantum.unsigned_value;
5423  buffer[1]=(unsigned char) (quantum.unsigned_value >> 8);
5424  buffer[2]=(unsigned char) (quantum.unsigned_value >> 16);
5425  buffer[3]=(unsigned char) (quantum.unsigned_value >> 24);
5426  return(WriteBlobStream(image,4,buffer));
5427 }
5428 
5429 /*
5430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5431 % %
5432 % %
5433 % %
5434 + W r i t e B l o b L S B S i g n e d S h o r t %
5435 % %
5436 % %
5437 % %
5438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5439 %
5440 % WriteBlobLSBSignedShort() writes a signed short value as a 16-bit quantity
5441 % in least-significant byte first order.
5442 %
5443 % The format of the WriteBlobLSBSignedShort method is:
5444 %
5445 % ssize_t WriteBlobLSBSignedShort(Image *image,const signed short value)
5446 %
5447 % A description of each parameter follows.
5448 %
5449 % o image: the image.
5450 %
5451 % o value: Specifies the value to write.
5452 %
5453 */
5454 MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5455  const signed short value)
5456 {
5457  union
5458  {
5459  unsigned short
5460  unsigned_value;
5461 
5462  signed short
5463  signed_value;
5464  } quantum;
5465 
5466  unsigned char
5467  buffer[2];
5468 
5469  assert(image != (Image *) NULL);
5470  assert(image->signature == MagickCoreSignature);
5471  quantum.signed_value=value;
5472  buffer[0]=(unsigned char) quantum.unsigned_value;
5473  buffer[1]=(unsigned char) (quantum.unsigned_value >> 8);
5474  return(WriteBlobStream(image,2,buffer));
5475 }
5476 
5477 /*
5478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5479 % %
5480 % %
5481 % %
5482 + W r i t e B l o b M S B L o n g %
5483 % %
5484 % %
5485 % %
5486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5487 %
5488 % WriteBlobMSBLong() writes a unsigned int value as a 32-bit quantity in
5489 % most-significant byte first order.
5490 %
5491 % The format of the WriteBlobMSBLong method is:
5492 %
5493 % ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
5494 %
5495 % A description of each parameter follows.
5496 %
5497 % o value: Specifies the value to write.
5498 %
5499 % o image: the image.
5500 %
5501 */
5502 MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
5503 {
5504  unsigned char
5505  buffer[4];
5506 
5507  assert(image != (Image *) NULL);
5508  assert(image->signature == MagickCoreSignature);
5509  buffer[0]=(unsigned char) (value >> 24);
5510  buffer[1]=(unsigned char) (value >> 16);
5511  buffer[2]=(unsigned char) (value >> 8);
5512  buffer[3]=(unsigned char) value;
5513  return(WriteBlobStream(image,4,buffer));
5514 }
5515 
5516 /*
5517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5518 % %
5519 % %
5520 % %
5521 + W r i t e B l o b M S B L o n g L o n g %
5522 % %
5523 % %
5524 % %
5525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5526 %
5527 % WriteBlobMSBLongLong() writes a long long value as a 64-bit quantity in
5528 % most-significant byte first order.
5529 %
5530 % The format of the WriteBlobMSBLongLong method is:
5531 %
5532 % ssize_t WriteBlobMSBLongLong(Image *image,const MagickSizeType value)
5533 %
5534 % A description of each parameter follows.
5535 %
5536 % o value: Specifies the value to write.
5537 %
5538 % o image: the image.
5539 %
5540 */
5541 MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5542  const MagickSizeType value)
5543 {
5544  unsigned char
5545  buffer[8];
5546 
5547  assert(image != (Image *) NULL);
5548  assert(image->signature == MagickCoreSignature);
5549  buffer[0]=(unsigned char) (value >> 56);
5550  buffer[1]=(unsigned char) (value >> 48);
5551  buffer[2]=(unsigned char) (value >> 40);
5552  buffer[3]=(unsigned char) (value >> 32);
5553  buffer[4]=(unsigned char) (value >> 24);
5554  buffer[5]=(unsigned char) (value >> 16);
5555  buffer[6]=(unsigned char) (value >> 8);
5556  buffer[7]=(unsigned char) value;
5557  return(WriteBlobStream(image,8,buffer));
5558 }
5559 
5560 /*
5561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5562 % %
5563 % %
5564 % %
5565 + W r i t e B l o b M S B S h o r t %
5566 % %
5567 % %
5568 % %
5569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5570 %
5571 % WriteBlobMSBShort() writes a unsigned short value as a 16-bit quantity in
5572 % most-significant byte first order.
5573 %
5574 % The format of the WriteBlobMSBShort method is:
5575 %
5576 % ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
5577 %
5578 % A description of each parameter follows.
5579 %
5580 % o value: Specifies the value to write.
5581 %
5582 % o file: Specifies the file to write the data to.
5583 %
5584 */
5585 MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
5586 {
5587  unsigned char
5588  buffer[2];
5589 
5590  assert(image != (Image *) NULL);
5591  assert(image->signature == MagickCoreSignature);
5592  buffer[0]=(unsigned char) (value >> 8);
5593  buffer[1]=(unsigned char) value;
5594  return(WriteBlobStream(image,2,buffer));
5595 }
5596 
5597 /*
5598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5599 % %
5600 % %
5601 % %
5602 + W r i t e B l o b M S B S i g n e d L o n g %
5603 % %
5604 % %
5605 % %
5606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5607 %
5608 % WriteBlobMSBSignedLong() writes a signed value as a 32-bit quantity in
5609 % most-significant byte first order.
5610 %
5611 % The format of the WriteBlobMSBSignedLong method is:
5612 %
5613 % ssize_t WriteBlobMSBSignedLong(Image *image,const signed int value)
5614 %
5615 % A description of each parameter follows.
5616 %
5617 % o image: the image.
5618 %
5619 % o value: Specifies the value to write.
5620 %
5621 */
5622 MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,const signed int value)
5623 {
5624  union
5625  {
5626  unsigned int
5627  unsigned_value;
5628 
5629  signed int
5630  signed_value;
5631  } quantum;
5632 
5633  unsigned char
5634  buffer[4];
5635 
5636  assert(image != (Image *) NULL);
5637  assert(image->signature == MagickCoreSignature);
5638  quantum.signed_value=value;
5639  buffer[0]=(unsigned char) (quantum.unsigned_value >> 24);
5640  buffer[1]=(unsigned char) (quantum.unsigned_value >> 16);
5641  buffer[2]=(unsigned char) (quantum.unsigned_value >> 8);
5642  buffer[3]=(unsigned char) quantum.unsigned_value;
5643  return(WriteBlobStream(image,4,buffer));
5644 }
5645 
5646 /*
5647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5648 % %
5649 % %
5650 % %
5651 + W r i t e B l o b M S B S i g n e d S h o r t %
5652 % %
5653 % %
5654 % %
5655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5656 %
5657 % WriteBlobMSBSignedShort() writes a signed short value as a 16-bit quantity
5658 % in most-significant byte first order.
5659 %
5660 % The format of the WriteBlobMSBSignedShort method is:
5661 %
5662 % ssize_t WriteBlobMSBSignedShort(Image *image,const signed short value)
5663 %
5664 % A description of each parameter follows.
5665 %
5666 % o image: the image.
5667 %
5668 % o value: Specifies the value to write.
5669 %
5670 */
5671 MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5672  const signed short value)
5673 {
5674  union
5675  {
5676  unsigned short
5677  unsigned_value;
5678 
5679  signed short
5680  signed_value;
5681  } quantum;
5682 
5683  unsigned char
5684  buffer[2];
5685 
5686  assert(image != (Image *) NULL);
5687  assert(image->signature == MagickCoreSignature);
5688  quantum.signed_value=value;
5689  buffer[0]=(unsigned char) (quantum.unsigned_value >> 8);
5690  buffer[1]=(unsigned char) quantum.unsigned_value;
5691  return(WriteBlobStream(image,2,buffer));
5692 }
5693 
5694 /*
5695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5696 % %
5697 % %
5698 % %
5699 + W r i t e B l o b S t r i n g %
5700 % %
5701 % %
5702 % %
5703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5704 %
5705 % WriteBlobString() write a string to a blob. It returns the number of
5706 % characters written.
5707 %
5708 % The format of the WriteBlobString method is:
5709 %
5710 % ssize_t WriteBlobString(Image *image,const char *string)
5711 %
5712 % A description of each parameter follows.
5713 %
5714 % o image: the image.
5715 %
5716 % o string: Specifies the string to write.
5717 %
5718 */
5719 MagickExport ssize_t WriteBlobString(Image *image,const char *string)
5720 {
5721  assert(image != (Image *) NULL);
5722  assert(image->signature == MagickCoreSignature);
5723  assert(string != (const char *) NULL);
5724  return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));
5725 }
Definition: image.h:152
Definition: blob.c:99