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 *);
245MagickExport CustomStreamInfo *AcquireCustomStreamInfo(
246 ExceptionInfo *magick_unused(exception))
251 magick_unreferenced(exception);
252 custom_stream=(CustomStreamInfo *) AcquireCriticalMemory(
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,
328 CustomStreamInfo *custom_stream)
330 assert(blob_info != (BlobInfo *) NULL);
331 assert(custom_stream != (CustomStreamInfo *) 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;
370MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
371 const size_t length,ExceptionInfo *exception)
382 assert(filename != (
const char *) NULL);
383 assert(blob != (
const void *) NULL);
384 if (IsEventLogging() != MagickFalse)
385 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
386 if (*filename ==
'\0')
387 file=AcquireUniqueFileResource(filename);
389 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
392 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
395 for (i=0; i < length; i+=(size_t) count)
397 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
398 MagickMaxBufferExtent));
406 file=close_utf8(file);
407 if ((file == -1) || (i < length))
409 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
446MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
447 const size_t length,ExceptionInfo *exception)
462 assert(image_info != (ImageInfo *) NULL);
463 assert(image_info->signature == MagickCoreSignature);
464 assert(exception != (ExceptionInfo *) NULL);
465 if (IsEventLogging() != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
467 image_info->filename);
468 if ((blob == (
const void *) NULL) || (length == 0))
470 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
471 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
472 return((Image *) NULL);
474 blob_info=CloneImageInfo(image_info);
475 blob_info->blob=(
void *) blob;
476 blob_info->length=length;
477 if (*blob_info->magick ==
'\0')
478 (void) SetImageInfo(blob_info,0,exception);
479 magick_info=GetMagickInfo(blob_info->magick,exception);
480 if (magick_info == (
const MagickInfo *) NULL)
482 (void) ThrowMagickException(exception,GetMagickModule(),
483 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
485 blob_info=DestroyImageInfo(blob_info);
486 return((Image *) NULL);
488 if (GetMagickBlobSupport(magick_info) != MagickFalse)
491 filename[MagickPathExtent];
496 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
497 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
498 blob_info->magick,filename);
499 image=ReadImage(blob_info,exception);
500 if (image != (Image *) NULL)
501 (void) DetachBlob(image->blob);
502 blob_info=DestroyImageInfo(blob_info);
508 blob_info->blob=(
void *) NULL;
510 *blob_info->filename=
'\0';
511 status=BlobToFile(blob_info->filename,blob,length,exception);
512 if (status == MagickFalse)
514 (void) RelinquishUniqueFileResource(blob_info->filename);
515 blob_info=DestroyImageInfo(blob_info);
516 return((Image *) NULL);
518 clone_info=CloneImageInfo(blob_info);
519 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
520 blob_info->magick,blob_info->filename);
521 image=ReadImage(clone_info,exception);
522 if (image != (Image *) NULL)
530 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
532 (void) CopyMagickString(images->filename,image_info->filename,
534 (void) CopyMagickString(images->magick_filename,image_info->filename,
536 (void) CopyMagickString(images->magick,magick_info->name,
538 images=GetNextImageInList(images);
541 clone_info=DestroyImageInfo(clone_info);
542 (void) RelinquishUniqueFileResource(blob_info->filename);
543 blob_info=DestroyImageInfo(blob_info);
570MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
578 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
579 GetBlobInfo(clone_info);
580 if (blob_info == (BlobInfo *) NULL)
582 semaphore=clone_info->semaphore;
583 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
584 if (blob_info->mapped != MagickFalse)
585 (void) AcquireMagickResource(MapResource,blob_info->length);
586 clone_info->semaphore=semaphore;
587 LockSemaphoreInfo(clone_info->semaphore);
588 clone_info->reference_count=1;
589 UnlockSemaphoreInfo(clone_info->semaphore);
616static inline void ThrowBlobException(BlobInfo *blob_info)
618 if ((blob_info->status == 0) && (errno != 0))
619 blob_info->error_number=errno;
620 blob_info->status=(-1);
623MagickExport MagickBooleanType CloseBlob(Image *image)
626 *magick_restrict blob_info;
634 assert(image != (Image *) NULL);
635 assert(image->signature == MagickCoreSignature);
636 if (IsEventLogging() != MagickFalse)
637 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
638 blob_info=image->blob;
639 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
641 (void) SyncBlob(image);
642 status=blob_info->status;
643 switch (blob_info->type)
645 case UndefinedStream:
651 if (blob_info->synchronize != MagickFalse)
653 status=fflush(blob_info->file_info.file);
655 ThrowBlobException(blob_info);
656 status=fsync(fileno(blob_info->file_info.file));
658 ThrowBlobException(blob_info);
660 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
661 ThrowBlobException(blob_info);
666#if defined(MAGICKCORE_ZLIB_DELEGATE)
668 (void) gzerror(blob_info->file_info.gzfile,&status);
670 ThrowBlobException(blob_info);
676#if defined(MAGICKCORE_BZLIB_DELEGATE)
678 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
680 ThrowBlobException(blob_info);
688 if (blob_info->file_info.file != (FILE *) NULL)
690 if (blob_info->synchronize != MagickFalse)
692 status=fflush(blob_info->file_info.file);
694 ThrowBlobException(blob_info);
695 status=fsync(fileno(blob_info->file_info.file));
697 ThrowBlobException(blob_info);
699 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
700 ThrowBlobException(blob_info);
707 blob_info->size=GetBlobSize(image);
708 image->extent=blob_info->size;
709 blob_info->eof=MagickFalse;
711 blob_info->mode=UndefinedBlobMode;
712 if (blob_info->exempt != MagickFalse)
714 blob_info->type=UndefinedStream;
715 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
717 switch (blob_info->type)
719 case UndefinedStream:
724 if (blob_info->file_info.file != (FILE *) NULL)
726 status=fclose(blob_info->file_info.file);
728 ThrowBlobException(blob_info);
734#if defined(MAGICKCORE_HAVE_PCLOSE)
735 status=pclose(blob_info->file_info.file);
737 ThrowBlobException(blob_info);
743#if defined(MAGICKCORE_ZLIB_DELEGATE)
744 status=gzclose(blob_info->file_info.gzfile);
746 ThrowBlobException(blob_info);
752#if defined(MAGICKCORE_BZLIB_DELEGATE)
753 BZ2_bzclose(blob_info->file_info.bzfile);
761 if (blob_info->file_info.file != (FILE *) NULL)
763 status=fclose(blob_info->file_info.file);
765 ThrowBlobException(blob_info);
772 (void) DetachBlob(blob_info);
773 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
802MagickExport Image *CustomStreamToImage(
const ImageInfo *image_info,
803 ExceptionInfo *exception)
814 assert(image_info != (ImageInfo *) NULL);
815 assert(image_info->signature == MagickCoreSignature);
816 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
817 assert(image_info->custom_stream->signature == MagickCoreSignature);
818 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
819 assert(exception != (ExceptionInfo *) NULL);
820 if (IsEventLogging() != MagickFalse)
821 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
822 image_info->filename);
823 blob_info=CloneImageInfo(image_info);
824 if (*blob_info->magick ==
'\0')
825 (void) SetImageInfo(blob_info,0,exception);
826 magick_info=GetMagickInfo(blob_info->magick,exception);
827 if (magick_info == (
const MagickInfo *) NULL)
829 (void) ThrowMagickException(exception,GetMagickModule(),
830 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
832 blob_info=DestroyImageInfo(blob_info);
833 return((Image *) NULL);
835 image=(Image *) NULL;
836 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
837 (*blob_info->filename !=
'\0'))
840 filename[MagickPathExtent];
846 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
847 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
848 blob_info->magick,filename);
849 image=ReadImage(blob_info,exception);
854 unique[MagickPathExtent];
868 blob_info->custom_stream=(CustomStreamInfo *) NULL;
869 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
871 if (blob == (
unsigned char *) NULL)
873 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
874 image_info->filename);
875 blob_info=DestroyImageInfo(blob_info);
876 return((Image *) NULL);
878 file=AcquireUniqueFileResource(unique);
881 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
882 image_info->filename);
883 blob=(
unsigned char *) RelinquishMagickMemory(blob);
884 blob_info=DestroyImageInfo(blob_info);
885 return((Image *) NULL);
887 clone_info=CloneImageInfo(blob_info);
888 blob_info->file=fdopen(file,
"wb+");
889 if (blob_info->file != (FILE *) NULL)
894 count=(ssize_t) MagickMaxBufferExtent;
895 while (count == (ssize_t) MagickMaxBufferExtent)
897 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
898 image_info->custom_stream->data);
899 count=(ssize_t) write(file,(
const char *) blob,(
size_t) count);
901 (void) fclose(blob_info->file);
902 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
903 "%s:%s",blob_info->magick,unique);
904 image=ReadImage(clone_info,exception);
905 if (image != (Image *) NULL)
913 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
915 (void) CopyMagickString(images->filename,image_info->filename,
917 (void) CopyMagickString(images->magick_filename,
918 image_info->filename,MagickPathExtent);
919 (void) CopyMagickString(images->magick,magick_info->name,
921 images=GetNextImageInList(images);
925 clone_info=DestroyImageInfo(clone_info);
926 blob=(
unsigned char *) RelinquishMagickMemory(blob);
927 (void) RelinquishUniqueFileResource(unique);
929 blob_info=DestroyImageInfo(blob_info);
930 if (image != (Image *) NULL)
931 if (CloseBlob(image) == MagickFalse)
932 image=DestroyImageList(image);
958MagickExport
void DestroyBlob(Image *image)
961 *magick_restrict blob_info;
966 assert(image != (Image *) NULL);
967 assert(image->signature == MagickCoreSignature);
968 assert(image->blob != (BlobInfo *) NULL);
969 assert(image->blob->signature == MagickCoreSignature);
970 if (IsEventLogging() != MagickFalse)
971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
972 blob_info=image->blob;
974 LockSemaphoreInfo(blob_info->semaphore);
975 blob_info->reference_count--;
976 assert(blob_info->reference_count >= 0);
977 if (blob_info->reference_count == 0)
979 UnlockSemaphoreInfo(blob_info->semaphore);
980 if (destroy == MagickFalse)
982 image->blob=(BlobInfo *) NULL;
985 (void) CloseBlob(image);
986 if (blob_info->mapped != MagickFalse)
988 (void) UnmapBlob(blob_info->data,blob_info->length);
989 RelinquishMagickResource(MapResource,blob_info->length);
992 RelinquishSemaphoreInfo(&blob_info->semaphore);
993 blob_info->signature=(~MagickCoreSignature);
994 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
1020MagickExport CustomStreamInfo *DestroyCustomStreamInfo(
1021 CustomStreamInfo *custom_stream)
1023 assert(custom_stream != (CustomStreamInfo *) NULL);
1024 assert(custom_stream->signature == MagickCoreSignature);
1025 if (IsEventLogging() != MagickFalse)
1026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1027 custom_stream->signature=(~MagickCoreSignature);
1028 custom_stream=(CustomStreamInfo *) RelinquishMagickMemory(custom_stream);
1029 return(custom_stream);
1054MagickExport
void *DetachBlob(BlobInfo *blob_info)
1059 assert(blob_info != (BlobInfo *) NULL);
1060 if (IsEventLogging() != MagickFalse)
1061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1062 if (blob_info->mapped != MagickFalse)
1064 (void) UnmapBlob(blob_info->data,blob_info->length);
1065 blob_info->data=NULL;
1066 RelinquishMagickResource(MapResource,blob_info->length);
1068 blob_info->mapped=MagickFalse;
1069 blob_info->length=0;
1074 blob_info->offset=0;
1075 blob_info->mode=UndefinedBlobMode;
1076 blob_info->eof=MagickFalse;
1078 blob_info->exempt=MagickFalse;
1079 blob_info->type=UndefinedStream;
1080 blob_info->file_info.file=(FILE *) NULL;
1081 data=blob_info->data;
1082 blob_info->data=(
unsigned char *) NULL;
1083 blob_info->stream=(StreamHandler) NULL;
1084 blob_info->custom_stream=(CustomStreamInfo *) NULL;
1112MagickExport
void DisassociateBlob(Image *image)
1115 *magick_restrict blob_info,
1121 assert(image != (Image *) NULL);
1122 assert(image->signature == MagickCoreSignature);
1123 assert(image->blob != (BlobInfo *) NULL);
1124 assert(image->blob->signature == MagickCoreSignature);
1125 if (IsEventLogging() != MagickFalse)
1126 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1127 blob_info=image->blob;
1129 LockSemaphoreInfo(blob_info->semaphore);
1130 assert(blob_info->reference_count >= 0);
1131 if (blob_info->reference_count > 1)
1133 UnlockSemaphoreInfo(blob_info->semaphore);
1134 if (clone == MagickFalse)
1136 clone_info=CloneBlobInfo(blob_info);
1138 image->blob=clone_info;
1166MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1167 const MagickSizeType length)
1179 buffer[MagickMinBufferExtent >> 1];
1181 assert(image != (Image *) NULL);
1182 assert(image->signature == MagickCoreSignature);
1183 if (length != (MagickSizeType) ((MagickOffsetType) length))
1184 return(MagickFalse);
1186 for (i=0; i < length; i+=(MagickSizeType) count)
1188 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1189 (void) ReadBlobStream(image,quantum,buffer,&count);
1197 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1224MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
1226 assert(image != (Image *) NULL);
1227 assert(image->signature == MagickCoreSignature);
1228 assert(duplicate != (Image *) NULL);
1229 assert(duplicate->signature == MagickCoreSignature);
1230 if (IsEventLogging() != MagickFalse)
1231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1233 image->blob=ReferenceBlob(duplicate->blob);
1259MagickExport
int EOFBlob(
const Image *image)
1262 *magick_restrict blob_info;
1264 assert(image != (Image *) NULL);
1265 assert(image->signature == MagickCoreSignature);
1266 assert(image->blob != (BlobInfo *) NULL);
1267 assert(image->blob->type != UndefinedStream);
1268 if (IsEventLogging() != MagickFalse)
1269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1270 blob_info=image->blob;
1271 switch (blob_info->type)
1273 case UndefinedStream:
1274 case StandardStream:
1279 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1285#if defined(MAGICKCORE_ZLIB_DELEGATE)
1286 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1293#if defined(MAGICKCORE_BZLIB_DELEGATE)
1298 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1299 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1305 blob_info->eof=MagickFalse;
1313 return((
int) blob_info->eof);
1339MagickExport
int ErrorBlob(
const Image *image)
1342 *magick_restrict blob_info;
1344 assert(image != (Image *) NULL);
1345 assert(image->signature == MagickCoreSignature);
1346 assert(image->blob != (BlobInfo *) NULL);
1347 assert(image->blob->type != UndefinedStream);
1348 if (IsEventLogging() != MagickFalse)
1349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1350 blob_info=image->blob;
1351 switch (blob_info->type)
1353 case UndefinedStream:
1354 case StandardStream:
1359 blob_info->error=ferror(blob_info->file_info.file);
1364#if defined(MAGICKCORE_ZLIB_DELEGATE)
1365 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1371#if defined(MAGICKCORE_BZLIB_DELEGATE)
1372 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1386 return(blob_info->error);
1424MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1425 size_t *length,ExceptionInfo *exception)
1451 assert(filename != (
const char *) NULL);
1452 assert(exception != (ExceptionInfo *) NULL);
1453 assert(exception->signature == MagickCoreSignature);
1454 if (IsEventLogging() != MagickFalse)
1455 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1457 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1458 if (status == MagickFalse)
1461 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1462 "NotAuthorized",
"`%s'",filename);
1466 if (LocaleCompare(filename,
"-") != 0)
1469 flags = O_RDONLY | O_BINARY;
1471 status=GetPathAttributes(filename,&attributes);
1472 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1474 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1477#if defined(O_NOFOLLOW)
1478 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1479 if (status == MagickFalse)
1482 file=open_utf8(filename,flags,0);
1486 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1489 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1490 if (status == MagickFalse)
1492 file=close_utf8(file)-1;
1494 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1495 "NotAuthorized",
"`%s'",filename);
1498 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1500 if ((file == fileno(stdin)) || (offset < 0) ||
1501 (offset != (MagickOffsetType) ((ssize_t) offset)))
1512 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1513 quantum=(size_t) MagickMaxBufferExtent;
1514 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1515 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1516 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1517 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1519 count=read(file,blob+i,quantum);
1526 if (~i < ((
size_t) count+quantum+1))
1528 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1531 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1532 quantum+1,
sizeof(*blob));
1533 if ((i+(
size_t) count) >= extent)
1536 if (LocaleCompare(filename,
"-") != 0)
1537 file=close_utf8(file);
1538 if (blob == (
unsigned char *) NULL)
1540 (void) ThrowMagickException(exception,GetMagickModule(),
1541 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1546 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1547 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1550 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1554 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1555 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1556 blob=(
unsigned char *) NULL;
1557 if (~(*length) >= (MagickPathExtent-1))
1558 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1560 if (blob == (
unsigned char *) NULL)
1562 file=close_utf8(file);
1563 (void) ThrowMagickException(exception,GetMagickModule(),
1564 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1567 map=MapBlob(file,ReadMode,0,*length);
1568 if (map != (
unsigned char *) NULL)
1570 (void) memcpy(blob,map,*length);
1571 (void) UnmapBlob(map,*length);
1575 (void) lseek(file,0,SEEK_SET);
1576 for (i=0; i < *length; i+=(size_t) count)
1578 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1579 MagickMaxBufferExtent));
1589 file=close_utf8(file)-1;
1590 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1591 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1596 if (LocaleCompare(filename,
"-") != 0)
1597 file=close_utf8(file);
1600 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1601 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1631static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1632 const void *magick_restrict data)
1635 *magick_restrict blob_info;
1643 assert(image->blob != (BlobInfo *) NULL);
1644 assert(image->blob->type != UndefinedStream);
1645 assert(data != NULL);
1646 blob_info=image->blob;
1647 if (blob_info->type != BlobStream)
1648 return(WriteBlob(image,length,(
const unsigned char *) data));
1649 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1654 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1655 if (extent >= blob_info->extent)
1657 extent+=blob_info->quantum+length;
1658 blob_info->quantum<<=1;
1659 if (SetBlobExtent(image,extent) == MagickFalse)
1662 q=blob_info->data+blob_info->offset;
1663 (void) memcpy(q,data,length);
1664 blob_info->offset+=(MagickOffsetType) length;
1665 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1666 blob_info->length=(size_t) blob_info->offset;
1667 return((ssize_t) length);
1670MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1671 ExceptionInfo *exception)
1692 assert(image != (
const Image *) NULL);
1693 assert(image->signature == MagickCoreSignature);
1694 assert(filename != (
const char *) NULL);
1695 if (IsEventLogging() != MagickFalse)
1696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1697 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1698 if (status == MagickFalse)
1701 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1702 "NotAuthorized",
"`%s'",filename);
1703 return(MagickFalse);
1706 if (LocaleCompare(filename,
"-") != 0)
1709 flags = O_RDONLY | O_BINARY;
1711#if defined(O_NOFOLLOW)
1712 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1713 if (status == MagickFalse)
1716 file=open_utf8(filename,flags,0);
1720 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1721 return(MagickFalse);
1723 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1724 if (status == MagickFalse)
1726 file=close_utf8(file);
1728 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1729 "NotAuthorized",
"`%s'",filename);
1730 return(MagickFalse);
1732 quantum=(size_t) MagickMaxBufferExtent;
1733 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1734 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1735 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1736 if (blob == (
unsigned char *) NULL)
1738 file=close_utf8(file);
1739 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1741 return(MagickFalse);
1745 count=read(file,blob,quantum);
1752 length=(size_t) count;
1753 count=WriteBlobStream(image,length,blob);
1754 if (count != (ssize_t) length)
1756 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1760 file=close_utf8(file);
1762 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1763 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1790MagickExport MagickBooleanType GetBlobError(
const Image *image)
1792 assert(image != (
const Image *) NULL);
1793 assert(image->signature == MagickCoreSignature);
1794 if (IsEventLogging() != MagickFalse)
1795 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1796 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1797 errno=image->blob->error_number;
1798 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1823MagickExport FILE *GetBlobFileHandle(
const Image *image)
1825 assert(image != (
const Image *) NULL);
1826 assert(image->signature == MagickCoreSignature);
1827 return(image->blob->file_info.file);
1852MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1854 assert(blob_info != (BlobInfo *) NULL);
1855 (void) memset(blob_info,0,
sizeof(*blob_info));
1856 blob_info->type=UndefinedStream;
1857 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1858 blob_info->properties.st_mtime=GetMagickTime();
1859 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1860 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1861 blob_info->reference_count=1;
1862 blob_info->semaphore=AcquireSemaphoreInfo();
1863 blob_info->signature=MagickCoreSignature;
1888MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1890 assert(image != (Image *) NULL);
1891 assert(image->signature == MagickCoreSignature);
1892 if (IsEventLogging() != MagickFalse)
1893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1894 return(&image->blob->properties);
1920MagickExport MagickSizeType GetBlobSize(
const Image *image)
1923 *magick_restrict blob_info;
1928 assert(image != (Image *) NULL);
1929 assert(image->signature == MagickCoreSignature);
1930 assert(image->blob != (BlobInfo *) NULL);
1931 if (IsEventLogging() != MagickFalse)
1932 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1933 blob_info=image->blob;
1935 switch (blob_info->type)
1937 case UndefinedStream:
1938 case StandardStream:
1940 extent=blob_info->size;
1948 extent=(MagickSizeType) blob_info->properties.st_size;
1950 extent=blob_info->size;
1951 file_descriptor=fileno(blob_info->file_info.file);
1952 if (file_descriptor == -1)
1954 if (fstat(file_descriptor,&blob_info->properties) == 0)
1955 extent=(MagickSizeType) blob_info->properties.st_size;
1960 extent=blob_info->size;
1969 status=GetPathAttributes(image->filename,&blob_info->properties);
1970 if (status != MagickFalse)
1971 extent=(MagickSizeType) blob_info->properties.st_size;
1978 extent=(MagickSizeType) blob_info->length;
1983 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1984 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1989 offset=blob_info->custom_stream->teller(
1990 blob_info->custom_stream->data);
1991 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1992 blob_info->custom_stream->data);
1993 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1994 blob_info->custom_stream->data);
2024MagickExport
void *GetBlobStreamData(
const Image *image)
2026 assert(image != (
const Image *) NULL);
2027 assert(image->signature == MagickCoreSignature);
2028 return(image->blob->data);
2053MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2055 assert(image != (
const Image *) NULL);
2056 assert(image->signature == MagickCoreSignature);
2057 if (IsEventLogging() != MagickFalse)
2058 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2059 return(image->blob->stream);
2095MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2096 Image *image,
size_t *length,ExceptionInfo *exception)
2110 assert(image_info != (
const ImageInfo *) NULL);
2111 assert(image_info->signature == MagickCoreSignature);
2112 assert(image != (Image *) NULL);
2113 assert(image->signature == MagickCoreSignature);
2114 assert(exception != (ExceptionInfo *) NULL);
2115 assert(exception->signature == MagickCoreSignature);
2116 if (IsEventLogging() != MagickFalse)
2117 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2118 image_info->filename);
2120 blob=(
unsigned char *) NULL;
2121 blob_info=CloneImageInfo(image_info);
2122 blob_info->adjoin=MagickFalse;
2123 (void) SetImageInfo(blob_info,1,exception);
2124 if (*blob_info->magick !=
'\0')
2125 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2126 magick_info=GetMagickInfo(image->magick,exception);
2127 if (magick_info == (
const MagickInfo *) NULL)
2129 (void) ThrowMagickException(exception,GetMagickModule(),
2130 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2132 blob_info=DestroyImageInfo(blob_info);
2135 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2136 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2141 blob_info->length=0;
2142 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2143 sizeof(
unsigned char));
2144 if (blob_info->blob == NULL)
2145 (void) ThrowMagickException(exception,GetMagickModule(),
2146 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2149 (void) CloseBlob(image);
2150 image->blob->exempt=MagickTrue;
2151 image->blob->extent=0;
2152 *image->filename=
'\0';
2153 status=WriteImage(blob_info,image,exception);
2154 *length=image->blob->length;
2155 blob=DetachBlob(image->blob);
2156 if (blob != (
void *) NULL)
2158 if (status == MagickFalse)
2159 blob=RelinquishMagickMemory(blob);
2161 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2163 else if ((status == MagickFalse) && (image->blob->extent == 0))
2164 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2170 unique[MagickPathExtent];
2178 file=AcquireUniqueFileResource(unique);
2181 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2182 image_info->filename);
2186 blob_info->file=fdopen(file,
"wb");
2187 if (blob_info->file != (FILE *) NULL)
2189 (void) FormatLocaleString(image->filename,MagickPathExtent,
2190 "%s:%s",image->magick,unique);
2191 status=WriteImage(blob_info,image,exception);
2192 (void) fclose(blob_info->file);
2193 if (status != MagickFalse)
2194 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2196 (void) RelinquishUniqueFileResource(unique);
2199 blob_info=DestroyImageInfo(blob_info);
2231MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2232 ExceptionInfo *exception)
2244 assert(image_info != (
const ImageInfo *) NULL);
2245 assert(image_info->signature == MagickCoreSignature);
2246 assert(image != (Image *) NULL);
2247 assert(image->signature == MagickCoreSignature);
2248 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2249 assert(image_info->custom_stream->signature == MagickCoreSignature);
2250 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2251 assert(exception != (ExceptionInfo *) NULL);
2252 if (IsEventLogging() != MagickFalse)
2253 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2254 image_info->filename);
2255 clone_info=CloneImageInfo(image_info);
2256 clone_info->adjoin=MagickFalse;
2257 (void) SetImageInfo(clone_info,1,exception);
2258 if (*clone_info->magick !=
'\0')
2259 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2260 magick_info=GetMagickInfo(image->magick,exception);
2261 if (magick_info == (
const MagickInfo *) NULL)
2263 (void) ThrowMagickException(exception,GetMagickModule(),
2264 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2266 clone_info=DestroyImageInfo(clone_info);
2269 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2270 blob_support=GetMagickBlobSupport(magick_info);
2271 if ((blob_support != MagickFalse) &&
2272 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2274 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2275 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2276 blob_support=MagickFalse;
2278 if (blob_support != MagickFalse)
2283 (void) CloseBlob(image);
2284 *image->filename=
'\0';
2285 (void) WriteImage(clone_info,image,exception);
2290 unique[MagickPathExtent];
2301 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2302 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2304 if (blob == (
unsigned char *) NULL)
2306 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2307 image_info->filename);
2308 clone_info=DestroyImageInfo(clone_info);
2311 file=AcquireUniqueFileResource(unique);
2314 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2315 image_info->filename);
2316 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2317 clone_info=DestroyImageInfo(clone_info);
2320 clone_info->file=fdopen(file,
"wb+");
2321 if (clone_info->file != (FILE *) NULL)
2326 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2327 image->magick,unique);
2328 status=WriteImage(clone_info,image,exception);
2329 if (status != MagickFalse)
2331 (void) fseek(clone_info->file,0,SEEK_SET);
2332 count=(ssize_t) MagickMaxBufferExtent;
2333 while (count == (ssize_t) MagickMaxBufferExtent)
2335 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2337 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2338 image_info->custom_stream->data);
2341 (void) fclose(clone_info->file);
2343 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2344 (void) RelinquishUniqueFileResource(unique);
2346 clone_info=DestroyImageInfo(clone_info);
2377MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2378 ExceptionInfo *exception)
2402 assert(image != (Image *) NULL);
2403 assert(image->signature == MagickCoreSignature);
2404 assert(image->blob != (BlobInfo *) NULL);
2405 assert(image->blob->type != UndefinedStream);
2406 assert(filename != (
const char *) NULL);
2407 if (IsEventLogging() != MagickFalse)
2408 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2409 if (*filename ==
'\0')
2410 file=AcquireUniqueFileResource(filename);
2412 if (LocaleCompare(filename,
"-") == 0)
2413 file=fileno(stdout);
2415 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2418 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2419 return(MagickFalse);
2421 quantum=(size_t) MagickMaxBufferExtent;
2422 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2423 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2424 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2425 if (buffer == (
unsigned char *) NULL)
2427 file=close_utf8(file)-1;
2428 (void) ThrowMagickException(exception,GetMagickModule(),
2429 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2430 return(MagickFalse);
2433 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2434 for (i=0; count > 0; )
2436 length=(size_t) count;
2437 for (i=0; i < length; i+=(size_t) count)
2439 count=write(file,p+i,(
size_t) (length-i));
2449 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2451 if (LocaleCompare(filename,
"-") != 0)
2452 file=close_utf8(file);
2453 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2454 if ((file == -1) || (i < length))
2457 file=close_utf8(file);
2458 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2459 return(MagickFalse);
2499MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2500 size_t *length,ExceptionInfo *exception)
2514 assert(image_info != (
const ImageInfo *) NULL);
2515 assert(image_info->signature == MagickCoreSignature);
2516 assert(images != (Image *) NULL);
2517 assert(images->signature == MagickCoreSignature);
2518 assert(exception != (ExceptionInfo *) NULL);
2519 if (IsEventLogging() != MagickFalse)
2520 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2521 image_info->filename);
2523 blob=(
unsigned char *) NULL;
2524 blob_info=CloneImageInfo(image_info);
2525 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2527 if (*blob_info->magick !=
'\0')
2528 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2529 magick_info=GetMagickInfo(images->magick,exception);
2530 if (magick_info == (
const MagickInfo *) NULL)
2532 (void) ThrowMagickException(exception,GetMagickModule(),
2533 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2535 blob_info=DestroyImageInfo(blob_info);
2538 if (GetMagickAdjoin(magick_info) == MagickFalse)
2540 blob_info=DestroyImageInfo(blob_info);
2541 return(ImageToBlob(image_info,images,length,exception));
2543 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2544 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2549 blob_info->length=0;
2550 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2551 sizeof(
unsigned char));
2552 if (blob_info->blob == (
void *) NULL)
2553 (void) ThrowMagickException(exception,GetMagickModule(),
2554 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2557 (void) CloseBlob(images);
2558 images->blob->exempt=MagickTrue;
2559 images->blob->extent=0;
2560 *images->filename=
'\0';
2561 status=WriteImages(blob_info,images,images->filename,exception);
2562 *length=images->blob->length;
2563 blob=DetachBlob(images->blob);
2564 if (blob != (
void *) NULL)
2566 if (status == MagickFalse)
2567 blob=RelinquishMagickMemory(blob);
2569 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2571 else if ((status == MagickFalse) && (images->blob->extent == 0))
2572 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2578 filename[MagickPathExtent],
2579 unique[MagickPathExtent];
2587 file=AcquireUniqueFileResource(unique);
2590 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2591 image_info->filename);
2595 blob_info->file=fdopen(file,
"wb");
2596 if (blob_info->file != (FILE *) NULL)
2598 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2599 images->magick,unique);
2600 status=WriteImages(blob_info,images,filename,exception);
2601 (void) fclose(blob_info->file);
2602 if (status != MagickFalse)
2603 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2605 (void) RelinquishUniqueFileResource(unique);
2608 blob_info=DestroyImageInfo(blob_info);
2640MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2641 Image *images,ExceptionInfo *exception)
2653 assert(image_info != (
const ImageInfo *) NULL);
2654 assert(image_info->signature == MagickCoreSignature);
2655 assert(images != (Image *) NULL);
2656 assert(images->signature == MagickCoreSignature);
2657 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2658 assert(image_info->custom_stream->signature == MagickCoreSignature);
2659 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2660 assert(exception != (ExceptionInfo *) NULL);
2661 if (IsEventLogging() != MagickFalse)
2662 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2663 image_info->filename);
2664 clone_info=CloneImageInfo(image_info);
2665 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2667 if (*clone_info->magick !=
'\0')
2668 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2669 magick_info=GetMagickInfo(images->magick,exception);
2670 if (magick_info == (
const MagickInfo *) NULL)
2672 (void) ThrowMagickException(exception,GetMagickModule(),
2673 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2675 clone_info=DestroyImageInfo(clone_info);
2678 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2679 blob_support=GetMagickBlobSupport(magick_info);
2680 if ((blob_support != MagickFalse) &&
2681 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2683 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2684 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2685 blob_support=MagickFalse;
2687 if (blob_support != MagickFalse)
2692 (void) CloseBlob(images);
2693 *images->filename=
'\0';
2694 (void) WriteImages(clone_info,images,images->filename,exception);
2699 filename[MagickPathExtent],
2700 unique[MagickPathExtent];
2711 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2712 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2714 if (blob == (
unsigned char *) NULL)
2716 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2717 image_info->filename);
2718 clone_info=DestroyImageInfo(clone_info);
2721 file=AcquireUniqueFileResource(unique);
2724 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2725 image_info->filename);
2726 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2727 clone_info=DestroyImageInfo(clone_info);
2730 clone_info->file=fdopen(file,
"wb+");
2731 if (clone_info->file != (FILE *) NULL)
2736 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2737 images->magick,unique);
2738 status=WriteImages(clone_info,images,filename,exception);
2739 if (status != MagickFalse)
2741 (void) fseek(clone_info->file,0,SEEK_SET);
2742 count=(ssize_t) MagickMaxBufferExtent;
2743 while (count == (ssize_t) MagickMaxBufferExtent)
2745 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2747 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2748 image_info->custom_stream->data);
2751 (void) fclose(clone_info->file);
2753 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2754 (void) RelinquishUniqueFileResource(unique);
2756 clone_info=DestroyImageInfo(clone_info);
2792MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2793 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2796 filename[MagickPathExtent];
2825 assert(image_info != (ImageInfo *) NULL);
2826 assert(image_info->signature == MagickCoreSignature);
2827 assert(image != (Image *) NULL);
2828 assert(image->signature == MagickCoreSignature);
2829 assert(inject_image != (Image *) NULL);
2830 assert(inject_image->signature == MagickCoreSignature);
2831 assert(exception != (ExceptionInfo *) NULL);
2832 if (IsEventLogging() != MagickFalse)
2833 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2834 unique_file=(FILE *) NULL;
2835 file=AcquireUniqueFileResource(filename);
2837 unique_file=fdopen(file,
"wb");
2838 if ((file == -1) || (unique_file == (FILE *) NULL))
2840 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2841 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2843 return(MagickFalse);
2845 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2846 if (byte_image == (Image *) NULL)
2848 (void) fclose(unique_file);
2849 (void) RelinquishUniqueFileResource(filename);
2850 return(MagickFalse);
2852 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2854 DestroyBlob(byte_image);
2855 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2856 write_info=CloneImageInfo(image_info);
2857 SetImageInfoFile(write_info,unique_file);
2858 status=WriteImage(write_info,byte_image,exception);
2859 write_info=DestroyImageInfo(write_info);
2860 byte_image=DestroyImage(byte_image);
2861 (void) fclose(unique_file);
2862 if (status == MagickFalse)
2864 (void) RelinquishUniqueFileResource(filename);
2865 return(MagickFalse);
2870 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2873 (void) RelinquishUniqueFileResource(filename);
2874 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2875 image_info->filename);
2876 return(MagickFalse);
2878 quantum=(size_t) MagickMaxBufferExtent;
2879 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2880 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2881 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2882 if (buffer == (
unsigned char *) NULL)
2884 (void) RelinquishUniqueFileResource(filename);
2885 file=close_utf8(file);
2886 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2891 ssize_t count = read(file,buffer,quantum);
2898 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2901 file=close_utf8(file);
2903 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2904 (void) RelinquishUniqueFileResource(filename);
2905 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2931MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2933 assert(image != (
const Image *) NULL);
2934 assert(image->signature == MagickCoreSignature);
2935 if (IsEventLogging() != MagickFalse)
2936 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2937 return(image->blob->exempt);
2962MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2965 *magick_restrict blob_info;
2967 assert(image != (
const Image *) NULL);
2968 assert(image->signature == MagickCoreSignature);
2969 if (IsEventLogging() != MagickFalse)
2970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2971 blob_info=image->blob;
2972 switch (blob_info->type)
2981 if (blob_info->file_info.file == (FILE *) NULL)
2982 return(MagickFalse);
2983 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2984 return(status == -1 ? MagickFalse : MagickTrue);
2988#if defined(MAGICKCORE_ZLIB_DELEGATE)
2992 if (blob_info->file_info.gzfile == (gzFile) NULL)
2993 return(MagickFalse);
2994 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2995 return(offset < 0 ? MagickFalse : MagickTrue);
3000 case UndefinedStream:
3004 case StandardStream:
3008 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3009 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3016 return(MagickFalse);
3041MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3043 assert(image != (
const Image *) NULL);
3044 assert(image->signature == MagickCoreSignature);
3045 if (IsEventLogging() != MagickFalse)
3046 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3047 return(image->blob->temporary);
3079MagickExport
void *MapBlob(
int file,
const MapMode mode,
3080 const MagickOffsetType offset,
const size_t length)
3082#if defined(MAGICKCORE_HAVE_MMAP)
3095#if defined(MAP_ANONYMOUS)
3096 flags|=MAP_ANONYMOUS;
3105 protection=PROT_READ;
3111 protection=PROT_WRITE;
3117 protection=PROT_READ | PROT_WRITE;
3122#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3123 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3125 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3126 if (map == MAP_FAILED)
3127 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3129 if (map == MAP_FAILED)
3166MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3175 assert(buffer != (
unsigned char *) NULL);
3182 *buffer++=(
unsigned char) c;
3186 *buffer++=(
unsigned char) c;
3216MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3224 assert(p != (
unsigned char *) NULL);
3231 *p++=(
unsigned char) c;
3267static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3268 const BlobInfo *blob_info)
3279 size=MagickMinBufferExtent;
3280 option=GetImageOption(image_info,
"stream:buffer-size");
3281 if (option != (
const char *) NULL)
3282 size=StringToUnsignedLong(option);
3283 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3284 _IONBF : _IOFBF,size);
3285 return(status == 0 ? MagickTrue : MagickFalse);
3288#if defined(MAGICKCORE_ZLIB_DELEGATE)
3289static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3291#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3292 return(gzopen(path,mode));
3300 path_wide=NTCreateWidePath(path);
3301 if (path_wide == (
wchar_t *) NULL)
3302 return((gzFile) NULL);
3303 file=gzopen_w(path_wide,mode);
3304 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3310MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3311 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3314 *magick_restrict blob_info;
3317 extension[MagickPathExtent],
3318 filename[MagickPathExtent];
3332 assert(image_info != (ImageInfo *) NULL);
3333 assert(image_info->signature == MagickCoreSignature);
3334 assert(image != (Image *) NULL);
3335 assert(image->signature == MagickCoreSignature);
3336 if (IsEventLogging() != MagickFalse)
3337 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3338 image_info->filename);
3339 blob_info=image->blob;
3340 if (image_info->blob != (
void *) NULL)
3342 if (image_info->stream != (StreamHandler) NULL)
3343 blob_info->stream=(StreamHandler) image_info->stream;
3344 AttachBlob(blob_info,image_info->blob,image_info->length);
3347 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3348 (*image->filename ==
'\0'))
3350 blob_info->type=CustomStream;
3351 blob_info->custom_stream=image_info->custom_stream;
3354 (void) DetachBlob(blob_info);
3355 blob_info->mode=mode;
3362 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3365 case ReadBinaryBlobMode:
3367 flags=O_RDONLY | O_BINARY;
3369 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3374 flags=O_WRONLY | O_CREAT | O_TRUNC;
3376 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
3379 case WriteBinaryBlobMode:
3381 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3383 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
3384 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
3385 MagickTrue : MagickFalse;
3388 case AppendBlobMode:
3390 flags=O_WRONLY | O_CREAT | O_APPEND;
3392 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
3395 case AppendBinaryBlobMode:
3397 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3399 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
3400 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
3401 MagickTrue : MagickFalse;
3408 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3412#if defined(O_NOFOLLOW)
3413 if (status == MagickFalse)
3417 blob_info->synchronize=image_info->synchronize;
3418 if (image_info->stream != (StreamHandler) NULL)
3420 blob_info->stream=image_info->stream;
3423 blob_info->type=FifoStream;
3431 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3432 rights=ReadPolicyRights;
3434 rights=WritePolicyRights;
3435 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3438 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3439 "NotAuthorized",
"`%s'",filename);
3440 return(MagickFalse);
3442 if ((LocaleCompare(filename,
"-") == 0) ||
3443 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3445 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
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=StandardStream;
3451 blob_info->exempt=MagickTrue;
3452 return(SetStreamBuffering(image_info,blob_info));
3454 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3455 (IsGeometry(filename+3) != MagickFalse))
3462 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3463 if (blob_info->file_info.file == (FILE *) NULL)
3465 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3466 return(MagickFalse);
3468#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3469 if (strchr(type,
'b') != (
char *) NULL)
3470 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3472 blob_info->type=FileStream;
3473 blob_info->exempt=MagickTrue;
3474 return(SetStreamBuffering(image_info,blob_info));
3476#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3477 if (*filename ==
'|')
3480 fileMode[MagickPathExtent],
3488 (void) signal(SIGPIPE,SIG_IGN);
3492 sanitize_command=SanitizeString(filename+1);
3493 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3494 sanitize_command=DestroyString(sanitize_command);
3495 if (blob_info->file_info.file == (FILE *) NULL)
3497 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3498 return(MagickFalse);
3500 blob_info->type=PipeStream;
3501 blob_info->exempt=MagickTrue;
3502 return(SetStreamBuffering(image_info,blob_info));
3505 status=GetPathAttributes(filename,&blob_info->properties);
3506#if defined(S_ISFIFO)
3507 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3509 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3510 if (blob_info->file_info.file == (FILE *) NULL)
3512 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3513 return(MagickFalse);
3515 blob_info->type=FileStream;
3516 blob_info->exempt=MagickTrue;
3517 return(SetStreamBuffering(image_info,blob_info));
3520 GetPathComponent(image->filename,ExtensionPath,extension);
3523 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3524 if ((image_info->adjoin == MagickFalse) ||
3525 (strchr(filename,
'%') != (
char *) NULL))
3530 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3531 image->scene,filename,exception);
3532 if ((LocaleCompare(filename,image->filename) == 0) &&
3533 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3534 (GetNextImageInList(image) != (Image *) NULL)))
3537 path[MagickPathExtent];
3539 GetPathComponent(image->filename,RootPath,path);
3540 if (*extension ==
'\0')
3541 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3542 path,(
double) image->scene);
3544 (
void) FormatLocaleString(filename,MagickPathExtent,
3545 "%s-%.20g.%s",path,(
double) image->scene,extension);
3547 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3550 if (image_info->file != (FILE *) NULL)
3552 blob_info->file_info.file=image_info->file;
3553 blob_info->type=FileStream;
3554 blob_info->exempt=MagickTrue;
3562 blob_info->file_info.file=(FILE *) NULL;
3563 file=open_utf8(filename,flags,0);
3565 blob_info->file_info.file=fdopen(file,type);
3566 if (blob_info->file_info.file != (FILE *) NULL)
3574 blob_info->type=FileStream;
3575 (void) SetStreamBuffering(image_info,blob_info);
3576 (void) memset(magick,0,
sizeof(magick));
3577 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3578 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3579#if defined(MAGICKCORE_POSIX_SUPPORT)
3580 (void) fflush(blob_info->file_info.file);
3582 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3583 " read %.20g magic header bytes",(
double) count);
3584#if defined(MAGICKCORE_ZLIB_DELEGATE)
3585 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3586 ((
int) magick[2] == 0x08))
3589 gzfile = gzopen_utf8(filename,
"rb");
3591 if (gzfile != (gzFile) NULL)
3593 if (blob_info->file_info.file != (FILE *) NULL)
3594 (void) fclose(blob_info->file_info.file);
3595 blob_info->file_info.file=(FILE *) NULL;
3596 blob_info->file_info.gzfile=gzfile;
3597 blob_info->type=ZipStream;
3601#if defined(MAGICKCORE_BZLIB_DELEGATE)
3602 if (strncmp((
char *) magick,
"BZh",3) == 0)
3605 *bzfile = BZ2_bzopen(filename,
"r");
3607 if (bzfile != (BZFILE *) NULL)
3609 if (blob_info->file_info.file != (FILE *) NULL)
3610 (void) fclose(blob_info->file_info.file);
3611 blob_info->file_info.file=(FILE *) NULL;
3612 blob_info->file_info.bzfile=bzfile;
3613 blob_info->type=BZipStream;
3617 if (blob_info->type == FileStream)
3628 sans_exception=AcquireExceptionInfo();
3629 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3630 sans_exception=DestroyExceptionInfo(sans_exception);
3631 length=(size_t) blob_info->properties.st_size;
3632 if ((magick_info != (
const MagickInfo *) NULL) &&
3633 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3634 (length > MagickMaxBufferExtent) &&
3635 (AcquireMagickResource(MapResource,length) != MagickFalse))
3640 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3642 if (blob == (
void *) NULL)
3643 RelinquishMagickResource(MapResource,length);
3649 if (image_info->file != (FILE *) NULL)
3650 blob_info->exempt=MagickFalse;
3653 (void) fclose(blob_info->file_info.file);
3654 blob_info->file_info.file=(FILE *) NULL;
3656 AttachBlob(blob_info,blob,length);
3657 blob_info->mapped=MagickTrue;
3664#if defined(MAGICKCORE_ZLIB_DELEGATE)
3665 if ((LocaleCompare(extension,
"gz") == 0) ||
3666 (LocaleCompare(extension,
"wmz") == 0) ||
3667 (LocaleCompare(extension,
"svgz") == 0))
3669 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3670 if (blob_info->file_info.gzfile != (gzFile) NULL)
3671 blob_info->type=ZipStream;
3675#if defined(MAGICKCORE_BZLIB_DELEGATE)
3676 if (LocaleCompare(extension,
"bz2") == 0)
3678 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3679 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3680 blob_info->type=BZipStream;
3688 blob_info->file_info.file=(FILE *) NULL;
3689 file=open_utf8(filename,flags,P_MODE);
3691 blob_info->file_info.file=fdopen(file,type);
3692 if (blob_info->file_info.file != (FILE *) NULL)
3694 blob_info->type=FileStream;
3695 (void) SetStreamBuffering(image_info,blob_info);
3698 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3701 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3702 "NotAuthorized",
"`%s'",filename);
3703 return(MagickFalse);
3705 blob_info->status=0;
3706 blob_info->error_number=0;
3707 if (blob_info->type != UndefinedStream)
3708 blob_info->size=GetBlobSize(image);
3711 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3712 return(MagickFalse);
3751#if defined(__cplusplus) || defined(c_plusplus)
3755static size_t PingStream(
const Image *magick_unused(image),
3756 const void *magick_unused(pixels),
const size_t columns)
3758 magick_unreferenced(image);
3759 magick_unreferenced(pixels);
3763#if defined(__cplusplus) || defined(c_plusplus)
3767MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3768 const size_t length,ExceptionInfo *exception)
3783 assert(image_info != (ImageInfo *) NULL);
3784 assert(image_info->signature == MagickCoreSignature);
3785 assert(exception != (ExceptionInfo *) NULL);
3786 if (IsEventLogging() != MagickFalse)
3787 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3788 image_info->filename);
3789 if ((blob == (
const void *) NULL) || (length == 0))
3791 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3792 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3793 return((Image *) NULL);
3795 ping_info=CloneImageInfo(image_info);
3796 ping_info->blob=(
void *) blob;
3797 ping_info->length=length;
3798 ping_info->ping=MagickTrue;
3799 if (*ping_info->magick ==
'\0')
3800 (void) SetImageInfo(ping_info,0,exception);
3801 magick_info=GetMagickInfo(ping_info->magick,exception);
3802 if (magick_info == (
const MagickInfo *) NULL)
3804 (void) ThrowMagickException(exception,GetMagickModule(),
3805 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3807 ping_info=DestroyImageInfo(ping_info);
3808 return((Image *) NULL);
3810 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3813 filename[MagickPathExtent];
3818 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3819 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3820 ping_info->magick,filename);
3821 image=ReadStream(ping_info,&PingStream,exception);
3822 if (image != (Image *) NULL)
3823 (void) DetachBlob(image->blob);
3824 ping_info=DestroyImageInfo(ping_info);
3830 ping_info->blob=(
void *) NULL;
3831 ping_info->length=0;
3832 *ping_info->filename=
'\0';
3833 status=BlobToFile(ping_info->filename,blob,length,exception);
3834 if (status == MagickFalse)
3836 (void) RelinquishUniqueFileResource(ping_info->filename);
3837 ping_info=DestroyImageInfo(ping_info);
3838 return((Image *) NULL);
3840 clone_info=CloneImageInfo(ping_info);
3841 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3842 ping_info->magick,ping_info->filename);
3843 image=ReadStream(clone_info,&PingStream,exception);
3844 if (image != (Image *) NULL)
3852 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3854 (void) CopyMagickString(images->filename,image_info->filename,
3856 (void) CopyMagickString(images->magick_filename,image_info->filename,
3858 (void) CopyMagickString(images->magick,magick_info->name,
3860 images=GetNextImageInList(images);
3863 clone_info=DestroyImageInfo(clone_info);
3864 (void) RelinquishUniqueFileResource(ping_info->filename);
3865 ping_info=DestroyImageInfo(ping_info);
3900MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3903 *magick_restrict blob_info;
3914 assert(image != (Image *) NULL);
3915 assert(image->signature == MagickCoreSignature);
3916 assert(image->blob != (BlobInfo *) NULL);
3917 assert(image->blob->type != UndefinedStream);
3920 assert(data != (
void *) NULL);
3921 blob_info=image->blob;
3923 q=(
unsigned char *) data;
3924 switch (blob_info->type)
3926 case UndefinedStream:
3928 case StandardStream:
3936 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3941 c=getc(blob_info->file_info.file);
3944 *q++=(
unsigned char) c;
3950 c=getc(blob_info->file_info.file);
3953 *q++=(
unsigned char) c;
3959 c=getc(blob_info->file_info.file);
3962 *q++=(
unsigned char) c;
3968 c=getc(blob_info->file_info.file);
3971 *q++=(
unsigned char) c;
3978 if ((count != (ssize_t) length) &&
3979 (ferror(blob_info->file_info.file) != 0))
3980 ThrowBlobException(blob_info);
3985#if defined(MAGICKCORE_ZLIB_DELEGATE)
3996 for (i=0; i < length; i+=(size_t) count)
3998 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3999 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4012 c=gzgetc(blob_info->file_info.gzfile);
4015 *q++=(
unsigned char) c;
4021 c=gzgetc(blob_info->file_info.gzfile);
4024 *q++=(
unsigned char) c;
4030 c=gzgetc(blob_info->file_info.gzfile);
4033 *q++=(
unsigned char) c;
4039 c=gzgetc(blob_info->file_info.gzfile);
4042 *q++=(
unsigned char) c;
4049 (void) gzerror(blob_info->file_info.gzfile,&status);
4050 if ((count != (ssize_t) length) && (status != Z_OK))
4051 ThrowBlobException(blob_info);
4052 if (blob_info->eof == MagickFalse)
4053 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4060#if defined(MAGICKCORE_BZLIB_DELEGATE)
4067 for (i=0; i < length; i+=(size_t) count)
4069 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4070 MagickMin(length-i,MagickMaxBufferExtent));
4080 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4081 if ((count != (ssize_t) length) && (status != BZ_OK))
4082 ThrowBlobException(blob_info);
4093 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4095 blob_info->eof=MagickTrue;
4098 p=blob_info->data+blob_info->offset;
4099 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4100 blob_info->length-blob_info->offset);
4101 blob_info->offset+=count;
4102 if (count != (ssize_t) length)
4103 blob_info->eof=MagickTrue;
4104 (void) memcpy(q,p,(
size_t) count);
4109 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4110 count=blob_info->custom_stream->reader(q,length,
4111 blob_info->custom_stream->data);
4140MagickExport
int ReadBlobByte(Image *image)
4143 *magick_restrict blob_info;
4148 assert(image != (Image *) NULL);
4149 assert(image->signature == MagickCoreSignature);
4150 assert(image->blob != (BlobInfo *) NULL);
4151 assert(image->blob->type != UndefinedStream);
4152 blob_info=image->blob;
4153 switch (blob_info->type)
4155 case StandardStream:
4159 c=getc(blob_info->file_info.file);
4162 if (ferror(blob_info->file_info.file) != 0)
4163 ThrowBlobException(blob_info);
4170 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4172 blob_info->eof=MagickTrue;
4175 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4176 blob_info->offset++;
4187 count=ReadBlob(image,1,buffer);
4220MagickExport
double ReadBlobDouble(Image *image)
4231 quantum.double_value=0.0;
4232 quantum.unsigned_value=ReadBlobLongLong(image);
4233 return(quantum.double_value);
4259MagickExport
float ReadBlobFloat(Image *image)
4270 quantum.float_value=0.0;
4271 quantum.unsigned_value=ReadBlobLong(image);
4272 return(quantum.float_value);
4298MagickExport
unsigned int ReadBlobLong(Image *image)
4312 assert(image != (Image *) NULL);
4313 assert(image->signature == MagickCoreSignature);
4315 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4318 if (image->endian == LSBEndian)
4320 value=(
unsigned int) (*p++);
4321 value|=(
unsigned int) (*p++) << 8;
4322 value|=(
unsigned int) (*p++) << 16;
4323 value|=(
unsigned int) (*p++) << 24;
4326 value=(
unsigned int) (*p++) << 24;
4327 value|=(
unsigned int) (*p++) << 16;
4328 value|=(
unsigned int) (*p++) << 8;
4329 value|=(
unsigned int) (*p++);
4356MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4370 assert(image != (Image *) NULL);
4371 assert(image->signature == MagickCoreSignature);
4373 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4375 return(MagickULLConstant(0));
4376 if (image->endian == LSBEndian)
4378 value=(MagickSizeType) (*p++);
4379 value|=(MagickSizeType) (*p++) << 8;
4380 value|=(MagickSizeType) (*p++) << 16;
4381 value|=(MagickSizeType) (*p++) << 24;
4382 value|=(MagickSizeType) (*p++) << 32;
4383 value|=(MagickSizeType) (*p++) << 40;
4384 value|=(MagickSizeType) (*p++) << 48;
4385 value|=(MagickSizeType) (*p++) << 56;
4388 value=(MagickSizeType) (*p++) << 56;
4389 value|=(MagickSizeType) (*p++) << 48;
4390 value|=(MagickSizeType) (*p++) << 40;
4391 value|=(MagickSizeType) (*p++) << 32;
4392 value|=(MagickSizeType) (*p++) << 24;
4393 value|=(MagickSizeType) (*p++) << 16;
4394 value|=(MagickSizeType) (*p++) << 8;
4395 value|=(MagickSizeType) (*p++);
4422MagickExport
unsigned short ReadBlobShort(Image *image)
4436 assert(image != (Image *) NULL);
4437 assert(image->signature == MagickCoreSignature);
4439 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4441 return((
unsigned short) 0U);
4442 if (image->endian == LSBEndian)
4444 value=(
unsigned short) (*p++);
4445 value|=(
unsigned short) (*p++) << 8;
4448 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4449 value|=(
unsigned short) (*p++);
4476MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4490 assert(image != (Image *) NULL);
4491 assert(image->signature == MagickCoreSignature);
4493 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4496 value=(
unsigned int) (*p++);
4497 value|=(
unsigned int) (*p++) << 8;
4498 value|=(
unsigned int) (*p++) << 16;
4499 value|=(
unsigned int) (*p++) << 24;
4526MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4537 quantum.unsigned_value=ReadBlobLSBLong(image);
4538 return(quantum.signed_value);
4564MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4578 assert(image != (Image *) NULL);
4579 assert(image->signature == MagickCoreSignature);
4581 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4583 return((
unsigned short) 0U);
4584 value=(
unsigned short) (*p++);
4585 value|=(
unsigned short) (*p++) << 8;
4612MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4623 quantum.unsigned_value=ReadBlobLSBShort(image);
4624 return(quantum.signed_value);
4650MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4664 assert(image != (Image *) NULL);
4665 assert(image->signature == MagickCoreSignature);
4667 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4670 value=(
unsigned int) (*p++) << 24;
4671 value|=(
unsigned int) (*p++) << 16;
4672 value|=(
unsigned int) (*p++) << 8;
4673 value|=(
unsigned int) (*p++);
4700MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4714 assert(image != (Image *) NULL);
4715 assert(image->signature == MagickCoreSignature);
4717 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4719 return(MagickULLConstant(0));
4720 value=(MagickSizeType) (*p++) << 56;
4721 value|=(MagickSizeType) (*p++) << 48;
4722 value|=(MagickSizeType) (*p++) << 40;
4723 value|=(MagickSizeType) (*p++) << 32;
4724 value|=(MagickSizeType) (*p++) << 24;
4725 value|=(MagickSizeType) (*p++) << 16;
4726 value|=(MagickSizeType) (*p++) << 8;
4727 value|=(MagickSizeType) (*p++);
4754MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4768 assert(image != (Image *) NULL);
4769 assert(image->signature == MagickCoreSignature);
4771 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4773 return((
unsigned short) 0U);
4774 value=(
unsigned short) ((*p++) << 8);
4775 value|=(
unsigned short) (*p++);
4776 return((
unsigned short) (value & 0xffff));
4802MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4813 quantum.unsigned_value=ReadBlobMSBLong(image);
4814 return(quantum.signed_value);
4840MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4851 quantum.unsigned_value=ReadBlobMSBShort(image);
4852 return(quantum.signed_value);
4878MagickExport
signed int ReadBlobSignedLong(Image *image)
4889 quantum.unsigned_value=ReadBlobLong(image);
4890 return(quantum.signed_value);
4916MagickExport
signed short ReadBlobSignedShort(Image *image)
4927 quantum.unsigned_value=ReadBlobShort(image);
4928 return(quantum.signed_value);
4966MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4967 const size_t length,
void *magick_restrict data,ssize_t *count)
4970 *magick_restrict blob_info;
4972 assert(image != (Image *) NULL);
4973 assert(image->signature == MagickCoreSignature);
4974 assert(image->blob != (BlobInfo *) NULL);
4975 assert(image->blob->type != UndefinedStream);
4976 assert(count != (ssize_t *) NULL);
4977 blob_info=image->blob;
4978 if (blob_info->type != BlobStream)
4980 assert(data != NULL);
4981 *count=ReadBlob(image,length,(
unsigned char *) data);
4984 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4987 blob_info->eof=MagickTrue;
4990 data=blob_info->data+blob_info->offset;
4991 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4992 blob_info->length-blob_info->offset);
4993 blob_info->offset+=(*count);
4994 if (*count != (ssize_t) length)
4995 blob_info->eof=MagickTrue;
5024MagickExport
char *ReadBlobString(Image *image,
char *
string)
5027 *magick_restrict blob_info;
5035 assert(image != (Image *) NULL);
5036 assert(image->signature == MagickCoreSignature);
5037 assert(image->blob != (BlobInfo *) NULL);
5038 assert(image->blob->type != UndefinedStream);
5039 if (IsEventLogging() != MagickFalse)
5040 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5042 blob_info=image->blob;
5043 switch (blob_info->type)
5045 case UndefinedStream:
5047 case StandardStream:
5050 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5051 if (p == (
char *) NULL)
5053 if (ferror(blob_info->file_info.file) != 0)
5054 ThrowBlobException(blob_info);
5055 return((
char *) NULL);
5062#if defined(MAGICKCORE_ZLIB_DELEGATE)
5063 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5064 if (p == (
char *) NULL)
5067 (void) gzerror(blob_info->file_info.gzfile,&status);
5069 ThrowBlobException(blob_info);
5070 return((
char *) NULL);
5080 c=ReadBlobByte(image);
5083 blob_info->eof=MagickTrue;
5086 string[i++]=(char) c;
5089 }
while (i < (MaxTextExtent-2));
5097 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5100 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5102 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5103 return((
char *) NULL);
5130MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5132 assert(blob != (BlobInfo *) NULL);
5133 assert(blob->signature == MagickCoreSignature);
5134 if (IsEventLogging() != MagickFalse)
5135 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5136 LockSemaphoreInfo(blob->semaphore);
5137 blob->reference_count++;
5138 UnlockSemaphoreInfo(blob->semaphore);
5175MagickExport MagickOffsetType SeekBlob(Image *image,
5176 const MagickOffsetType offset,
const int whence)
5179 *magick_restrict blob_info;
5181 assert(image != (Image *) NULL);
5182 assert(image->signature == MagickCoreSignature);
5183 assert(image->blob != (BlobInfo *) NULL);
5184 assert(image->blob->type != UndefinedStream);
5185 if (IsEventLogging() != MagickFalse)
5186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5187 blob_info=image->blob;
5188 switch (blob_info->type)
5190 case UndefinedStream:
5192 case StandardStream:
5197 if ((offset < 0) && (whence == SEEK_SET))
5199 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5201 blob_info->offset=TellBlob(image);
5206#if defined(MAGICKCORE_ZLIB_DELEGATE)
5207 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5210 blob_info->offset=TellBlob(image);
5226 blob_info->offset=offset;
5231 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5232 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5237 if ((blob_info->offset+offset) < 0)
5239 blob_info->offset+=offset;
5244 if (((MagickOffsetType) blob_info->length+offset) < 0)
5246 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5250 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5252 blob_info->eof=MagickFalse;
5259 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5261 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5262 blob_info->custom_stream->data);
5266 return(blob_info->offset);
5294MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5296 assert(image != (
const Image *) NULL);
5297 assert(image->signature == MagickCoreSignature);
5298 if (IsEventLogging() != MagickFalse)
5299 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5300 image->blob->exempt=exempt;
5329MagickExport MagickBooleanType SetBlobExtent(Image *image,
5330 const MagickSizeType extent)
5333 *magick_restrict blob_info;
5335 assert(image != (Image *) NULL);
5336 assert(image->signature == MagickCoreSignature);
5337 assert(image->blob != (BlobInfo *) NULL);
5338 assert(image->blob->type != UndefinedStream);
5339 if (IsEventLogging() != MagickFalse)
5340 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5341 blob_info=image->blob;
5342 switch (blob_info->type)
5344 case UndefinedStream:
5346 case StandardStream:
5347 return(MagickFalse);
5356 if (extent != (MagickSizeType) ((off_t) extent))
5357 return(MagickFalse);
5358 offset=SeekBlob(image,0,SEEK_END);
5360 return(MagickFalse);
5361 if ((MagickSizeType) offset >= extent)
5363 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5366 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5367 blob_info->file_info.file);
5368#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5369 if (blob_info->synchronize != MagickFalse)
5374 file=fileno(blob_info->file_info.file);
5375 if ((file == -1) || (offset < 0))
5376 return(MagickFalse);
5377 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5380 offset=SeekBlob(image,offset,SEEK_SET);
5382 return(MagickFalse);
5387 return(MagickFalse);
5389 return(MagickFalse);
5391 return(MagickFalse);
5394 if (extent != (MagickSizeType) ((
size_t) extent))
5395 return(MagickFalse);
5396 if (blob_info->mapped != MagickFalse)
5404 (void) UnmapBlob(blob_info->data,blob_info->length);
5405 RelinquishMagickResource(MapResource,blob_info->length);
5406 if (extent != (MagickSizeType) ((off_t) extent))
5407 return(MagickFalse);
5408 offset=SeekBlob(image,0,SEEK_END);
5410 return(MagickFalse);
5411 if ((MagickSizeType) offset >= extent)
5413 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5414 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5415 blob_info->file_info.file);
5416#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5417 if (blob_info->synchronize != MagickFalse)
5422 file=fileno(blob_info->file_info.file);
5423 if ((file == -1) || (offset < 0))
5424 return(MagickFalse);
5425 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5429 offset=SeekBlob(image,offset,SEEK_SET);
5431 return(MagickFalse);
5432 (void) AcquireMagickResource(MapResource,extent);
5433 blob_info->data=(
unsigned char*) MapBlob(fileno(
5434 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5435 blob_info->extent=(size_t) extent;
5436 blob_info->length=(size_t) extent;
5437 (void) SyncBlob(image);
5440 blob_info->extent=(size_t) extent;
5441 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5442 blob_info->extent+1,
sizeof(*blob_info->data));
5443 (void) SyncBlob(image);
5444 if (blob_info->data == (
unsigned char *) NULL)
5446 (void) DetachBlob(blob_info);
5447 return(MagickFalse);
5481MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5484 assert(custom_stream != (CustomStreamInfo *) NULL);
5485 assert(custom_stream->signature == MagickCoreSignature);
5486 custom_stream->data=data;
5514MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5515 CustomStreamHandler reader)
5517 assert(custom_stream != (CustomStreamInfo *) NULL);
5518 assert(custom_stream->signature == MagickCoreSignature);
5519 custom_stream->reader=reader;
5547MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5548 CustomStreamSeeker seeker)
5550 assert(custom_stream != (CustomStreamInfo *) NULL);
5551 assert(custom_stream->signature == MagickCoreSignature);
5552 custom_stream->seeker=seeker;
5580MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5581 CustomStreamTeller teller)
5583 assert(custom_stream != (CustomStreamInfo *) NULL);
5584 assert(custom_stream->signature == MagickCoreSignature);
5585 custom_stream->teller=teller;
5613MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5614 CustomStreamHandler writer)
5616 assert(custom_stream != (CustomStreamInfo *) NULL);
5617 assert(custom_stream->signature == MagickCoreSignature);
5618 custom_stream->writer=writer;
5645static int SyncBlob(
const Image *image)
5648 *magick_restrict blob_info;
5653 assert(image != (Image *) NULL);
5654 assert(image->signature == MagickCoreSignature);
5655 assert(image->blob != (BlobInfo *) NULL);
5656 if (IsEventLogging() != MagickFalse)
5657 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5658 if (EOFBlob(image) != 0)
5660 blob_info=image->blob;
5662 switch (blob_info->type)
5664 case UndefinedStream:
5665 case StandardStream:
5670 status=fflush(blob_info->file_info.file);
5675#if defined(MAGICKCORE_ZLIB_DELEGATE)
5676 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5682#if defined(MAGICKCORE_BZLIB_DELEGATE)
5683 status=BZ2_bzflush(blob_info->file_info.bzfile);
5719MagickExport MagickOffsetType TellBlob(
const Image *image)
5722 *magick_restrict blob_info;
5727 assert(image != (Image *) NULL);
5728 assert(image->signature == MagickCoreSignature);
5729 assert(image->blob != (BlobInfo *) NULL);
5730 assert(image->blob->type != UndefinedStream);
5731 if (IsEventLogging() != MagickFalse)
5732 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5733 blob_info=image->blob;
5735 switch (blob_info->type)
5737 case UndefinedStream:
5738 case StandardStream:
5742 offset=ftell(blob_info->file_info.file);
5749#if defined(MAGICKCORE_ZLIB_DELEGATE)
5750 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5760 offset=blob_info->offset;
5765 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5766 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5798MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5800#if defined(MAGICKCORE_HAVE_MMAP)
5804 status=munmap(map,length);
5805 return(status == -1 ? MagickFalse : MagickTrue);
5809 return(MagickFalse);
5841MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5845 *magick_restrict blob_info;
5859 assert(image != (Image *) NULL);
5860 assert(image->signature == MagickCoreSignature);
5861 assert(image->blob != (BlobInfo *) NULL);
5862 assert(image->blob->type != UndefinedStream);
5865 assert(data != (
const void *) NULL);
5866 blob_info=image->blob;
5868 p=(
const unsigned char *) data;
5869 q=(
unsigned char *) data;
5870 switch (blob_info->type)
5872 case UndefinedStream:
5874 case StandardStream:
5882 count=(ssize_t) fwrite((
const char *) data,1,length,
5883 blob_info->file_info.file);
5888 c=putc((
int) *p++,blob_info->file_info.file);
5896 c=putc((
int) *p++,blob_info->file_info.file);
5904 c=putc((
int) *p++,blob_info->file_info.file);
5912 c=putc((
int) *p++,blob_info->file_info.file);
5921 if ((count != (ssize_t) length) &&
5922 (ferror(blob_info->file_info.file) != 0))
5923 ThrowBlobException(blob_info);
5928#if defined(MAGICKCORE_ZLIB_DELEGATE)
5939 for (i=0; i < length; i+=(size_t) count)
5941 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5942 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5955 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5963 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5971 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5979 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5989 (void) gzerror(blob_info->file_info.gzfile,&status);
5990 if ((count != (ssize_t) length) && (status != Z_OK))
5991 ThrowBlobException(blob_info);
5997#if defined(MAGICKCORE_BZLIB_DELEGATE)
6004 for (i=0; i < length; i+=(size_t) count)
6006 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6007 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6017 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6018 if ((count != (ssize_t) length) && (status != BZ_OK))
6019 ThrowBlobException(blob_info);
6025 count=(ssize_t) blob_info->stream(image,data,length);
6033 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6038 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6039 if (extent >= blob_info->extent)
6041 extent+=blob_info->quantum+length;
6042 blob_info->quantum<<=1;
6043 if (SetBlobExtent(image,extent) == MagickFalse)
6046 q=blob_info->data+blob_info->offset;
6047 (void) memcpy(q,p,length);
6048 blob_info->offset+=(MagickOffsetType) length;
6049 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6050 blob_info->length=(size_t) blob_info->offset;
6051 count=(ssize_t) length;
6056 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6057 count=blob_info->custom_stream->writer((
unsigned char *) data,
6058 length,blob_info->custom_stream->data);
6090MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6093 *magick_restrict blob_info;
6098 assert(image != (Image *) NULL);
6099 assert(image->signature == MagickCoreSignature);
6100 assert(image->blob != (BlobInfo *) NULL);
6101 assert(image->blob->type != UndefinedStream);
6102 blob_info=image->blob;
6104 switch (blob_info->type)
6106 case StandardStream:
6113 c=putc((
int) value,blob_info->file_info.file);
6116 if (ferror(blob_info->file_info.file) != 0)
6117 ThrowBlobException(blob_info);
6125 count=WriteBlobStream(image,1,&value);
6157MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6168 quantum.unsigned_value=0U;
6169 quantum.float_value=value;
6170 return(WriteBlobLong(image,quantum.unsigned_value));
6198MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6203 assert(image != (Image *) NULL);
6204 assert(image->signature == MagickCoreSignature);
6205 if (image->endian == LSBEndian)
6207 buffer[0]=(
unsigned char) value;
6208 buffer[1]=(
unsigned char) (value >> 8);
6209 buffer[2]=(
unsigned char) (value >> 16);
6210 buffer[3]=(
unsigned char) (value >> 24);
6211 return(WriteBlobStream(image,4,buffer));
6213 buffer[0]=(
unsigned char) (value >> 24);
6214 buffer[1]=(
unsigned char) (value >> 16);
6215 buffer[2]=(
unsigned char) (value >> 8);
6216 buffer[3]=(
unsigned char) value;
6217 return(WriteBlobStream(image,4,buffer));
6245MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6250 assert(image != (Image *) NULL);
6251 assert(image->signature == MagickCoreSignature);
6252 if (image->endian == LSBEndian)
6254 buffer[0]=(
unsigned char) value;
6255 buffer[1]=(
unsigned char) (value >> 8);
6256 buffer[2]=(
unsigned char) (value >> 16);
6257 buffer[3]=(
unsigned char) (value >> 24);
6258 buffer[4]=(
unsigned char) (value >> 32);
6259 buffer[5]=(
unsigned char) (value >> 40);
6260 buffer[6]=(
unsigned char) (value >> 48);
6261 buffer[7]=(
unsigned char) (value >> 56);
6262 return(WriteBlobStream(image,8,buffer));
6264 buffer[0]=(
unsigned char) (value >> 56);
6265 buffer[1]=(
unsigned char) (value >> 48);
6266 buffer[2]=(
unsigned char) (value >> 40);
6267 buffer[3]=(
unsigned char) (value >> 32);
6268 buffer[4]=(
unsigned char) (value >> 24);
6269 buffer[5]=(
unsigned char) (value >> 16);
6270 buffer[6]=(
unsigned char) (value >> 8);
6271 buffer[7]=(
unsigned char) value;
6272 return(WriteBlobStream(image,8,buffer));
6300MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6305 assert(image != (Image *) NULL);
6306 assert(image->signature == MagickCoreSignature);
6307 if (image->endian == LSBEndian)
6309 buffer[0]=(
unsigned char) value;
6310 buffer[1]=(
unsigned char) (value >> 8);
6311 return(WriteBlobStream(image,2,buffer));
6313 buffer[0]=(
unsigned char) (value >> 8);
6314 buffer[1]=(
unsigned char) value;
6315 return(WriteBlobStream(image,2,buffer));
6343MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6357 assert(image != (Image *) NULL);
6358 assert(image->signature == MagickCoreSignature);
6359 quantum.signed_value=value;
6360 if (image->endian == LSBEndian)
6362 buffer[0]=(
unsigned char) quantum.unsigned_value;
6363 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6364 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6365 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6366 return(WriteBlobStream(image,4,buffer));
6368 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6369 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6370 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6371 buffer[3]=(
unsigned char) quantum.unsigned_value;
6372 return(WriteBlobStream(image,4,buffer));
6400MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6405 assert(image != (Image *) NULL);
6406 assert(image->signature == MagickCoreSignature);
6407 buffer[0]=(
unsigned char) value;
6408 buffer[1]=(
unsigned char) (value >> 8);
6409 buffer[2]=(
unsigned char) (value >> 16);
6410 buffer[3]=(
unsigned char) (value >> 24);
6411 return(WriteBlobStream(image,4,buffer));
6439MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6444 assert(image != (Image *) NULL);
6445 assert(image->signature == MagickCoreSignature);
6446 buffer[0]=(
unsigned char) value;
6447 buffer[1]=(
unsigned char) (value >> 8);
6448 return(WriteBlobStream(image,2,buffer));
6476MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6490 assert(image != (Image *) NULL);
6491 assert(image->signature == MagickCoreSignature);
6492 quantum.signed_value=value;
6493 buffer[0]=(
unsigned char) quantum.unsigned_value;
6494 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6495 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6496 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6497 return(WriteBlobStream(image,4,buffer));
6525MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6526 const signed short value)
6540 assert(image != (Image *) NULL);
6541 assert(image->signature == MagickCoreSignature);
6542 quantum.signed_value=value;
6543 buffer[0]=(
unsigned char) quantum.unsigned_value;
6544 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6545 return(WriteBlobStream(image,2,buffer));
6573MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6578 assert(image != (Image *) NULL);
6579 assert(image->signature == MagickCoreSignature);
6580 buffer[0]=(
unsigned char) (value >> 24);
6581 buffer[1]=(
unsigned char) (value >> 16);
6582 buffer[2]=(
unsigned char) (value >> 8);
6583 buffer[3]=(
unsigned char) value;
6584 return(WriteBlobStream(image,4,buffer));
6612MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6613 const signed short value)
6627 assert(image != (Image *) NULL);
6628 assert(image->signature == MagickCoreSignature);
6629 quantum.signed_value=value;
6630 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6631 buffer[1]=(
unsigned char) quantum.unsigned_value;
6632 return(WriteBlobStream(image,2,buffer));
6660MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6665 assert(image != (Image *) NULL);
6666 assert(image->signature == MagickCoreSignature);
6667 buffer[0]=(
unsigned char) (value >> 8);
6668 buffer[1]=(
unsigned char) value;
6669 return(WriteBlobStream(image,2,buffer));
6697MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6699 assert(image != (Image *) NULL);
6700 assert(image->signature == MagickCoreSignature);
6701 assert(
string != (
const char *) NULL);
6702 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));