]> granicus.if.org Git - imagemagick/blob - MagickCore/blob.c
(no commit message)
[imagemagick] / MagickCore / 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 %                                John Cristy                                  %
17 %                                 July 1999                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 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 %    http://www.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 \f
40 /*
41   Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/nt-base-private.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/client.h"
49 #include "MagickCore/constitute.h"
50 #include "MagickCore/delegate.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/locale_.h"
56 #include "MagickCore/log.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/policy.h"
60 #include "MagickCore/resource_.h"
61 #include "MagickCore/semaphore.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/string-private.h"
64 #include "MagickCore/token.h"
65 #include "MagickCore/utility.h"
66 #include "MagickCore/utility-private.h"
67 #if defined(MAGICKCORE_ZLIB_DELEGATE)
68 #include "zlib.h"
69 #endif
70 #if defined(MAGICKCORE_BZLIB_DELEGATE)
71 #include "bzlib.h"
72 #endif
73 \f
74 /*
75   Define declarations.
76 */
77 #define MagickMaxBlobExtent  65541
78 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
79 # define MAP_ANONYMOUS  MAP_ANON
80 #endif
81 #if !defined(MAP_FAILED)
82 #define MAP_FAILED  ((void *) -1)
83 #endif
84 #if !defined(MS_SYNC)
85 #define MS_SYNC  0x04
86 #endif
87 #if defined(__OS2__)
88 #include <io.h>
89 #define _O_BINARY O_BINARY
90 #endif
91 \f
92 /*
93   Typedef declarations.
94 */
95 typedef union FileInfo
96 {
97   FILE
98     *file;
99
100 #if defined(MAGICKCORE_ZLIB_DELEGATE)
101   gzFile
102     gzfile;
103 #endif
104
105 #if defined(MAGICKCORE_BZLIB_DELEGATE)
106   BZFILE
107     *bzfile;
108 #endif
109 } FileInfo;
110
111 struct _BlobInfo
112 {
113   size_t
114     length,
115     extent,
116     quantum;
117
118   MagickBooleanType
119     mapped,
120     eof;
121
122   MagickOffsetType
123     offset;
124
125   MagickSizeType
126     size;
127
128   MagickBooleanType
129     exempt,
130     synchronize,
131     status,
132     temporary;
133
134   StreamType
135     type;
136
137   FileInfo
138     file_info;
139
140   struct stat
141     properties;
142
143   StreamHandler
144     stream;
145
146   unsigned char
147     *data;
148
149   MagickBooleanType
150     debug;
151
152   SemaphoreInfo
153     *semaphore;
154
155   ssize_t
156     reference_count;
157
158   size_t
159     signature;
160 };
161 \f
162 /*
163   Forward declarations.
164 */
165 static int
166   SyncBlob(Image *);
167 \f
168 /*
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 %                                                                             %
171 %                                                                             %
172 %                                                                             %
173 +   A t t a c h B l o b                                                       %
174 %                                                                             %
175 %                                                                             %
176 %                                                                             %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 %
179 %  AttachBlob() attaches a blob to the BlobInfo structure.
180 %
181 %  The format of the AttachBlob method is:
182 %
183 %      void AttachBlob(BlobInfo *blob_info,const void *blob,const size_t length)
184 %
185 %  A description of each parameter follows:
186 %
187 %    o blob_info: Specifies a pointer to a BlobInfo structure.
188 %
189 %    o blob: the address of a character stream in one of the image formats
190 %      understood by ImageMagick.
191 %
192 %    o length: This size_t integer reflects the length in bytes of the blob.
193 %
194 */
195 MagickExport void AttachBlob(BlobInfo *blob_info,const void *blob,
196   const size_t length)
197 {
198   assert(blob_info != (BlobInfo *) NULL);
199   if (blob_info->debug != MagickFalse)
200     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
201   blob_info->length=length;
202   blob_info->extent=length;
203   blob_info->quantum=(size_t) MagickMaxBlobExtent;
204   blob_info->offset=0;
205   blob_info->type=BlobStream;
206   blob_info->file_info.file=(FILE *) NULL;
207   blob_info->data=(unsigned char *) blob;
208   blob_info->mapped=MagickFalse;
209 }
210 \f
211 /*
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 %                                                                             %
214 %                                                                             %
215 %                                                                             %
216 +   B l o b T o F i l e                                                       %
217 %                                                                             %
218 %                                                                             %
219 %                                                                             %
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 %
222 %  BlobToFile() writes a blob to a file.  It returns MagickFalse if an error
223 %  occurs otherwise MagickTrue.
224 %
225 %  The format of the BlobToFile method is:
226 %
227 %       MagickBooleanType BlobToFile(char *filename,const void *blob,
228 %         const size_t length,ExceptionInfo *exception)
229 %
230 %  A description of each parameter follows:
231 %
232 %    o filename: Write the blob to this file.
233 %
234 %    o blob: the address of a blob.
235 %
236 %    o length: This length in bytes of the blob.
237 %
238 %    o exception: return any errors or warnings in this structure.
239 %
240 */
241
242 static inline MagickSizeType MagickMin(const MagickSizeType x,
243   const MagickSizeType y)
244 {
245   if (x < y)
246     return(x);
247   return(y);
248 }
249
250 MagickExport MagickBooleanType BlobToFile(char *filename,const void *blob,
251   const size_t length,ExceptionInfo *exception)
252 {
253   int
254     file;
255
256   register size_t
257     i;
258
259   ssize_t
260     count;
261
262   assert(filename != (const char *) NULL);
263   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
264   assert(blob != (const void *) NULL);
265   if (*filename == '\0')
266     file=AcquireUniqueFileResource(filename);
267   else
268     file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
269   if (file == -1)
270     {
271       ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
272       return(MagickFalse);
273     }
274   for (i=0; i < length; i+=count)
275   {
276     count=(ssize_t) write(file,(const char *) blob+i,(size_t) MagickMin(length-
277       i,(MagickSizeType) SSIZE_MAX));
278     if (count <= 0)
279       {
280         count=0;
281         if (errno != EINTR)
282           break;
283       }
284   }
285   file=close(file);
286   if ((file == -1) || (i < length))
287     {
288       ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
289       return(MagickFalse);
290     }
291   return(MagickTrue);
292 }
293 \f
294 /*
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 %                                                                             %
297 %                                                                             %
298 %                                                                             %
299 %   B l o b T o I m a g e                                                     %
300 %                                                                             %
301 %                                                                             %
302 %                                                                             %
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 %
305 %  BlobToImage() implements direct to memory image formats.  It returns the
306 %  blob as an image.
307 %
308 %  The format of the BlobToImage method is:
309 %
310 %      Image *BlobToImage(const ImageInfo *image_info,const void *blob,
311 %        const size_t length,ExceptionInfo *exception)
312 %
313 %  A description of each parameter follows:
314 %
315 %    o image_info: the image info.
316 %
317 %    o blob: the address of a character stream in one of the image formats
318 %      understood by ImageMagick.
319 %
320 %    o length: This size_t integer reflects the length in bytes of the blob.
321 %
322 %    o exception: return any errors or warnings in this structure.
323 %
324 */
325 MagickExport Image *BlobToImage(const ImageInfo *image_info,const void *blob,
326   const size_t length,ExceptionInfo *exception)
327 {
328   const MagickInfo
329     *magick_info;
330
331   Image
332     *image;
333
334   ImageInfo
335     *blob_info,
336     *clone_info;
337
338   MagickBooleanType
339     status;
340
341   assert(image_info != (ImageInfo *) NULL);
342   assert(image_info->signature == MagickSignature);
343   if (image_info->debug != MagickFalse)
344     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
345       image_info->filename);
346   assert(exception != (ExceptionInfo *) NULL);
347   if ((blob == (const void *) NULL) || (length == 0))
348     {
349       (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
350         "ZeroLengthBlobNotPermitted","'%s'",image_info->filename);
351       return((Image *) NULL);
352     }
353   blob_info=CloneImageInfo(image_info);
354   blob_info->blob=(void *) blob;
355   blob_info->length=length;
356   if (*blob_info->magick == '\0')
357     (void) SetImageInfo(blob_info,0,exception);
358   magick_info=GetMagickInfo(blob_info->magick,exception);
359   if (magick_info == (const MagickInfo *) NULL)
360     {
361       blob_info=DestroyImageInfo(blob_info);
362       (void) ThrowMagickException(exception,GetMagickModule(),
363         MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
364         image_info->filename);
365       return((Image *) NULL);
366     }
367   if (GetMagickBlobSupport(magick_info) != MagickFalse)
368     {
369       /*
370         Native blob support for this image format.
371       */
372       (void) CopyMagickString(blob_info->filename,image_info->filename,
373         MaxTextExtent);
374       (void) CopyMagickString(blob_info->magick,image_info->magick,
375         MaxTextExtent);
376       image=ReadImage(blob_info,exception);
377       if (image != (Image *) NULL)
378         (void) DetachBlob(image->blob);
379       blob_info=DestroyImageInfo(blob_info);
380       return(image);
381     }
382   /*
383     Write blob to a temporary file on disk.
384   */
385   blob_info->blob=(void *) NULL;
386   blob_info->length=0;
387   *blob_info->filename='\0';
388   status=BlobToFile(blob_info->filename,blob,length,exception);
389   if (status == MagickFalse)
390     {
391       (void) RelinquishUniqueFileResource(blob_info->filename);
392       blob_info=DestroyImageInfo(blob_info);
393       return((Image *) NULL);
394     }
395   clone_info=CloneImageInfo(blob_info);
396   (void) FormatLocaleString(clone_info->filename,MaxTextExtent,"%s:%s",
397     blob_info->magick,blob_info->filename);
398   image=ReadImage(clone_info,exception);
399   if (image != (Image *) NULL)
400     {
401       Image
402         *images;
403
404       /*
405         Restore original filenames.
406       */
407       for (images=GetFirstImageInList(image); images != (Image *) NULL; )
408       {
409         (void) CopyMagickMemory(images->filename,image_info->filename,
410           sizeof(images->filename));
411         (void) CopyMagickMemory(images->magick_filename,image_info->filename,
412           sizeof(images->magick_filename));
413         images=GetNextImageInList(images);
414       }
415     }
416   clone_info=DestroyImageInfo(clone_info);
417   (void) RelinquishUniqueFileResource(blob_info->filename);
418   blob_info=DestroyImageInfo(blob_info);
419   return(image);
420 }
421 \f
422 /*
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 %                                                                             %
425 %                                                                             %
426 %                                                                             %
427 +   C l o n e B l o b I n f o                                                 %
428 %                                                                             %
429 %                                                                             %
430 %                                                                             %
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432 %
433 %  CloneBlobInfo() makes a duplicate of the given blob info structure, or if
434 %  blob info is NULL, a new one.
435 %
436 %  The format of the CloneBlobInfo method is:
437 %
438 %      BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
439 %
440 %  A description of each parameter follows:
441 %
442 %    o blob_info: the blob info.
443 %
444 */
445 MagickExport BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
446 {
447   BlobInfo
448     *clone_info;
449
450   clone_info=(BlobInfo *) AcquireMagickMemory(sizeof(*clone_info));
451   if (clone_info == (BlobInfo *) NULL)
452     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
453   GetBlobInfo(clone_info);
454   if (blob_info == (BlobInfo *) NULL)
455     return(clone_info);
456   clone_info->length=blob_info->length;
457   clone_info->extent=blob_info->extent;
458   clone_info->synchronize=blob_info->synchronize;
459   clone_info->quantum=blob_info->quantum;
460   clone_info->mapped=blob_info->mapped;
461   clone_info->eof=blob_info->eof;
462   clone_info->offset=blob_info->offset;
463   clone_info->size=blob_info->size;
464   clone_info->exempt=blob_info->exempt;
465   clone_info->status=blob_info->status;
466   clone_info->temporary=blob_info->temporary;
467   clone_info->type=blob_info->type;
468   clone_info->file_info.file=blob_info->file_info.file;
469   clone_info->properties=blob_info->properties;
470   clone_info->stream=blob_info->stream;
471   clone_info->data=blob_info->data;
472   clone_info->debug=IsEventLogging();
473   clone_info->reference_count=1;
474   return(clone_info);
475 }
476 \f
477 /*
478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479 %                                                                             %
480 %                                                                             %
481 %                                                                             %
482 +   C l o s e B l o b                                                         %
483 %                                                                             %
484 %                                                                             %
485 %                                                                             %
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 %
488 %  CloseBlob() closes a stream associated with the image.
489 %
490 %  The format of the CloseBlob method is:
491 %
492 %      MagickBooleanType CloseBlob(Image *image)
493 %
494 %  A description of each parameter follows:
495 %
496 %    o image: the image.
497 %
498 */
499 MagickExport MagickBooleanType CloseBlob(Image *image)
500 {
501   int
502     status;
503
504   /*
505     Close image file.
506   */
507   assert(image != (Image *) NULL);
508   assert(image->signature == MagickSignature);
509   if (image->debug != MagickFalse)
510     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
511   assert(image->blob != (BlobInfo *) NULL);
512   if (image->blob->type == UndefinedStream)
513     return(MagickTrue);
514   if (image->blob->synchronize != MagickFalse)
515     SyncBlob(image);
516   image->blob->size=GetBlobSize(image);
517   image->extent=image->blob->size;
518   image->blob->eof=MagickFalse;
519   if (image->blob->exempt != MagickFalse)
520     {
521       image->blob->type=UndefinedStream;
522       return(MagickTrue);
523     }
524   status=0;
525   switch (image->blob->type)
526   {
527     case UndefinedStream:
528     case StandardStream:
529       break;
530     case FileStream:
531     case PipeStream:
532     {
533       status=ferror(image->blob->file_info.file);
534       break;
535     }
536     case ZipStream:
537     {
538 #if defined(MAGICKCORE_ZLIB_DELEGATE)
539       (void) gzerror(image->blob->file_info.gzfile,&status);
540 #endif
541       break;
542     }
543     case BZipStream:
544     {
545 #if defined(MAGICKCORE_BZLIB_DELEGATE)
546       (void) BZ2_bzerror(image->blob->file_info.bzfile,&status);
547 #endif
548       break;
549     }
550     case FifoStream:
551     case BlobStream:
552       break;
553   }
554   image->blob->status=status < 0 ? MagickTrue : MagickFalse;
555   switch (image->blob->type)
556   {
557     case UndefinedStream:
558     case StandardStream:
559       break;
560     case FileStream:
561     {
562       if (image->blob->synchronize != MagickFalse)
563         {
564           status=fflush(image->blob->file_info.file);
565           status=fsync(fileno(image->blob->file_info.file));
566         }
567       status=fclose(image->blob->file_info.file);
568       break;
569     }
570     case PipeStream:
571     {
572 #if defined(MAGICKCORE_HAVE_PCLOSE)
573       status=pclose(image->blob->file_info.file);
574 #endif
575       break;
576     }
577     case ZipStream:
578     {
579 #if defined(MAGICKCORE_ZLIB_DELEGATE)
580       status=gzclose(image->blob->file_info.gzfile);
581 #endif
582       break;
583     }
584     case BZipStream:
585     {
586 #if defined(MAGICKCORE_BZLIB_DELEGATE)
587       BZ2_bzclose(image->blob->file_info.bzfile);
588 #endif
589       break;
590     }
591     case FifoStream:
592       break;
593     case BlobStream:
594     {
595       if (image->blob->file_info.file != (FILE *) NULL)
596         {
597           if (image->blob->synchronize != MagickFalse)
598             (void) fsync(fileno(image->blob->file_info.file));
599           status=fclose(image->blob->file_info.file);
600         }
601       break;
602     }
603   }
604   (void) DetachBlob(image->blob);
605   image->blob->status=status < 0 ? MagickTrue : MagickFalse;
606   return(image->blob->status);
607 }
608 \f
609 /*
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
611 %                                                                             %
612 %                                                                             %
613 %                                                                             %
614 +   D e s t r o y B l o b                                                     %
615 %                                                                             %
616 %                                                                             %
617 %                                                                             %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619 %
620 %  DestroyBlob() deallocates memory associated with a blob.
621 %
622 %  The format of the DestroyBlob method is:
623 %
624 %      void DestroyBlob(Image *image)
625 %
626 %  A description of each parameter follows:
627 %
628 %    o image: the image.
629 %
630 */
631 MagickExport void DestroyBlob(Image *image)
632 {
633   MagickBooleanType
634     destroy;
635
636   assert(image != (Image *) NULL);
637   assert(image->signature == MagickSignature);
638   if (image->debug != MagickFalse)
639     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
640   assert(image->blob != (BlobInfo *) NULL);
641   assert(image->blob->signature == MagickSignature);
642   destroy=MagickFalse;
643   LockSemaphoreInfo(image->blob->semaphore);
644   image->blob->reference_count--;
645   assert(image->blob->reference_count >= 0);
646   if (image->blob->reference_count == 0)
647     destroy=MagickTrue;
648   UnlockSemaphoreInfo(image->blob->semaphore);
649   if (destroy == MagickFalse)
650     return;
651   (void) CloseBlob(image);
652   if (image->blob->mapped != MagickFalse)
653     (void) UnmapBlob(image->blob->data,image->blob->length);
654   if (image->blob->semaphore != (SemaphoreInfo *) NULL)
655     DestroySemaphoreInfo(&image->blob->semaphore);
656   image->blob->signature=(~MagickSignature);
657   image->blob=(BlobInfo *) RelinquishMagickMemory(image->blob);
658 }
659 \f
660 /*
661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
662 %                                                                             %
663 %                                                                             %
664 %                                                                             %
665 +   D e t a c h B l o b                                                       %
666 %                                                                             %
667 %                                                                             %
668 %                                                                             %
669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
670 %
671 %  DetachBlob() detaches a blob from the BlobInfo structure.
672 %
673 %  The format of the DetachBlob method is:
674 %
675 %      unsigned char *DetachBlob(BlobInfo *blob_info)
676 %
677 %  A description of each parameter follows:
678 %
679 %    o blob_info: Specifies a pointer to a BlobInfo structure.
680 %
681 */
682 MagickExport unsigned char *DetachBlob(BlobInfo *blob_info)
683 {
684   unsigned char
685     *data;
686
687   assert(blob_info != (BlobInfo *) NULL);
688   if (blob_info->debug != MagickFalse)
689     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
690   if (blob_info->mapped != MagickFalse)
691     (void) UnmapBlob(blob_info->data,blob_info->length);
692   blob_info->mapped=MagickFalse;
693   blob_info->length=0;
694   blob_info->offset=0;
695   blob_info->eof=MagickFalse;
696   blob_info->exempt=MagickFalse;
697   blob_info->type=UndefinedStream;
698   blob_info->file_info.file=(FILE *) NULL;
699   data=blob_info->data;
700   blob_info->data=(unsigned char *) NULL;
701   blob_info->stream=(StreamHandler) NULL;
702   return(data);
703 }
704 \f
705 /*
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707 %                                                                             %
708 %                                                                             %
709 %                                                                             %
710 +  D i s c a r d B l o b B y t e s                                            %
711 %                                                                             %
712 %                                                                             %
713 %                                                                             %
714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715 %
716 %  DiscardBlobBytes() discards bytes in a blob.
717 %
718 %  The format of the DiscardBlobBytes method is:
719 %
720 %      MagickBooleanType DiscardBlobBytes(Image *image,const size_t length)
721 %
722 %  A description of each parameter follows.
723 %
724 %    o image: the image.
725 %
726 %    o length:  the number of bytes to skip.
727 %
728 */
729
730 static inline const unsigned char *ReadBlobStream(Image *image,
731   const size_t length,unsigned char *data,ssize_t *count)
732 {
733   assert(count != (ssize_t *) NULL);
734   assert(image->blob != (BlobInfo *) NULL);
735   if (image->blob->type != BlobStream)
736     {
737       *count=ReadBlob(image,length,data);
738       return(data);
739     }
740   if (image->blob->offset >= (MagickOffsetType) image->blob->length)
741     {
742       *count=0;
743       image->blob->eof=MagickTrue;
744       return(data);
745     }
746   data=image->blob->data+image->blob->offset;
747   *count=(ssize_t) MagickMin(length,(MagickSizeType) (image->blob->length-
748     image->blob->offset));
749   image->blob->offset+=(*count);
750   if (*count != (ssize_t) length)
751     image->blob->eof=MagickTrue;
752   return(data);
753 }
754
755 MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
756   const MagickSizeType length)
757 {
758   register MagickOffsetType
759     i;
760
761   size_t
762     quantum;
763
764   ssize_t
765     count;
766
767   unsigned char
768     buffer[16384];
769
770   assert(image != (Image *) NULL);
771   assert(image->signature == MagickSignature);
772   count=0;
773   for (i=0; i < (MagickOffsetType) length; i+=count)
774   {
775     quantum=(size_t) MagickMin(length-i,sizeof(buffer));
776     (void) ReadBlobStream(image,quantum,buffer,&count);
777     if (count <= 0)
778       {
779         count=0;
780         if (errno != EINTR)
781           break;
782       }
783   }
784   return(i < (MagickOffsetType) length ? MagickFalse : MagickTrue);
785 }
786 \f
787 /*
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
789 %                                                                             %
790 %                                                                             %
791 %                                                                             %
792 +   D u p l i c a t e s B l o b                                               %
793 %                                                                             %
794 %                                                                             %
795 %                                                                             %
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797 %
798 %  DuplicateBlob() duplicates a blob descriptor.
799 %
800 %  The format of the DuplicateBlob method is:
801 %
802 %      void DuplicateBlob(Image *image,const Image *duplicate)
803 %
804 %  A description of each parameter follows:
805 %
806 %    o image: the image.
807 %
808 %    o duplicate: the duplicate image.
809 %
810 */
811 MagickExport void DuplicateBlob(Image *image,const Image *duplicate)
812 {
813   assert(image != (Image *) NULL);
814   assert(image->signature == MagickSignature);
815   if (image->debug != MagickFalse)
816     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
817   assert(duplicate != (Image *) NULL);
818   assert(duplicate->signature == MagickSignature);
819   DestroyBlob(image);
820   image->blob=ReferenceBlob(duplicate->blob);
821 }
822 \f
823 /*
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
825 %                                                                             %
826 %                                                                             %
827 %                                                                             %
828 +  E O F B l o b                                                              %
829 %                                                                             %
830 %                                                                             %
831 %                                                                             %
832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 %
834 %  EOFBlob() returns a non-zero value when EOF has been detected reading from
835 %  a blob or file.
836 %
837 %  The format of the EOFBlob method is:
838 %
839 %      int EOFBlob(const Image *image)
840 %
841 %  A description of each parameter follows:
842 %
843 %    o image: the image.
844 %
845 */
846 MagickExport int EOFBlob(const Image *image)
847 {
848   assert(image != (Image *) NULL);
849   assert(image->signature == MagickSignature);
850   if (image->debug != MagickFalse)
851     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
852   assert(image->blob != (BlobInfo *) NULL);
853   assert(image->blob->type != UndefinedStream);
854   switch (image->blob->type)
855   {
856     case UndefinedStream:
857     case StandardStream:
858       break;
859     case FileStream:
860     case PipeStream:
861     {
862       image->blob->eof=feof(image->blob->file_info.file) != 0 ? MagickTrue :
863         MagickFalse;
864       break;
865     }
866     case ZipStream:
867     {
868       image->blob->eof=MagickFalse;
869       break;
870     }
871     case BZipStream:
872     {
873 #if defined(MAGICKCORE_BZLIB_DELEGATE)
874       int
875         status;
876
877       status=0;
878       (void) BZ2_bzerror(image->blob->file_info.bzfile,&status);
879       image->blob->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
880 #endif
881       break;
882     }
883     case FifoStream:
884     {
885       image->blob->eof=MagickFalse;
886       break;
887     }
888     case BlobStream:
889       break;
890   }
891   return((int) image->blob->eof);
892 }
893 \f
894 /*
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896 %                                                                             %
897 %                                                                             %
898 %                                                                             %
899 +   F i l e T o B l o b                                                       %
900 %                                                                             %
901 %                                                                             %
902 %                                                                             %
903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 %
905 %  FileToBlob() returns the contents of a file as a buffer terminated with
906 %  the '\0' character.  The length of the buffer (not including the extra
907 %  terminating '\0' character) is returned via the 'length' parameter.  Free
908 %  the buffer with RelinquishMagickMemory().
909 %
910 %  The format of the FileToBlob method is:
911 %
912 %      unsigned char *FileToBlob(const char *filename,const size_t extent,
913 %        size_t *length,ExceptionInfo *exception)
914 %
915 %  A description of each parameter follows:
916 %
917 %    o blob:  FileToBlob() returns the contents of a file as a blob.  If
918 %      an error occurs NULL is returned.
919 %
920 %    o filename: the filename.
921 %
922 %    o extent:  The maximum length of the blob.
923 %
924 %    o length: On return, this reflects the actual length of the blob.
925 %
926 %    o exception: return any errors or warnings in this structure.
927 %
928 */
929 MagickExport unsigned char *FileToBlob(const char *filename,const size_t extent,
930   size_t *length,ExceptionInfo *exception)
931 {
932   int
933     file;
934
935   MagickOffsetType
936     offset;
937
938   register size_t
939     i;
940
941   ssize_t
942     count;
943
944   unsigned char
945     *blob;
946
947   void
948     *map;
949
950   assert(filename != (const char *) NULL);
951   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
952   assert(exception != (ExceptionInfo *) NULL);
953   *length=0;
954   file=fileno(stdin);
955   if (LocaleCompare(filename,"-") != 0)
956     file=open_utf8(filename,O_RDONLY | O_BINARY,0);
957   if (file == -1)
958     {
959       ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
960       return((unsigned char *) NULL);
961     }
962   offset=(MagickOffsetType) lseek(file,0,SEEK_END);
963   count=0;
964   if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
965     {
966       size_t
967         quantum;
968
969       struct stat
970         file_stats;
971
972       /*
973         Stream is not seekable.
974       */
975       quantum=(size_t) MagickMaxBufferExtent;
976       if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
977         quantum=(size_t) MagickMin((MagickSizeType) file_stats.st_size,
978           MagickMaxBufferExtent);
979       blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
980       for (i=0; blob != (unsigned char *) NULL; i+=count)
981       {
982         count=(ssize_t) read(file,blob+i,quantum);
983         if (count <= 0)
984           {
985             count=0;
986             if (errno != EINTR)
987               break;
988           }
989         if (~((size_t) i) < (quantum+1))
990           {
991             blob=(unsigned char *) RelinquishMagickMemory(blob);
992             break;
993           }
994         blob=(unsigned char *) ResizeQuantumMemory(blob,i+quantum+1,
995           sizeof(*blob));
996         if ((size_t) (i+count) >= extent)
997           break;
998       }
999       if (LocaleCompare(filename,"-") != 0)
1000         file=close(file);
1001       if (blob == (unsigned char *) NULL)
1002         {
1003           (void) ThrowMagickException(exception,GetMagickModule(),
1004             ResourceLimitError,"MemoryAllocationFailed","'%s'",filename);
1005           return((unsigned char *) NULL);
1006         }
1007       if (file == -1)
1008         {
1009           blob=(unsigned char *) RelinquishMagickMemory(blob);
1010           ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1011           return((unsigned char *) NULL);
1012         }
1013       *length=(size_t) MagickMin(i+count,extent);
1014       blob[*length]='\0';
1015       return(blob);
1016     }
1017   *length=(size_t) MagickMin((MagickSizeType) offset,extent);
1018   blob=(unsigned char *) NULL;
1019   if (~(*length) >= (MaxTextExtent-1))
1020     blob=(unsigned char *) AcquireQuantumMemory(*length+MaxTextExtent,
1021       sizeof(*blob));
1022   if (blob == (unsigned char *) NULL)
1023     {
1024       file=close(file);
1025       (void) ThrowMagickException(exception,GetMagickModule(),
1026         ResourceLimitError,"MemoryAllocationFailed","'%s'",filename);
1027       return((unsigned char *) NULL);
1028     }
1029   map=MapBlob(file,ReadMode,0,*length);
1030   if (map != (unsigned char *) NULL)
1031     {
1032       (void) memcpy(blob,map,*length);
1033       (void) UnmapBlob(map,*length);
1034     }
1035   else
1036     {
1037       (void) lseek(file,0,SEEK_SET);
1038       for (i=0; i < *length; i+=count)
1039       {
1040         count=(ssize_t) read(file,blob+i,(size_t) MagickMin(*length-i,
1041           (MagickSizeType) SSIZE_MAX));
1042         if (count <= 0)
1043           {
1044             count=0;
1045             if (errno != EINTR)
1046               break;
1047           }
1048       }
1049       if (i < *length)
1050         {
1051           file=close(file)-1;
1052           blob=(unsigned char *) RelinquishMagickMemory(blob);
1053           ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1054           return((unsigned char *) NULL);
1055         }
1056     }
1057   blob[*length]='\0';
1058   if (LocaleCompare(filename,"-") != 0)
1059     file=close(file);
1060   if (file == -1)
1061     {
1062       blob=(unsigned char *) RelinquishMagickMemory(blob);
1063       ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1064     }
1065   return(blob);
1066 }
1067 \f
1068 /*
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1070 %                                                                             %
1071 %                                                                             %
1072 %                                                                             %
1073 %   F i l e T o I m a g e                                                     %
1074 %                                                                             %
1075 %                                                                             %
1076 %                                                                             %
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1078 %
1079 %  FileToImage() write the contents of a file to an image.
1080 %
1081 %  The format of the FileToImage method is:
1082 %
1083 %      MagickBooleanType FileToImage(Image *,const char *filename)
1084 %
1085 %  A description of each parameter follows:
1086 %
1087 %    o image: the image.
1088 %
1089 %    o filename: the filename.
1090 %
1091 */
1092
1093 static inline ssize_t WriteBlobStream(Image *image,const size_t length,
1094   const unsigned char *data)
1095 {
1096   MagickSizeType
1097     extent;
1098
1099   register unsigned char
1100     *q;
1101
1102   assert(image->blob != (BlobInfo *) NULL);
1103   if (image->blob->type != BlobStream)
1104     return(WriteBlob(image,length,data));
1105   assert(image->blob->type != UndefinedStream);
1106   assert(data != (void *) NULL);
1107   extent=(MagickSizeType) (image->blob->offset+(MagickOffsetType) length);
1108   if (extent >= image->blob->extent)
1109     {
1110       image->blob->quantum<<=1;
1111       extent=image->blob->extent+image->blob->quantum+length;
1112       if (SetBlobExtent(image,extent) == MagickFalse)
1113         return(0);
1114     }
1115   q=image->blob->data+image->blob->offset;
1116   (void) memcpy(q,data,length);
1117   image->blob->offset+=length;
1118   if (image->blob->offset >= (MagickOffsetType) image->blob->length)
1119     image->blob->length=(size_t) image->blob->offset;
1120   return((ssize_t) length);
1121 }
1122
1123 MagickExport MagickBooleanType FileToImage(Image *image,const char *filename,
1124   ExceptionInfo *exception)
1125 {
1126   int
1127     file;
1128
1129   size_t
1130     length,
1131     quantum;
1132
1133   ssize_t
1134     count;
1135
1136   struct stat
1137     file_stats;
1138
1139   unsigned char
1140     *blob;
1141
1142   assert(image != (const Image *) NULL);
1143   assert(image->signature == MagickSignature);
1144   assert(filename != (const char *) NULL);
1145   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1146   file=fileno(stdin);
1147   if (LocaleCompare(filename,"-") != 0)
1148     file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1149   if (file == -1)
1150     {
1151       ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
1152       return(MagickFalse);
1153     }
1154   quantum=(size_t) MagickMaxBufferExtent;
1155   if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1156     quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1157   blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1158   if (blob == (unsigned char *) NULL)
1159     {
1160       ThrowFileException(exception,ResourceLimitError,"MemoryAllocationFailed",
1161         filename);
1162       return(MagickFalse);
1163     }
1164   for ( ; ; )
1165   {
1166     count=(ssize_t) read(file,blob,quantum);
1167     if (count <= 0)
1168       {
1169         count=0;
1170         if (errno != EINTR)
1171           break;
1172       }
1173     length=(size_t) count;
1174     count=WriteBlobStream(image,length,blob);
1175     if (count != (ssize_t) length)
1176       {
1177         ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1178         break;
1179       }
1180   }
1181   file=close(file);
1182   if (file == -1)
1183     ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1184   blob=(unsigned char *) RelinquishMagickMemory(blob);
1185   return(MagickTrue);
1186 }
1187 \f
1188 /*
1189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1190 %                                                                             %
1191 %                                                                             %
1192 %                                                                             %
1193 +   G e t B l o b E r r o r                                                   %
1194 %                                                                             %
1195 %                                                                             %
1196 %                                                                             %
1197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1198 %
1199 %  GetBlobError() returns MagickTrue if the blob associated with the specified
1200 %  image encountered an error.
1201 %
1202 %  The format of the GetBlobError method is:
1203 %
1204 %       MagickBooleanType GetBlobError(const Image *image)
1205 %
1206 %  A description of each parameter follows:
1207 %
1208 %    o image: the image.
1209 %
1210 */
1211 MagickPrivate MagickBooleanType GetBlobError(const Image *image)
1212 {
1213   assert(image != (const Image *) NULL);
1214   assert(image->signature == MagickSignature);
1215   if (image->debug != MagickFalse)
1216     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1217   return(image->blob->status);
1218 }
1219 \f
1220 /*
1221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1222 %                                                                             %
1223 %                                                                             %
1224 %                                                                             %
1225 +   G e t B l o b F i l e H a n d l e                                         %
1226 %                                                                             %
1227 %                                                                             %
1228 %                                                                             %
1229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1230 %
1231 %  GetBlobFileHandle() returns the file handle associated with the image blob.
1232 %
1233 %  The format of the GetBlobFile method is:
1234 %
1235 %      FILE *GetBlobFileHandle(const Image *image)
1236 %
1237 %  A description of each parameter follows:
1238 %
1239 %    o image: the image.
1240 %
1241 */
1242 MagickExport FILE *GetBlobFileHandle(const Image *image)
1243 {
1244   assert(image != (const Image *) NULL);
1245   assert(image->signature == MagickSignature);
1246   return(image->blob->file_info.file);
1247 }
1248 \f
1249 /*
1250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1251 %                                                                             %
1252 %                                                                             %
1253 %                                                                             %
1254 +   G e t B l o b I n f o                                                     %
1255 %                                                                             %
1256 %                                                                             %
1257 %                                                                             %
1258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259 %
1260 %  GetBlobInfo() initializes the BlobInfo structure.
1261 %
1262 %  The format of the GetBlobInfo method is:
1263 %
1264 %      void GetBlobInfo(BlobInfo *blob_info)
1265 %
1266 %  A description of each parameter follows:
1267 %
1268 %    o blob_info: Specifies a pointer to a BlobInfo structure.
1269 %
1270 */
1271 MagickPrivate void GetBlobInfo(BlobInfo *blob_info)
1272 {
1273   assert(blob_info != (BlobInfo *) NULL);
1274   (void) ResetMagickMemory(blob_info,0,sizeof(*blob_info));
1275   blob_info->type=UndefinedStream;
1276   blob_info->quantum=(size_t) MagickMaxBlobExtent;
1277   blob_info->properties.st_mtime=time((time_t *) NULL);
1278   blob_info->properties.st_ctime=time((time_t *) NULL);
1279   blob_info->debug=IsEventLogging();
1280   blob_info->reference_count=1;
1281   blob_info->semaphore=AllocateSemaphoreInfo();
1282   blob_info->signature=MagickSignature;
1283 }
1284 \f
1285 /*
1286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1287 %                                                                             %
1288 %                                                                             %
1289 %                                                                             %
1290 %  G e t B l o b P r o p e r t i e s                                          %
1291 %                                                                             %
1292 %                                                                             %
1293 %                                                                             %
1294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1295 %
1296 %  GetBlobProperties() returns information about an image blob.
1297 %
1298 %  The format of the GetBlobProperties method is:
1299 %
1300 %      const struct stat *GetBlobProperties(const Image *image)
1301 %
1302 %  A description of each parameter follows:
1303 %
1304 %    o image: the image.
1305 %
1306 */
1307 MagickPrivate const struct stat *GetBlobProperties(const Image *image)
1308 {
1309   assert(image != (Image *) NULL);
1310   assert(image->signature == MagickSignature);
1311   if (image->debug != MagickFalse)
1312     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1313   return(&image->blob->properties);
1314 }
1315 \f
1316 /*
1317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1318 %                                                                             %
1319 %                                                                             %
1320 %                                                                             %
1321 +  G e t B l o b S i z e                                                      %
1322 %                                                                             %
1323 %                                                                             %
1324 %                                                                             %
1325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1326 %
1327 %  GetBlobSize() returns the current length of the image file or blob; zero is
1328 %  returned if the size cannot be determined.
1329 %
1330 %  The format of the GetBlobSize method is:
1331 %
1332 %      MagickSizeType GetBlobSize(const Image *image)
1333 %
1334 %  A description of each parameter follows:
1335 %
1336 %    o image: the image.
1337 %
1338 */
1339 MagickExport MagickSizeType GetBlobSize(const Image *image)
1340 {
1341   MagickSizeType
1342     extent;
1343
1344   assert(image != (Image *) NULL);
1345   assert(image->signature == MagickSignature);
1346   if (image->debug != MagickFalse)
1347     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1348   assert(image->blob != (BlobInfo *) NULL);
1349   extent=0;
1350   switch (image->blob->type)
1351   {
1352     case UndefinedStream:
1353     {
1354       extent=image->blob->size;
1355       break;
1356     }
1357     case StandardStream:
1358     {
1359       extent=image->blob->size;
1360       break;
1361     }
1362     case FileStream:
1363     {
1364       if (fstat(fileno(image->blob->file_info.file),&image->blob->properties) == 0)
1365         extent=(MagickSizeType) image->blob->properties.st_size;
1366       break;
1367     }
1368     case PipeStream:
1369     {
1370       extent=image->blob->size;
1371       break;
1372     }
1373     case ZipStream:
1374     case BZipStream:
1375     {
1376       MagickBooleanType
1377         status;
1378
1379       status=GetPathAttributes(image->filename,&image->blob->properties);
1380       if (status != MagickFalse)
1381         extent=(MagickSizeType) image->blob->properties.st_size;
1382       break;
1383     }
1384     case FifoStream:
1385       break;
1386     case BlobStream:
1387     {
1388       extent=(MagickSizeType) image->blob->length;
1389       break;
1390     }
1391   }
1392   return(extent);
1393 }
1394 \f
1395 /*
1396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1397 %                                                                             %
1398 %                                                                             %
1399 %                                                                             %
1400 +   G e t B l o b S t r e a m D a t a                                         %
1401 %                                                                             %
1402 %                                                                             %
1403 %                                                                             %
1404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1405 %
1406 %  GetBlobStreamData() returns the stream data for the image.
1407 %
1408 %  The format of the GetBlobStreamData method is:
1409 %
1410 %      unsigned char *GetBlobStreamData(const Image *image)
1411 %
1412 %  A description of each parameter follows:
1413 %
1414 %    o image: the image.
1415 %
1416 */
1417 MagickExport unsigned char *GetBlobStreamData(const Image *image)
1418 {
1419   assert(image != (const Image *) NULL);
1420   assert(image->signature == MagickSignature);
1421   return(image->blob->data);
1422 }
1423 \f
1424 /*
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 %                                                                             %
1427 %                                                                             %
1428 %                                                                             %
1429 +   G e t B l o b S t r e a m H a n d l e r                                   %
1430 %                                                                             %
1431 %                                                                             %
1432 %                                                                             %
1433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 %
1435 %  GetBlobStreamHandler() returns the stream handler for the image.
1436 %
1437 %  The format of the GetBlobStreamHandler method is:
1438 %
1439 %      StreamHandler GetBlobStreamHandler(const Image *image)
1440 %
1441 %  A description of each parameter follows:
1442 %
1443 %    o image: the image.
1444 %
1445 */
1446 MagickPrivate StreamHandler GetBlobStreamHandler(const Image *image)
1447 {
1448   assert(image != (const Image *) NULL);
1449   assert(image->signature == MagickSignature);
1450   if (image->debug != MagickFalse)
1451     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1452   return(image->blob->stream);
1453 }
1454 \f
1455 /*
1456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457 %                                                                             %
1458 %                                                                             %
1459 %                                                                             %
1460 %   I m a g e T o B l o b                                                     %
1461 %                                                                             %
1462 %                                                                             %
1463 %                                                                             %
1464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 %
1466 %  ImageToBlob() implements direct to memory image formats.  It returns the
1467 %  image as a formatted blob and its length.  The magick member of the Image
1468 %  structure determines the format of the returned blob (GIF, JPEG, PNG,
1469 %  etc.).  This method is the equivalent of WriteImage(), but writes the
1470 %  formatted "file" to a memory buffer rather than to an actual file.
1471 %
1472 %  The format of the ImageToBlob method is:
1473 %
1474 %      unsigned char *ImageToBlob(const ImageInfo *image_info,Image *image,
1475 %        size_t *length,ExceptionInfo *exception)
1476 %
1477 %  A description of each parameter follows:
1478 %
1479 %    o image_info: the image info.
1480 %
1481 %    o image: the image.
1482 %
1483 %    o length: This pointer to a size_t integer sets the initial length of the
1484 %      blob.  On return, it reflects the actual length of the blob.
1485 %
1486 %    o exception: return any errors or warnings in this structure.
1487 %
1488 */
1489 MagickExport unsigned char *ImageToBlob(const ImageInfo *image_info,
1490   Image *image,size_t *length,ExceptionInfo *exception)
1491 {
1492   const MagickInfo
1493     *magick_info;
1494
1495   ImageInfo
1496     *blob_info;
1497
1498   MagickBooleanType
1499     status;
1500
1501   unsigned char
1502     *blob;
1503
1504   assert(image_info != (const ImageInfo *) NULL);
1505   assert(image_info->signature == MagickSignature);
1506   if (image_info->debug != MagickFalse)
1507     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1508       image_info->filename);
1509   assert(image != (Image *) NULL);
1510   assert(image->signature == MagickSignature);
1511   assert(exception != (ExceptionInfo *) NULL);
1512   *length=0;
1513   blob=(unsigned char *) NULL;
1514   blob_info=CloneImageInfo(image_info);
1515   blob_info->adjoin=MagickFalse;
1516   (void) SetImageInfo(blob_info,1,exception);
1517   if (*blob_info->magick != '\0')
1518     (void) CopyMagickString(image->magick,blob_info->magick,MaxTextExtent);
1519   magick_info=GetMagickInfo(image->magick,exception);
1520   if (magick_info == (const MagickInfo *) NULL)
1521     {
1522       (void) ThrowMagickException(exception,GetMagickModule(),
1523         MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
1524         image->filename);
1525       return(blob);
1526     }
1527   (void) CopyMagickString(blob_info->magick,image->magick,MaxTextExtent);
1528   if (GetMagickBlobSupport(magick_info) != MagickFalse)
1529     {
1530       /*
1531         Native blob support for this image format.
1532       */
1533       blob_info->length=0;
1534       blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1535         sizeof(unsigned char));
1536       if (blob_info->blob == (void *) NULL)
1537         (void) ThrowMagickException(exception,GetMagickModule(),
1538           ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
1539       else
1540         {
1541           (void) CloseBlob(image);
1542           image->blob->exempt=MagickTrue;
1543           *image->filename='\0';
1544           status=WriteImage(blob_info,image,exception);
1545           *length=image->blob->length;
1546           blob=DetachBlob(image->blob);
1547           if (status == MagickFalse)
1548             blob=(unsigned char *) RelinquishMagickMemory(blob);
1549           else
1550             blob=(unsigned char *) ResizeQuantumMemory(blob,*length+1,
1551               sizeof(*blob));
1552         }
1553     }
1554   else
1555     {
1556       char
1557         unique[MaxTextExtent];
1558
1559       int
1560         file;
1561
1562       /*
1563         Write file to disk in blob image format.
1564       */
1565       file=AcquireUniqueFileResource(unique);
1566       if (file == -1)
1567         {
1568           ThrowFileException(exception,BlobError,"UnableToWriteBlob",
1569             image_info->filename);
1570         }
1571       else
1572         {
1573           blob_info->file=fdopen(file,"wb");
1574           if (blob_info->file != (FILE *) NULL)
1575             {
1576               (void) FormatLocaleString(image->filename,MaxTextExtent,"%s:%s",
1577                 image->magick,unique);
1578               status=WriteImage(blob_info,image,exception);
1579               (void) fclose(blob_info->file);
1580               if (status != MagickFalse)
1581                 blob=FileToBlob(image->filename,~0UL,length,exception);
1582             }
1583           (void) RelinquishUniqueFileResource(unique);
1584         }
1585     }
1586   blob_info=DestroyImageInfo(blob_info);
1587   return(blob);
1588 }
1589 \f
1590 /*
1591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1592 %                                                                             %
1593 %                                                                             %
1594 %                                                                             %
1595 %   I m a g e T o F i l e                                                     %
1596 %                                                                             %
1597 %                                                                             %
1598 %                                                                             %
1599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 %
1601 %  ImageToFile() writes an image to a file.  It returns MagickFalse if an error
1602 %  occurs otherwise MagickTrue.
1603 %
1604 %  The format of the ImageToFile method is:
1605 %
1606 %       MagickBooleanType ImageToFile(Image *image,char *filename,
1607 %         ExceptionInfo *exception)
1608 %
1609 %  A description of each parameter follows:
1610 %
1611 %    o image: the image.
1612 %
1613 %    o filename: Write the image to this file.
1614 %
1615 %    o exception: return any errors or warnings in this structure.
1616 %
1617 */
1618 MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
1619   ExceptionInfo *exception)
1620 {
1621   int
1622     file;
1623
1624   register const unsigned char
1625     *p;
1626
1627   register size_t
1628     i;
1629
1630   size_t
1631     length,
1632     quantum;
1633
1634   ssize_t
1635     count;
1636
1637   struct stat
1638     file_stats;
1639
1640   unsigned char
1641     *buffer;
1642
1643   assert(image != (Image *) NULL);
1644   assert(image->signature == MagickSignature);
1645   assert(image->blob != (BlobInfo *) NULL);
1646   assert(image->blob->type != UndefinedStream);
1647   if (image->debug != MagickFalse)
1648     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1649   assert(filename != (const char *) NULL);
1650   if (*filename == '\0')
1651     file=AcquireUniqueFileResource(filename);
1652   else
1653     if (LocaleCompare(filename,"-") == 0)
1654       file=fileno(stdout);
1655     else
1656       file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1657   if (file == -1)
1658     {
1659       ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1660       return(MagickFalse);
1661     }
1662   quantum=(size_t) MagickMaxBufferExtent;
1663   if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1664     quantum=(size_t) MagickMin((MagickSizeType) file_stats.st_size,
1665       MagickMaxBufferExtent);
1666   buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1667   if (buffer == (unsigned char *) NULL)
1668     {
1669       file=close(file)-1;
1670       (void) ThrowMagickException(exception,GetMagickModule(),
1671         ResourceLimitError,"MemoryAllocationError","'%s'",filename);
1672       return(MagickFalse);
1673     }
1674   length=0;
1675   p=ReadBlobStream(image,quantum,buffer,&count);
1676   for (i=0; count > 0; p=ReadBlobStream(image,quantum,buffer,&count))
1677   {
1678     length=(size_t) count;
1679     for (i=0; i < length; i+=count)
1680     {
1681       count=write(file,p+i,(size_t) (length-i));
1682       if (count <= 0)
1683         {
1684           count=0;
1685           if (errno != EINTR)
1686             break;
1687         }
1688     }
1689     if (i < length)
1690       break;
1691   }
1692   if (LocaleCompare(filename,"-") != 0)
1693     file=close(file);
1694   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1695   if ((file == -1) || (i < length))
1696     {
1697       ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1698       return(MagickFalse);
1699     }
1700   return(MagickTrue);
1701 }
1702 \f
1703 /*
1704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1705 %                                                                             %
1706 %                                                                             %
1707 %                                                                             %
1708 %   I m a g e s T o B l o b                                                   %
1709 %                                                                             %
1710 %                                                                             %
1711 %                                                                             %
1712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1713 %
1714 %  ImagesToBlob() implements direct to memory image formats.  It returns the
1715 %  image sequence as a blob and its length.  The magick member of the ImageInfo
1716 %  structure determines the format of the returned blob (GIF, JPEG,  PNG, etc.)
1717 %
1718 %  Note, some image formats do not permit multiple images to the same image
1719 %  stream (e.g. JPEG).  in this instance, just the first image of the
1720 %  sequence is returned as a blob.
1721 %
1722 %  The format of the ImagesToBlob method is:
1723 %
1724 %      unsigned char *ImagesToBlob(const ImageInfo *image_info,Image *images,
1725 %        size_t *length,ExceptionInfo *exception)
1726 %
1727 %  A description of each parameter follows:
1728 %
1729 %    o image_info: the image info.
1730 %
1731 %    o images: the image list.
1732 %
1733 %    o length: This pointer to a size_t integer sets the initial length of the
1734 %      blob.  On return, it reflects the actual length of the blob.
1735 %
1736 %    o exception: return any errors or warnings in this structure.
1737 %
1738 */
1739 MagickExport unsigned char *ImagesToBlob(const ImageInfo *image_info,
1740   Image *images,size_t *length,ExceptionInfo *exception)
1741 {
1742   const MagickInfo
1743     *magick_info;
1744
1745   ImageInfo
1746     *blob_info;
1747
1748   MagickBooleanType
1749     status;
1750
1751   unsigned char
1752     *blob;
1753
1754   assert(image_info != (const ImageInfo *) NULL);
1755   assert(image_info->signature == MagickSignature);
1756   if (image_info->debug != MagickFalse)
1757     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1758       image_info->filename);
1759   assert(images != (Image *) NULL);
1760   assert(images->signature == MagickSignature);
1761   assert(exception != (ExceptionInfo *) NULL);
1762   *length=0;
1763   blob=(unsigned char *) NULL;
1764   blob_info=CloneImageInfo(image_info);
1765   (void) SetImageInfo(blob_info,(unsigned int) GetImageListLength(images),
1766     exception);
1767   if (*blob_info->magick != '\0')
1768     (void) CopyMagickString(images->magick,blob_info->magick,MaxTextExtent);
1769   if (blob_info->adjoin == MagickFalse)
1770     {
1771       blob_info=DestroyImageInfo(blob_info);
1772       return(ImageToBlob(image_info,images,length,exception));
1773     }
1774   magick_info=GetMagickInfo(images->magick,exception);
1775   if (magick_info == (const MagickInfo *) NULL)
1776     {
1777       (void) ThrowMagickException(exception,GetMagickModule(),
1778         MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
1779         images->filename);
1780       return(blob);
1781     }
1782   (void) CopyMagickString(blob_info->magick,images->magick,MaxTextExtent);
1783   if (GetMagickBlobSupport(magick_info) != MagickFalse)
1784     {
1785       /*
1786         Native blob support for this images format.
1787       */
1788       blob_info->length=0;
1789       blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1790         sizeof(unsigned char));
1791       if (blob_info->blob == (void *) NULL)
1792         (void) ThrowMagickException(exception,GetMagickModule(),
1793           ResourceLimitError,"MemoryAllocationFailed","'%s'",images->filename);
1794       else
1795         {
1796           images->blob->exempt=MagickTrue;
1797           *images->filename='\0';
1798           status=WriteImages(blob_info,images,images->filename,exception);
1799           if ((status != MagickFalse) && (images->blob->length != 0))
1800             {
1801               *length=images->blob->length;
1802               blob=DetachBlob(images->blob);
1803               blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1804                 sizeof(*blob));
1805             }
1806         }
1807     }
1808   else
1809     {
1810       char
1811         filename[MaxTextExtent],
1812         unique[MaxTextExtent];
1813
1814       int
1815         file;
1816
1817       /*
1818         Write file to disk in blob images format.
1819       */
1820       file=AcquireUniqueFileResource(unique);
1821       if (file == -1)
1822         {
1823           ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
1824             image_info->filename);
1825         }
1826       else
1827         {
1828           blob_info->file=fdopen(file,"wb");
1829           if (blob_info->file != (FILE *) NULL)
1830             {
1831               (void) FormatLocaleString(filename,MaxTextExtent,"%s:%s",
1832                 images->magick,unique);
1833               status=WriteImages(blob_info,images,filename,exception);
1834               (void) fclose(blob_info->file);
1835               if (status != MagickFalse)
1836                 blob=FileToBlob(images->filename,~0UL,length,exception);
1837             }
1838           (void) RelinquishUniqueFileResource(unique);
1839         }
1840     }
1841   blob_info=DestroyImageInfo(blob_info);
1842   return(blob);
1843 }
1844 /*
1845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1846 %                                                                             %
1847 %                                                                             %
1848 %                                                                             %
1849 %   I n j e c t I m a g e B l o b                                             %
1850 %                                                                             %
1851 %                                                                             %
1852 %                                                                             %
1853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1854 %
1855 %  InjectImageBlob() injects the image with a copy of itself in the specified
1856 %  format (e.g. inject JPEG into a PDF image).
1857 %
1858 %  The format of the InjectImageBlob method is:
1859 %
1860 %      MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1861 %        Image *image,Image *inject_image,const char *format,
1862 %        ExceptionInfo *exception)
1863 %
1864 %  A description of each parameter follows:
1865 %
1866 %    o image_info: the image info..
1867 %
1868 %    o image: the image.
1869 %
1870 %    o inject_image: inject into the image stream.
1871 %
1872 %    o format: the image format.
1873 %
1874 %    o exception: return any errors or warnings in this structure.
1875 %
1876 */
1877 MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1878   Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
1879 {
1880   char
1881     filename[MaxTextExtent];
1882
1883   FILE
1884     *unique_file;
1885
1886   Image
1887     *byte_image;
1888
1889   ImageInfo
1890     *write_info;
1891
1892   int
1893     file;
1894
1895   MagickBooleanType
1896     status;
1897
1898   register ssize_t
1899     i;
1900
1901   size_t
1902     quantum;
1903
1904   ssize_t
1905     count;
1906
1907   struct stat
1908     file_stats;
1909
1910   unsigned char
1911     *buffer;
1912
1913   /*
1914     Write inject image to a temporary file.
1915   */
1916   assert(image_info != (ImageInfo *) NULL);
1917   assert(image_info->signature == MagickSignature);
1918   assert(image != (Image *) NULL);
1919   assert(image->signature == MagickSignature);
1920   if (image->debug != MagickFalse)
1921     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1922   assert(inject_image != (Image *) NULL);
1923   assert(inject_image->signature == MagickSignature);
1924   assert(exception != (ExceptionInfo *) NULL);
1925   unique_file=(FILE *) NULL;
1926   file=AcquireUniqueFileResource(filename);
1927   if (file != -1)
1928     unique_file=fdopen(file,"wb");
1929   if ((file == -1) || (unique_file == (FILE *) NULL))
1930     {
1931       (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1932       ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1933         image->filename);
1934       return(MagickFalse);
1935     }
1936   byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
1937   if (byte_image == (Image *) NULL)
1938     {
1939       (void) fclose(unique_file);
1940       (void) RelinquishUniqueFileResource(filename);
1941       return(MagickFalse);
1942     }
1943   (void) FormatLocaleString(byte_image->filename,MaxTextExtent,"%s:%s",format,
1944     filename);
1945   DestroyBlob(byte_image);
1946   byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
1947   write_info=CloneImageInfo(image_info);
1948   SetImageInfoFile(write_info,unique_file);
1949   status=WriteImage(write_info,byte_image,exception);
1950   write_info=DestroyImageInfo(write_info);
1951   byte_image=DestroyImage(byte_image);
1952   (void) fclose(unique_file);
1953   if (status == MagickFalse)
1954     {
1955       (void) RelinquishUniqueFileResource(filename);
1956       return(MagickFalse);
1957     }
1958   /*
1959     Inject into image stream.
1960   */
1961   file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1962   if (file == -1)
1963     {
1964       (void) RelinquishUniqueFileResource(filename);
1965       ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1966         image_info->filename);
1967       return(MagickFalse);
1968     }
1969   quantum=(size_t) MagickMaxBufferExtent;
1970   if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1971     quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1972   buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1973   if (buffer == (unsigned char *) NULL)
1974     {
1975       (void) RelinquishUniqueFileResource(filename);
1976       ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1977         image->filename);
1978     }
1979   for (i=0; ; i+=count)
1980   {
1981     count=(ssize_t) read(file,buffer,quantum);
1982     if (count <= 0)
1983       {
1984         count=0;
1985         if (errno != EINTR)
1986           break;
1987       }
1988     status=WriteBlobStream(image,(size_t) count,buffer) == count ? MagickTrue :
1989       MagickFalse;
1990   }
1991   file=close(file);
1992   if (file == -1)
1993     ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",filename);
1994   (void) RelinquishUniqueFileResource(filename);
1995   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1996   return(status);
1997 }
1998 \f
1999 /*
2000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001 %                                                                             %
2002 %                                                                             %
2003 %                                                                             %
2004 +   I s B l o b E x e m p t                                                   %
2005 %                                                                             %
2006 %                                                                             %
2007 %                                                                             %
2008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009 %
2010 %  IsBlobExempt() returns true if the blob is exempt.
2011 %
2012 %  The format of the IsBlobExempt method is:
2013 %
2014 %       MagickBooleanType IsBlobExempt(const Image *image)
2015 %
2016 %  A description of each parameter follows:
2017 %
2018 %    o image: the image.
2019 %
2020 */
2021 MagickPrivate MagickBooleanType IsBlobExempt(const Image *image)
2022 {
2023   assert(image != (const Image *) NULL);
2024   assert(image->signature == MagickSignature);
2025   if (image->debug != MagickFalse)
2026     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2027   return(image->blob->exempt);
2028 }
2029 \f
2030 /*
2031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2032 %                                                                             %
2033 %                                                                             %
2034 %                                                                             %
2035 +   I s B l o b S e e k a b l e                                               %
2036 %                                                                             %
2037 %                                                                             %
2038 %                                                                             %
2039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2040 %
2041 %  IsBlobSeekable() returns true if the blob is seekable.
2042 %
2043 %  The format of the IsBlobSeekable method is:
2044 %
2045 %       MagickBooleanType IsBlobSeekable(const Image *image)
2046 %
2047 %  A description of each parameter follows:
2048 %
2049 %    o image: the image.
2050 %
2051 */
2052 MagickPrivate MagickBooleanType IsBlobSeekable(const Image *image)
2053 {
2054   MagickBooleanType
2055     seekable;
2056
2057   assert(image != (const Image *) NULL);
2058   assert(image->signature == MagickSignature);
2059   if (image->debug != MagickFalse)
2060     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2061   switch (image->blob->type)
2062   {
2063     case FileStream:
2064     case BlobStream:
2065     case ZipStream:
2066     {
2067       seekable=MagickTrue;
2068       break;
2069     }
2070     default:
2071     {
2072       seekable=MagickFalse;
2073       break;
2074     }
2075   }
2076   return(seekable);
2077 }
2078 \f
2079 /*
2080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2081 %                                                                             %
2082 %                                                                             %
2083 %                                                                             %
2084 +   I s B l o b T e m p o r a r y                                             %
2085 %                                                                             %
2086 %                                                                             %
2087 %                                                                             %
2088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2089 %
2090 %  IsBlobTemporary() returns true if the blob is temporary.
2091 %
2092 %  The format of the IsBlobTemporary method is:
2093 %
2094 %       MagickBooleanType IsBlobTemporary(const Image *image)
2095 %
2096 %  A description of each parameter follows:
2097 %
2098 %    o image: the image.
2099 %
2100 */
2101 MagickPrivate MagickBooleanType IsBlobTemporary(const Image *image)
2102 {
2103   assert(image != (const Image *) NULL);
2104   assert(image->signature == MagickSignature);
2105   if (image->debug != MagickFalse)
2106     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2107   return(image->blob->temporary);
2108 }
2109 \f
2110 /*
2111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2112 %                                                                             %
2113 %                                                                             %
2114 %                                                                             %
2115 +  M a p B l o b                                                              %
2116 %                                                                             %
2117 %                                                                             %
2118 %                                                                             %
2119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2120 %
2121 %  MapBlob() creates a mapping from a file to a binary large object.
2122 %
2123 %  The format of the MapBlob method is:
2124 %
2125 %      unsigned char *MapBlob(int file,const MapMode mode,
2126 %        const MagickOffsetType offset,const size_t length)
2127 %
2128 %  A description of each parameter follows:
2129 %
2130 %    o file: map this file descriptor.
2131 %
2132 %    o mode: ReadMode, WriteMode, or IOMode.
2133 %
2134 %    o offset: starting at this offset within the file.
2135 %
2136 %    o length: the length of the mapping is returned in this pointer.
2137 %
2138 */
2139 MagickExport unsigned char *MapBlob(int file,const MapMode mode,
2140   const MagickOffsetType offset,const size_t length)
2141 {
2142 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
2143   int
2144     flags,
2145     protection;
2146
2147   unsigned char
2148     *map;
2149
2150   /*
2151     Map file.
2152   */
2153   flags=0;
2154   if (file == -1)
2155 #if defined(MAP_ANONYMOUS)
2156     flags|=MAP_ANONYMOUS;
2157 #else
2158     return((unsigned char *) NULL);
2159 #endif
2160   switch (mode)
2161   {
2162     case ReadMode:
2163     default:
2164     {
2165       protection=PROT_READ;
2166       flags|=MAP_PRIVATE;
2167       map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2168         (off_t) offset);
2169       break;
2170     }
2171     case WriteMode:
2172     {
2173       protection=PROT_WRITE;
2174       flags|=MAP_SHARED;
2175       map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2176         (off_t) offset);
2177 #if defined(MAGICKCORE_HAVE_POSIX_MADVISE)
2178       (void) posix_madvise(map,length,POSIX_MADV_SEQUENTIAL |
2179         POSIX_MADV_WILLNEED);
2180 #endif
2181       break;
2182     }
2183     case IOMode:
2184     {
2185       protection=PROT_READ | PROT_WRITE;
2186       flags|=MAP_SHARED;
2187       map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2188         (off_t) offset);
2189       break;
2190     }
2191   }
2192   if (map == (unsigned char *) MAP_FAILED)
2193     return((unsigned char *) NULL);
2194   return(map);
2195 #else
2196   (void) file;
2197   (void) mode;
2198   (void) offset;
2199   (void) length;
2200   return((unsigned char *) NULL);
2201 #endif
2202 }
2203 \f
2204 /*
2205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2206 %                                                                             %
2207 %                                                                             %
2208 %                                                                             %
2209 +  M S B O r d e r L o n g                                                    %
2210 %                                                                             %
2211 %                                                                             %
2212 %                                                                             %
2213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2214 %
2215 %  MSBOrderLong() converts a least-significant byte first buffer of integers to
2216 %  most-significant byte first.
2217 %
2218 %  The format of the MSBOrderLong method is:
2219 %
2220 %      void MSBOrderLong(unsigned char *buffer,const size_t length)
2221 %
2222 %  A description of each parameter follows.
2223 %
2224 %   o  buffer:  Specifies a pointer to a buffer of integers.
2225 %
2226 %   o  length:  Specifies the length of the buffer.
2227 %
2228 */
2229 MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
2230 {
2231   int
2232     c;
2233
2234   register unsigned char
2235     *p,
2236     *q;
2237
2238   assert(buffer != (unsigned char *) NULL);
2239   q=buffer+length;
2240   while (buffer < q)
2241   {
2242     p=buffer+3;
2243     c=(int) (*p);
2244     *p=(*buffer);
2245     *buffer++=(unsigned char) c;
2246     p=buffer+1;
2247     c=(int) (*p);
2248     *p=(*buffer);
2249     *buffer++=(unsigned char) c;
2250     buffer+=2;
2251   }
2252 }
2253 \f
2254 /*
2255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2256 %                                                                             %
2257 %                                                                             %
2258 %                                                                             %
2259 +  M S B O r d e r S h o r t                                                  %
2260 %                                                                             %
2261 %                                                                             %
2262 %                                                                             %
2263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2264 %
2265 %  MSBOrderShort() converts a least-significant byte first buffer of integers
2266 %  to most-significant byte first.
2267 %
2268 %  The format of the MSBOrderShort method is:
2269 %
2270 %      void MSBOrderShort(unsigned char *p,const size_t length)
2271 %
2272 %  A description of each parameter follows.
2273 %
2274 %   o  p:  Specifies a pointer to a buffer of integers.
2275 %
2276 %   o  length:  Specifies the length of the buffer.
2277 %
2278 */
2279 MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
2280 {
2281   int
2282     c;
2283
2284   register unsigned char
2285     *q;
2286
2287   assert(p != (unsigned char *) NULL);
2288   q=p+length;
2289   while (p < q)
2290   {
2291     c=(int) (*p);
2292     *p=(*(p+1));
2293     p++;
2294     *p++=(unsigned char) c;
2295   }
2296 }
2297 \f
2298 /*
2299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2300 %                                                                             %
2301 %                                                                             %
2302 %                                                                             %
2303 +   O p e n B l o b                                                           %
2304 %                                                                             %
2305 %                                                                             %
2306 %                                                                             %
2307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2308 %
2309 %  OpenBlob() opens a file associated with the image.  A file name of '-' sets
2310 %  the file to stdin for type 'r' and stdout for type 'w'.  If the filename
2311 %  suffix is '.gz' or '.Z', the image is decompressed for type 'r' and
2312 %  compressed for type 'w'.  If the filename prefix is '|', it is piped to or
2313 %  from a system command.
2314 %
2315 %  The format of the OpenBlob method is:
2316 %
2317 %       MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
2318 %        const BlobMode mode,ExceptionInfo *exception)
2319 %
2320 %  A description of each parameter follows:
2321 %
2322 %    o image_info: the image info.
2323 %
2324 %    o image: the image.
2325 %
2326 %    o mode: the mode for opening the file.
2327 %
2328 */
2329 MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
2330   Image *image,const BlobMode mode,ExceptionInfo *exception)
2331 {
2332   char
2333     extension[MaxTextExtent],
2334     filename[MaxTextExtent];
2335
2336   const char
2337     *type;
2338
2339   MagickBooleanType
2340     status;
2341
2342   PolicyRights
2343     rights;
2344
2345   assert(image_info != (ImageInfo *) NULL);
2346   assert(image_info->signature == MagickSignature);
2347   if (image_info->debug != MagickFalse)
2348     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2349       image_info->filename);
2350   assert(image != (Image *) NULL);
2351   assert(image->signature == MagickSignature);
2352   if (image_info->blob != (void *) NULL)
2353     {
2354       if (image_info->stream != (StreamHandler) NULL)
2355         image->blob->stream=(StreamHandler) image_info->stream;
2356       AttachBlob(image->blob,image_info->blob,image_info->length);
2357       return(MagickTrue);
2358     }
2359   (void) DetachBlob(image->blob);
2360   switch (mode)
2361   {
2362     default: type="r"; break;
2363     case ReadBlobMode: type="r"; break;
2364     case ReadBinaryBlobMode: type="rb"; break;
2365     case WriteBlobMode: type="w"; break;
2366     case WriteBinaryBlobMode: type="w+b"; break;
2367     case AppendBlobMode: type="a"; break;
2368     case AppendBinaryBlobMode: type="a+b"; break;
2369   }
2370   if (*type != 'r')
2371     image->blob->synchronize=image_info->synchronize;
2372   if (image_info->stream != (StreamHandler) NULL)
2373     {
2374       image->blob->stream=(StreamHandler) image_info->stream;
2375       if (*type == 'w')
2376         {
2377           image->blob->type=FifoStream;
2378           return(MagickTrue);
2379         }
2380     }
2381   /*
2382     Open image file.
2383   */
2384   *filename='\0';
2385   (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2386   rights=ReadPolicyRights;
2387   if (*type == 'w')
2388     rights=WritePolicyRights;
2389   if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2390     {
2391       errno=EPERM;
2392       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2393         "NotAuthorized","'%s'",filename);
2394       return(MagickFalse);
2395     }
2396   if ((LocaleCompare(filename,"-") == 0) ||
2397       ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
2398     {
2399       image->blob->file_info.file=(*type == 'r') ? stdin : stdout;
2400 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2401       if (strchr(type,'b') != (char *) NULL)
2402         setmode(_fileno(image->blob->file_info.file),_O_BINARY);
2403 #endif
2404       image->blob->type=StandardStream;
2405       image->blob->exempt=MagickTrue;
2406       return(MagickTrue);
2407     }
2408   if (LocaleNCompare(filename,"fd:",3) == 0)
2409     {
2410       char
2411         mode[MaxTextExtent];
2412
2413       *mode=(*type);
2414       mode[1]='\0';
2415       image->blob->file_info.file=fdopen(StringToLong(filename+3),mode);
2416 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2417       if (strchr(type,'b') != (char *) NULL)
2418         setmode(_fileno(image->blob->file_info.file),_O_BINARY);
2419 #endif
2420       image->blob->type=StandardStream;
2421       image->blob->exempt=MagickTrue;
2422       return(MagickTrue);
2423     }
2424 #if defined(MAGICKCORE_HAVE_POPEN)
2425   if (*filename == '|')
2426     {
2427       char
2428         mode[MaxTextExtent];
2429
2430       /*
2431         Pipe image to or from a system command.
2432       */
2433 #if defined(SIGPIPE)
2434       if (*type == 'w')
2435         (void) signal(SIGPIPE,SIG_IGN);
2436 #endif
2437       *mode=(*type);
2438       mode[1]='\0';
2439       image->blob->file_info.file=(FILE *) popen_utf8(filename+1,mode);
2440       if (image->blob->file_info.file == (FILE *) NULL)
2441         {
2442           ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2443           return(MagickFalse);
2444         }
2445       image->blob->type=PipeStream;
2446       image->blob->exempt=MagickTrue;
2447       return(MagickTrue);
2448     }
2449 #endif
2450   status=GetPathAttributes(filename,&image->blob->properties);
2451 #if defined(S_ISFIFO)
2452   if ((status == MagickTrue) && S_ISFIFO(image->blob->properties.st_mode))
2453     {
2454       image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2455       if (image->blob->file_info.file == (FILE *) NULL)
2456         {
2457           ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2458           return(MagickFalse);
2459         }
2460       image->blob->type=FileStream;
2461       image->blob->exempt=MagickTrue;
2462       return(MagickTrue);
2463     }
2464 #endif
2465   GetPathComponent(image->filename,ExtensionPath,extension);
2466   if (*type == 'w')
2467     {
2468       (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2469       if ((image_info->adjoin == MagickFalse) ||
2470           (strchr(filename,'%') != (char *) NULL))
2471         {
2472           /*
2473             Form filename for multi-part images.
2474           */
2475           (void) InterpretImageFilename(image_info,image,image->filename,(int)
2476             image->scene,filename,exception);
2477           if ((LocaleCompare(filename,image->filename) == 0) &&
2478               ((GetPreviousImageInList(image) != (Image *) NULL) ||
2479                (GetNextImageInList(image) != (Image *) NULL)))
2480             {
2481               char
2482                 path[MaxTextExtent];
2483
2484               GetPathComponent(image->filename,RootPath,path);
2485               if (*extension == '\0')
2486                 (void) FormatLocaleString(filename,MaxTextExtent,"%s-%.20g",
2487                   path,(double) image->scene);
2488               else
2489                 (void) FormatLocaleString(filename,MaxTextExtent,"%s-%.20g.%s",
2490                   path,(double) image->scene,extension);
2491             }
2492           (void) CopyMagickString(image->filename,filename,MaxTextExtent);
2493 #if defined(macintosh)
2494           SetApplicationType(filename,image_info->magick,'8BIM');
2495 #endif
2496         }
2497     }
2498   if (image_info->file != (FILE *) NULL)
2499     {
2500       image->blob->file_info.file=image_info->file;
2501       image->blob->type=FileStream;
2502       image->blob->exempt=MagickTrue;
2503     }
2504   else
2505     if (*type == 'r')
2506       {
2507         image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2508         if (image->blob->file_info.file != (FILE *) NULL)
2509           {
2510             size_t
2511               count;
2512
2513             unsigned char
2514               magick[3];
2515
2516             image->blob->type=FileStream;
2517 #if defined(MAGICKCORE_HAVE_SETVBUF)
2518             (void) setvbuf(image->blob->file_info.file,(char *) NULL,
2519               (int) _IOFBF,16384);
2520 #endif
2521             (void) ResetMagickMemory(magick,0,sizeof(magick));
2522             count=fread(magick,1,sizeof(magick),image->blob->file_info.file);
2523             (void) rewind(image->blob->file_info.file);
2524             (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2525                "  read %.20g magic header bytes",(double) count);
2526 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2527             if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
2528                 ((int) magick[2] == 0x08))
2529               {
2530                 (void) fclose(image->blob->file_info.file);
2531                 image->blob->file_info.gzfile=gzopen(filename,type);
2532                 if (image->blob->file_info.gzfile != (gzFile) NULL)
2533                   image->blob->type=ZipStream;
2534                }
2535 #endif
2536 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2537             if (strncmp((char *) magick,"BZh",3) == 0)
2538               {
2539                 (void) fclose(image->blob->file_info.file);
2540                 image->blob->file_info.bzfile=BZ2_bzopen(filename,type);
2541                 if (image->blob->file_info.bzfile != (BZFILE *) NULL)
2542                   image->blob->type=BZipStream;
2543               }
2544 #endif
2545             if (image->blob->type == FileStream)
2546               {
2547                 const MagickInfo
2548                   *magick_info;
2549
2550                 ExceptionInfo
2551                   *sans_exception;
2552
2553                 struct stat
2554                   *properties;
2555
2556                 sans_exception=AcquireExceptionInfo();
2557                 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2558                 sans_exception=DestroyExceptionInfo(sans_exception);
2559                 properties=(&image->blob->properties);
2560                 if ((magick_info != (const MagickInfo *) NULL) &&
2561                     (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2562                     (properties->st_size <= MagickMaxBufferExtent))
2563                   {
2564                     size_t
2565                       length;
2566
2567                     void
2568                       *blob;
2569
2570                     length=(size_t) properties->st_size;
2571                     blob=MapBlob(fileno(image->blob->file_info.file),ReadMode,
2572                       0,length);
2573                     if (blob != (void *) NULL)
2574                       {
2575                         /*
2576                           Format supports blobs-- use memory-mapped I/O.
2577                         */
2578                         if (image_info->file != (FILE *) NULL)
2579                           image->blob->exempt=MagickFalse;
2580                         else
2581                           {
2582                             (void) fclose(image->blob->file_info.file);
2583                             image->blob->file_info.file=(FILE *) NULL;
2584                           }
2585                         AttachBlob(image->blob,blob,length);
2586                         image->blob->mapped=MagickTrue;
2587                       }
2588                   }
2589               }
2590           }
2591         }
2592       else
2593 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2594         if ((LocaleCompare(extension,"Z") == 0) ||
2595             (LocaleCompare(extension,"gz") == 0) ||
2596             (LocaleCompare(extension,"wmz") == 0) ||
2597             (LocaleCompare(extension,"svgz") == 0))
2598           {
2599             if (mode == WriteBinaryBlobMode)
2600               type="wb";
2601             image->blob->file_info.gzfile=gzopen(filename,type);
2602             if (image->blob->file_info.gzfile != (gzFile) NULL)
2603               image->blob->type=ZipStream;
2604           }
2605         else
2606 #endif
2607 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2608           if (LocaleCompare(extension,"bz2") == 0)
2609             {
2610               image->blob->file_info.bzfile=BZ2_bzopen(filename,type);
2611               if (image->blob->file_info.bzfile != (BZFILE *) NULL)
2612                 image->blob->type=BZipStream;
2613             }
2614           else
2615 #endif
2616             {
2617               image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2618               if (image->blob->file_info.file != (FILE *) NULL)
2619                 {
2620                   image->blob->type=FileStream;
2621 #if defined(MAGICKCORE_HAVE_SETVBUF)
2622                   (void) setvbuf(image->blob->file_info.file,(char *) NULL,(int) _IOFBF,
2623                     16384);
2624 #endif
2625                 }
2626        }
2627   image->blob->status=MagickFalse;
2628   if (image->blob->type != UndefinedStream)
2629     image->blob->size=GetBlobSize(image);
2630   else
2631     {
2632       ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2633       return(MagickFalse);
2634     }
2635   return(MagickTrue);
2636 }
2637 \f
2638 /*
2639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2640 %                                                                             %
2641 %                                                                             %
2642 %                                                                             %
2643 +   P i n g B l o b                                                           %
2644 %                                                                             %
2645 %                                                                             %
2646 %                                                                             %
2647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2648 %
2649 %  PingBlob() returns all the attributes of an image or image sequence except
2650 %  for the pixels.  It is much faster and consumes far less memory than
2651 %  BlobToImage().  On failure, a NULL image is returned and exception
2652 %  describes the reason for the failure.
2653 %
2654 %  The format of the PingBlob method is:
2655 %
2656 %      Image *PingBlob(const ImageInfo *image_info,const void *blob,
2657 %        const size_t length,ExceptionInfo *exception)
2658 %
2659 %  A description of each parameter follows:
2660 %
2661 %    o image_info: the image info.
2662 %
2663 %    o blob: the address of a character stream in one of the image formats
2664 %      understood by ImageMagick.
2665 %
2666 %    o length: This size_t integer reflects the length in bytes of the blob.
2667 %
2668 %    o exception: return any errors or warnings in this structure.
2669 %
2670 */
2671
2672 #if defined(__cplusplus) || defined(c_plusplus)
2673 extern "C" {
2674 #endif
2675
2676 static size_t PingStream(const Image *magick_unused(image),
2677   const void *magick_unused(pixels),const size_t columns)
2678 {
2679   return(columns);
2680 }
2681
2682 #if defined(__cplusplus) || defined(c_plusplus)
2683 }
2684 #endif
2685
2686 MagickExport Image *PingBlob(const ImageInfo *image_info,const void *blob,
2687   const size_t length,ExceptionInfo *exception)
2688 {
2689   Image
2690     *image;
2691
2692   ImageInfo
2693     *ping_info;
2694
2695   assert(image_info != (ImageInfo *) NULL);
2696   assert(image_info->signature == MagickSignature);
2697   if (image_info->debug != MagickFalse)
2698     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2699       image_info->filename);
2700   assert(exception != (ExceptionInfo *) NULL);
2701   if ((blob == (const void *) NULL) || (length == 0))
2702     {
2703       (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
2704         "UnrecognizedImageFormat","'%s'",image_info->magick);
2705       return((Image *) NULL);
2706     }
2707   ping_info=CloneImageInfo(image_info);
2708   ping_info->blob=(void *) AcquireQuantumMemory(length,sizeof(unsigned char));
2709   if (ping_info->blob == (const void *) NULL)
2710     {
2711       (void) ThrowMagickException(exception,GetMagickModule(),
2712         ResourceLimitFatalError,"MemoryAllocationFailed","'%s'","");
2713       return((Image *) NULL);
2714     }
2715   (void) memcpy(ping_info->blob,blob,length);
2716   ping_info->length=length;
2717   ping_info->ping=MagickTrue;
2718   image=ReadStream(ping_info,&PingStream,exception);
2719   ping_info->blob=(void *) RelinquishMagickMemory(ping_info->blob);
2720   ping_info=DestroyImageInfo(ping_info);
2721   return(image);
2722 }
2723 \f
2724 /*
2725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2726 %                                                                             %
2727 %                                                                             %
2728 %                                                                             %
2729 +  R e a d B l o b                                                            %
2730 %                                                                             %
2731 %                                                                             %
2732 %                                                                             %
2733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2734 %
2735 %  ReadBlob() reads data from the blob or image file and returns it.  It
2736 %  returns the number of bytes read.
2737 %
2738 %  The format of the ReadBlob method is:
2739 %
2740 %      ssize_t ReadBlob(Image *image,const size_t length,unsigned char *data)
2741 %
2742 %  A description of each parameter follows:
2743 %
2744 %    o image: the image.
2745 %
2746 %    o length:  Specifies an integer representing the number of bytes to read
2747 %      from the file.
2748 %
2749 %    o data:  Specifies an area to place the information requested from the
2750 %      file.
2751 %
2752 */
2753 MagickExport ssize_t ReadBlob(Image *image,const size_t length,
2754   unsigned char *data)
2755 {
2756   int
2757     c;
2758
2759   register unsigned char
2760     *q;
2761
2762   ssize_t
2763     count;
2764
2765   assert(image != (Image *) NULL);
2766   assert(image->signature == MagickSignature);
2767   assert(image->blob != (BlobInfo *) NULL);
2768   assert(image->blob->type != UndefinedStream);
2769   if (length == 0)
2770     return(0);
2771   assert(data != (void *) NULL);
2772   count=0;
2773   q=data;
2774   switch (image->blob->type)
2775   {
2776     case UndefinedStream:
2777       break;
2778     case StandardStream:
2779     {
2780       register ssize_t
2781         i;
2782
2783       count=0;
2784       for (i=0; i < (ssize_t) length; i+=count)
2785       {
2786         count=read(fileno(image->blob->file_info.file),q+i,(size_t) MagickMin(
2787           length-i,SSIZE_MAX));
2788         if (count <= 0)
2789           {
2790             count=0;
2791             if (errno != EINTR)
2792               break;
2793           }
2794       }
2795       count=i;
2796       break;
2797     }
2798     case FileStream:
2799     case PipeStream:
2800     {
2801       switch (length)
2802       {
2803         default:
2804         {
2805           count=(ssize_t) fread(q,1,length,image->blob->file_info.file);
2806           break;
2807         }
2808         case 2:
2809         {
2810           c=getc(image->blob->file_info.file);
2811           if (c == EOF)
2812             break;
2813           *q++=(unsigned char) c;
2814           count++;
2815         }
2816         case 1:
2817         {
2818           c=getc(image->blob->file_info.file);
2819           if (c == EOF)
2820             break;
2821           *q++=(unsigned char) c;
2822           count++;
2823         }
2824         case 0:
2825           break;
2826       }
2827       break;
2828     }
2829     case ZipStream:
2830     {
2831 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2832       switch (length)
2833       {
2834         default:
2835         {
2836           count=(ssize_t) gzread(image->blob->file_info.gzfile,q,
2837             (unsigned int) length);
2838           break;
2839         }
2840         case 2:
2841         {
2842           c=gzgetc(image->blob->file_info.gzfile);
2843           if (c == EOF)
2844             break;
2845           *q++=(unsigned char) c;
2846           count++;
2847         }
2848         case 1:
2849         {
2850           c=gzgetc(image->blob->file_info.gzfile);
2851           if (c == EOF)
2852             break;
2853           *q++=(unsigned char) c;
2854           count++;
2855         }
2856         case 0:
2857           break;
2858       }
2859 #endif
2860       break;
2861     }
2862     case BZipStream:
2863     {
2864 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2865       count=(ssize_t) BZ2_bzread(image->blob->file_info.bzfile,q,
2866         (int) length);
2867 #endif
2868       break;
2869     }
2870     case FifoStream:
2871       break;
2872     case BlobStream:
2873     {
2874       register const unsigned char
2875         *p;
2876
2877       if (image->blob->offset >= (MagickOffsetType) image->blob->length)
2878         {
2879           image->blob->eof=MagickTrue;
2880           break;
2881         }
2882       p=image->blob->data+image->blob->offset;
2883       count=(ssize_t) MagickMin(length,(MagickSizeType) (image->blob->length-
2884         image->blob->offset));
2885       image->blob->offset+=count;
2886       if (count != (ssize_t) length)
2887         image->blob->eof=MagickTrue;
2888       (void) memcpy(q,p,(size_t) count);
2889       break;
2890     }
2891   }
2892   return(count);
2893 }
2894 \f
2895 /*
2896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2897 %                                                                             %
2898 %                                                                             %
2899 %                                                                             %
2900 +  R e a d B l o b B y t e                                                    %
2901 %                                                                             %
2902 %                                                                             %
2903 %                                                                             %
2904 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2905 %
2906 %  ReadBlobByte() reads a single byte from the image file and returns it.
2907 %
2908 %  The format of the ReadBlobByte method is:
2909 %
2910 %      int ReadBlobByte(Image *image)
2911 %
2912 %  A description of each parameter follows.
2913 %
2914 %    o image: the image.
2915 %
2916 */
2917 MagickExport int ReadBlobByte(Image *image)
2918 {
2919   register const unsigned char
2920     *p;
2921
2922   ssize_t
2923     count;
2924
2925   unsigned char
2926     buffer[1];
2927
2928   assert(image != (Image *) NULL);
2929   assert(image->signature == MagickSignature);
2930   p=ReadBlobStream(image,1,buffer,&count);
2931   if (count != 1)
2932     return(EOF);
2933   return((int) (*p));
2934 }
2935 \f
2936 /*
2937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2938 %                                                                             %
2939 %                                                                             %
2940 %                                                                             %
2941 +  R e a d B l o b D o u b l e                                                %
2942 %                                                                             %
2943 %                                                                             %
2944 %                                                                             %
2945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2946 %
2947 %  ReadBlobDouble() reads a double value as a 64-bit quantity in the byte-order
2948 %  specified by the endian member of the image structure.
2949 %
2950 %  The format of the ReadBlobDouble method is:
2951 %
2952 %      double ReadBlobDouble(Image *image)
2953 %
2954 %  A description of each parameter follows.
2955 %
2956 %    o image: the image.
2957 %
2958 */
2959 MagickExport double ReadBlobDouble(Image *image)
2960 {
2961   union
2962   {
2963     MagickSizeType
2964       unsigned_value;
2965
2966     double
2967       double_value;
2968   } quantum;
2969
2970   quantum.double_value=0.0;
2971   quantum.unsigned_value=ReadBlobLongLong(image);
2972   return(quantum.double_value);
2973 }
2974 \f
2975 /*
2976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2977 %                                                                             %
2978 %                                                                             %
2979 %                                                                             %
2980 +  R e a d B l o b F l o a t                                                  %
2981 %                                                                             %
2982 %                                                                             %
2983 %                                                                             %
2984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2985 %
2986 %  ReadBlobFloat() reads a float value as a 32-bit quantity in the byte-order
2987 %  specified by the endian member of the image structure.
2988 %
2989 %  The format of the ReadBlobFloat method is:
2990 %
2991 %      float ReadBlobFloat(Image *image)
2992 %
2993 %  A description of each parameter follows.
2994 %
2995 %    o image: the image.
2996 %
2997 */
2998 MagickExport float ReadBlobFloat(Image *image)
2999 {
3000   union
3001   {
3002     unsigned int
3003       unsigned_value;
3004
3005     float
3006       float_value;
3007   } quantum;
3008
3009   quantum.float_value=0.0;
3010   quantum.unsigned_value=ReadBlobLong(image);
3011   return(quantum.float_value);
3012 }
3013 \f
3014 /*
3015 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3016 %                                                                             %
3017 %                                                                             %
3018 %                                                                             %
3019 +  R e a d B l o b L o n g                                                    %
3020 %                                                                             %
3021 %                                                                             %
3022 %                                                                             %
3023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3024 %
3025 %  ReadBlobLong() reads a ssize_t value as a 32-bit quantity in the byte-order
3026 %  specified by the endian member of the image structure.
3027 %
3028 %  The format of the ReadBlobLong method is:
3029 %
3030 %      unsigned int ReadBlobLong(Image *image)
3031 %
3032 %  A description of each parameter follows.
3033 %
3034 %    o image: the image.
3035 %
3036 */
3037 MagickExport unsigned int ReadBlobLong(Image *image)
3038 {
3039   register const unsigned char
3040     *p;
3041
3042   ssize_t
3043     count;
3044
3045   unsigned char
3046     buffer[4];
3047
3048   unsigned int
3049     value;
3050
3051   assert(image != (Image *) NULL);
3052   assert(image->signature == MagickSignature);
3053   *buffer='\0';
3054   p=ReadBlobStream(image,4,buffer,&count);
3055   if (count != 4)
3056     return(0UL);
3057   if (image->endian == LSBEndian)
3058     {
3059       value=(unsigned int) (*p++);
3060       value|=((unsigned int) (*p++)) << 8;
3061       value|=((unsigned int) (*p++)) << 16;
3062       value|=((unsigned int) (*p++)) << 24;
3063       return(value);
3064     }
3065   value=((unsigned int) (*p++)) << 24;
3066   value|=((unsigned int) (*p++)) << 16;
3067   value|=((unsigned int) (*p++)) << 8;
3068   value|=((unsigned int) (*p++));
3069   return(value);
3070 }
3071 \f
3072 /*
3073 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3074 %                                                                             %
3075 %                                                                             %
3076 %                                                                             %
3077 +  R e a d B l o b L o n g L o n g                                            %
3078 %                                                                             %
3079 %                                                                             %
3080 %                                                                             %
3081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3082 %
3083 %  ReadBlobLongLong() reads a long long value as a 64-bit quantity in the
3084 %  byte-order specified by the endian member of the image structure.
3085 %
3086 %  The format of the ReadBlobLongLong method is:
3087 %
3088 %      MagickSizeType ReadBlobLongLong(Image *image)
3089 %
3090 %  A description of each parameter follows.
3091 %
3092 %    o image: the image.
3093 %
3094 */
3095 MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3096 {
3097   MagickSizeType
3098     value;
3099
3100   register const unsigned char
3101     *p;
3102
3103   ssize_t
3104     count;
3105
3106   unsigned char
3107     buffer[8];
3108
3109   assert(image != (Image *) NULL);
3110   assert(image->signature == MagickSignature);
3111   *buffer='\0';
3112   p=ReadBlobStream(image,8,buffer,&count);
3113   if (count != 8)
3114     return(MagickULLConstant(0));
3115   if (image->endian == LSBEndian)
3116     {
3117       value=(MagickSizeType) (*p++);
3118       value|=((MagickSizeType) (*p++)) << 8;
3119       value|=((MagickSizeType) (*p++)) << 16;
3120       value|=((MagickSizeType) (*p++)) << 24;
3121       value|=((MagickSizeType) (*p++)) << 32;
3122       value|=((MagickSizeType) (*p++)) << 40;
3123       value|=((MagickSizeType) (*p++)) << 48;
3124       value|=((MagickSizeType) (*p++)) << 56;
3125       return(value & MagickULLConstant(0xffffffffffffffff));
3126     }
3127   value=((MagickSizeType) (*p++)) << 56;
3128   value|=((MagickSizeType) (*p++)) << 48;
3129   value|=((MagickSizeType) (*p++)) << 40;
3130   value|=((MagickSizeType) (*p++)) << 32;
3131   value|=((MagickSizeType) (*p++)) << 24;
3132   value|=((MagickSizeType) (*p++)) << 16;
3133   value|=((MagickSizeType) (*p++)) << 8;
3134   value|=((MagickSizeType) (*p++));
3135   return(value & MagickULLConstant(0xffffffffffffffff));
3136 }
3137 \f
3138 /*
3139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3140 %                                                                             %
3141 %                                                                             %
3142 %                                                                             %
3143 +  R e a d B l o b S h o r t                                                  %
3144 %                                                                             %
3145 %                                                                             %
3146 %                                                                             %
3147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3148 %
3149 %  ReadBlobShort() reads a short value as a 16-bit quantity in the byte-order
3150 %  specified by the endian member of the image structure.
3151 %
3152 %  The format of the ReadBlobShort method is:
3153 %
3154 %      unsigned short ReadBlobShort(Image *image)
3155 %
3156 %  A description of each parameter follows.
3157 %
3158 %    o image: the image.
3159 %
3160 */
3161 MagickExport unsigned short ReadBlobShort(Image *image)
3162 {
3163   register const unsigned char
3164     *p;
3165
3166   register unsigned int
3167     value;
3168
3169   ssize_t
3170     count;
3171
3172   unsigned char
3173     buffer[2];
3174
3175   assert(image != (Image *) NULL);
3176   assert(image->signature == MagickSignature);
3177   *buffer='\0';
3178   p=ReadBlobStream(image,2,buffer,&count);
3179   if (count != 2)
3180     return((unsigned short) 0U);
3181   if (image->endian == LSBEndian)
3182     {
3183       value=(unsigned int) (*p++);
3184       value|=((unsigned int) (*p++)) << 8;
3185       return((unsigned short) (value & 0xffff));
3186     }
3187   value=(unsigned int) ((*p++) << 8);
3188   value|=(unsigned int) (*p++);
3189   return((unsigned short) (value & 0xffff));
3190 }
3191 \f
3192 /*
3193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3194 %                                                                             %
3195 %                                                                             %
3196 %                                                                             %
3197 +  R e a d B l o b L S B L o n g                                              %
3198 %                                                                             %
3199 %                                                                             %
3200 %                                                                             %
3201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3202 %
3203 %  ReadBlobLSBLong() reads a ssize_t value as a 32-bit quantity in
3204 %  least-significant byte first order.
3205 %
3206 %  The format of the ReadBlobLSBLong method is:
3207 %
3208 %      unsigned int ReadBlobLSBLong(Image *image)
3209 %
3210 %  A description of each parameter follows.
3211 %
3212 %    o image: the image.
3213 %
3214 */
3215 MagickExport unsigned int ReadBlobLSBLong(Image *image)
3216 {
3217   register const unsigned char
3218     *p;
3219
3220   register unsigned int
3221     value;
3222
3223   ssize_t
3224     count;
3225
3226   unsigned char
3227     buffer[4];
3228
3229   assert(image != (Image *) NULL);
3230   assert(image->signature == MagickSignature);
3231   *buffer='\0';
3232   p=ReadBlobStream(image,4,buffer,&count);
3233   if (count != 4)
3234     return(0U);
3235   value=(unsigned int) (*p++);
3236   value|=((unsigned int) (*p++)) << 8;
3237   value|=((unsigned int) (*p++)) << 16;
3238   value|=((unsigned int) (*p++)) << 24;
3239   return(value);
3240 }
3241 \f
3242 /*
3243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3244 %                                                                             %
3245 %                                                                             %
3246 %                                                                             %
3247 +  R e a d B l o b L S B S h o r t                                            %
3248 %                                                                             %
3249 %                                                                             %
3250 %                                                                             %
3251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3252 %
3253 %  ReadBlobLSBShort() reads a short value as a 16-bit quantity in
3254 %  least-significant byte first order.
3255 %
3256 %  The format of the ReadBlobLSBShort method is:
3257 %
3258 %      unsigned short ReadBlobLSBShort(Image *image)
3259 %
3260 %  A description of each parameter follows.
3261 %
3262 %    o image: the image.
3263 %
3264 */
3265 MagickExport unsigned short ReadBlobLSBShort(Image *image)
3266 {
3267   register const unsigned char
3268     *p;
3269
3270   register unsigned int
3271     value;
3272
3273   ssize_t
3274     count;
3275
3276   unsigned char
3277     buffer[2];
3278
3279   assert(image != (Image *) NULL);
3280   assert(image->signature == MagickSignature);
3281   *buffer='\0';
3282   p=ReadBlobStream(image,2,buffer,&count);
3283   if (count != 2)
3284     return((unsigned short) 0U);
3285   value=(unsigned int) (*p++);
3286   value|=((unsigned int) ((*p++)) << 8);
3287   return((unsigned short) (value & 0xffff));
3288 }
3289 \f
3290 /*
3291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3292 %                                                                             %
3293 %                                                                             %
3294 %                                                                             %
3295 +  R e a d B l o b M S B L o n g                                              %
3296 %                                                                             %
3297 %                                                                             %
3298 %                                                                             %
3299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3300 %
3301 %  ReadBlobMSBLong() reads a ssize_t value as a 32-bit quantity in
3302 %  most-significant byte first order.
3303 %
3304 %  The format of the ReadBlobMSBLong method is:
3305 %
3306 %      unsigned int ReadBlobMSBLong(Image *image)
3307 %
3308 %  A description of each parameter follows.
3309 %
3310 %    o image: the image.
3311 %
3312 */
3313 MagickExport unsigned int ReadBlobMSBLong(Image *image)
3314 {
3315   register const unsigned char
3316     *p;
3317
3318   register unsigned int
3319     value;
3320
3321   ssize_t
3322     count;
3323
3324   unsigned char
3325     buffer[4];
3326
3327   assert(image != (Image *) NULL);
3328   assert(image->signature == MagickSignature);
3329   *buffer='\0';
3330   p=ReadBlobStream(image,4,buffer,&count);
3331   if (count != 4)
3332     return(0UL);
3333   value=((unsigned int) (*p++) << 24);
3334   value|=((unsigned int) (*p++) << 16);
3335   value|=((unsigned int) (*p++) << 8);
3336   value|=(unsigned int) (*p++);
3337   return(value);
3338 }
3339 \f
3340 /*
3341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3342 %                                                                             %
3343 %                                                                             %
3344 %                                                                             %
3345 +  R e a d B l o b M S B L o n g L o n g                                      %
3346 %                                                                             %
3347 %                                                                             %
3348 %                                                                             %
3349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3350 %
3351 %  ReadBlobMSBLongLong() reads a ssize_t value as a 64-bit quantity in
3352 %  most-significant byte first order.
3353 %
3354 %  The format of the ReadBlobMSBLongLong method is:
3355 %
3356 %      unsigned int ReadBlobMSBLongLong(Image *image)
3357 %
3358 %  A description of each parameter follows.
3359 %
3360 %    o image: the image.
3361 %
3362 */
3363 MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
3364 {
3365   register const unsigned char
3366     *p;
3367
3368   register MagickSizeType
3369     value;
3370
3371   ssize_t
3372     count;
3373
3374   unsigned char
3375     buffer[8];
3376
3377   assert(image != (Image *) NULL);
3378   assert(image->signature == MagickSignature);
3379   *buffer='\0';
3380   p=ReadBlobStream(image,8,buffer,&count);
3381   if (count != 8)
3382     return(MagickULLConstant(0));
3383   value=((MagickSizeType) (*p++)) << 56;
3384   value|=((MagickSizeType) (*p++)) << 48;
3385   value|=((MagickSizeType) (*p++)) << 40;
3386   value|=((MagickSizeType) (*p++)) << 32;
3387   value|=((MagickSizeType) (*p++)) << 24;
3388   value|=((MagickSizeType) (*p++)) << 16;
3389   value|=((MagickSizeType) (*p++)) << 8;
3390   value|=((MagickSizeType) (*p++));
3391   return(value & MagickULLConstant(0xffffffffffffffff));
3392 }
3393 \f
3394 /*
3395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3396 %                                                                             %
3397 %                                                                             %
3398 %                                                                             %
3399 +  R e a d B l o b M S B S h o r t                                            %
3400 %                                                                             %
3401 %                                                                             %
3402 %                                                                             %
3403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3404 %
3405 %  ReadBlobMSBShort() reads a short value as a 16-bit quantity in
3406 %  most-significant byte first order.
3407 %
3408 %  The format of the ReadBlobMSBShort method is:
3409 %
3410 %      unsigned short ReadBlobMSBShort(Image *image)
3411 %
3412 %  A description of each parameter follows.
3413 %
3414 %    o image: the image.
3415 %
3416 */
3417 MagickExport unsigned short ReadBlobMSBShort(Image *image)
3418 {
3419   register const unsigned char
3420     *p;
3421
3422   register unsigned int
3423     value;
3424
3425   ssize_t
3426     count;
3427
3428   unsigned char
3429     buffer[2];
3430
3431   assert(image != (Image *) NULL);
3432   assert(image->signature == MagickSignature);
3433   *buffer='\0';
3434   p=ReadBlobStream(image,2,buffer,&count);
3435   if (count != 2)
3436     return((unsigned short) 0U);
3437   value=(unsigned int) ((*p++) << 8);
3438   value|=(unsigned int) (*p++);
3439   return((unsigned short) (value & 0xffff));
3440 }
3441 \f
3442 /*
3443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3444 %                                                                             %
3445 %                                                                             %
3446 %                                                                             %
3447 +   R e a d B l o b S t r i n g                                               %
3448 %                                                                             %
3449 %                                                                             %
3450 %                                                                             %
3451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3452 %
3453 %  ReadBlobString() reads characters from a blob or file until a newline
3454 %  character is read or an end-of-file condition is encountered.
3455 %
3456 %  The format of the ReadBlobString method is:
3457 %
3458 %      char *ReadBlobString(Image *image,char *string)
3459 %
3460 %  A description of each parameter follows:
3461 %
3462 %    o image: the image.
3463 %
3464 %    o string: the address of a character buffer.
3465 %
3466 */
3467 MagickExport char *ReadBlobString(Image *image,char *string)
3468 {
3469   register const unsigned char
3470     *p;
3471
3472   register ssize_t
3473     i;
3474
3475   ssize_t
3476     count;
3477
3478   unsigned char
3479     buffer[1];
3480
3481   assert(image != (Image *) NULL);
3482   assert(image->signature == MagickSignature);
3483   for (i=0; i < (MaxTextExtent-1L); i++)
3484   {
3485     p=ReadBlobStream(image,1,buffer,&count);
3486     if (count != 1)
3487       {
3488         if (i == 0)
3489           return((char *) NULL);
3490         break;
3491       }
3492     string[i]=(char) (*p);
3493     if ((string[i] == '\r') || (string[i] == '\n'))
3494       break;
3495   }
3496   if (string[i] == '\r')
3497     (void) ReadBlobStream(image,1,buffer,&count);
3498   string[i]='\0';
3499   return(string);
3500 }
3501 \f
3502 /*
3503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3504 %                                                                             %
3505 %                                                                             %
3506 %                                                                             %
3507 +   R e f e r e n c e B l o b                                                 %
3508 %                                                                             %
3509 %                                                                             %
3510 %                                                                             %
3511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3512 %
3513 %  ReferenceBlob() increments the reference count associated with the pixel
3514 %  blob returning a pointer to the blob.
3515 %
3516 %  The format of the ReferenceBlob method is:
3517 %
3518 %      BlobInfo ReferenceBlob(BlobInfo *blob_info)
3519 %
3520 %  A description of each parameter follows:
3521 %
3522 %    o blob_info: the blob_info.
3523 %
3524 */
3525 MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
3526 {
3527   assert(blob != (BlobInfo *) NULL);
3528   assert(blob->signature == MagickSignature);
3529   if (blob->debug != MagickFalse)
3530     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3531   LockSemaphoreInfo(blob->semaphore);
3532   blob->reference_count++;
3533   UnlockSemaphoreInfo(blob->semaphore);
3534   return(blob);
3535 }
3536 \f
3537 /*
3538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3539 %                                                                             %
3540 %                                                                             %
3541 %                                                                             %
3542 +  S e e k B l o b                                                            %
3543 %                                                                             %
3544 %                                                                             %
3545 %                                                                             %
3546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3547 %
3548 %  SeekBlob() sets the offset in bytes from the beginning of a blob or file
3549 %  and returns the resulting offset.
3550 %
3551 %  The format of the SeekBlob method is:
3552 %
3553 %      MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
3554 %        const int whence)
3555 %
3556 %  A description of each parameter follows:
3557 %
3558 %    o image: the image.
3559 %
3560 %    o offset:  Specifies an integer representing the offset in bytes.
3561 %
3562 %    o whence:  Specifies an integer representing how the offset is
3563 %      treated relative to the beginning of the blob as follows:
3564 %
3565 %        SEEK_SET  Set position equal to offset bytes.
3566 %        SEEK_CUR  Set position to current location plus offset.
3567 %        SEEK_END  Set position to EOF plus offset.
3568 %
3569 */
3570 MagickExport MagickOffsetType SeekBlob(Image *image,
3571   const MagickOffsetType offset,const int whence)
3572 {
3573   assert(image != (Image *) NULL);
3574   assert(image->signature == MagickSignature);
3575   if (image->debug != MagickFalse)
3576     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3577   assert(image->blob != (BlobInfo *) NULL);
3578   assert(image->blob->type != UndefinedStream);
3579   switch (image->blob->type)
3580   {
3581     case UndefinedStream:
3582       break;
3583     case StandardStream:
3584       return(-1);
3585     case FileStream:
3586     {
3587       if (fseek(image->blob->file_info.file,offset,whence) < 0)
3588         return(-1);
3589       image->blob->offset=TellBlob(image);
3590       break;
3591     }
3592     case PipeStream:
3593     case ZipStream:
3594     {
3595 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3596       if (gzseek(image->blob->file_info.gzfile,(off_t) offset,whence) < 0)
3597         return(-1);
3598 #endif
3599       image->blob->offset=TellBlob(image);
3600       break;
3601     }
3602     case BZipStream:
3603       return(-1);
3604     case FifoStream:
3605       return(-1);
3606     case BlobStream:
3607     {
3608       switch (whence)
3609       {
3610         case SEEK_SET:
3611         default:
3612         {
3613           if (offset < 0)
3614             return(-1);
3615           image->blob->offset=offset;
3616           break;
3617         }
3618         case SEEK_CUR:
3619         {
3620           if ((image->blob->offset+offset) < 0)
3621             return(-1);
3622           image->blob->offset+=offset;
3623           break;
3624         }
3625         case SEEK_END:
3626         {
3627           if (((MagickOffsetType) image->blob->length+offset) < 0)
3628             return(-1);
3629           image->blob->offset=image->blob->length+offset;
3630           break;
3631         }
3632       }
3633       if (image->blob->offset <= (MagickOffsetType)
3634           ((off_t) image->blob->length))
3635         image->blob->eof=MagickFalse;
3636       else
3637         if (image->blob->mapped != MagickFalse)
3638           return(-1);
3639         else
3640           {
3641             image->blob->extent=(size_t) (image->blob->offset+
3642               image->blob->quantum);
3643             image->blob->data=(unsigned char *) ResizeQuantumMemory(
3644               image->blob->data,image->blob->extent+1,
3645               sizeof(*image->blob->data));
3646             (void) SyncBlob(image);
3647             if (image->blob->data == (unsigned char *) NULL)
3648               {
3649                 (void) DetachBlob(image->blob);
3650                 return(-1);
3651               }
3652           }
3653       break;
3654     }
3655   }
3656   return(image->blob->offset);
3657 }
3658 \f
3659 /*
3660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3661 %                                                                             %
3662 %                                                                             %
3663 %                                                                             %
3664 +   S e t B l o b E x e m p t                                                 %
3665 %                                                                             %
3666 %                                                                             %
3667 %                                                                             %
3668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3669 %
3670 %  SetBlobExempt() sets the blob exempt status.
3671 %
3672 %  The format of the SetBlobExempt method is:
3673 %
3674 %      MagickBooleanType SetBlobExempt(const Image *image,
3675 %        const MagickBooleanType exempt)
3676 %
3677 %  A description of each parameter follows:
3678 %
3679 %    o image: the image.
3680 %
3681 %    o exempt: Set to true if this blob is exempt from being closed.
3682 %
3683 */
3684 MagickPrivate void SetBlobExempt(Image *image,const MagickBooleanType exempt)
3685 {
3686   assert(image != (const Image *) NULL);
3687   assert(image->signature == MagickSignature);
3688   if (image->debug != MagickFalse)
3689     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3690   image->blob->exempt=exempt;
3691 }
3692 \f
3693 /*
3694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3695 %                                                                             %
3696 %                                                                             %
3697 %                                                                             %
3698 +  S e t B l o b E x t e n t                                                  %
3699 %                                                                             %
3700 %                                                                             %
3701 %                                                                             %
3702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3703 %
3704 %  SetBlobExtent() ensures enough space is allocated for the blob.  If the
3705 %  method is successful, subsequent writes to bytes in the specified range are
3706 %  guaranteed not to fail.
3707 %
3708 %  The format of the SetBlobExtent method is:
3709 %
3710 %      MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
3711 %
3712 %  A description of each parameter follows:
3713 %
3714 %    o image: the image.
3715 %
3716 %    o extent:  the blob maximum extent.
3717 %
3718 */
3719 MagickPrivate MagickBooleanType SetBlobExtent(Image *image,
3720   const MagickSizeType extent)
3721 {
3722   assert(image != (Image *) NULL);
3723   assert(image->signature == MagickSignature);
3724   if (image->debug != MagickFalse)
3725     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3726   assert(image->blob != (BlobInfo *) NULL);
3727   assert(image->blob->type != UndefinedStream);
3728   switch (image->blob->type)
3729   {
3730     case UndefinedStream:
3731       break;
3732     case StandardStream:
3733       return(MagickFalse);
3734     case FileStream:
3735     {
3736       MagickOffsetType
3737         offset;
3738
3739       ssize_t
3740         count;
3741
3742       if (extent != (MagickSizeType) ((off_t) extent))
3743         return(MagickFalse);
3744       offset=SeekBlob(image,0,SEEK_END);
3745       if (offset < 0)
3746         return(MagickFalse);
3747       if ((MagickSizeType) offset >= extent)
3748         break;
3749       offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
3750       count=fwrite((const unsigned char *) "",1,1,image->blob->file_info.file);
3751       offset=SeekBlob(image,offset,SEEK_SET);
3752       if (count != (MagickOffsetType) 1)
3753         return(MagickFalse);
3754       break;
3755     }
3756     case PipeStream:
3757     case ZipStream:
3758       return(MagickFalse);
3759     case BZipStream:
3760       return(MagickFalse);
3761     case FifoStream:
3762       return(MagickFalse);
3763     case BlobStream:
3764     {
3765       if (extent != (MagickSizeType) ((size_t) extent))
3766         return(MagickFalse);
3767       if (image->blob->mapped != MagickFalse)
3768         {
3769           MagickOffsetType
3770             offset;
3771
3772           ssize_t
3773             count;
3774
3775           (void) UnmapBlob(image->blob->data,image->blob->length);
3776           if (extent != (MagickSizeType) ((off_t) extent))
3777             return(MagickFalse);
3778           offset=SeekBlob(image,0,SEEK_END);
3779           if (offset < 0)
3780             return(MagickFalse);
3781           if ((MagickSizeType) offset >= extent)
3782             break;
3783           offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
3784           count=fwrite((const unsigned char *) "",1,1,
3785             image->blob->file_info.file);
3786           offset=SeekBlob(image,offset,SEEK_SET);
3787           if (count != (MagickOffsetType) 1)
3788             return(MagickTrue);
3789           image->blob->data=(unsigned char*) MapBlob(fileno(
3790             image->blob->file_info.file),WriteMode,0,(size_t) extent);
3791           image->blob->extent=(size_t) extent;
3792           image->blob->length=(size_t) extent;
3793           (void) SyncBlob(image);
3794           break;
3795         }
3796       image->blob->extent=(size_t) extent;
3797       image->blob->data=(unsigned char *) ResizeQuantumMemory(image->blob->data,
3798         image->blob->extent+1,sizeof(*image->blob->data));
3799       (void) SyncBlob(image);
3800       if (image->blob->data == (unsigned char *) NULL)
3801         {
3802           (void) DetachBlob(image->blob);
3803           return(MagickFalse);
3804         }
3805       break;
3806     }
3807   }
3808   return(MagickTrue);
3809 }
3810 \f
3811 /*
3812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3813 %                                                                             %
3814 %                                                                             %
3815 %                                                                             %
3816 +  S y n c B l o b                                                            %
3817 %                                                                             %
3818 %                                                                             %
3819 %                                                                             %
3820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3821 %
3822 %  SyncBlob() flushes the datastream if it is a file or synchronizes the data
3823 %  attributes if it is an blob.
3824 %
3825 %  The format of the SyncBlob method is:
3826 %
3827 %      int SyncBlob(Image *image)
3828 %
3829 %  A description of each parameter follows:
3830 %
3831 %    o image: the image.
3832 %
3833 */
3834 static int SyncBlob(Image *image)
3835 {
3836   int
3837     status;
3838
3839   assert(image != (Image *) NULL);
3840   assert(image->signature == MagickSignature);
3841   if (image->debug != MagickFalse)
3842     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3843   assert(image->blob != (BlobInfo *) NULL);
3844   assert(image->blob->type != UndefinedStream);
3845   status=0;
3846   switch (image->blob->type)
3847   {
3848     case UndefinedStream:
3849     case StandardStream:
3850       break;
3851     case FileStream:
3852     case PipeStream:
3853     {
3854       status=fflush(image->blob->file_info.file);
3855       break;
3856     }
3857     case ZipStream:
3858     {
3859 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3860       status=gzflush(image->blob->file_info.gzfile,Z_SYNC_FLUSH);
3861 #endif
3862       break;
3863     }
3864     case BZipStream:
3865     {
3866 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3867       status=BZ2_bzflush(image->blob->file_info.bzfile);
3868 #endif
3869       break;
3870     }
3871     case FifoStream:
3872       break;
3873     case BlobStream:
3874     {
3875 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3876       if (image->blob->mapped != MagickFalse)
3877         status=msync(image->blob->data,image->blob->length,MS_SYNC);
3878 #endif
3879       break;
3880     }
3881   }
3882   return(status);
3883 }
3884 \f
3885 /*
3886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3887 %                                                                             %
3888 %                                                                             %
3889 %                                                                             %
3890 +  T e l l B l o b                                                            %
3891 %                                                                             %
3892 %                                                                             %
3893 %                                                                             %
3894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3895 %
3896 %  TellBlob() obtains the current value of the blob or file position.
3897 %
3898 %  The format of the TellBlob method is:
3899 %
3900 %      MagickOffsetType TellBlob(const Image *image)
3901 %
3902 %  A description of each parameter follows:
3903 %
3904 %    o image: the image.
3905 %
3906 */
3907 MagickExport MagickOffsetType TellBlob(const Image *image)
3908 {
3909   MagickOffsetType
3910     offset;
3911
3912   assert(image != (Image *) NULL);
3913   assert(image->signature == MagickSignature);
3914   if (image->debug != MagickFalse)
3915     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3916   assert(image->blob != (BlobInfo *) NULL);
3917   assert(image->blob->type != UndefinedStream);
3918   offset=(-1);
3919   switch (image->blob->type)
3920   {
3921     case UndefinedStream:
3922     case StandardStream:
3923       break;
3924     case FileStream:
3925     {
3926       offset=ftell(image->blob->file_info.file);
3927       break;
3928     }
3929     case PipeStream:
3930       break;
3931     case ZipStream:
3932     {
3933 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3934       offset=(MagickOffsetType) gztell(image->blob->file_info.gzfile);
3935 #endif
3936       break;
3937     }
3938     case BZipStream:
3939       break;
3940     case FifoStream:
3941       break;
3942     case BlobStream:
3943     {
3944       offset=image->blob->offset;
3945       break;
3946     }
3947   }
3948   return(offset);
3949 }
3950 \f
3951 /*
3952 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3953 %                                                                             %
3954 %                                                                             %
3955 %                                                                             %
3956 +  U n m a p B l o b                                                          %
3957 %                                                                             %
3958 %                                                                             %
3959 %                                                                             %
3960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3961 %
3962 %  UnmapBlob() deallocates the binary large object previously allocated with
3963 %  the MapBlob method.
3964 %
3965 %  The format of the UnmapBlob method is:
3966 %
3967 %       MagickBooleanType UnmapBlob(void *map,const size_t length)
3968 %
3969 %  A description of each parameter follows:
3970 %
3971 %    o map: the address  of the binary large object.
3972 %
3973 %    o length: the length of the binary large object.
3974 %
3975 */
3976 MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
3977 {
3978 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3979   int
3980     status;
3981
3982   status=munmap(map,length);
3983   return(status == -1 ? MagickFalse : MagickTrue);
3984 #else
3985   (void) map;
3986   (void) length;
3987   return(MagickFalse);
3988 #endif
3989 }
3990 \f
3991 /*
3992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3993 %                                                                             %
3994 %                                                                             %
3995 %                                                                             %
3996 +  W r i t e B l o b                                                          %
3997 %                                                                             %
3998 %                                                                             %
3999 %                                                                             %
4000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4001 %
4002 %  WriteBlob() writes data to a blob or image file.  It returns the number of
4003 %  bytes written.
4004 %
4005 %  The format of the WriteBlob method is:
4006 %
4007 %      ssize_t WriteBlob(Image *image,const size_t length,
4008 %        const unsigned char *data)
4009 %
4010 %  A description of each parameter follows:
4011 %
4012 %    o image: the image.
4013 %
4014 %    o length:  Specifies an integer representing the number of bytes to
4015 %      write to the file.
4016 %
4017 %    o data:  The address of the data to write to the blob or file.
4018 %
4019 */
4020 MagickExport ssize_t WriteBlob(Image *image,const size_t length,
4021   const unsigned char *data)
4022 {
4023   int
4024     c;
4025
4026   register const unsigned char
4027     *p;
4028
4029   ssize_t
4030     count;
4031
4032   assert(image != (Image *) NULL);
4033   assert(image->signature == MagickSignature);
4034   assert(data != (const unsigned char *) NULL);
4035   assert(image->blob != (BlobInfo *) NULL);
4036   assert(image->blob->type != UndefinedStream);
4037   if (length == 0)
4038     return(0);
4039   count=0;
4040   p=data;
4041   switch (image->blob->type)
4042   {
4043     case UndefinedStream:
4044       break;
4045     case StandardStream:
4046     {
4047       register ssize_t
4048         i;
4049
4050       count=0;
4051       for (i=0; i < (MagickOffsetType) length; i+=count)
4052       {
4053         count=write(fileno(image->blob->file_info.file),data+i,(size_t)
4054           MagickMin(length-i,SSIZE_MAX));
4055         if (count <= 0)
4056           {
4057             count=0;
4058             if (errno != EINTR)
4059               break;
4060           }
4061       }
4062       count=i;
4063       break;
4064     }
4065     case FileStream:
4066     case PipeStream:
4067     {
4068       switch (length)
4069       {
4070         default:
4071         {
4072           count=(ssize_t) fwrite((const char *) data,1,length,
4073             image->blob->file_info.file);
4074           break;
4075         }
4076         case 2:
4077         {
4078           c=putc((int) *p++,image->blob->file_info.file);
4079           if (c == EOF)
4080             break;
4081           count++;
4082         }
4083         case 1:
4084         {
4085           c=putc((int) *p++,image->blob->file_info.file);
4086           if (c == EOF)
4087             break;
4088           count++;
4089         }
4090         case 0:
4091           break;
4092       }
4093       break;
4094     }
4095     case ZipStream:
4096     {
4097 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4098       switch (length)
4099       {
4100         default:
4101         {
4102           count=(ssize_t) gzwrite(image->blob->file_info.gzfile,(void *) data,
4103             (unsigned int) length);
4104           break;
4105         }
4106         case 2:
4107         {
4108           c=gzputc(image->blob->file_info.gzfile,(int) *p++);
4109           if (c == EOF)
4110             break;
4111           count++;
4112         }
4113         case 1:
4114         {
4115           c=gzputc(image->blob->file_info.gzfile,(int) *p++);
4116           if (c == EOF)
4117             break;
4118           count++;
4119         }
4120         case 0:
4121           break;
4122       }
4123 #endif
4124       break;
4125     }
4126     case BZipStream:
4127     {
4128 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4129       count=(ssize_t) BZ2_bzwrite(image->blob->file_info.bzfile,(void *) data,
4130         (int) length);
4131 #endif
4132       break;
4133     }
4134     case FifoStream:
4135     {
4136       count=(ssize_t) image->blob->stream(image,data,length);
4137       break;
4138     }
4139     case BlobStream:
4140     {
4141       register unsigned char
4142         *q;
4143
4144       if ((image->blob->offset+(MagickOffsetType) length) >=
4145           (MagickOffsetType) image->blob->extent)
4146         {
4147           if (image->blob->mapped != MagickFalse)
4148             return(0);
4149           image->blob->quantum<<=1;
4150           image->blob->extent+=length+image->blob->quantum;
4151           image->blob->data=(unsigned char *) ResizeQuantumMemory(
4152             image->blob->data,image->blob->extent+1,sizeof(*image->blob->data));
4153           (void) SyncBlob(image);
4154           if (image->blob->data == (unsigned char *) NULL)
4155             {
4156               (void) DetachBlob(image->blob);
4157               return(0);
4158             }
4159         }
4160       q=image->blob->data+image->blob->offset;
4161       (void) memcpy(q,p,length);
4162       image->blob->offset+=length;
4163       if (image->blob->offset >= (MagickOffsetType) image->blob->length)
4164         image->blob->length=(size_t) image->blob->offset;
4165       count=(ssize_t) length;
4166     }
4167   }
4168   return(count);
4169 }
4170 \f
4171 /*
4172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4173 %                                                                             %
4174 %                                                                             %
4175 %                                                                             %
4176 +  W r i t e B l o b B y t e                                                  %
4177 %                                                                             %
4178 %                                                                             %
4179 %                                                                             %
4180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4181 %
4182 %  WriteBlobByte() write an integer to a blob.  It returns the number of bytes
4183 %  written (either 0 or 1);
4184 %
4185 %  The format of the WriteBlobByte method is:
4186 %
4187 %      ssize_t WriteBlobByte(Image *image,const unsigned char value)
4188 %
4189 %  A description of each parameter follows.
4190 %
4191 %    o image: the image.
4192 %
4193 %    o value: Specifies the value to write.
4194 %
4195 */
4196 MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
4197 {
4198   assert(image != (Image *) NULL);
4199   assert(image->signature == MagickSignature);
4200   return(WriteBlobStream(image,1,&value));
4201 }
4202 \f
4203 /*
4204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4205 %                                                                             %
4206 %                                                                             %
4207 %                                                                             %
4208 +  W r i t e B l o b F l o a t                                                %
4209 %                                                                             %
4210 %                                                                             %
4211 %                                                                             %
4212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4213 %
4214 %  WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
4215 %  specified by the endian member of the image structure.
4216 %
4217 %  The format of the WriteBlobFloat method is:
4218 %
4219 %      ssize_t WriteBlobFloat(Image *image,const float value)
4220 %
4221 %  A description of each parameter follows.
4222 %
4223 %    o image: the image.
4224 %
4225 %    o value: Specifies the value to write.
4226 %
4227 */
4228 MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
4229 {
4230   union
4231   {
4232     unsigned int
4233       unsigned_value;
4234
4235     float
4236       float_value;
4237   } quantum;
4238
4239   quantum.unsigned_value=0U;
4240   quantum.float_value=value;
4241   return(WriteBlobLong(image,quantum.unsigned_value));
4242 }
4243 \f
4244 /*
4245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4246 %                                                                             %
4247 %                                                                             %
4248 %                                                                             %
4249 +  W r i t e B l o b L o n g                                                  %
4250 %                                                                             %
4251 %                                                                             %
4252 %                                                                             %
4253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4254 %
4255 %  WriteBlobLong() writes a ssize_t value as a 32-bit quantity in the byte-order
4256 %  specified by the endian member of the image structure.
4257 %
4258 %  The format of the WriteBlobLong method is:
4259 %
4260 %      ssize_t WriteBlobLong(Image *image,const unsigned int value)
4261 %
4262 %  A description of each parameter follows.
4263 %
4264 %    o image: the image.
4265 %
4266 %    o value: Specifies the value to write.
4267 %
4268 */
4269 MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
4270 {
4271   unsigned char
4272     buffer[4];
4273
4274   assert(image != (Image *) NULL);
4275   assert(image->signature == MagickSignature);
4276   if (image->endian == LSBEndian)
4277     {
4278       buffer[0]=(unsigned char) value;
4279       buffer[1]=(unsigned char) (value >> 8);
4280       buffer[2]=(unsigned char) (value >> 16);
4281       buffer[3]=(unsigned char) (value >> 24);
4282       return(WriteBlobStream(image,4,buffer));
4283     }
4284   buffer[0]=(unsigned char) (value >> 24);
4285   buffer[1]=(unsigned char) (value >> 16);
4286   buffer[2]=(unsigned char) (value >> 8);
4287   buffer[3]=(unsigned char) value;
4288   return(WriteBlobStream(image,4,buffer));
4289 }
4290 \f
4291 /*
4292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4293 %                                                                             %
4294 %                                                                             %
4295 %                                                                             %
4296 +   W r i t e B l o b S h o r t                                               %
4297 %                                                                             %
4298 %                                                                             %
4299 %                                                                             %
4300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4301 %
4302 %  WriteBlobShort() writes a short value as a 16-bit quantity in the
4303 %  byte-order specified by the endian member of the image structure.
4304 %
4305 %  The format of the WriteBlobShort method is:
4306 %
4307 %      ssize_t WriteBlobShort(Image *image,const unsigned short value)
4308 %
4309 %  A description of each parameter follows.
4310 %
4311 %    o image: the image.
4312 %
4313 %    o value:  Specifies the value to write.
4314 %
4315 */
4316 MagickExport ssize_t WriteBlobShort(Image *image,const unsigned short value)
4317 {
4318   unsigned char
4319     buffer[2];
4320
4321   assert(image != (Image *) NULL);
4322   assert(image->signature == MagickSignature);
4323   if (image->endian == LSBEndian)
4324     {
4325       buffer[0]=(unsigned char) value;
4326       buffer[1]=(unsigned char) (value >> 8);
4327       return(WriteBlobStream(image,2,buffer));
4328     }
4329   buffer[0]=(unsigned char) (value >> 8);
4330   buffer[1]=(unsigned char) value;
4331   return(WriteBlobStream(image,2,buffer));
4332 }
4333 \f
4334 /*
4335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4336 %                                                                             %
4337 %                                                                             %
4338 %                                                                             %
4339 +  W r i t e B l o b L S B L o n g                                            %
4340 %                                                                             %
4341 %                                                                             %
4342 %                                                                             %
4343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4344 %
4345 %  WriteBlobLSBLong() writes a ssize_t value as a 32-bit quantity in
4346 %  least-significant byte first order.
4347 %
4348 %  The format of the WriteBlobLSBLong method is:
4349 %
4350 %      ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4351 %
4352 %  A description of each parameter follows.
4353 %
4354 %    o image: the image.
4355 %
4356 %    o value: Specifies the value to write.
4357 %
4358 */
4359 MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4360 {
4361   unsigned char
4362     buffer[4];
4363
4364   assert(image != (Image *) NULL);
4365   assert(image->signature == MagickSignature);
4366   buffer[0]=(unsigned char) value;
4367   buffer[1]=(unsigned char) (value >> 8);
4368   buffer[2]=(unsigned char) (value >> 16);
4369   buffer[3]=(unsigned char) (value >> 24);
4370   return(WriteBlobStream(image,4,buffer));
4371 }
4372 \f
4373 /*
4374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4375 %                                                                             %
4376 %                                                                             %
4377 %                                                                             %
4378 +   W r i t e B l o b L S B S h o r t                                         %
4379 %                                                                             %
4380 %                                                                             %
4381 %                                                                             %
4382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4383 %
4384 %  WriteBlobLSBShort() writes a ssize_t value as a 16-bit quantity in
4385 %  least-significant byte first order.
4386 %
4387 %  The format of the WriteBlobLSBShort method is:
4388 %
4389 %      ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4390 %
4391 %  A description of each parameter follows.
4392 %
4393 %    o image: the image.
4394 %
4395 %    o value:  Specifies the value to write.
4396 %
4397 */
4398 MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4399 {
4400   unsigned char
4401     buffer[2];
4402
4403   assert(image != (Image *) NULL);
4404   assert(image->signature == MagickSignature);
4405   buffer[0]=(unsigned char) value;
4406   buffer[1]=(unsigned char) (value >> 8);
4407   return(WriteBlobStream(image,2,buffer));
4408 }
4409 \f
4410 /*
4411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4412 %                                                                             %
4413 %                                                                             %
4414 %                                                                             %
4415 +  W r i t e B l o b M S B L o n g                                            %
4416 %                                                                             %
4417 %                                                                             %
4418 %                                                                             %
4419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4420 %
4421 %  WriteBlobMSBLong() writes a ssize_t value as a 32-bit quantity in
4422 %  most-significant byte first order.
4423 %
4424 %  The format of the WriteBlobMSBLong method is:
4425 %
4426 %      ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4427 %
4428 %  A description of each parameter follows.
4429 %
4430 %    o value:  Specifies the value to write.
4431 %
4432 %    o image: the image.
4433 %
4434 */
4435 MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4436 {
4437   unsigned char
4438     buffer[4];
4439
4440   assert(image != (Image *) NULL);
4441   assert(image->signature == MagickSignature);
4442   buffer[0]=(unsigned char) (value >> 24);
4443   buffer[1]=(unsigned char) (value >> 16);
4444   buffer[2]=(unsigned char) (value >> 8);
4445   buffer[3]=(unsigned char) value;
4446   return(WriteBlobStream(image,4,buffer));
4447 }
4448 \f
4449 /*
4450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4451 %                                                                             %
4452 %                                                                             %
4453 %                                                                             %
4454 +  W r i t e B l o b M S B L o n g L o n g                                    %
4455 %                                                                             %
4456 %                                                                             %
4457 %                                                                             %
4458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4459 %
4460 %  WriteBlobMSBLongLong() writes a long long value as a 64-bit quantity in
4461 %  most-significant byte first order.
4462 %
4463 %  The format of the WriteBlobMSBLongLong method is:
4464 %
4465 %      ssize_t WriteBlobMSBLongLong(Image *image,const MagickSizeType value)
4466 %
4467 %  A description of each parameter follows.
4468 %
4469 %    o value:  Specifies the value to write.
4470 %
4471 %    o image: the image.
4472 %
4473 */
4474 MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
4475   const MagickSizeType value)
4476 {
4477   unsigned char
4478     buffer[8];
4479
4480   assert(image != (Image *) NULL);
4481   assert(image->signature == MagickSignature);
4482   buffer[0]=(unsigned char) (value >> 56);
4483   buffer[1]=(unsigned char) (value >> 48);
4484   buffer[2]=(unsigned char) (value >> 40);
4485   buffer[3]=(unsigned char) (value >> 32);
4486   buffer[4]=(unsigned char) (value >> 24);
4487   buffer[5]=(unsigned char) (value >> 16);
4488   buffer[6]=(unsigned char) (value >> 8);
4489   buffer[7]=(unsigned char) value;
4490   return(WriteBlobStream(image,8,buffer));
4491 }
4492 \f
4493 /*
4494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4495 %                                                                             %
4496 %                                                                             %
4497 %                                                                             %
4498 +  W r i t e B l o b M S B S h o r t                                          %
4499 %                                                                             %
4500 %                                                                             %
4501 %                                                                             %
4502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4503 %
4504 %  WriteBlobMSBShort() writes a ssize_t value as a 16-bit quantity in
4505 %  most-significant byte first order.
4506 %
4507 %  The format of the WriteBlobMSBShort method is:
4508 %
4509 %      ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4510 %
4511 %  A description of each parameter follows.
4512 %
4513 %   o  value:  Specifies the value to write.
4514 %
4515 %   o  file:  Specifies the file to write the data to.
4516 %
4517 */
4518 MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4519 {
4520   unsigned char
4521     buffer[2];
4522
4523   assert(image != (Image *) NULL);
4524   assert(image->signature == MagickSignature);
4525   buffer[0]=(unsigned char) (value >> 8);
4526   buffer[1]=(unsigned char) value;
4527   return(WriteBlobStream(image,2,buffer));
4528 }
4529 \f
4530 /*
4531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4532 %                                                                             %
4533 %                                                                             %
4534 %                                                                             %
4535 +  W r i t e B l o b S t r i n g                                              %
4536 %                                                                             %
4537 %                                                                             %
4538 %                                                                             %
4539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4540 %
4541 %  WriteBlobString() write a string to a blob.  It returns the number of
4542 %  characters written.
4543 %
4544 %  The format of the WriteBlobString method is:
4545 %
4546 %      ssize_t WriteBlobString(Image *image,const char *string)
4547 %
4548 %  A description of each parameter follows.
4549 %
4550 %    o image: the image.
4551 %
4552 %    o string: Specifies the string to write.
4553 %
4554 */
4555 MagickExport ssize_t WriteBlobString(Image *image,const char *string)
4556 {
4557   assert(image != (Image *) NULL);
4558   assert(image->signature == MagickSignature);
4559   assert(string != (const char *) NULL);
4560   return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));
4561 }