2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % BBBB LLLLL OOO BBBB %
13 % MagickCore Binary Large OBjectS Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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)
70 #if defined(MAGICKCORE_BZLIB_DELEGATE)
77 #define MagickMaxBlobExtent 65541
78 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
79 # define MAP_ANONYMOUS MAP_ANON
81 #if !defined(MAP_FAILED)
82 #define MAP_FAILED ((void *) -1)
89 #define _O_BINARY O_BINARY
95 typedef union FileInfo
100 #if defined(MAGICKCORE_ZLIB_DELEGATE)
105 #if defined(MAGICKCORE_BZLIB_DELEGATE)
163 Forward declarations.
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 + A t t a c h B l o b %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % AttachBlob() attaches a blob to the BlobInfo structure.
181 % The format of the AttachBlob method is:
183 % void AttachBlob(BlobInfo *blob_info,const void *blob,const size_t length)
185 % A description of each parameter follows:
187 % o blob_info: Specifies a pointer to a BlobInfo structure.
189 % o blob: the address of a character stream in one of the image formats
190 % understood by ImageMagick.
192 % o length: This size_t integer reflects the length in bytes of the blob.
195 MagickExport void AttachBlob(BlobInfo *blob_info,const void *blob,
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;
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;
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 + B l o b T o F i l e %
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 % BlobToFile() writes a blob to a file. It returns MagickFalse if an error
223 % occurs otherwise MagickTrue.
225 % The format of the BlobToFile method is:
227 % MagickBooleanType BlobToFile(char *filename,const void *blob,
228 % const size_t length,ExceptionInfo *exception)
230 % A description of each parameter follows:
232 % o filename: Write the blob to this file.
234 % o blob: the address of a blob.
236 % o length: This length in bytes of the blob.
238 % o exception: return any errors or warnings in this structure.
242 static inline MagickSizeType MagickMin(const MagickSizeType x,
243 const MagickSizeType y)
250 MagickExport MagickBooleanType BlobToFile(char *filename,const void *blob,
251 const size_t length,ExceptionInfo *exception)
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);
268 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
271 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
274 for (i=0; i < length; i+=count)
276 count=(ssize_t) write(file,(const char *) blob+i,(size_t) MagickMin(length-
277 i,(MagickSizeType) SSIZE_MAX));
286 if ((file == -1) || (i < length))
288 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 % B l o b T o I m a g e %
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 % BlobToImage() implements direct to memory image formats. It returns the
308 % The format of the BlobToImage method is:
310 % Image *BlobToImage(const ImageInfo *image_info,const void *blob,
311 % const size_t length,ExceptionInfo *exception)
313 % A description of each parameter follows:
315 % o image_info: the image info.
317 % o blob: the address of a character stream in one of the image formats
318 % understood by ImageMagick.
320 % o length: This size_t integer reflects the length in bytes of the blob.
322 % o exception: return any errors or warnings in this structure.
325 MagickExport Image *BlobToImage(const ImageInfo *image_info,const void *blob,
326 const size_t length,ExceptionInfo *exception)
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))
349 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
350 "ZeroLengthBlobNotPermitted","'%s'",image_info->filename);
351 return((Image *) NULL);
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)
361 blob_info=DestroyImageInfo(blob_info);
362 (void) ThrowMagickException(exception,GetMagickModule(),
363 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
364 image_info->filename);
365 return((Image *) NULL);
367 if (GetMagickBlobSupport(magick_info) != MagickFalse)
370 Native blob support for this image format.
372 (void) CopyMagickString(blob_info->filename,image_info->filename,
374 (void) CopyMagickString(blob_info->magick,image_info->magick,
376 image=ReadImage(blob_info,exception);
377 if (image != (Image *) NULL)
378 (void) DetachBlob(image->blob);
379 blob_info=DestroyImageInfo(blob_info);
383 Write blob to a temporary file on disk.
385 blob_info->blob=(void *) NULL;
387 *blob_info->filename='\0';
388 status=BlobToFile(blob_info->filename,blob,length,exception);
389 if (status == MagickFalse)
391 (void) RelinquishUniqueFileResource(blob_info->filename);
392 blob_info=DestroyImageInfo(blob_info);
393 return((Image *) NULL);
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)
405 Restore original filenames.
407 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
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);
416 clone_info=DestroyImageInfo(clone_info);
417 (void) RelinquishUniqueFileResource(blob_info->filename);
418 blob_info=DestroyImageInfo(blob_info);
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 + C l o n e B l o b I n f o %
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433 % CloneBlobInfo() makes a duplicate of the given blob info structure, or if
434 % blob info is NULL, a new one.
436 % The format of the CloneBlobInfo method is:
438 % BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
440 % A description of each parameter follows:
442 % o blob_info: the blob info.
445 MagickExport BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
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)
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;
478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 + C l o s e B l o b %
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 % CloseBlob() closes a stream associated with the image.
490 % The format of the CloseBlob method is:
492 % MagickBooleanType CloseBlob(Image *image)
494 % A description of each parameter follows:
496 % o image: the image.
499 MagickExport MagickBooleanType CloseBlob(Image *image)
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)
514 if (image->blob->synchronize != MagickFalse)
516 image->blob->size=GetBlobSize(image);
517 image->extent=image->blob->size;
518 image->blob->eof=MagickFalse;
519 if (image->blob->exempt != MagickFalse)
521 image->blob->type=UndefinedStream;
525 switch (image->blob->type)
527 case UndefinedStream:
533 status=ferror(image->blob->file_info.file);
538 #if defined(MAGICKCORE_ZLIB_DELEGATE)
539 (void) gzerror(image->blob->file_info.gzfile,&status);
545 #if defined(MAGICKCORE_BZLIB_DELEGATE)
546 (void) BZ2_bzerror(image->blob->file_info.bzfile,&status);
554 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
555 switch (image->blob->type)
557 case UndefinedStream:
562 if (image->blob->synchronize != MagickFalse)
564 status=fflush(image->blob->file_info.file);
565 status=fsync(fileno(image->blob->file_info.file));
567 status=fclose(image->blob->file_info.file);
572 #if defined(MAGICKCORE_HAVE_PCLOSE)
573 status=pclose(image->blob->file_info.file);
579 #if defined(MAGICKCORE_ZLIB_DELEGATE)
580 status=gzclose(image->blob->file_info.gzfile);
586 #if defined(MAGICKCORE_BZLIB_DELEGATE)
587 BZ2_bzclose(image->blob->file_info.bzfile);
595 if (image->blob->file_info.file != (FILE *) NULL)
597 if (image->blob->synchronize != MagickFalse)
598 (void) fsync(fileno(image->blob->file_info.file));
599 status=fclose(image->blob->file_info.file);
604 (void) DetachBlob(image->blob);
605 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
606 return(image->blob->status);
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 + D e s t r o y B l o b %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % DestroyBlob() deallocates memory associated with a blob.
622 % The format of the DestroyBlob method is:
624 % void DestroyBlob(Image *image)
626 % A description of each parameter follows:
628 % o image: the image.
631 MagickExport void DestroyBlob(Image *image)
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);
643 LockSemaphoreInfo(image->blob->semaphore);
644 image->blob->reference_count--;
645 assert(image->blob->reference_count >= 0);
646 if (image->blob->reference_count == 0)
648 UnlockSemaphoreInfo(image->blob->semaphore);
649 if (destroy == MagickFalse)
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);
661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665 + D e t a c h B l o b %
669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 % DetachBlob() detaches a blob from the BlobInfo structure.
673 % The format of the DetachBlob method is:
675 % unsigned char *DetachBlob(BlobInfo *blob_info)
677 % A description of each parameter follows:
679 % o blob_info: Specifies a pointer to a BlobInfo structure.
682 MagickExport unsigned char *DetachBlob(BlobInfo *blob_info)
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;
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;
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 + D i s c a r d B l o b B y t e s %
714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716 % DiscardBlobBytes() discards bytes in a blob.
718 % The format of the DiscardBlobBytes method is:
720 % MagickBooleanType DiscardBlobBytes(Image *image,const size_t length)
722 % A description of each parameter follows.
724 % o image: the image.
726 % o length: the number of bytes to skip.
730 static inline const unsigned char *ReadBlobStream(Image *image,
731 const size_t length,unsigned char *data,ssize_t *count)
733 assert(count != (ssize_t *) NULL);
734 assert(image->blob != (BlobInfo *) NULL);
735 if (image->blob->type != BlobStream)
737 *count=ReadBlob(image,length,data);
740 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
743 image->blob->eof=MagickTrue;
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;
755 MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
756 const MagickSizeType length)
758 register MagickOffsetType
770 assert(image != (Image *) NULL);
771 assert(image->signature == MagickSignature);
773 for (i=0; i < (MagickOffsetType) length; i+=count)
775 quantum=(size_t) MagickMin(length-i,sizeof(buffer));
776 (void) ReadBlobStream(image,quantum,buffer,&count);
784 return(i < (MagickOffsetType) length ? MagickFalse : MagickTrue);
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792 + D u p l i c a t e s B l o b %
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 % DuplicateBlob() duplicates a blob descriptor.
800 % The format of the DuplicateBlob method is:
802 % void DuplicateBlob(Image *image,const Image *duplicate)
804 % A description of each parameter follows:
806 % o image: the image.
808 % o duplicate: the duplicate image.
811 MagickExport void DuplicateBlob(Image *image,const Image *duplicate)
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);
820 image->blob=ReferenceBlob(duplicate->blob);
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834 % EOFBlob() returns a non-zero value when EOF has been detected reading from
837 % The format of the EOFBlob method is:
839 % int EOFBlob(const Image *image)
841 % A description of each parameter follows:
843 % o image: the image.
846 MagickExport int EOFBlob(const Image *image)
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)
856 case UndefinedStream:
862 image->blob->eof=feof(image->blob->file_info.file) != 0 ? MagickTrue :
868 image->blob->eof=MagickFalse;
873 #if defined(MAGICKCORE_BZLIB_DELEGATE)
878 (void) BZ2_bzerror(image->blob->file_info.bzfile,&status);
879 image->blob->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
885 image->blob->eof=MagickFalse;
891 return((int) image->blob->eof);
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 + F i l e T o B l o b %
903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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().
910 % The format of the FileToBlob method is:
912 % unsigned char *FileToBlob(const char *filename,const size_t extent,
913 % size_t *length,ExceptionInfo *exception)
915 % A description of each parameter follows:
917 % o blob: FileToBlob() returns the contents of a file as a blob. If
918 % an error occurs NULL is returned.
920 % o filename: the filename.
922 % o extent: The maximum length of the blob.
924 % o length: On return, this reflects the actual length of the blob.
926 % o exception: return any errors or warnings in this structure.
929 MagickExport unsigned char *FileToBlob(const char *filename,const size_t extent,
930 size_t *length,ExceptionInfo *exception)
950 assert(filename != (const char *) NULL);
951 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
952 assert(exception != (ExceptionInfo *) NULL);
955 if (LocaleCompare(filename,"-") != 0)
956 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
959 ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
960 return((unsigned char *) NULL);
962 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
964 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
973 Stream is not seekable.
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)
982 count=(ssize_t) read(file,blob+i,quantum);
989 if (~((size_t) i) < (quantum+1))
991 blob=(unsigned char *) RelinquishMagickMemory(blob);
994 blob=(unsigned char *) ResizeQuantumMemory(blob,i+quantum+1,
996 if ((size_t) (i+count) >= extent)
999 if (LocaleCompare(filename,"-") != 0)
1001 if (blob == (unsigned char *) NULL)
1003 (void) ThrowMagickException(exception,GetMagickModule(),
1004 ResourceLimitError,"MemoryAllocationFailed","'%s'",filename);
1005 return((unsigned char *) NULL);
1009 blob=(unsigned char *) RelinquishMagickMemory(blob);
1010 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1011 return((unsigned char *) NULL);
1013 *length=(size_t) MagickMin(i+count,extent);
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,
1022 if (blob == (unsigned char *) NULL)
1025 (void) ThrowMagickException(exception,GetMagickModule(),
1026 ResourceLimitError,"MemoryAllocationFailed","'%s'",filename);
1027 return((unsigned char *) NULL);
1029 map=MapBlob(file,ReadMode,0,*length);
1030 if (map != (unsigned char *) NULL)
1032 (void) memcpy(blob,map,*length);
1033 (void) UnmapBlob(map,*length);
1037 (void) lseek(file,0,SEEK_SET);
1038 for (i=0; i < *length; i+=count)
1040 count=(ssize_t) read(file,blob+i,(size_t) MagickMin(*length-i,
1041 (MagickSizeType) SSIZE_MAX));
1052 blob=(unsigned char *) RelinquishMagickMemory(blob);
1053 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1054 return((unsigned char *) NULL);
1058 if (LocaleCompare(filename,"-") != 0)
1062 blob=(unsigned char *) RelinquishMagickMemory(blob);
1063 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1073 % F i l e T o I m a g e %
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079 % FileToImage() write the contents of a file to an image.
1081 % The format of the FileToImage method is:
1083 % MagickBooleanType FileToImage(Image *,const char *filename)
1085 % A description of each parameter follows:
1087 % o image: the image.
1089 % o filename: the filename.
1093 static inline ssize_t WriteBlobStream(Image *image,const size_t length,
1094 const unsigned char *data)
1099 register unsigned char
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)
1110 image->blob->quantum<<=1;
1111 extent=image->blob->extent+image->blob->quantum+length;
1112 if (SetBlobExtent(image,extent) == MagickFalse)
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);
1123 MagickExport MagickBooleanType FileToImage(Image *image,const char *filename,
1124 ExceptionInfo *exception)
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=open_utf8(filename,O_RDONLY | O_BINARY,0);
1149 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
1150 return(MagickFalse);
1152 quantum=(size_t) MagickMaxBufferExtent;
1153 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1154 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1155 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1156 if (blob == (unsigned char *) NULL)
1158 ThrowFileException(exception,ResourceLimitError,"MemoryAllocationFailed",
1160 return(MagickFalse);
1164 count=(ssize_t) read(file,blob,quantum);
1171 length=(size_t) count;
1172 count=WriteBlobStream(image,length,blob);
1173 if (count != (ssize_t) length)
1175 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1181 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1182 blob=(unsigned char *) RelinquishMagickMemory(blob);
1187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1191 + G e t B l o b E r r o r %
1195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1197 % GetBlobError() returns MagickTrue if the blob associated with the specified
1198 % image encountered an error.
1200 % The format of the GetBlobError method is:
1202 % MagickBooleanType GetBlobError(const Image *image)
1204 % A description of each parameter follows:
1206 % o image: the image.
1209 MagickPrivate MagickBooleanType GetBlobError(const Image *image)
1211 assert(image != (const Image *) NULL);
1212 assert(image->signature == MagickSignature);
1213 if (image->debug != MagickFalse)
1214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1215 return(image->blob->status);
1219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1223 + G e t B l o b F i l e H a n d l e %
1227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1229 % GetBlobFileHandle() returns the file handle associated with the image blob.
1231 % The format of the GetBlobFile method is:
1233 % FILE *GetBlobFileHandle(const Image *image)
1235 % A description of each parameter follows:
1237 % o image: the image.
1240 MagickExport FILE *GetBlobFileHandle(const Image *image)
1242 assert(image != (const Image *) NULL);
1243 assert(image->signature == MagickSignature);
1244 return(image->blob->file_info.file);
1248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1252 + G e t B l o b I n f o %
1256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1258 % GetBlobInfo() initializes the BlobInfo structure.
1260 % The format of the GetBlobInfo method is:
1262 % void GetBlobInfo(BlobInfo *blob_info)
1264 % A description of each parameter follows:
1266 % o blob_info: Specifies a pointer to a BlobInfo structure.
1269 MagickPrivate void GetBlobInfo(BlobInfo *blob_info)
1271 assert(blob_info != (BlobInfo *) NULL);
1272 (void) ResetMagickMemory(blob_info,0,sizeof(*blob_info));
1273 blob_info->type=UndefinedStream;
1274 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1275 blob_info->properties.st_mtime=time((time_t *) NULL);
1276 blob_info->properties.st_ctime=time((time_t *) NULL);
1277 blob_info->debug=IsEventLogging();
1278 blob_info->reference_count=1;
1279 blob_info->semaphore=AllocateSemaphoreInfo();
1280 blob_info->signature=MagickSignature;
1284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1288 % G e t B l o b P r o p e r t i e s %
1292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1294 % GetBlobProperties() returns information about an image blob.
1296 % The format of the GetBlobProperties method is:
1298 % const struct stat *GetBlobProperties(const Image *image)
1300 % A description of each parameter follows:
1302 % o image: the image.
1305 MagickPrivate const struct stat *GetBlobProperties(const Image *image)
1307 assert(image != (Image *) NULL);
1308 assert(image->signature == MagickSignature);
1309 if (image->debug != MagickFalse)
1310 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1311 return(&image->blob->properties);
1315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1319 + G e t B l o b S i z e %
1323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1325 % GetBlobSize() returns the current length of the image file or blob; zero is
1326 % returned if the size cannot be determined.
1328 % The format of the GetBlobSize method is:
1330 % MagickSizeType GetBlobSize(const Image *image)
1332 % A description of each parameter follows:
1334 % o image: the image.
1337 MagickExport MagickSizeType GetBlobSize(const Image *image)
1342 assert(image != (Image *) NULL);
1343 assert(image->signature == MagickSignature);
1344 if (image->debug != MagickFalse)
1345 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1346 assert(image->blob != (BlobInfo *) NULL);
1348 switch (image->blob->type)
1350 case UndefinedStream:
1352 extent=image->blob->size;
1355 case StandardStream:
1357 extent=image->blob->size;
1362 if (fstat(fileno(image->blob->file_info.file),&image->blob->properties) == 0)
1363 extent=(MagickSizeType) image->blob->properties.st_size;
1368 extent=image->blob->size;
1377 status=GetPathAttributes(image->filename,&image->blob->properties);
1378 if (status != MagickFalse)
1379 extent=(MagickSizeType) image->blob->properties.st_size;
1386 extent=(MagickSizeType) image->blob->length;
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1398 + G e t B l o b S t r e a m D a t a %
1402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % GetBlobStreamData() returns the stream data for the image.
1406 % The format of the GetBlobStreamData method is:
1408 % unsigned char *GetBlobStreamData(const Image *image)
1410 % A description of each parameter follows:
1412 % o image: the image.
1415 MagickExport unsigned char *GetBlobStreamData(const Image *image)
1417 assert(image != (const Image *) NULL);
1418 assert(image->signature == MagickSignature);
1419 return(image->blob->data);
1423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1427 + G e t B l o b S t r e a m H a n d l e r %
1431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1433 % GetBlobStreamHandler() returns the stream handler for the image.
1435 % The format of the GetBlobStreamHandler method is:
1437 % StreamHandler GetBlobStreamHandler(const Image *image)
1439 % A description of each parameter follows:
1441 % o image: the image.
1444 MagickPrivate StreamHandler GetBlobStreamHandler(const Image *image)
1446 assert(image != (const Image *) NULL);
1447 assert(image->signature == MagickSignature);
1448 if (image->debug != MagickFalse)
1449 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1450 return(image->blob->stream);
1454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 % I m a g e T o B l o b %
1462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464 % ImageToBlob() implements direct to memory image formats. It returns the
1465 % image as a formatted blob and its length. The magick member of the Image
1466 % structure determines the format of the returned blob (GIF, JPEG, PNG,
1467 % etc.). This method is the equivalent of WriteImage(), but writes the
1468 % formatted "file" to a memory buffer rather than to an actual file.
1470 % The format of the ImageToBlob method is:
1472 % unsigned char *ImageToBlob(const ImageInfo *image_info,Image *image,
1473 % size_t *length,ExceptionInfo *exception)
1475 % A description of each parameter follows:
1477 % o image_info: the image info.
1479 % o image: the image.
1481 % o length: This pointer to a size_t integer sets the initial length of the
1482 % blob. On return, it reflects the actual length of the blob.
1484 % o exception: return any errors or warnings in this structure.
1487 MagickExport unsigned char *ImageToBlob(const ImageInfo *image_info,
1488 Image *image,size_t *length,ExceptionInfo *exception)
1502 assert(image_info != (const ImageInfo *) NULL);
1503 assert(image_info->signature == MagickSignature);
1504 if (image_info->debug != MagickFalse)
1505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1506 image_info->filename);
1507 assert(image != (Image *) NULL);
1508 assert(image->signature == MagickSignature);
1509 assert(exception != (ExceptionInfo *) NULL);
1511 blob=(unsigned char *) NULL;
1512 blob_info=CloneImageInfo(image_info);
1513 blob_info->adjoin=MagickFalse;
1514 (void) SetImageInfo(blob_info,1,exception);
1515 if (*blob_info->magick != '\0')
1516 (void) CopyMagickString(image->magick,blob_info->magick,MaxTextExtent);
1517 magick_info=GetMagickInfo(image->magick,exception);
1518 if (magick_info == (const MagickInfo *) NULL)
1520 (void) ThrowMagickException(exception,GetMagickModule(),
1521 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
1525 (void) CopyMagickString(blob_info->magick,image->magick,MaxTextExtent);
1526 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1529 Native blob support for this image format.
1531 blob_info->length=0;
1532 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1533 sizeof(unsigned char));
1534 if (blob_info->blob == (void *) NULL)
1535 (void) ThrowMagickException(exception,GetMagickModule(),
1536 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
1539 (void) CloseBlob(image);
1540 image->blob->exempt=MagickTrue;
1541 *image->filename='\0';
1542 status=WriteImage(blob_info,image,exception);
1543 *length=image->blob->length;
1544 blob=DetachBlob(image->blob);
1545 if (status == MagickFalse)
1546 blob=(unsigned char *) RelinquishMagickMemory(blob);
1548 blob=(unsigned char *) ResizeQuantumMemory(blob,*length+1,
1555 unique[MaxTextExtent];
1561 Write file to disk in blob image format.
1563 file=AcquireUniqueFileResource(unique);
1566 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
1567 image_info->filename);
1571 blob_info->file=fdopen(file,"wb");
1572 if (blob_info->file != (FILE *) NULL)
1574 (void) FormatLocaleString(image->filename,MaxTextExtent,"%s:%s",
1575 image->magick,unique);
1576 status=WriteImage(blob_info,image,exception);
1577 (void) fclose(blob_info->file);
1578 if (status != MagickFalse)
1579 blob=FileToBlob(image->filename,~0UL,length,exception);
1581 (void) RelinquishUniqueFileResource(unique);
1584 blob_info=DestroyImageInfo(blob_info);
1589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1593 % I m a g e T o F i l e %
1597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1599 % ImageToFile() writes an image to a file. It returns MagickFalse if an error
1600 % occurs otherwise MagickTrue.
1602 % The format of the ImageToFile method is:
1604 % MagickBooleanType ImageToFile(Image *image,char *filename,
1605 % ExceptionInfo *exception)
1607 % A description of each parameter follows:
1609 % o image: the image.
1611 % o filename: Write the image to this file.
1613 % o exception: return any errors or warnings in this structure.
1616 MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
1617 ExceptionInfo *exception)
1622 register const unsigned char
1641 assert(image != (Image *) NULL);
1642 assert(image->signature == MagickSignature);
1643 assert(image->blob != (BlobInfo *) NULL);
1644 assert(image->blob->type != UndefinedStream);
1645 if (image->debug != MagickFalse)
1646 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1647 assert(filename != (const char *) NULL);
1648 if (*filename == '\0')
1649 file=AcquireUniqueFileResource(filename);
1651 if (LocaleCompare(filename,"-") == 0)
1652 file=fileno(stdout);
1654 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1657 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1658 return(MagickFalse);
1660 quantum=(size_t) MagickMaxBufferExtent;
1661 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1662 quantum=(size_t) MagickMin((MagickSizeType) file_stats.st_size,
1663 MagickMaxBufferExtent);
1664 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1665 if (buffer == (unsigned char *) NULL)
1668 (void) ThrowMagickException(exception,GetMagickModule(),
1669 ResourceLimitError,"MemoryAllocationError","'%s'",filename);
1670 return(MagickFalse);
1673 p=ReadBlobStream(image,quantum,buffer,&count);
1674 for (i=0; count > 0; p=ReadBlobStream(image,quantum,buffer,&count))
1676 length=(size_t) count;
1677 for (i=0; i < length; i+=count)
1679 count=write(file,p+i,(size_t) (length-i));
1690 if (LocaleCompare(filename,"-") != 0)
1692 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1693 if ((file == -1) || (i < length))
1695 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1696 return(MagickFalse);
1702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1706 % I m a g e s T o B l o b %
1710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1712 % ImagesToBlob() implements direct to memory image formats. It returns the
1713 % image sequence as a blob and its length. The magick member of the ImageInfo
1714 % structure determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1716 % Note, some image formats do not permit multiple images to the same image
1717 % stream (e.g. JPEG). in this instance, just the first image of the
1718 % sequence is returned as a blob.
1720 % The format of the ImagesToBlob method is:
1722 % unsigned char *ImagesToBlob(const ImageInfo *image_info,Image *images,
1723 % size_t *length,ExceptionInfo *exception)
1725 % A description of each parameter follows:
1727 % o image_info: the image info.
1729 % o images: the image list.
1731 % o length: This pointer to a size_t integer sets the initial length of the
1732 % blob. On return, it reflects the actual length of the blob.
1734 % o exception: return any errors or warnings in this structure.
1737 MagickExport unsigned char *ImagesToBlob(const ImageInfo *image_info,
1738 Image *images,size_t *length,ExceptionInfo *exception)
1752 assert(image_info != (const ImageInfo *) NULL);
1753 assert(image_info->signature == MagickSignature);
1754 if (image_info->debug != MagickFalse)
1755 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1756 image_info->filename);
1757 assert(images != (Image *) NULL);
1758 assert(images->signature == MagickSignature);
1759 assert(exception != (ExceptionInfo *) NULL);
1761 blob=(unsigned char *) NULL;
1762 blob_info=CloneImageInfo(image_info);
1763 (void) SetImageInfo(blob_info,(unsigned int) GetImageListLength(images),
1765 if (*blob_info->magick != '\0')
1766 (void) CopyMagickString(images->magick,blob_info->magick,MaxTextExtent);
1767 if (blob_info->adjoin == MagickFalse)
1769 blob_info=DestroyImageInfo(blob_info);
1770 return(ImageToBlob(image_info,images,length,exception));
1772 magick_info=GetMagickInfo(images->magick,exception);
1773 if (magick_info == (const MagickInfo *) NULL)
1775 (void) ThrowMagickException(exception,GetMagickModule(),
1776 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
1780 (void) CopyMagickString(blob_info->magick,images->magick,MaxTextExtent);
1781 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1784 Native blob support for this images format.
1786 blob_info->length=0;
1787 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1788 sizeof(unsigned char));
1789 if (blob_info->blob == (void *) NULL)
1790 (void) ThrowMagickException(exception,GetMagickModule(),
1791 ResourceLimitError,"MemoryAllocationFailed","'%s'",images->filename);
1794 images->blob->exempt=MagickTrue;
1795 *images->filename='\0';
1796 status=WriteImages(blob_info,images,images->filename,exception);
1797 if ((status != MagickFalse) && (images->blob->length != 0))
1799 *length=images->blob->length;
1800 blob=DetachBlob(images->blob);
1801 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1809 filename[MaxTextExtent],
1810 unique[MaxTextExtent];
1816 Write file to disk in blob images format.
1818 file=AcquireUniqueFileResource(unique);
1821 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
1822 image_info->filename);
1826 blob_info->file=fdopen(file,"wb");
1827 if (blob_info->file != (FILE *) NULL)
1829 (void) FormatLocaleString(filename,MaxTextExtent,"%s:%s",
1830 images->magick,unique);
1831 status=WriteImages(blob_info,images,filename,exception);
1832 (void) fclose(blob_info->file);
1833 if (status != MagickFalse)
1834 blob=FileToBlob(images->filename,~0UL,length,exception);
1836 (void) RelinquishUniqueFileResource(unique);
1839 blob_info=DestroyImageInfo(blob_info);
1843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1847 % I n j e c t I m a g e B l o b %
1851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1853 % InjectImageBlob() injects the image with a copy of itself in the specified
1854 % format (e.g. inject JPEG into a PDF image).
1856 % The format of the InjectImageBlob method is:
1858 % MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1859 % Image *image,Image *inject_image,const char *format,
1860 % ExceptionInfo *exception)
1862 % A description of each parameter follows:
1864 % o image_info: the image info..
1866 % o image: the image.
1868 % o inject_image: inject into the image stream.
1870 % o format: the image format.
1872 % o exception: return any errors or warnings in this structure.
1875 MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1876 Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
1879 filename[MaxTextExtent];
1912 Write inject image to a temporary file.
1914 assert(image_info != (ImageInfo *) NULL);
1915 assert(image_info->signature == MagickSignature);
1916 assert(image != (Image *) NULL);
1917 assert(image->signature == MagickSignature);
1918 if (image->debug != MagickFalse)
1919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1920 assert(inject_image != (Image *) NULL);
1921 assert(inject_image->signature == MagickSignature);
1922 assert(exception != (ExceptionInfo *) NULL);
1923 unique_file=(FILE *) NULL;
1924 file=AcquireUniqueFileResource(filename);
1926 unique_file=fdopen(file,"wb");
1927 if ((file == -1) || (unique_file == (FILE *) NULL))
1929 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1930 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1932 return(MagickFalse);
1934 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
1935 if (byte_image == (Image *) NULL)
1937 (void) fclose(unique_file);
1938 (void) RelinquishUniqueFileResource(filename);
1939 return(MagickFalse);
1941 (void) FormatLocaleString(byte_image->filename,MaxTextExtent,"%s:%s",format,
1943 DestroyBlob(byte_image);
1944 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
1945 write_info=CloneImageInfo(image_info);
1946 SetImageInfoFile(write_info,unique_file);
1947 status=WriteImage(write_info,byte_image,exception);
1948 write_info=DestroyImageInfo(write_info);
1949 byte_image=DestroyImage(byte_image);
1950 (void) fclose(unique_file);
1951 if (status == MagickFalse)
1953 (void) RelinquishUniqueFileResource(filename);
1954 return(MagickFalse);
1957 Inject into image stream.
1959 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1962 (void) RelinquishUniqueFileResource(filename);
1963 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1964 image_info->filename);
1965 return(MagickFalse);
1967 quantum=(size_t) MagickMaxBufferExtent;
1968 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1969 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1970 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1971 if (buffer == (unsigned char *) NULL)
1973 (void) RelinquishUniqueFileResource(filename);
1974 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1977 for (i=0; ; i+=count)
1979 count=(ssize_t) read(file,buffer,quantum);
1986 status=WriteBlobStream(image,(size_t) count,buffer) == count ? MagickTrue :
1991 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",filename);
1992 (void) RelinquishUniqueFileResource(filename);
1993 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2002 + I s B l o b E x e m p t %
2006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2008 % IsBlobExempt() returns true if the blob is exempt.
2010 % The format of the IsBlobExempt method is:
2012 % MagickBooleanType IsBlobExempt(const Image *image)
2014 % A description of each parameter follows:
2016 % o image: the image.
2019 MagickPrivate MagickBooleanType IsBlobExempt(const Image *image)
2021 assert(image != (const Image *) NULL);
2022 assert(image->signature == MagickSignature);
2023 if (image->debug != MagickFalse)
2024 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2025 return(image->blob->exempt);
2029 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2033 + I s B l o b S e e k a b l e %
2037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2039 % IsBlobSeekable() returns true if the blob is seekable.
2041 % The format of the IsBlobSeekable method is:
2043 % MagickBooleanType IsBlobSeekable(const Image *image)
2045 % A description of each parameter follows:
2047 % o image: the image.
2050 MagickPrivate MagickBooleanType IsBlobSeekable(const Image *image)
2055 assert(image != (const Image *) NULL);
2056 assert(image->signature == MagickSignature);
2057 if (image->debug != MagickFalse)
2058 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2059 switch (image->blob->type)
2065 seekable=MagickTrue;
2070 seekable=MagickFalse;
2078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2082 + I s B l o b T e m p o r a r y %
2086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2088 % IsBlobTemporary() returns true if the blob is temporary.
2090 % The format of the IsBlobTemporary method is:
2092 % MagickBooleanType IsBlobTemporary(const Image *image)
2094 % A description of each parameter follows:
2096 % o image: the image.
2099 MagickPrivate MagickBooleanType IsBlobTemporary(const Image *image)
2101 assert(image != (const Image *) NULL);
2102 assert(image->signature == MagickSignature);
2103 if (image->debug != MagickFalse)
2104 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2105 return(image->blob->temporary);
2109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2119 % MapBlob() creates a mapping from a file to a binary large object.
2121 % The format of the MapBlob method is:
2123 % unsigned char *MapBlob(int file,const MapMode mode,
2124 % const MagickOffsetType offset,const size_t length)
2126 % A description of each parameter follows:
2128 % o file: map this file descriptor.
2130 % o mode: ReadMode, WriteMode, or IOMode.
2132 % o offset: starting at this offset within the file.
2134 % o length: the length of the mapping is returned in this pointer.
2137 MagickExport unsigned char *MapBlob(int file,const MapMode mode,
2138 const MagickOffsetType offset,const size_t length)
2140 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
2153 #if defined(MAP_ANONYMOUS)
2154 flags|=MAP_ANONYMOUS;
2156 return((unsigned char *) NULL);
2163 protection=PROT_READ;
2165 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2171 protection=PROT_WRITE;
2173 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2175 #if defined(MAGICKCORE_HAVE_POSIX_MADVISE)
2176 (void) posix_madvise(map,length,POSIX_MADV_SEQUENTIAL |
2177 POSIX_MADV_WILLNEED);
2183 protection=PROT_READ | PROT_WRITE;
2185 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2190 if (map == (unsigned char *) MAP_FAILED)
2191 return((unsigned char *) NULL);
2198 return((unsigned char *) NULL);
2203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2207 + M S B O r d e r L o n g %
2211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2213 % MSBOrderLong() converts a least-significant byte first buffer of integers to
2214 % most-significant byte first.
2216 % The format of the MSBOrderLong method is:
2218 % void MSBOrderLong(unsigned char *buffer,const size_t length)
2220 % A description of each parameter follows.
2222 % o buffer: Specifies a pointer to a buffer of integers.
2224 % o length: Specifies the length of the buffer.
2227 MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
2232 register unsigned char
2236 assert(buffer != (unsigned char *) NULL);
2243 *buffer++=(unsigned char) c;
2247 *buffer++=(unsigned char) c;
2253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2257 + M S B O r d e r S h o r t %
2261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2263 % MSBOrderShort() converts a least-significant byte first buffer of integers
2264 % to most-significant byte first.
2266 % The format of the MSBOrderShort method is:
2268 % void MSBOrderShort(unsigned char *p,const size_t length)
2270 % A description of each parameter follows.
2272 % o p: Specifies a pointer to a buffer of integers.
2274 % o length: Specifies the length of the buffer.
2277 MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
2282 register unsigned char
2285 assert(p != (unsigned char *) NULL);
2292 *p++=(unsigned char) c;
2297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2307 % OpenBlob() opens a file associated with the image. A file name of '-' sets
2308 % the file to stdin for type 'r' and stdout for type 'w'. If the filename
2309 % suffix is '.gz' or '.Z', the image is decompressed for type 'r' and
2310 % compressed for type 'w'. If the filename prefix is '|', it is piped to or
2311 % from a system command.
2313 % The format of the OpenBlob method is:
2315 % MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
2316 % const BlobMode mode,ExceptionInfo *exception)
2318 % A description of each parameter follows:
2320 % o image_info: the image info.
2322 % o image: the image.
2324 % o mode: the mode for opening the file.
2327 MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
2328 Image *image,const BlobMode mode,ExceptionInfo *exception)
2331 extension[MaxTextExtent],
2332 filename[MaxTextExtent];
2343 assert(image_info != (ImageInfo *) NULL);
2344 assert(image_info->signature == MagickSignature);
2345 if (image_info->debug != MagickFalse)
2346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2347 image_info->filename);
2348 assert(image != (Image *) NULL);
2349 assert(image->signature == MagickSignature);
2350 if (image_info->blob != (void *) NULL)
2352 if (image_info->stream != (StreamHandler) NULL)
2353 image->blob->stream=(StreamHandler) image_info->stream;
2354 AttachBlob(image->blob,image_info->blob,image_info->length);
2357 (void) DetachBlob(image->blob);
2360 default: type="r"; break;
2361 case ReadBlobMode: type="r"; break;
2362 case ReadBinaryBlobMode: type="rb"; break;
2363 case WriteBlobMode: type="w"; break;
2364 case WriteBinaryBlobMode: type="w+b"; break;
2365 case AppendBlobMode: type="a"; break;
2366 case AppendBinaryBlobMode: type="a+b"; break;
2369 image->blob->synchronize=image_info->synchronize;
2370 if (image_info->stream != (StreamHandler) NULL)
2372 image->blob->stream=(StreamHandler) image_info->stream;
2375 image->blob->type=FifoStream;
2383 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2384 rights=ReadPolicyRights;
2386 rights=WritePolicyRights;
2387 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2390 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2391 "NotAuthorized","'%s'",filename);
2392 return(MagickFalse);
2394 if ((LocaleCompare(filename,"-") == 0) ||
2395 ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
2397 image->blob->file_info.file=(*type == 'r') ? stdin : stdout;
2398 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2399 if (strchr(type,'b') != (char *) NULL)
2400 setmode(_fileno(image->blob->file_info.file),_O_BINARY);
2402 image->blob->type=StandardStream;
2403 image->blob->exempt=MagickTrue;
2406 if (LocaleNCompare(filename,"fd:",3) == 0)
2409 mode[MaxTextExtent];
2413 image->blob->file_info.file=fdopen(StringToLong(filename+3),mode);
2414 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2415 if (strchr(type,'b') != (char *) NULL)
2416 setmode(_fileno(image->blob->file_info.file),_O_BINARY);
2418 image->blob->type=StandardStream;
2419 image->blob->exempt=MagickTrue;
2422 #if defined(MAGICKCORE_HAVE_POPEN)
2423 if (*filename == '|')
2426 mode[MaxTextExtent];
2429 Pipe image to or from a system command.
2431 #if defined(SIGPIPE)
2433 (void) signal(SIGPIPE,SIG_IGN);
2437 image->blob->file_info.file=(FILE *) popen_utf8(filename+1,mode);
2438 if (image->blob->file_info.file == (FILE *) NULL)
2440 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2441 return(MagickFalse);
2443 image->blob->type=PipeStream;
2444 image->blob->exempt=MagickTrue;
2448 status=GetPathAttributes(filename,&image->blob->properties);
2449 #if defined(S_ISFIFO)
2450 if ((status == MagickTrue) && S_ISFIFO(image->blob->properties.st_mode))
2452 image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2453 if (image->blob->file_info.file == (FILE *) NULL)
2455 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2456 return(MagickFalse);
2458 image->blob->type=FileStream;
2459 image->blob->exempt=MagickTrue;
2463 GetPathComponent(image->filename,ExtensionPath,extension);
2466 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2467 if ((image_info->adjoin == MagickFalse) ||
2468 (strchr(filename,'%') != (char *) NULL))
2471 Form filename for multi-part images.
2473 (void) InterpretImageFilename(image_info,image,image->filename,(int)
2474 image->scene,filename,exception);
2475 if ((LocaleCompare(filename,image->filename) == 0) &&
2476 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2477 (GetNextImageInList(image) != (Image *) NULL)))
2480 path[MaxTextExtent];
2482 GetPathComponent(image->filename,RootPath,path);
2483 if (*extension == '\0')
2484 (void) FormatLocaleString(filename,MaxTextExtent,"%s-%.20g",
2485 path,(double) image->scene);
2487 (void) FormatLocaleString(filename,MaxTextExtent,"%s-%.20g.%s",
2488 path,(double) image->scene,extension);
2490 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
2491 #if defined(macintosh)
2492 SetApplicationType(filename,image_info->magick,'8BIM');
2496 if (image_info->file != (FILE *) NULL)
2498 image->blob->file_info.file=image_info->file;
2499 image->blob->type=FileStream;
2500 image->blob->exempt=MagickTrue;
2505 image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2506 if (image->blob->file_info.file != (FILE *) NULL)
2514 image->blob->type=FileStream;
2515 #if defined(MAGICKCORE_HAVE_SETVBUF)
2516 (void) setvbuf(image->blob->file_info.file,(char *) NULL,
2517 (int) _IOFBF,16384);
2519 (void) ResetMagickMemory(magick,0,sizeof(magick));
2520 count=fread(magick,1,sizeof(magick),image->blob->file_info.file);
2521 (void) rewind(image->blob->file_info.file);
2522 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2523 " read %.20g magic header bytes",(double) count);
2524 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2525 if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
2526 ((int) magick[2] == 0x08))
2528 (void) fclose(image->blob->file_info.file);
2529 image->blob->file_info.gzfile=gzopen(filename,type);
2530 if (image->blob->file_info.gzfile != (gzFile) NULL)
2531 image->blob->type=ZipStream;
2534 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2535 if (strncmp((char *) magick,"BZh",3) == 0)
2537 (void) fclose(image->blob->file_info.file);
2538 image->blob->file_info.bzfile=BZ2_bzopen(filename,type);
2539 if (image->blob->file_info.bzfile != (BZFILE *) NULL)
2540 image->blob->type=BZipStream;
2543 if (image->blob->type == FileStream)
2554 sans_exception=AcquireExceptionInfo();
2555 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2556 sans_exception=DestroyExceptionInfo(sans_exception);
2557 properties=(&image->blob->properties);
2558 if ((magick_info != (const MagickInfo *) NULL) &&
2559 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2560 (properties->st_size <= MagickMaxBufferExtent))
2568 length=(size_t) properties->st_size;
2569 blob=MapBlob(fileno(image->blob->file_info.file),ReadMode,
2571 if (blob != (void *) NULL)
2574 Format supports blobs-- use memory-mapped I/O.
2576 if (image_info->file != (FILE *) NULL)
2577 image->blob->exempt=MagickFalse;
2580 (void) fclose(image->blob->file_info.file);
2581 image->blob->file_info.file=(FILE *) NULL;
2583 AttachBlob(image->blob,blob,length);
2584 image->blob->mapped=MagickTrue;
2591 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2592 if ((LocaleCompare(extension,"Z") == 0) ||
2593 (LocaleCompare(extension,"gz") == 0) ||
2594 (LocaleCompare(extension,"wmz") == 0) ||
2595 (LocaleCompare(extension,"svgz") == 0))
2597 if (mode == WriteBinaryBlobMode)
2599 image->blob->file_info.gzfile=gzopen(filename,type);
2600 if (image->blob->file_info.gzfile != (gzFile) NULL)
2601 image->blob->type=ZipStream;
2605 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2606 if (LocaleCompare(extension,"bz2") == 0)
2608 image->blob->file_info.bzfile=BZ2_bzopen(filename,type);
2609 if (image->blob->file_info.bzfile != (BZFILE *) NULL)
2610 image->blob->type=BZipStream;
2615 image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2616 if (image->blob->file_info.file != (FILE *) NULL)
2618 image->blob->type=FileStream;
2619 #if defined(MAGICKCORE_HAVE_SETVBUF)
2620 (void) setvbuf(image->blob->file_info.file,(char *) NULL,(int) _IOFBF,
2625 image->blob->status=MagickFalse;
2626 if (image->blob->type != UndefinedStream)
2627 image->blob->size=GetBlobSize(image);
2630 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2631 return(MagickFalse);
2637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2647 % PingBlob() returns all the attributes of an image or image sequence except
2648 % for the pixels. It is much faster and consumes far less memory than
2649 % BlobToImage(). On failure, a NULL image is returned and exception
2650 % describes the reason for the failure.
2652 % The format of the PingBlob method is:
2654 % Image *PingBlob(const ImageInfo *image_info,const void *blob,
2655 % const size_t length,ExceptionInfo *exception)
2657 % A description of each parameter follows:
2659 % o image_info: the image info.
2661 % o blob: the address of a character stream in one of the image formats
2662 % understood by ImageMagick.
2664 % o length: This size_t integer reflects the length in bytes of the blob.
2666 % o exception: return any errors or warnings in this structure.
2670 #if defined(__cplusplus) || defined(c_plusplus)
2674 static size_t PingStream(const Image *magick_unused(image),
2675 const void *magick_unused(pixels),const size_t columns)
2680 #if defined(__cplusplus) || defined(c_plusplus)
2684 MagickExport Image *PingBlob(const ImageInfo *image_info,const void *blob,
2685 const size_t length,ExceptionInfo *exception)
2693 assert(image_info != (ImageInfo *) NULL);
2694 assert(image_info->signature == MagickSignature);
2695 if (image_info->debug != MagickFalse)
2696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2697 image_info->filename);
2698 assert(exception != (ExceptionInfo *) NULL);
2699 if ((blob == (const void *) NULL) || (length == 0))
2701 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
2702 "UnrecognizedImageFormat","'%s'",image_info->magick);
2703 return((Image *) NULL);
2705 ping_info=CloneImageInfo(image_info);
2706 ping_info->blob=(void *) AcquireQuantumMemory(length,sizeof(unsigned char));
2707 if (ping_info->blob == (const void *) NULL)
2709 (void) ThrowMagickException(exception,GetMagickModule(),
2710 ResourceLimitFatalError,"MemoryAllocationFailed","'%s'","");
2711 return((Image *) NULL);
2713 (void) memcpy(ping_info->blob,blob,length);
2714 ping_info->length=length;
2715 ping_info->ping=MagickTrue;
2716 image=ReadStream(ping_info,&PingStream,exception);
2717 ping_info->blob=(void *) RelinquishMagickMemory(ping_info->blob);
2718 ping_info=DestroyImageInfo(ping_info);
2723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2733 % ReadBlob() reads data from the blob or image file and returns it. It
2734 % returns the number of bytes read.
2736 % The format of the ReadBlob method is:
2738 % ssize_t ReadBlob(Image *image,const size_t length,unsigned char *data)
2740 % A description of each parameter follows:
2742 % o image: the image.
2744 % o length: Specifies an integer representing the number of bytes to read
2747 % o data: Specifies an area to place the information requested from the
2751 MagickExport ssize_t ReadBlob(Image *image,const size_t length,
2752 unsigned char *data)
2757 register unsigned char
2763 assert(image != (Image *) NULL);
2764 assert(image->signature == MagickSignature);
2765 assert(image->blob != (BlobInfo *) NULL);
2766 assert(image->blob->type != UndefinedStream);
2769 assert(data != (void *) NULL);
2772 switch (image->blob->type)
2774 case UndefinedStream:
2776 case StandardStream:
2778 count=(ssize_t) read(fileno(image->blob->file_info.file),q,length);
2788 count=(ssize_t) fread(q,1,length,image->blob->file_info.file);
2793 c=getc(image->blob->file_info.file);
2796 *q++=(unsigned char) c;
2801 c=getc(image->blob->file_info.file);
2804 *q++=(unsigned char) c;
2814 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2819 count=(ssize_t) gzread(image->blob->file_info.gzfile,q,
2820 (unsigned int) length);
2825 c=gzgetc(image->blob->file_info.gzfile);
2828 *q++=(unsigned char) c;
2833 c=gzgetc(image->blob->file_info.gzfile);
2836 *q++=(unsigned char) c;
2847 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2848 count=(ssize_t) BZ2_bzread(image->blob->file_info.bzfile,q,
2857 register const unsigned char
2860 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
2862 image->blob->eof=MagickTrue;
2865 p=image->blob->data+image->blob->offset;
2866 count=(ssize_t) MagickMin(length,(MagickSizeType) (image->blob->length-
2867 image->blob->offset));
2868 image->blob->offset+=count;
2869 if (count != (ssize_t) length)
2870 image->blob->eof=MagickTrue;
2871 (void) memcpy(q,p,(size_t) count);
2879 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2883 + R e a d B l o b B y t e %
2887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2889 % ReadBlobByte() reads a single byte from the image file and returns it.
2891 % The format of the ReadBlobByte method is:
2893 % int ReadBlobByte(Image *image)
2895 % A description of each parameter follows.
2897 % o image: the image.
2900 MagickExport int ReadBlobByte(Image *image)
2902 register const unsigned char
2911 assert(image != (Image *) NULL);
2912 assert(image->signature == MagickSignature);
2913 p=ReadBlobStream(image,1,buffer,&count);
2920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2924 + R e a d B l o b D o u b l e %
2928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2930 % ReadBlobDouble() reads a double value as a 64-bit quantity in the byte-order
2931 % specified by the endian member of the image structure.
2933 % The format of the ReadBlobDouble method is:
2935 % double ReadBlobDouble(Image *image)
2937 % A description of each parameter follows.
2939 % o image: the image.
2942 MagickExport double ReadBlobDouble(Image *image)
2953 quantum.double_value=0.0;
2954 quantum.unsigned_value=ReadBlobLongLong(image);
2955 return(quantum.double_value);
2959 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2963 + R e a d B l o b F l o a t %
2967 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2969 % ReadBlobFloat() reads a float value as a 32-bit quantity in the byte-order
2970 % specified by the endian member of the image structure.
2972 % The format of the ReadBlobFloat method is:
2974 % float ReadBlobFloat(Image *image)
2976 % A description of each parameter follows.
2978 % o image: the image.
2981 MagickExport float ReadBlobFloat(Image *image)
2992 quantum.float_value=0.0;
2993 quantum.unsigned_value=ReadBlobLong(image);
2994 return(quantum.float_value);
2998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3002 + R e a d B l o b L o n g %
3006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3008 % ReadBlobLong() reads a ssize_t value as a 32-bit quantity in the byte-order
3009 % specified by the endian member of the image structure.
3011 % The format of the ReadBlobLong method is:
3013 % unsigned int ReadBlobLong(Image *image)
3015 % A description of each parameter follows.
3017 % o image: the image.
3020 MagickExport unsigned int ReadBlobLong(Image *image)
3022 register const unsigned char
3034 assert(image != (Image *) NULL);
3035 assert(image->signature == MagickSignature);
3037 p=ReadBlobStream(image,4,buffer,&count);
3040 if (image->endian == LSBEndian)
3042 value=(unsigned int) (*p++);
3043 value|=((unsigned int) (*p++)) << 8;
3044 value|=((unsigned int) (*p++)) << 16;
3045 value|=((unsigned int) (*p++)) << 24;
3048 value=((unsigned int) (*p++)) << 24;
3049 value|=((unsigned int) (*p++)) << 16;
3050 value|=((unsigned int) (*p++)) << 8;
3051 value|=((unsigned int) (*p++));
3056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3060 + R e a d B l o b L o n g L o n g %
3064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3066 % ReadBlobLongLong() reads a long long value as a 64-bit quantity in the
3067 % byte-order specified by the endian member of the image structure.
3069 % The format of the ReadBlobLongLong method is:
3071 % MagickSizeType ReadBlobLongLong(Image *image)
3073 % A description of each parameter follows.
3075 % o image: the image.
3078 MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3083 register const unsigned char
3092 assert(image != (Image *) NULL);
3093 assert(image->signature == MagickSignature);
3095 p=ReadBlobStream(image,8,buffer,&count);
3097 return(MagickULLConstant(0));
3098 if (image->endian == LSBEndian)
3100 value=(MagickSizeType) (*p++);
3101 value|=((MagickSizeType) (*p++)) << 8;
3102 value|=((MagickSizeType) (*p++)) << 16;
3103 value|=((MagickSizeType) (*p++)) << 24;
3104 value|=((MagickSizeType) (*p++)) << 32;
3105 value|=((MagickSizeType) (*p++)) << 40;
3106 value|=((MagickSizeType) (*p++)) << 48;
3107 value|=((MagickSizeType) (*p++)) << 56;
3108 return(value & MagickULLConstant(0xffffffffffffffff));
3110 value=((MagickSizeType) (*p++)) << 56;
3111 value|=((MagickSizeType) (*p++)) << 48;
3112 value|=((MagickSizeType) (*p++)) << 40;
3113 value|=((MagickSizeType) (*p++)) << 32;
3114 value|=((MagickSizeType) (*p++)) << 24;
3115 value|=((MagickSizeType) (*p++)) << 16;
3116 value|=((MagickSizeType) (*p++)) << 8;
3117 value|=((MagickSizeType) (*p++));
3118 return(value & MagickULLConstant(0xffffffffffffffff));
3122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3126 + R e a d B l o b S h o r t %
3130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3132 % ReadBlobShort() reads a short value as a 16-bit quantity in the byte-order
3133 % specified by the endian member of the image structure.
3135 % The format of the ReadBlobShort method is:
3137 % unsigned short ReadBlobShort(Image *image)
3139 % A description of each parameter follows.
3141 % o image: the image.
3144 MagickExport unsigned short ReadBlobShort(Image *image)
3146 register const unsigned char
3149 register unsigned int
3158 assert(image != (Image *) NULL);
3159 assert(image->signature == MagickSignature);
3161 p=ReadBlobStream(image,2,buffer,&count);
3163 return((unsigned short) 0U);
3164 if (image->endian == LSBEndian)
3166 value=(unsigned int) (*p++);
3167 value|=((unsigned int) (*p++)) << 8;
3168 return((unsigned short) (value & 0xffff));
3170 value=(unsigned int) ((*p++) << 8);
3171 value|=(unsigned int) (*p++);
3172 return((unsigned short) (value & 0xffff));
3176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3180 + R e a d B l o b L S B L o n g %
3184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3186 % ReadBlobLSBLong() reads a ssize_t value as a 32-bit quantity in
3187 % least-significant byte first order.
3189 % The format of the ReadBlobLSBLong method is:
3191 % unsigned int ReadBlobLSBLong(Image *image)
3193 % A description of each parameter follows.
3195 % o image: the image.
3198 MagickExport unsigned int ReadBlobLSBLong(Image *image)
3200 register const unsigned char
3203 register unsigned int
3212 assert(image != (Image *) NULL);
3213 assert(image->signature == MagickSignature);
3215 p=ReadBlobStream(image,4,buffer,&count);
3218 value=(unsigned int) (*p++);
3219 value|=((unsigned int) (*p++)) << 8;
3220 value|=((unsigned int) (*p++)) << 16;
3221 value|=((unsigned int) (*p++)) << 24;
3226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3230 + R e a d B l o b L S B S h o r t %
3234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3236 % ReadBlobLSBShort() reads a short value as a 16-bit quantity in
3237 % least-significant byte first order.
3239 % The format of the ReadBlobLSBShort method is:
3241 % unsigned short ReadBlobLSBShort(Image *image)
3243 % A description of each parameter follows.
3245 % o image: the image.
3248 MagickExport unsigned short ReadBlobLSBShort(Image *image)
3250 register const unsigned char
3253 register unsigned int
3262 assert(image != (Image *) NULL);
3263 assert(image->signature == MagickSignature);
3265 p=ReadBlobStream(image,2,buffer,&count);
3267 return((unsigned short) 0U);
3268 value=(unsigned int) (*p++);
3269 value|=((unsigned int) ((*p++)) << 8);
3270 return((unsigned short) (value & 0xffff));
3274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3278 + R e a d B l o b M S B L o n g %
3282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3284 % ReadBlobMSBLong() reads a ssize_t value as a 32-bit quantity in
3285 % most-significant byte first order.
3287 % The format of the ReadBlobMSBLong method is:
3289 % unsigned int ReadBlobMSBLong(Image *image)
3291 % A description of each parameter follows.
3293 % o image: the image.
3296 MagickExport unsigned int ReadBlobMSBLong(Image *image)
3298 register const unsigned char
3301 register unsigned int
3310 assert(image != (Image *) NULL);
3311 assert(image->signature == MagickSignature);
3313 p=ReadBlobStream(image,4,buffer,&count);
3316 value=((unsigned int) (*p++) << 24);
3317 value|=((unsigned int) (*p++) << 16);
3318 value|=((unsigned int) (*p++) << 8);
3319 value|=(unsigned int) (*p++);
3324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3328 + R e a d B l o b M S B L o n g L o n g %
3332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3334 % ReadBlobMSBLongLong() reads a ssize_t value as a 64-bit quantity in
3335 % most-significant byte first order.
3337 % The format of the ReadBlobMSBLongLong method is:
3339 % unsigned int ReadBlobMSBLongLong(Image *image)
3341 % A description of each parameter follows.
3343 % o image: the image.
3346 MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
3348 register const unsigned char
3351 register MagickSizeType
3360 assert(image != (Image *) NULL);
3361 assert(image->signature == MagickSignature);
3363 p=ReadBlobStream(image,8,buffer,&count);
3365 return(MagickULLConstant(0));
3366 value=((MagickSizeType) (*p++)) << 56;
3367 value|=((MagickSizeType) (*p++)) << 48;
3368 value|=((MagickSizeType) (*p++)) << 40;
3369 value|=((MagickSizeType) (*p++)) << 32;
3370 value|=((MagickSizeType) (*p++)) << 24;
3371 value|=((MagickSizeType) (*p++)) << 16;
3372 value|=((MagickSizeType) (*p++)) << 8;
3373 value|=((MagickSizeType) (*p++));
3374 return(value & MagickULLConstant(0xffffffffffffffff));
3378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3382 + R e a d B l o b M S B S h o r t %
3386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3388 % ReadBlobMSBShort() reads a short value as a 16-bit quantity in
3389 % most-significant byte first order.
3391 % The format of the ReadBlobMSBShort method is:
3393 % unsigned short ReadBlobMSBShort(Image *image)
3395 % A description of each parameter follows.
3397 % o image: the image.
3400 MagickExport unsigned short ReadBlobMSBShort(Image *image)
3402 register const unsigned char
3405 register unsigned int
3414 assert(image != (Image *) NULL);
3415 assert(image->signature == MagickSignature);
3417 p=ReadBlobStream(image,2,buffer,&count);
3419 return((unsigned short) 0U);
3420 value=(unsigned int) ((*p++) << 8);
3421 value|=(unsigned int) (*p++);
3422 return((unsigned short) (value & 0xffff));
3426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3430 + R e a d B l o b S t r i n g %
3434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3436 % ReadBlobString() reads characters from a blob or file until a newline
3437 % character is read or an end-of-file condition is encountered.
3439 % The format of the ReadBlobString method is:
3441 % char *ReadBlobString(Image *image,char *string)
3443 % A description of each parameter follows:
3445 % o image: the image.
3447 % o string: the address of a character buffer.
3450 MagickExport char *ReadBlobString(Image *image,char *string)
3452 register const unsigned char
3464 assert(image != (Image *) NULL);
3465 assert(image->signature == MagickSignature);
3466 for (i=0; i < (MaxTextExtent-1L); i++)
3468 p=ReadBlobStream(image,1,buffer,&count);
3472 return((char *) NULL);
3475 string[i]=(char) (*p);
3476 if ((string[i] == '\r') || (string[i] == '\n'))
3479 if (string[i] == '\r')
3480 (void) ReadBlobStream(image,1,buffer,&count);
3486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3490 + R e f e r e n c e B l o b %
3494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3496 % ReferenceBlob() increments the reference count associated with the pixel
3497 % blob returning a pointer to the blob.
3499 % The format of the ReferenceBlob method is:
3501 % BlobInfo ReferenceBlob(BlobInfo *blob_info)
3503 % A description of each parameter follows:
3505 % o blob_info: the blob_info.
3508 MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
3510 assert(blob != (BlobInfo *) NULL);
3511 assert(blob->signature == MagickSignature);
3512 if (blob->debug != MagickFalse)
3513 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3514 LockSemaphoreInfo(blob->semaphore);
3515 blob->reference_count++;
3516 UnlockSemaphoreInfo(blob->semaphore);
3521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3531 % SeekBlob() sets the offset in bytes from the beginning of a blob or file
3532 % and returns the resulting offset.
3534 % The format of the SeekBlob method is:
3536 % MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
3539 % A description of each parameter follows:
3541 % o image: the image.
3543 % o offset: Specifies an integer representing the offset in bytes.
3545 % o whence: Specifies an integer representing how the offset is
3546 % treated relative to the beginning of the blob as follows:
3548 % SEEK_SET Set position equal to offset bytes.
3549 % SEEK_CUR Set position to current location plus offset.
3550 % SEEK_END Set position to EOF plus offset.
3553 MagickExport MagickOffsetType SeekBlob(Image *image,
3554 const MagickOffsetType offset,const int whence)
3556 assert(image != (Image *) NULL);
3557 assert(image->signature == MagickSignature);
3558 if (image->debug != MagickFalse)
3559 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3560 assert(image->blob != (BlobInfo *) NULL);
3561 assert(image->blob->type != UndefinedStream);
3562 switch (image->blob->type)
3564 case UndefinedStream:
3566 case StandardStream:
3570 if (fseek(image->blob->file_info.file,offset,whence) < 0)
3572 image->blob->offset=TellBlob(image);
3578 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3579 if (gzseek(image->blob->file_info.gzfile,(off_t) offset,whence) < 0)
3582 image->blob->offset=TellBlob(image);
3598 image->blob->offset=offset;
3603 if ((image->blob->offset+offset) < 0)
3605 image->blob->offset+=offset;
3610 if (((MagickOffsetType) image->blob->length+offset) < 0)
3612 image->blob->offset=image->blob->length+offset;
3616 if (image->blob->offset <= (MagickOffsetType)
3617 ((off_t) image->blob->length))
3618 image->blob->eof=MagickFalse;
3620 if (image->blob->mapped != MagickFalse)
3624 image->blob->extent=(size_t) (image->blob->offset+
3625 image->blob->quantum);
3626 image->blob->data=(unsigned char *) ResizeQuantumMemory(
3627 image->blob->data,image->blob->extent+1,
3628 sizeof(*image->blob->data));
3629 (void) SyncBlob(image);
3630 if (image->blob->data == (unsigned char *) NULL)
3632 (void) DetachBlob(image->blob);
3639 return(image->blob->offset);
3643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3647 + S e t B l o b E x e m p t %
3651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3653 % SetBlobExempt() sets the blob exempt status.
3655 % The format of the SetBlobExempt method is:
3657 % MagickBooleanType SetBlobExempt(const Image *image,
3658 % const MagickBooleanType exempt)
3660 % A description of each parameter follows:
3662 % o image: the image.
3664 % o exempt: Set to true if this blob is exempt from being closed.
3667 MagickPrivate void SetBlobExempt(Image *image,const MagickBooleanType exempt)
3669 assert(image != (const Image *) NULL);
3670 assert(image->signature == MagickSignature);
3671 if (image->debug != MagickFalse)
3672 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3673 image->blob->exempt=exempt;
3677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3681 + S e t B l o b E x t e n t %
3685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3687 % SetBlobExtent() ensures enough space is allocated for the blob. If the
3688 % method is successful, subsequent writes to bytes in the specified range are
3689 % guaranteed not to fail.
3691 % The format of the SetBlobExtent method is:
3693 % MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
3695 % A description of each parameter follows:
3697 % o image: the image.
3699 % o extent: the blob maximum extent.
3702 MagickPrivate MagickBooleanType SetBlobExtent(Image *image,
3703 const MagickSizeType extent)
3705 assert(image != (Image *) NULL);
3706 assert(image->signature == MagickSignature);
3707 if (image->debug != MagickFalse)
3708 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3709 assert(image->blob != (BlobInfo *) NULL);
3710 assert(image->blob->type != UndefinedStream);
3711 switch (image->blob->type)
3713 case UndefinedStream:
3715 case StandardStream:
3716 return(MagickFalse);
3719 if (extent != (MagickSizeType) ((off_t) extent))
3720 return(MagickFalse);
3721 #if !defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
3722 return(MagickFalse);
3731 offset=TellBlob(image);
3732 status=posix_fallocate(fileno(image->blob->file_info.file),
3733 (off_t) offset,(off_t) (extent-offset));
3735 return(MagickFalse);
3742 return(MagickFalse);
3744 return(MagickFalse);
3746 return(MagickFalse);
3749 if (image->blob->mapped != MagickFalse)
3751 if (image->blob->file_info.file == (FILE *) NULL)
3752 return(MagickFalse);
3753 (void) UnmapBlob(image->blob->data,image->blob->length);
3754 #if !defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
3755 return(MagickFalse);
3764 offset=TellBlob(image);
3765 status=posix_fallocate(fileno(image->blob->file_info.file),
3766 (off_t) offset,(off_t) (extent-offset));
3768 return(MagickFalse);
3770 image->blob->data=(unsigned char*) MapBlob(fileno(
3771 image->blob->file_info.file),WriteMode,0,(size_t) extent);
3772 image->blob->extent=(size_t) extent;
3773 image->blob->length=(size_t) extent;
3774 (void) SyncBlob(image);
3778 if (extent != (MagickSizeType) ((size_t) extent))
3779 return(MagickFalse);
3780 image->blob->extent=(size_t) extent;
3781 image->blob->data=(unsigned char *) ResizeQuantumMemory(image->blob->data,
3782 image->blob->extent+1,sizeof(*image->blob->data));
3783 (void) SyncBlob(image);
3784 if (image->blob->data == (unsigned char *) NULL)
3786 (void) DetachBlob(image->blob);
3787 return(MagickFalse);
3796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3806 % SyncBlob() flushes the datastream if it is a file or synchronizes the data
3807 % attributes if it is an blob.
3809 % The format of the SyncBlob method is:
3811 % int SyncBlob(Image *image)
3813 % A description of each parameter follows:
3815 % o image: the image.
3818 static int SyncBlob(Image *image)
3823 assert(image != (Image *) NULL);
3824 assert(image->signature == MagickSignature);
3825 if (image->debug != MagickFalse)
3826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3827 assert(image->blob != (BlobInfo *) NULL);
3828 assert(image->blob->type != UndefinedStream);
3830 switch (image->blob->type)
3832 case UndefinedStream:
3833 case StandardStream:
3838 status=fflush(image->blob->file_info.file);
3843 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3844 status=gzflush(image->blob->file_info.gzfile,Z_SYNC_FLUSH);
3850 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3851 status=BZ2_bzflush(image->blob->file_info.bzfile);
3859 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3860 if (image->blob->mapped != MagickFalse)
3861 status=msync(image->blob->data,image->blob->length,MS_SYNC);
3870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3880 % TellBlob() obtains the current value of the blob or file position.
3882 % The format of the TellBlob method is:
3884 % MagickOffsetType TellBlob(const Image *image)
3886 % A description of each parameter follows:
3888 % o image: the image.
3891 MagickExport MagickOffsetType TellBlob(const Image *image)
3896 assert(image != (Image *) NULL);
3897 assert(image->signature == MagickSignature);
3898 if (image->debug != MagickFalse)
3899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3900 assert(image->blob != (BlobInfo *) NULL);
3901 assert(image->blob->type != UndefinedStream);
3903 switch (image->blob->type)
3905 case UndefinedStream:
3906 case StandardStream:
3910 offset=ftell(image->blob->file_info.file);
3917 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3918 offset=(MagickOffsetType) gztell(image->blob->file_info.gzfile);
3928 offset=image->blob->offset;
3936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3940 + U n m a p B l o b %
3944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3946 % UnmapBlob() deallocates the binary large object previously allocated with
3947 % the MapBlob method.
3949 % The format of the UnmapBlob method is:
3951 % MagickBooleanType UnmapBlob(void *map,const size_t length)
3953 % A description of each parameter follows:
3955 % o map: the address of the binary large object.
3957 % o length: the length of the binary large object.
3960 MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
3962 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3966 status=munmap(map,length);
3967 return(status == -1 ? MagickFalse : MagickTrue);
3971 return(MagickFalse);
3976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3980 + W r i t e B l o b %
3984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3986 % WriteBlob() writes data to a blob or image file. It returns the number of
3989 % The format of the WriteBlob method is:
3991 % ssize_t WriteBlob(Image *image,const size_t length,
3992 % const unsigned char *data)
3994 % A description of each parameter follows:
3996 % o image: the image.
3998 % o length: Specifies an integer representing the number of bytes to
3999 % write to the file.
4001 % o data: The address of the data to write to the blob or file.
4004 MagickExport ssize_t WriteBlob(Image *image,const size_t length,
4005 const unsigned char *data)
4010 register const unsigned char
4016 assert(image != (Image *) NULL);
4017 assert(image->signature == MagickSignature);
4018 assert(data != (const unsigned char *) NULL);
4019 assert(image->blob != (BlobInfo *) NULL);
4020 assert(image->blob->type != UndefinedStream);
4025 switch (image->blob->type)
4027 case UndefinedStream:
4029 case StandardStream:
4031 count=(ssize_t) write(fileno(image->blob->file_info.file),data,length);
4041 count=(ssize_t) fwrite((const char *) data,1,length,
4042 image->blob->file_info.file);
4047 c=putc((int) *p++,image->blob->file_info.file);
4054 c=putc((int) *p++,image->blob->file_info.file);
4066 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4071 count=(ssize_t) gzwrite(image->blob->file_info.gzfile,(void *) data,
4072 (unsigned int) length);
4077 c=gzputc(image->blob->file_info.gzfile,(int) *p++);
4084 c=gzputc(image->blob->file_info.gzfile,(int) *p++);
4097 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4098 count=(ssize_t) BZ2_bzwrite(image->blob->file_info.bzfile,(void *) data,
4105 count=(ssize_t) image->blob->stream(image,data,length);
4110 register unsigned char
4113 if ((image->blob->offset+(MagickOffsetType) length) >=
4114 (MagickOffsetType) image->blob->extent)
4116 if (image->blob->mapped != MagickFalse)
4118 image->blob->quantum<<=1;
4119 image->blob->extent+=length+image->blob->quantum;
4120 image->blob->data=(unsigned char *) ResizeQuantumMemory(
4121 image->blob->data,image->blob->extent+1,sizeof(*image->blob->data));
4122 (void) SyncBlob(image);
4123 if (image->blob->data == (unsigned char *) NULL)
4125 (void) DetachBlob(image->blob);
4129 q=image->blob->data+image->blob->offset;
4130 (void) memcpy(q,p,length);
4131 image->blob->offset+=length;
4132 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
4133 image->blob->length=(size_t) image->blob->offset;
4134 count=(ssize_t) length;
4141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4145 + W r i t e B l o b B y t e %
4149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4151 % WriteBlobByte() write an integer to a blob. It returns the number of bytes
4152 % written (either 0 or 1);
4154 % The format of the WriteBlobByte method is:
4156 % ssize_t WriteBlobByte(Image *image,const unsigned char value)
4158 % A description of each parameter follows.
4160 % o image: the image.
4162 % o value: Specifies the value to write.
4165 MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
4167 assert(image != (Image *) NULL);
4168 assert(image->signature == MagickSignature);
4169 return(WriteBlobStream(image,1,&value));
4173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4177 + W r i t e B l o b F l o a t %
4181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4183 % WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
4184 % specified by the endian member of the image structure.
4186 % The format of the WriteBlobFloat method is:
4188 % ssize_t WriteBlobFloat(Image *image,const float value)
4190 % A description of each parameter follows.
4192 % o image: the image.
4194 % o value: Specifies the value to write.
4197 MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
4208 quantum.unsigned_value=0U;
4209 quantum.float_value=value;
4210 return(WriteBlobLong(image,quantum.unsigned_value));
4214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4218 + W r i t e B l o b L o n g %
4222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4224 % WriteBlobLong() writes a ssize_t value as a 32-bit quantity in the byte-order
4225 % specified by the endian member of the image structure.
4227 % The format of the WriteBlobLong method is:
4229 % ssize_t WriteBlobLong(Image *image,const unsigned int value)
4231 % A description of each parameter follows.
4233 % o image: the image.
4235 % o value: Specifies the value to write.
4238 MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
4243 assert(image != (Image *) NULL);
4244 assert(image->signature == MagickSignature);
4245 if (image->endian == LSBEndian)
4247 buffer[0]=(unsigned char) value;
4248 buffer[1]=(unsigned char) (value >> 8);
4249 buffer[2]=(unsigned char) (value >> 16);
4250 buffer[3]=(unsigned char) (value >> 24);
4251 return(WriteBlobStream(image,4,buffer));
4253 buffer[0]=(unsigned char) (value >> 24);
4254 buffer[1]=(unsigned char) (value >> 16);
4255 buffer[2]=(unsigned char) (value >> 8);
4256 buffer[3]=(unsigned char) value;
4257 return(WriteBlobStream(image,4,buffer));
4261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4265 + W r i t e B l o b S h o r t %
4269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4271 % WriteBlobShort() writes a short value as a 16-bit quantity in the
4272 % byte-order specified by the endian member of the image structure.
4274 % The format of the WriteBlobShort method is:
4276 % ssize_t WriteBlobShort(Image *image,const unsigned short value)
4278 % A description of each parameter follows.
4280 % o image: the image.
4282 % o value: Specifies the value to write.
4285 MagickExport ssize_t WriteBlobShort(Image *image,const unsigned short value)
4290 assert(image != (Image *) NULL);
4291 assert(image->signature == MagickSignature);
4292 if (image->endian == LSBEndian)
4294 buffer[0]=(unsigned char) value;
4295 buffer[1]=(unsigned char) (value >> 8);
4296 return(WriteBlobStream(image,2,buffer));
4298 buffer[0]=(unsigned char) (value >> 8);
4299 buffer[1]=(unsigned char) value;
4300 return(WriteBlobStream(image,2,buffer));
4304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4308 + W r i t e B l o b L S B L o n g %
4312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4314 % WriteBlobLSBLong() writes a ssize_t value as a 32-bit quantity in
4315 % least-significant byte first order.
4317 % The format of the WriteBlobLSBLong method is:
4319 % ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4321 % A description of each parameter follows.
4323 % o image: the image.
4325 % o value: Specifies the value to write.
4328 MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4333 assert(image != (Image *) NULL);
4334 assert(image->signature == MagickSignature);
4335 buffer[0]=(unsigned char) value;
4336 buffer[1]=(unsigned char) (value >> 8);
4337 buffer[2]=(unsigned char) (value >> 16);
4338 buffer[3]=(unsigned char) (value >> 24);
4339 return(WriteBlobStream(image,4,buffer));
4343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4347 + W r i t e B l o b L S B S h o r t %
4351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4353 % WriteBlobLSBShort() writes a ssize_t value as a 16-bit quantity in
4354 % least-significant byte first order.
4356 % The format of the WriteBlobLSBShort method is:
4358 % ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4360 % A description of each parameter follows.
4362 % o image: the image.
4364 % o value: Specifies the value to write.
4367 MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4372 assert(image != (Image *) NULL);
4373 assert(image->signature == MagickSignature);
4374 buffer[0]=(unsigned char) value;
4375 buffer[1]=(unsigned char) (value >> 8);
4376 return(WriteBlobStream(image,2,buffer));
4380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4384 + W r i t e B l o b M S B L o n g %
4388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4390 % WriteBlobMSBLong() writes a ssize_t value as a 32-bit quantity in
4391 % most-significant byte first order.
4393 % The format of the WriteBlobMSBLong method is:
4395 % ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4397 % A description of each parameter follows.
4399 % o value: Specifies the value to write.
4401 % o image: the image.
4404 MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4409 assert(image != (Image *) NULL);
4410 assert(image->signature == MagickSignature);
4411 buffer[0]=(unsigned char) (value >> 24);
4412 buffer[1]=(unsigned char) (value >> 16);
4413 buffer[2]=(unsigned char) (value >> 8);
4414 buffer[3]=(unsigned char) value;
4415 return(WriteBlobStream(image,4,buffer));
4419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4423 + W r i t e B l o b M S B L o n g L o n g %
4427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4429 % WriteBlobMSBLongLong() writes a long long value as a 64-bit quantity in
4430 % most-significant byte first order.
4432 % The format of the WriteBlobMSBLongLong method is:
4434 % ssize_t WriteBlobMSBLongLong(Image *image,const MagickSizeType value)
4436 % A description of each parameter follows.
4438 % o value: Specifies the value to write.
4440 % o image: the image.
4443 MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
4444 const MagickSizeType value)
4449 assert(image != (Image *) NULL);
4450 assert(image->signature == MagickSignature);
4451 buffer[0]=(unsigned char) (value >> 56);
4452 buffer[1]=(unsigned char) (value >> 48);
4453 buffer[2]=(unsigned char) (value >> 40);
4454 buffer[3]=(unsigned char) (value >> 32);
4455 buffer[4]=(unsigned char) (value >> 24);
4456 buffer[5]=(unsigned char) (value >> 16);
4457 buffer[6]=(unsigned char) (value >> 8);
4458 buffer[7]=(unsigned char) value;
4459 return(WriteBlobStream(image,8,buffer));
4463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4467 + W r i t e B l o b M S B S h o r t %
4471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4473 % WriteBlobMSBShort() writes a ssize_t value as a 16-bit quantity in
4474 % most-significant byte first order.
4476 % The format of the WriteBlobMSBShort method is:
4478 % ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4480 % A description of each parameter follows.
4482 % o value: Specifies the value to write.
4484 % o file: Specifies the file to write the data to.
4487 MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4492 assert(image != (Image *) NULL);
4493 assert(image->signature == MagickSignature);
4494 buffer[0]=(unsigned char) (value >> 8);
4495 buffer[1]=(unsigned char) value;
4496 return(WriteBlobStream(image,2,buffer));
4500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4504 + W r i t e B l o b S t r i n g %
4508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4510 % WriteBlobString() write a string to a blob. It returns the number of
4511 % characters written.
4513 % The format of the WriteBlobString method is:
4515 % ssize_t WriteBlobString(Image *image,const char *string)
4517 % A description of each parameter follows.
4519 % o image: the image.
4521 % o string: Specifies the string to write.
4524 MagickExport ssize_t WriteBlobString(Image *image,const char *string)
4526 assert(image != (Image *) NULL);
4527 assert(image->signature == MagickSignature);
4528 assert(string != (const char *) NULL);
4529 return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));