2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % BBBB LLLLL OOO BBBB %
13 % MagickCore Binary Large OBjectS Methods %
20 % Copyright 1999-2011 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/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/client.h"
48 #include "MagickCore/constitute.h"
49 #include "MagickCore/delegate.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/locale_.h"
55 #include "MagickCore/log.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/policy.h"
59 #include "MagickCore/resource_.h"
60 #include "MagickCore/semaphore.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/string-private.h"
63 #include "MagickCore/token.h"
64 #include "MagickCore/utility.h"
65 #include "MagickCore/utility-private.h"
66 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO) && !defined(MAGICKCORE_WINDOWS_SUPPORT)
67 # include <sys/mman.h>
69 #if defined(MAGICKCORE_ZLIB_DELEGATE)
72 #if defined(MAGICKCORE_BZLIB_DELEGATE)
79 #define MagickMaxBlobExtent 65541
80 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
81 # define MAP_ANONYMOUS MAP_ANON
83 #if !defined(MAP_FAILED)
84 #define MAP_FAILED ((void *) -1)
91 #define _O_BINARY O_BINARY
149 Forward declarations.
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 + A t t a c h B l o b %
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 % AttachBlob() attaches a blob to the BlobInfo structure.
167 % The format of the AttachBlob method is:
169 % void AttachBlob(BlobInfo *blob_info,const void *blob,const size_t length)
171 % A description of each parameter follows:
173 % o blob_info: Specifies a pointer to a BlobInfo structure.
175 % o blob: the address of a character stream in one of the image formats
176 % understood by ImageMagick.
178 % o length: This size_t integer reflects the length in bytes of the blob.
181 MagickExport void AttachBlob(BlobInfo *blob_info,const void *blob,
184 assert(blob_info != (BlobInfo *) NULL);
185 if (blob_info->debug != MagickFalse)
186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
187 blob_info->length=length;
188 blob_info->extent=length;
189 blob_info->quantum=(size_t) MagickMaxBlobExtent;
191 blob_info->type=BlobStream;
192 blob_info->file=(FILE *) NULL;
193 blob_info->data=(unsigned char *) blob;
194 blob_info->mapped=MagickFalse;
198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 + B l o b T o F i l e %
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 % BlobToFile() writes a blob to a file. It returns MagickFalse if an error
209 % occurs otherwise MagickTrue.
211 % The format of the BlobToFile method is:
213 % MagickBooleanType BlobToFile(char *filename,const void *blob,
214 % const size_t length,ExceptionInfo *exception)
216 % A description of each parameter follows:
218 % o filename: Write the blob to this file.
220 % o blob: the address of a blob.
222 % o length: This length in bytes of the blob.
224 % o exception: return any errors or warnings in this structure.
228 static inline MagickSizeType MagickMin(const MagickSizeType x,
229 const MagickSizeType y)
236 MagickExport MagickBooleanType BlobToFile(char *filename,const void *blob,
237 const size_t length,ExceptionInfo *exception)
248 assert(filename != (const char *) NULL);
249 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
250 assert(blob != (const void *) NULL);
251 if (*filename == '\0')
252 file=AcquireUniqueFileResource(filename);
254 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
257 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
260 for (i=0; i < length; i+=count)
262 count=(ssize_t) write(file,(const char *) blob+i,(size_t) MagickMin(length-
263 i,(MagickSizeType) SSIZE_MAX));
272 if ((file == -1) || (i < length))
274 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % B l o b T o I m a g e %
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % BlobToImage() implements direct to memory image formats. It returns the
294 % The format of the BlobToImage method is:
296 % Image *BlobToImage(const ImageInfo *image_info,const void *blob,
297 % const size_t length,ExceptionInfo *exception)
299 % A description of each parameter follows:
301 % o image_info: the image info.
303 % o blob: the address of a character stream in one of the image formats
304 % understood by ImageMagick.
306 % o length: This size_t integer reflects the length in bytes of the blob.
308 % o exception: return any errors or warnings in this structure.
311 MagickExport Image *BlobToImage(const ImageInfo *image_info,const void *blob,
312 const size_t length,ExceptionInfo *exception)
327 assert(image_info != (ImageInfo *) NULL);
328 assert(image_info->signature == MagickSignature);
329 if (image_info->debug != MagickFalse)
330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
331 image_info->filename);
332 assert(exception != (ExceptionInfo *) NULL);
333 if ((blob == (const void *) NULL) || (length == 0))
335 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
336 "ZeroLengthBlobNotPermitted","`%s'",image_info->filename);
337 return((Image *) NULL);
339 blob_info=CloneImageInfo(image_info);
340 blob_info->blob=(void *) blob;
341 blob_info->length=length;
342 if (*blob_info->magick == '\0')
343 (void) SetImageInfo(blob_info,0,exception);
344 magick_info=GetMagickInfo(blob_info->magick,exception);
345 if (magick_info == (const MagickInfo *) NULL)
347 blob_info=DestroyImageInfo(blob_info);
348 (void) ThrowMagickException(exception,GetMagickModule(),
349 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
350 image_info->filename);
351 return((Image *) NULL);
353 if (GetMagickBlobSupport(magick_info) != MagickFalse)
356 Native blob support for this image format.
358 (void) CopyMagickString(blob_info->filename,image_info->filename,
360 (void) CopyMagickString(blob_info->magick,image_info->magick,
362 image=ReadImage(blob_info,exception);
363 if (image != (Image *) NULL)
364 (void) DetachBlob(image->blob);
365 blob_info=DestroyImageInfo(blob_info);
369 Write blob to a temporary file on disk.
371 blob_info->blob=(void *) NULL;
373 *blob_info->filename='\0';
374 status=BlobToFile(blob_info->filename,blob,length,exception);
375 if (status == MagickFalse)
377 (void) RelinquishUniqueFileResource(blob_info->filename);
378 blob_info=DestroyImageInfo(blob_info);
379 return((Image *) NULL);
381 clone_info=CloneImageInfo(blob_info);
382 (void) FormatLocaleString(clone_info->filename,MaxTextExtent,"%s:%s",
383 blob_info->magick,blob_info->filename);
384 image=ReadImage(clone_info,exception);
385 clone_info=DestroyImageInfo(clone_info);
386 (void) RelinquishUniqueFileResource(blob_info->filename);
387 blob_info=DestroyImageInfo(blob_info);
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 + C l o n e B l o b I n f o %
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 % CloneBlobInfo() makes a duplicate of the given blob info structure, or if
403 % blob info is NULL, a new one.
405 % The format of the CloneBlobInfo method is:
407 % BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
409 % A description of each parameter follows:
411 % o blob_info: the blob info.
414 MagickExport BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
419 clone_info=(BlobInfo *) AcquireMagickMemory(sizeof(*clone_info));
420 if (clone_info == (BlobInfo *) NULL)
421 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
422 GetBlobInfo(clone_info);
423 if (blob_info == (BlobInfo *) NULL)
425 clone_info->length=blob_info->length;
426 clone_info->extent=blob_info->extent;
427 clone_info->synchronize=blob_info->synchronize;
428 clone_info->quantum=blob_info->quantum;
429 clone_info->mapped=blob_info->mapped;
430 clone_info->eof=blob_info->eof;
431 clone_info->offset=blob_info->offset;
432 clone_info->size=blob_info->size;
433 clone_info->exempt=blob_info->exempt;
434 clone_info->status=blob_info->status;
435 clone_info->temporary=blob_info->temporary;
436 clone_info->type=blob_info->type;
437 clone_info->file=blob_info->file;
438 clone_info->properties=blob_info->properties;
439 clone_info->stream=blob_info->stream;
440 clone_info->data=blob_info->data;
441 clone_info->debug=IsEventLogging();
442 clone_info->reference_count=1;
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 + C l o s e B l o b %
455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 % CloseBlob() closes a stream associated with the image.
459 % The format of the CloseBlob method is:
461 % MagickBooleanType CloseBlob(Image *image)
463 % A description of each parameter follows:
465 % o image: the image.
468 MagickExport MagickBooleanType CloseBlob(Image *image)
476 assert(image != (Image *) NULL);
477 assert(image->signature == MagickSignature);
478 if (image->debug != MagickFalse)
479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
480 assert(image->blob != (BlobInfo *) NULL);
481 if (image->blob->type == UndefinedStream)
483 if (image->blob->synchronize != MagickFalse)
485 image->blob->size=GetBlobSize(image);
486 image->extent=image->blob->size;
487 image->blob->eof=MagickFalse;
488 if (image->blob->exempt != MagickFalse)
490 image->blob->type=UndefinedStream;
494 switch (image->blob->type)
496 case UndefinedStream:
502 status=ferror(image->blob->file);
507 #if defined(MAGICKCORE_ZLIB_DELEGATE)
508 (void) gzerror(image->blob->file,&status);
514 #if defined(MAGICKCORE_BZLIB_DELEGATE)
515 (void) BZ2_bzerror((BZFILE *) image->blob->file,&status);
523 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
524 switch (image->blob->type)
526 case UndefinedStream:
531 if (image->blob->synchronize != MagickFalse)
533 status=fflush(image->blob->file);
534 status=fsync(fileno(image->blob->file));
536 status=fclose(image->blob->file);
541 #if defined(MAGICKCORE_HAVE_PCLOSE)
542 status=pclose(image->blob->file);
548 #if defined(MAGICKCORE_ZLIB_DELEGATE)
549 status=gzclose(image->blob->file);
555 #if defined(MAGICKCORE_BZLIB_DELEGATE)
556 BZ2_bzclose((BZFILE *) image->blob->file);
564 if (image->blob->file != (FILE *) NULL)
566 if (image->blob->synchronize != MagickFalse)
567 (void) fsync(fileno(image->blob->file));
568 status=fclose(image->blob->file);
573 (void) DetachBlob(image->blob);
574 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
575 return(image->blob->status);
579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583 + D e s t r o y B l o b %
587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
589 % DestroyBlob() deallocates memory associated with a blob.
591 % The format of the DestroyBlob method is:
593 % void DestroyBlob(Image *image)
595 % A description of each parameter follows:
597 % o image: the image.
600 MagickExport void DestroyBlob(Image *image)
605 assert(image != (Image *) NULL);
606 assert(image->signature == MagickSignature);
607 if (image->debug != MagickFalse)
608 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
609 assert(image->blob != (BlobInfo *) NULL);
610 assert(image->blob->signature == MagickSignature);
612 LockSemaphoreInfo(image->blob->semaphore);
613 image->blob->reference_count--;
614 assert(image->blob->reference_count >= 0);
615 if (image->blob->reference_count == 0)
617 UnlockSemaphoreInfo(image->blob->semaphore);
618 if (destroy == MagickFalse)
620 (void) CloseBlob(image);
621 if (image->blob->mapped != MagickFalse)
622 (void) UnmapBlob(image->blob->data,image->blob->length);
623 if (image->blob->semaphore != (SemaphoreInfo *) NULL)
624 DestroySemaphoreInfo(&image->blob->semaphore);
625 image->blob->signature=(~MagickSignature);
626 image->blob=(BlobInfo *) RelinquishMagickMemory(image->blob);
630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 + D e t a c h B l o b %
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % DetachBlob() detaches a blob from the BlobInfo structure.
642 % The format of the DetachBlob method is:
644 % unsigned char *DetachBlob(BlobInfo *blob_info)
646 % A description of each parameter follows:
648 % o blob_info: Specifies a pointer to a BlobInfo structure.
651 MagickExport unsigned char *DetachBlob(BlobInfo *blob_info)
656 assert(blob_info != (BlobInfo *) NULL);
657 if (blob_info->debug != MagickFalse)
658 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
659 if (blob_info->mapped != MagickFalse)
660 (void) UnmapBlob(blob_info->data,blob_info->length);
661 blob_info->mapped=MagickFalse;
664 blob_info->eof=MagickFalse;
665 blob_info->exempt=MagickFalse;
666 blob_info->type=UndefinedStream;
667 blob_info->file=(FILE *) NULL;
668 data=blob_info->data;
669 blob_info->data=(unsigned char *) NULL;
670 blob_info->stream=(StreamHandler) NULL;
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 + D i s c a r d B l o b B y t e s %
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % DiscardBlobBytes() discards bytes in a blob.
687 % The format of the DiscardBlobBytes method is:
689 % MagickBooleanType DiscardBlobBytes(Image *image,const size_t length)
691 % A description of each parameter follows.
693 % o image: the image.
695 % o length: the number of bytes to skip.
699 static inline const unsigned char *ReadBlobStream(Image *image,
700 const size_t length,unsigned char *data,ssize_t *count)
702 assert(count != (ssize_t *) NULL);
703 assert(image->blob != (BlobInfo *) NULL);
704 if (image->blob->type != BlobStream)
706 *count=ReadBlob(image,length,data);
709 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
712 image->blob->eof=MagickTrue;
715 data=image->blob->data+image->blob->offset;
716 *count=(ssize_t) MagickMin(length,(MagickSizeType) (image->blob->length-
717 image->blob->offset));
718 image->blob->offset+=(*count);
719 if (*count != (ssize_t) length)
720 image->blob->eof=MagickTrue;
724 MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
725 const MagickSizeType length)
727 register MagickOffsetType
739 assert(image != (Image *) NULL);
740 assert(image->signature == MagickSignature);
742 for (i=0; i < (MagickOffsetType) length; i+=count)
744 quantum=(size_t) MagickMin(length-i,sizeof(buffer));
745 (void) ReadBlobStream(image,quantum,buffer,&count);
753 return(i < (MagickOffsetType) length ? MagickFalse : MagickTrue);
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 + D u p l i c a t e s B l o b %
765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767 % DuplicateBlob() duplicates a blob descriptor.
769 % The format of the DuplicateBlob method is:
771 % void DuplicateBlob(Image *image,const Image *duplicate)
773 % A description of each parameter follows:
775 % o image: the image.
777 % o duplicate: the duplicate image.
780 MagickExport void DuplicateBlob(Image *image,const Image *duplicate)
782 assert(image != (Image *) NULL);
783 assert(image->signature == MagickSignature);
784 if (image->debug != MagickFalse)
785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
786 assert(duplicate != (Image *) NULL);
787 assert(duplicate->signature == MagickSignature);
789 image->blob=ReferenceBlob(duplicate->blob);
793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803 % EOFBlob() returns a non-zero value when EOF has been detected reading from
806 % The format of the EOFBlob method is:
808 % int EOFBlob(const Image *image)
810 % A description of each parameter follows:
812 % o image: the image.
815 MagickExport int EOFBlob(const Image *image)
817 assert(image != (Image *) NULL);
818 assert(image->signature == MagickSignature);
819 if (image->debug != MagickFalse)
820 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
821 assert(image->blob != (BlobInfo *) NULL);
822 assert(image->blob->type != UndefinedStream);
823 switch (image->blob->type)
825 case UndefinedStream:
831 image->blob->eof=feof(image->blob->file) != 0 ? MagickTrue : MagickFalse;
836 image->blob->eof=MagickFalse;
841 #if defined(MAGICKCORE_BZLIB_DELEGATE)
846 (void) BZ2_bzerror((BZFILE *) image->blob->file,&status);
847 image->blob->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
853 image->blob->eof=MagickFalse;
859 return((int) image->blob->eof);
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 + F i l e T o B l o b %
871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873 % FileToBlob() returns the contents of a file as a buffer terminated with
874 % the '\0' character. The length of the buffer (not including the extra
875 % terminating '\0' character) is returned via the 'length' parameter. Free
876 % the buffer with RelinquishMagickMemory().
878 % The format of the FileToBlob method is:
880 % unsigned char *FileToBlob(const char *filename,const size_t extent,
881 % size_t *length,ExceptionInfo *exception)
883 % A description of each parameter follows:
885 % o blob: FileToBlob() returns the contents of a file as a blob. If
886 % an error occurs NULL is returned.
888 % o filename: the filename.
890 % o extent: The maximum length of the blob.
892 % o length: On return, this reflects the actual length of the blob.
894 % o exception: return any errors or warnings in this structure.
897 MagickExport unsigned char *FileToBlob(const char *filename,const size_t extent,
898 size_t *length,ExceptionInfo *exception)
918 assert(filename != (const char *) NULL);
919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
920 assert(exception != (ExceptionInfo *) NULL);
923 if (LocaleCompare(filename,"-") != 0)
924 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
927 ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
928 return((unsigned char *) NULL);
930 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
932 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
941 Stream is not seekable.
943 quantum=(size_t) MagickMaxBufferExtent;
944 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
945 quantum=(size_t) MagickMin((MagickSizeType) file_info.st_size,
946 MagickMaxBufferExtent);
947 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
948 for (i=0; blob != (unsigned char *) NULL; i+=count)
950 count=(ssize_t) read(file,blob+i,quantum);
957 if (~(1UL*i) < (quantum+1))
959 blob=(unsigned char *) RelinquishMagickMemory(blob);
962 blob=(unsigned char *) ResizeQuantumMemory(blob,i+quantum+1,
964 if ((size_t) (i+count) >= extent)
967 if (LocaleCompare(filename,"-") != 0)
969 if (blob == (unsigned char *) NULL)
971 (void) ThrowMagickException(exception,GetMagickModule(),
972 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
973 return((unsigned char *) NULL);
977 blob=(unsigned char *) RelinquishMagickMemory(blob);
978 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
979 return((unsigned char *) NULL);
981 *length=(size_t) MagickMin(i+count,extent);
985 *length=(size_t) MagickMin((MagickSizeType) offset,extent);
986 blob=(unsigned char *) NULL;
987 if (~(*length) >= (MaxTextExtent-1))
988 blob=(unsigned char *) AcquireQuantumMemory(*length+MaxTextExtent,
990 if (blob == (unsigned char *) NULL)
993 (void) ThrowMagickException(exception,GetMagickModule(),
994 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
995 return((unsigned char *) NULL);
997 map=MapBlob(file,ReadMode,0,*length);
998 if (map != (unsigned char *) NULL)
1000 (void) memcpy(blob,map,*length);
1001 (void) UnmapBlob(map,*length);
1005 (void) lseek(file,0,SEEK_SET);
1006 for (i=0; i < *length; i+=count)
1008 count=(ssize_t) read(file,blob+i,(size_t) MagickMin(*length-i,
1009 (MagickSizeType) SSIZE_MAX));
1020 blob=(unsigned char *) RelinquishMagickMemory(blob);
1021 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1022 return((unsigned char *) NULL);
1026 if (LocaleCompare(filename,"-") != 0)
1030 blob=(unsigned char *) RelinquishMagickMemory(blob);
1031 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1041 % F i l e T o I m a g e %
1045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047 % FileToImage() write the contents of a file to an image.
1049 % The format of the FileToImage method is:
1051 % MagickBooleanType FileToImage(Image *,const char *filename)
1053 % A description of each parameter follows:
1055 % o image: the image.
1057 % o filename: the filename.
1061 static inline ssize_t WriteBlobStream(Image *image,const size_t length,
1062 const unsigned char *data)
1067 register unsigned char
1070 assert(image->blob != (BlobInfo *) NULL);
1071 if (image->blob->type != BlobStream)
1072 return(WriteBlob(image,length,data));
1073 assert(image->blob->type != UndefinedStream);
1074 assert(data != (void *) NULL);
1075 extent=(MagickSizeType) (image->blob->offset+(MagickOffsetType) length);
1076 if (extent >= image->blob->extent)
1078 image->blob->quantum<<=1;
1079 extent=image->blob->extent+image->blob->quantum+length;
1080 if (SetBlobExtent(image,extent) == MagickFalse)
1083 q=image->blob->data+image->blob->offset;
1084 (void) memcpy(q,data,length);
1085 image->blob->offset+=length;
1086 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
1087 image->blob->length=(size_t) image->blob->offset;
1088 return((ssize_t) length);
1091 MagickExport MagickBooleanType FileToImage(Image *image,const char *filename)
1109 assert(image != (const Image *) NULL);
1110 assert(image->signature == MagickSignature);
1111 assert(filename != (const char *) NULL);
1112 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1113 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1116 ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob",
1118 return(MagickFalse);
1120 quantum=(size_t) MagickMaxBufferExtent;
1121 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1122 quantum=(size_t) MagickMin(file_info.st_size,MagickMaxBufferExtent);
1123 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1124 if (blob == (unsigned char *) NULL)
1126 ThrowFileException(&image->exception,ResourceLimitError,
1127 "MemoryAllocationFailed",filename);
1128 return(MagickFalse);
1132 count=(ssize_t) read(file,blob,quantum);
1139 length=(size_t) count;
1140 count=WriteBlobStream(image,length,blob);
1141 if (count != (ssize_t) length)
1143 ThrowFileException(&image->exception,BlobError,"UnableToWriteBlob",
1150 ThrowFileException(&image->exception,BlobError,"UnableToWriteBlob",
1152 blob=(unsigned char *) RelinquishMagickMemory(blob);
1157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1161 + G e t B l o b E r r o r %
1165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1167 % GetBlobError() returns MagickTrue if the blob associated with the specified
1168 % image encountered an error.
1170 % The format of the GetBlobError method is:
1172 % MagickBooleanType GetBlobError(const Image *image)
1174 % A description of each parameter follows:
1176 % o image: the image.
1179 MagickPrivate MagickBooleanType GetBlobError(const Image *image)
1181 assert(image != (const Image *) NULL);
1182 assert(image->signature == MagickSignature);
1183 if (image->debug != MagickFalse)
1184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1185 return(image->blob->status);
1189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193 + G e t B l o b F i l e H a n d l e %
1197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1199 % GetBlobFileHandle() returns the file handle associated with the image blob.
1201 % The format of the GetBlobFile method is:
1203 % FILE *GetBlobFileHandle(const Image *image)
1205 % A description of each parameter follows:
1207 % o image: the image.
1210 MagickExport FILE *GetBlobFileHandle(const Image *image)
1212 assert(image != (const Image *) NULL);
1213 assert(image->signature == MagickSignature);
1214 return(image->blob->file);
1218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1222 + G e t B l o b I n f o %
1226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1228 % GetBlobInfo() initializes the BlobInfo structure.
1230 % The format of the GetBlobInfo method is:
1232 % void GetBlobInfo(BlobInfo *blob_info)
1234 % A description of each parameter follows:
1236 % o blob_info: Specifies a pointer to a BlobInfo structure.
1239 MagickPrivate void GetBlobInfo(BlobInfo *blob_info)
1241 assert(blob_info != (BlobInfo *) NULL);
1242 (void) ResetMagickMemory(blob_info,0,sizeof(*blob_info));
1243 blob_info->type=UndefinedStream;
1244 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1245 blob_info->properties.st_mtime=time((time_t *) NULL);
1246 blob_info->properties.st_ctime=time((time_t *) NULL);
1247 blob_info->debug=IsEventLogging();
1248 blob_info->reference_count=1;
1249 blob_info->semaphore=AllocateSemaphoreInfo();
1250 blob_info->signature=MagickSignature;
1254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1258 % G e t B l o b P r o p e r t i e s %
1262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1264 % GetBlobProperties() returns information about an image blob.
1266 % The format of the GetBlobProperties method is:
1268 % const struct stat *GetBlobProperties(const Image *image)
1270 % A description of each parameter follows:
1272 % o image: the image.
1275 MagickPrivate const struct stat *GetBlobProperties(const Image *image)
1277 assert(image != (Image *) NULL);
1278 assert(image->signature == MagickSignature);
1279 if (image->debug != MagickFalse)
1280 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1281 return(&image->blob->properties);
1285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1289 + G e t B l o b S i z e %
1293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1295 % GetBlobSize() returns the current length of the image file or blob; zero is
1296 % returned if the size cannot be determined.
1298 % The format of the GetBlobSize method is:
1300 % MagickSizeType GetBlobSize(const Image *image)
1302 % A description of each parameter follows:
1304 % o image: the image.
1307 MagickExport MagickSizeType GetBlobSize(const Image *image)
1312 assert(image != (Image *) NULL);
1313 assert(image->signature == MagickSignature);
1314 if (image->debug != MagickFalse)
1315 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1316 assert(image->blob != (BlobInfo *) NULL);
1318 switch (image->blob->type)
1320 case UndefinedStream:
1322 extent=image->blob->size;
1327 if (fstat(fileno(image->blob->file),&image->blob->properties) == 0)
1328 extent=(MagickSizeType) image->blob->properties.st_size;
1331 case StandardStream:
1334 extent=image->blob->size;
1343 status=GetPathAttributes(image->filename,&image->blob->properties);
1344 if (status != MagickFalse)
1345 extent=(MagickSizeType) image->blob->properties.st_size;
1352 extent=(MagickSizeType) image->blob->length;
1360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364 + G e t B l o b S t r e a m D a t a %
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % GetBlobStreamData() returns the stream data for the image.
1372 % The format of the GetBlobStreamData method is:
1374 % unsigned char *GetBlobStreamData(const Image *image)
1376 % A description of each parameter follows:
1378 % o image: the image.
1381 MagickExport unsigned char *GetBlobStreamData(const Image *image)
1383 assert(image != (const Image *) NULL);
1384 assert(image->signature == MagickSignature);
1385 return(image->blob->data);
1389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 + G e t B l o b S t r e a m H a n d l e r %
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1399 % GetBlobStreamHandler() returns the stream handler for the image.
1401 % The format of the GetBlobStreamHandler method is:
1403 % StreamHandler GetBlobStreamHandler(const Image *image)
1405 % A description of each parameter follows:
1407 % o image: the image.
1410 MagickPrivate StreamHandler GetBlobStreamHandler(const Image *image)
1412 assert(image != (const Image *) NULL);
1413 assert(image->signature == MagickSignature);
1414 if (image->debug != MagickFalse)
1415 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1416 return(image->blob->stream);
1420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1424 % I m a g e T o B l o b %
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430 % ImageToBlob() implements direct to memory image formats. It returns the
1431 % image as a formatted blob and its length. The magick member of the Image
1432 % structure determines the format of the returned blob (GIF, JPEG, PNG,
1433 % etc.). This method is the equivalent of WriteImage(), but writes the
1434 % formatted "file" to a memory buffer rather than to an actual file.
1436 % The format of the ImageToBlob method is:
1438 % unsigned char *ImageToBlob(const ImageInfo *image_info,Image *image,
1439 % size_t *length,ExceptionInfo *exception)
1441 % A description of each parameter follows:
1443 % o image_info: the image info.
1445 % o image: the image.
1447 % o length: This pointer to a size_t integer sets the initial length of the
1448 % blob. On return, it reflects the actual length of the blob.
1450 % o exception: return any errors or warnings in this structure.
1453 MagickExport unsigned char *ImageToBlob(const ImageInfo *image_info,
1454 Image *image,size_t *length,ExceptionInfo *exception)
1468 assert(image_info != (const ImageInfo *) NULL);
1469 assert(image_info->signature == MagickSignature);
1470 if (image_info->debug != MagickFalse)
1471 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1472 image_info->filename);
1473 assert(image != (Image *) NULL);
1474 assert(image->signature == MagickSignature);
1475 assert(exception != (ExceptionInfo *) NULL);
1477 blob=(unsigned char *) NULL;
1478 blob_info=CloneImageInfo(image_info);
1479 blob_info->adjoin=MagickFalse;
1480 (void) SetImageInfo(blob_info,1,exception);
1481 if (*blob_info->magick != '\0')
1482 (void) CopyMagickString(image->magick,blob_info->magick,MaxTextExtent);
1483 magick_info=GetMagickInfo(image->magick,exception);
1484 if (magick_info == (const MagickInfo *) NULL)
1486 (void) ThrowMagickException(exception,GetMagickModule(),
1487 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
1491 (void) CopyMagickString(blob_info->magick,image->magick,MaxTextExtent);
1492 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1495 Native blob support for this image format.
1497 blob_info->length=0;
1498 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1499 sizeof(unsigned char));
1500 if (blob_info->blob == (void *) NULL)
1501 (void) ThrowMagickException(exception,GetMagickModule(),
1502 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1505 (void) CloseBlob(image);
1506 image->blob->exempt=MagickTrue;
1507 *image->filename='\0';
1508 status=WriteImage(blob_info,image,exception);
1509 if ((status != MagickFalse) && (image->blob->length != 0))
1511 *length=image->blob->length;
1512 blob=DetachBlob(image->blob);
1513 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1521 unique[MaxTextExtent];
1527 Write file to disk in blob image format.
1529 file=AcquireUniqueFileResource(unique);
1532 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
1533 image_info->filename);
1537 blob_info->file=fdopen(file,"wb");
1538 if (blob_info->file != (FILE *) NULL)
1540 (void) FormatLocaleString(image->filename,MaxTextExtent,"%s:%s",
1541 image->magick,unique);
1542 status=WriteImage(blob_info,image,exception);
1543 (void) fclose(blob_info->file);
1544 if (status != MagickFalse)
1545 blob=FileToBlob(image->filename,~0UL,length,exception);
1547 (void) RelinquishUniqueFileResource(unique);
1550 blob_info=DestroyImageInfo(blob_info);
1555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1559 % I m a g e T o F i l e %
1563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1565 % ImageToFile() writes an image to a file. It returns MagickFalse if an error
1566 % occurs otherwise MagickTrue.
1568 % The format of the ImageToFile method is:
1570 % MagickBooleanType ImageToFile(Image *image,char *filename,
1571 % ExceptionInfo *exception)
1573 % A description of each parameter follows:
1575 % o image: the image.
1577 % o filename: Write the image to this file.
1579 % o exception: return any errors or warnings in this structure.
1582 MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
1583 ExceptionInfo *exception)
1588 register const unsigned char
1607 assert(image != (Image *) NULL);
1608 assert(image->signature == MagickSignature);
1609 assert(image->blob != (BlobInfo *) NULL);
1610 assert(image->blob->type != UndefinedStream);
1611 if (image->debug != MagickFalse)
1612 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1613 assert(filename != (const char *) NULL);
1614 if (*filename == '\0')
1615 file=AcquireUniqueFileResource(filename);
1617 if (LocaleCompare(filename,"-") == 0)
1618 file=fileno(stdout);
1620 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1623 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1624 return(MagickFalse);
1626 quantum=(size_t) MagickMaxBufferExtent;
1627 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1628 quantum=(size_t) MagickMin((MagickSizeType) file_info.st_size,
1629 MagickMaxBufferExtent);
1630 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1631 if (buffer == (unsigned char *) NULL)
1634 (void) ThrowMagickException(exception,GetMagickModule(),
1635 ResourceLimitError,"MemoryAllocationError","`%s'",filename);
1636 return(MagickFalse);
1639 p=ReadBlobStream(image,quantum,buffer,&count);
1640 for (i=0; count > 0; p=ReadBlobStream(image,quantum,buffer,&count))
1642 length=(size_t) count;
1643 for (i=0; i < length; i+=count)
1645 count=write(file,p+i,(size_t) (length-i));
1656 if (LocaleCompare(filename,"-") != 0)
1658 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1659 if ((file == -1) || (i < length))
1661 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1662 return(MagickFalse);
1668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1672 % I m a g e s T o B l o b %
1676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1678 % ImagesToBlob() implements direct to memory image formats. It returns the
1679 % image sequence as a blob and its length. The magick member of the ImageInfo
1680 % structure determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1682 % Note, some image formats do not permit multiple images to the same image
1683 % stream (e.g. JPEG). in this instance, just the first image of the
1684 % sequence is returned as a blob.
1686 % The format of the ImagesToBlob method is:
1688 % unsigned char *ImagesToBlob(const ImageInfo *image_info,Image *images,
1689 % size_t *length,ExceptionInfo *exception)
1691 % A description of each parameter follows:
1693 % o image_info: the image info.
1695 % o images: the image list.
1697 % o length: This pointer to a size_t integer sets the initial length of the
1698 % blob. On return, it reflects the actual length of the blob.
1700 % o exception: return any errors or warnings in this structure.
1703 MagickExport unsigned char *ImagesToBlob(const ImageInfo *image_info,
1704 Image *images,size_t *length,ExceptionInfo *exception)
1718 assert(image_info != (const ImageInfo *) NULL);
1719 assert(image_info->signature == MagickSignature);
1720 if (image_info->debug != MagickFalse)
1721 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1722 image_info->filename);
1723 assert(images != (Image *) NULL);
1724 assert(images->signature == MagickSignature);
1725 assert(exception != (ExceptionInfo *) NULL);
1727 blob=(unsigned char *) NULL;
1728 blob_info=CloneImageInfo(image_info);
1729 (void) SetImageInfo(blob_info,(unsigned int) GetImageListLength(images),
1731 if (*blob_info->magick != '\0')
1732 (void) CopyMagickString(images->magick,blob_info->magick,MaxTextExtent);
1733 if (blob_info->adjoin == MagickFalse)
1735 blob_info=DestroyImageInfo(blob_info);
1736 return(ImageToBlob(image_info,images,length,exception));
1738 magick_info=GetMagickInfo(images->magick,exception);
1739 if (magick_info == (const MagickInfo *) NULL)
1741 (void) ThrowMagickException(exception,GetMagickModule(),
1742 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
1746 (void) CopyMagickString(blob_info->magick,images->magick,MaxTextExtent);
1747 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1750 Native blob support for this images format.
1752 blob_info->length=0;
1753 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1754 sizeof(unsigned char));
1755 if (blob_info->blob == (void *) NULL)
1756 (void) ThrowMagickException(exception,GetMagickModule(),
1757 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
1760 images->blob->exempt=MagickTrue;
1761 *images->filename='\0';
1762 status=WriteImages(blob_info,images,images->filename,exception);
1763 if ((status == MagickFalse) || (images->blob->length == 0))
1764 InheritException(exception,&images->exception);
1767 *length=images->blob->length;
1768 blob=DetachBlob(images->blob);
1769 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1777 filename[MaxTextExtent],
1778 unique[MaxTextExtent];
1784 Write file to disk in blob images format.
1786 file=AcquireUniqueFileResource(unique);
1789 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
1790 image_info->filename);
1794 blob_info->file=fdopen(file,"wb");
1795 if (blob_info->file != (FILE *) NULL)
1797 (void) FormatLocaleString(filename,MaxTextExtent,"%s:%s",
1798 images->magick,unique);
1799 status=WriteImages(blob_info,images,filename,exception);
1800 (void) fclose(blob_info->file);
1801 if (status == MagickFalse)
1802 InheritException(exception,&images->exception);
1804 blob=FileToBlob(images->filename,~0UL,length,exception);
1806 (void) RelinquishUniqueFileResource(unique);
1809 blob_info=DestroyImageInfo(blob_info);
1813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1817 % I n j e c t I m a g e B l o b %
1821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1823 % InjectImageBlob() injects the image with a copy of itself in the specified
1824 % format (e.g. inject JPEG into a PDF image).
1826 % The format of the InjectImageBlob method is:
1828 % MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1829 % Image *image,Image *inject_image,const char *format,
1830 % ExceptionInfo *exception)
1832 % A description of each parameter follows:
1834 % o image_info: the image info..
1836 % o image: the image.
1838 % o inject_image: inject into the image stream.
1840 % o format: the image format.
1842 % o exception: return any errors or warnings in this structure.
1845 MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1846 Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
1849 filename[MaxTextExtent];
1882 Write inject image to a temporary file.
1884 assert(image_info != (ImageInfo *) NULL);
1885 assert(image_info->signature == MagickSignature);
1886 assert(image != (Image *) NULL);
1887 assert(image->signature == MagickSignature);
1888 if (image->debug != MagickFalse)
1889 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1890 assert(inject_image != (Image *) NULL);
1891 assert(inject_image->signature == MagickSignature);
1892 assert(exception != (ExceptionInfo *) NULL);
1893 unique_file=(FILE *) NULL;
1894 file=AcquireUniqueFileResource(filename);
1896 unique_file=fdopen(file,"wb");
1897 if ((file == -1) || (unique_file == (FILE *) NULL))
1899 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1900 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1902 return(MagickFalse);
1904 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
1905 if (byte_image == (Image *) NULL)
1907 (void) fclose(unique_file);
1908 (void) RelinquishUniqueFileResource(filename);
1909 return(MagickFalse);
1911 (void) FormatLocaleString(byte_image->filename,MaxTextExtent,"%s:%s",format,
1913 DestroyBlob(byte_image);
1914 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
1915 write_info=CloneImageInfo(image_info);
1916 SetImageInfoFile(write_info,unique_file);
1917 status=WriteImage(write_info,byte_image,exception);
1918 write_info=DestroyImageInfo(write_info);
1919 byte_image=DestroyImage(byte_image);
1920 (void) fclose(unique_file);
1921 if (status == MagickFalse)
1923 (void) RelinquishUniqueFileResource(filename);
1924 return(MagickFalse);
1927 Inject into image stream.
1929 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1932 (void) RelinquishUniqueFileResource(filename);
1933 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1934 image_info->filename);
1935 return(MagickFalse);
1937 quantum=(size_t) MagickMaxBufferExtent;
1938 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1939 quantum=(size_t) MagickMin(file_info.st_size,MagickMaxBufferExtent);
1940 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1941 if (buffer == (unsigned char *) NULL)
1943 (void) RelinquishUniqueFileResource(filename);
1944 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1947 for (i=0; ; i+=count)
1949 count=(ssize_t) read(file,buffer,quantum);
1956 status=WriteBlobStream(image,(size_t) count,buffer) == count ? MagickTrue :
1961 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",filename);
1962 (void) RelinquishUniqueFileResource(filename);
1963 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1972 + I s B l o b E x e m p t %
1976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1978 % IsBlobExempt() returns true if the blob is exempt.
1980 % The format of the IsBlobExempt method is:
1982 % MagickBooleanType IsBlobExempt(const Image *image)
1984 % A description of each parameter follows:
1986 % o image: the image.
1989 MagickPrivate MagickBooleanType IsBlobExempt(const Image *image)
1991 assert(image != (const Image *) NULL);
1992 assert(image->signature == MagickSignature);
1993 if (image->debug != MagickFalse)
1994 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1995 return(image->blob->exempt);
1999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2003 + I s B l o b S e e k a b l e %
2007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009 % IsBlobSeekable() returns true if the blob is seekable.
2011 % The format of the IsBlobSeekable method is:
2013 % MagickBooleanType IsBlobSeekable(const Image *image)
2015 % A description of each parameter follows:
2017 % o image: the image.
2020 MagickPrivate MagickBooleanType IsBlobSeekable(const Image *image)
2025 assert(image != (const Image *) NULL);
2026 assert(image->signature == MagickSignature);
2027 if (image->debug != MagickFalse)
2028 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2029 seekable=(image->blob->type == FileStream) ||
2030 (image->blob->type == BlobStream) ? MagickTrue : MagickFalse;
2035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2039 + I s B l o b T e m p o r a r y %
2043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2045 % IsBlobTemporary() returns true if the blob is temporary.
2047 % The format of the IsBlobTemporary method is:
2049 % MagickBooleanType IsBlobTemporary(const Image *image)
2051 % A description of each parameter follows:
2053 % o image: the image.
2056 MagickPrivate MagickBooleanType IsBlobTemporary(const Image *image)
2058 assert(image != (const Image *) NULL);
2059 assert(image->signature == MagickSignature);
2060 if (image->debug != MagickFalse)
2061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2062 return(image->blob->temporary);
2066 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2076 % MapBlob() creates a mapping from a file to a binary large object.
2078 % The format of the MapBlob method is:
2080 % unsigned char *MapBlob(int file,const MapMode mode,
2081 % const MagickOffsetType offset,const size_t length)
2083 % A description of each parameter follows:
2085 % o file: map this file descriptor.
2087 % o mode: ReadMode, WriteMode, or IOMode.
2089 % o offset: starting at this offset within the file.
2091 % o length: the length of the mapping is returned in this pointer.
2094 MagickExport unsigned char *MapBlob(int file,const MapMode mode,
2095 const MagickOffsetType offset,const size_t length)
2097 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
2110 #if defined(MAP_ANONYMOUS)
2111 flags|=MAP_ANONYMOUS;
2113 return((unsigned char *) NULL);
2120 protection=PROT_READ;
2122 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2128 protection=PROT_WRITE;
2130 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2132 #if defined(MAGICKCORE_HAVE_POSIX_MADVISE)
2133 (void) posix_madvise(map,length,POSIX_MADV_SEQUENTIAL |
2134 POSIX_MADV_WILLNEED);
2140 protection=PROT_READ | PROT_WRITE;
2142 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2147 if (map == (unsigned char *) MAP_FAILED)
2148 return((unsigned char *) NULL);
2155 return((unsigned char *) NULL);
2160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2164 + M S B O r d e r L o n g %
2168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2170 % MSBOrderLong() converts a least-significant byte first buffer of integers to
2171 % most-significant byte first.
2173 % The format of the MSBOrderLong method is:
2175 % void MSBOrderLong(unsigned char *buffer,const size_t length)
2177 % A description of each parameter follows.
2179 % o buffer: Specifies a pointer to a buffer of integers.
2181 % o length: Specifies the length of the buffer.
2184 MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
2189 register unsigned char
2193 assert(buffer != (unsigned char *) NULL);
2200 *buffer++=(unsigned char) c;
2204 *buffer++=(unsigned char) c;
2210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2214 + M S B O r d e r S h o r t %
2218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2220 % MSBOrderShort() converts a least-significant byte first buffer of integers
2221 % to most-significant byte first.
2223 % The format of the MSBOrderShort method is:
2225 % void MSBOrderShort(unsigned char *p,const size_t length)
2227 % A description of each parameter follows.
2229 % o p: Specifies a pointer to a buffer of integers.
2231 % o length: Specifies the length of the buffer.
2234 MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
2239 register unsigned char
2242 assert(p != (unsigned char *) NULL);
2249 *p++=(unsigned char) c;
2254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2264 % OpenBlob() opens a file associated with the image. A file name of '-' sets
2265 % the file to stdin for type 'r' and stdout for type 'w'. If the filename
2266 % suffix is '.gz' or '.Z', the image is decompressed for type 'r' and
2267 % compressed for type 'w'. If the filename prefix is '|', it is piped to or
2268 % from a system command.
2270 % The format of the OpenBlob method is:
2272 % MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
2273 % const BlobMode mode,ExceptionInfo *exception)
2275 % A description of each parameter follows:
2277 % o image_info: the image info.
2279 % o image: the image.
2281 % o mode: the mode for opening the file.
2284 MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
2285 Image *image,const BlobMode mode,ExceptionInfo *exception)
2288 extension[MaxTextExtent],
2289 filename[MaxTextExtent];
2300 assert(image_info != (ImageInfo *) NULL);
2301 assert(image_info->signature == MagickSignature);
2302 if (image_info->debug != MagickFalse)
2303 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2304 image_info->filename);
2305 assert(image != (Image *) NULL);
2306 assert(image->signature == MagickSignature);
2307 if (image_info->blob != (void *) NULL)
2309 if (image_info->stream != (StreamHandler) NULL)
2310 image->blob->stream=(StreamHandler) image_info->stream;
2311 AttachBlob(image->blob,image_info->blob,image_info->length);
2314 (void) DetachBlob(image->blob);
2317 default: type="r"; break;
2318 case ReadBlobMode: type="r"; break;
2319 case ReadBinaryBlobMode: type="rb"; break;
2320 case WriteBlobMode: type="w"; break;
2321 case WriteBinaryBlobMode: type="w+b"; break;
2322 case AppendBlobMode: type="a"; break;
2323 case AppendBinaryBlobMode: type="a+b"; break;
2326 image->blob->synchronize=image_info->synchronize;
2327 if (image_info->stream != (StreamHandler) NULL)
2329 image->blob->stream=(StreamHandler) image_info->stream;
2332 image->blob->type=FifoStream;
2340 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2341 rights=ReadPolicyRights;
2343 rights=WritePolicyRights;
2344 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2347 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2348 "NotAuthorized","`%s'",filename);
2349 return(MagickFalse);
2351 if ((LocaleCompare(filename,"-") == 0) ||
2352 ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
2354 image->blob->file=(*type == 'r') ? stdin : stdout;
2355 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2356 if (strchr(type,'b') != (char *) NULL)
2357 setmode(_fileno(image->blob->file),_O_BINARY);
2359 image->blob->type=StandardStream;
2360 image->blob->exempt=MagickTrue;
2363 if (LocaleNCompare(filename,"fd:",3) == 0)
2366 mode[MaxTextExtent];
2370 image->blob->file=fdopen(StringToLong(filename+3),mode);
2371 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2372 if (strchr(type,'b') != (char *) NULL)
2373 setmode(_fileno(image->blob->file),_O_BINARY);
2375 image->blob->type=StandardStream;
2376 image->blob->exempt=MagickTrue;
2379 #if defined(MAGICKCORE_HAVE_POPEN)
2380 if (*filename == '|')
2383 mode[MaxTextExtent];
2386 Pipe image to or from a system command.
2388 #if defined(SIGPIPE)
2390 (void) signal(SIGPIPE,SIG_IGN);
2394 image->blob->file=(FILE *) popen_utf8(filename+1,mode);
2395 if (image->blob->file == (FILE *) NULL)
2397 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2398 return(MagickFalse);
2400 image->blob->type=PipeStream;
2401 image->blob->exempt=MagickTrue;
2405 status=GetPathAttributes(filename,&image->blob->properties);
2406 #if defined(S_ISFIFO)
2407 if ((status == MagickTrue) && S_ISFIFO(image->blob->properties.st_mode))
2409 image->blob->file=(FILE *) fopen_utf8(filename,type);
2410 if (image->blob->file == (FILE *) NULL)
2412 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2413 return(MagickFalse);
2415 image->blob->type=FileStream;
2416 image->blob->exempt=MagickTrue;
2420 GetPathComponent(image->filename,ExtensionPath,extension);
2423 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2424 if ((image_info->adjoin == MagickFalse) ||
2425 (strchr(filename,'%') != (char *) NULL))
2428 Form filename for multi-part images.
2430 (void) InterpretImageFilename(image_info,image,image->filename,(int)
2431 image->scene,filename);
2432 if ((LocaleCompare(filename,image->filename) == 0) &&
2433 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2434 (GetNextImageInList(image) != (Image *) NULL)))
2437 path[MaxTextExtent];
2439 GetPathComponent(image->filename,RootPath,path);
2440 if (*extension == '\0')
2441 (void) FormatLocaleString(filename,MaxTextExtent,"%s-%.20g",
2442 path,(double) image->scene);
2444 (void) FormatLocaleString(filename,MaxTextExtent,"%s-%.20g.%s",
2445 path,(double) image->scene,extension);
2447 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
2448 #if defined(macintosh)
2449 SetApplicationType(filename,image_info->magick,'8BIM');
2453 if (image_info->file != (FILE *) NULL)
2455 image->blob->file=image_info->file;
2456 image->blob->type=FileStream;
2457 image->blob->exempt=MagickTrue;
2462 image->blob->file=(FILE *) fopen_utf8(filename,type);
2463 if (image->blob->file != (FILE *) NULL)
2471 image->blob->type=FileStream;
2472 #if defined(MAGICKCORE_HAVE_SETVBUF)
2473 (void) setvbuf(image->blob->file,(char *) NULL,(int) _IOFBF,16384);
2475 (void) ResetMagickMemory(magick,0,sizeof(magick));
2476 count=fread(magick,1,sizeof(magick),image->blob->file);
2477 (void) rewind(image->blob->file);
2478 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2479 " read %.20g magic header bytes",(double) count);
2480 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2481 if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
2482 ((int) magick[2] == 0x08))
2484 (void) fclose(image->blob->file);
2485 image->blob->file=(FILE *) gzopen(filename,type);
2486 if (image->blob->file != (FILE *) NULL)
2487 image->blob->type=ZipStream;
2490 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2491 if (strncmp((char *) magick,"BZh",3) == 0)
2493 (void) fclose(image->blob->file);
2494 image->blob->file=(FILE *) BZ2_bzopen(filename,type);
2495 if (image->blob->file != (FILE *) NULL)
2496 image->blob->type=BZipStream;
2499 if (image->blob->type == FileStream)
2510 sans_exception=AcquireExceptionInfo();
2511 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2512 sans_exception=DestroyExceptionInfo(sans_exception);
2513 properties=(&image->blob->properties);
2514 if ((magick_info != (const MagickInfo *) NULL) &&
2515 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2516 (properties->st_size <= MagickMaxBufferExtent))
2524 length=(size_t) properties->st_size;
2525 blob=MapBlob(fileno(image->blob->file),ReadMode,0,length);
2526 if (blob != (void *) NULL)
2529 Format supports blobs-- use memory-mapped I/O.
2531 if (image_info->file != (FILE *) NULL)
2532 image->blob->exempt=MagickFalse;
2535 (void) fclose(image->blob->file);
2536 image->blob->file=(FILE *) NULL;
2538 AttachBlob(image->blob,blob,length);
2539 image->blob->mapped=MagickTrue;
2546 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2547 if ((LocaleCompare(extension,"Z") == 0) ||
2548 (LocaleCompare(extension,"gz") == 0) ||
2549 (LocaleCompare(extension,"wmz") == 0) ||
2550 (LocaleCompare(extension,"svgz") == 0))
2552 if (mode == WriteBinaryBlobMode)
2554 image->blob->file=(FILE *) gzopen(filename,type);
2555 if (image->blob->file != (FILE *) NULL)
2556 image->blob->type=ZipStream;
2560 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2561 if (LocaleCompare(extension,".bz2") == 0)
2563 image->blob->file=(FILE *) BZ2_bzopen(filename,type);
2564 if (image->blob->file != (FILE *) NULL)
2565 image->blob->type=BZipStream;
2570 image->blob->file=(FILE *) fopen_utf8(filename,type);
2571 if (image->blob->file != (FILE *) NULL)
2573 image->blob->type=FileStream;
2574 #if defined(MAGICKCORE_HAVE_SETVBUF)
2575 (void) setvbuf(image->blob->file,(char *) NULL,(int) _IOFBF,
2580 image->blob->status=MagickFalse;
2581 if (image->blob->type != UndefinedStream)
2582 image->blob->size=GetBlobSize(image);
2585 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2586 return(MagickFalse);
2592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2602 % PingBlob() returns all the attributes of an image or image sequence except
2603 % for the pixels. It is much faster and consumes far less memory than
2604 % BlobToImage(). On failure, a NULL image is returned and exception
2605 % describes the reason for the failure.
2607 % The format of the PingBlob method is:
2609 % Image *PingBlob(const ImageInfo *image_info,const void *blob,
2610 % const size_t length,ExceptionInfo *exception)
2612 % A description of each parameter follows:
2614 % o image_info: the image info.
2616 % o blob: the address of a character stream in one of the image formats
2617 % understood by ImageMagick.
2619 % o length: This size_t integer reflects the length in bytes of the blob.
2621 % o exception: return any errors or warnings in this structure.
2625 #if defined(__cplusplus) || defined(c_plusplus)
2629 static size_t PingStream(const Image *magick_unused(image),
2630 const void *magick_unused(pixels),const size_t columns)
2635 #if defined(__cplusplus) || defined(c_plusplus)
2639 MagickExport Image *PingBlob(const ImageInfo *image_info,const void *blob,
2640 const size_t length,ExceptionInfo *exception)
2648 assert(image_info != (ImageInfo *) NULL);
2649 assert(image_info->signature == MagickSignature);
2650 if (image_info->debug != MagickFalse)
2651 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2652 image_info->filename);
2653 assert(exception != (ExceptionInfo *) NULL);
2654 if ((blob == (const void *) NULL) || (length == 0))
2656 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
2657 "UnrecognizedImageFormat","`%s'",image_info->magick);
2658 return((Image *) NULL);
2660 ping_info=CloneImageInfo(image_info);
2661 ping_info->blob=(void *) AcquireQuantumMemory(length,sizeof(unsigned char));
2662 if (ping_info->blob == (const void *) NULL)
2664 (void) ThrowMagickException(exception,GetMagickModule(),
2665 ResourceLimitFatalError,"MemoryAllocationFailed","`%s'","");
2666 return((Image *) NULL);
2668 (void) memcpy(ping_info->blob,blob,length);
2669 ping_info->length=length;
2670 ping_info->ping=MagickTrue;
2671 image=ReadStream(ping_info,&PingStream,exception);
2672 ping_info->blob=(void *) RelinquishMagickMemory(ping_info->blob);
2673 ping_info=DestroyImageInfo(ping_info);
2678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2688 % ReadBlob() reads data from the blob or image file and returns it. It
2689 % returns the number of bytes read.
2691 % The format of the ReadBlob method is:
2693 % ssize_t ReadBlob(Image *image,const size_t length,unsigned char *data)
2695 % A description of each parameter follows:
2697 % o image: the image.
2699 % o length: Specifies an integer representing the number of bytes to read
2702 % o data: Specifies an area to place the information requested from the
2706 MagickExport ssize_t ReadBlob(Image *image,const size_t length,
2707 unsigned char *data)
2712 register unsigned char
2718 assert(image != (Image *) NULL);
2719 assert(image->signature == MagickSignature);
2720 assert(image->blob != (BlobInfo *) NULL);
2721 assert(image->blob->type != UndefinedStream);
2724 assert(data != (void *) NULL);
2727 switch (image->blob->type)
2729 case UndefinedStream:
2732 case StandardStream:
2739 count=(ssize_t) fread(q,1,length,image->blob->file);
2744 c=getc(image->blob->file);
2747 *q++=(unsigned char) c;
2752 c=getc(image->blob->file);
2755 *q++=(unsigned char) c;
2765 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2770 count=(ssize_t) gzread(image->blob->file,q,(unsigned int) length);
2775 c=gzgetc(image->blob->file);
2778 *q++=(unsigned char) c;
2783 c=gzgetc(image->blob->file);
2786 *q++=(unsigned char) c;
2797 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2798 count=(ssize_t) BZ2_bzread((BZFILE *) image->blob->file,q,(int) length);
2806 register const unsigned char
2809 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
2811 image->blob->eof=MagickTrue;
2814 p=image->blob->data+image->blob->offset;
2815 count=(ssize_t) MagickMin(length,image->blob->length-image->blob->offset);
2816 image->blob->offset+=count;
2817 if (count != (ssize_t) length)
2818 image->blob->eof=MagickTrue;
2819 (void) memcpy(q,p,(size_t) count);
2827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2831 + R e a d B l o b B y t e %
2835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2837 % ReadBlobByte() reads a single byte from the image file and returns it.
2839 % The format of the ReadBlobByte method is:
2841 % int ReadBlobByte(Image *image)
2843 % A description of each parameter follows.
2845 % o image: the image.
2848 MagickExport int ReadBlobByte(Image *image)
2850 register const unsigned char
2859 assert(image != (Image *) NULL);
2860 assert(image->signature == MagickSignature);
2861 p=ReadBlobStream(image,1,buffer,&count);
2868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2872 + R e a d B l o b D o u b l e %
2876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2878 % ReadBlobDouble() reads a double value as a 64-bit quantity in the byte-order
2879 % specified by the endian member of the image structure.
2881 % The format of the ReadBlobDouble method is:
2883 % double ReadBlobDouble(Image *image)
2885 % A description of each parameter follows.
2887 % o image: the image.
2890 MagickExport double ReadBlobDouble(Image *image)
2901 quantum.double_value=0.0;
2902 quantum.unsigned_value=ReadBlobLongLong(image);
2903 return(quantum.double_value);
2907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2911 + R e a d B l o b F l o a t %
2915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2917 % ReadBlobFloat() reads a float value as a 32-bit quantity in the byte-order
2918 % specified by the endian member of the image structure.
2920 % The format of the ReadBlobFloat method is:
2922 % float ReadBlobFloat(Image *image)
2924 % A description of each parameter follows.
2926 % o image: the image.
2929 MagickExport float ReadBlobFloat(Image *image)
2940 quantum.float_value=0.0;
2941 quantum.unsigned_value=ReadBlobLong(image);
2942 return(quantum.float_value);
2946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2950 + R e a d B l o b L o n g %
2954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2956 % ReadBlobLong() reads a ssize_t value as a 32-bit quantity in the byte-order
2957 % specified by the endian member of the image structure.
2959 % The format of the ReadBlobLong method is:
2961 % unsigned int ReadBlobLong(Image *image)
2963 % A description of each parameter follows.
2965 % o image: the image.
2968 MagickExport unsigned int ReadBlobLong(Image *image)
2970 register const unsigned char
2982 assert(image != (Image *) NULL);
2983 assert(image->signature == MagickSignature);
2985 p=ReadBlobStream(image,4,buffer,&count);
2988 if (image->endian == LSBEndian)
2990 value=(unsigned int) (*p++);
2991 value|=((unsigned int) (*p++)) << 8;
2992 value|=((unsigned int) (*p++)) << 16;
2993 value|=((unsigned int) (*p++)) << 24;
2996 value=((unsigned int) (*p++)) << 24;
2997 value|=((unsigned int) (*p++)) << 16;
2998 value|=((unsigned int) (*p++)) << 8;
2999 value|=((unsigned int) (*p++));
3004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3008 + R e a d B l o b L o n g L o n g %
3012 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3014 % ReadBlobLongLong() reads a long long value as a 64-bit quantity in the
3015 % byte-order specified by the endian member of the image structure.
3017 % The format of the ReadBlobLongLong method is:
3019 % MagickSizeType ReadBlobLongLong(Image *image)
3021 % A description of each parameter follows.
3023 % o image: the image.
3026 MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3031 register const unsigned char
3040 assert(image != (Image *) NULL);
3041 assert(image->signature == MagickSignature);
3043 p=ReadBlobStream(image,8,buffer,&count);
3045 return(MagickULLConstant(0));
3046 if (image->endian == LSBEndian)
3048 value=(MagickSizeType) (*p++);
3049 value|=((MagickSizeType) (*p++)) << 8;
3050 value|=((MagickSizeType) (*p++)) << 16;
3051 value|=((MagickSizeType) (*p++)) << 24;
3052 value|=((MagickSizeType) (*p++)) << 32;
3053 value|=((MagickSizeType) (*p++)) << 40;
3054 value|=((MagickSizeType) (*p++)) << 48;
3055 value|=((MagickSizeType) (*p++)) << 56;
3056 return(value & MagickULLConstant(0xffffffffffffffff));
3058 value=((MagickSizeType) (*p++)) << 56;
3059 value|=((MagickSizeType) (*p++)) << 48;
3060 value|=((MagickSizeType) (*p++)) << 40;
3061 value|=((MagickSizeType) (*p++)) << 32;
3062 value|=((MagickSizeType) (*p++)) << 24;
3063 value|=((MagickSizeType) (*p++)) << 16;
3064 value|=((MagickSizeType) (*p++)) << 8;
3065 value|=((MagickSizeType) (*p++));
3066 return(value & MagickULLConstant(0xffffffffffffffff));
3070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3074 + R e a d B l o b S h o r t %
3078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3080 % ReadBlobShort() reads a short value as a 16-bit quantity in the byte-order
3081 % specified by the endian member of the image structure.
3083 % The format of the ReadBlobShort method is:
3085 % unsigned short ReadBlobShort(Image *image)
3087 % A description of each parameter follows.
3089 % o image: the image.
3092 MagickExport unsigned short ReadBlobShort(Image *image)
3094 register const unsigned char
3097 register unsigned int
3106 assert(image != (Image *) NULL);
3107 assert(image->signature == MagickSignature);
3109 p=ReadBlobStream(image,2,buffer,&count);
3111 return((unsigned short) 0U);
3112 if (image->endian == LSBEndian)
3114 value=(unsigned int) (*p++);
3115 value|=((unsigned int) (*p++)) << 8;
3116 return((unsigned short) (value & 0xffff));
3118 value=(unsigned int) ((*p++) << 8);
3119 value|=(unsigned int) (*p++);
3120 return((unsigned short) (value & 0xffff));
3124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3128 + R e a d B l o b L S B L o n g %
3132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3134 % ReadBlobLSBLong() reads a ssize_t value as a 32-bit quantity in
3135 % least-significant byte first order.
3137 % The format of the ReadBlobLSBLong method is:
3139 % unsigned int ReadBlobLSBLong(Image *image)
3141 % A description of each parameter follows.
3143 % o image: the image.
3146 MagickExport unsigned int ReadBlobLSBLong(Image *image)
3148 register const unsigned char
3151 register unsigned int
3160 assert(image != (Image *) NULL);
3161 assert(image->signature == MagickSignature);
3163 p=ReadBlobStream(image,4,buffer,&count);
3166 value=(unsigned int) (*p++);
3167 value|=((unsigned int) (*p++)) << 8;
3168 value|=((unsigned int) (*p++)) << 16;
3169 value|=((unsigned int) (*p++)) << 24;
3174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3178 + R e a d B l o b L S B S h o r t %
3182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3184 % ReadBlobLSBShort() reads a short value as a 16-bit quantity in
3185 % least-significant byte first order.
3187 % The format of the ReadBlobLSBShort method is:
3189 % unsigned short ReadBlobLSBShort(Image *image)
3191 % A description of each parameter follows.
3193 % o image: the image.
3196 MagickExport unsigned short ReadBlobLSBShort(Image *image)
3198 register const unsigned char
3201 register unsigned int
3210 assert(image != (Image *) NULL);
3211 assert(image->signature == MagickSignature);
3213 p=ReadBlobStream(image,2,buffer,&count);
3215 return((unsigned short) 0U);
3216 value=(unsigned int) (*p++);
3217 value|=((unsigned int) ((*p++)) << 8);
3218 return((unsigned short) (value & 0xffff));
3222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3226 + R e a d B l o b M S B L o n g %
3230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3232 % ReadBlobMSBLong() reads a ssize_t value as a 32-bit quantity in
3233 % most-significant byte first order.
3235 % The format of the ReadBlobMSBLong method is:
3237 % unsigned int ReadBlobMSBLong(Image *image)
3239 % A description of each parameter follows.
3241 % o image: the image.
3244 MagickExport unsigned int ReadBlobMSBLong(Image *image)
3246 register const unsigned char
3249 register unsigned int
3258 assert(image != (Image *) NULL);
3259 assert(image->signature == MagickSignature);
3261 p=ReadBlobStream(image,4,buffer,&count);
3264 value=((unsigned int) (*p++) << 24);
3265 value|=((unsigned int) (*p++) << 16);
3266 value|=((unsigned int) (*p++) << 8);
3267 value|=(unsigned int) (*p++);
3272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3276 + R e a d B l o b M S B L o n g L o n g %
3280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3282 % ReadBlobMSBLongLong() reads a ssize_t value as a 64-bit quantity in
3283 % most-significant byte first order.
3285 % The format of the ReadBlobMSBLongLong method is:
3287 % unsigned int ReadBlobMSBLongLong(Image *image)
3289 % A description of each parameter follows.
3291 % o image: the image.
3294 MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
3296 register const unsigned char
3299 register MagickSizeType
3308 assert(image != (Image *) NULL);
3309 assert(image->signature == MagickSignature);
3311 p=ReadBlobStream(image,8,buffer,&count);
3313 return(MagickULLConstant(0));
3314 value=((MagickSizeType) (*p++)) << 56;
3315 value|=((MagickSizeType) (*p++)) << 48;
3316 value|=((MagickSizeType) (*p++)) << 40;
3317 value|=((MagickSizeType) (*p++)) << 32;
3318 value|=((MagickSizeType) (*p++)) << 24;
3319 value|=((MagickSizeType) (*p++)) << 16;
3320 value|=((MagickSizeType) (*p++)) << 8;
3321 value|=((MagickSizeType) (*p++));
3322 return(value & MagickULLConstant(0xffffffffffffffff));
3326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3330 + R e a d B l o b M S B S h o r t %
3334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3336 % ReadBlobMSBShort() reads a short value as a 16-bit quantity in
3337 % most-significant byte first order.
3339 % The format of the ReadBlobMSBShort method is:
3341 % unsigned short ReadBlobMSBShort(Image *image)
3343 % A description of each parameter follows.
3345 % o image: the image.
3348 MagickExport unsigned short ReadBlobMSBShort(Image *image)
3350 register const unsigned char
3353 register unsigned int
3362 assert(image != (Image *) NULL);
3363 assert(image->signature == MagickSignature);
3365 p=ReadBlobStream(image,2,buffer,&count);
3367 return((unsigned short) 0U);
3368 value=(unsigned int) ((*p++) << 8);
3369 value|=(unsigned int) (*p++);
3370 return((unsigned short) (value & 0xffff));
3374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3378 + R e a d B l o b S t r i n g %
3382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3384 % ReadBlobString() reads characters from a blob or file until a newline
3385 % character is read or an end-of-file condition is encountered.
3387 % The format of the ReadBlobString method is:
3389 % char *ReadBlobString(Image *image,char *string)
3391 % A description of each parameter follows:
3393 % o image: the image.
3395 % o string: the address of a character buffer.
3398 MagickExport char *ReadBlobString(Image *image,char *string)
3400 register const unsigned char
3412 assert(image != (Image *) NULL);
3413 assert(image->signature == MagickSignature);
3414 for (i=0; i < (MaxTextExtent-1L); i++)
3416 p=ReadBlobStream(image,1,buffer,&count);
3420 return((char *) NULL);
3423 string[i]=(char) (*p);
3424 if ((string[i] == '\r') || (string[i] == '\n'))
3427 if (string[i] == '\r')
3428 (void) ReadBlobStream(image,1,buffer,&count);
3434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3438 + R e f e r e n c e B l o b %
3442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3444 % ReferenceBlob() increments the reference count associated with the pixel
3445 % blob returning a pointer to the blob.
3447 % The format of the ReferenceBlob method is:
3449 % BlobInfo ReferenceBlob(BlobInfo *blob_info)
3451 % A description of each parameter follows:
3453 % o blob_info: the blob_info.
3456 MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
3458 assert(blob != (BlobInfo *) NULL);
3459 assert(blob->signature == MagickSignature);
3460 if (blob->debug != MagickFalse)
3461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3462 LockSemaphoreInfo(blob->semaphore);
3463 blob->reference_count++;
3464 UnlockSemaphoreInfo(blob->semaphore);
3469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3479 % SeekBlob() sets the offset in bytes from the beginning of a blob or file
3480 % and returns the resulting offset.
3482 % The format of the SeekBlob method is:
3484 % MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
3487 % A description of each parameter follows:
3489 % o image: the image.
3491 % o offset: Specifies an integer representing the offset in bytes.
3493 % o whence: Specifies an integer representing how the offset is
3494 % treated relative to the beginning of the blob as follows:
3496 % SEEK_SET Set position equal to offset bytes.
3497 % SEEK_CUR Set position to current location plus offset.
3498 % SEEK_END Set position to EOF plus offset.
3501 MagickExport MagickOffsetType SeekBlob(Image *image,
3502 const MagickOffsetType offset,const int whence)
3504 assert(image != (Image *) NULL);
3505 assert(image->signature == MagickSignature);
3506 if (image->debug != MagickFalse)
3507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3508 assert(image->blob != (BlobInfo *) NULL);
3509 assert(image->blob->type != UndefinedStream);
3510 switch (image->blob->type)
3512 case UndefinedStream:
3516 if (fseek(image->blob->file,offset,whence) < 0)
3518 image->blob->offset=TellBlob(image);
3521 case StandardStream:
3525 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3526 if (gzseek(image->blob->file,(off_t) offset,whence) < 0)
3529 image->blob->offset=TellBlob(image);
3545 image->blob->offset=offset;
3550 if ((image->blob->offset+offset) < 0)
3552 image->blob->offset+=offset;
3557 if (((MagickOffsetType) image->blob->length+offset) < 0)
3559 image->blob->offset=image->blob->length+offset;
3563 if (image->blob->offset <= (MagickOffsetType)
3564 ((off_t) image->blob->length))
3565 image->blob->eof=MagickFalse;
3567 if (image->blob->mapped != MagickFalse)
3571 image->blob->extent=(size_t) (image->blob->offset+
3572 image->blob->quantum);
3573 image->blob->data=(unsigned char *) ResizeQuantumMemory(
3574 image->blob->data,image->blob->extent+1,
3575 sizeof(*image->blob->data));
3576 (void) SyncBlob(image);
3577 if (image->blob->data == (unsigned char *) NULL)
3579 (void) DetachBlob(image->blob);
3586 return(image->blob->offset);
3590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3594 + S e t B l o b E x e m p t %
3598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3600 % SetBlobExempt() sets the blob exempt status.
3602 % The format of the SetBlobExempt method is:
3604 % MagickBooleanType SetBlobExempt(const Image *image,
3605 % const MagickBooleanType exempt)
3607 % A description of each parameter follows:
3609 % o image: the image.
3611 % o exempt: Set to true if this blob is exempt from being closed.
3614 MagickPrivate void SetBlobExempt(Image *image,const MagickBooleanType exempt)
3616 assert(image != (const Image *) NULL);
3617 assert(image->signature == MagickSignature);
3618 if (image->debug != MagickFalse)
3619 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3620 image->blob->exempt=exempt;
3624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3628 + S e t B l o b E x t e n t %
3632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3634 % SetBlobExtent() ensures enough space is allocated for the blob. If the
3635 % method is successful, subsequent writes to bytes in the specified range are
3636 % guaranteed not to fail.
3638 % The format of the SetBlobExtent method is:
3640 % MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
3642 % A description of each parameter follows:
3644 % o image: the image.
3646 % o extent: the blob maximum extent.
3649 MagickPrivate MagickBooleanType SetBlobExtent(Image *image,
3650 const MagickSizeType extent)
3652 assert(image != (Image *) NULL);
3653 assert(image->signature == MagickSignature);
3654 if (image->debug != MagickFalse)
3655 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3656 assert(image->blob != (BlobInfo *) NULL);
3657 assert(image->blob->type != UndefinedStream);
3658 switch (image->blob->type)
3660 case UndefinedStream:
3664 if (extent != (MagickSizeType) ((off_t) extent))
3665 return(MagickFalse);
3666 #if !defined(MAGICKCORE_POSIX_FALLOCATE)
3667 return(MagickFalse);
3676 offset=TellBlob(image);
3677 status=posix_fallocate(fileno(image->blob->file),(off_t) offset,
3678 (off_t) (extent-offset));
3680 return(MagickFalse);
3685 case StandardStream:
3688 return(MagickFalse);
3690 return(MagickFalse);
3692 return(MagickFalse);
3695 if (image->blob->mapped != MagickFalse)
3697 if (image->blob->file == (FILE *) NULL)
3698 return(MagickFalse);
3699 (void) UnmapBlob(image->blob->data,image->blob->length);
3700 #if !defined(MAGICKCORE_POSIX_FALLOCATE)
3701 return(MagickFalse);
3710 offset=TellBlob(image);
3711 status=posix_fallocate(fileno(image->blob->file),(off_t) offset,
3712 (off_t) (extent-offset));
3714 return(MagickFalse);
3716 image->blob->data=(unsigned char*) MapBlob(fileno(image->blob->file),
3717 WriteMode,0,(size_t) extent);
3718 image->blob->extent=(size_t) extent;
3719 image->blob->length=(size_t) extent;
3720 (void) SyncBlob(image);
3724 if (extent != (MagickSizeType) ((size_t) extent))
3725 return(MagickFalse);
3726 image->blob->extent=(size_t) extent;
3727 image->blob->data=(unsigned char *) ResizeQuantumMemory(image->blob->data,
3728 image->blob->extent+1,sizeof(*image->blob->data));
3729 (void) SyncBlob(image);
3730 if (image->blob->data == (unsigned char *) NULL)
3732 (void) DetachBlob(image->blob);
3733 return(MagickFalse);
3742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3752 % SyncBlob() flushes the datastream if it is a file or synchronizes the data
3753 % attributes if it is an blob.
3755 % The format of the SyncBlob method is:
3757 % int SyncBlob(Image *image)
3759 % A description of each parameter follows:
3761 % o image: the image.
3764 static int SyncBlob(Image *image)
3769 assert(image != (Image *) NULL);
3770 assert(image->signature == MagickSignature);
3771 if (image->debug != MagickFalse)
3772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3773 assert(image->blob != (BlobInfo *) NULL);
3774 assert(image->blob->type != UndefinedStream);
3776 switch (image->blob->type)
3778 case UndefinedStream:
3781 case StandardStream:
3784 status=fflush(image->blob->file);
3789 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3790 status=gzflush(image->blob->file,Z_SYNC_FLUSH);
3796 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3797 status=BZ2_bzflush((BZFILE *) image->blob->file);
3805 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3806 if (image->blob->mapped != MagickFalse)
3807 status=msync(image->blob->data,image->blob->length,MS_SYNC);
3816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3826 % TellBlob() obtains the current value of the blob or file position.
3828 % The format of the TellBlob method is:
3830 % MagickOffsetType TellBlob(const Image *image)
3832 % A description of each parameter follows:
3834 % o image: the image.
3837 MagickExport MagickOffsetType TellBlob(const Image *image)
3842 assert(image != (Image *) NULL);
3843 assert(image->signature == MagickSignature);
3844 if (image->debug != MagickFalse)
3845 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3846 assert(image->blob != (BlobInfo *) NULL);
3847 assert(image->blob->type != UndefinedStream);
3849 switch (image->blob->type)
3851 case UndefinedStream:
3855 offset=ftell(image->blob->file);
3858 case StandardStream:
3863 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3864 offset=(MagickOffsetType) gztell(image->blob->file);
3874 offset=image->blob->offset;
3882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3886 + U n m a p B l o b %
3890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3892 % UnmapBlob() deallocates the binary large object previously allocated with
3893 % the MapBlob method.
3895 % The format of the UnmapBlob method is:
3897 % MagickBooleanType UnmapBlob(void *map,const size_t length)
3899 % A description of each parameter follows:
3901 % o map: the address of the binary large object.
3903 % o length: the length of the binary large object.
3906 MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
3908 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3912 status=munmap(map,length);
3913 return(status == -1 ? MagickFalse : MagickTrue);
3917 return(MagickFalse);
3922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3926 + W r i t e B l o b %
3930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3932 % WriteBlob() writes data to a blob or image file. It returns the number of
3935 % The format of the WriteBlob method is:
3937 % ssize_t WriteBlob(Image *image,const size_t length,
3938 % const unsigned char *data)
3940 % A description of each parameter follows:
3942 % o image: the image.
3944 % o length: Specifies an integer representing the number of bytes to
3945 % write to the file.
3947 % o data: The address of the data to write to the blob or file.
3950 MagickExport ssize_t WriteBlob(Image *image,const size_t length,
3951 const unsigned char *data)
3956 register const unsigned char
3962 assert(image != (Image *) NULL);
3963 assert(image->signature == MagickSignature);
3964 assert(data != (const unsigned char *) NULL);
3965 assert(image->blob != (BlobInfo *) NULL);
3966 assert(image->blob->type != UndefinedStream);
3971 switch (image->blob->type)
3973 case UndefinedStream:
3976 case StandardStream:
3983 count=(ssize_t) fwrite((const char *) data,1,length,
3989 c=putc((int) *p++,image->blob->file);
3996 c=putc((int) *p++,image->blob->file);
4008 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4013 count=(ssize_t) gzwrite(image->blob->file,(void *) data,
4014 (unsigned int) length);
4019 c=gzputc(image->blob->file,(int) *p++);
4026 c=gzputc(image->blob->file,(int) *p++);
4039 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4040 count=(ssize_t) BZ2_bzwrite((BZFILE *) image->blob->file,(void *) data,
4047 count=(ssize_t) image->blob->stream(image,data,length);
4052 register unsigned char
4055 if ((image->blob->offset+(MagickOffsetType) length) >=
4056 (MagickOffsetType) image->blob->extent)
4058 if (image->blob->mapped != MagickFalse)
4060 image->blob->quantum<<=1;
4061 image->blob->extent+=length+image->blob->quantum;
4062 image->blob->data=(unsigned char *) ResizeQuantumMemory(
4063 image->blob->data,image->blob->extent+1,sizeof(*image->blob->data));
4064 (void) SyncBlob(image);
4065 if (image->blob->data == (unsigned char *) NULL)
4067 (void) DetachBlob(image->blob);
4071 q=image->blob->data+image->blob->offset;
4072 (void) memcpy(q,p,length);
4073 image->blob->offset+=length;
4074 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
4075 image->blob->length=(size_t) image->blob->offset;
4076 count=(ssize_t) length;
4083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4087 + W r i t e B l o b B y t e %
4091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4093 % WriteBlobByte() write an integer to a blob. It returns the number of bytes
4094 % written (either 0 or 1);
4096 % The format of the WriteBlobByte method is:
4098 % ssize_t WriteBlobByte(Image *image,const unsigned char value)
4100 % A description of each parameter follows.
4102 % o image: the image.
4104 % o value: Specifies the value to write.
4107 MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
4109 assert(image != (Image *) NULL);
4110 assert(image->signature == MagickSignature);
4111 return(WriteBlobStream(image,1,&value));
4115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4119 + W r i t e B l o b F l o a t %
4123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4125 % WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
4126 % specified by the endian member of the image structure.
4128 % The format of the WriteBlobFloat method is:
4130 % ssize_t WriteBlobFloat(Image *image,const float value)
4132 % A description of each parameter follows.
4134 % o image: the image.
4136 % o value: Specifies the value to write.
4139 MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
4150 quantum.unsigned_value=0U;
4151 quantum.float_value=value;
4152 return(WriteBlobLong(image,quantum.unsigned_value));
4156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4160 + W r i t e B l o b L o n g %
4164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4166 % WriteBlobLong() writes a ssize_t value as a 32-bit quantity in the byte-order
4167 % specified by the endian member of the image structure.
4169 % The format of the WriteBlobLong method is:
4171 % ssize_t WriteBlobLong(Image *image,const unsigned int value)
4173 % A description of each parameter follows.
4175 % o image: the image.
4177 % o value: Specifies the value to write.
4180 MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
4185 assert(image != (Image *) NULL);
4186 assert(image->signature == MagickSignature);
4187 if (image->endian == LSBEndian)
4189 buffer[0]=(unsigned char) value;
4190 buffer[1]=(unsigned char) (value >> 8);
4191 buffer[2]=(unsigned char) (value >> 16);
4192 buffer[3]=(unsigned char) (value >> 24);
4193 return(WriteBlobStream(image,4,buffer));
4195 buffer[0]=(unsigned char) (value >> 24);
4196 buffer[1]=(unsigned char) (value >> 16);
4197 buffer[2]=(unsigned char) (value >> 8);
4198 buffer[3]=(unsigned char) value;
4199 return(WriteBlobStream(image,4,buffer));
4203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4207 + W r i t e B l o b S h o r t %
4211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4213 % WriteBlobShort() writes a short value as a 16-bit quantity in the
4214 % byte-order specified by the endian member of the image structure.
4216 % The format of the WriteBlobShort method is:
4218 % ssize_t WriteBlobShort(Image *image,const unsigned short 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 WriteBlobShort(Image *image,const unsigned short 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 return(WriteBlobStream(image,2,buffer));
4240 buffer[0]=(unsigned char) (value >> 8);
4241 buffer[1]=(unsigned char) value;
4242 return(WriteBlobStream(image,2,buffer));
4246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4250 + W r i t e B l o b L S B L o n g %
4254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4256 % WriteBlobLSBLong() writes a ssize_t value as a 32-bit quantity in
4257 % least-significant byte first order.
4259 % The format of the WriteBlobLSBLong method is:
4261 % ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4263 % A description of each parameter follows.
4265 % o image: the image.
4267 % o value: Specifies the value to write.
4270 MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4275 assert(image != (Image *) NULL);
4276 assert(image->signature == MagickSignature);
4277 buffer[0]=(unsigned char) value;
4278 buffer[1]=(unsigned char) (value >> 8);
4279 buffer[2]=(unsigned char) (value >> 16);
4280 buffer[3]=(unsigned char) (value >> 24);
4281 return(WriteBlobStream(image,4,buffer));
4285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4289 + W r i t e B l o b L S B S h o r t %
4293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4295 % WriteBlobLSBShort() writes a ssize_t value as a 16-bit quantity in
4296 % least-significant byte first order.
4298 % The format of the WriteBlobLSBShort method is:
4300 % ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4302 % A description of each parameter follows.
4304 % o image: the image.
4306 % o value: Specifies the value to write.
4309 MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4314 assert(image != (Image *) NULL);
4315 assert(image->signature == MagickSignature);
4316 buffer[0]=(unsigned char) value;
4317 buffer[1]=(unsigned char) (value >> 8);
4318 return(WriteBlobStream(image,2,buffer));
4322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4326 + W r i t e B l o b M S B L o n g %
4330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4332 % WriteBlobMSBLong() writes a ssize_t value as a 32-bit quantity in
4333 % most-significant byte first order.
4335 % The format of the WriteBlobMSBLong method is:
4337 % ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4339 % A description of each parameter follows.
4341 % o value: Specifies the value to write.
4343 % o image: the image.
4346 MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4351 assert(image != (Image *) NULL);
4352 assert(image->signature == MagickSignature);
4353 buffer[0]=(unsigned char) (value >> 24);
4354 buffer[1]=(unsigned char) (value >> 16);
4355 buffer[2]=(unsigned char) (value >> 8);
4356 buffer[3]=(unsigned char) value;
4357 return(WriteBlobStream(image,4,buffer));
4361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4365 + W r i t e B l o b M S B L o n g L o n g %
4369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4371 % WriteBlobMSBLongLong() writes a long long value as a 64-bit quantity in
4372 % most-significant byte first order.
4374 % The format of the WriteBlobMSBLongLong method is:
4376 % ssize_t WriteBlobMSBLongLong(Image *image,const MagickSizeType value)
4378 % A description of each parameter follows.
4380 % o value: Specifies the value to write.
4382 % o image: the image.
4385 MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
4386 const MagickSizeType value)
4391 assert(image != (Image *) NULL);
4392 assert(image->signature == MagickSignature);
4393 buffer[0]=(unsigned char) (value >> 56);
4394 buffer[1]=(unsigned char) (value >> 48);
4395 buffer[2]=(unsigned char) (value >> 40);
4396 buffer[3]=(unsigned char) (value >> 32);
4397 buffer[4]=(unsigned char) (value >> 24);
4398 buffer[5]=(unsigned char) (value >> 16);
4399 buffer[6]=(unsigned char) (value >> 8);
4400 buffer[7]=(unsigned char) value;
4401 return(WriteBlobStream(image,8,buffer));
4405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4409 + W r i t e B l o b M S B S h o r t %
4413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4415 % WriteBlobMSBShort() writes a ssize_t value as a 16-bit quantity in
4416 % most-significant byte first order.
4418 % The format of the WriteBlobMSBShort method is:
4420 % ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4422 % A description of each parameter follows.
4424 % o value: Specifies the value to write.
4426 % o file: Specifies the file to write the data to.
4429 MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4434 assert(image != (Image *) NULL);
4435 assert(image->signature == MagickSignature);
4436 buffer[0]=(unsigned char) (value >> 8);
4437 buffer[1]=(unsigned char) value;
4438 return(WriteBlobStream(image,2,buffer));
4442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4446 + W r i t e B l o b S t r i n g %
4450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4452 % WriteBlobString() write a string to a blob. It returns the number of
4453 % characters written.
4455 % The format of the WriteBlobString method is:
4457 % ssize_t WriteBlobString(Image *image,const char *string)
4459 % A description of each parameter follows.
4461 % o image: the image.
4463 % o string: Specifies the string to write.
4466 MagickExport ssize_t WriteBlobString(Image *image,const char *string)
4468 assert(image != (Image *) NULL);
4469 assert(image->signature == MagickSignature);
4470 assert(string != (const char *) NULL);
4471 return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));