47 #include "MagickCore/studio.h"
48 #include "MagickCore/blob.h"
49 #include "MagickCore/blob-private.h"
50 #include "MagickCore/cache.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/delegate.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/locale_.h"
60 #include "MagickCore/log.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/memory-private.h"
64 #include "MagickCore/nt-base-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/policy.h"
67 #include "MagickCore/resource_.h"
68 #include "MagickCore/semaphore.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/string-private.h"
71 #include "MagickCore/timer-private.h"
72 #include "MagickCore/token.h"
73 #include "MagickCore/utility.h"
74 #include "MagickCore/utility-private.h"
75 #if defined(MAGICKCORE_ZLIB_DELEGATE)
78 #if defined(MAGICKCORE_BZLIB_DELEGATE)
85 #define MagickMaxBlobExtent (8*8192)
86 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
87 # define MAP_ANONYMOUS MAP_ANON
89 #if !defined(MAP_FAILED)
90 #define MAP_FAILED ((void *) -1)
94 #define _O_BINARY O_BINARY
104 #if defined(MAGICKCORE_ZLIB_DELEGATE)
109 #if defined(MAGICKCORE_BZLIB_DELEGATE)
229 magick_unreferenced(exception);
231 sizeof(*custom_stream));
232 (void) memset(custom_stream,0,
sizeof(*custom_stream));
233 custom_stream->signature=MagickCoreSignature;
234 return(custom_stream);
264 MagickExport
void AttachBlob(
BlobInfo *blob_info,
const void *blob,
267 assert(blob_info != (
BlobInfo *) NULL);
268 if (IsEventLogging() != MagickFalse)
269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
270 blob_info->length=length;
271 blob_info->extent=length;
272 blob_info->quantum=(size_t) MagickMaxBlobExtent;
274 blob_info->type=BlobStream;
275 blob_info->file_info.file=(FILE *) NULL;
276 blob_info->data=(
unsigned char *) blob;
277 blob_info->mapped=MagickFalse;
305 MagickExport
void AttachCustomStream(
BlobInfo *blob_info,
308 assert(blob_info != (
BlobInfo *) NULL);
310 assert(custom_stream->signature == MagickCoreSignature);
311 if (IsEventLogging() != MagickFalse)
312 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
313 blob_info->type=CustomStream;
314 blob_info->custom_stream=custom_stream;
347 MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
359 assert(filename != (
const char *) NULL);
360 assert(blob != (
const void *) NULL);
361 if (IsEventLogging() != MagickFalse)
362 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
363 if (*filename ==
'\0')
364 file=AcquireUniqueFileResource(filename);
366 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
369 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
372 for (i=0; i < length; i+=count)
374 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
384 if ((file == -1) || (i < length))
386 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
423 MagickExport
Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
439 assert(image_info != (
ImageInfo *) NULL);
440 assert(image_info->signature == MagickCoreSignature);
442 if (IsEventLogging() != MagickFalse)
443 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
444 image_info->filename);
445 if ((blob == (
const void *) NULL) || (length == 0))
447 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
448 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
449 return((
Image *) NULL);
451 blob_info=CloneImageInfo(image_info);
452 blob_info->blob=(
void *) blob;
453 blob_info->length=length;
454 if (*blob_info->magick ==
'\0')
455 (void) SetImageInfo(blob_info,0,exception);
456 magick_info=GetMagickInfo(blob_info->magick,exception);
459 (void) ThrowMagickException(exception,GetMagickModule(),
460 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
462 blob_info=DestroyImageInfo(blob_info);
463 return((
Image *) NULL);
465 if (GetMagickBlobSupport(magick_info) != MagickFalse)
468 filename[MagickPathExtent];
473 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
474 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
475 blob_info->magick,filename);
476 image=ReadImage(blob_info,exception);
477 if (image != (
Image *) NULL)
478 (void) DetachBlob(image->blob);
479 blob_info=DestroyImageInfo(blob_info);
485 blob_info->blob=(
void *) NULL;
487 *blob_info->filename=
'\0';
488 status=BlobToFile(blob_info->filename,blob,length,exception);
489 if (status == MagickFalse)
491 (void) RelinquishUniqueFileResource(blob_info->filename);
492 blob_info=DestroyImageInfo(blob_info);
493 return((
Image *) NULL);
495 clone_info=CloneImageInfo(blob_info);
496 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
497 blob_info->magick,blob_info->filename);
498 image=ReadImage(clone_info,exception);
499 if (image != (
Image *) NULL)
507 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
509 (void) CopyMagickString(images->filename,image_info->filename,
511 (void) CopyMagickString(images->magick_filename,image_info->filename,
513 (void) CopyMagickString(images->magick,magick_info->name,
515 images=GetNextImageInList(images);
518 clone_info=DestroyImageInfo(clone_info);
519 (void) RelinquishUniqueFileResource(blob_info->filename);
520 blob_info=DestroyImageInfo(blob_info);
555 clone_info=(
BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
556 GetBlobInfo(clone_info);
559 semaphore=clone_info->semaphore;
560 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
561 if (blob_info->mapped != MagickFalse)
562 (void) AcquireMagickResource(MapResource,blob_info->length);
563 clone_info->semaphore=semaphore;
564 LockSemaphoreInfo(clone_info->semaphore);
565 clone_info->reference_count=1;
566 UnlockSemaphoreInfo(clone_info->semaphore);
593 static inline void ThrowBlobException(
BlobInfo *blob_info)
595 if ((blob_info->status == MagickFalse) && (errno != 0))
596 blob_info->error_number=errno;
597 blob_info->status=MagickTrue;
600 MagickExport MagickBooleanType CloseBlob(
Image *image)
603 *magick_restrict blob_info;
611 assert(image != (
Image *) NULL);
612 assert(image->signature == MagickCoreSignature);
613 if (IsEventLogging() != MagickFalse)
614 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
615 blob_info=image->blob;
616 if ((blob_info == (
BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
618 status=SyncBlob(image);
619 switch (blob_info->type)
621 case UndefinedStream:
627 if (blob_info->synchronize != MagickFalse)
629 status=fflush(blob_info->file_info.file);
631 ThrowBlobException(blob_info);
632 status=fsync(fileno(blob_info->file_info.file));
634 ThrowBlobException(blob_info);
636 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
637 ThrowBlobException(blob_info);
642 #if defined(MAGICKCORE_ZLIB_DELEGATE)
644 (void) gzerror(blob_info->file_info.gzfile,&status);
646 ThrowBlobException(blob_info);
652 #if defined(MAGICKCORE_BZLIB_DELEGATE)
654 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
656 ThrowBlobException(blob_info);
664 if (blob_info->file_info.file != (FILE *) NULL)
666 if (blob_info->synchronize != MagickFalse)
668 status=fflush(blob_info->file_info.file);
670 ThrowBlobException(blob_info);
671 status=fsync(fileno(blob_info->file_info.file));
673 ThrowBlobException(blob_info);
675 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
676 ThrowBlobException(blob_info);
683 blob_info->size=GetBlobSize(image);
684 image->extent=blob_info->size;
685 blob_info->eof=MagickFalse;
687 blob_info->mode=UndefinedBlobMode;
688 if (blob_info->exempt != MagickFalse)
690 blob_info->type=UndefinedStream;
691 return(blob_info->status);
693 switch (blob_info->type)
695 case UndefinedStream:
700 if (blob_info->file_info.file != (FILE *) NULL)
702 status=fclose(blob_info->file_info.file);
704 ThrowBlobException(blob_info);
710 #if defined(MAGICKCORE_HAVE_PCLOSE)
711 status=pclose(blob_info->file_info.file);
713 ThrowBlobException(blob_info);
719 #if defined(MAGICKCORE_ZLIB_DELEGATE)
720 status=gzclose(blob_info->file_info.gzfile);
722 ThrowBlobException(blob_info);
728 #if defined(MAGICKCORE_BZLIB_DELEGATE)
729 BZ2_bzclose(blob_info->file_info.bzfile);
737 if (blob_info->file_info.file != (FILE *) NULL)
739 status=fclose(blob_info->file_info.file);
741 ThrowBlobException(blob_info);
748 (void) DetachBlob(blob_info);
749 return(blob_info->status);
778 MagickExport
Image *CustomStreamToImage(
const ImageInfo *image_info,
790 assert(image_info != (
ImageInfo *) NULL);
791 assert(image_info->signature == MagickCoreSignature);
793 assert(image_info->custom_stream->signature == MagickCoreSignature);
794 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
796 if (IsEventLogging() != MagickFalse)
797 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
798 image_info->filename);
799 blob_info=CloneImageInfo(image_info);
800 if (*blob_info->magick ==
'\0')
801 (void) SetImageInfo(blob_info,0,exception);
802 magick_info=GetMagickInfo(blob_info->magick,exception);
805 (void) ThrowMagickException(exception,GetMagickModule(),
806 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
808 blob_info=DestroyImageInfo(blob_info);
809 return((
Image *) NULL);
811 image=(
Image *) NULL;
812 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
813 (*blob_info->filename !=
'\0'))
816 filename[MagickPathExtent];
822 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
823 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
824 blob_info->magick,filename);
825 image=ReadImage(blob_info,exception);
826 if (image != (
Image *) NULL)
827 (void) CloseBlob(image);
832 unique[MagickPathExtent];
847 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
849 if (blob == (
unsigned char *) NULL)
851 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
852 image_info->filename);
853 blob_info=DestroyImageInfo(blob_info);
854 return((
Image *) NULL);
856 file=AcquireUniqueFileResource(unique);
859 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
860 image_info->filename);
861 blob=(
unsigned char *) RelinquishMagickMemory(blob);
862 blob_info=DestroyImageInfo(blob_info);
863 return((
Image *) NULL);
865 clone_info=CloneImageInfo(blob_info);
866 blob_info->file=fdopen(file,
"wb+");
867 if (blob_info->file != (FILE *) NULL)
872 count=(ssize_t) MagickMaxBufferExtent;
873 while (count == (ssize_t) MagickMaxBufferExtent)
875 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
876 image_info->custom_stream->data);
877 count=(ssize_t) write(file,(
const char *) blob,(size_t) count);
879 (void) fclose(blob_info->file);
880 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
881 "%s:%s",blob_info->magick,unique);
882 image=ReadImage(clone_info,exception);
883 if (image != (
Image *) NULL)
891 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
893 (void) CopyMagickString(images->filename,image_info->filename,
895 (void) CopyMagickString(images->magick_filename,
896 image_info->filename,MagickPathExtent);
897 (void) CopyMagickString(images->magick,magick_info->name,
899 (void) CloseBlob(images);
900 images=GetNextImageInList(images);
904 clone_info=DestroyImageInfo(clone_info);
905 blob=(
unsigned char *) RelinquishMagickMemory(blob);
906 (void) RelinquishUniqueFileResource(unique);
908 blob_info=DestroyImageInfo(blob_info);
934 MagickExport
void DestroyBlob(
Image *image)
937 *magick_restrict blob_info;
942 assert(image != (
Image *) NULL);
943 assert(image->signature == MagickCoreSignature);
944 assert(image->blob != (
BlobInfo *) NULL);
945 assert(image->blob->signature == MagickCoreSignature);
946 if (IsEventLogging() != MagickFalse)
947 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
948 blob_info=image->blob;
950 LockSemaphoreInfo(blob_info->semaphore);
951 blob_info->reference_count--;
952 assert(blob_info->reference_count >= 0);
953 if (blob_info->reference_count == 0)
955 UnlockSemaphoreInfo(blob_info->semaphore);
956 if (destroy == MagickFalse)
961 (void) CloseBlob(image);
962 if (blob_info->mapped != MagickFalse)
964 (void) UnmapBlob(blob_info->data,blob_info->length);
965 RelinquishMagickResource(MapResource,blob_info->length);
968 RelinquishSemaphoreInfo(&blob_info->semaphore);
969 blob_info->signature=(~MagickCoreSignature);
970 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
1000 assert(custom_stream->signature == MagickCoreSignature);
1001 if (IsEventLogging() != MagickFalse)
1002 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1003 custom_stream->signature=(~MagickCoreSignature);
1005 return(custom_stream);
1030 MagickExport
void *DetachBlob(
BlobInfo *blob_info)
1035 assert(blob_info != (
BlobInfo *) NULL);
1036 if (IsEventLogging() != MagickFalse)
1037 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1038 if (blob_info->mapped != MagickFalse)
1040 (void) UnmapBlob(blob_info->data,blob_info->length);
1041 blob_info->data=NULL;
1042 RelinquishMagickResource(MapResource,blob_info->length);
1044 blob_info->mapped=MagickFalse;
1045 blob_info->length=0;
1046 blob_info->offset=0;
1047 blob_info->eof=MagickFalse;
1049 blob_info->exempt=MagickFalse;
1050 blob_info->type=UndefinedStream;
1051 blob_info->file_info.file=(FILE *) NULL;
1052 data=blob_info->data;
1053 blob_info->data=(
unsigned char *) NULL;
1054 blob_info->stream=(StreamHandler) NULL;
1083 MagickExport
void DisassociateBlob(
Image *image)
1086 *magick_restrict blob_info,
1092 assert(image != (
Image *) NULL);
1093 assert(image->signature == MagickCoreSignature);
1094 assert(image->blob != (
BlobInfo *) NULL);
1095 assert(image->blob->signature == MagickCoreSignature);
1096 if (IsEventLogging() != MagickFalse)
1097 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1098 blob_info=image->blob;
1100 LockSemaphoreInfo(blob_info->semaphore);
1101 assert(blob_info->reference_count >= 0);
1102 if (blob_info->reference_count > 1)
1104 UnlockSemaphoreInfo(blob_info->semaphore);
1105 if (clone == MagickFalse)
1107 clone_info=CloneBlobInfo(blob_info);
1109 image->blob=clone_info;
1137 MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
1138 const MagickSizeType length)
1150 buffer[MagickMinBufferExtent >> 1];
1152 assert(image != (
Image *) NULL);
1153 assert(image->signature == MagickCoreSignature);
1154 if (length != (MagickSizeType) ((MagickOffsetType) length))
1155 return(MagickFalse);
1157 for (i=0; i < (MagickOffsetType) length; i+=count)
1159 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1160 (void) ReadBlobStream(image,quantum,buffer,&count);
1168 return(i < (MagickOffsetType) length ? MagickFalse : MagickTrue);
1195 MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
1197 assert(image != (
Image *) NULL);
1198 assert(image->signature == MagickCoreSignature);
1199 assert(duplicate != (
Image *) NULL);
1200 assert(duplicate->signature == MagickCoreSignature);
1201 if (IsEventLogging() != MagickFalse)
1202 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1204 image->blob=ReferenceBlob(duplicate->blob);
1230 MagickExport
int EOFBlob(
const Image *image)
1233 *magick_restrict blob_info;
1235 assert(image != (
Image *) NULL);
1236 assert(image->signature == MagickCoreSignature);
1237 assert(image->blob != (
BlobInfo *) NULL);
1238 assert(image->blob->type != UndefinedStream);
1239 if (IsEventLogging() != MagickFalse)
1240 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1241 blob_info=image->blob;
1242 switch (blob_info->type)
1244 case UndefinedStream:
1245 case StandardStream:
1250 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1256 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1257 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1264 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1269 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1270 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1276 blob_info->eof=MagickFalse;
1284 return((
int) blob_info->eof);
1310 MagickExport
int ErrorBlob(
const Image *image)
1313 *magick_restrict blob_info;
1315 assert(image != (
Image *) NULL);
1316 assert(image->signature == MagickCoreSignature);
1317 assert(image->blob != (
BlobInfo *) NULL);
1318 assert(image->blob->type != UndefinedStream);
1319 if (IsEventLogging() != MagickFalse)
1320 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1321 blob_info=image->blob;
1322 switch (blob_info->type)
1324 case UndefinedStream:
1325 case StandardStream:
1330 blob_info->error=ferror(blob_info->file_info.file);
1335 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1336 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1342 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1343 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1357 return(blob_info->error);
1395 MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1422 assert(filename != (
const char *) NULL);
1424 if (IsEventLogging() != MagickFalse)
1425 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1427 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1428 if (status == MagickFalse)
1431 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1432 "NotAuthorized",
"`%s'",filename);
1436 if (LocaleCompare(filename,
"-") != 0)
1438 status=GetPathAttributes(filename,&attributes);
1439 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1441 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1444 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1448 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1451 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1453 if ((file == fileno(stdin)) || (offset < 0) ||
1454 (offset != (MagickOffsetType) ((ssize_t) offset)))
1465 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1466 quantum=(size_t) MagickMaxBufferExtent;
1467 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1468 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1469 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1470 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1472 count=read(file,blob+i,quantum);
1479 if (~((
size_t) i) < (quantum+1))
1481 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1484 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+quantum+1,
1486 if ((
size_t) (i+count) >= extent)
1489 if (LocaleCompare(filename,
"-") != 0)
1491 if (blob == (
unsigned char *) NULL)
1493 (void) ThrowMagickException(exception,GetMagickModule(),
1494 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1499 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1500 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1503 *length=(size_t) MagickMin(i+count,extent);
1507 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1508 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1509 blob=(
unsigned char *) NULL;
1510 if (~(*length) >= (MagickPathExtent-1))
1511 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1513 if (blob == (
unsigned char *) NULL)
1516 (void) ThrowMagickException(exception,GetMagickModule(),
1517 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1520 map=MapBlob(file,ReadMode,0,*length);
1521 if (map != (
unsigned char *) NULL)
1523 (void) memcpy(blob,map,*length);
1524 (void) UnmapBlob(map,*length);
1528 (void) lseek(file,0,SEEK_SET);
1529 for (i=0; i < *length; i+=count)
1531 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1543 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1544 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1549 if (LocaleCompare(filename,
"-") != 0)
1553 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1554 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1583 static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1584 const void *magick_restrict data)
1587 *magick_restrict blob_info;
1595 assert(image->blob != (
BlobInfo *) NULL);
1596 assert(image->blob->type != UndefinedStream);
1597 assert(data != NULL);
1598 blob_info=image->blob;
1599 if (blob_info->type != BlobStream)
1600 return(WriteBlob(image,length,(
const unsigned char *) data));
1601 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1602 if (extent >= blob_info->extent)
1604 extent=blob_info->extent+blob_info->quantum+length;
1605 blob_info->quantum<<=1;
1606 if (SetBlobExtent(image,extent) == MagickFalse)
1609 q=blob_info->data+blob_info->offset;
1610 (void) memcpy(q,data,length);
1611 blob_info->offset+=length;
1612 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1613 blob_info->length=(
size_t) blob_info->offset;
1614 return((ssize_t) length);
1617 MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename,
1639 assert(image != (
const Image *) NULL);
1640 assert(image->signature == MagickCoreSignature);
1641 assert(filename != (
const char *) NULL);
1642 if (IsEventLogging() != MagickFalse)
1643 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1644 status=IsRightsAuthorized(PathPolicyDomain,WritePolicyRights,filename);
1645 if (status == MagickFalse)
1648 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1649 "NotAuthorized",
"`%s'",filename);
1650 return(MagickFalse);
1653 if (LocaleCompare(filename,
"-") != 0)
1654 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1657 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1658 return(MagickFalse);
1660 quantum=(size_t) MagickMaxBufferExtent;
1661 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1662 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1663 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1664 if (blob == (
unsigned char *) NULL)
1667 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1669 return(MagickFalse);
1673 count=read(file,blob,quantum);
1680 length=(size_t) count;
1681 count=WriteBlobStream(image,length,blob);
1682 if (count != (ssize_t) length)
1684 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1690 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1691 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1718 MagickExport MagickBooleanType GetBlobError(
const Image *image)
1720 assert(image != (
const Image *) NULL);
1721 assert(image->signature == MagickCoreSignature);
1722 if (IsEventLogging() != MagickFalse)
1723 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1724 if ((image->blob->status != MagickFalse) && (image->blob->error_number != 0))
1725 errno=image->blob->error_number;
1726 return(image->blob->status);
1751 MagickExport FILE *GetBlobFileHandle(
const Image *image)
1753 assert(image != (
const Image *) NULL);
1754 assert(image->signature == MagickCoreSignature);
1755 return(image->blob->file_info.file);
1780 MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1782 assert(blob_info != (
BlobInfo *) NULL);
1783 (void) memset(blob_info,0,
sizeof(*blob_info));
1784 blob_info->type=UndefinedStream;
1785 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1786 blob_info->properties.st_mtime=GetMagickTime();
1787 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1788 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1789 blob_info->reference_count=1;
1790 blob_info->semaphore=AcquireSemaphoreInfo();
1791 blob_info->signature=MagickCoreSignature;
1816 MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1818 assert(image != (
Image *) NULL);
1819 assert(image->signature == MagickCoreSignature);
1820 if (IsEventLogging() != MagickFalse)
1821 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1822 return(&image->blob->properties);
1848 MagickExport MagickSizeType GetBlobSize(
const Image *image)
1851 *magick_restrict blob_info;
1856 assert(image != (
Image *) NULL);
1857 assert(image->signature == MagickCoreSignature);
1858 assert(image->blob != (
BlobInfo *) NULL);
1859 if (IsEventLogging() != MagickFalse)
1860 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1861 blob_info=image->blob;
1863 switch (blob_info->type)
1865 case UndefinedStream:
1866 case StandardStream:
1868 extent=blob_info->size;
1876 extent=(MagickSizeType) blob_info->properties.st_size;
1878 extent=blob_info->size;
1879 file_descriptor=fileno(blob_info->file_info.file);
1880 if (file_descriptor == -1)
1882 if (fstat(file_descriptor,&blob_info->properties) == 0)
1883 extent=(MagickSizeType) blob_info->properties.st_size;
1888 extent=blob_info->size;
1897 status=GetPathAttributes(image->filename,&blob_info->properties);
1898 if (status != MagickFalse)
1899 extent=(MagickSizeType) blob_info->properties.st_size;
1906 extent=(MagickSizeType) blob_info->length;
1911 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1912 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1917 offset=blob_info->custom_stream->teller(
1918 blob_info->custom_stream->data);
1919 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1920 blob_info->custom_stream->data);
1921 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1922 blob_info->custom_stream->data);
1952 MagickExport
void *GetBlobStreamData(
const Image *image)
1954 assert(image != (
const Image *) NULL);
1955 assert(image->signature == MagickCoreSignature);
1956 return(image->blob->data);
1981 MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1983 assert(image != (
const Image *) NULL);
1984 assert(image->signature == MagickCoreSignature);
1985 if (IsEventLogging() != MagickFalse)
1986 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1987 return(image->blob->stream);
2023 MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2038 assert(image_info != (
const ImageInfo *) NULL);
2039 assert(image_info->signature == MagickCoreSignature);
2040 assert(image != (
Image *) NULL);
2041 assert(image->signature == MagickCoreSignature);
2043 if (IsEventLogging() != MagickFalse)
2044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2045 image_info->filename);
2047 blob=(
unsigned char *) NULL;
2048 blob_info=CloneImageInfo(image_info);
2049 blob_info->adjoin=MagickFalse;
2050 (void) SetImageInfo(blob_info,1,exception);
2051 if (*blob_info->magick !=
'\0')
2052 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2053 magick_info=GetMagickInfo(image->magick,exception);
2054 if (magick_info == (
const MagickInfo *) NULL)
2056 (void) ThrowMagickException(exception,GetMagickModule(),
2057 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
2059 blob_info=DestroyImageInfo(blob_info);
2062 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2063 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2068 blob_info->length=0;
2069 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2070 sizeof(
unsigned char));
2071 if (blob_info->blob == NULL)
2072 (void) ThrowMagickException(exception,GetMagickModule(),
2073 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2076 (void) CloseBlob(image);
2077 image->blob->exempt=MagickTrue;
2078 *image->filename=
'\0';
2079 status=WriteImage(blob_info,image,exception);
2080 *length=image->blob->length;
2081 blob=DetachBlob(image->blob);
2082 if (blob == (
void *) NULL)
2083 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2084 else if (status == MagickFalse)
2085 blob=RelinquishMagickMemory(blob);
2087 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2093 unique[MagickPathExtent];
2101 file=AcquireUniqueFileResource(unique);
2104 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2105 image_info->filename);
2109 blob_info->file=fdopen(file,
"wb");
2110 if (blob_info->file != (FILE *) NULL)
2112 (void) FormatLocaleString(image->filename,MagickPathExtent,
2113 "%s:%s",image->magick,unique);
2114 status=WriteImage(blob_info,image,exception);
2115 (void) CloseBlob(image);
2116 (void) fclose(blob_info->file);
2117 if (status != MagickFalse)
2118 blob=FileToBlob(unique,~0UL,length,exception);
2120 (void) RelinquishUniqueFileResource(unique);
2123 blob_info=DestroyImageInfo(blob_info);
2155 MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,
Image *image,
2168 assert(image_info != (
const ImageInfo *) NULL);
2169 assert(image_info->signature == MagickCoreSignature);
2170 assert(image != (
Image *) NULL);
2171 assert(image->signature == MagickCoreSignature);
2173 assert(image_info->custom_stream->signature == MagickCoreSignature);
2174 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2176 if (IsEventLogging() != MagickFalse)
2177 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2178 image_info->filename);
2179 clone_info=CloneImageInfo(image_info);
2180 clone_info->adjoin=MagickFalse;
2181 (void) SetImageInfo(clone_info,1,exception);
2182 if (*clone_info->magick !=
'\0')
2183 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2184 magick_info=GetMagickInfo(image->magick,exception);
2185 if (magick_info == (
const MagickInfo *) NULL)
2187 (void) ThrowMagickException(exception,GetMagickModule(),
2188 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2190 clone_info=DestroyImageInfo(clone_info);
2193 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2194 blob_support=GetMagickBlobSupport(magick_info);
2195 if ((blob_support != MagickFalse) &&
2196 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2198 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2199 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2200 blob_support=MagickFalse;
2202 if (blob_support != MagickFalse)
2207 (void) CloseBlob(image);
2208 *image->filename=
'\0';
2209 (void) WriteImage(clone_info,image,exception);
2210 (void) CloseBlob(image);
2215 unique[MagickPathExtent];
2227 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2229 if (blob == (
unsigned char *) NULL)
2231 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2232 image_info->filename);
2233 clone_info=DestroyImageInfo(clone_info);
2236 file=AcquireUniqueFileResource(unique);
2239 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2240 image_info->filename);
2241 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2242 clone_info=DestroyImageInfo(clone_info);
2245 clone_info->file=fdopen(file,
"wb+");
2246 if (clone_info->file != (FILE *) NULL)
2251 (void) FormatLocaleString(image->filename,MagickPathExtent,
2252 "%s:%s",image->magick,unique);
2253 status=WriteImage(clone_info,image,exception);
2254 (void) CloseBlob(image);
2255 if (status != MagickFalse)
2257 (void) fseek(clone_info->file,0,SEEK_SET);
2258 count=(ssize_t) MagickMaxBufferExtent;
2259 while (count == (ssize_t) MagickMaxBufferExtent)
2261 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2263 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2264 image_info->custom_stream->data);
2267 (void) fclose(clone_info->file);
2269 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2270 (void) RelinquishUniqueFileResource(unique);
2272 clone_info=DestroyImageInfo(clone_info);
2303 MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
2328 assert(image != (
Image *) NULL);
2329 assert(image->signature == MagickCoreSignature);
2330 assert(image->blob != (
BlobInfo *) NULL);
2331 assert(image->blob->type != UndefinedStream);
2332 assert(filename != (
const char *) NULL);
2333 if (IsEventLogging() != MagickFalse)
2334 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2335 if (*filename ==
'\0')
2336 file=AcquireUniqueFileResource(filename);
2338 if (LocaleCompare(filename,
"-") == 0)
2339 file=fileno(stdout);
2341 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
2344 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2345 return(MagickFalse);
2347 quantum=(size_t) MagickMaxBufferExtent;
2348 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2349 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2350 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2351 if (buffer == (
unsigned char *) NULL)
2354 (void) ThrowMagickException(exception,GetMagickModule(),
2355 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2356 return(MagickFalse);
2359 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2360 for (i=0; count > 0; )
2362 length=(size_t) count;
2363 for (i=0; i < length; i+=count)
2365 count=write(file,p+i,(
size_t) (length-i));
2375 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2377 if (LocaleCompare(filename,
"-") != 0)
2379 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2380 if ((file == -1) || (i < length))
2384 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2385 return(MagickFalse);
2425 MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,
Image *images,
2440 assert(image_info != (
const ImageInfo *) NULL);
2441 assert(image_info->signature == MagickCoreSignature);
2442 assert(images != (
Image *) NULL);
2443 assert(images->signature == MagickCoreSignature);
2445 if (IsEventLogging() != MagickFalse)
2446 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2447 image_info->filename);
2449 blob=(
unsigned char *) NULL;
2450 clone_info=CloneImageInfo(image_info);
2451 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2453 if (*clone_info->magick !=
'\0')
2454 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2455 magick_info=GetMagickInfo(images->magick,exception);
2456 if (magick_info == (
const MagickInfo *) NULL)
2458 (void) ThrowMagickException(exception,GetMagickModule(),
2459 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
2461 clone_info=DestroyImageInfo(clone_info);
2464 if (GetMagickAdjoin(magick_info) == MagickFalse)
2466 clone_info=DestroyImageInfo(clone_info);
2467 return(ImageToBlob(image_info,images,length,exception));
2469 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2470 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2475 clone_info->length=0;
2476 clone_info->blob=(
void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2477 sizeof(
unsigned char));
2478 if (clone_info->blob == (
void *) NULL)
2479 (
void) ThrowMagickException(exception,GetMagickModule(),
2480 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2483 (void) CloseBlob(images);
2484 images->blob->exempt=MagickTrue;
2485 *images->filename=
'\0';
2486 status=WriteImages(clone_info,images,images->filename,exception);
2487 *length=images->blob->length;
2488 blob=DetachBlob(images->blob);
2489 if (blob == (
void *) NULL)
2490 clone_info->blob=RelinquishMagickMemory(clone_info->blob);
2491 else if (status == MagickFalse)
2492 blob=RelinquishMagickMemory(blob);
2494 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2500 filename[MagickPathExtent],
2501 unique[MagickPathExtent];
2509 file=AcquireUniqueFileResource(unique);
2512 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2513 image_info->filename);
2517 clone_info->file=fdopen(file,
"wb");
2518 if (clone_info->file != (FILE *) NULL)
2520 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2521 images->magick,unique);
2522 status=WriteImages(clone_info,images,filename,exception);
2523 (void) CloseBlob(images);
2524 (void) fclose(clone_info->file);
2525 if (status != MagickFalse)
2526 blob=FileToBlob(unique,~0UL,length,exception);
2528 (void) RelinquishUniqueFileResource(unique);
2531 clone_info=DestroyImageInfo(clone_info);
2563 MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2576 assert(image_info != (
const ImageInfo *) NULL);
2577 assert(image_info->signature == MagickCoreSignature);
2578 assert(images != (
Image *) NULL);
2579 assert(images->signature == MagickCoreSignature);
2581 assert(image_info->custom_stream->signature == MagickCoreSignature);
2582 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2584 if (IsEventLogging() != MagickFalse)
2585 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2586 image_info->filename);
2587 clone_info=CloneImageInfo(image_info);
2588 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2590 if (*clone_info->magick !=
'\0')
2591 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2592 magick_info=GetMagickInfo(images->magick,exception);
2593 if (magick_info == (
const MagickInfo *) NULL)
2595 (void) ThrowMagickException(exception,GetMagickModule(),
2596 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2598 clone_info=DestroyImageInfo(clone_info);
2601 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2602 blob_support=GetMagickBlobSupport(magick_info);
2603 if ((blob_support != MagickFalse) &&
2604 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2606 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2607 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2608 blob_support=MagickFalse;
2610 if (blob_support != MagickFalse)
2615 (void) CloseBlob(images);
2616 *images->filename=
'\0';
2617 (void) WriteImages(clone_info,images,images->filename,exception);
2618 (void) CloseBlob(images);
2623 filename[MagickPathExtent],
2624 unique[MagickPathExtent];
2636 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2638 if (blob == (
unsigned char *) NULL)
2640 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2641 image_info->filename);
2642 clone_info=DestroyImageInfo(clone_info);
2645 file=AcquireUniqueFileResource(unique);
2648 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2649 image_info->filename);
2650 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2651 clone_info=DestroyImageInfo(clone_info);
2654 clone_info->file=fdopen(file,
"wb+");
2655 if (clone_info->file != (FILE *) NULL)
2660 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2661 images->magick,unique);
2662 status=WriteImages(clone_info,images,filename,exception);
2663 (void) CloseBlob(images);
2664 if (status != MagickFalse)
2666 (void) fseek(clone_info->file,0,SEEK_SET);
2667 count=(ssize_t) MagickMaxBufferExtent;
2668 while (count == (ssize_t) MagickMaxBufferExtent)
2670 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2672 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2673 image_info->custom_stream->data);
2676 (void) fclose(clone_info->file);
2678 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2679 (void) RelinquishUniqueFileResource(unique);
2681 clone_info=DestroyImageInfo(clone_info);
2717 MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2721 filename[MagickPathExtent];
2750 assert(image_info != (
ImageInfo *) NULL);
2751 assert(image_info->signature == MagickCoreSignature);
2752 assert(image != (
Image *) NULL);
2753 assert(image->signature == MagickCoreSignature);
2754 assert(inject_image != (
Image *) NULL);
2755 assert(inject_image->signature == MagickCoreSignature);
2757 if (IsEventLogging() != MagickFalse)
2758 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2759 unique_file=(FILE *) NULL;
2760 file=AcquireUniqueFileResource(filename);
2762 unique_file=fdopen(file,
"wb");
2763 if ((file == -1) || (unique_file == (FILE *) NULL))
2765 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2766 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2768 return(MagickFalse);
2770 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2771 if (byte_image == (
Image *) NULL)
2773 (void) fclose(unique_file);
2774 (void) RelinquishUniqueFileResource(filename);
2775 return(MagickFalse);
2777 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2779 DestroyBlob(byte_image);
2780 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2781 write_info=CloneImageInfo(image_info);
2782 SetImageInfoFile(write_info,unique_file);
2783 status=WriteImage(write_info,byte_image,exception);
2784 write_info=DestroyImageInfo(write_info);
2785 byte_image=DestroyImage(byte_image);
2786 (void) fclose(unique_file);
2787 if (status == MagickFalse)
2789 (void) RelinquishUniqueFileResource(filename);
2790 return(MagickFalse);
2795 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2798 (void) RelinquishUniqueFileResource(filename);
2799 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2800 image_info->filename);
2801 return(MagickFalse);
2803 quantum=(size_t) MagickMaxBufferExtent;
2804 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2805 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2806 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2807 if (buffer == (
unsigned char *) NULL)
2809 (void) RelinquishUniqueFileResource(filename);
2811 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2816 ssize_t count = read(file,buffer,quantum);
2823 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2828 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2829 (void) RelinquishUniqueFileResource(filename);
2830 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2856 MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2858 assert(image != (
const Image *) NULL);
2859 assert(image->signature == MagickCoreSignature);
2860 if (IsEventLogging() != MagickFalse)
2861 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2862 return(image->blob->exempt);
2887 MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2890 *magick_restrict blob_info;
2892 assert(image != (
const Image *) NULL);
2893 assert(image->signature == MagickCoreSignature);
2894 if (IsEventLogging() != MagickFalse)
2895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2896 blob_info=image->blob;
2897 switch (blob_info->type)
2906 if (blob_info->file_info.file == (FILE *) NULL)
2907 return(MagickFalse);
2908 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2909 return(status == -1 ? MagickFalse : MagickTrue);
2913 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2917 if (blob_info->file_info.gzfile == (gzFile) NULL)
2918 return(MagickFalse);
2919 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2920 return(offset < 0 ? MagickFalse : MagickTrue);
2925 case UndefinedStream:
2929 case StandardStream:
2933 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
2934 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
2941 return(MagickFalse);
2966 MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2968 assert(image != (
const Image *) NULL);
2969 assert(image->signature == MagickCoreSignature);
2970 if (IsEventLogging() != MagickFalse)
2971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2972 return(image->blob->temporary);
3004 MagickExport
void *MapBlob(
int file,
const MapMode mode,
3005 const MagickOffsetType offset,
const size_t length)
3007 #if defined(MAGICKCORE_HAVE_MMAP)
3020 #if defined(MAP_ANONYMOUS)
3021 flags|=MAP_ANONYMOUS;
3030 protection=PROT_READ;
3036 protection=PROT_WRITE;
3042 protection=PROT_READ | PROT_WRITE;
3047 #if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3048 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3050 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3051 if (map == MAP_FAILED)
3052 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3054 if (map == MAP_FAILED)
3091 MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3100 assert(buffer != (
unsigned char *) NULL);
3107 *buffer++=(
unsigned char) c;
3111 *buffer++=(
unsigned char) c;
3141 MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3149 assert(p != (
unsigned char *) NULL);
3156 *p++=(
unsigned char) c;
3192 static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3204 size=MagickMinBufferExtent;
3205 option=GetImageOption(image_info,
"stream:buffer-size");
3206 if (option != (
const char *) NULL)
3207 size=StringToUnsignedLong(option);
3208 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3209 _IONBF : _IOFBF,size);
3210 return(status == 0 ? MagickTrue : MagickFalse);
3213 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3214 static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3216 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3217 return(gzopen(path,mode));
3225 path_wide=create_wchar_path(path);
3226 if (path_wide == (
wchar_t *) NULL)
3227 return((gzFile) NULL);
3228 file=gzopen_w(path_wide,mode);
3229 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3235 MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3239 *magick_restrict blob_info;
3242 extension[MagickPathExtent],
3243 filename[MagickPathExtent];
3254 assert(image_info != (
ImageInfo *) NULL);
3255 assert(image_info->signature == MagickCoreSignature);
3256 assert(image != (
Image *) NULL);
3257 assert(image->signature == MagickCoreSignature);
3258 if (IsEventLogging() != MagickFalse)
3259 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3260 image_info->filename);
3261 blob_info=image->blob;
3262 if (image_info->blob != (
void *) NULL)
3264 if (image_info->stream != (StreamHandler) NULL)
3265 blob_info->stream=(StreamHandler) image_info->stream;
3266 AttachBlob(blob_info,image_info->blob,image_info->length);
3270 (*image->filename ==
'\0'))
3272 blob_info->type=CustomStream;
3273 blob_info->custom_stream=image_info->custom_stream;
3276 (void) DetachBlob(blob_info);
3277 blob_info->mode=mode;
3280 default: type=
"r";
break;
3281 case ReadBlobMode: type=
"r";
break;
3282 case ReadBinaryBlobMode: type=
"rb";
break;
3283 case WriteBlobMode: type=
"w";
break;
3284 case WriteBinaryBlobMode: type=
"w+b";
break;
3285 case AppendBlobMode: type=
"a";
break;
3286 case AppendBinaryBlobMode: type=
"a+b";
break;
3289 blob_info->synchronize=image_info->synchronize;
3290 if (image_info->stream != (StreamHandler) NULL)
3292 blob_info->stream=image_info->stream;
3295 blob_info->type=FifoStream;
3303 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3304 rights=ReadPolicyRights;
3306 rights=WritePolicyRights;
3307 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3310 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3311 "NotAuthorized",
"`%s'",filename);
3312 return(MagickFalse);
3314 if ((LocaleCompare(filename,
"-") == 0) ||
3315 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3317 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3318 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3319 if (strchr(type,
'b') != (
char *) NULL)
3320 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3322 blob_info->type=StandardStream;
3323 blob_info->exempt=MagickTrue;
3324 return(SetStreamBuffering(image_info,blob_info));
3326 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3327 (IsGeometry(filename+3) != MagickFalse))
3334 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3335 if (blob_info->file_info.file == (FILE *) NULL)
3337 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3338 return(MagickFalse);
3340 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3341 if (strchr(type,
'b') != (
char *) NULL)
3342 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3344 blob_info->type=FileStream;
3345 blob_info->exempt=MagickTrue;
3346 return(SetStreamBuffering(image_info,blob_info));
3348 #if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3349 if (*filename ==
'|')
3352 fileMode[MagickPathExtent],
3358 #if defined(SIGPIPE)
3360 (void) signal(SIGPIPE,SIG_IGN);
3364 sanitize_command=SanitizeString(filename+1);
3365 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3366 sanitize_command=DestroyString(sanitize_command);
3367 if (blob_info->file_info.file == (FILE *) NULL)
3369 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3370 return(MagickFalse);
3372 blob_info->type=PipeStream;
3373 blob_info->exempt=MagickTrue;
3374 return(SetStreamBuffering(image_info,blob_info));
3377 status=GetPathAttributes(filename,&blob_info->properties);
3378 #if defined(S_ISFIFO)
3379 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3381 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3382 if (blob_info->file_info.file == (FILE *) NULL)
3384 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3385 return(MagickFalse);
3387 blob_info->type=FileStream;
3388 blob_info->exempt=MagickTrue;
3389 return(SetStreamBuffering(image_info,blob_info));
3392 GetPathComponent(image->filename,ExtensionPath,extension);
3395 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3396 if ((image_info->adjoin == MagickFalse) ||
3397 (strchr(filename,
'%') != (
char *) NULL))
3402 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3403 image->scene,filename,exception);
3404 if ((LocaleCompare(filename,image->filename) == 0) &&
3405 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
3406 (GetNextImageInList(image) != (
Image *) NULL)))
3409 path[MagickPathExtent];
3411 GetPathComponent(image->filename,RootPath,path);
3412 if (*extension ==
'\0')
3413 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3414 path,(
double) image->scene);
3416 (
void) FormatLocaleString(filename,MagickPathExtent,
3417 "%s-%.20g.%s",path,(
double) image->scene,extension);
3419 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3422 if (image_info->file != (FILE *) NULL)
3424 blob_info->file_info.file=image_info->file;
3425 blob_info->type=FileStream;
3426 blob_info->exempt=MagickTrue;
3431 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3432 if (blob_info->file_info.file != (FILE *) NULL)
3440 blob_info->type=FileStream;
3441 (void) SetStreamBuffering(image_info,blob_info);
3442 (void) memset(magick,0,
sizeof(magick));
3443 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3444 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3445 #if defined(MAGICKCORE_POSIX_SUPPORT)
3446 (void) fflush(blob_info->file_info.file);
3448 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3449 " read %.20g magic header bytes",(double) count);
3450 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3451 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3452 ((
int) magick[2] == 0x08))
3455 gzfile = gzopen_utf8(filename,
"rb");
3457 if (gzfile != (gzFile) NULL)
3459 if (blob_info->file_info.file != (FILE *) NULL)
3460 (void) fclose(blob_info->file_info.file);
3461 blob_info->file_info.file=(FILE *) NULL;
3462 blob_info->file_info.gzfile=gzfile;
3463 blob_info->type=ZipStream;
3467 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3468 if (strncmp((
char *) magick,
"BZh",3) == 0)
3471 *bzfile = BZ2_bzopen(filename,
"r");
3473 if (bzfile != (BZFILE *) NULL)
3475 if (blob_info->file_info.file != (FILE *) NULL)
3476 (void) fclose(blob_info->file_info.file);
3477 blob_info->file_info.file=(FILE *) NULL;
3478 blob_info->file_info.bzfile=bzfile;
3479 blob_info->type=BZipStream;
3483 if (blob_info->type == FileStream)
3494 sans_exception=AcquireExceptionInfo();
3495 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3496 sans_exception=DestroyExceptionInfo(sans_exception);
3497 length=(size_t) blob_info->properties.st_size;
3498 if ((magick_info != (
const MagickInfo *) NULL) &&
3499 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3500 (length > MagickMaxBufferExtent) &&
3501 (AcquireMagickResource(MapResource,length) != MagickFalse))
3506 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3508 if (blob == (
void *) NULL)
3509 RelinquishMagickResource(MapResource,length);
3515 if (image_info->file != (FILE *) NULL)
3516 blob_info->exempt=MagickFalse;
3519 (void) fclose(blob_info->file_info.file);
3520 blob_info->file_info.file=(FILE *) NULL;
3522 AttachBlob(blob_info,blob,length);
3523 blob_info->mapped=MagickTrue;
3530 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3531 if ((LocaleCompare(extension,
"Z") == 0) ||
3532 (LocaleCompare(extension,
"gz") == 0) ||
3533 (LocaleCompare(extension,
"wmz") == 0) ||
3534 (LocaleCompare(extension,
"svgz") == 0))
3536 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3537 if (blob_info->file_info.gzfile != (gzFile) NULL)
3538 blob_info->type=ZipStream;
3542 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3543 if (LocaleCompare(extension,
"bz2") == 0)
3545 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3546 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3547 blob_info->type=BZipStream;
3552 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3553 if (blob_info->file_info.file != (FILE *) NULL)
3555 blob_info->type=FileStream;
3556 (void) SetStreamBuffering(image_info,blob_info);
3559 blob_info->status=MagickFalse;
3560 blob_info->error_number=MagickFalse;
3561 if (blob_info->type != UndefinedStream)
3562 blob_info->size=GetBlobSize(image);
3565 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3566 return(MagickFalse);
3605 #if defined(__cplusplus) || defined(c_plusplus)
3609 static size_t PingStream(
const Image *magick_unused(image),
3610 const void *magick_unused(pixels),
const size_t columns)
3612 magick_unreferenced(image);
3613 magick_unreferenced(pixels);
3617 #if defined(__cplusplus) || defined(c_plusplus)
3621 MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3637 assert(image_info != (
ImageInfo *) NULL);
3638 assert(image_info->signature == MagickCoreSignature);
3640 if (IsEventLogging() != MagickFalse)
3641 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3642 image_info->filename);
3643 if ((blob == (
const void *) NULL) || (length == 0))
3645 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3646 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3647 return((
Image *) NULL);
3649 ping_info=CloneImageInfo(image_info);
3650 ping_info->blob=(
void *) blob;
3651 ping_info->length=length;
3652 ping_info->ping=MagickTrue;
3653 if (*ping_info->magick ==
'\0')
3654 (void) SetImageInfo(ping_info,0,exception);
3655 magick_info=GetMagickInfo(ping_info->magick,exception);
3656 if (magick_info == (
const MagickInfo *) NULL)
3658 (void) ThrowMagickException(exception,GetMagickModule(),
3659 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3661 ping_info=DestroyImageInfo(ping_info);
3662 return((
Image *) NULL);
3664 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3667 filename[MagickPathExtent];
3672 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3673 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3674 ping_info->magick,filename);
3675 image=ReadStream(ping_info,&PingStream,exception);
3676 if (image != (
Image *) NULL)
3677 (void) DetachBlob(image->blob);
3678 ping_info=DestroyImageInfo(ping_info);
3684 ping_info->blob=(
void *) NULL;
3685 ping_info->length=0;
3686 *ping_info->filename=
'\0';
3687 status=BlobToFile(ping_info->filename,blob,length,exception);
3688 if (status == MagickFalse)
3690 (void) RelinquishUniqueFileResource(ping_info->filename);
3691 ping_info=DestroyImageInfo(ping_info);
3692 return((
Image *) NULL);
3694 clone_info=CloneImageInfo(ping_info);
3695 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3696 ping_info->magick,ping_info->filename);
3697 image=ReadStream(clone_info,&PingStream,exception);
3698 if (image != (
Image *) NULL)
3706 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3708 (void) CopyMagickString(images->filename,image_info->filename,
3710 (void) CopyMagickString(images->magick_filename,image_info->filename,
3712 (void) CopyMagickString(images->magick,magick_info->name,
3714 images=GetNextImageInList(images);
3717 clone_info=DestroyImageInfo(clone_info);
3718 (void) RelinquishUniqueFileResource(ping_info->filename);
3719 ping_info=DestroyImageInfo(ping_info);
3754 MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
void *data)
3757 *magick_restrict blob_info;
3768 assert(image != (
Image *) NULL);
3769 assert(image->signature == MagickCoreSignature);
3770 assert(image->blob != (
BlobInfo *) NULL);
3771 assert(image->blob->type != UndefinedStream);
3774 assert(data != (
void *) NULL);
3775 blob_info=image->blob;
3777 q=(
unsigned char *) data;
3778 switch (blob_info->type)
3780 case UndefinedStream:
3782 case StandardStream:
3790 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3795 c=getc(blob_info->file_info.file);
3798 *q++=(
unsigned char) c;
3803 c=getc(blob_info->file_info.file);
3806 *q++=(
unsigned char) c;
3811 c=getc(blob_info->file_info.file);
3814 *q++=(
unsigned char) c;
3819 c=getc(blob_info->file_info.file);
3822 *q++=(
unsigned char) c;
3828 if ((count != (ssize_t) length) &&
3829 (ferror(blob_info->file_info.file) != 0))
3830 ThrowBlobException(blob_info);
3835 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3846 for (i=0; i < (ssize_t) length; i+=count)
3848 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3849 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3862 c=gzgetc(blob_info->file_info.gzfile);
3865 *q++=(
unsigned char) c;
3870 c=gzgetc(blob_info->file_info.gzfile);
3873 *q++=(
unsigned char) c;
3878 c=gzgetc(blob_info->file_info.gzfile);
3881 *q++=(
unsigned char) c;
3886 c=gzgetc(blob_info->file_info.gzfile);
3889 *q++=(
unsigned char) c;
3896 (void) gzerror(blob_info->file_info.gzfile,&status);
3897 if ((count != (ssize_t) length) && (status != Z_OK))
3898 ThrowBlobException(blob_info);
3899 if (blob_info->eof == MagickFalse)
3900 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3907 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3914 for (i=0; i < (ssize_t) length; i+=count)
3916 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3917 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3927 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3928 if ((count != (ssize_t) length) && (status != BZ_OK))
3929 ThrowBlobException(blob_info);
3940 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3942 blob_info->eof=MagickTrue;
3945 p=blob_info->data+blob_info->offset;
3946 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3947 blob_info->length-blob_info->offset);
3948 blob_info->offset+=count;
3949 if (count != (ssize_t) length)
3950 blob_info->eof=MagickTrue;
3951 (
void) memcpy(q,p,(
size_t) count);
3956 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
3957 count=blob_info->custom_stream->reader(q,length,
3958 blob_info->custom_stream->data);
3987 MagickExport
int ReadBlobByte(
Image *image)
3990 *magick_restrict blob_info;
3998 assert(image != (
Image *) NULL);
3999 assert(image->signature == MagickCoreSignature);
4000 assert(image->blob != (
BlobInfo *) NULL);
4001 assert(image->blob->type != UndefinedStream);
4002 blob_info=image->blob;
4003 switch (blob_info->type)
4005 case StandardStream:
4012 p=(
const unsigned char *) buffer;
4013 c=getc(blob_info->file_info.file);
4016 *buffer=(
unsigned char) c;
4024 p=(
const unsigned char *) ReadBlobStream(image,1,buffer,&count);
4056 MagickExport
double ReadBlobDouble(
Image *image)
4067 quantum.double_value=0.0;
4068 quantum.unsigned_value=ReadBlobLongLong(image);
4069 return(quantum.double_value);
4095 MagickExport
float ReadBlobFloat(
Image *image)
4106 quantum.float_value=0.0;
4107 quantum.unsigned_value=ReadBlobLong(image);
4108 return(quantum.float_value);
4134 MagickExport
unsigned int ReadBlobLong(
Image *image)
4148 assert(image != (
Image *) NULL);
4149 assert(image->signature == MagickCoreSignature);
4151 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4154 if (image->endian == LSBEndian)
4156 value=(
unsigned int) (*p++);
4157 value|=(
unsigned int) (*p++) << 8;
4158 value|=(
unsigned int) (*p++) << 16;
4159 value|=(
unsigned int) (*p++) << 24;
4162 value=(
unsigned int) (*p++) << 24;
4163 value|=(
unsigned int) (*p++) << 16;
4164 value|=(
unsigned int) (*p++) << 8;
4165 value|=(
unsigned int) (*p++);
4192 MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
4206 assert(image != (
Image *) NULL);
4207 assert(image->signature == MagickCoreSignature);
4209 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4211 return(MagickULLConstant(0));
4212 if (image->endian == LSBEndian)
4214 value=(MagickSizeType) (*p++);
4215 value|=(MagickSizeType) (*p++) << 8;
4216 value|=(MagickSizeType) (*p++) << 16;
4217 value|=(MagickSizeType) (*p++) << 24;
4218 value|=(MagickSizeType) (*p++) << 32;
4219 value|=(MagickSizeType) (*p++) << 40;
4220 value|=(MagickSizeType) (*p++) << 48;
4221 value|=(MagickSizeType) (*p++) << 56;
4224 value=(MagickSizeType) (*p++) << 56;
4225 value|=(MagickSizeType) (*p++) << 48;
4226 value|=(MagickSizeType) (*p++) << 40;
4227 value|=(MagickSizeType) (*p++) << 32;
4228 value|=(MagickSizeType) (*p++) << 24;
4229 value|=(MagickSizeType) (*p++) << 16;
4230 value|=(MagickSizeType) (*p++) << 8;
4231 value|=(MagickSizeType) (*p++);
4258 MagickExport
unsigned short ReadBlobShort(
Image *image)
4272 assert(image != (
Image *) NULL);
4273 assert(image->signature == MagickCoreSignature);
4275 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4277 return((
unsigned short) 0U);
4278 if (image->endian == LSBEndian)
4280 value=(
unsigned short) (*p++);
4281 value|=(
unsigned short) (*p++) << 8;
4284 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4285 value|=(
unsigned short) (*p++);
4312 MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
4326 assert(image != (
Image *) NULL);
4327 assert(image->signature == MagickCoreSignature);
4329 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4332 value=(
unsigned int) (*p++);
4333 value|=(
unsigned int) (*p++) << 8;
4334 value|=(
unsigned int) (*p++) << 16;
4335 value|=(
unsigned int) (*p++) << 24;
4362 MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
4373 quantum.unsigned_value=ReadBlobLSBLong(image);
4374 return(quantum.signed_value);
4400 MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
4414 assert(image != (
Image *) NULL);
4415 assert(image->signature == MagickCoreSignature);
4417 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4419 return((
unsigned short) 0U);
4420 value=(
unsigned short) (*p++);
4421 value|=(
unsigned short) (*p++) << 8;
4448 MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
4459 quantum.unsigned_value=ReadBlobLSBShort(image);
4460 return(quantum.signed_value);
4486 MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
4500 assert(image != (
Image *) NULL);
4501 assert(image->signature == MagickCoreSignature);
4503 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4506 value=(
unsigned int) (*p++) << 24;
4507 value|=(
unsigned int) (*p++) << 16;
4508 value|=(
unsigned int) (*p++) << 8;
4509 value|=(
unsigned int) (*p++);
4536 MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
4550 assert(image != (
Image *) NULL);
4551 assert(image->signature == MagickCoreSignature);
4553 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4555 return(MagickULLConstant(0));
4556 value=(MagickSizeType) (*p++) << 56;
4557 value|=(MagickSizeType) (*p++) << 48;
4558 value|=(MagickSizeType) (*p++) << 40;
4559 value|=(MagickSizeType) (*p++) << 32;
4560 value|=(MagickSizeType) (*p++) << 24;
4561 value|=(MagickSizeType) (*p++) << 16;
4562 value|=(MagickSizeType) (*p++) << 8;
4563 value|=(MagickSizeType) (*p++);
4590 MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
4604 assert(image != (
Image *) NULL);
4605 assert(image->signature == MagickCoreSignature);
4607 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4609 return((
unsigned short) 0U);
4610 value=(
unsigned short) ((*p++) << 8);
4611 value|=(
unsigned short) (*p++);
4612 return((
unsigned short) (value & 0xffff));
4638 MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4649 quantum.unsigned_value=ReadBlobMSBLong(image);
4650 return(quantum.signed_value);
4676 MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4687 quantum.unsigned_value=ReadBlobMSBShort(image);
4688 return(quantum.signed_value);
4714 MagickExport
signed int ReadBlobSignedLong(
Image *image)
4725 quantum.unsigned_value=ReadBlobLong(image);
4726 return(quantum.signed_value);
4752 MagickExport
signed short ReadBlobSignedShort(
Image *image)
4763 quantum.unsigned_value=ReadBlobShort(image);
4764 return(quantum.signed_value);
4802 MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4803 const size_t length,
void *magick_restrict data,ssize_t *count)
4806 *magick_restrict blob_info;
4808 assert(image != (
Image *) NULL);
4809 assert(image->signature == MagickCoreSignature);
4810 assert(image->blob != (
BlobInfo *) NULL);
4811 assert(image->blob->type != UndefinedStream);
4812 assert(count != (ssize_t *) NULL);
4813 blob_info=image->blob;
4814 if (blob_info->type != BlobStream)
4816 assert(data != NULL);
4817 *count=ReadBlob(image,length,(
unsigned char *) data);
4820 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4823 blob_info->eof=MagickTrue;
4826 data=blob_info->data+blob_info->offset;
4827 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4828 blob_info->length-blob_info->offset);
4829 blob_info->offset+=(*count);
4830 if (*count != (ssize_t) length)
4831 blob_info->eof=MagickTrue;
4860 MagickExport
char *ReadBlobString(
Image *image,
char *
string)
4868 assert(image != (
Image *) NULL);
4869 assert(image->signature == MagickCoreSignature);
4870 for (i=0; i < (MagickPathExtent-1L); i++)
4872 c=ReadBlobByte(image);
4876 return((
char *) NULL);
4882 if ((i > 0) && (
string[i-1] ==
'\r'))
4917 assert(blob->signature == MagickCoreSignature);
4918 if (IsEventLogging() != MagickFalse)
4919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4920 LockSemaphoreInfo(blob->semaphore);
4921 blob->reference_count++;
4922 UnlockSemaphoreInfo(blob->semaphore);
4959 MagickExport MagickOffsetType SeekBlob(
Image *image,
4960 const MagickOffsetType offset,
const int whence)
4963 *magick_restrict blob_info;
4965 assert(image != (
Image *) NULL);
4966 assert(image->signature == MagickCoreSignature);
4967 assert(image->blob != (
BlobInfo *) NULL);
4968 assert(image->blob->type != UndefinedStream);
4969 if (IsEventLogging() != MagickFalse)
4970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4971 blob_info=image->blob;
4972 switch (blob_info->type)
4974 case UndefinedStream:
4976 case StandardStream:
4981 if ((offset < 0) && (whence == SEEK_SET))
4983 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4985 blob_info->offset=TellBlob(image);
4990 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4991 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4994 blob_info->offset=TellBlob(image);
5010 blob_info->offset=offset;
5015 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5016 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5021 if ((blob_info->offset+offset) < 0)
5023 blob_info->offset+=offset;
5028 if (((MagickOffsetType) blob_info->length+offset) < 0)
5030 blob_info->offset=blob_info->length+offset;
5034 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5036 blob_info->eof=MagickFalse;
5039 if (blob_info->offset >= (MagickOffsetType) ((off_t) blob_info->extent))
5045 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5047 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5048 blob_info->custom_stream->data);
5052 return(blob_info->offset);
5080 MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
5082 assert(image != (
const Image *) NULL);
5083 assert(image->signature == MagickCoreSignature);
5084 if (IsEventLogging() != MagickFalse)
5085 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5086 image->blob->exempt=exempt;
5115 MagickExport MagickBooleanType SetBlobExtent(
Image *image,
5116 const MagickSizeType extent)
5119 *magick_restrict blob_info;
5121 assert(image != (
Image *) NULL);
5122 assert(image->signature == MagickCoreSignature);
5123 assert(image->blob != (
BlobInfo *) NULL);
5124 assert(image->blob->type != UndefinedStream);
5125 if (IsEventLogging() != MagickFalse)
5126 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5127 blob_info=image->blob;
5128 switch (blob_info->type)
5130 case UndefinedStream:
5132 case StandardStream:
5133 return(MagickFalse);
5142 if (extent != (MagickSizeType) ((off_t) extent))
5143 return(MagickFalse);
5144 offset=SeekBlob(image,0,SEEK_END);
5146 return(MagickFalse);
5147 if ((MagickSizeType) offset >= extent)
5149 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5152 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5153 blob_info->file_info.file);
5154 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5155 if (blob_info->synchronize != MagickFalse)
5160 file=fileno(blob_info->file_info.file);
5161 if ((file == -1) || (offset < 0))
5162 return(MagickFalse);
5163 (void) posix_fallocate(file,offset,extent-offset);
5166 offset=SeekBlob(image,offset,SEEK_SET);
5168 return(MagickFalse);
5173 return(MagickFalse);
5175 return(MagickFalse);
5177 return(MagickFalse);
5180 if (extent != (MagickSizeType) ((
size_t) extent))
5181 return(MagickFalse);
5182 if (blob_info->mapped != MagickFalse)
5190 (void) UnmapBlob(blob_info->data,blob_info->length);
5191 RelinquishMagickResource(MapResource,blob_info->length);
5192 if (extent != (MagickSizeType) ((off_t) extent))
5193 return(MagickFalse);
5194 offset=SeekBlob(image,0,SEEK_END);
5196 return(MagickFalse);
5197 if ((MagickSizeType) offset >= extent)
5199 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5200 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5201 blob_info->file_info.file);
5202 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5203 if (blob_info->synchronize != MagickFalse)
5208 file=fileno(blob_info->file_info.file);
5209 if ((file == -1) || (offset < 0))
5210 return(MagickFalse);
5211 (void) posix_fallocate(file,offset,extent-offset);
5214 offset=SeekBlob(image,offset,SEEK_SET);
5216 return(MagickFalse);
5217 (void) AcquireMagickResource(MapResource,extent);
5218 blob_info->data=(
unsigned char*) MapBlob(fileno(
5219 blob_info->file_info.file),WriteMode,0,(size_t) extent);
5220 blob_info->extent=(size_t) extent;
5221 blob_info->length=(size_t) extent;
5222 (void) SyncBlob(image);
5225 blob_info->extent=(size_t) extent;
5226 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5227 blob_info->extent+1,
sizeof(*blob_info->data));
5228 (void) SyncBlob(image);
5229 if (blob_info->data == (
unsigned char *) NULL)
5231 (void) DetachBlob(blob_info);
5232 return(MagickFalse);
5270 assert(custom_stream->signature == MagickCoreSignature);
5271 custom_stream->data=data;
5300 CustomStreamHandler reader)
5303 assert(custom_stream->signature == MagickCoreSignature);
5304 custom_stream->reader=reader;
5333 CustomStreamSeeker seeker)
5336 assert(custom_stream->signature == MagickCoreSignature);
5337 custom_stream->seeker=seeker;
5366 CustomStreamTeller teller)
5369 assert(custom_stream->signature == MagickCoreSignature);
5370 custom_stream->teller=teller;
5399 CustomStreamHandler writer)
5402 assert(custom_stream->signature == MagickCoreSignature);
5403 custom_stream->writer=writer;
5429 static int SyncBlob(
Image *image)
5432 *magick_restrict blob_info;
5437 assert(image != (
Image *) NULL);
5438 assert(image->signature == MagickCoreSignature);
5439 assert(image->blob != (
BlobInfo *) NULL);
5440 assert(image->blob->type != UndefinedStream);
5441 if (IsEventLogging() != MagickFalse)
5442 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5443 blob_info=image->blob;
5445 switch (blob_info->type)
5447 case UndefinedStream:
5448 case StandardStream:
5453 status=fflush(blob_info->file_info.file);
5458 #if defined(MAGICKCORE_ZLIB_DELEGATE)
5459 status=gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5465 #if defined(MAGICKCORE_BZLIB_DELEGATE)
5466 status=BZ2_bzflush(blob_info->file_info.bzfile);
5502 MagickExport MagickOffsetType TellBlob(
const Image *image)
5505 *magick_restrict blob_info;
5510 assert(image != (
Image *) NULL);
5511 assert(image->signature == MagickCoreSignature);
5512 assert(image->blob != (
BlobInfo *) NULL);
5513 assert(image->blob->type != UndefinedStream);
5514 if (IsEventLogging() != MagickFalse)
5515 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5516 blob_info=image->blob;
5518 switch (blob_info->type)
5520 case UndefinedStream:
5521 case StandardStream:
5525 offset=ftell(blob_info->file_info.file);
5532 #if defined(MAGICKCORE_ZLIB_DELEGATE)
5533 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5543 offset=blob_info->offset;
5548 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5549 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5581 MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5583 #if defined(MAGICKCORE_HAVE_MMAP)
5587 status=munmap(map,length);
5588 return(status == -1 ? MagickFalse : MagickTrue);
5592 return(MagickFalse);
5624 MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
5628 *magick_restrict blob_info;
5642 assert(image != (
Image *) NULL);
5643 assert(image->signature == MagickCoreSignature);
5644 assert(image->blob != (
BlobInfo *) NULL);
5645 assert(image->blob->type != UndefinedStream);
5648 assert(data != (
const void *) NULL);
5649 blob_info=image->blob;
5651 p=(
const unsigned char *) data;
5652 q=(
unsigned char *) data;
5653 switch (blob_info->type)
5655 case UndefinedStream:
5657 case StandardStream:
5665 count=(ssize_t) fwrite((
const char *) data,1,length,
5666 blob_info->file_info.file);
5671 c=putc((
int) *p++,blob_info->file_info.file);
5678 c=putc((
int) *p++,blob_info->file_info.file);
5685 c=putc((
int) *p++,blob_info->file_info.file);
5692 c=putc((
int) *p++,blob_info->file_info.file);
5700 if ((count != (ssize_t) length) &&
5701 (ferror(blob_info->file_info.file) != 0))
5702 ThrowBlobException(blob_info);
5707 #if defined(MAGICKCORE_ZLIB_DELEGATE)
5718 for (i=0; i < (ssize_t) length; i+=count)
5720 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5721 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5734 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5741 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5748 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5755 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5764 (void) gzerror(blob_info->file_info.gzfile,&status);
5765 if ((count != (ssize_t) length) && (status != Z_OK))
5766 ThrowBlobException(blob_info);
5772 #if defined(MAGICKCORE_BZLIB_DELEGATE)
5779 for (i=0; i < (ssize_t) length; i+=count)
5781 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5782 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5792 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5793 if ((count != (ssize_t) length) && (status != BZ_OK))
5794 ThrowBlobException(blob_info);
5800 count=(ssize_t) blob_info->stream(image,data,length);
5805 if ((blob_info->offset+(MagickOffsetType) length) >=
5806 (MagickOffsetType) blob_info->extent)
5808 if (blob_info->mapped != MagickFalse)
5810 blob_info->extent+=length+blob_info->quantum;
5811 blob_info->quantum<<=1;
5812 blob_info->data=(
unsigned char *) ResizeQuantumMemory(
5813 blob_info->data,blob_info->extent+1,
sizeof(*blob_info->data));
5814 (void) SyncBlob(image);
5815 if (blob_info->data == (
unsigned char *) NULL)
5817 (void) DetachBlob(blob_info);
5821 q=blob_info->data+blob_info->offset;
5822 (void) memcpy(q,p,length);
5823 blob_info->offset+=length;
5824 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5825 blob_info->length=(
size_t) blob_info->offset;
5826 count=(ssize_t) length;
5831 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
5832 count=blob_info->custom_stream->writer((
unsigned char *) data,
5833 length,blob_info->custom_stream->data);
5865 MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
5868 *magick_restrict blob_info;
5873 assert(image != (
Image *) NULL);
5874 assert(image->signature == MagickCoreSignature);
5875 assert(image->blob != (
BlobInfo *) NULL);
5876 assert(image->blob->type != UndefinedStream);
5877 blob_info=image->blob;
5879 switch (blob_info->type)
5881 case StandardStream:
5888 c=putc((
int) value,blob_info->file_info.file);
5891 if (ferror(blob_info->file_info.file) != 0)
5892 ThrowBlobException(blob_info);
5900 count=WriteBlobStream(image,1,&value);
5932 MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
5943 quantum.unsigned_value=0U;
5944 quantum.float_value=value;
5945 return(WriteBlobLong(image,quantum.unsigned_value));
5973 MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
5978 assert(image != (
Image *) NULL);
5979 assert(image->signature == MagickCoreSignature);
5980 if (image->endian == LSBEndian)
5982 buffer[0]=(
unsigned char) value;
5983 buffer[1]=(
unsigned char) (value >> 8);
5984 buffer[2]=(
unsigned char) (value >> 16);
5985 buffer[3]=(
unsigned char) (value >> 24);
5986 return(WriteBlobStream(image,4,buffer));
5988 buffer[0]=(
unsigned char) (value >> 24);
5989 buffer[1]=(
unsigned char) (value >> 16);
5990 buffer[2]=(
unsigned char) (value >> 8);
5991 buffer[3]=(
unsigned char) value;
5992 return(WriteBlobStream(image,4,buffer));
6020 MagickExport ssize_t WriteBlobLongLong(
Image *image,
const MagickSizeType value)
6025 assert(image != (
Image *) NULL);
6026 assert(image->signature == MagickCoreSignature);
6027 if (image->endian == LSBEndian)
6029 buffer[0]=(
unsigned char) value;
6030 buffer[1]=(
unsigned char) (value >> 8);
6031 buffer[2]=(
unsigned char) (value >> 16);
6032 buffer[3]=(
unsigned char) (value >> 24);
6033 buffer[4]=(
unsigned char) (value >> 32);
6034 buffer[5]=(
unsigned char) (value >> 40);
6035 buffer[6]=(
unsigned char) (value >> 48);
6036 buffer[7]=(
unsigned char) (value >> 56);
6037 return(WriteBlobStream(image,8,buffer));
6039 buffer[0]=(
unsigned char) (value >> 56);
6040 buffer[1]=(
unsigned char) (value >> 48);
6041 buffer[2]=(
unsigned char) (value >> 40);
6042 buffer[3]=(
unsigned char) (value >> 32);
6043 buffer[4]=(
unsigned char) (value >> 24);
6044 buffer[5]=(
unsigned char) (value >> 16);
6045 buffer[6]=(
unsigned char) (value >> 8);
6046 buffer[7]=(
unsigned char) value;
6047 return(WriteBlobStream(image,8,buffer));
6075 MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
6080 assert(image != (
Image *) NULL);
6081 assert(image->signature == MagickCoreSignature);
6082 if (image->endian == LSBEndian)
6084 buffer[0]=(
unsigned char) value;
6085 buffer[1]=(
unsigned char) (value >> 8);
6086 return(WriteBlobStream(image,2,buffer));
6088 buffer[0]=(
unsigned char) (value >> 8);
6089 buffer[1]=(
unsigned char) value;
6090 return(WriteBlobStream(image,2,buffer));
6118 MagickExport ssize_t WriteBlobSignedLong(
Image *image,
const signed int value)
6132 assert(image != (
Image *) NULL);
6133 assert(image->signature == MagickCoreSignature);
6134 quantum.signed_value=value;
6135 if (image->endian == LSBEndian)
6137 buffer[0]=(
unsigned char) quantum.unsigned_value;
6138 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6139 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6140 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6141 return(WriteBlobStream(image,4,buffer));
6143 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6144 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6145 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6146 buffer[3]=(
unsigned char) quantum.unsigned_value;
6147 return(WriteBlobStream(image,4,buffer));
6175 MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
6180 assert(image != (
Image *) NULL);
6181 assert(image->signature == MagickCoreSignature);
6182 buffer[0]=(
unsigned char) value;
6183 buffer[1]=(
unsigned char) (value >> 8);
6184 buffer[2]=(
unsigned char) (value >> 16);
6185 buffer[3]=(
unsigned char) (value >> 24);
6186 return(WriteBlobStream(image,4,buffer));
6214 MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
6219 assert(image != (
Image *) NULL);
6220 assert(image->signature == MagickCoreSignature);
6221 buffer[0]=(
unsigned char) value;
6222 buffer[1]=(
unsigned char) (value >> 8);
6223 return(WriteBlobStream(image,2,buffer));
6251 MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
6265 assert(image != (
Image *) NULL);
6266 assert(image->signature == MagickCoreSignature);
6267 quantum.signed_value=value;
6268 buffer[0]=(
unsigned char) quantum.unsigned_value;
6269 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6270 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6271 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6272 return(WriteBlobStream(image,4,buffer));
6300 MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
6301 const signed short value)
6315 assert(image != (
Image *) NULL);
6316 assert(image->signature == MagickCoreSignature);
6317 quantum.signed_value=value;
6318 buffer[0]=(
unsigned char) quantum.unsigned_value;
6319 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6320 return(WriteBlobStream(image,2,buffer));
6348 MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
6353 assert(image != (
Image *) NULL);
6354 assert(image->signature == MagickCoreSignature);
6355 buffer[0]=(
unsigned char) (value >> 24);
6356 buffer[1]=(
unsigned char) (value >> 16);
6357 buffer[2]=(
unsigned char) (value >> 8);
6358 buffer[3]=(
unsigned char) value;
6359 return(WriteBlobStream(image,4,buffer));
6387 MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
6388 const signed short value)
6402 assert(image != (
Image *) NULL);
6403 assert(image->signature == MagickCoreSignature);
6404 quantum.signed_value=value;
6405 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6406 buffer[1]=(
unsigned char) quantum.unsigned_value;
6407 return(WriteBlobStream(image,2,buffer));
6435 MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
6440 assert(image != (
Image *) NULL);
6441 assert(image->signature == MagickCoreSignature);
6442 buffer[0]=(
unsigned char) (value >> 8);
6443 buffer[1]=(
unsigned char) value;
6444 return(WriteBlobStream(image,2,buffer));
6472 MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
6474 assert(image != (
Image *) NULL);
6475 assert(image->signature == MagickCoreSignature);
6476 assert(
string != (
const char *) NULL);
6477 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));