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
96#if defined(MAGICKCORE_WINDOWS_SUPPORT)
101# define MAGICKCORE_HAVE_MMAP 1
102# define mmap(address,length,protection,access,file,offset) \
103 NTMapMemory(address,length,protection,access,file,offset)
106# define munmap(address,length) NTUnmapMemory(address,length)
109# define pclose _pclose
124#if defined(MAGICKCORE_ZLIB_DELEGATE)
129#if defined(MAGICKCORE_BZLIB_DELEGATE)
221 SyncBlob(
const Image *);
251 magick_unreferenced(exception);
253 sizeof(*custom_stream));
254 (void) memset(custom_stream,0,
sizeof(*custom_stream));
255 custom_stream->signature=MagickCoreSignature;
256 return(custom_stream);
286MagickExport
void AttachBlob(
BlobInfo *blob_info,
const void *blob,
289 assert(blob_info != (
BlobInfo *) NULL);
290 if (IsEventLogging() != MagickFalse)
291 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
292 blob_info->length=length;
293 blob_info->extent=length;
294 blob_info->quantum=(size_t) MagickMaxBlobExtent;
296 blob_info->type=BlobStream;
297 blob_info->file_info.file=(FILE *) NULL;
298 blob_info->data=(
unsigned char *) blob;
299 blob_info->mapped=MagickFalse;
327MagickExport
void AttachCustomStream(
BlobInfo *blob_info,
330 assert(blob_info != (
BlobInfo *) NULL);
332 assert(custom_stream->signature == MagickCoreSignature);
333 if (IsEventLogging() != MagickFalse)
334 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
335 blob_info->type=CustomStream;
336 blob_info->custom_stream=custom_stream;
369MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
381 assert(filename != (
const char *) NULL);
382 assert(blob != (
const void *) NULL);
383 if (IsEventLogging() != MagickFalse)
384 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
385 if (*filename ==
'\0')
386 file=AcquireUniqueFileResource(filename);
388 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
391 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
394 for (i=0; i < length; i+=(size_t) count)
396 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
397 MagickMaxBufferExtent));
405 file=close_utf8(file);
406 if ((file == -1) || (i < length))
408 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
445MagickExport
Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
461 assert(image_info != (
ImageInfo *) NULL);
462 assert(image_info->signature == MagickCoreSignature);
464 if (IsEventLogging() != MagickFalse)
465 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
466 image_info->filename);
467 if ((blob == (
const void *) NULL) || (length == 0))
469 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
470 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
471 return((
Image *) NULL);
473 blob_info=CloneImageInfo(image_info);
474 blob_info->blob=(
void *) blob;
475 blob_info->length=length;
476 if (*blob_info->magick ==
'\0')
477 (void) SetImageInfo(blob_info,0,exception);
478 magick_info=GetMagickInfo(blob_info->magick,exception);
481 (void) ThrowMagickException(exception,GetMagickModule(),
482 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
484 blob_info=DestroyImageInfo(blob_info);
485 return((
Image *) NULL);
487 if (GetMagickBlobSupport(magick_info) != MagickFalse)
490 filename[MagickPathExtent];
495 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
496 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
497 blob_info->magick,filename);
498 image=ReadImage(blob_info,exception);
499 if (image != (
Image *) NULL)
500 (void) DetachBlob(image->blob);
501 blob_info=DestroyImageInfo(blob_info);
507 blob_info->blob=(
void *) NULL;
509 *blob_info->filename=
'\0';
510 status=BlobToFile(blob_info->filename,blob,length,exception);
511 if (status == MagickFalse)
513 (void) RelinquishUniqueFileResource(blob_info->filename);
514 blob_info=DestroyImageInfo(blob_info);
515 return((
Image *) NULL);
517 clone_info=CloneImageInfo(blob_info);
518 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
519 blob_info->magick,blob_info->filename);
520 image=ReadImage(clone_info,exception);
521 if (image != (
Image *) NULL)
529 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
531 (void) CopyMagickString(images->filename,image_info->filename,
533 (void) CopyMagickString(images->magick_filename,image_info->filename,
535 (void) CopyMagickString(images->magick,magick_info->name,
537 images=GetNextImageInList(images);
540 clone_info=DestroyImageInfo(clone_info);
541 (void) RelinquishUniqueFileResource(blob_info->filename);
542 blob_info=DestroyImageInfo(blob_info);
577 clone_info=(
BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
578 GetBlobInfo(clone_info);
581 semaphore=clone_info->semaphore;
582 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
583 if (blob_info->mapped != MagickFalse)
584 (void) AcquireMagickResource(MapResource,blob_info->length);
585 clone_info->semaphore=semaphore;
586 LockSemaphoreInfo(clone_info->semaphore);
587 clone_info->reference_count=1;
588 UnlockSemaphoreInfo(clone_info->semaphore);
615static inline void ThrowBlobException(
BlobInfo *blob_info)
617 if ((blob_info->status == 0) && (errno != 0))
618 blob_info->error_number=errno;
619 blob_info->status=(-1);
622MagickExport MagickBooleanType CloseBlob(
Image *image)
625 *magick_restrict blob_info;
633 assert(image != (
Image *) NULL);
634 assert(image->signature == MagickCoreSignature);
635 if (IsEventLogging() != MagickFalse)
636 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
637 blob_info=image->blob;
638 if ((blob_info == (
BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
640 (void) SyncBlob(image);
641 status=blob_info->status;
642 switch (blob_info->type)
644 case UndefinedStream:
650 if (blob_info->synchronize != MagickFalse)
652 status=fflush(blob_info->file_info.file);
654 ThrowBlobException(blob_info);
655 status=fsync(fileno(blob_info->file_info.file));
657 ThrowBlobException(blob_info);
659 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
660 ThrowBlobException(blob_info);
665#if defined(MAGICKCORE_ZLIB_DELEGATE)
667 (void) gzerror(blob_info->file_info.gzfile,&status);
669 ThrowBlobException(blob_info);
675#if defined(MAGICKCORE_BZLIB_DELEGATE)
677 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
679 ThrowBlobException(blob_info);
687 if (blob_info->file_info.file != (FILE *) NULL)
689 if (blob_info->synchronize != MagickFalse)
691 status=fflush(blob_info->file_info.file);
693 ThrowBlobException(blob_info);
694 status=fsync(fileno(blob_info->file_info.file));
696 ThrowBlobException(blob_info);
698 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
699 ThrowBlobException(blob_info);
706 blob_info->size=GetBlobSize(image);
707 image->extent=blob_info->size;
708 blob_info->eof=MagickFalse;
710 blob_info->mode=UndefinedBlobMode;
711 if (blob_info->exempt != MagickFalse)
713 blob_info->type=UndefinedStream;
714 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
716 switch (blob_info->type)
718 case UndefinedStream:
723 if (blob_info->file_info.file != (FILE *) NULL)
725 status=fclose(blob_info->file_info.file);
727 ThrowBlobException(blob_info);
733#if defined(MAGICKCORE_HAVE_PCLOSE)
734 status=pclose(blob_info->file_info.file);
736 ThrowBlobException(blob_info);
742#if defined(MAGICKCORE_ZLIB_DELEGATE)
743 status=gzclose(blob_info->file_info.gzfile);
745 ThrowBlobException(blob_info);
751#if defined(MAGICKCORE_BZLIB_DELEGATE)
752 BZ2_bzclose(blob_info->file_info.bzfile);
760 if (blob_info->file_info.file != (FILE *) NULL)
762 status=fclose(blob_info->file_info.file);
764 ThrowBlobException(blob_info);
771 (void) DetachBlob(blob_info);
772 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
801MagickExport
Image *CustomStreamToImage(
const ImageInfo *image_info,
813 assert(image_info != (
ImageInfo *) NULL);
814 assert(image_info->signature == MagickCoreSignature);
816 assert(image_info->custom_stream->signature == MagickCoreSignature);
817 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
819 if (IsEventLogging() != MagickFalse)
820 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
821 image_info->filename);
822 blob_info=CloneImageInfo(image_info);
823 if (*blob_info->magick ==
'\0')
824 (void) SetImageInfo(blob_info,0,exception);
825 magick_info=GetMagickInfo(blob_info->magick,exception);
828 (void) ThrowMagickException(exception,GetMagickModule(),
829 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
831 blob_info=DestroyImageInfo(blob_info);
832 return((
Image *) NULL);
834 image=(
Image *) NULL;
835 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
836 (*blob_info->filename !=
'\0'))
839 filename[MagickPathExtent];
845 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
846 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
847 blob_info->magick,filename);
848 image=ReadImage(blob_info,exception);
853 unique[MagickPathExtent];
868 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
870 if (blob == (
unsigned char *) NULL)
872 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
873 image_info->filename);
874 blob_info=DestroyImageInfo(blob_info);
875 return((
Image *) NULL);
877 file=AcquireUniqueFileResource(unique);
880 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
881 image_info->filename);
882 blob=(
unsigned char *) RelinquishMagickMemory(blob);
883 blob_info=DestroyImageInfo(blob_info);
884 return((
Image *) NULL);
886 clone_info=CloneImageInfo(blob_info);
887 blob_info->file=fdopen(file,
"wb+");
888 if (blob_info->file != (FILE *) NULL)
893 count=(ssize_t) MagickMaxBufferExtent;
894 while (count == (ssize_t) MagickMaxBufferExtent)
896 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
897 image_info->custom_stream->data);
898 count=(ssize_t) write(file,(
const char *) blob,(size_t) count);
900 (void) fclose(blob_info->file);
901 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
902 "%s:%s",blob_info->magick,unique);
903 image=ReadImage(clone_info,exception);
904 if (image != (
Image *) NULL)
912 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
914 (void) CopyMagickString(images->filename,image_info->filename,
916 (void) CopyMagickString(images->magick_filename,
917 image_info->filename,MagickPathExtent);
918 (void) CopyMagickString(images->magick,magick_info->name,
920 images=GetNextImageInList(images);
924 clone_info=DestroyImageInfo(clone_info);
925 blob=(
unsigned char *) RelinquishMagickMemory(blob);
926 (void) RelinquishUniqueFileResource(unique);
928 blob_info=DestroyImageInfo(blob_info);
929 if (image != (
Image *) NULL)
930 if (CloseBlob(image) == MagickFalse)
931 image=DestroyImageList(image);
957MagickExport
void DestroyBlob(
Image *image)
960 *magick_restrict blob_info;
965 assert(image != (
Image *) NULL);
966 assert(image->signature == MagickCoreSignature);
967 assert(image->blob != (
BlobInfo *) NULL);
968 assert(image->blob->signature == MagickCoreSignature);
969 if (IsEventLogging() != MagickFalse)
970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
971 blob_info=image->blob;
973 LockSemaphoreInfo(blob_info->semaphore);
974 blob_info->reference_count--;
975 assert(blob_info->reference_count >= 0);
976 if (blob_info->reference_count == 0)
978 UnlockSemaphoreInfo(blob_info->semaphore);
979 if (destroy == MagickFalse)
984 (void) CloseBlob(image);
985 if (blob_info->mapped != MagickFalse)
987 (void) UnmapBlob(blob_info->data,blob_info->length);
988 RelinquishMagickResource(MapResource,blob_info->length);
991 RelinquishSemaphoreInfo(&blob_info->semaphore);
992 blob_info->signature=(~MagickCoreSignature);
993 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
1023 assert(custom_stream->signature == MagickCoreSignature);
1024 if (IsEventLogging() != MagickFalse)
1025 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1026 custom_stream->signature=(~MagickCoreSignature);
1028 return(custom_stream);
1053MagickExport
void *DetachBlob(
BlobInfo *blob_info)
1058 assert(blob_info != (
BlobInfo *) NULL);
1059 if (IsEventLogging() != MagickFalse)
1060 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1061 if (blob_info->mapped != MagickFalse)
1063 (void) UnmapBlob(blob_info->data,blob_info->length);
1064 blob_info->data=NULL;
1065 RelinquishMagickResource(MapResource,blob_info->length);
1067 blob_info->mapped=MagickFalse;
1068 blob_info->length=0;
1073 blob_info->offset=0;
1074 blob_info->mode=UndefinedBlobMode;
1075 blob_info->eof=MagickFalse;
1077 blob_info->exempt=MagickFalse;
1078 blob_info->type=UndefinedStream;
1079 blob_info->file_info.file=(FILE *) NULL;
1080 data=blob_info->data;
1081 blob_info->data=(
unsigned char *) NULL;
1082 blob_info->stream=(StreamHandler) NULL;
1111MagickExport
void DisassociateBlob(
Image *image)
1114 *magick_restrict blob_info,
1120 assert(image != (
Image *) NULL);
1121 assert(image->signature == MagickCoreSignature);
1122 assert(image->blob != (
BlobInfo *) NULL);
1123 assert(image->blob->signature == MagickCoreSignature);
1124 if (IsEventLogging() != MagickFalse)
1125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1126 blob_info=image->blob;
1128 LockSemaphoreInfo(blob_info->semaphore);
1129 assert(blob_info->reference_count >= 0);
1130 if (blob_info->reference_count > 1)
1132 UnlockSemaphoreInfo(blob_info->semaphore);
1133 if (clone == MagickFalse)
1135 clone_info=CloneBlobInfo(blob_info);
1137 image->blob=clone_info;
1165MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
1166 const MagickSizeType length)
1178 buffer[MagickMinBufferExtent >> 1];
1180 assert(image != (
Image *) NULL);
1181 assert(image->signature == MagickCoreSignature);
1182 if (length != (MagickSizeType) ((MagickOffsetType) length))
1183 return(MagickFalse);
1185 for (i=0; i < length; i+=(MagickSizeType) count)
1187 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1188 (void) ReadBlobStream(image,quantum,buffer,&count);
1196 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1223MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
1225 assert(image != (
Image *) NULL);
1226 assert(image->signature == MagickCoreSignature);
1227 assert(duplicate != (
Image *) NULL);
1228 assert(duplicate->signature == MagickCoreSignature);
1229 if (IsEventLogging() != MagickFalse)
1230 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1232 image->blob=ReferenceBlob(duplicate->blob);
1258MagickExport
int EOFBlob(
const Image *image)
1261 *magick_restrict blob_info;
1263 assert(image != (
Image *) NULL);
1264 assert(image->signature == MagickCoreSignature);
1265 assert(image->blob != (
BlobInfo *) NULL);
1266 assert(image->blob->type != UndefinedStream);
1267 if (IsEventLogging() != MagickFalse)
1268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1269 blob_info=image->blob;
1270 switch (blob_info->type)
1272 case UndefinedStream:
1273 case StandardStream:
1278 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1284#if defined(MAGICKCORE_ZLIB_DELEGATE)
1285 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1292#if defined(MAGICKCORE_BZLIB_DELEGATE)
1297 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1298 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1304 blob_info->eof=MagickFalse;
1312 return((
int) blob_info->eof);
1338MagickExport
int ErrorBlob(
const Image *image)
1341 *magick_restrict blob_info;
1343 assert(image != (
Image *) NULL);
1344 assert(image->signature == MagickCoreSignature);
1345 assert(image->blob != (
BlobInfo *) NULL);
1346 assert(image->blob->type != UndefinedStream);
1347 if (IsEventLogging() != MagickFalse)
1348 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1349 blob_info=image->blob;
1350 switch (blob_info->type)
1352 case UndefinedStream:
1353 case StandardStream:
1358 blob_info->error=ferror(blob_info->file_info.file);
1363#if defined(MAGICKCORE_ZLIB_DELEGATE)
1364 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1370#if defined(MAGICKCORE_BZLIB_DELEGATE)
1371 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1385 return(blob_info->error);
1423MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1450 assert(filename != (
const char *) NULL);
1452 assert(exception->signature == MagickCoreSignature);
1453 if (IsEventLogging() != MagickFalse)
1454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1456 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1457 if (status == MagickFalse)
1460 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1461 "NotAuthorized",
"`%s'",filename);
1465 if (LocaleCompare(filename,
"-") != 0)
1468 flags = O_RDONLY | O_BINARY;
1470 status=GetPathAttributes(filename,&attributes);
1471 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1473 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1476#if defined(O_NOFOLLOW)
1477 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1478 if (status == MagickFalse)
1481 file=open_utf8(filename,flags,0);
1485 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1488 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1489 if (status == MagickFalse)
1491 file=close_utf8(file)-1;
1493 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1494 "NotAuthorized",
"`%s'",filename);
1497 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1499 if ((file == fileno(stdin)) || (offset < 0) ||
1500 (offset != (MagickOffsetType) ((ssize_t) offset)))
1511 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1512 quantum=(size_t) MagickMaxBufferExtent;
1513 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1514 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1515 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1516 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1518 count=read(file,blob+i,quantum);
1525 if (~i < ((
size_t) count+quantum+1))
1527 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1530 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1531 quantum+1,
sizeof(*blob));
1532 if ((i+(
size_t) count) >= extent)
1535 if (LocaleCompare(filename,
"-") != 0)
1536 file=close_utf8(file);
1537 if (blob == (
unsigned char *) NULL)
1539 (void) ThrowMagickException(exception,GetMagickModule(),
1540 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1545 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1546 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1549 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1553 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1554 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1555 blob=(
unsigned char *) NULL;
1556 if (~(*length) >= (MagickPathExtent-1))
1557 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1559 if (blob == (
unsigned char *) NULL)
1561 file=close_utf8(file);
1562 (void) ThrowMagickException(exception,GetMagickModule(),
1563 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1566 map=MapBlob(file,ReadMode,0,*length);
1567 if (map != (
unsigned char *) NULL)
1569 (void) memcpy(blob,map,*length);
1570 (void) UnmapBlob(map,*length);
1574 (void) lseek(file,0,SEEK_SET);
1575 for (i=0; i < *length; i+=(size_t) count)
1577 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1578 MagickMaxBufferExtent));
1588 file=close_utf8(file)-1;
1589 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1590 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1595 if (LocaleCompare(filename,
"-") != 0)
1596 file=close_utf8(file);
1599 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1600 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1629static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1630 const void *magick_restrict data)
1633 *magick_restrict blob_info;
1641 assert(image->blob != (
BlobInfo *) NULL);
1642 assert(image->blob->type != UndefinedStream);
1643 assert(data != NULL);
1644 blob_info=image->blob;
1645 if (blob_info->type != BlobStream)
1646 return(WriteBlob(image,length,(
const unsigned char *) data));
1647 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1648 if (extent >= blob_info->extent)
1650 extent+=blob_info->quantum+length;
1651 blob_info->quantum<<=1;
1652 if (SetBlobExtent(image,extent) == MagickFalse)
1655 q=blob_info->data+blob_info->offset;
1656 (void) memcpy(q,data,length);
1657 blob_info->offset+=(MagickOffsetType) length;
1658 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1659 blob_info->length=(
size_t) blob_info->offset;
1660 return((ssize_t) length);
1663MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename,
1685 assert(image != (
const Image *) NULL);
1686 assert(image->signature == MagickCoreSignature);
1687 assert(filename != (
const char *) NULL);
1688 if (IsEventLogging() != MagickFalse)
1689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1690 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1691 if (status == MagickFalse)
1694 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1695 "NotAuthorized",
"`%s'",filename);
1696 return(MagickFalse);
1699 if (LocaleCompare(filename,
"-") != 0)
1702 flags = O_RDONLY | O_BINARY;
1704#if defined(O_NOFOLLOW)
1705 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1706 if (status == MagickFalse)
1709 file=open_utf8(filename,flags,0);
1713 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1714 return(MagickFalse);
1716 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1717 if (status == MagickFalse)
1719 file=close_utf8(file);
1721 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1722 "NotAuthorized",
"`%s'",filename);
1723 return(MagickFalse);
1725 quantum=(size_t) MagickMaxBufferExtent;
1726 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1727 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1728 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1729 if (blob == (
unsigned char *) NULL)
1731 file=close_utf8(file);
1732 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1734 return(MagickFalse);
1738 count=read(file,blob,quantum);
1745 length=(size_t) count;
1746 count=WriteBlobStream(image,length,blob);
1747 if (count != (ssize_t) length)
1749 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1753 file=close_utf8(file);
1755 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1756 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1783MagickExport MagickBooleanType GetBlobError(
const Image *image)
1785 assert(image != (
const Image *) NULL);
1786 assert(image->signature == MagickCoreSignature);
1787 if (IsEventLogging() != MagickFalse)
1788 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1789 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1790 errno=image->blob->error_number;
1791 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1816MagickExport FILE *GetBlobFileHandle(
const Image *image)
1818 assert(image != (
const Image *) NULL);
1819 assert(image->signature == MagickCoreSignature);
1820 return(image->blob->file_info.file);
1845MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1847 assert(blob_info != (
BlobInfo *) NULL);
1848 (void) memset(blob_info,0,
sizeof(*blob_info));
1849 blob_info->type=UndefinedStream;
1850 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1851 blob_info->properties.st_mtime=GetMagickTime();
1852 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1853 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1854 blob_info->reference_count=1;
1855 blob_info->semaphore=AcquireSemaphoreInfo();
1856 blob_info->signature=MagickCoreSignature;
1881MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1883 assert(image != (
Image *) NULL);
1884 assert(image->signature == MagickCoreSignature);
1885 if (IsEventLogging() != MagickFalse)
1886 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1887 return(&image->blob->properties);
1913MagickExport MagickSizeType GetBlobSize(
const Image *image)
1916 *magick_restrict blob_info;
1921 assert(image != (
Image *) NULL);
1922 assert(image->signature == MagickCoreSignature);
1923 assert(image->blob != (
BlobInfo *) NULL);
1924 if (IsEventLogging() != MagickFalse)
1925 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1926 blob_info=image->blob;
1928 switch (blob_info->type)
1930 case UndefinedStream:
1931 case StandardStream:
1933 extent=blob_info->size;
1941 extent=(MagickSizeType) blob_info->properties.st_size;
1943 extent=blob_info->size;
1944 file_descriptor=fileno(blob_info->file_info.file);
1945 if (file_descriptor == -1)
1947 if (fstat(file_descriptor,&blob_info->properties) == 0)
1948 extent=(MagickSizeType) blob_info->properties.st_size;
1953 extent=blob_info->size;
1962 status=GetPathAttributes(image->filename,&blob_info->properties);
1963 if (status != MagickFalse)
1964 extent=(MagickSizeType) blob_info->properties.st_size;
1971 extent=(MagickSizeType) blob_info->length;
1976 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1977 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1982 offset=blob_info->custom_stream->teller(
1983 blob_info->custom_stream->data);
1984 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1985 blob_info->custom_stream->data);
1986 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1987 blob_info->custom_stream->data);
2017MagickExport
void *GetBlobStreamData(
const Image *image)
2019 assert(image != (
const Image *) NULL);
2020 assert(image->signature == MagickCoreSignature);
2021 return(image->blob->data);
2046MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2048 assert(image != (
const Image *) NULL);
2049 assert(image->signature == MagickCoreSignature);
2050 if (IsEventLogging() != MagickFalse)
2051 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2052 return(image->blob->stream);
2088MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2103 assert(image_info != (
const ImageInfo *) NULL);
2104 assert(image_info->signature == MagickCoreSignature);
2105 assert(image != (
Image *) NULL);
2106 assert(image->signature == MagickCoreSignature);
2108 assert(exception->signature == MagickCoreSignature);
2109 if (IsEventLogging() != MagickFalse)
2110 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2111 image_info->filename);
2113 blob=(
unsigned char *) NULL;
2114 blob_info=CloneImageInfo(image_info);
2115 blob_info->adjoin=MagickFalse;
2116 (void) SetImageInfo(blob_info,1,exception);
2117 if (*blob_info->magick !=
'\0')
2118 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2119 magick_info=GetMagickInfo(image->magick,exception);
2120 if (magick_info == (
const MagickInfo *) NULL)
2122 (void) ThrowMagickException(exception,GetMagickModule(),
2123 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2125 blob_info=DestroyImageInfo(blob_info);
2128 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2129 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2134 blob_info->length=0;
2135 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2136 sizeof(
unsigned char));
2137 if (blob_info->blob == NULL)
2138 (void) ThrowMagickException(exception,GetMagickModule(),
2139 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2142 (void) CloseBlob(image);
2143 image->blob->exempt=MagickTrue;
2144 image->blob->extent=0;
2145 *image->filename=
'\0';
2146 status=WriteImage(blob_info,image,exception);
2147 *length=image->blob->length;
2148 blob=DetachBlob(image->blob);
2149 if (blob != (
void *) NULL)
2151 if (status == MagickFalse)
2152 blob=RelinquishMagickMemory(blob);
2154 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2156 else if ((status == MagickFalse) && (image->blob->extent == 0))
2157 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2163 unique[MagickPathExtent];
2171 file=AcquireUniqueFileResource(unique);
2174 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2175 image_info->filename);
2179 blob_info->file=fdopen(file,
"wb");
2180 if (blob_info->file != (FILE *) NULL)
2182 (void) FormatLocaleString(image->filename,MagickPathExtent,
2183 "%s:%s",image->magick,unique);
2184 status=WriteImage(blob_info,image,exception);
2185 (void) fclose(blob_info->file);
2186 if (status != MagickFalse)
2187 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2189 (void) RelinquishUniqueFileResource(unique);
2192 blob_info=DestroyImageInfo(blob_info);
2224MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,
Image *image,
2237 assert(image_info != (
const ImageInfo *) NULL);
2238 assert(image_info->signature == MagickCoreSignature);
2239 assert(image != (
Image *) NULL);
2240 assert(image->signature == MagickCoreSignature);
2242 assert(image_info->custom_stream->signature == MagickCoreSignature);
2243 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2245 if (IsEventLogging() != MagickFalse)
2246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2247 image_info->filename);
2248 clone_info=CloneImageInfo(image_info);
2249 clone_info->adjoin=MagickFalse;
2250 (void) SetImageInfo(clone_info,1,exception);
2251 if (*clone_info->magick !=
'\0')
2252 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2253 magick_info=GetMagickInfo(image->magick,exception);
2254 if (magick_info == (
const MagickInfo *) NULL)
2256 (void) ThrowMagickException(exception,GetMagickModule(),
2257 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2259 clone_info=DestroyImageInfo(clone_info);
2262 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2263 blob_support=GetMagickBlobSupport(magick_info);
2264 if ((blob_support != MagickFalse) &&
2265 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2267 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2268 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2269 blob_support=MagickFalse;
2271 if (blob_support != MagickFalse)
2276 (void) CloseBlob(image);
2277 *image->filename=
'\0';
2278 (void) WriteImage(clone_info,image,exception);
2283 unique[MagickPathExtent];
2295 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2297 if (blob == (
unsigned char *) NULL)
2299 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2300 image_info->filename);
2301 clone_info=DestroyImageInfo(clone_info);
2304 file=AcquireUniqueFileResource(unique);
2307 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2308 image_info->filename);
2309 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2310 clone_info=DestroyImageInfo(clone_info);
2313 clone_info->file=fdopen(file,
"wb+");
2314 if (clone_info->file != (FILE *) NULL)
2319 (void) FormatLocaleString(image->filename,MagickPathExtent,
2320 "%s:%s",image->magick,unique);
2321 status=WriteImage(clone_info,image,exception);
2322 if (status != MagickFalse)
2324 (void) fseek(clone_info->file,0,SEEK_SET);
2325 count=(ssize_t) MagickMaxBufferExtent;
2326 while (count == (ssize_t) MagickMaxBufferExtent)
2328 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2330 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2331 image_info->custom_stream->data);
2334 (void) fclose(clone_info->file);
2336 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2337 (void) RelinquishUniqueFileResource(unique);
2339 clone_info=DestroyImageInfo(clone_info);
2370MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
2395 assert(image != (
Image *) NULL);
2396 assert(image->signature == MagickCoreSignature);
2397 assert(image->blob != (
BlobInfo *) NULL);
2398 assert(image->blob->type != UndefinedStream);
2399 assert(filename != (
const char *) NULL);
2400 if (IsEventLogging() != MagickFalse)
2401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2402 if (*filename ==
'\0')
2403 file=AcquireUniqueFileResource(filename);
2405 if (LocaleCompare(filename,
"-") == 0)
2406 file=fileno(stdout);
2408 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
2411 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2412 return(MagickFalse);
2414 quantum=(size_t) MagickMaxBufferExtent;
2415 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2416 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2417 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2418 if (buffer == (
unsigned char *) NULL)
2420 file=close_utf8(file)-1;
2421 (void) ThrowMagickException(exception,GetMagickModule(),
2422 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2423 return(MagickFalse);
2426 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2427 for (i=0; count > 0; )
2429 length=(size_t) count;
2430 for (i=0; i < length; i+=(size_t) count)
2432 count=write(file,p+i,(
size_t) (length-i));
2442 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2444 if (LocaleCompare(filename,
"-") != 0)
2445 file=close_utf8(file);
2446 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2447 if ((file == -1) || (i < length))
2450 file=close_utf8(file);
2451 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2452 return(MagickFalse);
2492MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,
Image *images,
2507 assert(image_info != (
const ImageInfo *) NULL);
2508 assert(image_info->signature == MagickCoreSignature);
2509 assert(images != (
Image *) NULL);
2510 assert(images->signature == MagickCoreSignature);
2512 if (IsEventLogging() != MagickFalse)
2513 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2514 image_info->filename);
2516 blob=(
unsigned char *) NULL;
2517 blob_info=CloneImageInfo(image_info);
2518 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2520 if (*blob_info->magick !=
'\0')
2521 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2522 magick_info=GetMagickInfo(images->magick,exception);
2523 if (magick_info == (
const MagickInfo *) NULL)
2525 (void) ThrowMagickException(exception,GetMagickModule(),
2526 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2528 blob_info=DestroyImageInfo(blob_info);
2531 if (GetMagickAdjoin(magick_info) == MagickFalse)
2533 blob_info=DestroyImageInfo(blob_info);
2534 return(ImageToBlob(image_info,images,length,exception));
2536 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2537 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2542 blob_info->length=0;
2543 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2544 sizeof(
unsigned char));
2545 if (blob_info->blob == (
void *) NULL)
2546 (void) ThrowMagickException(exception,GetMagickModule(),
2547 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2550 (void) CloseBlob(images);
2551 images->blob->exempt=MagickTrue;
2552 images->blob->extent=0;
2553 *images->filename=
'\0';
2554 status=WriteImages(blob_info,images,images->filename,exception);
2555 *length=images->blob->length;
2556 blob=DetachBlob(images->blob);
2557 if (blob != (
void *) NULL)
2559 if (status == MagickFalse)
2560 blob=RelinquishMagickMemory(blob);
2562 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2564 else if ((status == MagickFalse) && (images->blob->extent == 0))
2565 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2571 filename[MagickPathExtent],
2572 unique[MagickPathExtent];
2580 file=AcquireUniqueFileResource(unique);
2583 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2584 image_info->filename);
2588 blob_info->file=fdopen(file,
"wb");
2589 if (blob_info->file != (FILE *) NULL)
2591 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2592 images->magick,unique);
2593 status=WriteImages(blob_info,images,filename,exception);
2594 (void) fclose(blob_info->file);
2595 if (status != MagickFalse)
2596 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2598 (void) RelinquishUniqueFileResource(unique);
2601 blob_info=DestroyImageInfo(blob_info);
2633MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2646 assert(image_info != (
const ImageInfo *) NULL);
2647 assert(image_info->signature == MagickCoreSignature);
2648 assert(images != (
Image *) NULL);
2649 assert(images->signature == MagickCoreSignature);
2651 assert(image_info->custom_stream->signature == MagickCoreSignature);
2652 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2654 if (IsEventLogging() != MagickFalse)
2655 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2656 image_info->filename);
2657 clone_info=CloneImageInfo(image_info);
2658 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2660 if (*clone_info->magick !=
'\0')
2661 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2662 magick_info=GetMagickInfo(images->magick,exception);
2663 if (magick_info == (
const MagickInfo *) NULL)
2665 (void) ThrowMagickException(exception,GetMagickModule(),
2666 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2668 clone_info=DestroyImageInfo(clone_info);
2671 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2672 blob_support=GetMagickBlobSupport(magick_info);
2673 if ((blob_support != MagickFalse) &&
2674 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2676 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2677 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2678 blob_support=MagickFalse;
2680 if (blob_support != MagickFalse)
2685 (void) CloseBlob(images);
2686 *images->filename=
'\0';
2687 (void) WriteImages(clone_info,images,images->filename,exception);
2692 filename[MagickPathExtent],
2693 unique[MagickPathExtent];
2705 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2707 if (blob == (
unsigned char *) NULL)
2709 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2710 image_info->filename);
2711 clone_info=DestroyImageInfo(clone_info);
2714 file=AcquireUniqueFileResource(unique);
2717 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2718 image_info->filename);
2719 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2720 clone_info=DestroyImageInfo(clone_info);
2723 clone_info->file=fdopen(file,
"wb+");
2724 if (clone_info->file != (FILE *) NULL)
2729 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2730 images->magick,unique);
2731 status=WriteImages(clone_info,images,filename,exception);
2732 if (status != MagickFalse)
2734 (void) fseek(clone_info->file,0,SEEK_SET);
2735 count=(ssize_t) MagickMaxBufferExtent;
2736 while (count == (ssize_t) MagickMaxBufferExtent)
2738 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2740 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2741 image_info->custom_stream->data);
2744 (void) fclose(clone_info->file);
2746 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2747 (void) RelinquishUniqueFileResource(unique);
2749 clone_info=DestroyImageInfo(clone_info);
2785MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2789 filename[MagickPathExtent];
2818 assert(image_info != (
ImageInfo *) NULL);
2819 assert(image_info->signature == MagickCoreSignature);
2820 assert(image != (
Image *) NULL);
2821 assert(image->signature == MagickCoreSignature);
2822 assert(inject_image != (
Image *) NULL);
2823 assert(inject_image->signature == MagickCoreSignature);
2825 if (IsEventLogging() != MagickFalse)
2826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2827 unique_file=(FILE *) NULL;
2828 file=AcquireUniqueFileResource(filename);
2830 unique_file=fdopen(file,
"wb");
2831 if ((file == -1) || (unique_file == (FILE *) NULL))
2833 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2834 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2836 return(MagickFalse);
2838 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2839 if (byte_image == (
Image *) NULL)
2841 (void) fclose(unique_file);
2842 (void) RelinquishUniqueFileResource(filename);
2843 return(MagickFalse);
2845 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2847 DestroyBlob(byte_image);
2848 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2849 write_info=CloneImageInfo(image_info);
2850 SetImageInfoFile(write_info,unique_file);
2851 status=WriteImage(write_info,byte_image,exception);
2852 write_info=DestroyImageInfo(write_info);
2853 byte_image=DestroyImage(byte_image);
2854 (void) fclose(unique_file);
2855 if (status == MagickFalse)
2857 (void) RelinquishUniqueFileResource(filename);
2858 return(MagickFalse);
2863 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2866 (void) RelinquishUniqueFileResource(filename);
2867 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2868 image_info->filename);
2869 return(MagickFalse);
2871 quantum=(size_t) MagickMaxBufferExtent;
2872 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2873 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2874 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2875 if (buffer == (
unsigned char *) NULL)
2877 (void) RelinquishUniqueFileResource(filename);
2878 file=close_utf8(file);
2879 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2884 ssize_t count = read(file,buffer,quantum);
2891 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2894 file=close_utf8(file);
2896 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2897 (void) RelinquishUniqueFileResource(filename);
2898 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2924MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2926 assert(image != (
const Image *) NULL);
2927 assert(image->signature == MagickCoreSignature);
2928 if (IsEventLogging() != MagickFalse)
2929 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2930 return(image->blob->exempt);
2955MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2958 *magick_restrict blob_info;
2960 assert(image != (
const Image *) NULL);
2961 assert(image->signature == MagickCoreSignature);
2962 if (IsEventLogging() != MagickFalse)
2963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2964 blob_info=image->blob;
2965 switch (blob_info->type)
2974 if (blob_info->file_info.file == (FILE *) NULL)
2975 return(MagickFalse);
2976 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2977 return(status == -1 ? MagickFalse : MagickTrue);
2981#if defined(MAGICKCORE_ZLIB_DELEGATE)
2985 if (blob_info->file_info.gzfile == (gzFile) NULL)
2986 return(MagickFalse);
2987 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2988 return(offset < 0 ? MagickFalse : MagickTrue);
2993 case UndefinedStream:
2997 case StandardStream:
3001 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3002 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3009 return(MagickFalse);
3034MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3036 assert(image != (
const Image *) NULL);
3037 assert(image->signature == MagickCoreSignature);
3038 if (IsEventLogging() != MagickFalse)
3039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3040 return(image->blob->temporary);
3072MagickExport
void *MapBlob(
int file,
const MapMode mode,
3073 const MagickOffsetType offset,
const size_t length)
3075#if defined(MAGICKCORE_HAVE_MMAP)
3088#if defined(MAP_ANONYMOUS)
3089 flags|=MAP_ANONYMOUS;
3098 protection=PROT_READ;
3104 protection=PROT_WRITE;
3110 protection=PROT_READ | PROT_WRITE;
3115#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3116 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3118 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3119 if (map == MAP_FAILED)
3120 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3122 if (map == MAP_FAILED)
3159MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3168 assert(buffer != (
unsigned char *) NULL);
3175 *buffer++=(
unsigned char) c;
3179 *buffer++=(
unsigned char) c;
3209MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3217 assert(p != (
unsigned char *) NULL);
3224 *p++=(
unsigned char) c;
3260static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3272 size=MagickMinBufferExtent;
3273 option=GetImageOption(image_info,
"stream:buffer-size");
3274 if (option != (
const char *) NULL)
3275 size=StringToUnsignedLong(option);
3276 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3277 _IONBF : _IOFBF,size);
3278 return(status == 0 ? MagickTrue : MagickFalse);
3281#if defined(MAGICKCORE_ZLIB_DELEGATE)
3282static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3284#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3285 return(gzopen(path,mode));
3293 path_wide=create_wchar_path(path);
3294 if (path_wide == (
wchar_t *) NULL)
3295 return((gzFile) NULL);
3296 file=gzopen_w(path_wide,mode);
3297 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3303MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3307 *magick_restrict blob_info;
3310 extension[MagickPathExtent],
3311 filename[MagickPathExtent];
3325 assert(image_info != (
ImageInfo *) NULL);
3326 assert(image_info->signature == MagickCoreSignature);
3327 assert(image != (
Image *) NULL);
3328 assert(image->signature == MagickCoreSignature);
3329 if (IsEventLogging() != MagickFalse)
3330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3331 image_info->filename);
3332 blob_info=image->blob;
3333 if (image_info->blob != (
void *) NULL)
3335 if (image_info->stream != (StreamHandler) NULL)
3336 blob_info->stream=(StreamHandler) image_info->stream;
3337 AttachBlob(blob_info,image_info->blob,image_info->length);
3341 (*image->filename ==
'\0'))
3343 blob_info->type=CustomStream;
3344 blob_info->custom_stream=image_info->custom_stream;
3347 (void) DetachBlob(blob_info);
3348 blob_info->mode=mode;
3357 case ReadBinaryBlobMode:
3359 flags=O_RDONLY | O_BINARY;
3365 flags=O_WRONLY | O_CREAT | O_TRUNC;
3369 case WriteBinaryBlobMode:
3371 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3375 case AppendBlobMode:
3377 flags=O_WRONLY | O_CREAT | O_APPEND;
3381 case AppendBinaryBlobMode:
3383 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3395 blob_info->synchronize=image_info->synchronize;
3396 if (image_info->stream != (StreamHandler) NULL)
3398 blob_info->stream=image_info->stream;
3401 blob_info->type=FifoStream;
3409 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3410 rights=ReadPolicyRights;
3412 rights=WritePolicyRights;
3413 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3416 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3417 "NotAuthorized",
"`%s'",filename);
3418 return(MagickFalse);
3420 if ((LocaleCompare(filename,
"-") == 0) ||
3421 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3423 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3424#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3425 if (strchr(type,
'b') != (
char *) NULL)
3426 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3428 blob_info->type=StandardStream;
3429 blob_info->exempt=MagickTrue;
3430 return(SetStreamBuffering(image_info,blob_info));
3432 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3433 (IsGeometry(filename+3) != MagickFalse))
3440 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3441 if (blob_info->file_info.file == (FILE *) NULL)
3443 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3444 return(MagickFalse);
3446#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3447 if (strchr(type,
'b') != (
char *) NULL)
3448 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3450 blob_info->type=FileStream;
3451 blob_info->exempt=MagickTrue;
3452 return(SetStreamBuffering(image_info,blob_info));
3454#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3455 if (*filename ==
'|')
3458 fileMode[MagickPathExtent],
3466 (void) signal(SIGPIPE,SIG_IGN);
3470 sanitize_command=SanitizeString(filename+1);
3471 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3472 sanitize_command=DestroyString(sanitize_command);
3473 if (blob_info->file_info.file == (FILE *) NULL)
3475 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3476 return(MagickFalse);
3478 blob_info->type=PipeStream;
3479 blob_info->exempt=MagickTrue;
3480 return(SetStreamBuffering(image_info,blob_info));
3483 status=GetPathAttributes(filename,&blob_info->properties);
3484#if defined(S_ISFIFO)
3485 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3487 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3488 if (blob_info->file_info.file == (FILE *) NULL)
3490 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3491 return(MagickFalse);
3493 blob_info->type=FileStream;
3494 blob_info->exempt=MagickTrue;
3495 return(SetStreamBuffering(image_info,blob_info));
3498 GetPathComponent(image->filename,ExtensionPath,extension);
3501 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3502 if ((image_info->adjoin == MagickFalse) ||
3503 (strchr(filename,
'%') != (
char *) NULL))
3508 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3509 image->scene,filename,exception);
3510 if ((LocaleCompare(filename,image->filename) == 0) &&
3511 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
3512 (GetNextImageInList(image) != (
Image *) NULL)))
3515 path[MagickPathExtent];
3517 GetPathComponent(image->filename,RootPath,path);
3518 if (*extension ==
'\0')
3519 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3520 path,(
double) image->scene);
3522 (
void) FormatLocaleString(filename,MagickPathExtent,
3523 "%s-%.20g.%s",path,(
double) image->scene,extension);
3525 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3528 if (image_info->file != (FILE *) NULL)
3530 blob_info->file_info.file=image_info->file;
3531 blob_info->type=FileStream;
3532 blob_info->exempt=MagickTrue;
3540 blob_info->file_info.file=(FILE *) NULL;
3541#if defined(O_NOFOLLOW)
3542 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3543 if (status == MagickFalse)
3546 file=open_utf8(filename,flags,0);
3548 blob_info->file_info.file=fdopen(file,type);
3549 if (blob_info->file_info.file != (FILE *) NULL)
3557 blob_info->type=FileStream;
3558 (void) SetStreamBuffering(image_info,blob_info);
3559 (void) memset(magick,0,
sizeof(magick));
3560 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3561 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3562#if defined(MAGICKCORE_POSIX_SUPPORT)
3563 (void) fflush(blob_info->file_info.file);
3565 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3566 " read %.20g magic header bytes",(double) count);
3567#if defined(MAGICKCORE_ZLIB_DELEGATE)
3568 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3569 ((
int) magick[2] == 0x08))
3572 gzfile = gzopen_utf8(filename,
"rb");
3574 if (gzfile != (gzFile) NULL)
3576 if (blob_info->file_info.file != (FILE *) NULL)
3577 (void) fclose(blob_info->file_info.file);
3578 blob_info->file_info.file=(FILE *) NULL;
3579 blob_info->file_info.gzfile=gzfile;
3580 blob_info->type=ZipStream;
3584#if defined(MAGICKCORE_BZLIB_DELEGATE)
3585 if (strncmp((
char *) magick,
"BZh",3) == 0)
3588 *bzfile = BZ2_bzopen(filename,
"r");
3590 if (bzfile != (BZFILE *) NULL)
3592 if (blob_info->file_info.file != (FILE *) NULL)
3593 (void) fclose(blob_info->file_info.file);
3594 blob_info->file_info.file=(FILE *) NULL;
3595 blob_info->file_info.bzfile=bzfile;
3596 blob_info->type=BZipStream;
3600 if (blob_info->type == FileStream)
3611 sans_exception=AcquireExceptionInfo();
3612 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3613 sans_exception=DestroyExceptionInfo(sans_exception);
3614 length=(size_t) blob_info->properties.st_size;
3615 if ((magick_info != (
const MagickInfo *) NULL) &&
3616 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3617 (length > MagickMaxBufferExtent) &&
3618 (AcquireMagickResource(MapResource,length) != MagickFalse))
3623 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3625 if (blob == (
void *) NULL)
3626 RelinquishMagickResource(MapResource,length);
3632 if (image_info->file != (FILE *) NULL)
3633 blob_info->exempt=MagickFalse;
3636 (void) fclose(blob_info->file_info.file);
3637 blob_info->file_info.file=(FILE *) NULL;
3639 AttachBlob(blob_info,blob,length);
3640 blob_info->mapped=MagickTrue;
3647#if defined(MAGICKCORE_ZLIB_DELEGATE)
3648 if ((LocaleCompare(extension,
"gz") == 0) ||
3649 (LocaleCompare(extension,
"wmz") == 0) ||
3650 (LocaleCompare(extension,
"svgz") == 0))
3652 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3653 if (blob_info->file_info.gzfile != (gzFile) NULL)
3654 blob_info->type=ZipStream;
3658#if defined(MAGICKCORE_BZLIB_DELEGATE)
3659 if (LocaleCompare(extension,
"bz2") == 0)
3661 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3662 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3663 blob_info->type=BZipStream;
3671 blob_info->file_info.file=(FILE *) NULL;
3672#if defined(O_NOFOLLOW)
3673 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
3675 if (status == MagickFalse)
3678 file=open_utf8(filename,flags,0666);
3680 blob_info->file_info.file=fdopen(file,type);
3681 if (blob_info->file_info.file != (FILE *) NULL)
3683 blob_info->type=FileStream;
3684 (void) SetStreamBuffering(image_info,blob_info);
3687 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3690 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3691 "NotAuthorized",
"`%s'",filename);
3692 return(MagickFalse);
3694 blob_info->status=0;
3695 blob_info->error_number=0;
3696 if (blob_info->type != UndefinedStream)
3697 blob_info->size=GetBlobSize(image);
3700 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3701 return(MagickFalse);
3740#if defined(__cplusplus) || defined(c_plusplus)
3744static size_t PingStream(
const Image *magick_unused(image),
3745 const void *magick_unused(pixels),
const size_t columns)
3747 magick_unreferenced(image);
3748 magick_unreferenced(pixels);
3752#if defined(__cplusplus) || defined(c_plusplus)
3756MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3772 assert(image_info != (
ImageInfo *) NULL);
3773 assert(image_info->signature == MagickCoreSignature);
3775 if (IsEventLogging() != MagickFalse)
3776 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3777 image_info->filename);
3778 if ((blob == (
const void *) NULL) || (length == 0))
3780 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3781 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3782 return((
Image *) NULL);
3784 ping_info=CloneImageInfo(image_info);
3785 ping_info->blob=(
void *) blob;
3786 ping_info->length=length;
3787 ping_info->ping=MagickTrue;
3788 if (*ping_info->magick ==
'\0')
3789 (void) SetImageInfo(ping_info,0,exception);
3790 magick_info=GetMagickInfo(ping_info->magick,exception);
3791 if (magick_info == (
const MagickInfo *) NULL)
3793 (void) ThrowMagickException(exception,GetMagickModule(),
3794 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3796 ping_info=DestroyImageInfo(ping_info);
3797 return((
Image *) NULL);
3799 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3802 filename[MagickPathExtent];
3807 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3808 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3809 ping_info->magick,filename);
3810 image=ReadStream(ping_info,&PingStream,exception);
3811 if (image != (
Image *) NULL)
3812 (void) DetachBlob(image->blob);
3813 ping_info=DestroyImageInfo(ping_info);
3819 ping_info->blob=(
void *) NULL;
3820 ping_info->length=0;
3821 *ping_info->filename=
'\0';
3822 status=BlobToFile(ping_info->filename,blob,length,exception);
3823 if (status == MagickFalse)
3825 (void) RelinquishUniqueFileResource(ping_info->filename);
3826 ping_info=DestroyImageInfo(ping_info);
3827 return((
Image *) NULL);
3829 clone_info=CloneImageInfo(ping_info);
3830 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3831 ping_info->magick,ping_info->filename);
3832 image=ReadStream(clone_info,&PingStream,exception);
3833 if (image != (
Image *) NULL)
3841 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3843 (void) CopyMagickString(images->filename,image_info->filename,
3845 (void) CopyMagickString(images->magick_filename,image_info->filename,
3847 (void) CopyMagickString(images->magick,magick_info->name,
3849 images=GetNextImageInList(images);
3852 clone_info=DestroyImageInfo(clone_info);
3853 (void) RelinquishUniqueFileResource(ping_info->filename);
3854 ping_info=DestroyImageInfo(ping_info);
3889MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
void *data)
3892 *magick_restrict blob_info;
3903 assert(image != (
Image *) NULL);
3904 assert(image->signature == MagickCoreSignature);
3905 assert(image->blob != (
BlobInfo *) NULL);
3906 assert(image->blob->type != UndefinedStream);
3909 assert(data != (
void *) NULL);
3910 blob_info=image->blob;
3912 q=(
unsigned char *) data;
3913 switch (blob_info->type)
3915 case UndefinedStream:
3917 case StandardStream:
3925 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3930 c=getc(blob_info->file_info.file);
3933 *q++=(
unsigned char) c;
3939 c=getc(blob_info->file_info.file);
3942 *q++=(
unsigned char) c;
3948 c=getc(blob_info->file_info.file);
3951 *q++=(
unsigned char) c;
3957 c=getc(blob_info->file_info.file);
3960 *q++=(
unsigned char) c;
3967 if ((count != (ssize_t) length) &&
3968 (ferror(blob_info->file_info.file) != 0))
3969 ThrowBlobException(blob_info);
3974#if defined(MAGICKCORE_ZLIB_DELEGATE)
3985 for (i=0; i < length; i+=(size_t) count)
3987 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3988 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4001 c=gzgetc(blob_info->file_info.gzfile);
4004 *q++=(
unsigned char) c;
4010 c=gzgetc(blob_info->file_info.gzfile);
4013 *q++=(
unsigned char) c;
4019 c=gzgetc(blob_info->file_info.gzfile);
4022 *q++=(
unsigned char) c;
4028 c=gzgetc(blob_info->file_info.gzfile);
4031 *q++=(
unsigned char) c;
4038 (void) gzerror(blob_info->file_info.gzfile,&status);
4039 if ((count != (ssize_t) length) && (status != Z_OK))
4040 ThrowBlobException(blob_info);
4041 if (blob_info->eof == MagickFalse)
4042 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4049#if defined(MAGICKCORE_BZLIB_DELEGATE)
4056 for (i=0; i < length; i+=(size_t) count)
4058 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4059 MagickMin(length-i,MagickMaxBufferExtent));
4069 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4070 if ((count != (ssize_t) length) && (status != BZ_OK))
4071 ThrowBlobException(blob_info);
4082 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4084 blob_info->eof=MagickTrue;
4087 p=blob_info->data+blob_info->offset;
4088 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4089 blob_info->length-blob_info->offset);
4090 blob_info->offset+=count;
4091 if (count != (ssize_t) length)
4092 blob_info->eof=MagickTrue;
4093 (void) memcpy(q,p,(
size_t) count);
4098 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4099 count=blob_info->custom_stream->reader(q,length,
4100 blob_info->custom_stream->data);
4129MagickExport
int ReadBlobByte(
Image *image)
4132 *magick_restrict blob_info;
4137 assert(image != (
Image *) NULL);
4138 assert(image->signature == MagickCoreSignature);
4139 assert(image->blob != (
BlobInfo *) NULL);
4140 assert(image->blob->type != UndefinedStream);
4141 blob_info=image->blob;
4142 switch (blob_info->type)
4144 case StandardStream:
4148 c=getc(blob_info->file_info.file);
4151 if (ferror(blob_info->file_info.file) != 0)
4152 ThrowBlobException(blob_info);
4159 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4161 blob_info->eof=MagickTrue;
4164 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4165 blob_info->offset++;
4176 count=ReadBlob(image,1,buffer);
4209MagickExport
double ReadBlobDouble(
Image *image)
4220 quantum.double_value=0.0;
4221 quantum.unsigned_value=ReadBlobLongLong(image);
4222 return(quantum.double_value);
4248MagickExport
float ReadBlobFloat(
Image *image)
4259 quantum.float_value=0.0;
4260 quantum.unsigned_value=ReadBlobLong(image);
4261 return(quantum.float_value);
4287MagickExport
unsigned int ReadBlobLong(
Image *image)
4301 assert(image != (
Image *) NULL);
4302 assert(image->signature == MagickCoreSignature);
4304 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4307 if (image->endian == LSBEndian)
4309 value=(
unsigned int) (*p++);
4310 value|=(
unsigned int) (*p++) << 8;
4311 value|=(
unsigned int) (*p++) << 16;
4312 value|=(
unsigned int) (*p++) << 24;
4315 value=(
unsigned int) (*p++) << 24;
4316 value|=(
unsigned int) (*p++) << 16;
4317 value|=(
unsigned int) (*p++) << 8;
4318 value|=(
unsigned int) (*p++);
4345MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
4359 assert(image != (
Image *) NULL);
4360 assert(image->signature == MagickCoreSignature);
4362 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4364 return(MagickULLConstant(0));
4365 if (image->endian == LSBEndian)
4367 value=(MagickSizeType) (*p++);
4368 value|=(MagickSizeType) (*p++) << 8;
4369 value|=(MagickSizeType) (*p++) << 16;
4370 value|=(MagickSizeType) (*p++) << 24;
4371 value|=(MagickSizeType) (*p++) << 32;
4372 value|=(MagickSizeType) (*p++) << 40;
4373 value|=(MagickSizeType) (*p++) << 48;
4374 value|=(MagickSizeType) (*p++) << 56;
4377 value=(MagickSizeType) (*p++) << 56;
4378 value|=(MagickSizeType) (*p++) << 48;
4379 value|=(MagickSizeType) (*p++) << 40;
4380 value|=(MagickSizeType) (*p++) << 32;
4381 value|=(MagickSizeType) (*p++) << 24;
4382 value|=(MagickSizeType) (*p++) << 16;
4383 value|=(MagickSizeType) (*p++) << 8;
4384 value|=(MagickSizeType) (*p++);
4411MagickExport
unsigned short ReadBlobShort(
Image *image)
4425 assert(image != (
Image *) NULL);
4426 assert(image->signature == MagickCoreSignature);
4428 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4430 return((
unsigned short) 0U);
4431 if (image->endian == LSBEndian)
4433 value=(
unsigned short) (*p++);
4434 value|=(
unsigned short) (*p++) << 8;
4437 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4438 value|=(
unsigned short) (*p++);
4465MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
4479 assert(image != (
Image *) NULL);
4480 assert(image->signature == MagickCoreSignature);
4482 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4485 value=(
unsigned int) (*p++);
4486 value|=(
unsigned int) (*p++) << 8;
4487 value|=(
unsigned int) (*p++) << 16;
4488 value|=(
unsigned int) (*p++) << 24;
4515MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
4526 quantum.unsigned_value=ReadBlobLSBLong(image);
4527 return(quantum.signed_value);
4553MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
4567 assert(image != (
Image *) NULL);
4568 assert(image->signature == MagickCoreSignature);
4570 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4572 return((
unsigned short) 0U);
4573 value=(
unsigned short) (*p++);
4574 value|=(
unsigned short) (*p++) << 8;
4601MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
4612 quantum.unsigned_value=ReadBlobLSBShort(image);
4613 return(quantum.signed_value);
4639MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
4653 assert(image != (
Image *) NULL);
4654 assert(image->signature == MagickCoreSignature);
4656 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4659 value=(
unsigned int) (*p++) << 24;
4660 value|=(
unsigned int) (*p++) << 16;
4661 value|=(
unsigned int) (*p++) << 8;
4662 value|=(
unsigned int) (*p++);
4689MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
4703 assert(image != (
Image *) NULL);
4704 assert(image->signature == MagickCoreSignature);
4706 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4708 return(MagickULLConstant(0));
4709 value=(MagickSizeType) (*p++) << 56;
4710 value|=(MagickSizeType) (*p++) << 48;
4711 value|=(MagickSizeType) (*p++) << 40;
4712 value|=(MagickSizeType) (*p++) << 32;
4713 value|=(MagickSizeType) (*p++) << 24;
4714 value|=(MagickSizeType) (*p++) << 16;
4715 value|=(MagickSizeType) (*p++) << 8;
4716 value|=(MagickSizeType) (*p++);
4743MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
4757 assert(image != (
Image *) NULL);
4758 assert(image->signature == MagickCoreSignature);
4760 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4762 return((
unsigned short) 0U);
4763 value=(
unsigned short) ((*p++) << 8);
4764 value|=(
unsigned short) (*p++);
4765 return((
unsigned short) (value & 0xffff));
4791MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4802 quantum.unsigned_value=ReadBlobMSBLong(image);
4803 return(quantum.signed_value);
4829MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4840 quantum.unsigned_value=ReadBlobMSBShort(image);
4841 return(quantum.signed_value);
4867MagickExport
signed int ReadBlobSignedLong(
Image *image)
4878 quantum.unsigned_value=ReadBlobLong(image);
4879 return(quantum.signed_value);
4905MagickExport
signed short ReadBlobSignedShort(
Image *image)
4916 quantum.unsigned_value=ReadBlobShort(image);
4917 return(quantum.signed_value);
4955MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4956 const size_t length,
void *magick_restrict data,ssize_t *count)
4959 *magick_restrict blob_info;
4961 assert(image != (
Image *) NULL);
4962 assert(image->signature == MagickCoreSignature);
4963 assert(image->blob != (
BlobInfo *) NULL);
4964 assert(image->blob->type != UndefinedStream);
4965 assert(count != (ssize_t *) NULL);
4966 blob_info=image->blob;
4967 if (blob_info->type != BlobStream)
4969 assert(data != NULL);
4970 *count=ReadBlob(image,length,(
unsigned char *) data);
4973 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4976 blob_info->eof=MagickTrue;
4979 data=blob_info->data+blob_info->offset;
4980 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4981 blob_info->length-blob_info->offset);
4982 blob_info->offset+=(*count);
4983 if (*count != (ssize_t) length)
4984 blob_info->eof=MagickTrue;
5013MagickExport
char *ReadBlobString(
Image *image,
char *
string)
5016 *magick_restrict blob_info;
5024 assert(image != (
Image *) NULL);
5025 assert(image->signature == MagickCoreSignature);
5026 assert(image->blob != (
BlobInfo *) NULL);
5027 assert(image->blob->type != UndefinedStream);
5028 if (IsEventLogging() != MagickFalse)
5029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5031 blob_info=image->blob;
5032 switch (blob_info->type)
5034 case UndefinedStream:
5036 case StandardStream:
5039 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5040 if (p == (
char *) NULL)
5042 if (ferror(blob_info->file_info.file) != 0)
5043 ThrowBlobException(blob_info);
5044 return((
char *) NULL);
5051#if defined(MAGICKCORE_ZLIB_DELEGATE)
5052 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5053 if (p == (
char *) NULL)
5056 (void) gzerror(blob_info->file_info.gzfile,&status);
5058 ThrowBlobException(blob_info);
5059 return((
char *) NULL);
5069 c=ReadBlobByte(image);
5072 blob_info->eof=MagickTrue;
5078 }
while (i < (MaxTextExtent-2));
5086 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5089 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5091 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5092 return((
char *) NULL);
5122 assert(blob->signature == MagickCoreSignature);
5123 if (IsEventLogging() != MagickFalse)
5124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5125 LockSemaphoreInfo(blob->semaphore);
5126 blob->reference_count++;
5127 UnlockSemaphoreInfo(blob->semaphore);
5164MagickExport MagickOffsetType SeekBlob(
Image *image,
5165 const MagickOffsetType offset,
const int whence)
5168 *magick_restrict blob_info;
5170 assert(image != (
Image *) NULL);
5171 assert(image->signature == MagickCoreSignature);
5172 assert(image->blob != (
BlobInfo *) NULL);
5173 assert(image->blob->type != UndefinedStream);
5174 if (IsEventLogging() != MagickFalse)
5175 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5176 blob_info=image->blob;
5177 switch (blob_info->type)
5179 case UndefinedStream:
5181 case StandardStream:
5186 if ((offset < 0) && (whence == SEEK_SET))
5188 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5190 blob_info->offset=TellBlob(image);
5195#if defined(MAGICKCORE_ZLIB_DELEGATE)
5196 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
5199 blob_info->offset=TellBlob(image);
5215 blob_info->offset=offset;
5220 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5221 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5226 if ((blob_info->offset+offset) < 0)
5228 blob_info->offset+=offset;
5233 if (((MagickOffsetType) blob_info->length+offset) < 0)
5235 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5239 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5241 blob_info->eof=MagickFalse;
5248 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5250 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5251 blob_info->custom_stream->data);
5255 return(blob_info->offset);
5283MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
5285 assert(image != (
const Image *) NULL);
5286 assert(image->signature == MagickCoreSignature);
5287 if (IsEventLogging() != MagickFalse)
5288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5289 image->blob->exempt=exempt;
5318MagickExport MagickBooleanType SetBlobExtent(
Image *image,
5319 const MagickSizeType extent)
5322 *magick_restrict blob_info;
5324 assert(image != (
Image *) NULL);
5325 assert(image->signature == MagickCoreSignature);
5326 assert(image->blob != (
BlobInfo *) NULL);
5327 assert(image->blob->type != UndefinedStream);
5328 if (IsEventLogging() != MagickFalse)
5329 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5330 blob_info=image->blob;
5331 switch (blob_info->type)
5333 case UndefinedStream:
5335 case StandardStream:
5336 return(MagickFalse);
5345 if (extent != (MagickSizeType) ((off_t) extent))
5346 return(MagickFalse);
5347 offset=SeekBlob(image,0,SEEK_END);
5349 return(MagickFalse);
5350 if ((MagickSizeType) offset >= extent)
5352 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5355 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5356 blob_info->file_info.file);
5357#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5358 if (blob_info->synchronize != MagickFalse)
5363 file=fileno(blob_info->file_info.file);
5364 if ((file == -1) || (offset < 0))
5365 return(MagickFalse);
5366 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5369 offset=SeekBlob(image,offset,SEEK_SET);
5371 return(MagickFalse);
5376 return(MagickFalse);
5378 return(MagickFalse);
5380 return(MagickFalse);
5383 if (extent != (MagickSizeType) ((
size_t) extent))
5384 return(MagickFalse);
5385 if (blob_info->mapped != MagickFalse)
5393 (void) UnmapBlob(blob_info->data,blob_info->length);
5394 RelinquishMagickResource(MapResource,blob_info->length);
5395 if (extent != (MagickSizeType) ((off_t) extent))
5396 return(MagickFalse);
5397 offset=SeekBlob(image,0,SEEK_END);
5399 return(MagickFalse);
5400 if ((MagickSizeType) offset >= extent)
5402 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5403 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5404 blob_info->file_info.file);
5405#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5406 if (blob_info->synchronize != MagickFalse)
5411 file=fileno(blob_info->file_info.file);
5412 if ((file == -1) || (offset < 0))
5413 return(MagickFalse);
5414 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5418 offset=SeekBlob(image,offset,SEEK_SET);
5420 return(MagickFalse);
5421 (void) AcquireMagickResource(MapResource,extent);
5422 blob_info->data=(
unsigned char*) MapBlob(fileno(
5423 blob_info->file_info.file),WriteMode,0,(size_t) extent);
5424 blob_info->extent=(size_t) extent;
5425 blob_info->length=(size_t) extent;
5426 (void) SyncBlob(image);
5429 blob_info->extent=(size_t) extent;
5430 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5431 blob_info->extent+1,
sizeof(*blob_info->data));
5432 (void) SyncBlob(image);
5433 if (blob_info->data == (
unsigned char *) NULL)
5435 (void) DetachBlob(blob_info);
5436 return(MagickFalse);
5474 assert(custom_stream->signature == MagickCoreSignature);
5475 custom_stream->data=data;
5504 CustomStreamHandler reader)
5507 assert(custom_stream->signature == MagickCoreSignature);
5508 custom_stream->reader=reader;
5537 CustomStreamSeeker seeker)
5540 assert(custom_stream->signature == MagickCoreSignature);
5541 custom_stream->seeker=seeker;
5570 CustomStreamTeller teller)
5573 assert(custom_stream->signature == MagickCoreSignature);
5574 custom_stream->teller=teller;
5603 CustomStreamHandler writer)
5606 assert(custom_stream->signature == MagickCoreSignature);
5607 custom_stream->writer=writer;
5634static int SyncBlob(
const Image *image)
5637 *magick_restrict blob_info;
5642 assert(image != (
Image *) NULL);
5643 assert(image->signature == MagickCoreSignature);
5644 assert(image->blob != (
BlobInfo *) NULL);
5645 if (IsEventLogging() != MagickFalse)
5646 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5647 if (EOFBlob(image) != 0)
5649 blob_info=image->blob;
5651 switch (blob_info->type)
5653 case UndefinedStream:
5654 case StandardStream:
5659 status=fflush(blob_info->file_info.file);
5664#if defined(MAGICKCORE_ZLIB_DELEGATE)
5665 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5671#if defined(MAGICKCORE_BZLIB_DELEGATE)
5672 status=BZ2_bzflush(blob_info->file_info.bzfile);
5708MagickExport MagickOffsetType TellBlob(
const Image *image)
5711 *magick_restrict blob_info;
5716 assert(image != (
Image *) NULL);
5717 assert(image->signature == MagickCoreSignature);
5718 assert(image->blob != (
BlobInfo *) NULL);
5719 assert(image->blob->type != UndefinedStream);
5720 if (IsEventLogging() != MagickFalse)
5721 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5722 blob_info=image->blob;
5724 switch (blob_info->type)
5726 case UndefinedStream:
5727 case StandardStream:
5731 offset=ftell(blob_info->file_info.file);
5738#if defined(MAGICKCORE_ZLIB_DELEGATE)
5739 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5749 offset=blob_info->offset;
5754 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5755 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5787MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5789#if defined(MAGICKCORE_HAVE_MMAP)
5793 status=munmap(map,length);
5794 return(status == -1 ? MagickFalse : MagickTrue);
5798 return(MagickFalse);
5830MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
5834 *magick_restrict blob_info;
5848 assert(image != (
Image *) NULL);
5849 assert(image->signature == MagickCoreSignature);
5850 assert(image->blob != (
BlobInfo *) NULL);
5851 assert(image->blob->type != UndefinedStream);
5854 assert(data != (
const void *) NULL);
5855 blob_info=image->blob;
5857 p=(
const unsigned char *) data;
5858 q=(
unsigned char *) data;
5859 switch (blob_info->type)
5861 case UndefinedStream:
5863 case StandardStream:
5871 count=(ssize_t) fwrite((
const char *) data,1,length,
5872 blob_info->file_info.file);
5877 c=putc((
int) *p++,blob_info->file_info.file);
5885 c=putc((
int) *p++,blob_info->file_info.file);
5893 c=putc((
int) *p++,blob_info->file_info.file);
5901 c=putc((
int) *p++,blob_info->file_info.file);
5910 if ((count != (ssize_t) length) &&
5911 (ferror(blob_info->file_info.file) != 0))
5912 ThrowBlobException(blob_info);
5917#if defined(MAGICKCORE_ZLIB_DELEGATE)
5928 for (i=0; i < length; i+=(size_t) count)
5930 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5931 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5944 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5952 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5960 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5968 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5978 (void) gzerror(blob_info->file_info.gzfile,&status);
5979 if ((count != (ssize_t) length) && (status != Z_OK))
5980 ThrowBlobException(blob_info);
5986#if defined(MAGICKCORE_BZLIB_DELEGATE)
5993 for (i=0; i < length; i+=(size_t) count)
5995 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5996 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6006 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6007 if ((count != (ssize_t) length) && (status != BZ_OK))
6008 ThrowBlobException(blob_info);
6014 count=(ssize_t) blob_info->stream(image,data,length);
6022 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6023 if (extent >= blob_info->extent)
6025 if (blob_info->mapped != MagickFalse)
6027 blob_info->extent=extent+blob_info->quantum+length;
6028 blob_info->quantum<<=1;
6029 blob_info->data=(
unsigned char *) ResizeQuantumMemory(
6030 blob_info->data,blob_info->extent+1,
sizeof(*blob_info->data));
6031 (void) SyncBlob(image);
6032 if (blob_info->data == (
unsigned char *) NULL)
6034 (void) DetachBlob(blob_info);
6038 q=blob_info->data+blob_info->offset;
6039 (void) memcpy(q,p,length);
6040 blob_info->offset+=(MagickOffsetType) length;
6041 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6042 blob_info->length=(
size_t) blob_info->offset;
6043 count=(ssize_t) length;
6048 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6049 count=blob_info->custom_stream->writer((
unsigned char *) data,
6050 length,blob_info->custom_stream->data);
6082MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
6085 *magick_restrict blob_info;
6090 assert(image != (
Image *) NULL);
6091 assert(image->signature == MagickCoreSignature);
6092 assert(image->blob != (
BlobInfo *) NULL);
6093 assert(image->blob->type != UndefinedStream);
6094 blob_info=image->blob;
6096 switch (blob_info->type)
6098 case StandardStream:
6105 c=putc((
int) value,blob_info->file_info.file);
6108 if (ferror(blob_info->file_info.file) != 0)
6109 ThrowBlobException(blob_info);
6117 count=WriteBlobStream(image,1,&value);
6149MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
6160 quantum.unsigned_value=0U;
6161 quantum.float_value=value;
6162 return(WriteBlobLong(image,quantum.unsigned_value));
6190MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
6195 assert(image != (
Image *) NULL);
6196 assert(image->signature == MagickCoreSignature);
6197 if (image->endian == LSBEndian)
6199 buffer[0]=(
unsigned char) value;
6200 buffer[1]=(
unsigned char) (value >> 8);
6201 buffer[2]=(
unsigned char) (value >> 16);
6202 buffer[3]=(
unsigned char) (value >> 24);
6203 return(WriteBlobStream(image,4,buffer));
6205 buffer[0]=(
unsigned char) (value >> 24);
6206 buffer[1]=(
unsigned char) (value >> 16);
6207 buffer[2]=(
unsigned char) (value >> 8);
6208 buffer[3]=(
unsigned char) value;
6209 return(WriteBlobStream(image,4,buffer));
6237MagickExport ssize_t WriteBlobLongLong(
Image *image,
const MagickSizeType value)
6242 assert(image != (
Image *) NULL);
6243 assert(image->signature == MagickCoreSignature);
6244 if (image->endian == LSBEndian)
6246 buffer[0]=(
unsigned char) value;
6247 buffer[1]=(
unsigned char) (value >> 8);
6248 buffer[2]=(
unsigned char) (value >> 16);
6249 buffer[3]=(
unsigned char) (value >> 24);
6250 buffer[4]=(
unsigned char) (value >> 32);
6251 buffer[5]=(
unsigned char) (value >> 40);
6252 buffer[6]=(
unsigned char) (value >> 48);
6253 buffer[7]=(
unsigned char) (value >> 56);
6254 return(WriteBlobStream(image,8,buffer));
6256 buffer[0]=(
unsigned char) (value >> 56);
6257 buffer[1]=(
unsigned char) (value >> 48);
6258 buffer[2]=(
unsigned char) (value >> 40);
6259 buffer[3]=(
unsigned char) (value >> 32);
6260 buffer[4]=(
unsigned char) (value >> 24);
6261 buffer[5]=(
unsigned char) (value >> 16);
6262 buffer[6]=(
unsigned char) (value >> 8);
6263 buffer[7]=(
unsigned char) value;
6264 return(WriteBlobStream(image,8,buffer));
6292MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
6297 assert(image != (
Image *) NULL);
6298 assert(image->signature == MagickCoreSignature);
6299 if (image->endian == LSBEndian)
6301 buffer[0]=(
unsigned char) value;
6302 buffer[1]=(
unsigned char) (value >> 8);
6303 return(WriteBlobStream(image,2,buffer));
6305 buffer[0]=(
unsigned char) (value >> 8);
6306 buffer[1]=(
unsigned char) value;
6307 return(WriteBlobStream(image,2,buffer));
6335MagickExport ssize_t WriteBlobSignedLong(
Image *image,
const signed int value)
6349 assert(image != (
Image *) NULL);
6350 assert(image->signature == MagickCoreSignature);
6351 quantum.signed_value=value;
6352 if (image->endian == LSBEndian)
6354 buffer[0]=(
unsigned char) quantum.unsigned_value;
6355 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6356 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6357 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6358 return(WriteBlobStream(image,4,buffer));
6360 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6361 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6362 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6363 buffer[3]=(
unsigned char) quantum.unsigned_value;
6364 return(WriteBlobStream(image,4,buffer));
6392MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
6397 assert(image != (
Image *) NULL);
6398 assert(image->signature == MagickCoreSignature);
6399 buffer[0]=(
unsigned char) value;
6400 buffer[1]=(
unsigned char) (value >> 8);
6401 buffer[2]=(
unsigned char) (value >> 16);
6402 buffer[3]=(
unsigned char) (value >> 24);
6403 return(WriteBlobStream(image,4,buffer));
6431MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
6436 assert(image != (
Image *) NULL);
6437 assert(image->signature == MagickCoreSignature);
6438 buffer[0]=(
unsigned char) value;
6439 buffer[1]=(
unsigned char) (value >> 8);
6440 return(WriteBlobStream(image,2,buffer));
6468MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
6482 assert(image != (
Image *) NULL);
6483 assert(image->signature == MagickCoreSignature);
6484 quantum.signed_value=value;
6485 buffer[0]=(
unsigned char) quantum.unsigned_value;
6486 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6487 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6488 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6489 return(WriteBlobStream(image,4,buffer));
6517MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
6518 const signed short value)
6532 assert(image != (
Image *) NULL);
6533 assert(image->signature == MagickCoreSignature);
6534 quantum.signed_value=value;
6535 buffer[0]=(
unsigned char) quantum.unsigned_value;
6536 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6537 return(WriteBlobStream(image,2,buffer));
6565MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
6570 assert(image != (
Image *) NULL);
6571 assert(image->signature == MagickCoreSignature);
6572 buffer[0]=(
unsigned char) (value >> 24);
6573 buffer[1]=(
unsigned char) (value >> 16);
6574 buffer[2]=(
unsigned char) (value >> 8);
6575 buffer[3]=(
unsigned char) value;
6576 return(WriteBlobStream(image,4,buffer));
6604MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
6605 const signed short value)
6619 assert(image != (
Image *) NULL);
6620 assert(image->signature == MagickCoreSignature);
6621 quantum.signed_value=value;
6622 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6623 buffer[1]=(
unsigned char) quantum.unsigned_value;
6624 return(WriteBlobStream(image,2,buffer));
6652MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
6657 assert(image != (
Image *) NULL);
6658 assert(image->signature == MagickCoreSignature);
6659 buffer[0]=(
unsigned char) (value >> 8);
6660 buffer[1]=(
unsigned char) value;
6661 return(WriteBlobStream(image,2,buffer));
6689MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
6691 assert(image != (
Image *) NULL);
6692 assert(image->signature == MagickCoreSignature);
6693 assert(
string != (
const char *) NULL);
6694 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));