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 IsPathAuthorized(rights,filename) \
86 ((IsRightsAuthorized(PathPolicyDomain,rights,filename) != MagickFalse) && \
87 ((IsRightsAuthorizedByName(SystemPolicyDomain,"symlink",rights,"follow") != MagickFalse) || \
88 (is_symlink_utf8(filename) == MagickFalse)))
89#define MagickMaxBlobExtent (8*8192)
90#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
91# define MAP_ANONYMOUS MAP_ANON
93#if !defined(MAP_FAILED)
94#define MAP_FAILED ((void *) -1)
98#define _O_BINARY O_BINARY
100#if defined(MAGICKCORE_WINDOWS_SUPPORT)
102# define fsync _commit
105# define MAGICKCORE_HAVE_MMAP 1
106# define mmap(address,length,protection,access,file,offset) \
107 NTMapMemory(address,length,protection,access,file,offset)
110# define munmap(address,length) NTUnmapMemory(address,length)
113# define pclose _pclose
128#if defined(MAGICKCORE_ZLIB_DELEGATE)
133#if defined(MAGICKCORE_BZLIB_DELEGATE)
225 SyncBlob(
const Image *);
255 magick_unreferenced(exception);
257 sizeof(*custom_stream));
258 (void) memset(custom_stream,0,
sizeof(*custom_stream));
259 custom_stream->signature=MagickCoreSignature;
260 return(custom_stream);
290MagickExport
void AttachBlob(
BlobInfo *blob_info,
const void *blob,
293 assert(blob_info != (
BlobInfo *) NULL);
294 if (IsEventLogging() != MagickFalse)
295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
296 blob_info->length=length;
297 blob_info->extent=length;
298 blob_info->quantum=(size_t) MagickMaxBlobExtent;
300 blob_info->type=BlobStream;
301 blob_info->file_info.file=(FILE *) NULL;
302 blob_info->data=(
unsigned char *) blob;
303 blob_info->mapped=MagickFalse;
331MagickExport
void AttachCustomStream(
BlobInfo *blob_info,
334 assert(blob_info != (
BlobInfo *) NULL);
336 assert(custom_stream->signature == MagickCoreSignature);
337 if (IsEventLogging() != MagickFalse)
338 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
339 blob_info->type=CustomStream;
340 blob_info->custom_stream=custom_stream;
373MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
385 assert(filename != (
const char *) NULL);
386 assert(blob != (
const void *) NULL);
387 if (IsEventLogging() != MagickFalse)
388 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
389 if (*filename ==
'\0')
390 file=AcquireUniqueFileResource(filename);
392 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
395 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
398 for (i=0; i < length; i+=(size_t) count)
400 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
401 MagickMaxBufferExtent));
409 file=close_utf8(file);
410 if ((file == -1) || (i < length))
412 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
449MagickExport
Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
465 assert(image_info != (
ImageInfo *) NULL);
466 assert(image_info->signature == MagickCoreSignature);
468 if (IsEventLogging() != MagickFalse)
469 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
470 image_info->filename);
471 if ((blob == (
const void *) NULL) || (length == 0))
473 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
474 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
475 return((
Image *) NULL);
477 blob_info=CloneImageInfo(image_info);
478 blob_info->blob=(
void *) blob;
479 blob_info->length=length;
480 if (*blob_info->magick ==
'\0')
481 (void) SetImageInfo(blob_info,0,exception);
482 magick_info=GetMagickInfo(blob_info->magick,exception);
485 (void) ThrowMagickException(exception,GetMagickModule(),
486 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
488 blob_info=DestroyImageInfo(blob_info);
489 return((
Image *) NULL);
491 if (GetMagickBlobSupport(magick_info) != MagickFalse)
494 filename[MagickPathExtent];
499 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
500 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
501 blob_info->magick,filename);
502 image=ReadImage(blob_info,exception);
503 if (image != (
Image *) NULL)
504 (void) DetachBlob(image->blob);
505 blob_info=DestroyImageInfo(blob_info);
511 blob_info->blob=(
void *) NULL;
513 *blob_info->filename=
'\0';
514 status=BlobToFile(blob_info->filename,blob,length,exception);
515 if (status == MagickFalse)
517 (void) RelinquishUniqueFileResource(blob_info->filename);
518 blob_info=DestroyImageInfo(blob_info);
519 return((
Image *) NULL);
521 clone_info=CloneImageInfo(blob_info);
522 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
523 blob_info->magick,blob_info->filename);
524 image=ReadImage(clone_info,exception);
525 if (image != (
Image *) NULL)
533 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
535 (void) CopyMagickString(images->filename,image_info->filename,
537 (void) CopyMagickString(images->magick_filename,image_info->filename,
539 (void) CopyMagickString(images->magick,magick_info->name,
541 images=GetNextImageInList(images);
544 clone_info=DestroyImageInfo(clone_info);
545 (void) RelinquishUniqueFileResource(blob_info->filename);
546 blob_info=DestroyImageInfo(blob_info);
581 clone_info=(
BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
582 GetBlobInfo(clone_info);
585 semaphore=clone_info->semaphore;
586 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
587 if (blob_info->mapped != MagickFalse)
588 (void) AcquireMagickResource(MapResource,blob_info->length);
589 clone_info->semaphore=semaphore;
590 LockSemaphoreInfo(clone_info->semaphore);
591 clone_info->reference_count=1;
592 UnlockSemaphoreInfo(clone_info->semaphore);
619static inline void ThrowBlobException(
BlobInfo *blob_info)
621 if ((blob_info->status == 0) && (errno != 0))
622 blob_info->error_number=errno;
623 blob_info->status=(-1);
626MagickExport MagickBooleanType CloseBlob(
Image *image)
629 *magick_restrict blob_info;
637 assert(image != (
Image *) NULL);
638 assert(image->signature == MagickCoreSignature);
639 if (IsEventLogging() != MagickFalse)
640 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
641 blob_info=image->blob;
642 if ((blob_info == (
BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
644 (void) SyncBlob(image);
645 status=blob_info->status;
646 switch (blob_info->type)
648 case UndefinedStream:
654 if (blob_info->synchronize != MagickFalse)
656 status=fflush(blob_info->file_info.file);
658 ThrowBlobException(blob_info);
659 status=fsync(fileno(blob_info->file_info.file));
661 ThrowBlobException(blob_info);
663 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
664 ThrowBlobException(blob_info);
669#if defined(MAGICKCORE_ZLIB_DELEGATE)
671 (void) gzerror(blob_info->file_info.gzfile,&status);
673 ThrowBlobException(blob_info);
679#if defined(MAGICKCORE_BZLIB_DELEGATE)
681 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
683 ThrowBlobException(blob_info);
691 if (blob_info->file_info.file != (FILE *) NULL)
693 if (blob_info->synchronize != MagickFalse)
695 status=fflush(blob_info->file_info.file);
697 ThrowBlobException(blob_info);
698 status=fsync(fileno(blob_info->file_info.file));
700 ThrowBlobException(blob_info);
702 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
703 ThrowBlobException(blob_info);
710 blob_info->size=GetBlobSize(image);
711 image->extent=blob_info->size;
712 blob_info->eof=MagickFalse;
714 blob_info->mode=UndefinedBlobMode;
715 if (blob_info->exempt != MagickFalse)
717 blob_info->type=UndefinedStream;
718 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
720 switch (blob_info->type)
722 case UndefinedStream:
727 if (blob_info->file_info.file != (FILE *) NULL)
729 status=fclose(blob_info->file_info.file);
731 ThrowBlobException(blob_info);
737#if defined(MAGICKCORE_HAVE_PCLOSE)
738 status=pclose(blob_info->file_info.file);
740 ThrowBlobException(blob_info);
746#if defined(MAGICKCORE_ZLIB_DELEGATE)
747 status=gzclose(blob_info->file_info.gzfile);
749 ThrowBlobException(blob_info);
755#if defined(MAGICKCORE_BZLIB_DELEGATE)
756 BZ2_bzclose(blob_info->file_info.bzfile);
764 if (blob_info->file_info.file != (FILE *) NULL)
766 status=fclose(blob_info->file_info.file);
768 ThrowBlobException(blob_info);
775 (void) DetachBlob(blob_info);
776 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
805MagickExport
Image *CustomStreamToImage(
const ImageInfo *image_info,
817 assert(image_info != (
ImageInfo *) NULL);
818 assert(image_info->signature == MagickCoreSignature);
820 assert(image_info->custom_stream->signature == MagickCoreSignature);
821 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
823 if (IsEventLogging() != MagickFalse)
824 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
825 image_info->filename);
826 blob_info=CloneImageInfo(image_info);
827 if (*blob_info->magick ==
'\0')
828 (void) SetImageInfo(blob_info,0,exception);
829 magick_info=GetMagickInfo(blob_info->magick,exception);
832 (void) ThrowMagickException(exception,GetMagickModule(),
833 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
835 blob_info=DestroyImageInfo(blob_info);
836 return((
Image *) NULL);
838 image=(
Image *) NULL;
839 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
840 (*blob_info->filename !=
'\0'))
843 filename[MagickPathExtent];
849 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
850 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
851 blob_info->magick,filename);
852 image=ReadImage(blob_info,exception);
857 unique[MagickPathExtent];
872 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
874 if (blob == (
unsigned char *) NULL)
876 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
877 image_info->filename);
878 blob_info=DestroyImageInfo(blob_info);
879 return((
Image *) NULL);
881 file=AcquireUniqueFileResource(unique);
884 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
885 image_info->filename);
886 blob=(
unsigned char *) RelinquishMagickMemory(blob);
887 blob_info=DestroyImageInfo(blob_info);
888 return((
Image *) NULL);
890 clone_info=CloneImageInfo(blob_info);
891 blob_info->file=fdopen(file,
"wb+");
892 if (blob_info->file != (FILE *) NULL)
897 count=(ssize_t) MagickMaxBufferExtent;
898 while (count == (ssize_t) MagickMaxBufferExtent)
900 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
901 image_info->custom_stream->data);
902 count=(ssize_t) write(file,(
const char *) blob,(size_t) count);
904 (void) fclose(blob_info->file);
905 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
906 "%s:%s",blob_info->magick,unique);
907 image=ReadImage(clone_info,exception);
908 if (image != (
Image *) NULL)
916 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
918 (void) CopyMagickString(images->filename,image_info->filename,
920 (void) CopyMagickString(images->magick_filename,
921 image_info->filename,MagickPathExtent);
922 (void) CopyMagickString(images->magick,magick_info->name,
924 images=GetNextImageInList(images);
928 clone_info=DestroyImageInfo(clone_info);
929 blob=(
unsigned char *) RelinquishMagickMemory(blob);
930 (void) RelinquishUniqueFileResource(unique);
932 blob_info=DestroyImageInfo(blob_info);
933 if (image != (
Image *) NULL)
934 if (CloseBlob(image) == MagickFalse)
935 image=DestroyImageList(image);
961MagickExport
void DestroyBlob(
Image *image)
964 *magick_restrict blob_info;
969 assert(image != (
Image *) NULL);
970 assert(image->signature == MagickCoreSignature);
971 assert(image->blob != (
BlobInfo *) NULL);
972 assert(image->blob->signature == MagickCoreSignature);
973 if (IsEventLogging() != MagickFalse)
974 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
975 blob_info=image->blob;
977 LockSemaphoreInfo(blob_info->semaphore);
978 blob_info->reference_count--;
979 assert(blob_info->reference_count >= 0);
980 if (blob_info->reference_count == 0)
982 UnlockSemaphoreInfo(blob_info->semaphore);
983 if (destroy == MagickFalse)
988 (void) CloseBlob(image);
989 if (blob_info->mapped != MagickFalse)
991 (void) UnmapBlob(blob_info->data,blob_info->length);
992 RelinquishMagickResource(MapResource,blob_info->length);
995 RelinquishSemaphoreInfo(&blob_info->semaphore);
996 blob_info->signature=(~MagickCoreSignature);
997 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
1027 assert(custom_stream->signature == MagickCoreSignature);
1028 if (IsEventLogging() != MagickFalse)
1029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1030 custom_stream->signature=(~MagickCoreSignature);
1032 return(custom_stream);
1057MagickExport
void *DetachBlob(
BlobInfo *blob_info)
1062 assert(blob_info != (
BlobInfo *) NULL);
1063 if (IsEventLogging() != MagickFalse)
1064 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1065 if (blob_info->mapped != MagickFalse)
1067 (void) UnmapBlob(blob_info->data,blob_info->length);
1068 blob_info->data=NULL;
1069 RelinquishMagickResource(MapResource,blob_info->length);
1071 blob_info->mapped=MagickFalse;
1072 blob_info->length=0;
1077 blob_info->offset=0;
1078 blob_info->mode=UndefinedBlobMode;
1079 blob_info->eof=MagickFalse;
1081 blob_info->exempt=MagickFalse;
1082 blob_info->type=UndefinedStream;
1083 blob_info->file_info.file=(FILE *) NULL;
1084 data=blob_info->data;
1085 blob_info->data=(
unsigned char *) NULL;
1086 blob_info->stream=(StreamHandler) NULL;
1115MagickExport
void DisassociateBlob(
Image *image)
1118 *magick_restrict blob_info,
1124 assert(image != (
Image *) NULL);
1125 assert(image->signature == MagickCoreSignature);
1126 assert(image->blob != (
BlobInfo *) NULL);
1127 assert(image->blob->signature == MagickCoreSignature);
1128 if (IsEventLogging() != MagickFalse)
1129 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1130 blob_info=image->blob;
1132 LockSemaphoreInfo(blob_info->semaphore);
1133 assert(blob_info->reference_count >= 0);
1134 if (blob_info->reference_count > 1)
1136 UnlockSemaphoreInfo(blob_info->semaphore);
1137 if (clone == MagickFalse)
1139 clone_info=CloneBlobInfo(blob_info);
1141 image->blob=clone_info;
1169MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
1170 const MagickSizeType length)
1182 buffer[MagickMinBufferExtent >> 1];
1184 assert(image != (
Image *) NULL);
1185 assert(image->signature == MagickCoreSignature);
1186 if (length != (MagickSizeType) ((MagickOffsetType) length))
1187 return(MagickFalse);
1189 for (i=0; i < length; i+=(MagickSizeType) count)
1191 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1192 (void) ReadBlobStream(image,quantum,buffer,&count);
1200 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1227MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
1229 assert(image != (
Image *) NULL);
1230 assert(image->signature == MagickCoreSignature);
1231 assert(duplicate != (
Image *) NULL);
1232 assert(duplicate->signature == MagickCoreSignature);
1233 if (IsEventLogging() != MagickFalse)
1234 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1236 image->blob=ReferenceBlob(duplicate->blob);
1262MagickExport
int EOFBlob(
const Image *image)
1265 *magick_restrict blob_info;
1267 assert(image != (
Image *) NULL);
1268 assert(image->signature == MagickCoreSignature);
1269 assert(image->blob != (
BlobInfo *) NULL);
1270 assert(image->blob->type != UndefinedStream);
1271 if (IsEventLogging() != MagickFalse)
1272 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1273 blob_info=image->blob;
1274 switch (blob_info->type)
1276 case UndefinedStream:
1277 case StandardStream:
1282 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1288#if defined(MAGICKCORE_ZLIB_DELEGATE)
1289 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1296#if defined(MAGICKCORE_BZLIB_DELEGATE)
1301 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1302 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1308 blob_info->eof=MagickFalse;
1316 return((
int) blob_info->eof);
1342MagickExport
int ErrorBlob(
const Image *image)
1345 *magick_restrict blob_info;
1347 assert(image != (
Image *) NULL);
1348 assert(image->signature == MagickCoreSignature);
1349 assert(image->blob != (
BlobInfo *) NULL);
1350 assert(image->blob->type != UndefinedStream);
1351 if (IsEventLogging() != MagickFalse)
1352 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1353 blob_info=image->blob;
1354 switch (blob_info->type)
1356 case UndefinedStream:
1357 case StandardStream:
1362 blob_info->error=ferror(blob_info->file_info.file);
1367#if defined(MAGICKCORE_ZLIB_DELEGATE)
1368 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1374#if defined(MAGICKCORE_BZLIB_DELEGATE)
1375 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1389 return(blob_info->error);
1427MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1454 assert(filename != (
const char *) NULL);
1456 assert(exception->signature == MagickCoreSignature);
1457 if (IsEventLogging() != MagickFalse)
1458 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1460 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1461 if (status == MagickFalse)
1464 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1465 "NotAuthorized",
"`%s'",filename);
1469 if (LocaleCompare(filename,
"-") != 0)
1472 flags = O_RDONLY | O_BINARY;
1474 status=GetPathAttributes(filename,&attributes);
1475 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1477 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1480#if defined(O_NOFOLLOW)
1481 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1482 if (status == MagickFalse)
1485 file=open_utf8(filename,flags,0);
1489 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1492 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1493 if (status == MagickFalse)
1495 file=close_utf8(file)-1;
1497 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1498 "NotAuthorized",
"`%s'",filename);
1501 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1503 if ((file == fileno(stdin)) || (offset < 0) ||
1504 (offset != (MagickOffsetType) ((ssize_t) offset)))
1515 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1516 quantum=(size_t) MagickMaxBufferExtent;
1517 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1518 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1519 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1520 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1522 count=read(file,blob+i,quantum);
1529 if (~i < ((
size_t) count+quantum+1))
1531 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1534 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1535 quantum+1,
sizeof(*blob));
1536 if ((i+(
size_t) count) >= extent)
1539 if (LocaleCompare(filename,
"-") != 0)
1540 file=close_utf8(file);
1541 if (blob == (
unsigned char *) NULL)
1543 (void) ThrowMagickException(exception,GetMagickModule(),
1544 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1549 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1550 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1553 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1557 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1558 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1559 blob=(
unsigned char *) NULL;
1560 if (~(*length) >= (MagickPathExtent-1))
1561 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1563 if (blob == (
unsigned char *) NULL)
1565 file=close_utf8(file);
1566 (void) ThrowMagickException(exception,GetMagickModule(),
1567 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1570 map=MapBlob(file,ReadMode,0,*length);
1571 if (map != (
unsigned char *) NULL)
1573 (void) memcpy(blob,map,*length);
1574 (void) UnmapBlob(map,*length);
1578 (void) lseek(file,0,SEEK_SET);
1579 for (i=0; i < *length; i+=(size_t) count)
1581 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1582 MagickMaxBufferExtent));
1592 file=close_utf8(file)-1;
1593 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1594 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1599 if (LocaleCompare(filename,
"-") != 0)
1600 file=close_utf8(file);
1603 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1604 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1633static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1634 const void *magick_restrict data)
1637 *magick_restrict blob_info;
1645 assert(image->blob != (
BlobInfo *) NULL);
1646 assert(image->blob->type != UndefinedStream);
1647 assert(data != NULL);
1648 blob_info=image->blob;
1649 if (blob_info->type != BlobStream)
1650 return(WriteBlob(image,length,(
const unsigned char *) data));
1651 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1652 if (extent >= blob_info->extent)
1654 extent+=blob_info->quantum+length;
1655 blob_info->quantum<<=1;
1656 if (SetBlobExtent(image,extent) == MagickFalse)
1659 q=blob_info->data+blob_info->offset;
1660 (void) memcpy(q,data,length);
1661 blob_info->offset+=(MagickOffsetType) length;
1662 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1663 blob_info->length=(
size_t) blob_info->offset;
1664 return((ssize_t) length);
1667MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename,
1689 assert(image != (
const Image *) NULL);
1690 assert(image->signature == MagickCoreSignature);
1691 assert(filename != (
const char *) NULL);
1692 if (IsEventLogging() != MagickFalse)
1693 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1694 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1695 if (status == MagickFalse)
1698 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1699 "NotAuthorized",
"`%s'",filename);
1700 return(MagickFalse);
1703 if (LocaleCompare(filename,
"-") != 0)
1706 flags = O_RDONLY | O_BINARY;
1708#if defined(O_NOFOLLOW)
1709 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1710 if (status == MagickFalse)
1713 file=open_utf8(filename,flags,0);
1717 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1718 return(MagickFalse);
1720 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1721 if (status == MagickFalse)
1723 file=close_utf8(file);
1725 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1726 "NotAuthorized",
"`%s'",filename);
1727 return(MagickFalse);
1729 quantum=(size_t) MagickMaxBufferExtent;
1730 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1731 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1732 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1733 if (blob == (
unsigned char *) NULL)
1735 file=close_utf8(file);
1736 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1738 return(MagickFalse);
1742 count=read(file,blob,quantum);
1749 length=(size_t) count;
1750 count=WriteBlobStream(image,length,blob);
1751 if (count != (ssize_t) length)
1753 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1757 file=close_utf8(file);
1759 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1760 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1787MagickExport MagickBooleanType GetBlobError(
const Image *image)
1789 assert(image != (
const Image *) NULL);
1790 assert(image->signature == MagickCoreSignature);
1791 if (IsEventLogging() != MagickFalse)
1792 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1793 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1794 errno=image->blob->error_number;
1795 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1820MagickExport FILE *GetBlobFileHandle(
const Image *image)
1822 assert(image != (
const Image *) NULL);
1823 assert(image->signature == MagickCoreSignature);
1824 return(image->blob->file_info.file);
1849MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1851 assert(blob_info != (
BlobInfo *) NULL);
1852 (void) memset(blob_info,0,
sizeof(*blob_info));
1853 blob_info->type=UndefinedStream;
1854 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1855 blob_info->properties.st_mtime=GetMagickTime();
1856 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1857 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1858 blob_info->reference_count=1;
1859 blob_info->semaphore=AcquireSemaphoreInfo();
1860 blob_info->signature=MagickCoreSignature;
1885MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1887 assert(image != (
Image *) NULL);
1888 assert(image->signature == MagickCoreSignature);
1889 if (IsEventLogging() != MagickFalse)
1890 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1891 return(&image->blob->properties);
1917MagickExport MagickSizeType GetBlobSize(
const Image *image)
1920 *magick_restrict blob_info;
1925 assert(image != (
Image *) NULL);
1926 assert(image->signature == MagickCoreSignature);
1927 assert(image->blob != (
BlobInfo *) NULL);
1928 if (IsEventLogging() != MagickFalse)
1929 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1930 blob_info=image->blob;
1932 switch (blob_info->type)
1934 case UndefinedStream:
1935 case StandardStream:
1937 extent=blob_info->size;
1945 extent=(MagickSizeType) blob_info->properties.st_size;
1947 extent=blob_info->size;
1948 file_descriptor=fileno(blob_info->file_info.file);
1949 if (file_descriptor == -1)
1951 if (fstat(file_descriptor,&blob_info->properties) == 0)
1952 extent=(MagickSizeType) blob_info->properties.st_size;
1957 extent=blob_info->size;
1966 status=GetPathAttributes(image->filename,&blob_info->properties);
1967 if (status != MagickFalse)
1968 extent=(MagickSizeType) blob_info->properties.st_size;
1975 extent=(MagickSizeType) blob_info->length;
1980 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1981 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1986 offset=blob_info->custom_stream->teller(
1987 blob_info->custom_stream->data);
1988 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1989 blob_info->custom_stream->data);
1990 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1991 blob_info->custom_stream->data);
2021MagickExport
void *GetBlobStreamData(
const Image *image)
2023 assert(image != (
const Image *) NULL);
2024 assert(image->signature == MagickCoreSignature);
2025 return(image->blob->data);
2050MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2052 assert(image != (
const Image *) NULL);
2053 assert(image->signature == MagickCoreSignature);
2054 if (IsEventLogging() != MagickFalse)
2055 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2056 return(image->blob->stream);
2092MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2107 assert(image_info != (
const ImageInfo *) NULL);
2108 assert(image_info->signature == MagickCoreSignature);
2109 assert(image != (
Image *) NULL);
2110 assert(image->signature == MagickCoreSignature);
2112 assert(exception->signature == MagickCoreSignature);
2113 if (IsEventLogging() != MagickFalse)
2114 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2115 image_info->filename);
2117 blob=(
unsigned char *) NULL;
2118 blob_info=CloneImageInfo(image_info);
2119 blob_info->adjoin=MagickFalse;
2120 (void) SetImageInfo(blob_info,1,exception);
2121 if (*blob_info->magick !=
'\0')
2122 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2123 magick_info=GetMagickInfo(image->magick,exception);
2124 if (magick_info == (
const MagickInfo *) NULL)
2126 (void) ThrowMagickException(exception,GetMagickModule(),
2127 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2129 blob_info=DestroyImageInfo(blob_info);
2132 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2133 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2138 blob_info->length=0;
2139 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2140 sizeof(
unsigned char));
2141 if (blob_info->blob == NULL)
2142 (void) ThrowMagickException(exception,GetMagickModule(),
2143 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2146 (void) CloseBlob(image);
2147 image->blob->exempt=MagickTrue;
2148 image->blob->extent=0;
2149 *image->filename=
'\0';
2150 status=WriteImage(blob_info,image,exception);
2151 *length=image->blob->length;
2152 blob=DetachBlob(image->blob);
2153 if (blob != (
void *) NULL)
2155 if (status == MagickFalse)
2156 blob=RelinquishMagickMemory(blob);
2158 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2160 else if ((status == MagickFalse) && (image->blob->extent == 0))
2161 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2167 unique[MagickPathExtent];
2175 file=AcquireUniqueFileResource(unique);
2178 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2179 image_info->filename);
2183 blob_info->file=fdopen(file,
"wb");
2184 if (blob_info->file != (FILE *) NULL)
2186 (void) FormatLocaleString(image->filename,MagickPathExtent,
2187 "%s:%s",image->magick,unique);
2188 status=WriteImage(blob_info,image,exception);
2189 (void) fclose(blob_info->file);
2190 if (status != MagickFalse)
2191 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2193 (void) RelinquishUniqueFileResource(unique);
2196 blob_info=DestroyImageInfo(blob_info);
2228MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,
Image *image,
2241 assert(image_info != (
const ImageInfo *) NULL);
2242 assert(image_info->signature == MagickCoreSignature);
2243 assert(image != (
Image *) NULL);
2244 assert(image->signature == MagickCoreSignature);
2246 assert(image_info->custom_stream->signature == MagickCoreSignature);
2247 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2249 if (IsEventLogging() != MagickFalse)
2250 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2251 image_info->filename);
2252 clone_info=CloneImageInfo(image_info);
2253 clone_info->adjoin=MagickFalse;
2254 (void) SetImageInfo(clone_info,1,exception);
2255 if (*clone_info->magick !=
'\0')
2256 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2257 magick_info=GetMagickInfo(image->magick,exception);
2258 if (magick_info == (
const MagickInfo *) NULL)
2260 (void) ThrowMagickException(exception,GetMagickModule(),
2261 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2263 clone_info=DestroyImageInfo(clone_info);
2266 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2267 blob_support=GetMagickBlobSupport(magick_info);
2268 if ((blob_support != MagickFalse) &&
2269 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2271 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2272 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2273 blob_support=MagickFalse;
2275 if (blob_support != MagickFalse)
2280 (void) CloseBlob(image);
2281 *image->filename=
'\0';
2282 (void) WriteImage(clone_info,image,exception);
2287 unique[MagickPathExtent];
2299 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2301 if (blob == (
unsigned char *) NULL)
2303 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2304 image_info->filename);
2305 clone_info=DestroyImageInfo(clone_info);
2308 file=AcquireUniqueFileResource(unique);
2311 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2312 image_info->filename);
2313 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2314 clone_info=DestroyImageInfo(clone_info);
2317 clone_info->file=fdopen(file,
"wb+");
2318 if (clone_info->file != (FILE *) NULL)
2323 (void) FormatLocaleString(image->filename,MagickPathExtent,
2324 "%s:%s",image->magick,unique);
2325 status=WriteImage(clone_info,image,exception);
2326 if (status != MagickFalse)
2328 (void) fseek(clone_info->file,0,SEEK_SET);
2329 count=(ssize_t) MagickMaxBufferExtent;
2330 while (count == (ssize_t) MagickMaxBufferExtent)
2332 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2334 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2335 image_info->custom_stream->data);
2338 (void) fclose(clone_info->file);
2340 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2341 (void) RelinquishUniqueFileResource(unique);
2343 clone_info=DestroyImageInfo(clone_info);
2374MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
2399 assert(image != (
Image *) NULL);
2400 assert(image->signature == MagickCoreSignature);
2401 assert(image->blob != (
BlobInfo *) NULL);
2402 assert(image->blob->type != UndefinedStream);
2403 assert(filename != (
const char *) NULL);
2404 if (IsEventLogging() != MagickFalse)
2405 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2406 if (*filename ==
'\0')
2407 file=AcquireUniqueFileResource(filename);
2409 if (LocaleCompare(filename,
"-") == 0)
2410 file=fileno(stdout);
2412 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
2415 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2416 return(MagickFalse);
2418 quantum=(size_t) MagickMaxBufferExtent;
2419 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2420 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2421 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2422 if (buffer == (
unsigned char *) NULL)
2424 file=close_utf8(file)-1;
2425 (void) ThrowMagickException(exception,GetMagickModule(),
2426 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2427 return(MagickFalse);
2430 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2431 for (i=0; count > 0; )
2433 length=(size_t) count;
2434 for (i=0; i < length; i+=(size_t) count)
2436 count=write(file,p+i,(
size_t) (length-i));
2446 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2448 if (LocaleCompare(filename,
"-") != 0)
2449 file=close_utf8(file);
2450 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2451 if ((file == -1) || (i < length))
2454 file=close_utf8(file);
2455 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2456 return(MagickFalse);
2496MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,
Image *images,
2511 assert(image_info != (
const ImageInfo *) NULL);
2512 assert(image_info->signature == MagickCoreSignature);
2513 assert(images != (
Image *) NULL);
2514 assert(images->signature == MagickCoreSignature);
2516 if (IsEventLogging() != MagickFalse)
2517 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2518 image_info->filename);
2520 blob=(
unsigned char *) NULL;
2521 blob_info=CloneImageInfo(image_info);
2522 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2524 if (*blob_info->magick !=
'\0')
2525 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2526 magick_info=GetMagickInfo(images->magick,exception);
2527 if (magick_info == (
const MagickInfo *) NULL)
2529 (void) ThrowMagickException(exception,GetMagickModule(),
2530 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2532 blob_info=DestroyImageInfo(blob_info);
2535 if (GetMagickAdjoin(magick_info) == MagickFalse)
2537 blob_info=DestroyImageInfo(blob_info);
2538 return(ImageToBlob(image_info,images,length,exception));
2540 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2541 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2546 blob_info->length=0;
2547 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2548 sizeof(
unsigned char));
2549 if (blob_info->blob == (
void *) NULL)
2550 (void) ThrowMagickException(exception,GetMagickModule(),
2551 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2554 (void) CloseBlob(images);
2555 images->blob->exempt=MagickTrue;
2556 images->blob->extent=0;
2557 *images->filename=
'\0';
2558 status=WriteImages(blob_info,images,images->filename,exception);
2559 *length=images->blob->length;
2560 blob=DetachBlob(images->blob);
2561 if (blob != (
void *) NULL)
2563 if (status == MagickFalse)
2564 blob=RelinquishMagickMemory(blob);
2566 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2568 else if ((status == MagickFalse) && (images->blob->extent == 0))
2569 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2575 filename[MagickPathExtent],
2576 unique[MagickPathExtent];
2584 file=AcquireUniqueFileResource(unique);
2587 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2588 image_info->filename);
2592 blob_info->file=fdopen(file,
"wb");
2593 if (blob_info->file != (FILE *) NULL)
2595 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2596 images->magick,unique);
2597 status=WriteImages(blob_info,images,filename,exception);
2598 (void) fclose(blob_info->file);
2599 if (status != MagickFalse)
2600 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2602 (void) RelinquishUniqueFileResource(unique);
2605 blob_info=DestroyImageInfo(blob_info);
2637MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2650 assert(image_info != (
const ImageInfo *) NULL);
2651 assert(image_info->signature == MagickCoreSignature);
2652 assert(images != (
Image *) NULL);
2653 assert(images->signature == MagickCoreSignature);
2655 assert(image_info->custom_stream->signature == MagickCoreSignature);
2656 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2658 if (IsEventLogging() != MagickFalse)
2659 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2660 image_info->filename);
2661 clone_info=CloneImageInfo(image_info);
2662 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2664 if (*clone_info->magick !=
'\0')
2665 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2666 magick_info=GetMagickInfo(images->magick,exception);
2667 if (magick_info == (
const MagickInfo *) NULL)
2669 (void) ThrowMagickException(exception,GetMagickModule(),
2670 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2672 clone_info=DestroyImageInfo(clone_info);
2675 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2676 blob_support=GetMagickBlobSupport(magick_info);
2677 if ((blob_support != MagickFalse) &&
2678 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2680 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2681 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2682 blob_support=MagickFalse;
2684 if (blob_support != MagickFalse)
2689 (void) CloseBlob(images);
2690 *images->filename=
'\0';
2691 (void) WriteImages(clone_info,images,images->filename,exception);
2696 filename[MagickPathExtent],
2697 unique[MagickPathExtent];
2709 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2711 if (blob == (
unsigned char *) NULL)
2713 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2714 image_info->filename);
2715 clone_info=DestroyImageInfo(clone_info);
2718 file=AcquireUniqueFileResource(unique);
2721 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2722 image_info->filename);
2723 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2724 clone_info=DestroyImageInfo(clone_info);
2727 clone_info->file=fdopen(file,
"wb+");
2728 if (clone_info->file != (FILE *) NULL)
2733 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2734 images->magick,unique);
2735 status=WriteImages(clone_info,images,filename,exception);
2736 if (status != MagickFalse)
2738 (void) fseek(clone_info->file,0,SEEK_SET);
2739 count=(ssize_t) MagickMaxBufferExtent;
2740 while (count == (ssize_t) MagickMaxBufferExtent)
2742 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2744 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2745 image_info->custom_stream->data);
2748 (void) fclose(clone_info->file);
2750 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2751 (void) RelinquishUniqueFileResource(unique);
2753 clone_info=DestroyImageInfo(clone_info);
2789MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2793 filename[MagickPathExtent];
2822 assert(image_info != (
ImageInfo *) NULL);
2823 assert(image_info->signature == MagickCoreSignature);
2824 assert(image != (
Image *) NULL);
2825 assert(image->signature == MagickCoreSignature);
2826 assert(inject_image != (
Image *) NULL);
2827 assert(inject_image->signature == MagickCoreSignature);
2829 if (IsEventLogging() != MagickFalse)
2830 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2831 unique_file=(FILE *) NULL;
2832 file=AcquireUniqueFileResource(filename);
2834 unique_file=fdopen(file,
"wb");
2835 if ((file == -1) || (unique_file == (FILE *) NULL))
2837 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2838 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2840 return(MagickFalse);
2842 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2843 if (byte_image == (
Image *) NULL)
2845 (void) fclose(unique_file);
2846 (void) RelinquishUniqueFileResource(filename);
2847 return(MagickFalse);
2849 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2851 DestroyBlob(byte_image);
2852 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2853 write_info=CloneImageInfo(image_info);
2854 SetImageInfoFile(write_info,unique_file);
2855 status=WriteImage(write_info,byte_image,exception);
2856 write_info=DestroyImageInfo(write_info);
2857 byte_image=DestroyImage(byte_image);
2858 (void) fclose(unique_file);
2859 if (status == MagickFalse)
2861 (void) RelinquishUniqueFileResource(filename);
2862 return(MagickFalse);
2867 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2870 (void) RelinquishUniqueFileResource(filename);
2871 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2872 image_info->filename);
2873 return(MagickFalse);
2875 quantum=(size_t) MagickMaxBufferExtent;
2876 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2877 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2878 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2879 if (buffer == (
unsigned char *) NULL)
2881 (void) RelinquishUniqueFileResource(filename);
2882 file=close_utf8(file);
2883 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2888 ssize_t count = read(file,buffer,quantum);
2895 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2898 file=close_utf8(file);
2900 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2901 (void) RelinquishUniqueFileResource(filename);
2902 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2928MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2930 assert(image != (
const Image *) NULL);
2931 assert(image->signature == MagickCoreSignature);
2932 if (IsEventLogging() != MagickFalse)
2933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2934 return(image->blob->exempt);
2959MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2962 *magick_restrict blob_info;
2964 assert(image != (
const Image *) NULL);
2965 assert(image->signature == MagickCoreSignature);
2966 if (IsEventLogging() != MagickFalse)
2967 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2968 blob_info=image->blob;
2969 switch (blob_info->type)
2978 if (blob_info->file_info.file == (FILE *) NULL)
2979 return(MagickFalse);
2980 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2981 return(status == -1 ? MagickFalse : MagickTrue);
2985#if defined(MAGICKCORE_ZLIB_DELEGATE)
2989 if (blob_info->file_info.gzfile == (gzFile) NULL)
2990 return(MagickFalse);
2991 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2992 return(offset < 0 ? MagickFalse : MagickTrue);
2997 case UndefinedStream:
3001 case StandardStream:
3005 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3006 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3013 return(MagickFalse);
3038MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3040 assert(image != (
const Image *) NULL);
3041 assert(image->signature == MagickCoreSignature);
3042 if (IsEventLogging() != MagickFalse)
3043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3044 return(image->blob->temporary);
3076MagickExport
void *MapBlob(
int file,
const MapMode mode,
3077 const MagickOffsetType offset,
const size_t length)
3079#if defined(MAGICKCORE_HAVE_MMAP)
3092#if defined(MAP_ANONYMOUS)
3093 flags|=MAP_ANONYMOUS;
3102 protection=PROT_READ;
3108 protection=PROT_WRITE;
3114 protection=PROT_READ | PROT_WRITE;
3119#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3120 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3122 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3123 if (map == MAP_FAILED)
3124 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3126 if (map == MAP_FAILED)
3163MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3172 assert(buffer != (
unsigned char *) NULL);
3179 *buffer++=(
unsigned char) c;
3183 *buffer++=(
unsigned char) c;
3213MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3221 assert(p != (
unsigned char *) NULL);
3228 *p++=(
unsigned char) c;
3264static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3276 size=MagickMinBufferExtent;
3277 option=GetImageOption(image_info,
"stream:buffer-size");
3278 if (option != (
const char *) NULL)
3279 size=StringToUnsignedLong(option);
3280 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3281 _IONBF : _IOFBF,size);
3282 return(status == 0 ? MagickTrue : MagickFalse);
3285#if defined(MAGICKCORE_ZLIB_DELEGATE)
3286static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3288#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3289 return(gzopen(path,mode));
3297 path_wide=create_wchar_path(path);
3298 if (path_wide == (
wchar_t *) NULL)
3299 return((gzFile) NULL);
3300 file=gzopen_w(path_wide,mode);
3301 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3307MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3311 *magick_restrict blob_info;
3314 extension[MagickPathExtent],
3315 filename[MagickPathExtent];
3329 assert(image_info != (
ImageInfo *) NULL);
3330 assert(image_info->signature == MagickCoreSignature);
3331 assert(image != (
Image *) NULL);
3332 assert(image->signature == MagickCoreSignature);
3333 if (IsEventLogging() != MagickFalse)
3334 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3335 image_info->filename);
3336 blob_info=image->blob;
3337 if (image_info->blob != (
void *) NULL)
3339 if (image_info->stream != (StreamHandler) NULL)
3340 blob_info->stream=(StreamHandler) image_info->stream;
3341 AttachBlob(blob_info,image_info->blob,image_info->length);
3345 (*image->filename ==
'\0'))
3347 blob_info->type=CustomStream;
3348 blob_info->custom_stream=image_info->custom_stream;
3351 (void) DetachBlob(blob_info);
3352 blob_info->mode=mode;
3361 case ReadBinaryBlobMode:
3363 flags=O_RDONLY | O_BINARY;
3369 flags=O_WRONLY | O_CREAT | O_TRUNC;
3373 case WriteBinaryBlobMode:
3375 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3379 case AppendBlobMode:
3381 flags=O_WRONLY | O_CREAT | O_APPEND;
3385 case AppendBinaryBlobMode:
3387 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3399 blob_info->synchronize=image_info->synchronize;
3400 if (image_info->stream != (StreamHandler) NULL)
3402 blob_info->stream=image_info->stream;
3405 blob_info->type=FifoStream;
3413 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3414 rights=ReadPolicyRights;
3416 rights=WritePolicyRights;
3417 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3420 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3421 "NotAuthorized",
"`%s'",filename);
3422 return(MagickFalse);
3424 if ((LocaleCompare(filename,
"-") == 0) ||
3425 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3427 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3428#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3429 if (strchr(type,
'b') != (
char *) NULL)
3430 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3432 blob_info->type=StandardStream;
3433 blob_info->exempt=MagickTrue;
3434 return(SetStreamBuffering(image_info,blob_info));
3436 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3437 (IsGeometry(filename+3) != MagickFalse))
3444 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3445 if (blob_info->file_info.file == (FILE *) NULL)
3447 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3448 return(MagickFalse);
3450#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3451 if (strchr(type,
'b') != (
char *) NULL)
3452 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3454 blob_info->type=FileStream;
3455 blob_info->exempt=MagickTrue;
3456 return(SetStreamBuffering(image_info,blob_info));
3458#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3459 if (*filename ==
'|')
3462 fileMode[MagickPathExtent],
3470 (void) signal(SIGPIPE,SIG_IGN);
3474 sanitize_command=SanitizeString(filename+1);
3475 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3476 sanitize_command=DestroyString(sanitize_command);
3477 if (blob_info->file_info.file == (FILE *) NULL)
3479 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3480 return(MagickFalse);
3482 blob_info->type=PipeStream;
3483 blob_info->exempt=MagickTrue;
3484 return(SetStreamBuffering(image_info,blob_info));
3487 status=GetPathAttributes(filename,&blob_info->properties);
3488#if defined(S_ISFIFO)
3489 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3491 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3492 if (blob_info->file_info.file == (FILE *) NULL)
3494 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3495 return(MagickFalse);
3497 blob_info->type=FileStream;
3498 blob_info->exempt=MagickTrue;
3499 return(SetStreamBuffering(image_info,blob_info));
3502 GetPathComponent(image->filename,ExtensionPath,extension);
3505 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3506 if ((image_info->adjoin == MagickFalse) ||
3507 (strchr(filename,
'%') != (
char *) NULL))
3512 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3513 image->scene,filename,exception);
3514 if ((LocaleCompare(filename,image->filename) == 0) &&
3515 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
3516 (GetNextImageInList(image) != (
Image *) NULL)))
3519 path[MagickPathExtent];
3521 GetPathComponent(image->filename,RootPath,path);
3522 if (*extension ==
'\0')
3523 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3524 path,(
double) image->scene);
3526 (
void) FormatLocaleString(filename,MagickPathExtent,
3527 "%s-%.20g.%s",path,(
double) image->scene,extension);
3529 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3532 if (image_info->file != (FILE *) NULL)
3534 blob_info->file_info.file=image_info->file;
3535 blob_info->type=FileStream;
3536 blob_info->exempt=MagickTrue;
3544 blob_info->file_info.file=(FILE *) NULL;
3545#if defined(O_NOFOLLOW)
3546 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3547 if (status == MagickFalse)
3550 file=open_utf8(filename,flags,0);
3552 blob_info->file_info.file=fdopen(file,type);
3553 if (blob_info->file_info.file != (FILE *) NULL)
3561 blob_info->type=FileStream;
3562 (void) SetStreamBuffering(image_info,blob_info);
3563 (void) memset(magick,0,
sizeof(magick));
3564 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3565 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3566#if defined(MAGICKCORE_POSIX_SUPPORT)
3567 (void) fflush(blob_info->file_info.file);
3569 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3570 " read %.20g magic header bytes",(double) count);
3571#if defined(MAGICKCORE_ZLIB_DELEGATE)
3572 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3573 ((
int) magick[2] == 0x08))
3576 gzfile = gzopen_utf8(filename,
"rb");
3578 if (gzfile != (gzFile) NULL)
3580 if (blob_info->file_info.file != (FILE *) NULL)
3581 (void) fclose(blob_info->file_info.file);
3582 blob_info->file_info.file=(FILE *) NULL;
3583 blob_info->file_info.gzfile=gzfile;
3584 blob_info->type=ZipStream;
3588#if defined(MAGICKCORE_BZLIB_DELEGATE)
3589 if (strncmp((
char *) magick,
"BZh",3) == 0)
3592 *bzfile = BZ2_bzopen(filename,
"r");
3594 if (bzfile != (BZFILE *) NULL)
3596 if (blob_info->file_info.file != (FILE *) NULL)
3597 (void) fclose(blob_info->file_info.file);
3598 blob_info->file_info.file=(FILE *) NULL;
3599 blob_info->file_info.bzfile=bzfile;
3600 blob_info->type=BZipStream;
3604 if (blob_info->type == FileStream)
3615 sans_exception=AcquireExceptionInfo();
3616 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3617 sans_exception=DestroyExceptionInfo(sans_exception);
3618 length=(size_t) blob_info->properties.st_size;
3619 if ((magick_info != (
const MagickInfo *) NULL) &&
3620 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3621 (length > MagickMaxBufferExtent) &&
3622 (AcquireMagickResource(MapResource,length) != MagickFalse))
3627 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3629 if (blob == (
void *) NULL)
3630 RelinquishMagickResource(MapResource,length);
3636 if (image_info->file != (FILE *) NULL)
3637 blob_info->exempt=MagickFalse;
3640 (void) fclose(blob_info->file_info.file);
3641 blob_info->file_info.file=(FILE *) NULL;
3643 AttachBlob(blob_info,blob,length);
3644 blob_info->mapped=MagickTrue;
3651#if defined(MAGICKCORE_ZLIB_DELEGATE)
3652 if ((LocaleCompare(extension,
"gz") == 0) ||
3653 (LocaleCompare(extension,
"wmz") == 0) ||
3654 (LocaleCompare(extension,
"svgz") == 0))
3656 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3657 if (blob_info->file_info.gzfile != (gzFile) NULL)
3658 blob_info->type=ZipStream;
3662#if defined(MAGICKCORE_BZLIB_DELEGATE)
3663 if (LocaleCompare(extension,
"bz2") == 0)
3665 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3666 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3667 blob_info->type=BZipStream;
3675 blob_info->file_info.file=(FILE *) NULL;
3676#if defined(O_NOFOLLOW)
3677 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
3679 if (status == MagickFalse)
3682 file=open_utf8(filename,flags,0666);
3684 blob_info->file_info.file=fdopen(file,type);
3685 if (blob_info->file_info.file != (FILE *) NULL)
3687 blob_info->type=FileStream;
3688 (void) SetStreamBuffering(image_info,blob_info);
3691 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3694 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3695 "NotAuthorized",
"`%s'",filename);
3696 return(MagickFalse);
3698 blob_info->status=0;
3699 blob_info->error_number=0;
3700 if (blob_info->type != UndefinedStream)
3701 blob_info->size=GetBlobSize(image);
3704 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3705 return(MagickFalse);
3744#if defined(__cplusplus) || defined(c_plusplus)
3748static size_t PingStream(
const Image *magick_unused(image),
3749 const void *magick_unused(pixels),
const size_t columns)
3751 magick_unreferenced(image);
3752 magick_unreferenced(pixels);
3756#if defined(__cplusplus) || defined(c_plusplus)
3760MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3776 assert(image_info != (
ImageInfo *) NULL);
3777 assert(image_info->signature == MagickCoreSignature);
3779 if (IsEventLogging() != MagickFalse)
3780 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3781 image_info->filename);
3782 if ((blob == (
const void *) NULL) || (length == 0))
3784 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3785 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3786 return((
Image *) NULL);
3788 ping_info=CloneImageInfo(image_info);
3789 ping_info->blob=(
void *) blob;
3790 ping_info->length=length;
3791 ping_info->ping=MagickTrue;
3792 if (*ping_info->magick ==
'\0')
3793 (void) SetImageInfo(ping_info,0,exception);
3794 magick_info=GetMagickInfo(ping_info->magick,exception);
3795 if (magick_info == (
const MagickInfo *) NULL)
3797 (void) ThrowMagickException(exception,GetMagickModule(),
3798 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3800 ping_info=DestroyImageInfo(ping_info);
3801 return((
Image *) NULL);
3803 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3806 filename[MagickPathExtent];
3811 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3812 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3813 ping_info->magick,filename);
3814 image=ReadStream(ping_info,&PingStream,exception);
3815 if (image != (
Image *) NULL)
3816 (void) DetachBlob(image->blob);
3817 ping_info=DestroyImageInfo(ping_info);
3823 ping_info->blob=(
void *) NULL;
3824 ping_info->length=0;
3825 *ping_info->filename=
'\0';
3826 status=BlobToFile(ping_info->filename,blob,length,exception);
3827 if (status == MagickFalse)
3829 (void) RelinquishUniqueFileResource(ping_info->filename);
3830 ping_info=DestroyImageInfo(ping_info);
3831 return((
Image *) NULL);
3833 clone_info=CloneImageInfo(ping_info);
3834 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3835 ping_info->magick,ping_info->filename);
3836 image=ReadStream(clone_info,&PingStream,exception);
3837 if (image != (
Image *) NULL)
3845 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3847 (void) CopyMagickString(images->filename,image_info->filename,
3849 (void) CopyMagickString(images->magick_filename,image_info->filename,
3851 (void) CopyMagickString(images->magick,magick_info->name,
3853 images=GetNextImageInList(images);
3856 clone_info=DestroyImageInfo(clone_info);
3857 (void) RelinquishUniqueFileResource(ping_info->filename);
3858 ping_info=DestroyImageInfo(ping_info);
3893MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
void *data)
3896 *magick_restrict blob_info;
3907 assert(image != (
Image *) NULL);
3908 assert(image->signature == MagickCoreSignature);
3909 assert(image->blob != (
BlobInfo *) NULL);
3910 assert(image->blob->type != UndefinedStream);
3913 assert(data != (
void *) NULL);
3914 blob_info=image->blob;
3916 q=(
unsigned char *) data;
3917 switch (blob_info->type)
3919 case UndefinedStream:
3921 case StandardStream:
3929 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3934 c=getc(blob_info->file_info.file);
3937 *q++=(
unsigned char) c;
3943 c=getc(blob_info->file_info.file);
3946 *q++=(
unsigned char) c;
3952 c=getc(blob_info->file_info.file);
3955 *q++=(
unsigned char) c;
3961 c=getc(blob_info->file_info.file);
3964 *q++=(
unsigned char) c;
3971 if ((count != (ssize_t) length) &&
3972 (ferror(blob_info->file_info.file) != 0))
3973 ThrowBlobException(blob_info);
3978#if defined(MAGICKCORE_ZLIB_DELEGATE)
3989 for (i=0; i < length; i+=(size_t) count)
3991 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3992 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4005 c=gzgetc(blob_info->file_info.gzfile);
4008 *q++=(
unsigned char) c;
4014 c=gzgetc(blob_info->file_info.gzfile);
4017 *q++=(
unsigned char) c;
4023 c=gzgetc(blob_info->file_info.gzfile);
4026 *q++=(
unsigned char) c;
4032 c=gzgetc(blob_info->file_info.gzfile);
4035 *q++=(
unsigned char) c;
4042 (void) gzerror(blob_info->file_info.gzfile,&status);
4043 if ((count != (ssize_t) length) && (status != Z_OK))
4044 ThrowBlobException(blob_info);
4045 if (blob_info->eof == MagickFalse)
4046 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4053#if defined(MAGICKCORE_BZLIB_DELEGATE)
4060 for (i=0; i < length; i+=(size_t) count)
4062 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4063 MagickMin(length-i,MagickMaxBufferExtent));
4073 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4074 if ((count != (ssize_t) length) && (status != BZ_OK))
4075 ThrowBlobException(blob_info);
4086 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4088 blob_info->eof=MagickTrue;
4091 p=blob_info->data+blob_info->offset;
4092 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4093 blob_info->length-blob_info->offset);
4094 blob_info->offset+=count;
4095 if (count != (ssize_t) length)
4096 blob_info->eof=MagickTrue;
4097 (void) memcpy(q,p,(
size_t) count);
4102 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4103 count=blob_info->custom_stream->reader(q,length,
4104 blob_info->custom_stream->data);
4133MagickExport
int ReadBlobByte(
Image *image)
4136 *magick_restrict blob_info;
4141 assert(image != (
Image *) NULL);
4142 assert(image->signature == MagickCoreSignature);
4143 assert(image->blob != (
BlobInfo *) NULL);
4144 assert(image->blob->type != UndefinedStream);
4145 blob_info=image->blob;
4146 switch (blob_info->type)
4148 case StandardStream:
4152 c=getc(blob_info->file_info.file);
4155 if (ferror(blob_info->file_info.file) != 0)
4156 ThrowBlobException(blob_info);
4163 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4165 blob_info->eof=MagickTrue;
4168 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4169 blob_info->offset++;
4180 count=ReadBlob(image,1,buffer);
4213MagickExport
double ReadBlobDouble(
Image *image)
4224 quantum.double_value=0.0;
4225 quantum.unsigned_value=ReadBlobLongLong(image);
4226 return(quantum.double_value);
4252MagickExport
float ReadBlobFloat(
Image *image)
4263 quantum.float_value=0.0;
4264 quantum.unsigned_value=ReadBlobLong(image);
4265 return(quantum.float_value);
4291MagickExport
unsigned int ReadBlobLong(
Image *image)
4305 assert(image != (
Image *) NULL);
4306 assert(image->signature == MagickCoreSignature);
4308 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4311 if (image->endian == LSBEndian)
4313 value=(
unsigned int) (*p++);
4314 value|=(
unsigned int) (*p++) << 8;
4315 value|=(
unsigned int) (*p++) << 16;
4316 value|=(
unsigned int) (*p++) << 24;
4319 value=(
unsigned int) (*p++) << 24;
4320 value|=(
unsigned int) (*p++) << 16;
4321 value|=(
unsigned int) (*p++) << 8;
4322 value|=(
unsigned int) (*p++);
4349MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
4363 assert(image != (
Image *) NULL);
4364 assert(image->signature == MagickCoreSignature);
4366 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4368 return(MagickULLConstant(0));
4369 if (image->endian == LSBEndian)
4371 value=(MagickSizeType) (*p++);
4372 value|=(MagickSizeType) (*p++) << 8;
4373 value|=(MagickSizeType) (*p++) << 16;
4374 value|=(MagickSizeType) (*p++) << 24;
4375 value|=(MagickSizeType) (*p++) << 32;
4376 value|=(MagickSizeType) (*p++) << 40;
4377 value|=(MagickSizeType) (*p++) << 48;
4378 value|=(MagickSizeType) (*p++) << 56;
4381 value=(MagickSizeType) (*p++) << 56;
4382 value|=(MagickSizeType) (*p++) << 48;
4383 value|=(MagickSizeType) (*p++) << 40;
4384 value|=(MagickSizeType) (*p++) << 32;
4385 value|=(MagickSizeType) (*p++) << 24;
4386 value|=(MagickSizeType) (*p++) << 16;
4387 value|=(MagickSizeType) (*p++) << 8;
4388 value|=(MagickSizeType) (*p++);
4415MagickExport
unsigned short ReadBlobShort(
Image *image)
4429 assert(image != (
Image *) NULL);
4430 assert(image->signature == MagickCoreSignature);
4432 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4434 return((
unsigned short) 0U);
4435 if (image->endian == LSBEndian)
4437 value=(
unsigned short) (*p++);
4438 value|=(
unsigned short) (*p++) << 8;
4441 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4442 value|=(
unsigned short) (*p++);
4469MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
4483 assert(image != (
Image *) NULL);
4484 assert(image->signature == MagickCoreSignature);
4486 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4489 value=(
unsigned int) (*p++);
4490 value|=(
unsigned int) (*p++) << 8;
4491 value|=(
unsigned int) (*p++) << 16;
4492 value|=(
unsigned int) (*p++) << 24;
4519MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
4530 quantum.unsigned_value=ReadBlobLSBLong(image);
4531 return(quantum.signed_value);
4557MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
4571 assert(image != (
Image *) NULL);
4572 assert(image->signature == MagickCoreSignature);
4574 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4576 return((
unsigned short) 0U);
4577 value=(
unsigned short) (*p++);
4578 value|=(
unsigned short) (*p++) << 8;
4605MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
4616 quantum.unsigned_value=ReadBlobLSBShort(image);
4617 return(quantum.signed_value);
4643MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
4657 assert(image != (
Image *) NULL);
4658 assert(image->signature == MagickCoreSignature);
4660 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4663 value=(
unsigned int) (*p++) << 24;
4664 value|=(
unsigned int) (*p++) << 16;
4665 value|=(
unsigned int) (*p++) << 8;
4666 value|=(
unsigned int) (*p++);
4693MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
4707 assert(image != (
Image *) NULL);
4708 assert(image->signature == MagickCoreSignature);
4710 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4712 return(MagickULLConstant(0));
4713 value=(MagickSizeType) (*p++) << 56;
4714 value|=(MagickSizeType) (*p++) << 48;
4715 value|=(MagickSizeType) (*p++) << 40;
4716 value|=(MagickSizeType) (*p++) << 32;
4717 value|=(MagickSizeType) (*p++) << 24;
4718 value|=(MagickSizeType) (*p++) << 16;
4719 value|=(MagickSizeType) (*p++) << 8;
4720 value|=(MagickSizeType) (*p++);
4747MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
4761 assert(image != (
Image *) NULL);
4762 assert(image->signature == MagickCoreSignature);
4764 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4766 return((
unsigned short) 0U);
4767 value=(
unsigned short) ((*p++) << 8);
4768 value|=(
unsigned short) (*p++);
4769 return((
unsigned short) (value & 0xffff));
4795MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4806 quantum.unsigned_value=ReadBlobMSBLong(image);
4807 return(quantum.signed_value);
4833MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4844 quantum.unsigned_value=ReadBlobMSBShort(image);
4845 return(quantum.signed_value);
4871MagickExport
signed int ReadBlobSignedLong(
Image *image)
4882 quantum.unsigned_value=ReadBlobLong(image);
4883 return(quantum.signed_value);
4909MagickExport
signed short ReadBlobSignedShort(
Image *image)
4920 quantum.unsigned_value=ReadBlobShort(image);
4921 return(quantum.signed_value);
4959MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4960 const size_t length,
void *magick_restrict data,ssize_t *count)
4963 *magick_restrict blob_info;
4965 assert(image != (
Image *) NULL);
4966 assert(image->signature == MagickCoreSignature);
4967 assert(image->blob != (
BlobInfo *) NULL);
4968 assert(image->blob->type != UndefinedStream);
4969 assert(count != (ssize_t *) NULL);
4970 blob_info=image->blob;
4971 if (blob_info->type != BlobStream)
4973 assert(data != NULL);
4974 *count=ReadBlob(image,length,(
unsigned char *) data);
4977 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4980 blob_info->eof=MagickTrue;
4983 data=blob_info->data+blob_info->offset;
4984 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4985 blob_info->length-blob_info->offset);
4986 blob_info->offset+=(*count);
4987 if (*count != (ssize_t) length)
4988 blob_info->eof=MagickTrue;
5017MagickExport
char *ReadBlobString(
Image *image,
char *
string)
5020 *magick_restrict blob_info;
5028 assert(image != (
Image *) NULL);
5029 assert(image->signature == MagickCoreSignature);
5030 assert(image->blob != (
BlobInfo *) NULL);
5031 assert(image->blob->type != UndefinedStream);
5032 if (IsEventLogging() != MagickFalse)
5033 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5035 blob_info=image->blob;
5036 switch (blob_info->type)
5038 case UndefinedStream:
5040 case StandardStream:
5043 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5044 if (p == (
char *) NULL)
5046 if (ferror(blob_info->file_info.file) != 0)
5047 ThrowBlobException(blob_info);
5048 return((
char *) NULL);
5055#if defined(MAGICKCORE_ZLIB_DELEGATE)
5056 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5057 if (p == (
char *) NULL)
5060 (void) gzerror(blob_info->file_info.gzfile,&status);
5062 ThrowBlobException(blob_info);
5063 return((
char *) NULL);
5073 c=ReadBlobByte(image);
5076 blob_info->eof=MagickTrue;
5082 }
while (i < (MaxTextExtent-2));
5090 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5093 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5095 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5096 return((
char *) NULL);
5126 assert(blob->signature == MagickCoreSignature);
5127 if (IsEventLogging() != MagickFalse)
5128 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5129 LockSemaphoreInfo(blob->semaphore);
5130 blob->reference_count++;
5131 UnlockSemaphoreInfo(blob->semaphore);
5168MagickExport MagickOffsetType SeekBlob(
Image *image,
5169 const MagickOffsetType offset,
const int whence)
5172 *magick_restrict blob_info;
5174 assert(image != (
Image *) NULL);
5175 assert(image->signature == MagickCoreSignature);
5176 assert(image->blob != (
BlobInfo *) NULL);
5177 assert(image->blob->type != UndefinedStream);
5178 if (IsEventLogging() != MagickFalse)
5179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5180 blob_info=image->blob;
5181 switch (blob_info->type)
5183 case UndefinedStream:
5185 case StandardStream:
5190 if ((offset < 0) && (whence == SEEK_SET))
5192 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5194 blob_info->offset=TellBlob(image);
5199#if defined(MAGICKCORE_ZLIB_DELEGATE)
5200 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
5203 blob_info->offset=TellBlob(image);
5219 blob_info->offset=offset;
5224 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5225 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5230 if ((blob_info->offset+offset) < 0)
5232 blob_info->offset+=offset;
5237 if (((MagickOffsetType) blob_info->length+offset) < 0)
5239 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5243 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5245 blob_info->eof=MagickFalse;
5252 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5254 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5255 blob_info->custom_stream->data);
5259 return(blob_info->offset);
5287MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
5289 assert(image != (
const Image *) NULL);
5290 assert(image->signature == MagickCoreSignature);
5291 if (IsEventLogging() != MagickFalse)
5292 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5293 image->blob->exempt=exempt;
5322MagickExport MagickBooleanType SetBlobExtent(
Image *image,
5323 const MagickSizeType extent)
5326 *magick_restrict blob_info;
5328 assert(image != (
Image *) NULL);
5329 assert(image->signature == MagickCoreSignature);
5330 assert(image->blob != (
BlobInfo *) NULL);
5331 assert(image->blob->type != UndefinedStream);
5332 if (IsEventLogging() != MagickFalse)
5333 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5334 blob_info=image->blob;
5335 switch (blob_info->type)
5337 case UndefinedStream:
5339 case StandardStream:
5340 return(MagickFalse);
5349 if (extent != (MagickSizeType) ((off_t) extent))
5350 return(MagickFalse);
5351 offset=SeekBlob(image,0,SEEK_END);
5353 return(MagickFalse);
5354 if ((MagickSizeType) offset >= extent)
5356 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5359 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5360 blob_info->file_info.file);
5361#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5362 if (blob_info->synchronize != MagickFalse)
5367 file=fileno(blob_info->file_info.file);
5368 if ((file == -1) || (offset < 0))
5369 return(MagickFalse);
5370 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5373 offset=SeekBlob(image,offset,SEEK_SET);
5375 return(MagickFalse);
5380 return(MagickFalse);
5382 return(MagickFalse);
5384 return(MagickFalse);
5387 if (extent != (MagickSizeType) ((
size_t) extent))
5388 return(MagickFalse);
5389 if (blob_info->mapped != MagickFalse)
5397 (void) UnmapBlob(blob_info->data,blob_info->length);
5398 RelinquishMagickResource(MapResource,blob_info->length);
5399 if (extent != (MagickSizeType) ((off_t) extent))
5400 return(MagickFalse);
5401 offset=SeekBlob(image,0,SEEK_END);
5403 return(MagickFalse);
5404 if ((MagickSizeType) offset >= extent)
5406 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5407 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5408 blob_info->file_info.file);
5409#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5410 if (blob_info->synchronize != MagickFalse)
5415 file=fileno(blob_info->file_info.file);
5416 if ((file == -1) || (offset < 0))
5417 return(MagickFalse);
5418 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5422 offset=SeekBlob(image,offset,SEEK_SET);
5424 return(MagickFalse);
5425 (void) AcquireMagickResource(MapResource,extent);
5426 blob_info->data=(
unsigned char*) MapBlob(fileno(
5427 blob_info->file_info.file),WriteMode,0,(size_t) extent);
5428 blob_info->extent=(size_t) extent;
5429 blob_info->length=(size_t) extent;
5430 (void) SyncBlob(image);
5433 blob_info->extent=(size_t) extent;
5434 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5435 blob_info->extent+1,
sizeof(*blob_info->data));
5436 (void) SyncBlob(image);
5437 if (blob_info->data == (
unsigned char *) NULL)
5439 (void) DetachBlob(blob_info);
5440 return(MagickFalse);
5478 assert(custom_stream->signature == MagickCoreSignature);
5479 custom_stream->data=data;
5508 CustomStreamHandler reader)
5511 assert(custom_stream->signature == MagickCoreSignature);
5512 custom_stream->reader=reader;
5541 CustomStreamSeeker seeker)
5544 assert(custom_stream->signature == MagickCoreSignature);
5545 custom_stream->seeker=seeker;
5574 CustomStreamTeller teller)
5577 assert(custom_stream->signature == MagickCoreSignature);
5578 custom_stream->teller=teller;
5607 CustomStreamHandler writer)
5610 assert(custom_stream->signature == MagickCoreSignature);
5611 custom_stream->writer=writer;
5638static int SyncBlob(
const Image *image)
5641 *magick_restrict blob_info;
5646 assert(image != (
Image *) NULL);
5647 assert(image->signature == MagickCoreSignature);
5648 assert(image->blob != (
BlobInfo *) NULL);
5649 if (IsEventLogging() != MagickFalse)
5650 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5651 if (EOFBlob(image) != 0)
5653 blob_info=image->blob;
5655 switch (blob_info->type)
5657 case UndefinedStream:
5658 case StandardStream:
5663 status=fflush(blob_info->file_info.file);
5668#if defined(MAGICKCORE_ZLIB_DELEGATE)
5669 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5675#if defined(MAGICKCORE_BZLIB_DELEGATE)
5676 status=BZ2_bzflush(blob_info->file_info.bzfile);
5712MagickExport MagickOffsetType TellBlob(
const Image *image)
5715 *magick_restrict blob_info;
5720 assert(image != (
Image *) NULL);
5721 assert(image->signature == MagickCoreSignature);
5722 assert(image->blob != (
BlobInfo *) NULL);
5723 assert(image->blob->type != UndefinedStream);
5724 if (IsEventLogging() != MagickFalse)
5725 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5726 blob_info=image->blob;
5728 switch (blob_info->type)
5730 case UndefinedStream:
5731 case StandardStream:
5735 offset=ftell(blob_info->file_info.file);
5742#if defined(MAGICKCORE_ZLIB_DELEGATE)
5743 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5753 offset=blob_info->offset;
5758 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5759 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5791MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5793#if defined(MAGICKCORE_HAVE_MMAP)
5797 status=munmap(map,length);
5798 return(status == -1 ? MagickFalse : MagickTrue);
5802 return(MagickFalse);
5834MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
5838 *magick_restrict blob_info;
5852 assert(image != (
Image *) NULL);
5853 assert(image->signature == MagickCoreSignature);
5854 assert(image->blob != (
BlobInfo *) NULL);
5855 assert(image->blob->type != UndefinedStream);
5858 assert(data != (
const void *) NULL);
5859 blob_info=image->blob;
5861 p=(
const unsigned char *) data;
5862 q=(
unsigned char *) data;
5863 switch (blob_info->type)
5865 case UndefinedStream:
5867 case StandardStream:
5875 count=(ssize_t) fwrite((
const char *) data,1,length,
5876 blob_info->file_info.file);
5881 c=putc((
int) *p++,blob_info->file_info.file);
5889 c=putc((
int) *p++,blob_info->file_info.file);
5897 c=putc((
int) *p++,blob_info->file_info.file);
5905 c=putc((
int) *p++,blob_info->file_info.file);
5914 if ((count != (ssize_t) length) &&
5915 (ferror(blob_info->file_info.file) != 0))
5916 ThrowBlobException(blob_info);
5921#if defined(MAGICKCORE_ZLIB_DELEGATE)
5932 for (i=0; i < length; i+=(size_t) count)
5934 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5935 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5948 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5956 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5964 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5972 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5982 (void) gzerror(blob_info->file_info.gzfile,&status);
5983 if ((count != (ssize_t) length) && (status != Z_OK))
5984 ThrowBlobException(blob_info);
5990#if defined(MAGICKCORE_BZLIB_DELEGATE)
5997 for (i=0; i < length; i+=(size_t) count)
5999 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6000 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6010 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6011 if ((count != (ssize_t) length) && (status != BZ_OK))
6012 ThrowBlobException(blob_info);
6018 count=(ssize_t) blob_info->stream(image,data,length);
6026 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6027 if (extent >= blob_info->extent)
6029 if (blob_info->mapped != MagickFalse)
6031 blob_info->extent=extent+blob_info->quantum+length;
6032 blob_info->quantum<<=1;
6033 blob_info->data=(
unsigned char *) ResizeQuantumMemory(
6034 blob_info->data,blob_info->extent+1,
sizeof(*blob_info->data));
6035 (void) SyncBlob(image);
6036 if (blob_info->data == (
unsigned char *) NULL)
6038 (void) DetachBlob(blob_info);
6042 q=blob_info->data+blob_info->offset;
6043 (void) memcpy(q,p,length);
6044 blob_info->offset+=(MagickOffsetType) length;
6045 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6046 blob_info->length=(
size_t) blob_info->offset;
6047 count=(ssize_t) length;
6052 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6053 count=blob_info->custom_stream->writer((
unsigned char *) data,
6054 length,blob_info->custom_stream->data);
6086MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
6089 *magick_restrict blob_info;
6094 assert(image != (
Image *) NULL);
6095 assert(image->signature == MagickCoreSignature);
6096 assert(image->blob != (
BlobInfo *) NULL);
6097 assert(image->blob->type != UndefinedStream);
6098 blob_info=image->blob;
6100 switch (blob_info->type)
6102 case StandardStream:
6109 c=putc((
int) value,blob_info->file_info.file);
6112 if (ferror(blob_info->file_info.file) != 0)
6113 ThrowBlobException(blob_info);
6121 count=WriteBlobStream(image,1,&value);
6153MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
6164 quantum.unsigned_value=0U;
6165 quantum.float_value=value;
6166 return(WriteBlobLong(image,quantum.unsigned_value));
6194MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
6199 assert(image != (
Image *) NULL);
6200 assert(image->signature == MagickCoreSignature);
6201 if (image->endian == LSBEndian)
6203 buffer[0]=(
unsigned char) value;
6204 buffer[1]=(
unsigned char) (value >> 8);
6205 buffer[2]=(
unsigned char) (value >> 16);
6206 buffer[3]=(
unsigned char) (value >> 24);
6207 return(WriteBlobStream(image,4,buffer));
6209 buffer[0]=(
unsigned char) (value >> 24);
6210 buffer[1]=(
unsigned char) (value >> 16);
6211 buffer[2]=(
unsigned char) (value >> 8);
6212 buffer[3]=(
unsigned char) value;
6213 return(WriteBlobStream(image,4,buffer));
6241MagickExport ssize_t WriteBlobLongLong(
Image *image,
const MagickSizeType value)
6246 assert(image != (
Image *) NULL);
6247 assert(image->signature == MagickCoreSignature);
6248 if (image->endian == LSBEndian)
6250 buffer[0]=(
unsigned char) value;
6251 buffer[1]=(
unsigned char) (value >> 8);
6252 buffer[2]=(
unsigned char) (value >> 16);
6253 buffer[3]=(
unsigned char) (value >> 24);
6254 buffer[4]=(
unsigned char) (value >> 32);
6255 buffer[5]=(
unsigned char) (value >> 40);
6256 buffer[6]=(
unsigned char) (value >> 48);
6257 buffer[7]=(
unsigned char) (value >> 56);
6258 return(WriteBlobStream(image,8,buffer));
6260 buffer[0]=(
unsigned char) (value >> 56);
6261 buffer[1]=(
unsigned char) (value >> 48);
6262 buffer[2]=(
unsigned char) (value >> 40);
6263 buffer[3]=(
unsigned char) (value >> 32);
6264 buffer[4]=(
unsigned char) (value >> 24);
6265 buffer[5]=(
unsigned char) (value >> 16);
6266 buffer[6]=(
unsigned char) (value >> 8);
6267 buffer[7]=(
unsigned char) value;
6268 return(WriteBlobStream(image,8,buffer));
6296MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
6301 assert(image != (
Image *) NULL);
6302 assert(image->signature == MagickCoreSignature);
6303 if (image->endian == LSBEndian)
6305 buffer[0]=(
unsigned char) value;
6306 buffer[1]=(
unsigned char) (value >> 8);
6307 return(WriteBlobStream(image,2,buffer));
6309 buffer[0]=(
unsigned char) (value >> 8);
6310 buffer[1]=(
unsigned char) value;
6311 return(WriteBlobStream(image,2,buffer));
6339MagickExport ssize_t WriteBlobSignedLong(
Image *image,
const signed int value)
6353 assert(image != (
Image *) NULL);
6354 assert(image->signature == MagickCoreSignature);
6355 quantum.signed_value=value;
6356 if (image->endian == LSBEndian)
6358 buffer[0]=(
unsigned char) quantum.unsigned_value;
6359 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6360 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6361 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6362 return(WriteBlobStream(image,4,buffer));
6364 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6365 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6366 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6367 buffer[3]=(
unsigned char) quantum.unsigned_value;
6368 return(WriteBlobStream(image,4,buffer));
6396MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
6401 assert(image != (
Image *) NULL);
6402 assert(image->signature == MagickCoreSignature);
6403 buffer[0]=(
unsigned char) value;
6404 buffer[1]=(
unsigned char) (value >> 8);
6405 buffer[2]=(
unsigned char) (value >> 16);
6406 buffer[3]=(
unsigned char) (value >> 24);
6407 return(WriteBlobStream(image,4,buffer));
6435MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
6440 assert(image != (
Image *) NULL);
6441 assert(image->signature == MagickCoreSignature);
6442 buffer[0]=(
unsigned char) value;
6443 buffer[1]=(
unsigned char) (value >> 8);
6444 return(WriteBlobStream(image,2,buffer));
6472MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
6486 assert(image != (
Image *) NULL);
6487 assert(image->signature == MagickCoreSignature);
6488 quantum.signed_value=value;
6489 buffer[0]=(
unsigned char) quantum.unsigned_value;
6490 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6491 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6492 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6493 return(WriteBlobStream(image,4,buffer));
6521MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
6522 const signed short value)
6536 assert(image != (
Image *) NULL);
6537 assert(image->signature == MagickCoreSignature);
6538 quantum.signed_value=value;
6539 buffer[0]=(
unsigned char) quantum.unsigned_value;
6540 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6541 return(WriteBlobStream(image,2,buffer));
6569MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
6574 assert(image != (
Image *) NULL);
6575 assert(image->signature == MagickCoreSignature);
6576 buffer[0]=(
unsigned char) (value >> 24);
6577 buffer[1]=(
unsigned char) (value >> 16);
6578 buffer[2]=(
unsigned char) (value >> 8);
6579 buffer[3]=(
unsigned char) value;
6580 return(WriteBlobStream(image,4,buffer));
6608MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
6609 const signed short value)
6623 assert(image != (
Image *) NULL);
6624 assert(image->signature == MagickCoreSignature);
6625 quantum.signed_value=value;
6626 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6627 buffer[1]=(
unsigned char) quantum.unsigned_value;
6628 return(WriteBlobStream(image,2,buffer));
6656MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
6661 assert(image != (
Image *) NULL);
6662 assert(image->signature == MagickCoreSignature);
6663 buffer[0]=(
unsigned char) (value >> 8);
6664 buffer[1]=(
unsigned char) value;
6665 return(WriteBlobStream(image,2,buffer));
6693MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
6695 assert(image != (
Image *) NULL);
6696 assert(image->signature == MagickCoreSignature);
6697 assert(
string != (
const char *) NULL);
6698 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));