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/policy-private.h"
68#include "MagickCore/resource_.h"
69#include "MagickCore/semaphore.h"
70#include "MagickCore/string_.h"
71#include "MagickCore/string-private.h"
72#include "MagickCore/timer-private.h"
73#include "MagickCore/token.h"
74#include "MagickCore/utility.h"
75#include "MagickCore/utility-private.h"
76#if defined(MAGICKCORE_ZLIB_DELEGATE)
79#if defined(MAGICKCORE_BZLIB_DELEGATE)
86#define MagickMaxBlobExtent (8*8192)
87#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
88# define MAP_ANONYMOUS MAP_ANON
90#if !defined(MAP_FAILED)
91#define MAP_FAILED ((void *) -1)
95#define _O_BINARY O_BINARY
97#if defined(MAGICKCORE_WINDOWS_SUPPORT)
102# define MAGICKCORE_HAVE_MMAP 1
103# define mmap(address,length,protection,access,file,offset) \
104 NTMapMemory(address,length,protection,access,file,offset)
107# define munmap(address,length) NTUnmapMemory(address,length)
110# define pclose _pclose
125#if defined(MAGICKCORE_ZLIB_DELEGATE)
130#if defined(MAGICKCORE_BZLIB_DELEGATE)
222 SyncBlob(
const Image *);
246MagickExport CustomStreamInfo *AcquireCustomStreamInfo(
247 ExceptionInfo *magick_unused(exception))
252 magick_unreferenced(exception);
253 custom_stream=(CustomStreamInfo *) AcquireCriticalMemory(
254 sizeof(*custom_stream));
255 (void) memset(custom_stream,0,
sizeof(*custom_stream));
256 custom_stream->signature=MagickCoreSignature;
257 return(custom_stream);
287MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
290 assert(blob_info != (BlobInfo *) NULL);
291 if (IsEventLogging() != MagickFalse)
292 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
293 blob_info->length=length;
294 blob_info->extent=length;
295 blob_info->quantum=(size_t) MagickMaxBlobExtent;
297 blob_info->type=BlobStream;
298 blob_info->file_info.file=(FILE *) NULL;
299 blob_info->data=(
unsigned char *) blob;
300 blob_info->mapped=MagickFalse;
328MagickExport
void AttachCustomStream(BlobInfo *blob_info,
329 CustomStreamInfo *custom_stream)
331 assert(blob_info != (BlobInfo *) NULL);
332 assert(custom_stream != (CustomStreamInfo *) NULL);
333 assert(custom_stream->signature == MagickCoreSignature);
334 if (IsEventLogging() != MagickFalse)
335 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
336 blob_info->type=CustomStream;
337 blob_info->custom_stream=custom_stream;
371MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
372 const size_t length,ExceptionInfo *exception)
383 assert(filename != (
const char *) NULL);
384 assert(blob != (
const void *) NULL);
385 if (IsEventLogging() != MagickFalse)
386 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
387 if (*filename ==
'\0')
388 file=AcquireUniqueFileResource(filename);
390 file=open_utf8(filename,O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL | O_BINARY,P_MODE);
393 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
396 for (i=0; i < length; i+=(size_t) count)
398 count=MagickWrite(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
399 MagickMaxBufferExtent));
403 file=close_utf8(file);
404 if ((file == -1) || (i < length))
406 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
443MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
444 const size_t length,ExceptionInfo *exception)
459 assert(image_info != (ImageInfo *) NULL);
460 assert(image_info->signature == MagickCoreSignature);
461 assert(exception != (ExceptionInfo *) NULL);
462 if (IsEventLogging() != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
464 image_info->filename);
465 if ((blob == (
const void *) NULL) || (length == 0))
467 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
468 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
469 return((Image *) NULL);
471 blob_info=CloneImageInfo(image_info);
472 blob_info->blob=(
void *) blob;
473 blob_info->length=length;
474 if (*blob_info->magick ==
'\0')
475 (void) SetImageInfo(blob_info,0,exception);
476 magick_info=GetMagickInfo(blob_info->magick,exception);
477 if (magick_info == (
const MagickInfo *) NULL)
479 (void) ThrowMagickException(exception,GetMagickModule(),
480 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
482 blob_info=DestroyImageInfo(blob_info);
483 return((Image *) NULL);
485 if (GetMagickBlobSupport(magick_info) != MagickFalse)
488 filename[MagickPathExtent];
493 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
494 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
495 blob_info->magick,filename);
496 image=ReadImage(blob_info,exception);
497 if (image != (Image *) NULL)
498 (void) DetachBlob(image->blob);
499 blob_info=DestroyImageInfo(blob_info);
505 blob_info->blob=(
void *) NULL;
507 *blob_info->filename=
'\0';
508 status=BlobToFile(blob_info->filename,blob,length,exception);
509 if (status == MagickFalse)
511 (void) RelinquishUniqueFileResource(blob_info->filename);
512 blob_info=DestroyImageInfo(blob_info);
513 return((Image *) NULL);
515 clone_info=CloneImageInfo(blob_info);
516 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
517 blob_info->magick,blob_info->filename);
518 image=ReadImage(clone_info,exception);
519 if (image != (Image *) NULL)
527 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
529 (void) CopyMagickString(images->filename,image_info->filename,
531 (void) CopyMagickString(images->magick_filename,image_info->filename,
533 (void) CopyMagickString(images->magick,magick_info->name,
535 images=GetNextImageInList(images);
538 clone_info=DestroyImageInfo(clone_info);
539 (void) RelinquishUniqueFileResource(blob_info->filename);
540 blob_info=DestroyImageInfo(blob_info);
567MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
575 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
576 GetBlobInfo(clone_info);
577 if (blob_info == (BlobInfo *) NULL)
579 semaphore=clone_info->semaphore;
580 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
581 if (blob_info->mapped != MagickFalse)
582 (void) AcquireMagickResource(MapResource,blob_info->length);
583 clone_info->semaphore=semaphore;
584 LockSemaphoreInfo(clone_info->semaphore);
585 clone_info->reference_count=1;
586 UnlockSemaphoreInfo(clone_info->semaphore);
613static inline void ThrowBlobException(BlobInfo *blob_info)
615 if ((blob_info->status == 0) && (errno != 0))
616 blob_info->error_number=errno;
617 blob_info->status=(-1);
620MagickExport MagickBooleanType CloseBlob(Image *image)
623 *magick_restrict blob_info;
631 assert(image != (Image *) NULL);
632 assert(image->signature == MagickCoreSignature);
633 if (IsEventLogging() != MagickFalse)
634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
635 blob_info=image->blob;
636 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
638 status=SyncBlob(image);
641 ((blob_info->mode == WriteBlobMode) ||
642 (blob_info->mode == WriteBinaryBlobMode) ||
643 (blob_info->mode == AppendBlobMode) ||
644 (blob_info->mode == AppendBinaryBlobMode)))
645 ThrowBlobException(blob_info);
646 status=blob_info->status;
647 switch (blob_info->type)
649 case UndefinedStream:
655 if (blob_info->synchronize != MagickFalse)
657 status=fflush(blob_info->file_info.file);
659 ThrowBlobException(blob_info);
660 status=fsync(fileno(blob_info->file_info.file));
662 ThrowBlobException(blob_info);
664 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
665 ThrowBlobException(blob_info);
670#if defined(MAGICKCORE_ZLIB_DELEGATE)
672 (void) gzerror(blob_info->file_info.gzfile,&status);
674 ThrowBlobException(blob_info);
680#if defined(MAGICKCORE_BZLIB_DELEGATE)
682 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
684 ThrowBlobException(blob_info);
692 if (blob_info->file_info.file != (FILE *) NULL)
694 if (blob_info->synchronize != MagickFalse)
696 status=fflush(blob_info->file_info.file);
698 ThrowBlobException(blob_info);
699 status=fsync(fileno(blob_info->file_info.file));
701 ThrowBlobException(blob_info);
703 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
704 ThrowBlobException(blob_info);
711 blob_info->size=GetBlobSize(image);
712 image->extent=blob_info->size;
713 blob_info->eof=MagickFalse;
715 blob_info->mode=UndefinedBlobMode;
716 if (blob_info->exempt != MagickFalse)
718 blob_info->type=UndefinedStream;
719 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
721 switch (blob_info->type)
723 case UndefinedStream:
728 if (blob_info->file_info.file != (FILE *) NULL)
730 status=fclose(blob_info->file_info.file);
732 ThrowBlobException(blob_info);
738#if defined(MAGICKCORE_HAVE_PCLOSE)
739 status=pclose(blob_info->file_info.file);
741 ThrowBlobException(blob_info);
747#if defined(MAGICKCORE_ZLIB_DELEGATE)
748 status=gzclose(blob_info->file_info.gzfile);
750 ThrowBlobException(blob_info);
756#if defined(MAGICKCORE_BZLIB_DELEGATE)
757 BZ2_bzclose(blob_info->file_info.bzfile);
765 if (blob_info->file_info.file != (FILE *) NULL)
767 status=fclose(blob_info->file_info.file);
769 ThrowBlobException(blob_info);
776 (void) DetachBlob(blob_info);
777 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
806MagickExport Image *CustomStreamToImage(
const ImageInfo *image_info,
807 ExceptionInfo *exception)
818 assert(image_info != (ImageInfo *) NULL);
819 assert(image_info->signature == MagickCoreSignature);
820 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
821 assert(image_info->custom_stream->signature == MagickCoreSignature);
822 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
823 assert(exception != (ExceptionInfo *) NULL);
824 if (IsEventLogging() != MagickFalse)
825 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
826 image_info->filename);
827 blob_info=CloneImageInfo(image_info);
828 if (*blob_info->magick ==
'\0')
829 (void) SetImageInfo(blob_info,0,exception);
830 magick_info=GetMagickInfo(blob_info->magick,exception);
831 if (magick_info == (
const MagickInfo *) NULL)
833 (void) ThrowMagickException(exception,GetMagickModule(),
834 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
836 blob_info=DestroyImageInfo(blob_info);
837 return((Image *) NULL);
839 image=(Image *) NULL;
840 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
841 (*blob_info->filename !=
'\0'))
844 filename[MagickPathExtent];
850 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
851 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
852 blob_info->magick,filename);
853 image=ReadImage(blob_info,exception);
858 unique[MagickPathExtent];
872 blob_info->custom_stream=(CustomStreamInfo *) NULL;
873 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
875 if (blob == (
unsigned char *) NULL)
877 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
878 image_info->filename);
879 blob_info=DestroyImageInfo(blob_info);
880 return((Image *) NULL);
882 file=AcquireUniqueFileResource(unique);
885 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
886 image_info->filename);
887 blob=(
unsigned char *) RelinquishMagickMemory(blob);
888 blob_info=DestroyImageInfo(blob_info);
889 return((Image *) NULL);
891 clone_info=CloneImageInfo(blob_info);
892 blob_info->file=fdopen(file,
"wb+");
893 if (blob_info->file != (FILE *) NULL)
898 count=(ssize_t) MagickMaxBufferExtent;
899 while (count == (ssize_t) MagickMaxBufferExtent)
901 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
902 image_info->custom_stream->data);
903 count=(ssize_t) write(file,(
const char *) blob,(
size_t) count);
905 (void) fclose(blob_info->file);
906 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
907 "%s:%s",blob_info->magick,unique);
908 image=ReadImage(clone_info,exception);
909 if (image != (Image *) NULL)
917 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
919 (void) CopyMagickString(images->filename,image_info->filename,
921 (void) CopyMagickString(images->magick_filename,
922 image_info->filename,MagickPathExtent);
923 (void) CopyMagickString(images->magick,magick_info->name,
925 images=GetNextImageInList(images);
929 clone_info=DestroyImageInfo(clone_info);
930 blob=(
unsigned char *) RelinquishMagickMemory(blob);
931 (void) RelinquishUniqueFileResource(unique);
933 blob_info=DestroyImageInfo(blob_info);
934 if (image != (Image *) NULL)
935 if (CloseBlob(image) == MagickFalse)
936 image=DestroyImageList(image);
962MagickExport
void DestroyBlob(Image *image)
965 *magick_restrict blob_info;
970 assert(image != (Image *) NULL);
971 assert(image->signature == MagickCoreSignature);
972 assert(image->blob != (BlobInfo *) NULL);
973 assert(image->blob->signature == MagickCoreSignature);
974 if (IsEventLogging() != MagickFalse)
975 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
976 blob_info=image->blob;
978 LockSemaphoreInfo(blob_info->semaphore);
979 blob_info->reference_count--;
980 assert(blob_info->reference_count >= 0);
981 if (blob_info->reference_count == 0)
983 UnlockSemaphoreInfo(blob_info->semaphore);
984 if (destroy == MagickFalse)
986 image->blob=(BlobInfo *) NULL;
989 (void) CloseBlob(image);
990 if (blob_info->mapped != MagickFalse)
992 (void) UnmapBlob(blob_info->data,blob_info->length);
993 RelinquishMagickResource(MapResource,blob_info->length);
996 RelinquishSemaphoreInfo(&blob_info->semaphore);
997 blob_info->signature=(~MagickCoreSignature);
998 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
1024MagickExport CustomStreamInfo *DestroyCustomStreamInfo(
1025 CustomStreamInfo *custom_stream)
1027 assert(custom_stream != (CustomStreamInfo *) NULL);
1028 assert(custom_stream->signature == MagickCoreSignature);
1029 if (IsEventLogging() != MagickFalse)
1030 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1031 custom_stream->signature=(~MagickCoreSignature);
1032 custom_stream=(CustomStreamInfo *) RelinquishMagickMemory(custom_stream);
1033 return(custom_stream);
1058MagickExport
void *DetachBlob(BlobInfo *blob_info)
1063 assert(blob_info != (BlobInfo *) NULL);
1064 if (IsEventLogging() != MagickFalse)
1065 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1066 if (blob_info->mapped != MagickFalse)
1068 (void) UnmapBlob(blob_info->data,blob_info->length);
1069 blob_info->data=NULL;
1070 RelinquishMagickResource(MapResource,blob_info->length);
1072 blob_info->mapped=MagickFalse;
1073 blob_info->length=0;
1078 blob_info->offset=0;
1079 blob_info->mode=UndefinedBlobMode;
1080 blob_info->eof=MagickFalse;
1082 blob_info->exempt=MagickFalse;
1083 blob_info->type=UndefinedStream;
1084 blob_info->file_info.file=(FILE *) NULL;
1085 data=blob_info->data;
1086 blob_info->data=(
unsigned char *) NULL;
1087 blob_info->stream=(StreamHandler) NULL;
1088 blob_info->custom_stream=(CustomStreamInfo *) NULL;
1117static inline void ResetDisassociatedBlobStorage(BlobInfo *blob_info)
1119 assert(blob_info != (BlobInfo *) NULL);
1120 assert(blob_info->signature == MagickCoreSignature);
1121 if (blob_info->mapped != MagickFalse)
1123 RelinquishMagickResource(MapResource,blob_info->length);
1124 blob_info->mapped=MagickFalse;
1126 blob_info->data=(
unsigned char *) NULL;
1127 blob_info->length=0;
1128 blob_info->offset=0;
1129 blob_info->mode=UndefinedBlobMode;
1130 blob_info->type=UndefinedStream;
1133MagickExport
void DisassociateBlob(Image *image)
1136 *magick_restrict blob_info,
1142 assert(image != (Image *) NULL);
1143 assert(image->signature == MagickCoreSignature);
1144 assert(image->blob != (BlobInfo *) NULL);
1145 assert(image->blob->signature == MagickCoreSignature);
1146 if (IsEventLogging() != MagickFalse)
1147 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1148 blob_info=image->blob;
1150 LockSemaphoreInfo(blob_info->semaphore);
1151 assert(blob_info->reference_count >= 0);
1152 if (blob_info->reference_count > 1)
1154 UnlockSemaphoreInfo(blob_info->semaphore);
1155 if (clone == MagickFalse)
1157 clone_info=CloneBlobInfo(blob_info);
1158 ResetDisassociatedBlobStorage(clone_info);
1160 image->blob=clone_info;
1188MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1189 const MagickSizeType length)
1201 buffer[MagickMinBufferExtent >> 1];
1203 assert(image != (Image *) NULL);
1204 assert(image->signature == MagickCoreSignature);
1205 if (length != (MagickSizeType) ((MagickOffsetType) length))
1206 return(MagickFalse);
1208 for (i=0; i < length; i+=(MagickSizeType) count)
1210 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1211 (void) ReadBlobStream(image,quantum,buffer,&count);
1221 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1248MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
1250 assert(image != (Image *) NULL);
1251 assert(image->signature == MagickCoreSignature);
1252 assert(duplicate != (Image *) NULL);
1253 assert(duplicate->signature == MagickCoreSignature);
1254 if (IsEventLogging() != MagickFalse)
1255 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1257 image->blob=ReferenceBlob(duplicate->blob);
1283MagickExport
int EOFBlob(
const Image *image)
1286 *magick_restrict blob_info;
1288 assert(image != (Image *) NULL);
1289 assert(image->signature == MagickCoreSignature);
1290 assert(image->blob != (BlobInfo *) NULL);
1291 assert(image->blob->type != UndefinedStream);
1292 if (IsEventLogging() != MagickFalse)
1293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1294 blob_info=image->blob;
1295 switch (blob_info->type)
1297 case UndefinedStream:
1298 case StandardStream:
1303 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1309#if defined(MAGICKCORE_ZLIB_DELEGATE)
1310 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1317#if defined(MAGICKCORE_BZLIB_DELEGATE)
1322 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1323 if (status == BZ_UNEXPECTED_EOF)
1324 blob_info->eof=MagickTrue;
1330 blob_info->eof=MagickFalse;
1338 return((
int) blob_info->eof);
1364MagickExport
int ErrorBlob(
const Image *image)
1367 *magick_restrict blob_info;
1369 assert(image != (Image *) NULL);
1370 assert(image->signature == MagickCoreSignature);
1371 assert(image->blob != (BlobInfo *) NULL);
1372 assert(image->blob->type != UndefinedStream);
1373 if (IsEventLogging() != MagickFalse)
1374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1375 blob_info=image->blob;
1376 switch (blob_info->type)
1378 case UndefinedStream:
1379 case StandardStream:
1384 blob_info->error=ferror(blob_info->file_info.file);
1389#if defined(MAGICKCORE_ZLIB_DELEGATE)
1390 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1396#if defined(MAGICKCORE_BZLIB_DELEGATE)
1397 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1411 return(blob_info->error);
1449MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1450 size_t *length,ExceptionInfo *exception)
1476 assert(filename != (
const char *) NULL);
1477 assert(exception != (ExceptionInfo *) NULL);
1478 assert(exception->signature == MagickCoreSignature);
1479 if (IsEventLogging() != MagickFalse)
1480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1482 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1483 ThrowPolicyException(filename,NULL);
1485 if (LocaleCompare(filename,
"-") != 0)
1488 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1490 status=GetPathAttributes(filename,&attributes);
1491 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1493 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1496 file=open_utf8(filename,flags,0);
1500 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1503 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1505 file=close_utf8(file)-1;
1506 ThrowPolicyException(filename,NULL);
1508 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1510 if ((file == fileno(stdin)) || (offset < 0) ||
1511 (offset != (MagickOffsetType) ((ssize_t) offset)))
1522 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1523 quantum=(size_t) MagickMaxBufferExtent;
1524 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1525 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1526 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1527 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1529 count=MagickRead(file,blob+i,quantum);
1532 if (~i < ((
size_t) count+quantum+1))
1534 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1537 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1538 quantum+1,
sizeof(*blob));
1539 if ((i+(
size_t) count) >= extent)
1542 if (LocaleCompare(filename,
"-") != 0)
1543 file=close_utf8(file);
1544 if (blob == (
unsigned char *) NULL)
1546 (void) ThrowMagickException(exception,GetMagickModule(),
1547 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1550 if ((count < 0) || (file == -1))
1552 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1553 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1556 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1560 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1561 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1562 blob=(
unsigned char *) NULL;
1563 if (~(*length) >= (MagickPathExtent-1))
1564 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1566 if (blob == (
unsigned char *) NULL)
1568 file=close_utf8(file);
1569 (void) ThrowMagickException(exception,GetMagickModule(),
1570 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1573 map=MapBlob(file,ReadMode,0,*length);
1574 if (map != (
unsigned char *) NULL)
1576 (void) memcpy(blob,map,*length);
1577 (void) UnmapBlob(map,*length);
1581 (void) lseek(file,0,SEEK_SET);
1582 for (i=0; i < *length; i+=(size_t) count)
1584 count=MagickRead(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1585 MagickMaxBufferExtent));
1591 file=close_utf8(file)-1;
1592 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1593 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1598 if (LocaleCompare(filename,
"-") != 0)
1599 file=close_utf8(file);
1602 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1603 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 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1656 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1657 if (extent >= blob_info->extent)
1659 extent+=blob_info->quantum+length;
1660 blob_info->quantum<<=1;
1661 if (SetBlobExtent(image,extent) == MagickFalse)
1664 q=blob_info->data+blob_info->offset;
1665 (void) memcpy(q,data,length);
1666 blob_info->offset+=(MagickOffsetType) length;
1667 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1668 blob_info->length=(size_t) blob_info->offset;
1669 return((ssize_t) length);
1672MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1673 ExceptionInfo *exception)
1691 assert(image != (
const Image *) NULL);
1692 assert(image->signature == MagickCoreSignature);
1693 assert(filename != (
const char *) NULL);
1694 if (IsEventLogging() != MagickFalse)
1695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1696 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1697 ThrowPolicyException(filename,MagickFalse);
1699 if (LocaleCompare(filename,
"-") != 0)
1702 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1704 file=open_utf8(filename,flags,0);
1708 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1709 return(MagickFalse);
1711 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1712 ThrowPolicyException(filename,MagickFalse);
1713 quantum=(size_t) MagickMaxBufferExtent;
1714 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1715 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1716 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1717 if (blob == (
unsigned char *) NULL)
1719 file=close_utf8(file);
1720 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1722 return(MagickFalse);
1726 count=MagickRead(file,blob,quantum);
1729 length=(size_t) count;
1730 count=WriteBlobStream(image,length,blob);
1731 if (count != (ssize_t) length)
1733 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1737 file=close_utf8(file);
1739 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1740 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1767MagickExport MagickBooleanType GetBlobError(
const Image *image)
1769 assert(image != (
const Image *) NULL);
1770 assert(image->signature == MagickCoreSignature);
1771 if (IsEventLogging() != MagickFalse)
1772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1773 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1774 errno=image->blob->error_number;
1775 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1801MagickExport FILE *GetBlobFileHandle(
const Image *image)
1803 assert(image != (
const Image *) NULL);
1804 assert(image->signature == MagickCoreSignature);
1805 assert(image->blob != (BlobInfo *) NULL);
1806 assert(image->blob->signature == MagickCoreSignature);
1807 switch (image->blob->type)
1809 case StandardStream:
1812 return(image->blob->file_info.file);
1813 case UndefinedStream:
1821 return((FILE *) NULL);
1846MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1848 assert(blob_info != (BlobInfo *) NULL);
1849 (void) memset(blob_info,0,
sizeof(*blob_info));
1850 blob_info->type=UndefinedStream;
1851 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1852 blob_info->properties.st_mtime=GetMagickTime();
1853 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1854 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1855 blob_info->reference_count=1;
1856 blob_info->semaphore=AcquireSemaphoreInfo();
1857 blob_info->signature=MagickCoreSignature;
1882MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1884 assert(image != (Image *) NULL);
1885 assert(image->signature == MagickCoreSignature);
1886 if (IsEventLogging() != MagickFalse)
1887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1888 return(&image->blob->properties);
1914MagickExport MagickSizeType GetBlobSize(
const Image *image)
1917 *magick_restrict blob_info;
1922 assert(image != (Image *) NULL);
1923 assert(image->signature == MagickCoreSignature);
1924 assert(image->blob != (BlobInfo *) NULL);
1925 if (IsEventLogging() != MagickFalse)
1926 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1927 blob_info=image->blob;
1929 switch (blob_info->type)
1931 case UndefinedStream:
1932 case StandardStream:
1934 extent=blob_info->size;
1942 extent=(MagickSizeType) blob_info->properties.st_size;
1944 extent=blob_info->size;
1945 file_descriptor=fileno(blob_info->file_info.file);
1946 if (file_descriptor == -1)
1948 if (fstat(file_descriptor,&blob_info->properties) == 0)
1949 extent=(MagickSizeType) blob_info->properties.st_size;
1954 extent=blob_info->size;
1963 status=GetPathAttributes(image->filename,&blob_info->properties);
1964 if (status != MagickFalse)
1965 extent=(MagickSizeType) blob_info->properties.st_size;
1972 extent=(MagickSizeType) blob_info->length;
1977 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1978 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1983 offset=blob_info->custom_stream->teller(
1984 blob_info->custom_stream->data);
1985 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1986 blob_info->custom_stream->data);
1987 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1988 blob_info->custom_stream->data);
2018MagickExport
void *GetBlobStreamData(
const Image *image)
2020 assert(image != (
const Image *) NULL);
2021 assert(image->signature == MagickCoreSignature);
2022 return(image->blob->data);
2047MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2049 assert(image != (
const Image *) NULL);
2050 assert(image->signature == MagickCoreSignature);
2051 if (IsEventLogging() != MagickFalse)
2052 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2053 return(image->blob->stream);
2089MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2090 Image *image,
size_t *length,ExceptionInfo *exception)
2104 assert(image_info != (
const ImageInfo *) NULL);
2105 assert(image_info->signature == MagickCoreSignature);
2106 assert(image != (Image *) NULL);
2107 assert(image->signature == MagickCoreSignature);
2108 assert(exception != (ExceptionInfo *) NULL);
2109 assert(exception->signature == MagickCoreSignature);
2110 if (IsEventLogging() != MagickFalse)
2111 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2112 image_info->filename);
2114 blob=(
unsigned char *) NULL;
2115 blob_info=CloneImageInfo(image_info);
2116 blob_info->adjoin=MagickFalse;
2117 (void) SetImageInfo(blob_info,1,exception);
2118 if (*blob_info->magick !=
'\0')
2119 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2120 magick_info=GetMagickInfo(image->magick,exception);
2121 if (magick_info == (
const MagickInfo *) NULL)
2123 (void) ThrowMagickException(exception,GetMagickModule(),
2124 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2126 blob_info=DestroyImageInfo(blob_info);
2129 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2130 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2135 blob_info->length=0;
2136 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2137 sizeof(
unsigned char));
2138 if (blob_info->blob == NULL)
2139 (void) ThrowMagickException(exception,GetMagickModule(),
2140 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2143 (void) CloseBlob(image);
2144 image->blob->exempt=MagickTrue;
2145 image->blob->extent=0;
2146 *image->filename=
'\0';
2147 status=WriteImage(blob_info,image,exception);
2148 SyncBlobStream(blob_info,image,image);
2149 *length=image->blob->length;
2150 blob=DetachBlob(image->blob);
2151 if (blob != (
void *) NULL)
2153 if (status == MagickFalse)
2154 blob=RelinquishMagickMemory(blob);
2156 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2159 if ((status == MagickFalse) && (image->blob->extent == 0))
2160 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2166 unique[MagickPathExtent];
2174 file=AcquireUniqueFileResource(unique);
2177 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2178 image_info->filename);
2182 blob_info->file=fdopen(file,
"wb");
2183 if (blob_info->file != (FILE *) NULL)
2185 (void) FormatLocaleString(image->filename,MagickPathExtent,
2186 "%s:%s",image->magick,unique);
2187 status=WriteImage(blob_info,image,exception);
2188 (void) fclose(blob_info->file);
2189 if (status != MagickFalse)
2190 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2192 (void) RelinquishUniqueFileResource(unique);
2195 blob_info=DestroyImageInfo(blob_info);
2227MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2228 ExceptionInfo *exception)
2240 assert(image_info != (
const ImageInfo *) NULL);
2241 assert(image_info->signature == MagickCoreSignature);
2242 assert(image != (Image *) NULL);
2243 assert(image->signature == MagickCoreSignature);
2244 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2245 assert(image_info->custom_stream->signature == MagickCoreSignature);
2246 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2247 assert(exception != (ExceptionInfo *) NULL);
2248 if (IsEventLogging() != MagickFalse)
2249 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2250 image_info->filename);
2251 clone_info=CloneImageInfo(image_info);
2252 clone_info->adjoin=MagickFalse;
2253 (void) SetImageInfo(clone_info,1,exception);
2254 if (*clone_info->magick !=
'\0')
2255 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2256 magick_info=GetMagickInfo(image->magick,exception);
2257 if (magick_info == (
const MagickInfo *) NULL)
2259 (void) ThrowMagickException(exception,GetMagickModule(),
2260 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2262 clone_info=DestroyImageInfo(clone_info);
2265 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2266 blob_support=GetMagickBlobSupport(magick_info);
2267 if ((blob_support != MagickFalse) &&
2268 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2270 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2271 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2272 blob_support=MagickFalse;
2274 if (blob_support != MagickFalse)
2279 (void) CloseBlob(image);
2280 *image->filename=
'\0';
2281 (void) WriteImage(clone_info,image,exception);
2286 unique[MagickPathExtent];
2297 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2298 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2300 if (blob == (
unsigned char *) NULL)
2302 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2303 image_info->filename);
2304 clone_info=DestroyImageInfo(clone_info);
2307 file=AcquireUniqueFileResource(unique);
2310 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2311 image_info->filename);
2312 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2313 clone_info=DestroyImageInfo(clone_info);
2316 clone_info->file=fdopen(file,
"wb+");
2317 if (clone_info->file != (FILE *) NULL)
2322 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2323 image->magick,unique);
2324 status=WriteImage(clone_info,image,exception);
2325 if (status != MagickFalse)
2327 (void) fseek(clone_info->file,0,SEEK_SET);
2328 count=(ssize_t) MagickMaxBufferExtent;
2329 while (count == (ssize_t) MagickMaxBufferExtent)
2331 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2333 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2334 image_info->custom_stream->data);
2337 (void) fclose(clone_info->file);
2339 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2340 (void) RelinquishUniqueFileResource(unique);
2342 clone_info=DestroyImageInfo(clone_info);
2373MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2374 ExceptionInfo *exception)
2398 assert(image != (Image *) NULL);
2399 assert(image->signature == MagickCoreSignature);
2400 assert(image->blob != (BlobInfo *) NULL);
2401 assert(image->blob->type != UndefinedStream);
2402 assert(filename != (
const char *) NULL);
2403 if (IsEventLogging() != MagickFalse)
2404 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2405 if (*filename ==
'\0')
2406 file=AcquireUniqueFileResource(filename);
2408 if (LocaleCompare(filename,
"-") == 0)
2409 file=fileno(stdout);
2411 file=open_utf8(filename,O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2414 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2415 return(MagickFalse);
2417 quantum=(size_t) MagickMaxBufferExtent;
2418 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2419 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2420 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2421 if (buffer == (
unsigned char *) NULL)
2423 file=close_utf8(file)-1;
2424 (void) ThrowMagickException(exception,GetMagickModule(),
2425 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2426 return(MagickFalse);
2429 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2430 for (i=0; count > 0; )
2432 length=(size_t) count;
2433 for (i=0; i < length; i+=(size_t) count)
2435 count=MagickWrite(file,p+i,(
size_t) (length-i));
2441 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2443 if (LocaleCompare(filename,
"-") != 0)
2444 file=close_utf8(file);
2445 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2446 if ((file == -1) || (i < length))
2449 file=close_utf8(file);
2450 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2451 return(MagickFalse);
2491MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2492 size_t *length,ExceptionInfo *exception)
2506 assert(image_info != (
const ImageInfo *) NULL);
2507 assert(image_info->signature == MagickCoreSignature);
2508 assert(images != (Image *) NULL);
2509 assert(images->signature == MagickCoreSignature);
2510 assert(exception != (ExceptionInfo *) NULL);
2511 if (IsEventLogging() != MagickFalse)
2512 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2513 image_info->filename);
2515 blob=(
unsigned char *) NULL;
2516 blob_info=CloneImageInfo(image_info);
2517 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2519 if (*blob_info->magick !=
'\0')
2520 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2521 magick_info=GetMagickInfo(images->magick,exception);
2522 if (magick_info == (
const MagickInfo *) NULL)
2524 (void) ThrowMagickException(exception,GetMagickModule(),
2525 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2527 blob_info=DestroyImageInfo(blob_info);
2530 if (GetMagickAdjoin(magick_info) == MagickFalse)
2532 blob_info=DestroyImageInfo(blob_info);
2533 return(ImageToBlob(image_info,images,length,exception));
2535 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2536 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2541 blob_info->length=0;
2542 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2543 sizeof(
unsigned char));
2544 if (blob_info->blob == (
void *) NULL)
2545 (void) ThrowMagickException(exception,GetMagickModule(),
2546 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2549 (void) CloseBlob(images);
2550 images->blob->exempt=MagickTrue;
2551 images->blob->extent=0;
2552 *images->filename=
'\0';
2553 status=WriteImages(blob_info,images,images->filename,exception);
2554 SyncBlobStream(blob_info,images,images);
2555 *length=images->blob->length;
2556 blob=DetachBlob(images->blob);
2557 if (blob != (
void *) NULL)
2559 if (status == MagickFalse)
2560 blob=RelinquishMagickMemory(blob);
2562 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2564 else if ((status == MagickFalse) && (images->blob->extent == 0))
2565 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2571 filename[MagickPathExtent],
2572 unique[MagickPathExtent];
2580 file=AcquireUniqueFileResource(unique);
2583 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2584 image_info->filename);
2588 blob_info->file=fdopen(file,
"wb");
2589 if (blob_info->file != (FILE *) NULL)
2591 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2592 images->magick,unique);
2593 status=WriteImages(blob_info,images,filename,exception);
2594 (void) fclose(blob_info->file);
2595 if (status != MagickFalse)
2596 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2598 (void) RelinquishUniqueFileResource(unique);
2601 blob_info=DestroyImageInfo(blob_info);
2633MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2634 Image *images,ExceptionInfo *exception)
2646 assert(image_info != (
const ImageInfo *) NULL);
2647 assert(image_info->signature == MagickCoreSignature);
2648 assert(images != (Image *) NULL);
2649 assert(images->signature == MagickCoreSignature);
2650 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2651 assert(image_info->custom_stream->signature == MagickCoreSignature);
2652 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2653 assert(exception != (ExceptionInfo *) NULL);
2654 if (IsEventLogging() != MagickFalse)
2655 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2656 image_info->filename);
2657 clone_info=CloneImageInfo(image_info);
2658 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2660 if (*clone_info->magick !=
'\0')
2661 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2662 magick_info=GetMagickInfo(images->magick,exception);
2663 if (magick_info == (
const MagickInfo *) NULL)
2665 (void) ThrowMagickException(exception,GetMagickModule(),
2666 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2668 clone_info=DestroyImageInfo(clone_info);
2671 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2672 blob_support=GetMagickBlobSupport(magick_info);
2673 if ((blob_support != MagickFalse) &&
2674 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2676 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2677 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2678 blob_support=MagickFalse;
2680 if (blob_support != MagickFalse)
2685 (void) CloseBlob(images);
2686 *images->filename=
'\0';
2687 (void) WriteImages(clone_info,images,images->filename,exception);
2692 filename[MagickPathExtent],
2693 unique[MagickPathExtent];
2704 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2705 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2707 if (blob == (
unsigned char *) NULL)
2709 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2710 image_info->filename);
2711 clone_info=DestroyImageInfo(clone_info);
2714 file=AcquireUniqueFileResource(unique);
2717 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2718 image_info->filename);
2719 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2720 clone_info=DestroyImageInfo(clone_info);
2723 clone_info->file=fdopen(file,
"wb+");
2724 if (clone_info->file != (FILE *) NULL)
2729 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2730 images->magick,unique);
2731 status=WriteImages(clone_info,images,filename,exception);
2732 if (status != MagickFalse)
2734 (void) fseek(clone_info->file,0,SEEK_SET);
2735 count=(ssize_t) MagickMaxBufferExtent;
2736 while (count == (ssize_t) MagickMaxBufferExtent)
2738 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2740 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2741 image_info->custom_stream->data);
2744 (void) fclose(clone_info->file);
2746 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2747 (void) RelinquishUniqueFileResource(unique);
2749 clone_info=DestroyImageInfo(clone_info);
2785MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2786 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2789 filename[MagickPathExtent];
2818 assert(image_info != (ImageInfo *) NULL);
2819 assert(image_info->signature == MagickCoreSignature);
2820 assert(image != (Image *) NULL);
2821 assert(image->signature == MagickCoreSignature);
2822 assert(inject_image != (Image *) NULL);
2823 assert(inject_image->signature == MagickCoreSignature);
2824 assert(exception != (ExceptionInfo *) NULL);
2825 if (IsEventLogging() != MagickFalse)
2826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2827 unique_file=(FILE *) NULL;
2828 file=AcquireUniqueFileResource(filename);
2830 unique_file=fdopen(file,
"wb");
2831 if ((file == -1) || (unique_file == (FILE *) NULL))
2833 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2834 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2836 return(MagickFalse);
2838 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2839 if (byte_image == (Image *) NULL)
2841 (void) fclose(unique_file);
2842 (void) RelinquishUniqueFileResource(filename);
2843 return(MagickFalse);
2845 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2847 DestroyBlob(byte_image);
2848 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2849 write_info=CloneImageInfo(image_info);
2850 SetImageInfoFile(write_info,unique_file);
2851 status=WriteImage(write_info,byte_image,exception);
2852 write_info=DestroyImageInfo(write_info);
2853 byte_image=DestroyImage(byte_image);
2854 (void) fclose(unique_file);
2855 if (status == MagickFalse)
2857 (void) RelinquishUniqueFileResource(filename);
2858 return(MagickFalse);
2863 file=open_utf8(filename,O_RDONLY | O_CLOEXEC | O_BINARY,0);
2866 (void) RelinquishUniqueFileResource(filename);
2867 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2868 image_info->filename);
2869 return(MagickFalse);
2871 quantum=(size_t) MagickMaxBufferExtent;
2872 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2873 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2874 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2875 if (buffer == (
unsigned char *) NULL)
2877 (void) RelinquishUniqueFileResource(filename);
2878 file=close_utf8(file);
2879 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2884 ssize_t count = read(file,buffer,quantum);
2891 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2894 file=close_utf8(file);
2896 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2897 (void) RelinquishUniqueFileResource(filename);
2898 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2924MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2926 assert(image != (
const Image *) NULL);
2927 assert(image->signature == MagickCoreSignature);
2928 if (IsEventLogging() != MagickFalse)
2929 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2930 return(image->blob->exempt);
2955MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2958 *magick_restrict blob_info;
2960 assert(image != (
const Image *) NULL);
2961 assert(image->signature == MagickCoreSignature);
2962 if (IsEventLogging() != MagickFalse)
2963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2964 blob_info=image->blob;
2965 switch (blob_info->type)
2974 if (blob_info->file_info.file == (FILE *) NULL)
2975 return(MagickFalse);
2976 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2977 return(status == -1 ? MagickFalse : MagickTrue);
2981#if defined(MAGICKCORE_ZLIB_DELEGATE)
2985 if (blob_info->file_info.gzfile == (gzFile) NULL)
2986 return(MagickFalse);
2987 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2988 return(offset < 0 ? MagickFalse : MagickTrue);
2993 case UndefinedStream:
2997 case StandardStream:
3001 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3002 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3009 return(MagickFalse);
3034MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3036 assert(image != (
const Image *) NULL);
3037 assert(image->signature == MagickCoreSignature);
3038 if (IsEventLogging() != MagickFalse)
3039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3040 return(image->blob->temporary);
3072MagickExport
void *MapBlob(
int file,
const MapMode mode,
3073 const MagickOffsetType offset,
const size_t length)
3075#if defined(MAGICKCORE_HAVE_MMAP)
3088#if defined(MAP_ANONYMOUS)
3089 flags|=MAP_ANONYMOUS;
3098 protection=PROT_READ;
3104 protection=PROT_WRITE;
3110 protection=PROT_READ | PROT_WRITE;
3115#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3116 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3118 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3119 if (map == MAP_FAILED)
3120 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3122 if (map == MAP_FAILED)
3159MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3168 assert(buffer != (
unsigned char *) NULL);
3175 *buffer++=(
unsigned char) c;
3179 *buffer++=(
unsigned char) c;
3209MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3217 assert(p != (
unsigned char *) NULL);
3224 *p++=(
unsigned char) c;
3260static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3261 const BlobInfo *blob_info)
3272 size=MagickMinBufferExtent;
3273 option=GetImageOption(image_info,
"stream:buffer-size");
3274 if (option != (
const char *) NULL)
3275 size=StringToUnsignedLong(option);
3276 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3277 _IONBF : _IOFBF,size);
3278 return(status == 0 ? MagickTrue : MagickFalse);
3281#if defined(MAGICKCORE_ZLIB_DELEGATE)
3282static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3284#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3285 return(gzopen(path,mode));
3293 path_wide=NTCreateWidePath(path);
3294 if (path_wide == (
wchar_t *) NULL)
3295 return((gzFile) NULL);
3296 file=gzopen_w(path_wide,mode);
3297 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3303MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3304 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3307 *magick_restrict blob_info;
3310 extension[MagickPathExtent],
3311 filename[MagickPathExtent];
3317 flags = O_RDONLY | O_CLOEXEC;
3325 assert(image_info != (ImageInfo *) NULL);
3326 assert(image_info->signature == MagickCoreSignature);
3327 assert(image != (Image *) NULL);
3328 assert(image->signature == MagickCoreSignature);
3329 if (IsEventLogging() != MagickFalse)
3330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3331 image_info->filename);
3332 blob_info=image->blob;
3333 if (image_info->blob != (
void *) NULL)
3335 if (image_info->stream != (StreamHandler) NULL)
3336 blob_info->stream=(StreamHandler) image_info->stream;
3337 AttachBlob(blob_info,image_info->blob,image_info->length);
3340 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3341 (*image->filename ==
'\0'))
3343 blob_info->type=CustomStream;
3344 blob_info->custom_stream=image_info->custom_stream;
3345 if (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL)
3346 blob_info->custom_stream->seeker(0,SEEK_SET,
3347 blob_info->custom_stream->data);
3350 (void) DetachBlob(blob_info);
3351 blob_info->mode=mode;
3356 flags=O_RDONLY | O_CLOEXEC;
3360 case ReadBinaryBlobMode:
3362 flags=O_RDONLY | O_CLOEXEC | O_BINARY;
3368 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC;
3372 case WriteBinaryBlobMode:
3374 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_TRUNC | O_BINARY;
3378 case AppendBlobMode:
3380 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_APPEND;
3384 case AppendBinaryBlobMode:
3386 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_APPEND | O_BINARY;
3392 flags=O_RDONLY | O_CLOEXEC;
3398 blob_info->synchronize=image_info->synchronize;
3399 if (image_info->stream != (StreamHandler) NULL)
3401 blob_info->stream=image_info->stream;
3404 blob_info->type=FifoStream;
3412 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3413 rights=ReadPolicyRights;
3415 rights=WritePolicyRights;
3416 if (IsPathAuthorized(rights,filename) == MagickFalse)
3417 ThrowPolicyException(filename,MagickFalse);
3418 if ((LocaleCompare(filename,
"-") == 0) ||
3419 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3421 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3422#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3423 if (strchr(type,
'b') != (
char *) NULL)
3424 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3426 blob_info->type=StandardStream;
3427 blob_info->exempt=MagickTrue;
3428 return(SetStreamBuffering(image_info,blob_info));
3430 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3431 (IsGeometry(filename+3) != MagickFalse))
3438 blob_info->file_info.file=fdopen(StringToInteger(filename+3),fileMode);
3439 if (blob_info->file_info.file == (FILE *) NULL)
3441 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3442 return(MagickFalse);
3444#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3445 if (strchr(type,
'b') != (
char *) NULL)
3446 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3448 blob_info->type=FileStream;
3449 blob_info->exempt=MagickTrue;
3450 return(SetStreamBuffering(image_info,blob_info));
3452#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3453 if (*filename ==
'|')
3456 fileMode[MagickPathExtent],
3464 (void) signal(SIGPIPE,SIG_IGN);
3468 sanitize_command=SanitizeString(filename+1);
3469 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3470 sanitize_command=DestroyString(sanitize_command);
3471 if (blob_info->file_info.file == (FILE *) NULL)
3473 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3474 return(MagickFalse);
3476 blob_info->type=PipeStream;
3477 blob_info->exempt=MagickTrue;
3478 return(SetStreamBuffering(image_info,blob_info));
3481 status=GetPathAttributes(filename,&blob_info->properties);
3482#if defined(S_ISFIFO)
3483 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3485 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3486 if (blob_info->file_info.file == (FILE *) NULL)
3488 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3489 return(MagickFalse);
3491 blob_info->type=FileStream;
3492 blob_info->exempt=MagickTrue;
3493 return(SetStreamBuffering(image_info,blob_info));
3496 GetPathComponent(image->filename,ExtensionPath,extension);
3499 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3500 if ((image_info->adjoin == MagickFalse) ||
3501 (strchr(filename,
'%') != (
char *) NULL))
3506 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3507 image->scene,filename,exception);
3508 if ((LocaleCompare(filename,image->filename) == 0) &&
3509 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3510 (GetNextImageInList(image) != (Image *) NULL)))
3513 path[MagickPathExtent];
3515 GetPathComponent(image->filename,RootPath,path);
3516 if (*extension ==
'\0')
3517 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.17g",
3518 path,(
double) image->scene);
3520 (
void) FormatLocaleString(filename,MagickPathExtent,
3521 "%s-%.17g.%s",path,(
double) image->scene,extension);
3523 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3525 if (IsPathAuthorized(rights,filename) == MagickFalse)
3526 ThrowPolicyException(filename,MagickFalse);
3528 if (image_info->file != (FILE *) NULL)
3530 blob_info->file_info.file=image_info->file;
3531 blob_info->type=FileStream;
3532 blob_info->exempt=MagickTrue;
3540 blob_info->file_info.file=(FILE *) NULL;
3541 file=open_utf8(filename,flags,0);
3543 blob_info->file_info.file=fdopen(file,type);
3544 if (blob_info->file_info.file != (FILE *) NULL)
3552 blob_info->type=FileStream;
3553 (void) SetStreamBuffering(image_info,blob_info);
3554 (void) memset(magick,0,
sizeof(magick));
3555 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3556 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3557#if defined(MAGICKCORE_POSIX_SUPPORT)
3558 (void) fflush(blob_info->file_info.file);
3560 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3561 " read %.17g magic header bytes",(
double) count);
3562#if defined(MAGICKCORE_ZLIB_DELEGATE)
3563 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3564 ((
int) magick[2] == 0x08))
3567 gzfile = gzopen_utf8(filename,
"rb");
3569 if (gzfile != (gzFile) NULL)
3571 if (blob_info->file_info.file != (FILE *) NULL)
3572 (void) fclose(blob_info->file_info.file);
3573 blob_info->file_info.file=(FILE *) NULL;
3574 blob_info->file_info.gzfile=gzfile;
3575 blob_info->type=ZipStream;
3579#if defined(MAGICKCORE_BZLIB_DELEGATE)
3580 if (strncmp((
char *) magick,
"BZh",3) == 0)
3583 *bzfile = BZ2_bzopen(filename,
"r");
3585 if (bzfile != (BZFILE *) NULL)
3587 if (blob_info->file_info.file != (FILE *) NULL)
3588 (void) fclose(blob_info->file_info.file);
3589 blob_info->file_info.file=(FILE *) NULL;
3590 blob_info->file_info.bzfile=bzfile;
3591 blob_info->type=BZipStream;
3595 if (blob_info->type == FileStream)
3606 sans_exception=AcquireExceptionInfo();
3607 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3608 sans_exception=DestroyExceptionInfo(sans_exception);
3609 length=(size_t) blob_info->properties.st_size;
3610 if ((magick_info != (
const MagickInfo *) NULL) &&
3611 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3612 (AcquireMagickResource(MapResource,length) != MagickFalse))
3617 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3619 if (blob == (
void *) NULL)
3620 RelinquishMagickResource(MapResource,length);
3626 if (image_info->file != (FILE *) NULL)
3627 blob_info->exempt=MagickFalse;
3630 (void) fclose(blob_info->file_info.file);
3631 blob_info->file_info.file=(FILE *) NULL;
3633 AttachBlob(blob_info,blob,length);
3634 blob_info->mapped=MagickTrue;
3641#if defined(MAGICKCORE_ZLIB_DELEGATE)
3642 if ((LocaleCompare(extension,
"gz") == 0) ||
3643 (LocaleCompare(extension,
"wmz") == 0) ||
3644 (LocaleCompare(extension,
"svgz") == 0))
3646 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3647 if (blob_info->file_info.gzfile != (gzFile) NULL)
3648 blob_info->type=ZipStream;
3652#if defined(MAGICKCORE_BZLIB_DELEGATE)
3653 if (LocaleCompare(extension,
"bz2") == 0)
3655 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3656 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3657 blob_info->type=BZipStream;
3665 blob_info->file_info.file=(FILE *) NULL;
3666 file=open_utf8(filename,flags,P_MODE);
3668 blob_info->file_info.file=fdopen(file,type);
3669 if (blob_info->file_info.file != (FILE *) NULL)
3671 blob_info->type=FileStream;
3672 (void) SetStreamBuffering(image_info,blob_info);
3675 if (IsPathAuthorized(rights,filename) == MagickFalse)
3676 ThrowPolicyException(filename,MagickFalse);
3677 blob_info->status=0;
3678 blob_info->error_number=0;
3679 if (blob_info->type != UndefinedStream)
3680 blob_info->size=GetBlobSize(image);
3683 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3684 return(MagickFalse);
3723#if defined(__cplusplus) || defined(c_plusplus)
3727static size_t PingStream(
const Image *magick_unused(image),
3728 const void *magick_unused(pixels),
const size_t columns)
3730 magick_unreferenced(image);
3731 magick_unreferenced(pixels);
3735#if defined(__cplusplus) || defined(c_plusplus)
3739MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3740 const size_t length,ExceptionInfo *exception)
3755 assert(image_info != (ImageInfo *) NULL);
3756 assert(image_info->signature == MagickCoreSignature);
3757 assert(exception != (ExceptionInfo *) NULL);
3758 if (IsEventLogging() != MagickFalse)
3759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3760 image_info->filename);
3761 if ((blob == (
const void *) NULL) || (length == 0))
3763 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3764 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3765 return((Image *) NULL);
3767 ping_info=CloneImageInfo(image_info);
3768 ping_info->blob=(
void *) blob;
3769 ping_info->length=length;
3770 ping_info->ping=MagickTrue;
3771 if (*ping_info->magick ==
'\0')
3772 (void) SetImageInfo(ping_info,0,exception);
3773 magick_info=GetMagickInfo(ping_info->magick,exception);
3774 if (magick_info == (
const MagickInfo *) NULL)
3776 (void) ThrowMagickException(exception,GetMagickModule(),
3777 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3779 ping_info=DestroyImageInfo(ping_info);
3780 return((Image *) NULL);
3782 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3785 filename[MagickPathExtent];
3790 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3791 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3792 ping_info->magick,filename);
3793 image=ReadStream(ping_info,&PingStream,exception);
3794 if (image != (Image *) NULL)
3795 (void) DetachBlob(image->blob);
3796 ping_info=DestroyImageInfo(ping_info);
3802 ping_info->blob=(
void *) NULL;
3803 ping_info->length=0;
3804 *ping_info->filename=
'\0';
3805 status=BlobToFile(ping_info->filename,blob,length,exception);
3806 if (status == MagickFalse)
3808 (void) RelinquishUniqueFileResource(ping_info->filename);
3809 ping_info=DestroyImageInfo(ping_info);
3810 return((Image *) NULL);
3812 clone_info=CloneImageInfo(ping_info);
3813 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3814 ping_info->magick,ping_info->filename);
3815 image=ReadStream(clone_info,&PingStream,exception);
3816 if (image != (Image *) NULL)
3824 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3826 (void) CopyMagickString(images->filename,image_info->filename,
3828 (void) CopyMagickString(images->magick_filename,image_info->filename,
3830 (void) CopyMagickString(images->magick,magick_info->name,
3832 images=GetNextImageInList(images);
3835 clone_info=DestroyImageInfo(clone_info);
3836 (void) RelinquishUniqueFileResource(ping_info->filename);
3837 ping_info=DestroyImageInfo(ping_info);
3872MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3875 *magick_restrict blob_info;
3886 assert(image != (Image *) NULL);
3887 assert(image->signature == MagickCoreSignature);
3888 assert(image->blob != (BlobInfo *) NULL);
3889 assert(image->blob->type != UndefinedStream);
3892 assert(data != (
void *) NULL);
3893 blob_info=image->blob;
3895 q=(
unsigned char *) data;
3896 switch (blob_info->type)
3898 case UndefinedStream:
3900 case StandardStream:
3908 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3913 c=getc(blob_info->file_info.file);
3916 *q++=(
unsigned char) c;
3922 c=getc(blob_info->file_info.file);
3925 *q++=(
unsigned char) c;
3931 c=getc(blob_info->file_info.file);
3934 *q++=(
unsigned char) c;
3940 c=getc(blob_info->file_info.file);
3943 *q++=(
unsigned char) c;
3950 if ((count != (ssize_t) length) &&
3951 (ferror(blob_info->file_info.file) != 0))
3952 ThrowBlobException(blob_info);
3957#if defined(MAGICKCORE_ZLIB_DELEGATE)
3968 for (i=0; i < length; i+=(size_t) count)
3970 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3971 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3984 c=gzgetc(blob_info->file_info.gzfile);
3987 *q++=(
unsigned char) c;
3993 c=gzgetc(blob_info->file_info.gzfile);
3996 *q++=(
unsigned char) c;
4002 c=gzgetc(blob_info->file_info.gzfile);
4005 *q++=(
unsigned char) c;
4011 c=gzgetc(blob_info->file_info.gzfile);
4014 *q++=(
unsigned char) c;
4021 (void) gzerror(blob_info->file_info.gzfile,&status);
4022 if ((count != (ssize_t) length) && (status != Z_OK))
4023 ThrowBlobException(blob_info);
4024 if (blob_info->eof == MagickFalse)
4025 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4032#if defined(MAGICKCORE_BZLIB_DELEGATE)
4039 for (i=0; i < length; i+=(size_t) count)
4041 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4042 MagickMin(length-i,MagickMaxBufferExtent));
4052 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4053 if ((count != (ssize_t) length) && (status != BZ_OK))
4054 ThrowBlobException(blob_info);
4055 if ((count != (ssize_t) length) && (status == BZ_OK))
4056 blob_info->eof=MagickTrue;
4067 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4069 blob_info->eof=MagickTrue;
4072 p=blob_info->data+blob_info->offset;
4073 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4074 blob_info->length-blob_info->offset);
4075 blob_info->offset+=count;
4076 if (count != (ssize_t) length)
4077 blob_info->eof=MagickTrue;
4078 (void) memcpy(q,p,(
size_t) count);
4083 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4085 count=blob_info->custom_stream->reader(q,length,
4086 blob_info->custom_stream->data);
4089 blob_info->eof=MagickTrue;
4121MagickExport
int ReadBlobByte(Image *image)
4124 *magick_restrict blob_info;
4129 assert(image != (Image *) NULL);
4130 assert(image->signature == MagickCoreSignature);
4131 assert(image->blob != (BlobInfo *) NULL);
4132 assert(image->blob->type != UndefinedStream);
4133 blob_info=image->blob;
4134 switch (blob_info->type)
4136 case StandardStream:
4140 c=getc(blob_info->file_info.file);
4143 if (ferror(blob_info->file_info.file) != 0)
4144 ThrowBlobException(blob_info);
4151 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4153 blob_info->eof=MagickTrue;
4156 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4157 blob_info->offset++;
4168 count=ReadBlob(image,1,buffer);
4201MagickExport
double ReadBlobDouble(Image *image)
4212 quantum.double_value=0.0;
4213 quantum.unsigned_value=ReadBlobLongLong(image);
4214 return(quantum.double_value);
4240MagickExport
float ReadBlobFloat(Image *image)
4251 quantum.float_value=0.0;
4252 quantum.unsigned_value=ReadBlobLong(image);
4253 return(quantum.float_value);
4279MagickExport
unsigned int ReadBlobLong(Image *image)
4293 assert(image != (Image *) NULL);
4294 assert(image->signature == MagickCoreSignature);
4296 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4299 if (image->endian == LSBEndian)
4301 value=(
unsigned int) (*p++);
4302 value|=(
unsigned int) (*p++) << 8;
4303 value|=(
unsigned int) (*p++) << 16;
4304 value|=(
unsigned int) (*p++) << 24;
4307 value=(
unsigned int) (*p++) << 24;
4308 value|=(
unsigned int) (*p++) << 16;
4309 value|=(
unsigned int) (*p++) << 8;
4310 value|=(
unsigned int) (*p++);
4337MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4351 assert(image != (Image *) NULL);
4352 assert(image->signature == MagickCoreSignature);
4354 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4356 return(MagickULLConstant(0));
4357 if (image->endian == LSBEndian)
4359 value=(MagickSizeType) (*p++);
4360 value|=(MagickSizeType) (*p++) << 8;
4361 value|=(MagickSizeType) (*p++) << 16;
4362 value|=(MagickSizeType) (*p++) << 24;
4363 value|=(MagickSizeType) (*p++) << 32;
4364 value|=(MagickSizeType) (*p++) << 40;
4365 value|=(MagickSizeType) (*p++) << 48;
4366 value|=(MagickSizeType) (*p++) << 56;
4369 value=(MagickSizeType) (*p++) << 56;
4370 value|=(MagickSizeType) (*p++) << 48;
4371 value|=(MagickSizeType) (*p++) << 40;
4372 value|=(MagickSizeType) (*p++) << 32;
4373 value|=(MagickSizeType) (*p++) << 24;
4374 value|=(MagickSizeType) (*p++) << 16;
4375 value|=(MagickSizeType) (*p++) << 8;
4376 value|=(MagickSizeType) (*p++);
4403MagickExport
unsigned short ReadBlobShort(Image *image)
4417 assert(image != (Image *) NULL);
4418 assert(image->signature == MagickCoreSignature);
4420 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4422 return((
unsigned short) 0U);
4423 if (image->endian == LSBEndian)
4425 value=(
unsigned short) (*p++);
4426 value|=(
unsigned short) (*p++) << 8;
4429 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4430 value|=(
unsigned short) (*p++);
4457MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4471 assert(image != (Image *) NULL);
4472 assert(image->signature == MagickCoreSignature);
4474 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4477 value=(
unsigned int) (*p++);
4478 value|=(
unsigned int) (*p++) << 8;
4479 value|=(
unsigned int) (*p++) << 16;
4480 value|=(
unsigned int) (*p++) << 24;
4507MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4518 quantum.unsigned_value=ReadBlobLSBLong(image);
4519 return(quantum.signed_value);
4545MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4559 assert(image != (Image *) NULL);
4560 assert(image->signature == MagickCoreSignature);
4562 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4564 return((
unsigned short) 0U);
4565 value=(
unsigned short) (*p++);
4566 value|=(
unsigned short) (*p++) << 8;
4593MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4604 quantum.unsigned_value=ReadBlobLSBShort(image);
4605 return(quantum.signed_value);
4631MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4645 assert(image != (Image *) NULL);
4646 assert(image->signature == MagickCoreSignature);
4648 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4651 value=(
unsigned int) (*p++) << 24;
4652 value|=(
unsigned int) (*p++) << 16;
4653 value|=(
unsigned int) (*p++) << 8;
4654 value|=(
unsigned int) (*p++);
4681MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4695 assert(image != (Image *) NULL);
4696 assert(image->signature == MagickCoreSignature);
4698 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4700 return(MagickULLConstant(0));
4701 value=(MagickSizeType) (*p++) << 56;
4702 value|=(MagickSizeType) (*p++) << 48;
4703 value|=(MagickSizeType) (*p++) << 40;
4704 value|=(MagickSizeType) (*p++) << 32;
4705 value|=(MagickSizeType) (*p++) << 24;
4706 value|=(MagickSizeType) (*p++) << 16;
4707 value|=(MagickSizeType) (*p++) << 8;
4708 value|=(MagickSizeType) (*p++);
4735MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4749 assert(image != (Image *) NULL);
4750 assert(image->signature == MagickCoreSignature);
4752 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4754 return((
unsigned short) 0U);
4755 value=(
unsigned short) ((*p++) << 8);
4756 value|=(
unsigned short) (*p++);
4757 return((
unsigned short) (value & 0xffff));
4783MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4794 quantum.unsigned_value=ReadBlobMSBLong(image);
4795 return(quantum.signed_value);
4821MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4832 quantum.unsigned_value=ReadBlobMSBShort(image);
4833 return(quantum.signed_value);
4859MagickExport
signed int ReadBlobSignedLong(Image *image)
4870 quantum.unsigned_value=ReadBlobLong(image);
4871 return(quantum.signed_value);
4897MagickExport
signed short ReadBlobSignedShort(Image *image)
4908 quantum.unsigned_value=ReadBlobShort(image);
4909 return(quantum.signed_value);
4947MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4948 const size_t length,
void *magick_restrict data,ssize_t *count)
4951 *magick_restrict blob_info;
4953 assert(image != (Image *) NULL);
4954 assert(image->signature == MagickCoreSignature);
4955 assert(image->blob != (BlobInfo *) NULL);
4956 assert(image->blob->type != UndefinedStream);
4957 assert(count != (ssize_t *) NULL);
4958 blob_info=image->blob;
4959 if (blob_info->type != BlobStream)
4961 assert(data != NULL);
4962 *count=ReadBlob(image,length,(
unsigned char *) data);
4965 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4968 blob_info->eof=MagickTrue;
4971 data=blob_info->data+blob_info->offset;
4972 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4973 blob_info->length-blob_info->offset);
4974 blob_info->offset+=(*count);
4975 if (*count != (ssize_t) length)
4976 blob_info->eof=MagickTrue;
5005MagickExport
char *ReadBlobString(Image *image,
char *
string)
5008 *magick_restrict blob_info;
5016 assert(image != (Image *) NULL);
5017 assert(image->signature == MagickCoreSignature);
5018 assert(image->blob != (BlobInfo *) NULL);
5019 assert(image->blob->type != UndefinedStream);
5020 if (IsEventLogging() != MagickFalse)
5021 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5023 blob_info=image->blob;
5024 switch (blob_info->type)
5026 case UndefinedStream:
5028 case StandardStream:
5031 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5032 if (p == (
char *) NULL)
5034 if (ferror(blob_info->file_info.file) != 0)
5035 ThrowBlobException(blob_info);
5036 return((
char *) NULL);
5043#if defined(MAGICKCORE_ZLIB_DELEGATE)
5044 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5045 if (p == (
char *) NULL)
5048 (void) gzerror(blob_info->file_info.gzfile,&status);
5050 ThrowBlobException(blob_info);
5051 return((
char *) NULL);
5061 c=ReadBlobByte(image);
5064 blob_info->eof=MagickTrue;
5067 string[i++]=(char) c;
5070 }
while (i < (MaxTextExtent-2));
5078 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5081 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5083 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5084 return((
char *) NULL);
5111MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5113 assert(blob != (BlobInfo *) NULL);
5114 assert(blob->signature == MagickCoreSignature);
5115 if (IsEventLogging() != MagickFalse)
5116 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5117 LockSemaphoreInfo(blob->semaphore);
5118 blob->reference_count++;
5119 UnlockSemaphoreInfo(blob->semaphore);
5156MagickExport MagickOffsetType SeekBlob(Image *image,
5157 const MagickOffsetType offset,
const int whence)
5160 *magick_restrict blob_info;
5162 assert(image != (Image *) NULL);
5163 assert(image->signature == MagickCoreSignature);
5164 assert(image->blob != (BlobInfo *) NULL);
5165 assert(image->blob->type != UndefinedStream);
5166 if (IsEventLogging() != MagickFalse)
5167 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5168 blob_info=image->blob;
5169 switch (blob_info->type)
5171 case UndefinedStream:
5173 case StandardStream:
5178 if ((offset < 0) && (whence == SEEK_SET))
5180 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5182 blob_info->offset=TellBlob(image);
5187#if defined(MAGICKCORE_ZLIB_DELEGATE)
5188 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5191 blob_info->offset=TellBlob(image);
5207 blob_info->offset=offset;
5212 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5213 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5218 if ((blob_info->offset+offset) < 0)
5220 blob_info->offset+=offset;
5225 if (((MagickOffsetType) blob_info->length+offset) < 0)
5227 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5231 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5233 blob_info->eof=MagickFalse;
5240 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5242 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5243 blob_info->custom_stream->data);
5247 return(blob_info->offset);
5275MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5277 assert(image != (
const Image *) NULL);
5278 assert(image->signature == MagickCoreSignature);
5279 if (IsEventLogging() != MagickFalse)
5280 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5281 image->blob->exempt=exempt;
5310MagickExport MagickBooleanType SetBlobExtent(Image *image,
5311 const MagickSizeType extent)
5314 *magick_restrict blob_info;
5316 assert(image != (Image *) NULL);
5317 assert(image->signature == MagickCoreSignature);
5318 assert(image->blob != (BlobInfo *) NULL);
5319 assert(image->blob->type != UndefinedStream);
5320 if (IsEventLogging() != MagickFalse)
5321 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5322 blob_info=image->blob;
5323 switch (blob_info->type)
5325 case UndefinedStream:
5327 case StandardStream:
5328 return(MagickFalse);
5337 if (extent != (MagickSizeType) ((off_t) extent))
5338 return(MagickFalse);
5339 offset=SeekBlob(image,0,SEEK_END);
5341 return(MagickFalse);
5342 if ((MagickSizeType) offset >= extent)
5344 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5347 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5348 blob_info->file_info.file);
5349#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5350 if (blob_info->synchronize != MagickFalse)
5355 file=fileno(blob_info->file_info.file);
5356 if ((file == -1) || (offset < 0))
5357 return(MagickFalse);
5358 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5361 offset=SeekBlob(image,offset,SEEK_SET);
5363 return(MagickFalse);
5368 return(MagickFalse);
5370 return(MagickFalse);
5372 return(MagickFalse);
5375 if (extent != (MagickSizeType) ((
size_t) extent))
5376 return(MagickFalse);
5377 if (blob_info->mapped != MagickFalse)
5385 (void) UnmapBlob(blob_info->data,blob_info->length);
5386 RelinquishMagickResource(MapResource,blob_info->length);
5387 if (extent != (MagickSizeType) ((off_t) extent))
5388 return(MagickFalse);
5389 offset=SeekBlob(image,0,SEEK_END);
5391 return(MagickFalse);
5392 if ((MagickSizeType) offset >= extent)
5394 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5395 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5396 blob_info->file_info.file);
5397#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5398 if (blob_info->synchronize != MagickFalse)
5403 file=fileno(blob_info->file_info.file);
5404 if ((file == -1) || (offset < 0))
5405 return(MagickFalse);
5406 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5410 offset=SeekBlob(image,offset,SEEK_SET);
5412 return(MagickFalse);
5413 (void) AcquireMagickResource(MapResource,extent);
5414 blob_info->data=(
unsigned char*) MapBlob(fileno(
5415 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5416 blob_info->extent=(size_t) extent;
5417 blob_info->length=(size_t) extent;
5418 (void) SyncBlob(image);
5421 blob_info->extent=(size_t) extent;
5422 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5423 blob_info->extent+1,
sizeof(*blob_info->data));
5424 (void) SyncBlob(image);
5425 if (blob_info->data == (
unsigned char *) NULL)
5427 (void) DetachBlob(blob_info);
5428 return(MagickFalse);
5462MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5465 assert(custom_stream != (CustomStreamInfo *) NULL);
5466 assert(custom_stream->signature == MagickCoreSignature);
5467 custom_stream->data=data;
5495MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5496 CustomStreamHandler reader)
5498 assert(custom_stream != (CustomStreamInfo *) NULL);
5499 assert(custom_stream->signature == MagickCoreSignature);
5500 custom_stream->reader=reader;
5528MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5529 CustomStreamSeeker seeker)
5531 assert(custom_stream != (CustomStreamInfo *) NULL);
5532 assert(custom_stream->signature == MagickCoreSignature);
5533 custom_stream->seeker=seeker;
5561MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5562 CustomStreamTeller teller)
5564 assert(custom_stream != (CustomStreamInfo *) NULL);
5565 assert(custom_stream->signature == MagickCoreSignature);
5566 custom_stream->teller=teller;
5594MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5595 CustomStreamHandler writer)
5597 assert(custom_stream != (CustomStreamInfo *) NULL);
5598 assert(custom_stream->signature == MagickCoreSignature);
5599 custom_stream->writer=writer;
5626static int SyncBlob(
const Image *image)
5629 *magick_restrict blob_info;
5634 assert(image != (Image *) NULL);
5635 assert(image->signature == MagickCoreSignature);
5636 assert(image->blob != (BlobInfo *) NULL);
5637 if (IsEventLogging() != MagickFalse)
5638 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5639 if (EOFBlob(image) != 0)
5641 blob_info=image->blob;
5643 switch (blob_info->type)
5645 case UndefinedStream:
5647 case StandardStream:
5649 if ((blob_info->mode == WriteBlobMode) ||
5650 (blob_info->mode == WriteBinaryBlobMode) ||
5651 (blob_info->mode == AppendBlobMode) ||
5652 (blob_info->mode == AppendBinaryBlobMode))
5653 status=fflush(blob_info->file_info.file);
5659 status=fflush(blob_info->file_info.file);
5664#if defined(MAGICKCORE_ZLIB_DELEGATE)
5665 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5671#if defined(MAGICKCORE_BZLIB_DELEGATE)
5672 status=BZ2_bzflush(blob_info->file_info.bzfile);
5717MagickPrivate
void SyncBlobStream(ImageInfo *image_info,Image *image,
5720 assert(owner != (Image *) NULL);
5721 assert(image != (Image *) NULL);
5722 assert(image->signature == MagickCoreSignature);
5723 if (image->blob == (BlobInfo *) NULL)
5725 if (image->blob->data == (
unsigned char *) NULL)
5727 if (image_info != (ImageInfo *) NULL)
5729 image_info->blob=(
void *) image->blob->data;
5730 image_info->length=image->blob->length;
5732 if ((image == owner) || (owner->blob == (BlobInfo *) NULL))
5734 if (owner->blob == (BlobInfo *) NULL)
5740 owner->blob->data=image->blob->data;
5741 owner->blob->extent=image->blob->extent;
5742 owner->blob->length=image->blob->length;
5743 owner->blob->offset=image->blob->offset;
5744 owner->blob->quantum=image->blob->quantum;
5745 owner->blob->type=BlobStream;
5746 owner->blob->exempt=MagickTrue;
5747 (void) DetachBlob(image->blob);
5772MagickExport MagickOffsetType TellBlob(
const Image *image)
5775 *magick_restrict blob_info;
5780 assert(image != (Image *) NULL);
5781 assert(image->signature == MagickCoreSignature);
5782 assert(image->blob != (BlobInfo *) NULL);
5783 assert(image->blob->type != UndefinedStream);
5784 if (IsEventLogging() != MagickFalse)
5785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5786 blob_info=image->blob;
5788 switch (blob_info->type)
5790 case UndefinedStream:
5791 case StandardStream:
5795 offset=ftell(blob_info->file_info.file);
5802#if defined(MAGICKCORE_ZLIB_DELEGATE)
5803 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5813 offset=blob_info->offset;
5818 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5819 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5851MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5853#if defined(MAGICKCORE_HAVE_MMAP)
5857 status=munmap(map,length);
5858 return(status == -1 ? MagickFalse : MagickTrue);
5862 return(MagickFalse);
5894MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5898 *magick_restrict blob_info;
5912 assert(image != (Image *) NULL);
5913 assert(image->signature == MagickCoreSignature);
5914 assert(image->blob != (BlobInfo *) NULL);
5915 assert(image->blob->type != UndefinedStream);
5918 assert(data != (
const void *) NULL);
5919 blob_info=image->blob;
5921 p=(
const unsigned char *) data;
5922 q=(
unsigned char *) data;
5923 switch (blob_info->type)
5925 case UndefinedStream:
5927 case StandardStream:
5935 count=(ssize_t) fwrite((
const char *) data,1,length,
5936 blob_info->file_info.file);
5941 c=putc((
int) *p++,blob_info->file_info.file);
5949 c=putc((
int) *p++,blob_info->file_info.file);
5957 c=putc((
int) *p++,blob_info->file_info.file);
5965 c=putc((
int) *p++,blob_info->file_info.file);
5974 if ((count != (ssize_t) length) &&
5975 (ferror(blob_info->file_info.file) != 0))
5976 ThrowBlobException(blob_info);
5981#if defined(MAGICKCORE_ZLIB_DELEGATE)
5992 for (i=0; i < length; i+=(size_t) count)
5994 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5995 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
6008 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6016 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6024 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6032 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6042 (void) gzerror(blob_info->file_info.gzfile,&status);
6043 if ((count != (ssize_t) length) && (status != Z_OK))
6044 ThrowBlobException(blob_info);
6050#if defined(MAGICKCORE_BZLIB_DELEGATE)
6057 for (i=0; i < length; i+=(size_t) count)
6059 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6060 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6070 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6071 if ((count != (ssize_t) length) && (status != BZ_OK))
6072 ThrowBlobException(blob_info);
6078 count=(ssize_t) blob_info->stream(image,data,length);
6086 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6091 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6092 if (extent >= blob_info->extent)
6094 extent+=blob_info->quantum+length;
6095 blob_info->quantum<<=1;
6096 if (SetBlobExtent(image,extent) == MagickFalse)
6099 q=blob_info->data+blob_info->offset;
6100 (void) memcpy(q,p,length);
6101 blob_info->offset+=(MagickOffsetType) length;
6102 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6103 blob_info->length=(size_t) blob_info->offset;
6104 count=(ssize_t) length;
6109 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6110 count=blob_info->custom_stream->writer((
unsigned char *) data,
6111 length,blob_info->custom_stream->data);
6143MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6146 *magick_restrict blob_info;
6151 assert(image != (Image *) NULL);
6152 assert(image->signature == MagickCoreSignature);
6153 assert(image->blob != (BlobInfo *) NULL);
6154 assert(image->blob->type != UndefinedStream);
6155 blob_info=image->blob;
6157 switch (blob_info->type)
6159 case StandardStream:
6166 c=putc((
int) value,blob_info->file_info.file);
6169 if (ferror(blob_info->file_info.file) != 0)
6170 ThrowBlobException(blob_info);
6178 count=WriteBlobStream(image,1,&value);
6210MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6221 quantum.unsigned_value=0U;
6222 quantum.float_value=value;
6223 return(WriteBlobLong(image,quantum.unsigned_value));
6251MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6256 assert(image != (Image *) NULL);
6257 assert(image->signature == MagickCoreSignature);
6258 if (image->endian == LSBEndian)
6260 buffer[0]=(
unsigned char) value;
6261 buffer[1]=(
unsigned char) (value >> 8);
6262 buffer[2]=(
unsigned char) (value >> 16);
6263 buffer[3]=(
unsigned char) (value >> 24);
6264 return(WriteBlobStream(image,4,buffer));
6266 buffer[0]=(
unsigned char) (value >> 24);
6267 buffer[1]=(
unsigned char) (value >> 16);
6268 buffer[2]=(
unsigned char) (value >> 8);
6269 buffer[3]=(
unsigned char) value;
6270 return(WriteBlobStream(image,4,buffer));
6298MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6303 assert(image != (Image *) NULL);
6304 assert(image->signature == MagickCoreSignature);
6305 if (image->endian == LSBEndian)
6307 buffer[0]=(
unsigned char) value;
6308 buffer[1]=(
unsigned char) (value >> 8);
6309 buffer[2]=(
unsigned char) (value >> 16);
6310 buffer[3]=(
unsigned char) (value >> 24);
6311 buffer[4]=(
unsigned char) (value >> 32);
6312 buffer[5]=(
unsigned char) (value >> 40);
6313 buffer[6]=(
unsigned char) (value >> 48);
6314 buffer[7]=(
unsigned char) (value >> 56);
6315 return(WriteBlobStream(image,8,buffer));
6317 buffer[0]=(
unsigned char) (value >> 56);
6318 buffer[1]=(
unsigned char) (value >> 48);
6319 buffer[2]=(
unsigned char) (value >> 40);
6320 buffer[3]=(
unsigned char) (value >> 32);
6321 buffer[4]=(
unsigned char) (value >> 24);
6322 buffer[5]=(
unsigned char) (value >> 16);
6323 buffer[6]=(
unsigned char) (value >> 8);
6324 buffer[7]=(
unsigned char) value;
6325 return(WriteBlobStream(image,8,buffer));
6353MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6358 assert(image != (Image *) NULL);
6359 assert(image->signature == MagickCoreSignature);
6360 if (image->endian == LSBEndian)
6362 buffer[0]=(
unsigned char) value;
6363 buffer[1]=(
unsigned char) (value >> 8);
6364 return(WriteBlobStream(image,2,buffer));
6366 buffer[0]=(
unsigned char) (value >> 8);
6367 buffer[1]=(
unsigned char) value;
6368 return(WriteBlobStream(image,2,buffer));
6396MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6410 assert(image != (Image *) NULL);
6411 assert(image->signature == MagickCoreSignature);
6412 quantum.signed_value=value;
6413 if (image->endian == LSBEndian)
6415 buffer[0]=(
unsigned char) quantum.unsigned_value;
6416 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6417 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6418 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6419 return(WriteBlobStream(image,4,buffer));
6421 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6422 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6423 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6424 buffer[3]=(
unsigned char) quantum.unsigned_value;
6425 return(WriteBlobStream(image,4,buffer));
6453MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6458 assert(image != (Image *) NULL);
6459 assert(image->signature == MagickCoreSignature);
6460 buffer[0]=(
unsigned char) value;
6461 buffer[1]=(
unsigned char) (value >> 8);
6462 buffer[2]=(
unsigned char) (value >> 16);
6463 buffer[3]=(
unsigned char) (value >> 24);
6464 return(WriteBlobStream(image,4,buffer));
6492MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6497 assert(image != (Image *) NULL);
6498 assert(image->signature == MagickCoreSignature);
6499 buffer[0]=(
unsigned char) value;
6500 buffer[1]=(
unsigned char) (value >> 8);
6501 return(WriteBlobStream(image,2,buffer));
6529MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6543 assert(image != (Image *) NULL);
6544 assert(image->signature == MagickCoreSignature);
6545 quantum.signed_value=value;
6546 buffer[0]=(
unsigned char) quantum.unsigned_value;
6547 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6548 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6549 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6550 return(WriteBlobStream(image,4,buffer));
6578MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6579 const signed short value)
6593 assert(image != (Image *) NULL);
6594 assert(image->signature == MagickCoreSignature);
6595 quantum.signed_value=value;
6596 buffer[0]=(
unsigned char) quantum.unsigned_value;
6597 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6598 return(WriteBlobStream(image,2,buffer));
6626MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6631 assert(image != (Image *) NULL);
6632 assert(image->signature == MagickCoreSignature);
6633 buffer[0]=(
unsigned char) (value >> 24);
6634 buffer[1]=(
unsigned char) (value >> 16);
6635 buffer[2]=(
unsigned char) (value >> 8);
6636 buffer[3]=(
unsigned char) value;
6637 return(WriteBlobStream(image,4,buffer));
6665MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6666 const signed short value)
6680 assert(image != (Image *) NULL);
6681 assert(image->signature == MagickCoreSignature);
6682 quantum.signed_value=value;
6683 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6684 buffer[1]=(
unsigned char) quantum.unsigned_value;
6685 return(WriteBlobStream(image,2,buffer));
6713MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6718 assert(image != (Image *) NULL);
6719 assert(image->signature == MagickCoreSignature);
6720 buffer[0]=(
unsigned char) (value >> 8);
6721 buffer[1]=(
unsigned char) value;
6722 return(WriteBlobStream(image,2,buffer));
6750MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6752 assert(image != (Image *) NULL);
6753 assert(image->signature == MagickCoreSignature);
6754 assert(
string != (
const char *) NULL);
6755 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));