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)
81#include "MagickCore/policy-private.h"
86#define MagickMaxBlobExtent (8*8192)
87#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
88# define MAP_ANONYMOUS MAP_ANON
90#if !defined(MAP_FAILED)
91#define MAP_FAILED ((void *) -1)
95#define _O_BINARY O_BINARY
97#if defined(MAGICKCORE_WINDOWS_SUPPORT)
102# define MAGICKCORE_HAVE_MMAP 1
103# define mmap(address,length,protection,access,file,offset) \
104 NTMapMemory(address,length,protection,access,file,offset)
107# define munmap(address,length) NTUnmapMemory(address,length)
110# define pclose _pclose
125#if defined(MAGICKCORE_ZLIB_DELEGATE)
130#if defined(MAGICKCORE_BZLIB_DELEGATE)
222 SyncBlob(
const Image *);
252 magick_unreferenced(exception);
254 sizeof(*custom_stream));
255 (void) memset(custom_stream,0,
sizeof(*custom_stream));
256 custom_stream->signature=MagickCoreSignature;
257 return(custom_stream);
287MagickExport
void AttachBlob(
BlobInfo *blob_info,
const void *blob,
290 assert(blob_info != (
BlobInfo *) NULL);
291 if (IsEventLogging() != MagickFalse)
292 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
293 blob_info->length=length;
294 blob_info->extent=length;
295 blob_info->quantum=(size_t) MagickMaxBlobExtent;
297 blob_info->type=BlobStream;
298 blob_info->file_info.file=(FILE *) NULL;
299 blob_info->data=(
unsigned char *) blob;
300 blob_info->mapped=MagickFalse;
328MagickExport
void AttachCustomStream(
BlobInfo *blob_info,
331 assert(blob_info != (
BlobInfo *) NULL);
333 assert(custom_stream->signature == MagickCoreSignature);
334 if (IsEventLogging() != MagickFalse)
335 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
336 blob_info->type=CustomStream;
337 blob_info->custom_stream=custom_stream;
370MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
382 assert(filename != (
const char *) NULL);
383 assert(blob != (
const void *) NULL);
384 if (IsEventLogging() != MagickFalse)
385 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
386 if (*filename ==
'\0')
387 file=AcquireUniqueFileResource(filename);
389 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
392 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
395 for (i=0; i < length; i+=(size_t) count)
397 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
398 MagickMaxBufferExtent));
406 file=close_utf8(file);
407 if ((file == -1) || (i < length))
409 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
446MagickExport
Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
462 assert(image_info != (
ImageInfo *) NULL);
463 assert(image_info->signature == MagickCoreSignature);
465 if (IsEventLogging() != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
467 image_info->filename);
468 if ((blob == (
const void *) NULL) || (length == 0))
470 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
471 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
472 return((
Image *) NULL);
474 blob_info=CloneImageInfo(image_info);
475 blob_info->blob=(
void *) blob;
476 blob_info->length=length;
477 if (*blob_info->magick ==
'\0')
478 (void) SetImageInfo(blob_info,0,exception);
479 magick_info=GetMagickInfo(blob_info->magick,exception);
482 (void) ThrowMagickException(exception,GetMagickModule(),
483 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
485 blob_info=DestroyImageInfo(blob_info);
486 return((
Image *) NULL);
488 if (GetMagickBlobSupport(magick_info) != MagickFalse)
491 filename[MagickPathExtent];
496 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
497 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
498 blob_info->magick,filename);
499 image=ReadImage(blob_info,exception);
500 if (image != (
Image *) NULL)
501 (void) DetachBlob(image->blob);
502 blob_info=DestroyImageInfo(blob_info);
508 blob_info->blob=(
void *) NULL;
510 *blob_info->filename=
'\0';
511 status=BlobToFile(blob_info->filename,blob,length,exception);
512 if (status == MagickFalse)
514 (void) RelinquishUniqueFileResource(blob_info->filename);
515 blob_info=DestroyImageInfo(blob_info);
516 return((
Image *) NULL);
518 clone_info=CloneImageInfo(blob_info);
519 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
520 blob_info->magick,blob_info->filename);
521 image=ReadImage(clone_info,exception);
522 if (image != (
Image *) NULL)
530 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
532 (void) CopyMagickString(images->filename,image_info->filename,
534 (void) CopyMagickString(images->magick_filename,image_info->filename,
536 (void) CopyMagickString(images->magick,magick_info->name,
538 images=GetNextImageInList(images);
541 clone_info=DestroyImageInfo(clone_info);
542 (void) RelinquishUniqueFileResource(blob_info->filename);
543 blob_info=DestroyImageInfo(blob_info);
578 clone_info=(
BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
579 GetBlobInfo(clone_info);
582 semaphore=clone_info->semaphore;
583 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
584 if (blob_info->mapped != MagickFalse)
585 (void) AcquireMagickResource(MapResource,blob_info->length);
586 clone_info->semaphore=semaphore;
587 LockSemaphoreInfo(clone_info->semaphore);
588 clone_info->reference_count=1;
589 UnlockSemaphoreInfo(clone_info->semaphore);
616static inline void ThrowBlobException(
BlobInfo *blob_info)
618 if ((blob_info->status == 0) && (errno != 0))
619 blob_info->error_number=errno;
620 blob_info->status=(-1);
623MagickExport MagickBooleanType CloseBlob(
Image *image)
626 *magick_restrict blob_info;
634 assert(image != (
Image *) NULL);
635 assert(image->signature == MagickCoreSignature);
636 if (IsEventLogging() != MagickFalse)
637 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
638 blob_info=image->blob;
639 if ((blob_info == (
BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
641 (void) SyncBlob(image);
642 status=blob_info->status;
643 switch (blob_info->type)
645 case UndefinedStream:
651 if (blob_info->synchronize != MagickFalse)
653 status=fflush(blob_info->file_info.file);
655 ThrowBlobException(blob_info);
656 status=fsync(fileno(blob_info->file_info.file));
658 ThrowBlobException(blob_info);
660 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
661 ThrowBlobException(blob_info);
666#if defined(MAGICKCORE_ZLIB_DELEGATE)
668 (void) gzerror(blob_info->file_info.gzfile,&status);
670 ThrowBlobException(blob_info);
676#if defined(MAGICKCORE_BZLIB_DELEGATE)
678 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
680 ThrowBlobException(blob_info);
688 if (blob_info->file_info.file != (FILE *) NULL)
690 if (blob_info->synchronize != MagickFalse)
692 status=fflush(blob_info->file_info.file);
694 ThrowBlobException(blob_info);
695 status=fsync(fileno(blob_info->file_info.file));
697 ThrowBlobException(blob_info);
699 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
700 ThrowBlobException(blob_info);
707 blob_info->size=GetBlobSize(image);
708 image->extent=blob_info->size;
709 blob_info->eof=MagickFalse;
711 blob_info->mode=UndefinedBlobMode;
712 if (blob_info->exempt != MagickFalse)
714 blob_info->type=UndefinedStream;
715 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
717 switch (blob_info->type)
719 case UndefinedStream:
724 if (blob_info->file_info.file != (FILE *) NULL)
726 status=fclose(blob_info->file_info.file);
728 ThrowBlobException(blob_info);
734#if defined(MAGICKCORE_HAVE_PCLOSE)
735 status=pclose(blob_info->file_info.file);
737 ThrowBlobException(blob_info);
743#if defined(MAGICKCORE_ZLIB_DELEGATE)
744 status=gzclose(blob_info->file_info.gzfile);
746 ThrowBlobException(blob_info);
752#if defined(MAGICKCORE_BZLIB_DELEGATE)
753 BZ2_bzclose(blob_info->file_info.bzfile);
761 if (blob_info->file_info.file != (FILE *) NULL)
763 status=fclose(blob_info->file_info.file);
765 ThrowBlobException(blob_info);
772 (void) DetachBlob(blob_info);
773 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
802MagickExport
Image *CustomStreamToImage(
const ImageInfo *image_info,
814 assert(image_info != (
ImageInfo *) NULL);
815 assert(image_info->signature == MagickCoreSignature);
817 assert(image_info->custom_stream->signature == MagickCoreSignature);
818 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
820 if (IsEventLogging() != MagickFalse)
821 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
822 image_info->filename);
823 blob_info=CloneImageInfo(image_info);
824 if (*blob_info->magick ==
'\0')
825 (void) SetImageInfo(blob_info,0,exception);
826 magick_info=GetMagickInfo(blob_info->magick,exception);
829 (void) ThrowMagickException(exception,GetMagickModule(),
830 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
832 blob_info=DestroyImageInfo(blob_info);
833 return((
Image *) NULL);
835 image=(
Image *) NULL;
836 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
837 (*blob_info->filename !=
'\0'))
840 filename[MagickPathExtent];
846 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
847 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
848 blob_info->magick,filename);
849 image=ReadImage(blob_info,exception);
854 unique[MagickPathExtent];
869 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
871 if (blob == (
unsigned char *) NULL)
873 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
874 image_info->filename);
875 blob_info=DestroyImageInfo(blob_info);
876 return((
Image *) NULL);
878 file=AcquireUniqueFileResource(unique);
881 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
882 image_info->filename);
883 blob=(
unsigned char *) RelinquishMagickMemory(blob);
884 blob_info=DestroyImageInfo(blob_info);
885 return((
Image *) NULL);
887 clone_info=CloneImageInfo(blob_info);
888 blob_info->file=fdopen(file,
"wb+");
889 if (blob_info->file != (FILE *) NULL)
894 count=(ssize_t) MagickMaxBufferExtent;
895 while (count == (ssize_t) MagickMaxBufferExtent)
897 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
898 image_info->custom_stream->data);
899 count=(ssize_t) write(file,(
const char *) blob,(size_t) count);
901 (void) fclose(blob_info->file);
902 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
903 "%s:%s",blob_info->magick,unique);
904 image=ReadImage(clone_info,exception);
905 if (image != (
Image *) NULL)
913 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
915 (void) CopyMagickString(images->filename,image_info->filename,
917 (void) CopyMagickString(images->magick_filename,
918 image_info->filename,MagickPathExtent);
919 (void) CopyMagickString(images->magick,magick_info->name,
921 images=GetNextImageInList(images);
925 clone_info=DestroyImageInfo(clone_info);
926 blob=(
unsigned char *) RelinquishMagickMemory(blob);
927 (void) RelinquishUniqueFileResource(unique);
929 blob_info=DestroyImageInfo(blob_info);
930 if (image != (
Image *) NULL)
931 if (CloseBlob(image) == MagickFalse)
932 image=DestroyImageList(image);
958MagickExport
void DestroyBlob(
Image *image)
961 *magick_restrict blob_info;
966 assert(image != (
Image *) NULL);
967 assert(image->signature == MagickCoreSignature);
968 assert(image->blob != (
BlobInfo *) NULL);
969 assert(image->blob->signature == MagickCoreSignature);
970 if (IsEventLogging() != MagickFalse)
971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
972 blob_info=image->blob;
974 LockSemaphoreInfo(blob_info->semaphore);
975 blob_info->reference_count--;
976 assert(blob_info->reference_count >= 0);
977 if (blob_info->reference_count == 0)
979 UnlockSemaphoreInfo(blob_info->semaphore);
980 if (destroy == MagickFalse)
985 (void) CloseBlob(image);
986 if (blob_info->mapped != MagickFalse)
988 (void) UnmapBlob(blob_info->data,blob_info->length);
989 RelinquishMagickResource(MapResource,blob_info->length);
992 RelinquishSemaphoreInfo(&blob_info->semaphore);
993 blob_info->signature=(~MagickCoreSignature);
994 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
1024 assert(custom_stream->signature == MagickCoreSignature);
1025 if (IsEventLogging() != MagickFalse)
1026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1027 custom_stream->signature=(~MagickCoreSignature);
1029 return(custom_stream);
1054MagickExport
void *DetachBlob(
BlobInfo *blob_info)
1059 assert(blob_info != (
BlobInfo *) NULL);
1060 if (IsEventLogging() != MagickFalse)
1061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1062 if (blob_info->mapped != MagickFalse)
1064 (void) UnmapBlob(blob_info->data,blob_info->length);
1065 blob_info->data=NULL;
1066 RelinquishMagickResource(MapResource,blob_info->length);
1068 blob_info->mapped=MagickFalse;
1069 blob_info->length=0;
1074 blob_info->offset=0;
1075 blob_info->mode=UndefinedBlobMode;
1076 blob_info->eof=MagickFalse;
1078 blob_info->exempt=MagickFalse;
1079 blob_info->type=UndefinedStream;
1080 blob_info->file_info.file=(FILE *) NULL;
1081 data=blob_info->data;
1082 blob_info->data=(
unsigned char *) NULL;
1083 blob_info->stream=(StreamHandler) NULL;
1112MagickExport
void DisassociateBlob(
Image *image)
1115 *magick_restrict blob_info,
1121 assert(image != (
Image *) NULL);
1122 assert(image->signature == MagickCoreSignature);
1123 assert(image->blob != (
BlobInfo *) NULL);
1124 assert(image->blob->signature == MagickCoreSignature);
1125 if (IsEventLogging() != MagickFalse)
1126 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1127 blob_info=image->blob;
1129 LockSemaphoreInfo(blob_info->semaphore);
1130 assert(blob_info->reference_count >= 0);
1131 if (blob_info->reference_count > 1)
1133 UnlockSemaphoreInfo(blob_info->semaphore);
1134 if (clone == MagickFalse)
1136 clone_info=CloneBlobInfo(blob_info);
1138 image->blob=clone_info;
1166MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
1167 const MagickSizeType length)
1179 buffer[MagickMinBufferExtent >> 1];
1181 assert(image != (
Image *) NULL);
1182 assert(image->signature == MagickCoreSignature);
1183 if (length != (MagickSizeType) ((MagickOffsetType) length))
1184 return(MagickFalse);
1186 for (i=0; i < length; i+=(MagickSizeType) count)
1188 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1189 (void) ReadBlobStream(image,quantum,buffer,&count);
1197 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1224MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
1226 assert(image != (
Image *) NULL);
1227 assert(image->signature == MagickCoreSignature);
1228 assert(duplicate != (
Image *) NULL);
1229 assert(duplicate->signature == MagickCoreSignature);
1230 if (IsEventLogging() != MagickFalse)
1231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1233 image->blob=ReferenceBlob(duplicate->blob);
1259MagickExport
int EOFBlob(
const Image *image)
1262 *magick_restrict blob_info;
1264 assert(image != (
Image *) NULL);
1265 assert(image->signature == MagickCoreSignature);
1266 assert(image->blob != (
BlobInfo *) NULL);
1267 assert(image->blob->type != UndefinedStream);
1268 if (IsEventLogging() != MagickFalse)
1269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1270 blob_info=image->blob;
1271 switch (blob_info->type)
1273 case UndefinedStream:
1274 case StandardStream:
1279 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1285#if defined(MAGICKCORE_ZLIB_DELEGATE)
1286 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1293#if defined(MAGICKCORE_BZLIB_DELEGATE)
1298 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1299 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1305 blob_info->eof=MagickFalse;
1313 return((
int) blob_info->eof);
1339MagickExport
int ErrorBlob(
const Image *image)
1342 *magick_restrict blob_info;
1344 assert(image != (
Image *) NULL);
1345 assert(image->signature == MagickCoreSignature);
1346 assert(image->blob != (
BlobInfo *) NULL);
1347 assert(image->blob->type != UndefinedStream);
1348 if (IsEventLogging() != MagickFalse)
1349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1350 blob_info=image->blob;
1351 switch (blob_info->type)
1353 case UndefinedStream:
1354 case StandardStream:
1359 blob_info->error=ferror(blob_info->file_info.file);
1364#if defined(MAGICKCORE_ZLIB_DELEGATE)
1365 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1371#if defined(MAGICKCORE_BZLIB_DELEGATE)
1372 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1386 return(blob_info->error);
1424MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1451 assert(filename != (
const char *) NULL);
1453 assert(exception->signature == MagickCoreSignature);
1454 if (IsEventLogging() != MagickFalse)
1455 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1457 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1458 if (status == MagickFalse)
1461 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1462 "NotAuthorized",
"`%s'",filename);
1466 if (LocaleCompare(filename,
"-") != 0)
1469 flags = O_RDONLY | O_BINARY;
1471 status=GetPathAttributes(filename,&attributes);
1472 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1474 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1477#if defined(O_NOFOLLOW)
1478 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1479 if (status == MagickFalse)
1482 file=open_utf8(filename,flags,0);
1486 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1489 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1490 if (status == MagickFalse)
1492 file=close_utf8(file)-1;
1494 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1495 "NotAuthorized",
"`%s'",filename);
1498 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1500 if ((file == fileno(stdin)) || (offset < 0) ||
1501 (offset != (MagickOffsetType) ((ssize_t) offset)))
1512 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1513 quantum=(size_t) MagickMaxBufferExtent;
1514 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1515 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1516 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1517 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1519 count=read(file,blob+i,quantum);
1526 if (~i < ((
size_t) count+quantum+1))
1528 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1531 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1532 quantum+1,
sizeof(*blob));
1533 if ((i+(
size_t) count) >= extent)
1536 if (LocaleCompare(filename,
"-") != 0)
1537 file=close_utf8(file);
1538 if (blob == (
unsigned char *) NULL)
1540 (void) ThrowMagickException(exception,GetMagickModule(),
1541 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1546 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1547 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1550 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1554 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1555 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1556 blob=(
unsigned char *) NULL;
1557 if (~(*length) >= (MagickPathExtent-1))
1558 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1560 if (blob == (
unsigned char *) NULL)
1562 file=close_utf8(file);
1563 (void) ThrowMagickException(exception,GetMagickModule(),
1564 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1567 map=MapBlob(file,ReadMode,0,*length);
1568 if (map != (
unsigned char *) NULL)
1570 (void) memcpy(blob,map,*length);
1571 (void) UnmapBlob(map,*length);
1575 (void) lseek(file,0,SEEK_SET);
1576 for (i=0; i < *length; i+=(size_t) count)
1578 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1579 MagickMaxBufferExtent));
1589 file=close_utf8(file)-1;
1590 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1591 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1596 if (LocaleCompare(filename,
"-") != 0)
1597 file=close_utf8(file);
1600 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1601 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1630static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1631 const void *magick_restrict data)
1634 *magick_restrict blob_info;
1642 assert(image->blob != (
BlobInfo *) NULL);
1643 assert(image->blob->type != UndefinedStream);
1644 assert(data != NULL);
1645 blob_info=image->blob;
1646 if (blob_info->type != BlobStream)
1647 return(WriteBlob(image,length,(
const unsigned char *) data));
1648 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1649 if (extent >= blob_info->extent)
1651 extent+=blob_info->quantum+length;
1652 blob_info->quantum<<=1;
1653 if (SetBlobExtent(image,extent) == MagickFalse)
1656 q=blob_info->data+blob_info->offset;
1657 (void) memcpy(q,data,length);
1658 blob_info->offset+=(MagickOffsetType) length;
1659 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1660 blob_info->length=(
size_t) blob_info->offset;
1661 return((ssize_t) length);
1664MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename,
1686 assert(image != (
const Image *) NULL);
1687 assert(image->signature == MagickCoreSignature);
1688 assert(filename != (
const char *) NULL);
1689 if (IsEventLogging() != MagickFalse)
1690 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1691 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1692 if (status == MagickFalse)
1695 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1696 "NotAuthorized",
"`%s'",filename);
1697 return(MagickFalse);
1700 if (LocaleCompare(filename,
"-") != 0)
1703 flags = O_RDONLY | O_BINARY;
1705#if defined(O_NOFOLLOW)
1706 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1707 if (status == MagickFalse)
1710 file=open_utf8(filename,flags,0);
1714 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1715 return(MagickFalse);
1717 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1718 if (status == MagickFalse)
1720 file=close_utf8(file);
1722 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1723 "NotAuthorized",
"`%s'",filename);
1724 return(MagickFalse);
1726 quantum=(size_t) MagickMaxBufferExtent;
1727 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1728 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1729 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1730 if (blob == (
unsigned char *) NULL)
1732 file=close_utf8(file);
1733 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1735 return(MagickFalse);
1739 count=read(file,blob,quantum);
1746 length=(size_t) count;
1747 count=WriteBlobStream(image,length,blob);
1748 if (count != (ssize_t) length)
1750 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1754 file=close_utf8(file);
1756 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1757 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1784MagickExport MagickBooleanType GetBlobError(
const Image *image)
1786 assert(image != (
const Image *) NULL);
1787 assert(image->signature == MagickCoreSignature);
1788 if (IsEventLogging() != MagickFalse)
1789 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1790 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1791 errno=image->blob->error_number;
1792 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1817MagickExport FILE *GetBlobFileHandle(
const Image *image)
1819 assert(image != (
const Image *) NULL);
1820 assert(image->signature == MagickCoreSignature);
1821 return(image->blob->file_info.file);
1846MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1848 assert(blob_info != (
BlobInfo *) NULL);
1849 (void) memset(blob_info,0,
sizeof(*blob_info));
1850 blob_info->type=UndefinedStream;
1851 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1852 blob_info->properties.st_mtime=GetMagickTime();
1853 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1854 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1855 blob_info->reference_count=1;
1856 blob_info->semaphore=AcquireSemaphoreInfo();
1857 blob_info->signature=MagickCoreSignature;
1882MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1884 assert(image != (
Image *) NULL);
1885 assert(image->signature == MagickCoreSignature);
1886 if (IsEventLogging() != MagickFalse)
1887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1888 return(&image->blob->properties);
1914MagickExport MagickSizeType GetBlobSize(
const Image *image)
1917 *magick_restrict blob_info;
1922 assert(image != (
Image *) NULL);
1923 assert(image->signature == MagickCoreSignature);
1924 assert(image->blob != (
BlobInfo *) NULL);
1925 if (IsEventLogging() != MagickFalse)
1926 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1927 blob_info=image->blob;
1929 switch (blob_info->type)
1931 case UndefinedStream:
1932 case StandardStream:
1934 extent=blob_info->size;
1942 extent=(MagickSizeType) blob_info->properties.st_size;
1944 extent=blob_info->size;
1945 file_descriptor=fileno(blob_info->file_info.file);
1946 if (file_descriptor == -1)
1948 if (fstat(file_descriptor,&blob_info->properties) == 0)
1949 extent=(MagickSizeType) blob_info->properties.st_size;
1954 extent=blob_info->size;
1963 status=GetPathAttributes(image->filename,&blob_info->properties);
1964 if (status != MagickFalse)
1965 extent=(MagickSizeType) blob_info->properties.st_size;
1972 extent=(MagickSizeType) blob_info->length;
1977 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1978 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1983 offset=blob_info->custom_stream->teller(
1984 blob_info->custom_stream->data);
1985 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1986 blob_info->custom_stream->data);
1987 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1988 blob_info->custom_stream->data);
2018MagickExport
void *GetBlobStreamData(
const Image *image)
2020 assert(image != (
const Image *) NULL);
2021 assert(image->signature == MagickCoreSignature);
2022 return(image->blob->data);
2047MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2049 assert(image != (
const Image *) NULL);
2050 assert(image->signature == MagickCoreSignature);
2051 if (IsEventLogging() != MagickFalse)
2052 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2053 return(image->blob->stream);
2089MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2104 assert(image_info != (
const ImageInfo *) NULL);
2105 assert(image_info->signature == MagickCoreSignature);
2106 assert(image != (
Image *) NULL);
2107 assert(image->signature == MagickCoreSignature);
2109 assert(exception->signature == MagickCoreSignature);
2110 if (IsEventLogging() != MagickFalse)
2111 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2112 image_info->filename);
2114 blob=(
unsigned char *) NULL;
2115 blob_info=CloneImageInfo(image_info);
2116 blob_info->adjoin=MagickFalse;
2117 (void) SetImageInfo(blob_info,1,exception);
2118 if (*blob_info->magick !=
'\0')
2119 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2120 magick_info=GetMagickInfo(image->magick,exception);
2121 if (magick_info == (
const MagickInfo *) NULL)
2123 (void) ThrowMagickException(exception,GetMagickModule(),
2124 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2126 blob_info=DestroyImageInfo(blob_info);
2129 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2130 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2135 blob_info->length=0;
2136 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2137 sizeof(
unsigned char));
2138 if (blob_info->blob == NULL)
2139 (void) ThrowMagickException(exception,GetMagickModule(),
2140 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2143 (void) CloseBlob(image);
2144 image->blob->exempt=MagickTrue;
2145 image->blob->extent=0;
2146 *image->filename=
'\0';
2147 status=WriteImage(blob_info,image,exception);
2148 *length=image->blob->length;
2149 blob=DetachBlob(image->blob);
2150 if (blob != (
void *) NULL)
2152 if (status == MagickFalse)
2153 blob=RelinquishMagickMemory(blob);
2155 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2157 else if ((status == MagickFalse) && (image->blob->extent == 0))
2158 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2164 unique[MagickPathExtent];
2172 file=AcquireUniqueFileResource(unique);
2175 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2176 image_info->filename);
2180 blob_info->file=fdopen(file,
"wb");
2181 if (blob_info->file != (FILE *) NULL)
2183 (void) FormatLocaleString(image->filename,MagickPathExtent,
2184 "%s:%s",image->magick,unique);
2185 status=WriteImage(blob_info,image,exception);
2186 (void) fclose(blob_info->file);
2187 if (status != MagickFalse)
2188 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2190 (void) RelinquishUniqueFileResource(unique);
2193 blob_info=DestroyImageInfo(blob_info);
2225MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,
Image *image,
2238 assert(image_info != (
const ImageInfo *) NULL);
2239 assert(image_info->signature == MagickCoreSignature);
2240 assert(image != (
Image *) NULL);
2241 assert(image->signature == MagickCoreSignature);
2243 assert(image_info->custom_stream->signature == MagickCoreSignature);
2244 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2246 if (IsEventLogging() != MagickFalse)
2247 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2248 image_info->filename);
2249 clone_info=CloneImageInfo(image_info);
2250 clone_info->adjoin=MagickFalse;
2251 (void) SetImageInfo(clone_info,1,exception);
2252 if (*clone_info->magick !=
'\0')
2253 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2254 magick_info=GetMagickInfo(image->magick,exception);
2255 if (magick_info == (
const MagickInfo *) NULL)
2257 (void) ThrowMagickException(exception,GetMagickModule(),
2258 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2260 clone_info=DestroyImageInfo(clone_info);
2263 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2264 blob_support=GetMagickBlobSupport(magick_info);
2265 if ((blob_support != MagickFalse) &&
2266 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2268 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2269 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2270 blob_support=MagickFalse;
2272 if (blob_support != MagickFalse)
2277 (void) CloseBlob(image);
2278 *image->filename=
'\0';
2279 (void) WriteImage(clone_info,image,exception);
2284 unique[MagickPathExtent];
2296 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2298 if (blob == (
unsigned char *) NULL)
2300 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2301 image_info->filename);
2302 clone_info=DestroyImageInfo(clone_info);
2305 file=AcquireUniqueFileResource(unique);
2308 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2309 image_info->filename);
2310 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2311 clone_info=DestroyImageInfo(clone_info);
2314 clone_info->file=fdopen(file,
"wb+");
2315 if (clone_info->file != (FILE *) NULL)
2320 (void) FormatLocaleString(image->filename,MagickPathExtent,
2321 "%s:%s",image->magick,unique);
2322 status=WriteImage(clone_info,image,exception);
2323 if (status != MagickFalse)
2325 (void) fseek(clone_info->file,0,SEEK_SET);
2326 count=(ssize_t) MagickMaxBufferExtent;
2327 while (count == (ssize_t) MagickMaxBufferExtent)
2329 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2331 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2332 image_info->custom_stream->data);
2335 (void) fclose(clone_info->file);
2337 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2338 (void) RelinquishUniqueFileResource(unique);
2340 clone_info=DestroyImageInfo(clone_info);
2371MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
2396 assert(image != (
Image *) NULL);
2397 assert(image->signature == MagickCoreSignature);
2398 assert(image->blob != (
BlobInfo *) NULL);
2399 assert(image->blob->type != UndefinedStream);
2400 assert(filename != (
const char *) NULL);
2401 if (IsEventLogging() != MagickFalse)
2402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2403 if (*filename ==
'\0')
2404 file=AcquireUniqueFileResource(filename);
2406 if (LocaleCompare(filename,
"-") == 0)
2407 file=fileno(stdout);
2409 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
2412 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2413 return(MagickFalse);
2415 quantum=(size_t) MagickMaxBufferExtent;
2416 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2417 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2418 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2419 if (buffer == (
unsigned char *) NULL)
2421 file=close_utf8(file)-1;
2422 (void) ThrowMagickException(exception,GetMagickModule(),
2423 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2424 return(MagickFalse);
2427 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2428 for (i=0; count > 0; )
2430 length=(size_t) count;
2431 for (i=0; i < length; i+=(size_t) count)
2433 count=write(file,p+i,(
size_t) (length-i));
2443 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2445 if (LocaleCompare(filename,
"-") != 0)
2446 file=close_utf8(file);
2447 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2448 if ((file == -1) || (i < length))
2451 file=close_utf8(file);
2452 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2453 return(MagickFalse);
2493MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,
Image *images,
2508 assert(image_info != (
const ImageInfo *) NULL);
2509 assert(image_info->signature == MagickCoreSignature);
2510 assert(images != (
Image *) NULL);
2511 assert(images->signature == MagickCoreSignature);
2513 if (IsEventLogging() != MagickFalse)
2514 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2515 image_info->filename);
2517 blob=(
unsigned char *) NULL;
2518 blob_info=CloneImageInfo(image_info);
2519 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2521 if (*blob_info->magick !=
'\0')
2522 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2523 magick_info=GetMagickInfo(images->magick,exception);
2524 if (magick_info == (
const MagickInfo *) NULL)
2526 (void) ThrowMagickException(exception,GetMagickModule(),
2527 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2529 blob_info=DestroyImageInfo(blob_info);
2532 if (GetMagickAdjoin(magick_info) == MagickFalse)
2534 blob_info=DestroyImageInfo(blob_info);
2535 return(ImageToBlob(image_info,images,length,exception));
2537 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2538 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2543 blob_info->length=0;
2544 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2545 sizeof(
unsigned char));
2546 if (blob_info->blob == (
void *) NULL)
2547 (void) ThrowMagickException(exception,GetMagickModule(),
2548 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2551 (void) CloseBlob(images);
2552 images->blob->exempt=MagickTrue;
2553 images->blob->extent=0;
2554 *images->filename=
'\0';
2555 status=WriteImages(blob_info,images,images->filename,exception);
2556 *length=images->blob->length;
2557 blob=DetachBlob(images->blob);
2558 if (blob != (
void *) NULL)
2560 if (status == MagickFalse)
2561 blob=RelinquishMagickMemory(blob);
2563 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2565 else if ((status == MagickFalse) && (images->blob->extent == 0))
2566 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2572 filename[MagickPathExtent],
2573 unique[MagickPathExtent];
2581 file=AcquireUniqueFileResource(unique);
2584 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2585 image_info->filename);
2589 blob_info->file=fdopen(file,
"wb");
2590 if (blob_info->file != (FILE *) NULL)
2592 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2593 images->magick,unique);
2594 status=WriteImages(blob_info,images,filename,exception);
2595 (void) fclose(blob_info->file);
2596 if (status != MagickFalse)
2597 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2599 (void) RelinquishUniqueFileResource(unique);
2602 blob_info=DestroyImageInfo(blob_info);
2634MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2647 assert(image_info != (
const ImageInfo *) NULL);
2648 assert(image_info->signature == MagickCoreSignature);
2649 assert(images != (
Image *) NULL);
2650 assert(images->signature == MagickCoreSignature);
2652 assert(image_info->custom_stream->signature == MagickCoreSignature);
2653 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2655 if (IsEventLogging() != MagickFalse)
2656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2657 image_info->filename);
2658 clone_info=CloneImageInfo(image_info);
2659 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2661 if (*clone_info->magick !=
'\0')
2662 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2663 magick_info=GetMagickInfo(images->magick,exception);
2664 if (magick_info == (
const MagickInfo *) NULL)
2666 (void) ThrowMagickException(exception,GetMagickModule(),
2667 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2669 clone_info=DestroyImageInfo(clone_info);
2672 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2673 blob_support=GetMagickBlobSupport(magick_info);
2674 if ((blob_support != MagickFalse) &&
2675 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2677 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2678 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2679 blob_support=MagickFalse;
2681 if (blob_support != MagickFalse)
2686 (void) CloseBlob(images);
2687 *images->filename=
'\0';
2688 (void) WriteImages(clone_info,images,images->filename,exception);
2693 filename[MagickPathExtent],
2694 unique[MagickPathExtent];
2706 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2708 if (blob == (
unsigned char *) NULL)
2710 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2711 image_info->filename);
2712 clone_info=DestroyImageInfo(clone_info);
2715 file=AcquireUniqueFileResource(unique);
2718 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2719 image_info->filename);
2720 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2721 clone_info=DestroyImageInfo(clone_info);
2724 clone_info->file=fdopen(file,
"wb+");
2725 if (clone_info->file != (FILE *) NULL)
2730 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2731 images->magick,unique);
2732 status=WriteImages(clone_info,images,filename,exception);
2733 if (status != MagickFalse)
2735 (void) fseek(clone_info->file,0,SEEK_SET);
2736 count=(ssize_t) MagickMaxBufferExtent;
2737 while (count == (ssize_t) MagickMaxBufferExtent)
2739 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2741 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2742 image_info->custom_stream->data);
2745 (void) fclose(clone_info->file);
2747 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2748 (void) RelinquishUniqueFileResource(unique);
2750 clone_info=DestroyImageInfo(clone_info);
2786MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2790 filename[MagickPathExtent];
2819 assert(image_info != (
ImageInfo *) NULL);
2820 assert(image_info->signature == MagickCoreSignature);
2821 assert(image != (
Image *) NULL);
2822 assert(image->signature == MagickCoreSignature);
2823 assert(inject_image != (
Image *) NULL);
2824 assert(inject_image->signature == MagickCoreSignature);
2826 if (IsEventLogging() != MagickFalse)
2827 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2828 unique_file=(FILE *) NULL;
2829 file=AcquireUniqueFileResource(filename);
2831 unique_file=fdopen(file,
"wb");
2832 if ((file == -1) || (unique_file == (FILE *) NULL))
2834 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2835 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2837 return(MagickFalse);
2839 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2840 if (byte_image == (
Image *) NULL)
2842 (void) fclose(unique_file);
2843 (void) RelinquishUniqueFileResource(filename);
2844 return(MagickFalse);
2846 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2848 DestroyBlob(byte_image);
2849 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2850 write_info=CloneImageInfo(image_info);
2851 SetImageInfoFile(write_info,unique_file);
2852 status=WriteImage(write_info,byte_image,exception);
2853 write_info=DestroyImageInfo(write_info);
2854 byte_image=DestroyImage(byte_image);
2855 (void) fclose(unique_file);
2856 if (status == MagickFalse)
2858 (void) RelinquishUniqueFileResource(filename);
2859 return(MagickFalse);
2864 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2867 (void) RelinquishUniqueFileResource(filename);
2868 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2869 image_info->filename);
2870 return(MagickFalse);
2872 quantum=(size_t) MagickMaxBufferExtent;
2873 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2874 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2875 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2876 if (buffer == (
unsigned char *) NULL)
2878 (void) RelinquishUniqueFileResource(filename);
2879 file=close_utf8(file);
2880 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2885 ssize_t count = read(file,buffer,quantum);
2892 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2895 file=close_utf8(file);
2897 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2898 (void) RelinquishUniqueFileResource(filename);
2899 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2925MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2927 assert(image != (
const Image *) NULL);
2928 assert(image->signature == MagickCoreSignature);
2929 if (IsEventLogging() != MagickFalse)
2930 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2931 return(image->blob->exempt);
2956MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2959 *magick_restrict blob_info;
2961 assert(image != (
const Image *) NULL);
2962 assert(image->signature == MagickCoreSignature);
2963 if (IsEventLogging() != MagickFalse)
2964 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2965 blob_info=image->blob;
2966 switch (blob_info->type)
2975 if (blob_info->file_info.file == (FILE *) NULL)
2976 return(MagickFalse);
2977 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2978 return(status == -1 ? MagickFalse : MagickTrue);
2982#if defined(MAGICKCORE_ZLIB_DELEGATE)
2986 if (blob_info->file_info.gzfile == (gzFile) NULL)
2987 return(MagickFalse);
2988 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2989 return(offset < 0 ? MagickFalse : MagickTrue);
2994 case UndefinedStream:
2998 case StandardStream:
3002 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3003 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3010 return(MagickFalse);
3035MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3037 assert(image != (
const Image *) NULL);
3038 assert(image->signature == MagickCoreSignature);
3039 if (IsEventLogging() != MagickFalse)
3040 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3041 return(image->blob->temporary);
3073MagickExport
void *MapBlob(
int file,
const MapMode mode,
3074 const MagickOffsetType offset,
const size_t length)
3076#if defined(MAGICKCORE_HAVE_MMAP)
3089#if defined(MAP_ANONYMOUS)
3090 flags|=MAP_ANONYMOUS;
3099 protection=PROT_READ;
3105 protection=PROT_WRITE;
3111 protection=PROT_READ | PROT_WRITE;
3116#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3117 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3119 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3120 if (map == MAP_FAILED)
3121 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3123 if (map == MAP_FAILED)
3160MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3169 assert(buffer != (
unsigned char *) NULL);
3176 *buffer++=(
unsigned char) c;
3180 *buffer++=(
unsigned char) c;
3210MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3218 assert(p != (
unsigned char *) NULL);
3225 *p++=(
unsigned char) c;
3261static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3273 size=MagickMinBufferExtent;
3274 option=GetImageOption(image_info,
"stream:buffer-size");
3275 if (option != (
const char *) NULL)
3276 size=StringToUnsignedLong(option);
3277 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3278 _IONBF : _IOFBF,size);
3279 return(status == 0 ? MagickTrue : MagickFalse);
3282#if defined(MAGICKCORE_ZLIB_DELEGATE)
3283static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3285#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3286 return(gzopen(path,mode));
3294 path_wide=create_wchar_path(path);
3295 if (path_wide == (
wchar_t *) NULL)
3296 return((gzFile) NULL);
3297 file=gzopen_w(path_wide,mode);
3298 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3304MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3308 *magick_restrict blob_info;
3311 extension[MagickPathExtent],
3312 filename[MagickPathExtent];
3326 assert(image_info != (
ImageInfo *) NULL);
3327 assert(image_info->signature == MagickCoreSignature);
3328 assert(image != (
Image *) NULL);
3329 assert(image->signature == MagickCoreSignature);
3330 if (IsEventLogging() != MagickFalse)
3331 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3332 image_info->filename);
3333 blob_info=image->blob;
3334 if (image_info->blob != (
void *) NULL)
3336 if (image_info->stream != (StreamHandler) NULL)
3337 blob_info->stream=(StreamHandler) image_info->stream;
3338 AttachBlob(blob_info,image_info->blob,image_info->length);
3342 (*image->filename ==
'\0'))
3344 blob_info->type=CustomStream;
3345 blob_info->custom_stream=image_info->custom_stream;
3348 (void) DetachBlob(blob_info);
3349 blob_info->mode=mode;
3358 case ReadBinaryBlobMode:
3360 flags=O_RDONLY | O_BINARY;
3366 flags=O_WRONLY | O_CREAT | O_TRUNC;
3370 case WriteBinaryBlobMode:
3372 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3376 case AppendBlobMode:
3378 flags=O_WRONLY | O_CREAT | O_APPEND;
3382 case AppendBinaryBlobMode:
3384 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3396 blob_info->synchronize=image_info->synchronize;
3397 if (image_info->stream != (StreamHandler) NULL)
3399 blob_info->stream=image_info->stream;
3402 blob_info->type=FifoStream;
3410 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3411 rights=ReadPolicyRights;
3413 rights=WritePolicyRights;
3414 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3417 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3418 "NotAuthorized",
"`%s'",filename);
3419 return(MagickFalse);
3421 if ((LocaleCompare(filename,
"-") == 0) ||
3422 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3424 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3425#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3426 if (strchr(type,
'b') != (
char *) NULL)
3427 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3429 blob_info->type=StandardStream;
3430 blob_info->exempt=MagickTrue;
3431 return(SetStreamBuffering(image_info,blob_info));
3433 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3434 (IsGeometry(filename+3) != MagickFalse))
3441 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3442 if (blob_info->file_info.file == (FILE *) NULL)
3444 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3445 return(MagickFalse);
3447#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3448 if (strchr(type,
'b') != (
char *) NULL)
3449 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3451 blob_info->type=FileStream;
3452 blob_info->exempt=MagickTrue;
3453 return(SetStreamBuffering(image_info,blob_info));
3455#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3456 if (*filename ==
'|')
3459 fileMode[MagickPathExtent],
3467 (void) signal(SIGPIPE,SIG_IGN);
3471 sanitize_command=SanitizeString(filename+1);
3472 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3473 sanitize_command=DestroyString(sanitize_command);
3474 if (blob_info->file_info.file == (FILE *) NULL)
3476 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3477 return(MagickFalse);
3479 blob_info->type=PipeStream;
3480 blob_info->exempt=MagickTrue;
3481 return(SetStreamBuffering(image_info,blob_info));
3484 status=GetPathAttributes(filename,&blob_info->properties);
3485#if defined(S_ISFIFO)
3486 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3488 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3489 if (blob_info->file_info.file == (FILE *) NULL)
3491 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3492 return(MagickFalse);
3494 blob_info->type=FileStream;
3495 blob_info->exempt=MagickTrue;
3496 return(SetStreamBuffering(image_info,blob_info));
3499 GetPathComponent(image->filename,ExtensionPath,extension);
3502 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3503 if ((image_info->adjoin == MagickFalse) ||
3504 (strchr(filename,
'%') != (
char *) NULL))
3509 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3510 image->scene,filename,exception);
3511 if ((LocaleCompare(filename,image->filename) == 0) &&
3512 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
3513 (GetNextImageInList(image) != (
Image *) NULL)))
3516 path[MagickPathExtent];
3518 GetPathComponent(image->filename,RootPath,path);
3519 if (*extension ==
'\0')
3520 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3521 path,(
double) image->scene);
3523 (
void) FormatLocaleString(filename,MagickPathExtent,
3524 "%s-%.20g.%s",path,(
double) image->scene,extension);
3526 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3528 if (IsPathAuthorized(rights,filename) == MagickFalse)
3531 (void) ThrowMagickException(exception,GetMagickModule(),
3532 PolicyError,
"NotAuthorized",
"`%s'",filename);
3533 return(MagickFalse);
3536 if (image_info->file != (FILE *) NULL)
3538 blob_info->file_info.file=image_info->file;
3539 blob_info->type=FileStream;
3540 blob_info->exempt=MagickTrue;
3548 blob_info->file_info.file=(FILE *) NULL;
3549#if defined(O_NOFOLLOW)
3550 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3551 if (status == MagickFalse)
3554 file=open_utf8(filename,flags,0);
3556 blob_info->file_info.file=fdopen(file,type);
3557 if (blob_info->file_info.file != (FILE *) NULL)
3565 blob_info->type=FileStream;
3566 (void) SetStreamBuffering(image_info,blob_info);
3567 (void) memset(magick,0,
sizeof(magick));
3568 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3569 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3570#if defined(MAGICKCORE_POSIX_SUPPORT)
3571 (void) fflush(blob_info->file_info.file);
3573 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3574 " read %.20g magic header bytes",(double) count);
3575#if defined(MAGICKCORE_ZLIB_DELEGATE)
3576 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3577 ((
int) magick[2] == 0x08))
3580 gzfile = gzopen_utf8(filename,
"rb");
3582 if (gzfile != (gzFile) NULL)
3584 if (blob_info->file_info.file != (FILE *) NULL)
3585 (void) fclose(blob_info->file_info.file);
3586 blob_info->file_info.file=(FILE *) NULL;
3587 blob_info->file_info.gzfile=gzfile;
3588 blob_info->type=ZipStream;
3592#if defined(MAGICKCORE_BZLIB_DELEGATE)
3593 if (strncmp((
char *) magick,
"BZh",3) == 0)
3596 *bzfile = BZ2_bzopen(filename,
"r");
3598 if (bzfile != (BZFILE *) NULL)
3600 if (blob_info->file_info.file != (FILE *) NULL)
3601 (void) fclose(blob_info->file_info.file);
3602 blob_info->file_info.file=(FILE *) NULL;
3603 blob_info->file_info.bzfile=bzfile;
3604 blob_info->type=BZipStream;
3608 if (blob_info->type == FileStream)
3619 sans_exception=AcquireExceptionInfo();
3620 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3621 sans_exception=DestroyExceptionInfo(sans_exception);
3622 length=(size_t) blob_info->properties.st_size;
3623 if ((magick_info != (
const MagickInfo *) NULL) &&
3624 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3625 (length > MagickMaxBufferExtent) &&
3626 (AcquireMagickResource(MapResource,length) != MagickFalse))
3631 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3633 if (blob == (
void *) NULL)
3634 RelinquishMagickResource(MapResource,length);
3640 if (image_info->file != (FILE *) NULL)
3641 blob_info->exempt=MagickFalse;
3644 (void) fclose(blob_info->file_info.file);
3645 blob_info->file_info.file=(FILE *) NULL;
3647 AttachBlob(blob_info,blob,length);
3648 blob_info->mapped=MagickTrue;
3655#if defined(MAGICKCORE_ZLIB_DELEGATE)
3656 if ((LocaleCompare(extension,
"gz") == 0) ||
3657 (LocaleCompare(extension,
"wmz") == 0) ||
3658 (LocaleCompare(extension,
"svgz") == 0))
3660 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3661 if (blob_info->file_info.gzfile != (gzFile) NULL)
3662 blob_info->type=ZipStream;
3666#if defined(MAGICKCORE_BZLIB_DELEGATE)
3667 if (LocaleCompare(extension,
"bz2") == 0)
3669 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3670 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3671 blob_info->type=BZipStream;
3679 blob_info->file_info.file=(FILE *) NULL;
3680#if defined(O_NOFOLLOW)
3681 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
3683 if (status == MagickFalse)
3686 file=open_utf8(filename,flags,0666);
3688 blob_info->file_info.file=fdopen(file,type);
3689 if (blob_info->file_info.file != (FILE *) NULL)
3691 blob_info->type=FileStream;
3692 (void) SetStreamBuffering(image_info,blob_info);
3695 blob_info->status=0;
3696 blob_info->error_number=0;
3697 if (blob_info->type != UndefinedStream)
3698 blob_info->size=GetBlobSize(image);
3701 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3702 return(MagickFalse);
3741#if defined(__cplusplus) || defined(c_plusplus)
3745static size_t PingStream(
const Image *magick_unused(image),
3746 const void *magick_unused(pixels),
const size_t columns)
3748 magick_unreferenced(image);
3749 magick_unreferenced(pixels);
3753#if defined(__cplusplus) || defined(c_plusplus)
3757MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3773 assert(image_info != (
ImageInfo *) NULL);
3774 assert(image_info->signature == MagickCoreSignature);
3776 if (IsEventLogging() != MagickFalse)
3777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3778 image_info->filename);
3779 if ((blob == (
const void *) NULL) || (length == 0))
3781 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3782 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3783 return((
Image *) NULL);
3785 ping_info=CloneImageInfo(image_info);
3786 ping_info->blob=(
void *) blob;
3787 ping_info->length=length;
3788 ping_info->ping=MagickTrue;
3789 if (*ping_info->magick ==
'\0')
3790 (void) SetImageInfo(ping_info,0,exception);
3791 magick_info=GetMagickInfo(ping_info->magick,exception);
3792 if (magick_info == (
const MagickInfo *) NULL)
3794 (void) ThrowMagickException(exception,GetMagickModule(),
3795 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3797 ping_info=DestroyImageInfo(ping_info);
3798 return((
Image *) NULL);
3800 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3803 filename[MagickPathExtent];
3808 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3809 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3810 ping_info->magick,filename);
3811 image=ReadStream(ping_info,&PingStream,exception);
3812 if (image != (
Image *) NULL)
3813 (void) DetachBlob(image->blob);
3814 ping_info=DestroyImageInfo(ping_info);
3820 ping_info->blob=(
void *) NULL;
3821 ping_info->length=0;
3822 *ping_info->filename=
'\0';
3823 status=BlobToFile(ping_info->filename,blob,length,exception);
3824 if (status == MagickFalse)
3826 (void) RelinquishUniqueFileResource(ping_info->filename);
3827 ping_info=DestroyImageInfo(ping_info);
3828 return((
Image *) NULL);
3830 clone_info=CloneImageInfo(ping_info);
3831 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3832 ping_info->magick,ping_info->filename);
3833 image=ReadStream(clone_info,&PingStream,exception);
3834 if (image != (
Image *) NULL)
3842 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3844 (void) CopyMagickString(images->filename,image_info->filename,
3846 (void) CopyMagickString(images->magick_filename,image_info->filename,
3848 (void) CopyMagickString(images->magick,magick_info->name,
3850 images=GetNextImageInList(images);
3853 clone_info=DestroyImageInfo(clone_info);
3854 (void) RelinquishUniqueFileResource(ping_info->filename);
3855 ping_info=DestroyImageInfo(ping_info);
3890MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
void *data)
3893 *magick_restrict blob_info;
3904 assert(image != (
Image *) NULL);
3905 assert(image->signature == MagickCoreSignature);
3906 assert(image->blob != (
BlobInfo *) NULL);
3907 assert(image->blob->type != UndefinedStream);
3910 assert(data != (
void *) NULL);
3911 blob_info=image->blob;
3913 q=(
unsigned char *) data;
3914 switch (blob_info->type)
3916 case UndefinedStream:
3918 case StandardStream:
3926 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3931 c=getc(blob_info->file_info.file);
3934 *q++=(
unsigned char) c;
3940 c=getc(blob_info->file_info.file);
3943 *q++=(
unsigned char) c;
3949 c=getc(blob_info->file_info.file);
3952 *q++=(
unsigned char) c;
3958 c=getc(blob_info->file_info.file);
3961 *q++=(
unsigned char) c;
3968 if ((count != (ssize_t) length) &&
3969 (ferror(blob_info->file_info.file) != 0))
3970 ThrowBlobException(blob_info);
3975#if defined(MAGICKCORE_ZLIB_DELEGATE)
3986 for (i=0; i < length; i+=(size_t) count)
3988 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3989 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4002 c=gzgetc(blob_info->file_info.gzfile);
4005 *q++=(
unsigned char) c;
4011 c=gzgetc(blob_info->file_info.gzfile);
4014 *q++=(
unsigned char) c;
4020 c=gzgetc(blob_info->file_info.gzfile);
4023 *q++=(
unsigned char) c;
4029 c=gzgetc(blob_info->file_info.gzfile);
4032 *q++=(
unsigned char) c;
4039 (void) gzerror(blob_info->file_info.gzfile,&status);
4040 if ((count != (ssize_t) length) && (status != Z_OK))
4041 ThrowBlobException(blob_info);
4042 if (blob_info->eof == MagickFalse)
4043 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4050#if defined(MAGICKCORE_BZLIB_DELEGATE)
4057 for (i=0; i < length; i+=(size_t) count)
4059 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4060 MagickMin(length-i,MagickMaxBufferExtent));
4070 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4071 if ((count != (ssize_t) length) && (status != BZ_OK))
4072 ThrowBlobException(blob_info);
4083 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4085 blob_info->eof=MagickTrue;
4088 p=blob_info->data+blob_info->offset;
4089 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4090 blob_info->length-blob_info->offset);
4091 blob_info->offset+=count;
4092 if (count != (ssize_t) length)
4093 blob_info->eof=MagickTrue;
4094 (void) memcpy(q,p,(
size_t) count);
4099 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4100 count=blob_info->custom_stream->reader(q,length,
4101 blob_info->custom_stream->data);
4130MagickExport
int ReadBlobByte(
Image *image)
4133 *magick_restrict blob_info;
4138 assert(image != (
Image *) NULL);
4139 assert(image->signature == MagickCoreSignature);
4140 assert(image->blob != (
BlobInfo *) NULL);
4141 assert(image->blob->type != UndefinedStream);
4142 blob_info=image->blob;
4143 switch (blob_info->type)
4145 case StandardStream:
4149 c=getc(blob_info->file_info.file);
4152 if (ferror(blob_info->file_info.file) != 0)
4153 ThrowBlobException(blob_info);
4160 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4162 blob_info->eof=MagickTrue;
4165 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4166 blob_info->offset++;
4177 count=ReadBlob(image,1,buffer);
4210MagickExport
double ReadBlobDouble(
Image *image)
4221 quantum.double_value=0.0;
4222 quantum.unsigned_value=ReadBlobLongLong(image);
4223 return(quantum.double_value);
4249MagickExport
float ReadBlobFloat(
Image *image)
4260 quantum.float_value=0.0;
4261 quantum.unsigned_value=ReadBlobLong(image);
4262 return(quantum.float_value);
4288MagickExport
unsigned int ReadBlobLong(
Image *image)
4302 assert(image != (
Image *) NULL);
4303 assert(image->signature == MagickCoreSignature);
4305 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4308 if (image->endian == LSBEndian)
4310 value=(
unsigned int) (*p++);
4311 value|=(
unsigned int) (*p++) << 8;
4312 value|=(
unsigned int) (*p++) << 16;
4313 value|=(
unsigned int) (*p++) << 24;
4316 value=(
unsigned int) (*p++) << 24;
4317 value|=(
unsigned int) (*p++) << 16;
4318 value|=(
unsigned int) (*p++) << 8;
4319 value|=(
unsigned int) (*p++);
4346MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
4360 assert(image != (
Image *) NULL);
4361 assert(image->signature == MagickCoreSignature);
4363 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4365 return(MagickULLConstant(0));
4366 if (image->endian == LSBEndian)
4368 value=(MagickSizeType) (*p++);
4369 value|=(MagickSizeType) (*p++) << 8;
4370 value|=(MagickSizeType) (*p++) << 16;
4371 value|=(MagickSizeType) (*p++) << 24;
4372 value|=(MagickSizeType) (*p++) << 32;
4373 value|=(MagickSizeType) (*p++) << 40;
4374 value|=(MagickSizeType) (*p++) << 48;
4375 value|=(MagickSizeType) (*p++) << 56;
4378 value=(MagickSizeType) (*p++) << 56;
4379 value|=(MagickSizeType) (*p++) << 48;
4380 value|=(MagickSizeType) (*p++) << 40;
4381 value|=(MagickSizeType) (*p++) << 32;
4382 value|=(MagickSizeType) (*p++) << 24;
4383 value|=(MagickSizeType) (*p++) << 16;
4384 value|=(MagickSizeType) (*p++) << 8;
4385 value|=(MagickSizeType) (*p++);
4412MagickExport
unsigned short ReadBlobShort(
Image *image)
4426 assert(image != (
Image *) NULL);
4427 assert(image->signature == MagickCoreSignature);
4429 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4431 return((
unsigned short) 0U);
4432 if (image->endian == LSBEndian)
4434 value=(
unsigned short) (*p++);
4435 value|=(
unsigned short) (*p++) << 8;
4438 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4439 value|=(
unsigned short) (*p++);
4466MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
4480 assert(image != (
Image *) NULL);
4481 assert(image->signature == MagickCoreSignature);
4483 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4486 value=(
unsigned int) (*p++);
4487 value|=(
unsigned int) (*p++) << 8;
4488 value|=(
unsigned int) (*p++) << 16;
4489 value|=(
unsigned int) (*p++) << 24;
4516MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
4527 quantum.unsigned_value=ReadBlobLSBLong(image);
4528 return(quantum.signed_value);
4554MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
4568 assert(image != (
Image *) NULL);
4569 assert(image->signature == MagickCoreSignature);
4571 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4573 return((
unsigned short) 0U);
4574 value=(
unsigned short) (*p++);
4575 value|=(
unsigned short) (*p++) << 8;
4602MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
4613 quantum.unsigned_value=ReadBlobLSBShort(image);
4614 return(quantum.signed_value);
4640MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
4654 assert(image != (
Image *) NULL);
4655 assert(image->signature == MagickCoreSignature);
4657 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4660 value=(
unsigned int) (*p++) << 24;
4661 value|=(
unsigned int) (*p++) << 16;
4662 value|=(
unsigned int) (*p++) << 8;
4663 value|=(
unsigned int) (*p++);
4690MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
4704 assert(image != (
Image *) NULL);
4705 assert(image->signature == MagickCoreSignature);
4707 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4709 return(MagickULLConstant(0));
4710 value=(MagickSizeType) (*p++) << 56;
4711 value|=(MagickSizeType) (*p++) << 48;
4712 value|=(MagickSizeType) (*p++) << 40;
4713 value|=(MagickSizeType) (*p++) << 32;
4714 value|=(MagickSizeType) (*p++) << 24;
4715 value|=(MagickSizeType) (*p++) << 16;
4716 value|=(MagickSizeType) (*p++) << 8;
4717 value|=(MagickSizeType) (*p++);
4744MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
4758 assert(image != (
Image *) NULL);
4759 assert(image->signature == MagickCoreSignature);
4761 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4763 return((
unsigned short) 0U);
4764 value=(
unsigned short) ((*p++) << 8);
4765 value|=(
unsigned short) (*p++);
4766 return((
unsigned short) (value & 0xffff));
4792MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4803 quantum.unsigned_value=ReadBlobMSBLong(image);
4804 return(quantum.signed_value);
4830MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4841 quantum.unsigned_value=ReadBlobMSBShort(image);
4842 return(quantum.signed_value);
4868MagickExport
signed int ReadBlobSignedLong(
Image *image)
4879 quantum.unsigned_value=ReadBlobLong(image);
4880 return(quantum.signed_value);
4906MagickExport
signed short ReadBlobSignedShort(
Image *image)
4917 quantum.unsigned_value=ReadBlobShort(image);
4918 return(quantum.signed_value);
4956MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4957 const size_t length,
void *magick_restrict data,ssize_t *count)
4960 *magick_restrict blob_info;
4962 assert(image != (
Image *) NULL);
4963 assert(image->signature == MagickCoreSignature);
4964 assert(image->blob != (
BlobInfo *) NULL);
4965 assert(image->blob->type != UndefinedStream);
4966 assert(count != (ssize_t *) NULL);
4967 blob_info=image->blob;
4968 if (blob_info->type != BlobStream)
4970 assert(data != NULL);
4971 *count=ReadBlob(image,length,(
unsigned char *) data);
4974 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4977 blob_info->eof=MagickTrue;
4980 data=blob_info->data+blob_info->offset;
4981 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4982 blob_info->length-blob_info->offset);
4983 blob_info->offset+=(*count);
4984 if (*count != (ssize_t) length)
4985 blob_info->eof=MagickTrue;
5014MagickExport
char *ReadBlobString(
Image *image,
char *
string)
5017 *magick_restrict blob_info;
5025 assert(image != (
Image *) NULL);
5026 assert(image->signature == MagickCoreSignature);
5027 assert(image->blob != (
BlobInfo *) NULL);
5028 assert(image->blob->type != UndefinedStream);
5029 if (IsEventLogging() != MagickFalse)
5030 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5032 blob_info=image->blob;
5033 switch (blob_info->type)
5035 case UndefinedStream:
5037 case StandardStream:
5040 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5041 if (p == (
char *) NULL)
5043 if (ferror(blob_info->file_info.file) != 0)
5044 ThrowBlobException(blob_info);
5045 return((
char *) NULL);
5052#if defined(MAGICKCORE_ZLIB_DELEGATE)
5053 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5054 if (p == (
char *) NULL)
5057 (void) gzerror(blob_info->file_info.gzfile,&status);
5059 ThrowBlobException(blob_info);
5060 return((
char *) NULL);
5070 c=ReadBlobByte(image);
5073 blob_info->eof=MagickTrue;
5079 }
while (i < (MaxTextExtent-2));
5087 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5090 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5092 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5093 return((
char *) NULL);
5123 assert(blob->signature == MagickCoreSignature);
5124 if (IsEventLogging() != MagickFalse)
5125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5126 LockSemaphoreInfo(blob->semaphore);
5127 blob->reference_count++;
5128 UnlockSemaphoreInfo(blob->semaphore);
5165MagickExport MagickOffsetType SeekBlob(
Image *image,
5166 const MagickOffsetType offset,
const int whence)
5169 *magick_restrict blob_info;
5171 assert(image != (
Image *) NULL);
5172 assert(image->signature == MagickCoreSignature);
5173 assert(image->blob != (
BlobInfo *) NULL);
5174 assert(image->blob->type != UndefinedStream);
5175 if (IsEventLogging() != MagickFalse)
5176 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5177 blob_info=image->blob;
5178 switch (blob_info->type)
5180 case UndefinedStream:
5182 case StandardStream:
5187 if ((offset < 0) && (whence == SEEK_SET))
5189 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5191 blob_info->offset=TellBlob(image);
5196#if defined(MAGICKCORE_ZLIB_DELEGATE)
5197 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
5200 blob_info->offset=TellBlob(image);
5216 blob_info->offset=offset;
5221 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5222 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5227 if ((blob_info->offset+offset) < 0)
5229 blob_info->offset+=offset;
5234 if (((MagickOffsetType) blob_info->length+offset) < 0)
5236 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5240 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5242 blob_info->eof=MagickFalse;
5249 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5251 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5252 blob_info->custom_stream->data);
5256 return(blob_info->offset);
5284MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
5286 assert(image != (
const Image *) NULL);
5287 assert(image->signature == MagickCoreSignature);
5288 if (IsEventLogging() != MagickFalse)
5289 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5290 image->blob->exempt=exempt;
5319MagickExport MagickBooleanType SetBlobExtent(
Image *image,
5320 const MagickSizeType extent)
5323 *magick_restrict blob_info;
5325 assert(image != (
Image *) NULL);
5326 assert(image->signature == MagickCoreSignature);
5327 assert(image->blob != (
BlobInfo *) NULL);
5328 assert(image->blob->type != UndefinedStream);
5329 if (IsEventLogging() != MagickFalse)
5330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5331 blob_info=image->blob;
5332 switch (blob_info->type)
5334 case UndefinedStream:
5336 case StandardStream:
5337 return(MagickFalse);
5346 if (extent != (MagickSizeType) ((off_t) extent))
5347 return(MagickFalse);
5348 offset=SeekBlob(image,0,SEEK_END);
5350 return(MagickFalse);
5351 if ((MagickSizeType) offset >= extent)
5353 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5356 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5357 blob_info->file_info.file);
5358#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5359 if (blob_info->synchronize != MagickFalse)
5364 file=fileno(blob_info->file_info.file);
5365 if ((file == -1) || (offset < 0))
5366 return(MagickFalse);
5367 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5370 offset=SeekBlob(image,offset,SEEK_SET);
5372 return(MagickFalse);
5377 return(MagickFalse);
5379 return(MagickFalse);
5381 return(MagickFalse);
5384 if (extent != (MagickSizeType) ((
size_t) extent))
5385 return(MagickFalse);
5386 if (blob_info->mapped != MagickFalse)
5394 (void) UnmapBlob(blob_info->data,blob_info->length);
5395 RelinquishMagickResource(MapResource,blob_info->length);
5396 if (extent != (MagickSizeType) ((off_t) extent))
5397 return(MagickFalse);
5398 offset=SeekBlob(image,0,SEEK_END);
5400 return(MagickFalse);
5401 if ((MagickSizeType) offset >= extent)
5403 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5404 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5405 blob_info->file_info.file);
5406#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5407 if (blob_info->synchronize != MagickFalse)
5412 file=fileno(blob_info->file_info.file);
5413 if ((file == -1) || (offset < 0))
5414 return(MagickFalse);
5415 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5419 offset=SeekBlob(image,offset,SEEK_SET);
5421 return(MagickFalse);
5422 (void) AcquireMagickResource(MapResource,extent);
5423 blob_info->data=(
unsigned char*) MapBlob(fileno(
5424 blob_info->file_info.file),WriteMode,0,(size_t) extent);
5425 blob_info->extent=(size_t) extent;
5426 blob_info->length=(size_t) extent;
5427 (void) SyncBlob(image);
5430 blob_info->extent=(size_t) extent;
5431 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5432 blob_info->extent+1,
sizeof(*blob_info->data));
5433 (void) SyncBlob(image);
5434 if (blob_info->data == (
unsigned char *) NULL)
5436 (void) DetachBlob(blob_info);
5437 return(MagickFalse);
5475 assert(custom_stream->signature == MagickCoreSignature);
5476 custom_stream->data=data;
5505 CustomStreamHandler reader)
5508 assert(custom_stream->signature == MagickCoreSignature);
5509 custom_stream->reader=reader;
5538 CustomStreamSeeker seeker)
5541 assert(custom_stream->signature == MagickCoreSignature);
5542 custom_stream->seeker=seeker;
5571 CustomStreamTeller teller)
5574 assert(custom_stream->signature == MagickCoreSignature);
5575 custom_stream->teller=teller;
5604 CustomStreamHandler writer)
5607 assert(custom_stream->signature == MagickCoreSignature);
5608 custom_stream->writer=writer;
5635static int SyncBlob(
const Image *image)
5638 *magick_restrict blob_info;
5643 assert(image != (
Image *) NULL);
5644 assert(image->signature == MagickCoreSignature);
5645 assert(image->blob != (
BlobInfo *) NULL);
5646 if (IsEventLogging() != MagickFalse)
5647 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5648 if (EOFBlob(image) != 0)
5650 blob_info=image->blob;
5652 switch (blob_info->type)
5654 case UndefinedStream:
5655 case StandardStream:
5660 status=fflush(blob_info->file_info.file);
5665#if defined(MAGICKCORE_ZLIB_DELEGATE)
5666 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5672#if defined(MAGICKCORE_BZLIB_DELEGATE)
5673 status=BZ2_bzflush(blob_info->file_info.bzfile);
5709MagickExport MagickOffsetType TellBlob(
const Image *image)
5712 *magick_restrict blob_info;
5717 assert(image != (
Image *) NULL);
5718 assert(image->signature == MagickCoreSignature);
5719 assert(image->blob != (
BlobInfo *) NULL);
5720 assert(image->blob->type != UndefinedStream);
5721 if (IsEventLogging() != MagickFalse)
5722 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5723 blob_info=image->blob;
5725 switch (blob_info->type)
5727 case UndefinedStream:
5728 case StandardStream:
5732 offset=ftell(blob_info->file_info.file);
5739#if defined(MAGICKCORE_ZLIB_DELEGATE)
5740 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5750 offset=blob_info->offset;
5755 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5756 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5788MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5790#if defined(MAGICKCORE_HAVE_MMAP)
5794 status=munmap(map,length);
5795 return(status == -1 ? MagickFalse : MagickTrue);
5799 return(MagickFalse);
5831MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
5835 *magick_restrict blob_info;
5849 assert(image != (
Image *) NULL);
5850 assert(image->signature == MagickCoreSignature);
5851 assert(image->blob != (
BlobInfo *) NULL);
5852 assert(image->blob->type != UndefinedStream);
5855 assert(data != (
const void *) NULL);
5856 blob_info=image->blob;
5858 p=(
const unsigned char *) data;
5859 q=(
unsigned char *) data;
5860 switch (blob_info->type)
5862 case UndefinedStream:
5864 case StandardStream:
5872 count=(ssize_t) fwrite((
const char *) data,1,length,
5873 blob_info->file_info.file);
5878 c=putc((
int) *p++,blob_info->file_info.file);
5886 c=putc((
int) *p++,blob_info->file_info.file);
5894 c=putc((
int) *p++,blob_info->file_info.file);
5902 c=putc((
int) *p++,blob_info->file_info.file);
5911 if ((count != (ssize_t) length) &&
5912 (ferror(blob_info->file_info.file) != 0))
5913 ThrowBlobException(blob_info);
5918#if defined(MAGICKCORE_ZLIB_DELEGATE)
5929 for (i=0; i < length; i+=(size_t) count)
5931 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5932 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5945 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5953 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5961 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5969 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5979 (void) gzerror(blob_info->file_info.gzfile,&status);
5980 if ((count != (ssize_t) length) && (status != Z_OK))
5981 ThrowBlobException(blob_info);
5987#if defined(MAGICKCORE_BZLIB_DELEGATE)
5994 for (i=0; i < length; i+=(size_t) count)
5996 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5997 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6007 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6008 if ((count != (ssize_t) length) && (status != BZ_OK))
6009 ThrowBlobException(blob_info);
6015 count=(ssize_t) blob_info->stream(image,data,length);
6023 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6024 if (extent >= blob_info->extent)
6026 if (blob_info->mapped != MagickFalse)
6028 blob_info->extent=extent+blob_info->quantum+length;
6029 blob_info->quantum<<=1;
6030 blob_info->data=(
unsigned char *) ResizeQuantumMemory(
6031 blob_info->data,blob_info->extent+1,
sizeof(*blob_info->data));
6032 (void) SyncBlob(image);
6033 if (blob_info->data == (
unsigned char *) NULL)
6035 (void) DetachBlob(blob_info);
6039 q=blob_info->data+blob_info->offset;
6040 (void) memcpy(q,p,length);
6041 blob_info->offset+=(MagickOffsetType) length;
6042 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6043 blob_info->length=(
size_t) blob_info->offset;
6044 count=(ssize_t) length;
6049 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6050 count=blob_info->custom_stream->writer((
unsigned char *) data,
6051 length,blob_info->custom_stream->data);
6083MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
6086 *magick_restrict blob_info;
6091 assert(image != (
Image *) NULL);
6092 assert(image->signature == MagickCoreSignature);
6093 assert(image->blob != (
BlobInfo *) NULL);
6094 assert(image->blob->type != UndefinedStream);
6095 blob_info=image->blob;
6097 switch (blob_info->type)
6099 case StandardStream:
6106 c=putc((
int) value,blob_info->file_info.file);
6109 if (ferror(blob_info->file_info.file) != 0)
6110 ThrowBlobException(blob_info);
6118 count=WriteBlobStream(image,1,&value);
6150MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
6161 quantum.unsigned_value=0U;
6162 quantum.float_value=value;
6163 return(WriteBlobLong(image,quantum.unsigned_value));
6191MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
6196 assert(image != (
Image *) NULL);
6197 assert(image->signature == MagickCoreSignature);
6198 if (image->endian == LSBEndian)
6200 buffer[0]=(
unsigned char) value;
6201 buffer[1]=(
unsigned char) (value >> 8);
6202 buffer[2]=(
unsigned char) (value >> 16);
6203 buffer[3]=(
unsigned char) (value >> 24);
6204 return(WriteBlobStream(image,4,buffer));
6206 buffer[0]=(
unsigned char) (value >> 24);
6207 buffer[1]=(
unsigned char) (value >> 16);
6208 buffer[2]=(
unsigned char) (value >> 8);
6209 buffer[3]=(
unsigned char) value;
6210 return(WriteBlobStream(image,4,buffer));
6238MagickExport ssize_t WriteBlobLongLong(
Image *image,
const MagickSizeType value)
6243 assert(image != (
Image *) NULL);
6244 assert(image->signature == MagickCoreSignature);
6245 if (image->endian == LSBEndian)
6247 buffer[0]=(
unsigned char) value;
6248 buffer[1]=(
unsigned char) (value >> 8);
6249 buffer[2]=(
unsigned char) (value >> 16);
6250 buffer[3]=(
unsigned char) (value >> 24);
6251 buffer[4]=(
unsigned char) (value >> 32);
6252 buffer[5]=(
unsigned char) (value >> 40);
6253 buffer[6]=(
unsigned char) (value >> 48);
6254 buffer[7]=(
unsigned char) (value >> 56);
6255 return(WriteBlobStream(image,8,buffer));
6257 buffer[0]=(
unsigned char) (value >> 56);
6258 buffer[1]=(
unsigned char) (value >> 48);
6259 buffer[2]=(
unsigned char) (value >> 40);
6260 buffer[3]=(
unsigned char) (value >> 32);
6261 buffer[4]=(
unsigned char) (value >> 24);
6262 buffer[5]=(
unsigned char) (value >> 16);
6263 buffer[6]=(
unsigned char) (value >> 8);
6264 buffer[7]=(
unsigned char) value;
6265 return(WriteBlobStream(image,8,buffer));
6293MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
6298 assert(image != (
Image *) NULL);
6299 assert(image->signature == MagickCoreSignature);
6300 if (image->endian == LSBEndian)
6302 buffer[0]=(
unsigned char) value;
6303 buffer[1]=(
unsigned char) (value >> 8);
6304 return(WriteBlobStream(image,2,buffer));
6306 buffer[0]=(
unsigned char) (value >> 8);
6307 buffer[1]=(
unsigned char) value;
6308 return(WriteBlobStream(image,2,buffer));
6336MagickExport ssize_t WriteBlobSignedLong(
Image *image,
const signed int value)
6350 assert(image != (
Image *) NULL);
6351 assert(image->signature == MagickCoreSignature);
6352 quantum.signed_value=value;
6353 if (image->endian == LSBEndian)
6355 buffer[0]=(
unsigned char) quantum.unsigned_value;
6356 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6357 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6358 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6359 return(WriteBlobStream(image,4,buffer));
6361 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6362 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6363 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6364 buffer[3]=(
unsigned char) quantum.unsigned_value;
6365 return(WriteBlobStream(image,4,buffer));
6393MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
6398 assert(image != (
Image *) NULL);
6399 assert(image->signature == MagickCoreSignature);
6400 buffer[0]=(
unsigned char) value;
6401 buffer[1]=(
unsigned char) (value >> 8);
6402 buffer[2]=(
unsigned char) (value >> 16);
6403 buffer[3]=(
unsigned char) (value >> 24);
6404 return(WriteBlobStream(image,4,buffer));
6432MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
6437 assert(image != (
Image *) NULL);
6438 assert(image->signature == MagickCoreSignature);
6439 buffer[0]=(
unsigned char) value;
6440 buffer[1]=(
unsigned char) (value >> 8);
6441 return(WriteBlobStream(image,2,buffer));
6469MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
6483 assert(image != (
Image *) NULL);
6484 assert(image->signature == MagickCoreSignature);
6485 quantum.signed_value=value;
6486 buffer[0]=(
unsigned char) quantum.unsigned_value;
6487 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6488 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6489 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6490 return(WriteBlobStream(image,4,buffer));
6518MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
6519 const signed short value)
6533 assert(image != (
Image *) NULL);
6534 assert(image->signature == MagickCoreSignature);
6535 quantum.signed_value=value;
6536 buffer[0]=(
unsigned char) quantum.unsigned_value;
6537 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6538 return(WriteBlobStream(image,2,buffer));
6566MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
6571 assert(image != (
Image *) NULL);
6572 assert(image->signature == MagickCoreSignature);
6573 buffer[0]=(
unsigned char) (value >> 24);
6574 buffer[1]=(
unsigned char) (value >> 16);
6575 buffer[2]=(
unsigned char) (value >> 8);
6576 buffer[3]=(
unsigned char) value;
6577 return(WriteBlobStream(image,4,buffer));
6605MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
6606 const signed short value)
6620 assert(image != (
Image *) NULL);
6621 assert(image->signature == MagickCoreSignature);
6622 quantum.signed_value=value;
6623 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6624 buffer[1]=(
unsigned char) quantum.unsigned_value;
6625 return(WriteBlobStream(image,2,buffer));
6653MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
6658 assert(image != (
Image *) NULL);
6659 assert(image->signature == MagickCoreSignature);
6660 buffer[0]=(
unsigned char) (value >> 8);
6661 buffer[1]=(
unsigned char) value;
6662 return(WriteBlobStream(image,2,buffer));
6690MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
6692 assert(image != (
Image *) NULL);
6693 assert(image->signature == MagickCoreSignature);
6694 assert(
string != (
const char *) NULL);
6695 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));