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_HAVE_MMAP_FILEIO) && !defined(MAGICKCORE_WINDOWS_SUPPORT)
68 # include <sys/mman.h>
70 #if defined(MAGICKCORE_ZLIB_DELEGATE)
73 #if defined(MAGICKCORE_BZLIB_DELEGATE)
80 #define MagickMaxBlobExtent 65541
81 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
82 # define MAP_ANONYMOUS MAP_ANON
84 #if !defined(MAP_FAILED)
85 #define MAP_FAILED ((void *) -1)
92 #define _O_BINARY O_BINARY
98 typedef union FileInfo
103 #if defined(MAGICKCORE_ZLIB_DELEGATE)
108 #if defined(MAGICKCORE_BZLIB_DELEGATE)
166 Forward declarations.
172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 + A t t a c h B l o b %
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % AttachBlob() attaches a blob to the BlobInfo structure.
184 % The format of the AttachBlob method is:
186 % void AttachBlob(BlobInfo *blob_info,const void *blob,const size_t length)
188 % A description of each parameter follows:
190 % o blob_info: Specifies a pointer to a BlobInfo structure.
192 % o blob: the address of a character stream in one of the image formats
193 % understood by ImageMagick.
195 % o length: This size_t integer reflects the length in bytes of the blob.
198 MagickExport void AttachBlob(BlobInfo *blob_info,const void *blob,
201 assert(blob_info != (BlobInfo *) NULL);
202 if (blob_info->debug != MagickFalse)
203 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
204 blob_info->length=length;
205 blob_info->extent=length;
206 blob_info->quantum=(size_t) MagickMaxBlobExtent;
208 blob_info->type=BlobStream;
209 blob_info->file_info.file=(FILE *) NULL;
210 blob_info->data=(unsigned char *) blob;
211 blob_info->mapped=MagickFalse;
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 + B l o b T o F i l e %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 % BlobToFile() writes a blob to a file. It returns MagickFalse if an error
226 % occurs otherwise MagickTrue.
228 % The format of the BlobToFile method is:
230 % MagickBooleanType BlobToFile(char *filename,const void *blob,
231 % const size_t length,ExceptionInfo *exception)
233 % A description of each parameter follows:
235 % o filename: Write the blob to this file.
237 % o blob: the address of a blob.
239 % o length: This length in bytes of the blob.
241 % o exception: return any errors or warnings in this structure.
245 static inline MagickSizeType MagickMin(const MagickSizeType x,
246 const MagickSizeType y)
253 MagickExport MagickBooleanType BlobToFile(char *filename,const void *blob,
254 const size_t length,ExceptionInfo *exception)
265 assert(filename != (const char *) NULL);
266 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
267 assert(blob != (const void *) NULL);
268 if (*filename == '\0')
269 file=AcquireUniqueFileResource(filename);
271 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
274 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
277 for (i=0; i < length; i+=count)
279 count=(ssize_t) write(file,(const char *) blob+i,(size_t) MagickMin(length-
280 i,(MagickSizeType) SSIZE_MAX));
289 if ((file == -1) || (i < length))
291 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % B l o b T o I m a g e %
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 % BlobToImage() implements direct to memory image formats. It returns the
311 % The format of the BlobToImage method is:
313 % Image *BlobToImage(const ImageInfo *image_info,const void *blob,
314 % const size_t length,ExceptionInfo *exception)
316 % A description of each parameter follows:
318 % o image_info: the image info.
320 % o blob: the address of a character stream in one of the image formats
321 % understood by ImageMagick.
323 % o length: This size_t integer reflects the length in bytes of the blob.
325 % o exception: return any errors or warnings in this structure.
328 MagickExport Image *BlobToImage(const ImageInfo *image_info,const void *blob,
329 const size_t length,ExceptionInfo *exception)
344 assert(image_info != (ImageInfo *) NULL);
345 assert(image_info->signature == MagickSignature);
346 if (image_info->debug != MagickFalse)
347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
348 image_info->filename);
349 assert(exception != (ExceptionInfo *) NULL);
350 if ((blob == (const void *) NULL) || (length == 0))
352 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
353 "ZeroLengthBlobNotPermitted","'%s'",image_info->filename);
354 return((Image *) NULL);
356 blob_info=CloneImageInfo(image_info);
357 blob_info->blob=(void *) blob;
358 blob_info->length=length;
359 if (*blob_info->magick == '\0')
360 (void) SetImageInfo(blob_info,0,exception);
361 magick_info=GetMagickInfo(blob_info->magick,exception);
362 if (magick_info == (const MagickInfo *) NULL)
364 blob_info=DestroyImageInfo(blob_info);
365 (void) ThrowMagickException(exception,GetMagickModule(),
366 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
367 image_info->filename);
368 return((Image *) NULL);
370 if (GetMagickBlobSupport(magick_info) != MagickFalse)
373 Native blob support for this image format.
375 (void) CopyMagickString(blob_info->filename,image_info->filename,
377 (void) CopyMagickString(blob_info->magick,image_info->magick,
379 image=ReadImage(blob_info,exception);
380 if (image != (Image *) NULL)
381 (void) DetachBlob(image->blob);
382 blob_info=DestroyImageInfo(blob_info);
386 Write blob to a temporary file on disk.
388 blob_info->blob=(void *) NULL;
390 *blob_info->filename='\0';
391 status=BlobToFile(blob_info->filename,blob,length,exception);
392 if (status == MagickFalse)
394 (void) RelinquishUniqueFileResource(blob_info->filename);
395 blob_info=DestroyImageInfo(blob_info);
396 return((Image *) NULL);
398 clone_info=CloneImageInfo(blob_info);
399 (void) FormatLocaleString(clone_info->filename,MaxTextExtent,"%s:%s",
400 blob_info->magick,blob_info->filename);
401 image=ReadImage(clone_info,exception);
402 if (image != (Image *) NULL)
408 Restore original filenames.
410 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
412 (void) CopyMagickMemory(images->magick_filename,image_info->filename,
413 sizeof(images->magick_filename));
414 (void) CopyMagickMemory(images->filename,image_info->filename,
415 sizeof(images->filename));
416 images=GetNextImageInList(images);
419 clone_info=DestroyImageInfo(clone_info);
420 (void) RelinquishUniqueFileResource(blob_info->filename);
421 blob_info=DestroyImageInfo(blob_info);
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 + C l o n e B l o b I n f o %
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % CloneBlobInfo() makes a duplicate of the given blob info structure, or if
437 % blob info is NULL, a new one.
439 % The format of the CloneBlobInfo method is:
441 % BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
443 % A description of each parameter follows:
445 % o blob_info: the blob info.
448 MagickExport BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
453 clone_info=(BlobInfo *) AcquireMagickMemory(sizeof(*clone_info));
454 if (clone_info == (BlobInfo *) NULL)
455 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
456 GetBlobInfo(clone_info);
457 if (blob_info == (BlobInfo *) NULL)
459 clone_info->length=blob_info->length;
460 clone_info->extent=blob_info->extent;
461 clone_info->synchronize=blob_info->synchronize;
462 clone_info->quantum=blob_info->quantum;
463 clone_info->mapped=blob_info->mapped;
464 clone_info->eof=blob_info->eof;
465 clone_info->offset=blob_info->offset;
466 clone_info->size=blob_info->size;
467 clone_info->exempt=blob_info->exempt;
468 clone_info->status=blob_info->status;
469 clone_info->temporary=blob_info->temporary;
470 clone_info->type=blob_info->type;
471 clone_info->file_info.file=blob_info->file_info.file;
472 clone_info->properties=blob_info->properties;
473 clone_info->stream=blob_info->stream;
474 clone_info->data=blob_info->data;
475 clone_info->debug=IsEventLogging();
476 clone_info->reference_count=1;
481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485 + C l o s e B l o b %
489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491 % CloseBlob() closes a stream associated with the image.
493 % The format of the CloseBlob method is:
495 % MagickBooleanType CloseBlob(Image *image)
497 % A description of each parameter follows:
499 % o image: the image.
502 MagickExport MagickBooleanType CloseBlob(Image *image)
510 assert(image != (Image *) NULL);
511 assert(image->signature == MagickSignature);
512 if (image->debug != MagickFalse)
513 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
514 assert(image->blob != (BlobInfo *) NULL);
515 if (image->blob->type == UndefinedStream)
517 if (image->blob->synchronize != MagickFalse)
519 image->blob->size=GetBlobSize(image);
520 image->extent=image->blob->size;
521 image->blob->eof=MagickFalse;
522 if (image->blob->exempt != MagickFalse)
524 image->blob->type=UndefinedStream;
528 switch (image->blob->type)
530 case UndefinedStream:
536 status=ferror(image->blob->file_info.file);
541 #if defined(MAGICKCORE_ZLIB_DELEGATE)
542 (void) gzerror(image->blob->file_info.gzfile,&status);
548 #if defined(MAGICKCORE_BZLIB_DELEGATE)
549 (void) BZ2_bzerror(image->blob->file_info.bzfile,&status);
557 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
558 switch (image->blob->type)
560 case UndefinedStream:
565 if (image->blob->synchronize != MagickFalse)
567 status=fflush(image->blob->file_info.file);
568 status=fsync(fileno(image->blob->file_info.file));
570 status=fclose(image->blob->file_info.file);
575 #if defined(MAGICKCORE_HAVE_PCLOSE)
576 status=pclose(image->blob->file_info.file);
582 #if defined(MAGICKCORE_ZLIB_DELEGATE)
583 status=gzclose(image->blob->file_info.gzfile);
589 #if defined(MAGICKCORE_BZLIB_DELEGATE)
590 BZ2_bzclose(image->blob->file_info.bzfile);
598 if (image->blob->file_info.file != (FILE *) NULL)
600 if (image->blob->synchronize != MagickFalse)
601 (void) fsync(fileno(image->blob->file_info.file));
602 status=fclose(image->blob->file_info.file);
607 (void) DetachBlob(image->blob);
608 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
609 return(image->blob->status);
613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617 + D e s t r o y B l o b %
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 % DestroyBlob() deallocates memory associated with a blob.
625 % The format of the DestroyBlob method is:
627 % void DestroyBlob(Image *image)
629 % A description of each parameter follows:
631 % o image: the image.
634 MagickExport void DestroyBlob(Image *image)
639 assert(image != (Image *) NULL);
640 assert(image->signature == MagickSignature);
641 if (image->debug != MagickFalse)
642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
643 assert(image->blob != (BlobInfo *) NULL);
644 assert(image->blob->signature == MagickSignature);
646 LockSemaphoreInfo(image->blob->semaphore);
647 image->blob->reference_count--;
648 assert(image->blob->reference_count >= 0);
649 if (image->blob->reference_count == 0)
651 UnlockSemaphoreInfo(image->blob->semaphore);
652 if (destroy == MagickFalse)
654 (void) CloseBlob(image);
655 if (image->blob->mapped != MagickFalse)
656 (void) UnmapBlob(image->blob->data,image->blob->length);
657 if (image->blob->semaphore != (SemaphoreInfo *) NULL)
658 DestroySemaphoreInfo(&image->blob->semaphore);
659 image->blob->signature=(~MagickSignature);
660 image->blob=(BlobInfo *) RelinquishMagickMemory(image->blob);
664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 + D e t a c h B l o b %
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % DetachBlob() detaches a blob from the BlobInfo structure.
676 % The format of the DetachBlob method is:
678 % unsigned char *DetachBlob(BlobInfo *blob_info)
680 % A description of each parameter follows:
682 % o blob_info: Specifies a pointer to a BlobInfo structure.
685 MagickExport unsigned char *DetachBlob(BlobInfo *blob_info)
690 assert(blob_info != (BlobInfo *) NULL);
691 if (blob_info->debug != MagickFalse)
692 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
693 if (blob_info->mapped != MagickFalse)
694 (void) UnmapBlob(blob_info->data,blob_info->length);
695 blob_info->mapped=MagickFalse;
698 blob_info->eof=MagickFalse;
699 blob_info->exempt=MagickFalse;
700 blob_info->type=UndefinedStream;
701 blob_info->file_info.file=(FILE *) NULL;
702 data=blob_info->data;
703 blob_info->data=(unsigned char *) NULL;
704 blob_info->stream=(StreamHandler) NULL;
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 + D i s c a r d B l o b B y t e s %
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 % DiscardBlobBytes() discards bytes in a blob.
721 % The format of the DiscardBlobBytes method is:
723 % MagickBooleanType DiscardBlobBytes(Image *image,const size_t length)
725 % A description of each parameter follows.
727 % o image: the image.
729 % o length: the number of bytes to skip.
733 static inline const unsigned char *ReadBlobStream(Image *image,
734 const size_t length,unsigned char *data,ssize_t *count)
736 assert(count != (ssize_t *) NULL);
737 assert(image->blob != (BlobInfo *) NULL);
738 if (image->blob->type != BlobStream)
740 *count=ReadBlob(image,length,data);
743 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
746 image->blob->eof=MagickTrue;
749 data=image->blob->data+image->blob->offset;
750 *count=(ssize_t) MagickMin(length,(MagickSizeType) (image->blob->length-
751 image->blob->offset));
752 image->blob->offset+=(*count);
753 if (*count != (ssize_t) length)
754 image->blob->eof=MagickTrue;
758 MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
759 const MagickSizeType length)
761 register MagickOffsetType
773 assert(image != (Image *) NULL);
774 assert(image->signature == MagickSignature);
776 for (i=0; i < (MagickOffsetType) length; i+=count)
778 quantum=(size_t) MagickMin(length-i,sizeof(buffer));
779 (void) ReadBlobStream(image,quantum,buffer,&count);
787 return(i < (MagickOffsetType) length ? MagickFalse : MagickTrue);
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795 + D u p l i c a t e s B l o b %
799 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 % DuplicateBlob() duplicates a blob descriptor.
803 % The format of the DuplicateBlob method is:
805 % void DuplicateBlob(Image *image,const Image *duplicate)
807 % A description of each parameter follows:
809 % o image: the image.
811 % o duplicate: the duplicate image.
814 MagickExport void DuplicateBlob(Image *image,const Image *duplicate)
816 assert(image != (Image *) NULL);
817 assert(image->signature == MagickSignature);
818 if (image->debug != MagickFalse)
819 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
820 assert(duplicate != (Image *) NULL);
821 assert(duplicate->signature == MagickSignature);
823 image->blob=ReferenceBlob(duplicate->blob);
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 % EOFBlob() returns a non-zero value when EOF has been detected reading from
840 % The format of the EOFBlob method is:
842 % int EOFBlob(const Image *image)
844 % A description of each parameter follows:
846 % o image: the image.
849 MagickExport int EOFBlob(const Image *image)
851 assert(image != (Image *) NULL);
852 assert(image->signature == MagickSignature);
853 if (image->debug != MagickFalse)
854 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
855 assert(image->blob != (BlobInfo *) NULL);
856 assert(image->blob->type != UndefinedStream);
857 switch (image->blob->type)
859 case UndefinedStream:
865 image->blob->eof=feof(image->blob->file_info.file) != 0 ? MagickTrue :
871 image->blob->eof=MagickFalse;
876 #if defined(MAGICKCORE_BZLIB_DELEGATE)
881 (void) BZ2_bzerror(image->blob->file_info.bzfile,&status);
882 image->blob->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
888 image->blob->eof=MagickFalse;
894 return((int) image->blob->eof);
898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902 + F i l e T o B l o b %
906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
908 % FileToBlob() returns the contents of a file as a buffer terminated with
909 % the '\0' character. The length of the buffer (not including the extra
910 % terminating '\0' character) is returned via the 'length' parameter. Free
911 % the buffer with RelinquishMagickMemory().
913 % The format of the FileToBlob method is:
915 % unsigned char *FileToBlob(const char *filename,const size_t extent,
916 % size_t *length,ExceptionInfo *exception)
918 % A description of each parameter follows:
920 % o blob: FileToBlob() returns the contents of a file as a blob. If
921 % an error occurs NULL is returned.
923 % o filename: the filename.
925 % o extent: The maximum length of the blob.
927 % o length: On return, this reflects the actual length of the blob.
929 % o exception: return any errors or warnings in this structure.
932 MagickExport unsigned char *FileToBlob(const char *filename,const size_t extent,
933 size_t *length,ExceptionInfo *exception)
953 assert(filename != (const char *) NULL);
954 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
955 assert(exception != (ExceptionInfo *) NULL);
958 if (LocaleCompare(filename,"-") != 0)
959 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
962 ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
963 return((unsigned char *) NULL);
965 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
967 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
976 Stream is not seekable.
978 quantum=(size_t) MagickMaxBufferExtent;
979 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
980 quantum=(size_t) MagickMin((MagickSizeType) file_stats.st_size,
981 MagickMaxBufferExtent);
982 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
983 for (i=0; blob != (unsigned char *) NULL; i+=count)
985 count=(ssize_t) read(file,blob+i,quantum);
992 if (~(1UL*i) < (quantum+1))
994 blob=(unsigned char *) RelinquishMagickMemory(blob);
997 blob=(unsigned char *) ResizeQuantumMemory(blob,i+quantum+1,
999 if ((size_t) (i+count) >= extent)
1002 if (LocaleCompare(filename,"-") != 0)
1004 if (blob == (unsigned char *) NULL)
1006 (void) ThrowMagickException(exception,GetMagickModule(),
1007 ResourceLimitError,"MemoryAllocationFailed","'%s'",filename);
1008 return((unsigned char *) NULL);
1012 blob=(unsigned char *) RelinquishMagickMemory(blob);
1013 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1014 return((unsigned char *) NULL);
1016 *length=(size_t) MagickMin(i+count,extent);
1020 *length=(size_t) MagickMin((MagickSizeType) offset,extent);
1021 blob=(unsigned char *) NULL;
1022 if (~(*length) >= (MaxTextExtent-1))
1023 blob=(unsigned char *) AcquireQuantumMemory(*length+MaxTextExtent,
1025 if (blob == (unsigned char *) NULL)
1028 (void) ThrowMagickException(exception,GetMagickModule(),
1029 ResourceLimitError,"MemoryAllocationFailed","'%s'",filename);
1030 return((unsigned char *) NULL);
1032 map=MapBlob(file,ReadMode,0,*length);
1033 if (map != (unsigned char *) NULL)
1035 (void) memcpy(blob,map,*length);
1036 (void) UnmapBlob(map,*length);
1040 (void) lseek(file,0,SEEK_SET);
1041 for (i=0; i < *length; i+=count)
1043 count=(ssize_t) read(file,blob+i,(size_t) MagickMin(*length-i,
1044 (MagickSizeType) SSIZE_MAX));
1055 blob=(unsigned char *) RelinquishMagickMemory(blob);
1056 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1057 return((unsigned char *) NULL);
1061 if (LocaleCompare(filename,"-") != 0)
1065 blob=(unsigned char *) RelinquishMagickMemory(blob);
1066 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1076 % F i l e T o I m a g e %
1080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 % FileToImage() write the contents of a file to an image.
1084 % The format of the FileToImage method is:
1086 % MagickBooleanType FileToImage(Image *,const char *filename)
1088 % A description of each parameter follows:
1090 % o image: the image.
1092 % o filename: the filename.
1096 static inline ssize_t WriteBlobStream(Image *image,const size_t length,
1097 const unsigned char *data)
1102 register unsigned char
1105 assert(image->blob != (BlobInfo *) NULL);
1106 if (image->blob->type != BlobStream)
1107 return(WriteBlob(image,length,data));
1108 assert(image->blob->type != UndefinedStream);
1109 assert(data != (void *) NULL);
1110 extent=(MagickSizeType) (image->blob->offset+(MagickOffsetType) length);
1111 if (extent >= image->blob->extent)
1113 image->blob->quantum<<=1;
1114 extent=image->blob->extent+image->blob->quantum+length;
1115 if (SetBlobExtent(image,extent) == MagickFalse)
1118 q=image->blob->data+image->blob->offset;
1119 (void) memcpy(q,data,length);
1120 image->blob->offset+=length;
1121 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
1122 image->blob->length=(size_t) image->blob->offset;
1123 return((ssize_t) length);
1126 MagickExport MagickBooleanType FileToImage(Image *image,const char *filename,
1127 ExceptionInfo *exception)
1145 assert(image != (const Image *) NULL);
1146 assert(image->signature == MagickSignature);
1147 assert(filename != (const char *) NULL);
1148 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1149 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1152 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
1153 return(MagickFalse);
1155 quantum=(size_t) MagickMaxBufferExtent;
1156 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1157 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1158 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1159 if (blob == (unsigned char *) NULL)
1161 ThrowFileException(exception,ResourceLimitError,"MemoryAllocationFailed",
1163 return(MagickFalse);
1167 count=(ssize_t) read(file,blob,quantum);
1174 length=(size_t) count;
1175 count=WriteBlobStream(image,length,blob);
1176 if (count != (ssize_t) length)
1178 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1184 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1185 blob=(unsigned char *) RelinquishMagickMemory(blob);
1190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1194 + G e t B l o b E r r o r %
1198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200 % GetBlobError() returns MagickTrue if the blob associated with the specified
1201 % image encountered an error.
1203 % The format of the GetBlobError method is:
1205 % MagickBooleanType GetBlobError(const Image *image)
1207 % A description of each parameter follows:
1209 % o image: the image.
1212 MagickPrivate MagickBooleanType GetBlobError(const Image *image)
1214 assert(image != (const Image *) NULL);
1215 assert(image->signature == MagickSignature);
1216 if (image->debug != MagickFalse)
1217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1218 return(image->blob->status);
1222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226 + G e t B l o b F i l e H a n d l e %
1230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 % GetBlobFileHandle() returns the file handle associated with the image blob.
1234 % The format of the GetBlobFile method is:
1236 % FILE *GetBlobFileHandle(const Image *image)
1238 % A description of each parameter follows:
1240 % o image: the image.
1243 MagickExport FILE *GetBlobFileHandle(const Image *image)
1245 assert(image != (const Image *) NULL);
1246 assert(image->signature == MagickSignature);
1247 return(image->blob->file_info.file);
1251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1255 + G e t B l o b I n f o %
1259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1261 % GetBlobInfo() initializes the BlobInfo structure.
1263 % The format of the GetBlobInfo method is:
1265 % void GetBlobInfo(BlobInfo *blob_info)
1267 % A description of each parameter follows:
1269 % o blob_info: Specifies a pointer to a BlobInfo structure.
1272 MagickPrivate void GetBlobInfo(BlobInfo *blob_info)
1274 assert(blob_info != (BlobInfo *) NULL);
1275 (void) ResetMagickMemory(blob_info,0,sizeof(*blob_info));
1276 blob_info->type=UndefinedStream;
1277 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1278 blob_info->properties.st_mtime=time((time_t *) NULL);
1279 blob_info->properties.st_ctime=time((time_t *) NULL);
1280 blob_info->debug=IsEventLogging();
1281 blob_info->reference_count=1;
1282 blob_info->semaphore=AllocateSemaphoreInfo();
1283 blob_info->signature=MagickSignature;
1287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1291 % G e t B l o b P r o p e r t i e s %
1295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1297 % GetBlobProperties() returns information about an image blob.
1299 % The format of the GetBlobProperties method is:
1301 % const struct stat *GetBlobProperties(const Image *image)
1303 % A description of each parameter follows:
1305 % o image: the image.
1308 MagickPrivate const struct stat *GetBlobProperties(const Image *image)
1310 assert(image != (Image *) NULL);
1311 assert(image->signature == MagickSignature);
1312 if (image->debug != MagickFalse)
1313 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1314 return(&image->blob->properties);
1318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1322 + G e t B l o b S i z e %
1326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1328 % GetBlobSize() returns the current length of the image file or blob; zero is
1329 % returned if the size cannot be determined.
1331 % The format of the GetBlobSize method is:
1333 % MagickSizeType GetBlobSize(const Image *image)
1335 % A description of each parameter follows:
1337 % o image: the image.
1340 MagickExport MagickSizeType GetBlobSize(const Image *image)
1345 assert(image != (Image *) NULL);
1346 assert(image->signature == MagickSignature);
1347 if (image->debug != MagickFalse)
1348 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1349 assert(image->blob != (BlobInfo *) NULL);
1351 switch (image->blob->type)
1353 case UndefinedStream:
1355 extent=image->blob->size;
1360 if (fstat(fileno(image->blob->file_info.file),&image->blob->properties) == 0)
1361 extent=(MagickSizeType) image->blob->properties.st_size;
1364 case StandardStream:
1367 extent=image->blob->size;
1376 status=GetPathAttributes(image->filename,&image->blob->properties);
1377 if (status != MagickFalse)
1378 extent=(MagickSizeType) image->blob->properties.st_size;
1385 extent=(MagickSizeType) image->blob->length;
1393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1397 + G e t B l o b S t r e a m D a t a %
1401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1403 % GetBlobStreamData() returns the stream data for the image.
1405 % The format of the GetBlobStreamData method is:
1407 % unsigned char *GetBlobStreamData(const Image *image)
1409 % A description of each parameter follows:
1411 % o image: the image.
1414 MagickExport unsigned char *GetBlobStreamData(const Image *image)
1416 assert(image != (const Image *) NULL);
1417 assert(image->signature == MagickSignature);
1418 return(image->blob->data);
1422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 + G e t B l o b S t r e a m H a n d l e r %
1430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432 % GetBlobStreamHandler() returns the stream handler for the image.
1434 % The format of the GetBlobStreamHandler method is:
1436 % StreamHandler GetBlobStreamHandler(const Image *image)
1438 % A description of each parameter follows:
1440 % o image: the image.
1443 MagickPrivate StreamHandler GetBlobStreamHandler(const Image *image)
1445 assert(image != (const Image *) NULL);
1446 assert(image->signature == MagickSignature);
1447 if (image->debug != MagickFalse)
1448 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1449 return(image->blob->stream);
1453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457 % I m a g e T o B l o b %
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463 % ImageToBlob() implements direct to memory image formats. It returns the
1464 % image as a formatted blob and its length. The magick member of the Image
1465 % structure determines the format of the returned blob (GIF, JPEG, PNG,
1466 % etc.). This method is the equivalent of WriteImage(), but writes the
1467 % formatted "file" to a memory buffer rather than to an actual file.
1469 % The format of the ImageToBlob method is:
1471 % unsigned char *ImageToBlob(const ImageInfo *image_info,Image *image,
1472 % size_t *length,ExceptionInfo *exception)
1474 % A description of each parameter follows:
1476 % o image_info: the image info.
1478 % o image: the image.
1480 % o length: This pointer to a size_t integer sets the initial length of the
1481 % blob. On return, it reflects the actual length of the blob.
1483 % o exception: return any errors or warnings in this structure.
1486 MagickExport unsigned char *ImageToBlob(const ImageInfo *image_info,
1487 Image *image,size_t *length,ExceptionInfo *exception)
1501 assert(image_info != (const ImageInfo *) NULL);
1502 assert(image_info->signature == MagickSignature);
1503 if (image_info->debug != MagickFalse)
1504 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1505 image_info->filename);
1506 assert(image != (Image *) NULL);
1507 assert(image->signature == MagickSignature);
1508 assert(exception != (ExceptionInfo *) NULL);
1510 blob=(unsigned char *) NULL;
1511 blob_info=CloneImageInfo(image_info);
1512 blob_info->adjoin=MagickFalse;
1513 (void) SetImageInfo(blob_info,1,exception);
1514 if (*blob_info->magick != '\0')
1515 (void) CopyMagickString(image->magick,blob_info->magick,MaxTextExtent);
1516 magick_info=GetMagickInfo(image->magick,exception);
1517 if (magick_info == (const MagickInfo *) NULL)
1519 (void) ThrowMagickException(exception,GetMagickModule(),
1520 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
1524 (void) CopyMagickString(blob_info->magick,image->magick,MaxTextExtent);
1525 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1528 Native blob support for this image format.
1530 blob_info->length=0;
1531 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1532 sizeof(unsigned char));
1533 if (blob_info->blob == (void *) NULL)
1534 (void) ThrowMagickException(exception,GetMagickModule(),
1535 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
1538 (void) CloseBlob(image);
1539 image->blob->exempt=MagickTrue;
1540 *image->filename='\0';
1541 status=WriteImage(blob_info,image,exception);
1542 if ((status != MagickFalse) && (image->blob->length != 0))
1544 *length=image->blob->length;
1545 blob=DetachBlob(image->blob);
1546 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1554 unique[MaxTextExtent];
1560 Write file to disk in blob image format.
1562 file=AcquireUniqueFileResource(unique);
1565 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
1566 image_info->filename);
1570 blob_info->file=fdopen(file,"wb");
1571 if (blob_info->file != (FILE *) NULL)
1573 (void) FormatLocaleString(image->filename,MaxTextExtent,"%s:%s",
1574 image->magick,unique);
1575 status=WriteImage(blob_info,image,exception);
1576 (void) fclose(blob_info->file);
1577 if (status != MagickFalse)
1578 blob=FileToBlob(image->filename,~0UL,length,exception);
1580 (void) RelinquishUniqueFileResource(unique);
1583 blob_info=DestroyImageInfo(blob_info);
1588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1592 % I m a g e T o F i l e %
1596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1598 % ImageToFile() writes an image to a file. It returns MagickFalse if an error
1599 % occurs otherwise MagickTrue.
1601 % The format of the ImageToFile method is:
1603 % MagickBooleanType ImageToFile(Image *image,char *filename,
1604 % ExceptionInfo *exception)
1606 % A description of each parameter follows:
1608 % o image: the image.
1610 % o filename: Write the image to this file.
1612 % o exception: return any errors or warnings in this structure.
1615 MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
1616 ExceptionInfo *exception)
1621 register const unsigned char
1640 assert(image != (Image *) NULL);
1641 assert(image->signature == MagickSignature);
1642 assert(image->blob != (BlobInfo *) NULL);
1643 assert(image->blob->type != UndefinedStream);
1644 if (image->debug != MagickFalse)
1645 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1646 assert(filename != (const char *) NULL);
1647 if (*filename == '\0')
1648 file=AcquireUniqueFileResource(filename);
1650 if (LocaleCompare(filename,"-") == 0)
1651 file=fileno(stdout);
1653 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1656 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1657 return(MagickFalse);
1659 quantum=(size_t) MagickMaxBufferExtent;
1660 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1661 quantum=(size_t) MagickMin((MagickSizeType) file_stats.st_size,
1662 MagickMaxBufferExtent);
1663 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1664 if (buffer == (unsigned char *) NULL)
1667 (void) ThrowMagickException(exception,GetMagickModule(),
1668 ResourceLimitError,"MemoryAllocationError","'%s'",filename);
1669 return(MagickFalse);
1672 p=ReadBlobStream(image,quantum,buffer,&count);
1673 for (i=0; count > 0; p=ReadBlobStream(image,quantum,buffer,&count))
1675 length=(size_t) count;
1676 for (i=0; i < length; i+=count)
1678 count=write(file,p+i,(size_t) (length-i));
1689 if (LocaleCompare(filename,"-") != 0)
1691 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1692 if ((file == -1) || (i < length))
1694 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1695 return(MagickFalse);
1701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1705 % I m a g e s T o B l o b %
1709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1711 % ImagesToBlob() implements direct to memory image formats. It returns the
1712 % image sequence as a blob and its length. The magick member of the ImageInfo
1713 % structure determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1715 % Note, some image formats do not permit multiple images to the same image
1716 % stream (e.g. JPEG). in this instance, just the first image of the
1717 % sequence is returned as a blob.
1719 % The format of the ImagesToBlob method is:
1721 % unsigned char *ImagesToBlob(const ImageInfo *image_info,Image *images,
1722 % size_t *length,ExceptionInfo *exception)
1724 % A description of each parameter follows:
1726 % o image_info: the image info.
1728 % o images: the image list.
1730 % o length: This pointer to a size_t integer sets the initial length of the
1731 % blob. On return, it reflects the actual length of the blob.
1733 % o exception: return any errors or warnings in this structure.
1736 MagickExport unsigned char *ImagesToBlob(const ImageInfo *image_info,
1737 Image *images,size_t *length,ExceptionInfo *exception)
1751 assert(image_info != (const ImageInfo *) NULL);
1752 assert(image_info->signature == MagickSignature);
1753 if (image_info->debug != MagickFalse)
1754 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1755 image_info->filename);
1756 assert(images != (Image *) NULL);
1757 assert(images->signature == MagickSignature);
1758 assert(exception != (ExceptionInfo *) NULL);
1760 blob=(unsigned char *) NULL;
1761 blob_info=CloneImageInfo(image_info);
1762 (void) SetImageInfo(blob_info,(unsigned int) GetImageListLength(images),
1764 if (*blob_info->magick != '\0')
1765 (void) CopyMagickString(images->magick,blob_info->magick,MaxTextExtent);
1766 if (blob_info->adjoin == MagickFalse)
1768 blob_info=DestroyImageInfo(blob_info);
1769 return(ImageToBlob(image_info,images,length,exception));
1771 magick_info=GetMagickInfo(images->magick,exception);
1772 if (magick_info == (const MagickInfo *) NULL)
1774 (void) ThrowMagickException(exception,GetMagickModule(),
1775 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
1779 (void) CopyMagickString(blob_info->magick,images->magick,MaxTextExtent);
1780 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1783 Native blob support for this images format.
1785 blob_info->length=0;
1786 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1787 sizeof(unsigned char));
1788 if (blob_info->blob == (void *) NULL)
1789 (void) ThrowMagickException(exception,GetMagickModule(),
1790 ResourceLimitError,"MemoryAllocationFailed","'%s'",images->filename);
1793 images->blob->exempt=MagickTrue;
1794 *images->filename='\0';
1795 status=WriteImages(blob_info,images,images->filename,exception);
1796 if ((status != MagickFalse) && (images->blob->length != 0))
1798 *length=images->blob->length;
1799 blob=DetachBlob(images->blob);
1800 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1808 filename[MaxTextExtent],
1809 unique[MaxTextExtent];
1815 Write file to disk in blob images format.
1817 file=AcquireUniqueFileResource(unique);
1820 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
1821 image_info->filename);
1825 blob_info->file=fdopen(file,"wb");
1826 if (blob_info->file != (FILE *) NULL)
1828 (void) FormatLocaleString(filename,MaxTextExtent,"%s:%s",
1829 images->magick,unique);
1830 status=WriteImages(blob_info,images,filename,exception);
1831 (void) fclose(blob_info->file);
1832 if (status != MagickFalse)
1833 blob=FileToBlob(images->filename,~0UL,length,exception);
1835 (void) RelinquishUniqueFileResource(unique);
1838 blob_info=DestroyImageInfo(blob_info);
1842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1846 % I n j e c t I m a g e B l o b %
1850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1852 % InjectImageBlob() injects the image with a copy of itself in the specified
1853 % format (e.g. inject JPEG into a PDF image).
1855 % The format of the InjectImageBlob method is:
1857 % MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1858 % Image *image,Image *inject_image,const char *format,
1859 % ExceptionInfo *exception)
1861 % A description of each parameter follows:
1863 % o image_info: the image info..
1865 % o image: the image.
1867 % o inject_image: inject into the image stream.
1869 % o format: the image format.
1871 % o exception: return any errors or warnings in this structure.
1874 MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1875 Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
1878 filename[MaxTextExtent];
1911 Write inject image to a temporary file.
1913 assert(image_info != (ImageInfo *) NULL);
1914 assert(image_info->signature == MagickSignature);
1915 assert(image != (Image *) NULL);
1916 assert(image->signature == MagickSignature);
1917 if (image->debug != MagickFalse)
1918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1919 assert(inject_image != (Image *) NULL);
1920 assert(inject_image->signature == MagickSignature);
1921 assert(exception != (ExceptionInfo *) NULL);
1922 unique_file=(FILE *) NULL;
1923 file=AcquireUniqueFileResource(filename);
1925 unique_file=fdopen(file,"wb");
1926 if ((file == -1) || (unique_file == (FILE *) NULL))
1928 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1929 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1931 return(MagickFalse);
1933 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
1934 if (byte_image == (Image *) NULL)
1936 (void) fclose(unique_file);
1937 (void) RelinquishUniqueFileResource(filename);
1938 return(MagickFalse);
1940 (void) FormatLocaleString(byte_image->filename,MaxTextExtent,"%s:%s",format,
1942 DestroyBlob(byte_image);
1943 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
1944 write_info=CloneImageInfo(image_info);
1945 SetImageInfoFile(write_info,unique_file);
1946 status=WriteImage(write_info,byte_image,exception);
1947 write_info=DestroyImageInfo(write_info);
1948 byte_image=DestroyImage(byte_image);
1949 (void) fclose(unique_file);
1950 if (status == MagickFalse)
1952 (void) RelinquishUniqueFileResource(filename);
1953 return(MagickFalse);
1956 Inject into image stream.
1958 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1961 (void) RelinquishUniqueFileResource(filename);
1962 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1963 image_info->filename);
1964 return(MagickFalse);
1966 quantum=(size_t) MagickMaxBufferExtent;
1967 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
1968 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1969 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1970 if (buffer == (unsigned char *) NULL)
1972 (void) RelinquishUniqueFileResource(filename);
1973 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1976 for (i=0; ; i+=count)
1978 count=(ssize_t) read(file,buffer,quantum);
1985 status=WriteBlobStream(image,(size_t) count,buffer) == count ? MagickTrue :
1990 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",filename);
1991 (void) RelinquishUniqueFileResource(filename);
1992 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001 + I s B l o b E x e m p t %
2005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2007 % IsBlobExempt() returns true if the blob is exempt.
2009 % The format of the IsBlobExempt method is:
2011 % MagickBooleanType IsBlobExempt(const Image *image)
2013 % A description of each parameter follows:
2015 % o image: the image.
2018 MagickPrivate MagickBooleanType IsBlobExempt(const Image *image)
2020 assert(image != (const Image *) NULL);
2021 assert(image->signature == MagickSignature);
2022 if (image->debug != MagickFalse)
2023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2024 return(image->blob->exempt);
2028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2032 + I s B l o b S e e k a b l e %
2036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2038 % IsBlobSeekable() returns true if the blob is seekable.
2040 % The format of the IsBlobSeekable method is:
2042 % MagickBooleanType IsBlobSeekable(const Image *image)
2044 % A description of each parameter follows:
2046 % o image: the image.
2049 MagickPrivate MagickBooleanType IsBlobSeekable(const Image *image)
2054 assert(image != (const Image *) NULL);
2055 assert(image->signature == MagickSignature);
2056 if (image->debug != MagickFalse)
2057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2058 switch (image->blob->type)
2064 seekable=MagickTrue;
2069 seekable=MagickFalse;
2077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2081 + I s B l o b T e m p o r a r y %
2085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2087 % IsBlobTemporary() returns true if the blob is temporary.
2089 % The format of the IsBlobTemporary method is:
2091 % MagickBooleanType IsBlobTemporary(const Image *image)
2093 % A description of each parameter follows:
2095 % o image: the image.
2098 MagickPrivate MagickBooleanType IsBlobTemporary(const Image *image)
2100 assert(image != (const Image *) NULL);
2101 assert(image->signature == MagickSignature);
2102 if (image->debug != MagickFalse)
2103 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2104 return(image->blob->temporary);
2108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2118 % MapBlob() creates a mapping from a file to a binary large object.
2120 % The format of the MapBlob method is:
2122 % unsigned char *MapBlob(int file,const MapMode mode,
2123 % const MagickOffsetType offset,const size_t length)
2125 % A description of each parameter follows:
2127 % o file: map this file descriptor.
2129 % o mode: ReadMode, WriteMode, or IOMode.
2131 % o offset: starting at this offset within the file.
2133 % o length: the length of the mapping is returned in this pointer.
2136 MagickExport unsigned char *MapBlob(int file,const MapMode mode,
2137 const MagickOffsetType offset,const size_t length)
2139 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
2152 #if defined(MAP_ANONYMOUS)
2153 flags|=MAP_ANONYMOUS;
2155 return((unsigned char *) NULL);
2162 protection=PROT_READ;
2164 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2170 protection=PROT_WRITE;
2172 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2174 #if defined(MAGICKCORE_HAVE_POSIX_MADVISE)
2175 (void) posix_madvise(map,length,POSIX_MADV_SEQUENTIAL |
2176 POSIX_MADV_WILLNEED);
2182 protection=PROT_READ | PROT_WRITE;
2184 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2189 if (map == (unsigned char *) MAP_FAILED)
2190 return((unsigned char *) NULL);
2197 return((unsigned char *) NULL);
2202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2206 + M S B O r d e r L o n g %
2210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212 % MSBOrderLong() converts a least-significant byte first buffer of integers to
2213 % most-significant byte first.
2215 % The format of the MSBOrderLong method is:
2217 % void MSBOrderLong(unsigned char *buffer,const size_t length)
2219 % A description of each parameter follows.
2221 % o buffer: Specifies a pointer to a buffer of integers.
2223 % o length: Specifies the length of the buffer.
2226 MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
2231 register unsigned char
2235 assert(buffer != (unsigned char *) NULL);
2242 *buffer++=(unsigned char) c;
2246 *buffer++=(unsigned char) c;
2252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2256 + M S B O r d e r S h o r t %
2260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2262 % MSBOrderShort() converts a least-significant byte first buffer of integers
2263 % to most-significant byte first.
2265 % The format of the MSBOrderShort method is:
2267 % void MSBOrderShort(unsigned char *p,const size_t length)
2269 % A description of each parameter follows.
2271 % o p: Specifies a pointer to a buffer of integers.
2273 % o length: Specifies the length of the buffer.
2276 MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
2281 register unsigned char
2284 assert(p != (unsigned char *) NULL);
2291 *p++=(unsigned char) c;
2296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2306 % OpenBlob() opens a file associated with the image. A file name of '-' sets
2307 % the file to stdin for type 'r' and stdout for type 'w'. If the filename
2308 % suffix is '.gz' or '.Z', the image is decompressed for type 'r' and
2309 % compressed for type 'w'. If the filename prefix is '|', it is piped to or
2310 % from a system command.
2312 % The format of the OpenBlob method is:
2314 % MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
2315 % const BlobMode mode,ExceptionInfo *exception)
2317 % A description of each parameter follows:
2319 % o image_info: the image info.
2321 % o image: the image.
2323 % o mode: the mode for opening the file.
2326 MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
2327 Image *image,const BlobMode mode,ExceptionInfo *exception)
2330 extension[MaxTextExtent],
2331 filename[MaxTextExtent];
2342 assert(image_info != (ImageInfo *) NULL);
2343 assert(image_info->signature == MagickSignature);
2344 if (image_info->debug != MagickFalse)
2345 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2346 image_info->filename);
2347 assert(image != (Image *) NULL);
2348 assert(image->signature == MagickSignature);
2349 if (image_info->blob != (void *) NULL)
2351 if (image_info->stream != (StreamHandler) NULL)
2352 image->blob->stream=(StreamHandler) image_info->stream;
2353 AttachBlob(image->blob,image_info->blob,image_info->length);
2356 (void) DetachBlob(image->blob);
2359 default: type="r"; break;
2360 case ReadBlobMode: type="r"; break;
2361 case ReadBinaryBlobMode: type="rb"; break;
2362 case WriteBlobMode: type="w"; break;
2363 case WriteBinaryBlobMode: type="w+b"; break;
2364 case AppendBlobMode: type="a"; break;
2365 case AppendBinaryBlobMode: type="a+b"; break;
2368 image->blob->synchronize=image_info->synchronize;
2369 if (image_info->stream != (StreamHandler) NULL)
2371 image->blob->stream=(StreamHandler) image_info->stream;
2374 image->blob->type=FifoStream;
2382 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2383 rights=ReadPolicyRights;
2385 rights=WritePolicyRights;
2386 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2389 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2390 "NotAuthorized","'%s'",filename);
2391 return(MagickFalse);
2393 if ((LocaleCompare(filename,"-") == 0) ||
2394 ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
2396 image->blob->file_info.file=(*type == 'r') ? stdin : stdout;
2397 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2398 if (strchr(type,'b') != (char *) NULL)
2399 setmode(_fileno(image->blob->file_info.file),_O_BINARY);
2401 image->blob->type=StandardStream;
2402 image->blob->exempt=MagickTrue;
2405 if (LocaleNCompare(filename,"fd:",3) == 0)
2408 mode[MaxTextExtent];
2412 image->blob->file_info.file=fdopen(StringToLong(filename+3),mode);
2413 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2414 if (strchr(type,'b') != (char *) NULL)
2415 setmode(_fileno(image->blob->file_info.file),_O_BINARY);
2417 image->blob->type=StandardStream;
2418 image->blob->exempt=MagickTrue;
2421 #if defined(MAGICKCORE_HAVE_POPEN)
2422 if (*filename == '|')
2425 mode[MaxTextExtent];
2428 Pipe image to or from a system command.
2430 #if defined(SIGPIPE)
2432 (void) signal(SIGPIPE,SIG_IGN);
2436 image->blob->file_info.file=(FILE *) popen_utf8(filename+1,mode);
2437 if (image->blob->file_info.file == (FILE *) NULL)
2439 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2440 return(MagickFalse);
2442 image->blob->type=PipeStream;
2443 image->blob->exempt=MagickTrue;
2447 status=GetPathAttributes(filename,&image->blob->properties);
2448 #if defined(S_ISFIFO)
2449 if ((status == MagickTrue) && S_ISFIFO(image->blob->properties.st_mode))
2451 image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2452 if (image->blob->file_info.file == (FILE *) NULL)
2454 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2455 return(MagickFalse);
2457 image->blob->type=FileStream;
2458 image->blob->exempt=MagickTrue;
2462 GetPathComponent(image->filename,ExtensionPath,extension);
2465 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2466 if ((image_info->adjoin == MagickFalse) ||
2467 (strchr(filename,'%') != (char *) NULL))
2470 Form filename for multi-part images.
2472 (void) InterpretImageFilename(image_info,image,image->filename,(int)
2473 image->scene,filename,exception);
2474 if ((LocaleCompare(filename,image->filename) == 0) &&
2475 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2476 (GetNextImageInList(image) != (Image *) NULL)))
2479 path[MaxTextExtent];
2481 GetPathComponent(image->filename,RootPath,path);
2482 if (*extension == '\0')
2483 (void) FormatLocaleString(filename,MaxTextExtent,"%s-%.20g",
2484 path,(double) image->scene);
2486 (void) FormatLocaleString(filename,MaxTextExtent,"%s-%.20g.%s",
2487 path,(double) image->scene,extension);
2489 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
2490 #if defined(macintosh)
2491 SetApplicationType(filename,image_info->magick,'8BIM');
2495 if (image_info->file != (FILE *) NULL)
2497 image->blob->file_info.file=image_info->file;
2498 image->blob->type=FileStream;
2499 image->blob->exempt=MagickTrue;
2504 image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2505 if (image->blob->file_info.file != (FILE *) NULL)
2513 image->blob->type=FileStream;
2514 #if defined(MAGICKCORE_HAVE_SETVBUF)
2515 (void) setvbuf(image->blob->file_info.file,(char *) NULL,
2516 (int) _IOFBF,16384);
2518 (void) ResetMagickMemory(magick,0,sizeof(magick));
2519 count=fread(magick,1,sizeof(magick),image->blob->file_info.file);
2520 (void) rewind(image->blob->file_info.file);
2521 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2522 " read %.20g magic header bytes",(double) count);
2523 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2524 if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
2525 ((int) magick[2] == 0x08))
2527 (void) fclose(image->blob->file_info.file);
2528 image->blob->file_info.gzfile=gzopen(filename,type);
2529 if (image->blob->file_info.gzfile != (gzFile) NULL)
2530 image->blob->type=ZipStream;
2533 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2534 if (strncmp((char *) magick,"BZh",3) == 0)
2536 (void) fclose(image->blob->file_info.file);
2537 image->blob->file_info.bzfile=BZ2_bzopen(filename,type);
2538 if (image->blob->file_info.bzfile != (BZFILE *) NULL)
2539 image->blob->type=BZipStream;
2542 if (image->blob->type == FileStream)
2553 sans_exception=AcquireExceptionInfo();
2554 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2555 sans_exception=DestroyExceptionInfo(sans_exception);
2556 properties=(&image->blob->properties);
2557 if ((magick_info != (const MagickInfo *) NULL) &&
2558 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2559 (properties->st_size <= MagickMaxBufferExtent))
2567 length=(size_t) properties->st_size;
2568 blob=MapBlob(fileno(image->blob->file_info.file),ReadMode,
2570 if (blob != (void *) NULL)
2573 Format supports blobs-- use memory-mapped I/O.
2575 if (image_info->file != (FILE *) NULL)
2576 image->blob->exempt=MagickFalse;
2579 (void) fclose(image->blob->file_info.file);
2580 image->blob->file_info.file=(FILE *) NULL;
2582 AttachBlob(image->blob,blob,length);
2583 image->blob->mapped=MagickTrue;
2590 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2591 if ((LocaleCompare(extension,"Z") == 0) ||
2592 (LocaleCompare(extension,"gz") == 0) ||
2593 (LocaleCompare(extension,"wmz") == 0) ||
2594 (LocaleCompare(extension,"svgz") == 0))
2596 if (mode == WriteBinaryBlobMode)
2598 image->blob->file_info.gzfile=gzopen(filename,type);
2599 if (image->blob->file_info.gzfile != (gzFile) NULL)
2600 image->blob->type=ZipStream;
2604 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2605 if (LocaleCompare(extension,"bz2") == 0)
2607 image->blob->file_info.bzfile=BZ2_bzopen(filename,type);
2608 if (image->blob->file_info.bzfile != (BZFILE *) NULL)
2609 image->blob->type=BZipStream;
2614 image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
2615 if (image->blob->file_info.file != (FILE *) NULL)
2617 image->blob->type=FileStream;
2618 #if defined(MAGICKCORE_HAVE_SETVBUF)
2619 (void) setvbuf(image->blob->file_info.file,(char *) NULL,(int) _IOFBF,
2624 image->blob->status=MagickFalse;
2625 if (image->blob->type != UndefinedStream)
2626 image->blob->size=GetBlobSize(image);
2629 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2630 return(MagickFalse);
2636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2646 % PingBlob() returns all the attributes of an image or image sequence except
2647 % for the pixels. It is much faster and consumes far less memory than
2648 % BlobToImage(). On failure, a NULL image is returned and exception
2649 % describes the reason for the failure.
2651 % The format of the PingBlob method is:
2653 % Image *PingBlob(const ImageInfo *image_info,const void *blob,
2654 % const size_t length,ExceptionInfo *exception)
2656 % A description of each parameter follows:
2658 % o image_info: the image info.
2660 % o blob: the address of a character stream in one of the image formats
2661 % understood by ImageMagick.
2663 % o length: This size_t integer reflects the length in bytes of the blob.
2665 % o exception: return any errors or warnings in this structure.
2669 #if defined(__cplusplus) || defined(c_plusplus)
2673 static size_t PingStream(const Image *magick_unused(image),
2674 const void *magick_unused(pixels),const size_t columns)
2679 #if defined(__cplusplus) || defined(c_plusplus)
2683 MagickExport Image *PingBlob(const ImageInfo *image_info,const void *blob,
2684 const size_t length,ExceptionInfo *exception)
2692 assert(image_info != (ImageInfo *) NULL);
2693 assert(image_info->signature == MagickSignature);
2694 if (image_info->debug != MagickFalse)
2695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2696 image_info->filename);
2697 assert(exception != (ExceptionInfo *) NULL);
2698 if ((blob == (const void *) NULL) || (length == 0))
2700 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
2701 "UnrecognizedImageFormat","'%s'",image_info->magick);
2702 return((Image *) NULL);
2704 ping_info=CloneImageInfo(image_info);
2705 ping_info->blob=(void *) AcquireQuantumMemory(length,sizeof(unsigned char));
2706 if (ping_info->blob == (const void *) NULL)
2708 (void) ThrowMagickException(exception,GetMagickModule(),
2709 ResourceLimitFatalError,"MemoryAllocationFailed","'%s'","");
2710 return((Image *) NULL);
2712 (void) memcpy(ping_info->blob,blob,length);
2713 ping_info->length=length;
2714 ping_info->ping=MagickTrue;
2715 image=ReadStream(ping_info,&PingStream,exception);
2716 ping_info->blob=(void *) RelinquishMagickMemory(ping_info->blob);
2717 ping_info=DestroyImageInfo(ping_info);
2722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2732 % ReadBlob() reads data from the blob or image file and returns it. It
2733 % returns the number of bytes read.
2735 % The format of the ReadBlob method is:
2737 % ssize_t ReadBlob(Image *image,const size_t length,unsigned char *data)
2739 % A description of each parameter follows:
2741 % o image: the image.
2743 % o length: Specifies an integer representing the number of bytes to read
2746 % o data: Specifies an area to place the information requested from the
2750 MagickExport ssize_t ReadBlob(Image *image,const size_t length,
2751 unsigned char *data)
2756 register unsigned char
2762 assert(image != (Image *) NULL);
2763 assert(image->signature == MagickSignature);
2764 assert(image->blob != (BlobInfo *) NULL);
2765 assert(image->blob->type != UndefinedStream);
2768 assert(data != (void *) NULL);
2771 switch (image->blob->type)
2773 case UndefinedStream:
2776 case StandardStream:
2783 count=(ssize_t) fread(q,1,length,image->blob->file_info.file);
2788 c=getc(image->blob->file_info.file);
2791 *q++=(unsigned char) c;
2796 c=getc(image->blob->file_info.file);
2799 *q++=(unsigned char) c;
2809 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2814 count=(ssize_t) gzread(image->blob->file_info.gzfile,q,
2815 (unsigned int) length);
2820 c=gzgetc(image->blob->file_info.gzfile);
2823 *q++=(unsigned char) c;
2828 c=gzgetc(image->blob->file_info.gzfile);
2831 *q++=(unsigned char) c;
2842 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2843 count=(ssize_t) BZ2_bzread(image->blob->file_info.bzfile,q,
2852 register const unsigned char
2855 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
2857 image->blob->eof=MagickTrue;
2860 p=image->blob->data+image->blob->offset;
2861 count=(ssize_t) MagickMin(length,(MagickSizeType) (image->blob->length-
2862 image->blob->offset));
2863 image->blob->offset+=count;
2864 if (count != (ssize_t) length)
2865 image->blob->eof=MagickTrue;
2866 (void) memcpy(q,p,(size_t) count);
2874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2878 + R e a d B l o b B y t e %
2882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2884 % ReadBlobByte() reads a single byte from the image file and returns it.
2886 % The format of the ReadBlobByte method is:
2888 % int ReadBlobByte(Image *image)
2890 % A description of each parameter follows.
2892 % o image: the image.
2895 MagickExport int ReadBlobByte(Image *image)
2897 register const unsigned char
2906 assert(image != (Image *) NULL);
2907 assert(image->signature == MagickSignature);
2908 p=ReadBlobStream(image,1,buffer,&count);
2915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2919 + R e a d B l o b D o u b l e %
2923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2925 % ReadBlobDouble() reads a double value as a 64-bit quantity in the byte-order
2926 % specified by the endian member of the image structure.
2928 % The format of the ReadBlobDouble method is:
2930 % double ReadBlobDouble(Image *image)
2932 % A description of each parameter follows.
2934 % o image: the image.
2937 MagickExport double ReadBlobDouble(Image *image)
2948 quantum.double_value=0.0;
2949 quantum.unsigned_value=ReadBlobLongLong(image);
2950 return(quantum.double_value);
2954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2958 + R e a d B l o b F l o a t %
2962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2964 % ReadBlobFloat() reads a float value as a 32-bit quantity in the byte-order
2965 % specified by the endian member of the image structure.
2967 % The format of the ReadBlobFloat method is:
2969 % float ReadBlobFloat(Image *image)
2971 % A description of each parameter follows.
2973 % o image: the image.
2976 MagickExport float ReadBlobFloat(Image *image)
2987 quantum.float_value=0.0;
2988 quantum.unsigned_value=ReadBlobLong(image);
2989 return(quantum.float_value);
2993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2997 + R e a d B l o b L o n g %
3001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3003 % ReadBlobLong() reads a ssize_t value as a 32-bit quantity in the byte-order
3004 % specified by the endian member of the image structure.
3006 % The format of the ReadBlobLong method is:
3008 % unsigned int ReadBlobLong(Image *image)
3010 % A description of each parameter follows.
3012 % o image: the image.
3015 MagickExport unsigned int ReadBlobLong(Image *image)
3017 register const unsigned char
3029 assert(image != (Image *) NULL);
3030 assert(image->signature == MagickSignature);
3032 p=ReadBlobStream(image,4,buffer,&count);
3035 if (image->endian == LSBEndian)
3037 value=(unsigned int) (*p++);
3038 value|=((unsigned int) (*p++)) << 8;
3039 value|=((unsigned int) (*p++)) << 16;
3040 value|=((unsigned int) (*p++)) << 24;
3043 value=((unsigned int) (*p++)) << 24;
3044 value|=((unsigned int) (*p++)) << 16;
3045 value|=((unsigned int) (*p++)) << 8;
3046 value|=((unsigned int) (*p++));
3051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3055 + R e a d B l o b L o n g L o n g %
3059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3061 % ReadBlobLongLong() reads a long long value as a 64-bit quantity in the
3062 % byte-order specified by the endian member of the image structure.
3064 % The format of the ReadBlobLongLong method is:
3066 % MagickSizeType ReadBlobLongLong(Image *image)
3068 % A description of each parameter follows.
3070 % o image: the image.
3073 MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3078 register const unsigned char
3087 assert(image != (Image *) NULL);
3088 assert(image->signature == MagickSignature);
3090 p=ReadBlobStream(image,8,buffer,&count);
3092 return(MagickULLConstant(0));
3093 if (image->endian == LSBEndian)
3095 value=(MagickSizeType) (*p++);
3096 value|=((MagickSizeType) (*p++)) << 8;
3097 value|=((MagickSizeType) (*p++)) << 16;
3098 value|=((MagickSizeType) (*p++)) << 24;
3099 value|=((MagickSizeType) (*p++)) << 32;
3100 value|=((MagickSizeType) (*p++)) << 40;
3101 value|=((MagickSizeType) (*p++)) << 48;
3102 value|=((MagickSizeType) (*p++)) << 56;
3103 return(value & MagickULLConstant(0xffffffffffffffff));
3105 value=((MagickSizeType) (*p++)) << 56;
3106 value|=((MagickSizeType) (*p++)) << 48;
3107 value|=((MagickSizeType) (*p++)) << 40;
3108 value|=((MagickSizeType) (*p++)) << 32;
3109 value|=((MagickSizeType) (*p++)) << 24;
3110 value|=((MagickSizeType) (*p++)) << 16;
3111 value|=((MagickSizeType) (*p++)) << 8;
3112 value|=((MagickSizeType) (*p++));
3113 return(value & MagickULLConstant(0xffffffffffffffff));
3117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3121 + R e a d B l o b S h o r t %
3125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3127 % ReadBlobShort() reads a short value as a 16-bit quantity in the byte-order
3128 % specified by the endian member of the image structure.
3130 % The format of the ReadBlobShort method is:
3132 % unsigned short ReadBlobShort(Image *image)
3134 % A description of each parameter follows.
3136 % o image: the image.
3139 MagickExport unsigned short ReadBlobShort(Image *image)
3141 register const unsigned char
3144 register unsigned int
3153 assert(image != (Image *) NULL);
3154 assert(image->signature == MagickSignature);
3156 p=ReadBlobStream(image,2,buffer,&count);
3158 return((unsigned short) 0U);
3159 if (image->endian == LSBEndian)
3161 value=(unsigned int) (*p++);
3162 value|=((unsigned int) (*p++)) << 8;
3163 return((unsigned short) (value & 0xffff));
3165 value=(unsigned int) ((*p++) << 8);
3166 value|=(unsigned int) (*p++);
3167 return((unsigned short) (value & 0xffff));
3171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3175 + R e a d B l o b L S B L o n g %
3179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3181 % ReadBlobLSBLong() reads a ssize_t value as a 32-bit quantity in
3182 % least-significant byte first order.
3184 % The format of the ReadBlobLSBLong method is:
3186 % unsigned int ReadBlobLSBLong(Image *image)
3188 % A description of each parameter follows.
3190 % o image: the image.
3193 MagickExport unsigned int ReadBlobLSBLong(Image *image)
3195 register const unsigned char
3198 register unsigned int
3207 assert(image != (Image *) NULL);
3208 assert(image->signature == MagickSignature);
3210 p=ReadBlobStream(image,4,buffer,&count);
3213 value=(unsigned int) (*p++);
3214 value|=((unsigned int) (*p++)) << 8;
3215 value|=((unsigned int) (*p++)) << 16;
3216 value|=((unsigned int) (*p++)) << 24;
3221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3225 + R e a d B l o b L S B S h o r t %
3229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3231 % ReadBlobLSBShort() reads a short value as a 16-bit quantity in
3232 % least-significant byte first order.
3234 % The format of the ReadBlobLSBShort method is:
3236 % unsigned short ReadBlobLSBShort(Image *image)
3238 % A description of each parameter follows.
3240 % o image: the image.
3243 MagickExport unsigned short ReadBlobLSBShort(Image *image)
3245 register const unsigned char
3248 register unsigned int
3257 assert(image != (Image *) NULL);
3258 assert(image->signature == MagickSignature);
3260 p=ReadBlobStream(image,2,buffer,&count);
3262 return((unsigned short) 0U);
3263 value=(unsigned int) (*p++);
3264 value|=((unsigned int) ((*p++)) << 8);
3265 return((unsigned short) (value & 0xffff));
3269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3273 + R e a d B l o b M S B L o n g %
3277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3279 % ReadBlobMSBLong() reads a ssize_t value as a 32-bit quantity in
3280 % most-significant byte first order.
3282 % The format of the ReadBlobMSBLong method is:
3284 % unsigned int ReadBlobMSBLong(Image *image)
3286 % A description of each parameter follows.
3288 % o image: the image.
3291 MagickExport unsigned int ReadBlobMSBLong(Image *image)
3293 register const unsigned char
3296 register unsigned int
3305 assert(image != (Image *) NULL);
3306 assert(image->signature == MagickSignature);
3308 p=ReadBlobStream(image,4,buffer,&count);
3311 value=((unsigned int) (*p++) << 24);
3312 value|=((unsigned int) (*p++) << 16);
3313 value|=((unsigned int) (*p++) << 8);
3314 value|=(unsigned int) (*p++);
3319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3323 + R e a d B l o b M S B L o n g L o n g %
3327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3329 % ReadBlobMSBLongLong() reads a ssize_t value as a 64-bit quantity in
3330 % most-significant byte first order.
3332 % The format of the ReadBlobMSBLongLong method is:
3334 % unsigned int ReadBlobMSBLongLong(Image *image)
3336 % A description of each parameter follows.
3338 % o image: the image.
3341 MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
3343 register const unsigned char
3346 register MagickSizeType
3355 assert(image != (Image *) NULL);
3356 assert(image->signature == MagickSignature);
3358 p=ReadBlobStream(image,8,buffer,&count);
3360 return(MagickULLConstant(0));
3361 value=((MagickSizeType) (*p++)) << 56;
3362 value|=((MagickSizeType) (*p++)) << 48;
3363 value|=((MagickSizeType) (*p++)) << 40;
3364 value|=((MagickSizeType) (*p++)) << 32;
3365 value|=((MagickSizeType) (*p++)) << 24;
3366 value|=((MagickSizeType) (*p++)) << 16;
3367 value|=((MagickSizeType) (*p++)) << 8;
3368 value|=((MagickSizeType) (*p++));
3369 return(value & MagickULLConstant(0xffffffffffffffff));
3373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3377 + R e a d B l o b M S B S h o r t %
3381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3383 % ReadBlobMSBShort() reads a short value as a 16-bit quantity in
3384 % most-significant byte first order.
3386 % The format of the ReadBlobMSBShort method is:
3388 % unsigned short ReadBlobMSBShort(Image *image)
3390 % A description of each parameter follows.
3392 % o image: the image.
3395 MagickExport unsigned short ReadBlobMSBShort(Image *image)
3397 register const unsigned char
3400 register unsigned int
3409 assert(image != (Image *) NULL);
3410 assert(image->signature == MagickSignature);
3412 p=ReadBlobStream(image,2,buffer,&count);
3414 return((unsigned short) 0U);
3415 value=(unsigned int) ((*p++) << 8);
3416 value|=(unsigned int) (*p++);
3417 return((unsigned short) (value & 0xffff));
3421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3425 + R e a d B l o b S t r i n g %
3429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3431 % ReadBlobString() reads characters from a blob or file until a newline
3432 % character is read or an end-of-file condition is encountered.
3434 % The format of the ReadBlobString method is:
3436 % char *ReadBlobString(Image *image,char *string)
3438 % A description of each parameter follows:
3440 % o image: the image.
3442 % o string: the address of a character buffer.
3445 MagickExport char *ReadBlobString(Image *image,char *string)
3447 register const unsigned char
3459 assert(image != (Image *) NULL);
3460 assert(image->signature == MagickSignature);
3461 for (i=0; i < (MaxTextExtent-1L); i++)
3463 p=ReadBlobStream(image,1,buffer,&count);
3467 return((char *) NULL);
3470 string[i]=(char) (*p);
3471 if ((string[i] == '\r') || (string[i] == '\n'))
3474 if (string[i] == '\r')
3475 (void) ReadBlobStream(image,1,buffer,&count);
3481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3485 + R e f e r e n c e B l o b %
3489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3491 % ReferenceBlob() increments the reference count associated with the pixel
3492 % blob returning a pointer to the blob.
3494 % The format of the ReferenceBlob method is:
3496 % BlobInfo ReferenceBlob(BlobInfo *blob_info)
3498 % A description of each parameter follows:
3500 % o blob_info: the blob_info.
3503 MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
3505 assert(blob != (BlobInfo *) NULL);
3506 assert(blob->signature == MagickSignature);
3507 if (blob->debug != MagickFalse)
3508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3509 LockSemaphoreInfo(blob->semaphore);
3510 blob->reference_count++;
3511 UnlockSemaphoreInfo(blob->semaphore);
3516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3526 % SeekBlob() sets the offset in bytes from the beginning of a blob or file
3527 % and returns the resulting offset.
3529 % The format of the SeekBlob method is:
3531 % MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
3534 % A description of each parameter follows:
3536 % o image: the image.
3538 % o offset: Specifies an integer representing the offset in bytes.
3540 % o whence: Specifies an integer representing how the offset is
3541 % treated relative to the beginning of the blob as follows:
3543 % SEEK_SET Set position equal to offset bytes.
3544 % SEEK_CUR Set position to current location plus offset.
3545 % SEEK_END Set position to EOF plus offset.
3548 MagickExport MagickOffsetType SeekBlob(Image *image,
3549 const MagickOffsetType offset,const int whence)
3551 assert(image != (Image *) NULL);
3552 assert(image->signature == MagickSignature);
3553 if (image->debug != MagickFalse)
3554 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3555 assert(image->blob != (BlobInfo *) NULL);
3556 assert(image->blob->type != UndefinedStream);
3557 switch (image->blob->type)
3559 case UndefinedStream:
3563 if (fseek(image->blob->file_info.file,offset,whence) < 0)
3565 image->blob->offset=TellBlob(image);
3568 case StandardStream:
3572 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3573 if (gzseek(image->blob->file_info.gzfile,(off_t) offset,whence) < 0)
3576 image->blob->offset=TellBlob(image);
3592 image->blob->offset=offset;
3597 if ((image->blob->offset+offset) < 0)
3599 image->blob->offset+=offset;
3604 if (((MagickOffsetType) image->blob->length+offset) < 0)
3606 image->blob->offset=image->blob->length+offset;
3610 if (image->blob->offset <= (MagickOffsetType)
3611 ((off_t) image->blob->length))
3612 image->blob->eof=MagickFalse;
3614 if (image->blob->mapped != MagickFalse)
3618 image->blob->extent=(size_t) (image->blob->offset+
3619 image->blob->quantum);
3620 image->blob->data=(unsigned char *) ResizeQuantumMemory(
3621 image->blob->data,image->blob->extent+1,
3622 sizeof(*image->blob->data));
3623 (void) SyncBlob(image);
3624 if (image->blob->data == (unsigned char *) NULL)
3626 (void) DetachBlob(image->blob);
3633 return(image->blob->offset);
3637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3641 + S e t B l o b E x e m p t %
3645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3647 % SetBlobExempt() sets the blob exempt status.
3649 % The format of the SetBlobExempt method is:
3651 % MagickBooleanType SetBlobExempt(const Image *image,
3652 % const MagickBooleanType exempt)
3654 % A description of each parameter follows:
3656 % o image: the image.
3658 % o exempt: Set to true if this blob is exempt from being closed.
3661 MagickPrivate void SetBlobExempt(Image *image,const MagickBooleanType exempt)
3663 assert(image != (const Image *) NULL);
3664 assert(image->signature == MagickSignature);
3665 if (image->debug != MagickFalse)
3666 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3667 image->blob->exempt=exempt;
3671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3675 + S e t B l o b E x t e n t %
3679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3681 % SetBlobExtent() ensures enough space is allocated for the blob. If the
3682 % method is successful, subsequent writes to bytes in the specified range are
3683 % guaranteed not to fail.
3685 % The format of the SetBlobExtent method is:
3687 % MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
3689 % A description of each parameter follows:
3691 % o image: the image.
3693 % o extent: the blob maximum extent.
3696 MagickPrivate MagickBooleanType SetBlobExtent(Image *image,
3697 const MagickSizeType extent)
3699 assert(image != (Image *) NULL);
3700 assert(image->signature == MagickSignature);
3701 if (image->debug != MagickFalse)
3702 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3703 assert(image->blob != (BlobInfo *) NULL);
3704 assert(image->blob->type != UndefinedStream);
3705 switch (image->blob->type)
3707 case UndefinedStream:
3711 if (extent != (MagickSizeType) ((off_t) extent))
3712 return(MagickFalse);
3713 #if !defined(MAGICKCORE_POSIX_FALLOCATE)
3714 return(MagickFalse);
3723 offset=TellBlob(image);
3724 status=posix_fallocate(fileno(image->blob->file_info.file),
3725 (off_t) offset,(off_t) (extent-offset));
3727 return(MagickFalse);
3732 case StandardStream:
3735 return(MagickFalse);
3737 return(MagickFalse);
3739 return(MagickFalse);
3742 if (image->blob->mapped != MagickFalse)
3744 if (image->blob->file_info.file == (FILE *) NULL)
3745 return(MagickFalse);
3746 (void) UnmapBlob(image->blob->data,image->blob->length);
3747 #if !defined(MAGICKCORE_POSIX_FALLOCATE)
3748 return(MagickFalse);
3757 offset=TellBlob(image);
3758 status=posix_fallocate(fileno(image->blob->file_info.file),
3759 (off_t) offset,(off_t) (extent-offset));
3761 return(MagickFalse);
3763 image->blob->data=(unsigned char*) MapBlob(fileno(
3764 image->blob->file_info.file),WriteMode,0,(size_t) extent);
3765 image->blob->extent=(size_t) extent;
3766 image->blob->length=(size_t) extent;
3767 (void) SyncBlob(image);
3771 if (extent != (MagickSizeType) ((size_t) extent))
3772 return(MagickFalse);
3773 image->blob->extent=(size_t) extent;
3774 image->blob->data=(unsigned char *) ResizeQuantumMemory(image->blob->data,
3775 image->blob->extent+1,sizeof(*image->blob->data));
3776 (void) SyncBlob(image);
3777 if (image->blob->data == (unsigned char *) NULL)
3779 (void) DetachBlob(image->blob);
3780 return(MagickFalse);
3789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3799 % SyncBlob() flushes the datastream if it is a file or synchronizes the data
3800 % attributes if it is an blob.
3802 % The format of the SyncBlob method is:
3804 % int SyncBlob(Image *image)
3806 % A description of each parameter follows:
3808 % o image: the image.
3811 static int SyncBlob(Image *image)
3816 assert(image != (Image *) NULL);
3817 assert(image->signature == MagickSignature);
3818 if (image->debug != MagickFalse)
3819 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3820 assert(image->blob != (BlobInfo *) NULL);
3821 assert(image->blob->type != UndefinedStream);
3823 switch (image->blob->type)
3825 case UndefinedStream:
3828 case StandardStream:
3831 status=fflush(image->blob->file_info.file);
3836 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3837 status=gzflush(image->blob->file_info.gzfile,Z_SYNC_FLUSH);
3843 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3844 status=BZ2_bzflush(image->blob->file_info.bzfile);
3852 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3853 if (image->blob->mapped != MagickFalse)
3854 status=msync(image->blob->data,image->blob->length,MS_SYNC);
3863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3873 % TellBlob() obtains the current value of the blob or file position.
3875 % The format of the TellBlob method is:
3877 % MagickOffsetType TellBlob(const Image *image)
3879 % A description of each parameter follows:
3881 % o image: the image.
3884 MagickExport MagickOffsetType TellBlob(const Image *image)
3889 assert(image != (Image *) NULL);
3890 assert(image->signature == MagickSignature);
3891 if (image->debug != MagickFalse)
3892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3893 assert(image->blob != (BlobInfo *) NULL);
3894 assert(image->blob->type != UndefinedStream);
3896 switch (image->blob->type)
3898 case UndefinedStream:
3902 offset=ftell(image->blob->file_info.file);
3905 case StandardStream:
3910 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3911 offset=(MagickOffsetType) gztell(image->blob->file_info.gzfile);
3921 offset=image->blob->offset;
3929 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3933 + U n m a p B l o b %
3937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3939 % UnmapBlob() deallocates the binary large object previously allocated with
3940 % the MapBlob method.
3942 % The format of the UnmapBlob method is:
3944 % MagickBooleanType UnmapBlob(void *map,const size_t length)
3946 % A description of each parameter follows:
3948 % o map: the address of the binary large object.
3950 % o length: the length of the binary large object.
3953 MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
3955 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3959 status=munmap(map,length);
3960 return(status == -1 ? MagickFalse : MagickTrue);
3964 return(MagickFalse);
3969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3973 + W r i t e B l o b %
3977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3979 % WriteBlob() writes data to a blob or image file. It returns the number of
3982 % The format of the WriteBlob method is:
3984 % ssize_t WriteBlob(Image *image,const size_t length,
3985 % const unsigned char *data)
3987 % A description of each parameter follows:
3989 % o image: the image.
3991 % o length: Specifies an integer representing the number of bytes to
3992 % write to the file.
3994 % o data: The address of the data to write to the blob or file.
3997 MagickExport ssize_t WriteBlob(Image *image,const size_t length,
3998 const unsigned char *data)
4003 register const unsigned char
4009 assert(image != (Image *) NULL);
4010 assert(image->signature == MagickSignature);
4011 assert(data != (const unsigned char *) NULL);
4012 assert(image->blob != (BlobInfo *) NULL);
4013 assert(image->blob->type != UndefinedStream);
4018 switch (image->blob->type)
4020 case UndefinedStream:
4023 case StandardStream:
4030 count=(ssize_t) fwrite((const char *) data,1,length,
4031 image->blob->file_info.file);
4036 c=putc((int) *p++,image->blob->file_info.file);
4043 c=putc((int) *p++,image->blob->file_info.file);
4055 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4060 count=(ssize_t) gzwrite(image->blob->file_info.gzfile,(void *) data,
4061 (unsigned int) length);
4066 c=gzputc(image->blob->file_info.gzfile,(int) *p++);
4073 c=gzputc(image->blob->file_info.gzfile,(int) *p++);
4086 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4087 count=(ssize_t) BZ2_bzwrite(image->blob->file_info.bzfile,(void *) data,
4094 count=(ssize_t) image->blob->stream(image,data,length);
4099 register unsigned char
4102 if ((image->blob->offset+(MagickOffsetType) length) >=
4103 (MagickOffsetType) image->blob->extent)
4105 if (image->blob->mapped != MagickFalse)
4107 image->blob->quantum<<=1;
4108 image->blob->extent+=length+image->blob->quantum;
4109 image->blob->data=(unsigned char *) ResizeQuantumMemory(
4110 image->blob->data,image->blob->extent+1,sizeof(*image->blob->data));
4111 (void) SyncBlob(image);
4112 if (image->blob->data == (unsigned char *) NULL)
4114 (void) DetachBlob(image->blob);
4118 q=image->blob->data+image->blob->offset;
4119 (void) memcpy(q,p,length);
4120 image->blob->offset+=length;
4121 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
4122 image->blob->length=(size_t) image->blob->offset;
4123 count=(ssize_t) length;
4130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4134 + W r i t e B l o b B y t e %
4138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4140 % WriteBlobByte() write an integer to a blob. It returns the number of bytes
4141 % written (either 0 or 1);
4143 % The format of the WriteBlobByte method is:
4145 % ssize_t WriteBlobByte(Image *image,const unsigned char value)
4147 % A description of each parameter follows.
4149 % o image: the image.
4151 % o value: Specifies the value to write.
4154 MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
4156 assert(image != (Image *) NULL);
4157 assert(image->signature == MagickSignature);
4158 return(WriteBlobStream(image,1,&value));
4162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4166 + W r i t e B l o b F l o a t %
4170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4172 % WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
4173 % specified by the endian member of the image structure.
4175 % The format of the WriteBlobFloat method is:
4177 % ssize_t WriteBlobFloat(Image *image,const float value)
4179 % A description of each parameter follows.
4181 % o image: the image.
4183 % o value: Specifies the value to write.
4186 MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
4197 quantum.unsigned_value=0U;
4198 quantum.float_value=value;
4199 return(WriteBlobLong(image,quantum.unsigned_value));
4203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4207 + W r i t e B l o b L o n g %
4211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4213 % WriteBlobLong() writes a ssize_t value as a 32-bit quantity in the byte-order
4214 % specified by the endian member of the image structure.
4216 % The format of the WriteBlobLong method is:
4218 % ssize_t WriteBlobLong(Image *image,const unsigned int value)
4220 % A description of each parameter follows.
4222 % o image: the image.
4224 % o value: Specifies the value to write.
4227 MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
4232 assert(image != (Image *) NULL);
4233 assert(image->signature == MagickSignature);
4234 if (image->endian == LSBEndian)
4236 buffer[0]=(unsigned char) value;
4237 buffer[1]=(unsigned char) (value >> 8);
4238 buffer[2]=(unsigned char) (value >> 16);
4239 buffer[3]=(unsigned char) (value >> 24);
4240 return(WriteBlobStream(image,4,buffer));
4242 buffer[0]=(unsigned char) (value >> 24);
4243 buffer[1]=(unsigned char) (value >> 16);
4244 buffer[2]=(unsigned char) (value >> 8);
4245 buffer[3]=(unsigned char) value;
4246 return(WriteBlobStream(image,4,buffer));
4250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4254 + W r i t e B l o b S h o r t %
4258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4260 % WriteBlobShort() writes a short value as a 16-bit quantity in the
4261 % byte-order specified by the endian member of the image structure.
4263 % The format of the WriteBlobShort method is:
4265 % ssize_t WriteBlobShort(Image *image,const unsigned short value)
4267 % A description of each parameter follows.
4269 % o image: the image.
4271 % o value: Specifies the value to write.
4274 MagickExport ssize_t WriteBlobShort(Image *image,const unsigned short value)
4279 assert(image != (Image *) NULL);
4280 assert(image->signature == MagickSignature);
4281 if (image->endian == LSBEndian)
4283 buffer[0]=(unsigned char) value;
4284 buffer[1]=(unsigned char) (value >> 8);
4285 return(WriteBlobStream(image,2,buffer));
4287 buffer[0]=(unsigned char) (value >> 8);
4288 buffer[1]=(unsigned char) value;
4289 return(WriteBlobStream(image,2,buffer));
4293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4297 + W r i t e B l o b L S B L o n g %
4301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4303 % WriteBlobLSBLong() writes a ssize_t value as a 32-bit quantity in
4304 % least-significant byte first order.
4306 % The format of the WriteBlobLSBLong method is:
4308 % ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4310 % A description of each parameter follows.
4312 % o image: the image.
4314 % o value: Specifies the value to write.
4317 MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4322 assert(image != (Image *) NULL);
4323 assert(image->signature == MagickSignature);
4324 buffer[0]=(unsigned char) value;
4325 buffer[1]=(unsigned char) (value >> 8);
4326 buffer[2]=(unsigned char) (value >> 16);
4327 buffer[3]=(unsigned char) (value >> 24);
4328 return(WriteBlobStream(image,4,buffer));
4332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4336 + W r i t e B l o b L S B S h o r t %
4340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4342 % WriteBlobLSBShort() writes a ssize_t value as a 16-bit quantity in
4343 % least-significant byte first order.
4345 % The format of the WriteBlobLSBShort method is:
4347 % ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4349 % A description of each parameter follows.
4351 % o image: the image.
4353 % o value: Specifies the value to write.
4356 MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4361 assert(image != (Image *) NULL);
4362 assert(image->signature == MagickSignature);
4363 buffer[0]=(unsigned char) value;
4364 buffer[1]=(unsigned char) (value >> 8);
4365 return(WriteBlobStream(image,2,buffer));
4369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4373 + W r i t e B l o b M S B L o n g %
4377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4379 % WriteBlobMSBLong() writes a ssize_t value as a 32-bit quantity in
4380 % most-significant byte first order.
4382 % The format of the WriteBlobMSBLong method is:
4384 % ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4386 % A description of each parameter follows.
4388 % o value: Specifies the value to write.
4390 % o image: the image.
4393 MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4398 assert(image != (Image *) NULL);
4399 assert(image->signature == MagickSignature);
4400 buffer[0]=(unsigned char) (value >> 24);
4401 buffer[1]=(unsigned char) (value >> 16);
4402 buffer[2]=(unsigned char) (value >> 8);
4403 buffer[3]=(unsigned char) value;
4404 return(WriteBlobStream(image,4,buffer));
4408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4412 + W r i t e B l o b M S B L o n g L o n g %
4416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4418 % WriteBlobMSBLongLong() writes a long long value as a 64-bit quantity in
4419 % most-significant byte first order.
4421 % The format of the WriteBlobMSBLongLong method is:
4423 % ssize_t WriteBlobMSBLongLong(Image *image,const MagickSizeType value)
4425 % A description of each parameter follows.
4427 % o value: Specifies the value to write.
4429 % o image: the image.
4432 MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
4433 const MagickSizeType value)
4438 assert(image != (Image *) NULL);
4439 assert(image->signature == MagickSignature);
4440 buffer[0]=(unsigned char) (value >> 56);
4441 buffer[1]=(unsigned char) (value >> 48);
4442 buffer[2]=(unsigned char) (value >> 40);
4443 buffer[3]=(unsigned char) (value >> 32);
4444 buffer[4]=(unsigned char) (value >> 24);
4445 buffer[5]=(unsigned char) (value >> 16);
4446 buffer[6]=(unsigned char) (value >> 8);
4447 buffer[7]=(unsigned char) value;
4448 return(WriteBlobStream(image,8,buffer));
4452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4456 + W r i t e B l o b M S B S h o r t %
4460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4462 % WriteBlobMSBShort() writes a ssize_t value as a 16-bit quantity in
4463 % most-significant byte first order.
4465 % The format of the WriteBlobMSBShort method is:
4467 % ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4469 % A description of each parameter follows.
4471 % o value: Specifies the value to write.
4473 % o file: Specifies the file to write the data to.
4476 MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4481 assert(image != (Image *) NULL);
4482 assert(image->signature == MagickSignature);
4483 buffer[0]=(unsigned char) (value >> 8);
4484 buffer[1]=(unsigned char) value;
4485 return(WriteBlobStream(image,2,buffer));
4489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4493 + W r i t e B l o b S t r i n g %
4497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4499 % WriteBlobString() write a string to a blob. It returns the number of
4500 % characters written.
4502 % The format of the WriteBlobString method is:
4504 % ssize_t WriteBlobString(Image *image,const char *string)
4506 % A description of each parameter follows.
4508 % o image: the image.
4510 % o string: Specifies the string to write.
4513 MagickExport ssize_t WriteBlobString(Image *image,const char *string)
4515 assert(image != (Image *) NULL);
4516 assert(image->signature == MagickSignature);
4517 assert(string != (const char *) NULL);
4518 return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));