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;
1116MagickExport
void DisassociateBlob(Image *image)
1119 *magick_restrict blob_info,
1125 assert(image != (Image *) NULL);
1126 assert(image->signature == MagickCoreSignature);
1127 assert(image->blob != (BlobInfo *) NULL);
1128 assert(image->blob->signature == MagickCoreSignature);
1129 if (IsEventLogging() != MagickFalse)
1130 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1131 blob_info=image->blob;
1133 LockSemaphoreInfo(blob_info->semaphore);
1134 assert(blob_info->reference_count >= 0);
1135 if (blob_info->reference_count > 1)
1137 UnlockSemaphoreInfo(blob_info->semaphore);
1138 if (clone == MagickFalse)
1140 clone_info=CloneBlobInfo(blob_info);
1142 image->blob=clone_info;
1170MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1171 const MagickSizeType length)
1183 buffer[MagickMinBufferExtent >> 1];
1185 assert(image != (Image *) NULL);
1186 assert(image->signature == MagickCoreSignature);
1187 if (length != (MagickSizeType) ((MagickOffsetType) length))
1188 return(MagickFalse);
1190 for (i=0; i < length; i+=(MagickSizeType) count)
1192 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1193 (void) ReadBlobStream(image,quantum,buffer,&count);
1201 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1228MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
1230 assert(image != (Image *) NULL);
1231 assert(image->signature == MagickCoreSignature);
1232 assert(duplicate != (Image *) NULL);
1233 assert(duplicate->signature == MagickCoreSignature);
1234 if (IsEventLogging() != MagickFalse)
1235 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1237 image->blob=ReferenceBlob(duplicate->blob);
1263MagickExport
int EOFBlob(
const Image *image)
1266 *magick_restrict blob_info;
1268 assert(image != (Image *) NULL);
1269 assert(image->signature == MagickCoreSignature);
1270 assert(image->blob != (BlobInfo *) NULL);
1271 assert(image->blob->type != UndefinedStream);
1272 if (IsEventLogging() != MagickFalse)
1273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1274 blob_info=image->blob;
1275 switch (blob_info->type)
1277 case UndefinedStream:
1278 case StandardStream:
1283 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1289#if defined(MAGICKCORE_ZLIB_DELEGATE)
1290 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1297#if defined(MAGICKCORE_BZLIB_DELEGATE)
1302 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1303 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1309 blob_info->eof=MagickFalse;
1317 return((
int) blob_info->eof);
1343MagickExport
int ErrorBlob(
const Image *image)
1346 *magick_restrict blob_info;
1348 assert(image != (Image *) NULL);
1349 assert(image->signature == MagickCoreSignature);
1350 assert(image->blob != (BlobInfo *) NULL);
1351 assert(image->blob->type != UndefinedStream);
1352 if (IsEventLogging() != MagickFalse)
1353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1354 blob_info=image->blob;
1355 switch (blob_info->type)
1357 case UndefinedStream:
1358 case StandardStream:
1363 blob_info->error=ferror(blob_info->file_info.file);
1368#if defined(MAGICKCORE_ZLIB_DELEGATE)
1369 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1375#if defined(MAGICKCORE_BZLIB_DELEGATE)
1376 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1390 return(blob_info->error);
1428MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1429 size_t *length,ExceptionInfo *exception)
1455 assert(filename != (
const char *) NULL);
1456 assert(exception != (ExceptionInfo *) NULL);
1457 assert(exception->signature == MagickCoreSignature);
1458 if (IsEventLogging() != MagickFalse)
1459 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1461 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1462 ThrowPolicyException(filename,NULL);
1464 if (LocaleCompare(filename,
"-") != 0)
1467 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1469 status=GetPathAttributes(filename,&attributes);
1470 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1472 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1475 file=open_utf8(filename,flags,0);
1479 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1482 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1484 file=close_utf8(file)-1;
1485 ThrowPolicyException(filename,NULL);
1487 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1489 if ((file == fileno(stdin)) || (offset < 0) ||
1490 (offset != (MagickOffsetType) ((ssize_t) offset)))
1501 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1502 quantum=(size_t) MagickMaxBufferExtent;
1503 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1504 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1505 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1506 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1508 count=MagickRead(file,blob+i,quantum);
1511 if (~i < ((
size_t) count+quantum+1))
1513 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1516 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1517 quantum+1,
sizeof(*blob));
1518 if ((i+(
size_t) count) >= extent)
1521 if (LocaleCompare(filename,
"-") != 0)
1522 file=close_utf8(file);
1523 if (blob == (
unsigned char *) NULL)
1525 (void) ThrowMagickException(exception,GetMagickModule(),
1526 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1531 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1532 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1535 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1539 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1540 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1541 blob=(
unsigned char *) NULL;
1542 if (~(*length) >= (MagickPathExtent-1))
1543 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1545 if (blob == (
unsigned char *) NULL)
1547 file=close_utf8(file);
1548 (void) ThrowMagickException(exception,GetMagickModule(),
1549 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1552 map=MapBlob(file,ReadMode,0,*length);
1553 if (map != (
unsigned char *) NULL)
1555 (void) memcpy(blob,map,*length);
1556 (void) UnmapBlob(map,*length);
1560 (void) lseek(file,0,SEEK_SET);
1561 for (i=0; i < *length; i+=(size_t) count)
1563 count=MagickRead(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1564 MagickMaxBufferExtent));
1570 file=close_utf8(file)-1;
1571 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1572 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1577 if (LocaleCompare(filename,
"-") != 0)
1578 file=close_utf8(file);
1581 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1582 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1612static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1613 const void *magick_restrict data)
1616 *magick_restrict blob_info;
1624 assert(image->blob != (BlobInfo *) NULL);
1625 assert(image->blob->type != UndefinedStream);
1626 assert(data != NULL);
1627 blob_info=image->blob;
1628 if (blob_info->type != BlobStream)
1629 return(WriteBlob(image,length,(
const unsigned char *) data));
1630 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1635 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1636 if (extent >= blob_info->extent)
1638 extent+=blob_info->quantum+length;
1639 blob_info->quantum<<=1;
1640 if (SetBlobExtent(image,extent) == MagickFalse)
1643 q=blob_info->data+blob_info->offset;
1644 (void) memcpy(q,data,length);
1645 blob_info->offset+=(MagickOffsetType) length;
1646 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1647 blob_info->length=(size_t) blob_info->offset;
1648 return((ssize_t) length);
1651MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1652 ExceptionInfo *exception)
1670 assert(image != (
const Image *) NULL);
1671 assert(image->signature == MagickCoreSignature);
1672 assert(filename != (
const char *) NULL);
1673 if (IsEventLogging() != MagickFalse)
1674 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1675 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1676 ThrowPolicyException(filename,MagickFalse);
1678 if (LocaleCompare(filename,
"-") != 0)
1681 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1683 file=open_utf8(filename,flags,0);
1687 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1688 return(MagickFalse);
1690 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1691 ThrowPolicyException(filename,MagickFalse);
1692 quantum=(size_t) MagickMaxBufferExtent;
1693 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1694 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1695 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1696 if (blob == (
unsigned char *) NULL)
1698 file=close_utf8(file);
1699 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1701 return(MagickFalse);
1705 count=MagickRead(file,blob,quantum);
1708 length=(size_t) count;
1709 count=WriteBlobStream(image,length,blob);
1710 if (count != (ssize_t) length)
1712 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1716 file=close_utf8(file);
1718 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1719 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1746MagickExport MagickBooleanType GetBlobError(
const Image *image)
1748 assert(image != (
const Image *) NULL);
1749 assert(image->signature == MagickCoreSignature);
1750 if (IsEventLogging() != MagickFalse)
1751 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1752 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1753 errno=image->blob->error_number;
1754 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1780MagickExport FILE *GetBlobFileHandle(
const Image *image)
1782 assert(image != (
const Image *) NULL);
1783 assert(image->signature == MagickCoreSignature);
1784 assert(image->blob != (BlobInfo *) NULL);
1785 assert(image->blob->signature == MagickCoreSignature);
1786 switch (image->blob->type)
1788 case StandardStream:
1791 return(image->blob->file_info.file);
1792 case UndefinedStream:
1800 return((FILE *) NULL);
1825MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1827 assert(blob_info != (BlobInfo *) NULL);
1828 (void) memset(blob_info,0,
sizeof(*blob_info));
1829 blob_info->type=UndefinedStream;
1830 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1831 blob_info->properties.st_mtime=GetMagickTime();
1832 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1833 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1834 blob_info->reference_count=1;
1835 blob_info->semaphore=AcquireSemaphoreInfo();
1836 blob_info->signature=MagickCoreSignature;
1861MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1863 assert(image != (Image *) NULL);
1864 assert(image->signature == MagickCoreSignature);
1865 if (IsEventLogging() != MagickFalse)
1866 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1867 return(&image->blob->properties);
1893MagickExport MagickSizeType GetBlobSize(
const Image *image)
1896 *magick_restrict blob_info;
1901 assert(image != (Image *) NULL);
1902 assert(image->signature == MagickCoreSignature);
1903 assert(image->blob != (BlobInfo *) NULL);
1904 if (IsEventLogging() != MagickFalse)
1905 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1906 blob_info=image->blob;
1908 switch (blob_info->type)
1910 case UndefinedStream:
1911 case StandardStream:
1913 extent=blob_info->size;
1921 extent=(MagickSizeType) blob_info->properties.st_size;
1923 extent=blob_info->size;
1924 file_descriptor=fileno(blob_info->file_info.file);
1925 if (file_descriptor == -1)
1927 if (fstat(file_descriptor,&blob_info->properties) == 0)
1928 extent=(MagickSizeType) blob_info->properties.st_size;
1933 extent=blob_info->size;
1942 status=GetPathAttributes(image->filename,&blob_info->properties);
1943 if (status != MagickFalse)
1944 extent=(MagickSizeType) blob_info->properties.st_size;
1951 extent=(MagickSizeType) blob_info->length;
1956 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1957 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1962 offset=blob_info->custom_stream->teller(
1963 blob_info->custom_stream->data);
1964 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1965 blob_info->custom_stream->data);
1966 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1967 blob_info->custom_stream->data);
1997MagickExport
void *GetBlobStreamData(
const Image *image)
1999 assert(image != (
const Image *) NULL);
2000 assert(image->signature == MagickCoreSignature);
2001 return(image->blob->data);
2026MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2028 assert(image != (
const Image *) NULL);
2029 assert(image->signature == MagickCoreSignature);
2030 if (IsEventLogging() != MagickFalse)
2031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2032 return(image->blob->stream);
2068MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2069 Image *image,
size_t *length,ExceptionInfo *exception)
2083 assert(image_info != (
const ImageInfo *) NULL);
2084 assert(image_info->signature == MagickCoreSignature);
2085 assert(image != (Image *) NULL);
2086 assert(image->signature == MagickCoreSignature);
2087 assert(exception != (ExceptionInfo *) NULL);
2088 assert(exception->signature == MagickCoreSignature);
2089 if (IsEventLogging() != MagickFalse)
2090 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2091 image_info->filename);
2093 blob=(
unsigned char *) NULL;
2094 blob_info=CloneImageInfo(image_info);
2095 blob_info->adjoin=MagickFalse;
2096 (void) SetImageInfo(blob_info,1,exception);
2097 if (*blob_info->magick !=
'\0')
2098 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2099 magick_info=GetMagickInfo(image->magick,exception);
2100 if (magick_info == (
const MagickInfo *) NULL)
2102 (void) ThrowMagickException(exception,GetMagickModule(),
2103 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2105 blob_info=DestroyImageInfo(blob_info);
2108 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2109 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2114 blob_info->length=0;
2115 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2116 sizeof(
unsigned char));
2117 if (blob_info->blob == NULL)
2118 (void) ThrowMagickException(exception,GetMagickModule(),
2119 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2122 (void) CloseBlob(image);
2123 image->blob->exempt=MagickTrue;
2124 image->blob->extent=0;
2125 *image->filename=
'\0';
2126 status=WriteImage(blob_info,image,exception);
2127 SyncBlobStream(blob_info,image,image);
2128 *length=image->blob->length;
2129 blob=DetachBlob(image->blob);
2130 if (blob != (
void *) NULL)
2132 if (status == MagickFalse)
2133 blob=RelinquishMagickMemory(blob);
2135 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2138 if ((status == MagickFalse) && (image->blob->extent == 0))
2139 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2145 unique[MagickPathExtent];
2153 file=AcquireUniqueFileResource(unique);
2156 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2157 image_info->filename);
2161 blob_info->file=fdopen(file,
"wb");
2162 if (blob_info->file != (FILE *) NULL)
2164 (void) FormatLocaleString(image->filename,MagickPathExtent,
2165 "%s:%s",image->magick,unique);
2166 status=WriteImage(blob_info,image,exception);
2167 (void) fclose(blob_info->file);
2168 if (status != MagickFalse)
2169 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2171 (void) RelinquishUniqueFileResource(unique);
2174 blob_info=DestroyImageInfo(blob_info);
2206MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2207 ExceptionInfo *exception)
2219 assert(image_info != (
const ImageInfo *) NULL);
2220 assert(image_info->signature == MagickCoreSignature);
2221 assert(image != (Image *) NULL);
2222 assert(image->signature == MagickCoreSignature);
2223 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2224 assert(image_info->custom_stream->signature == MagickCoreSignature);
2225 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2226 assert(exception != (ExceptionInfo *) NULL);
2227 if (IsEventLogging() != MagickFalse)
2228 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2229 image_info->filename);
2230 clone_info=CloneImageInfo(image_info);
2231 clone_info->adjoin=MagickFalse;
2232 (void) SetImageInfo(clone_info,1,exception);
2233 if (*clone_info->magick !=
'\0')
2234 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2235 magick_info=GetMagickInfo(image->magick,exception);
2236 if (magick_info == (
const MagickInfo *) NULL)
2238 (void) ThrowMagickException(exception,GetMagickModule(),
2239 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2241 clone_info=DestroyImageInfo(clone_info);
2244 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2245 blob_support=GetMagickBlobSupport(magick_info);
2246 if ((blob_support != MagickFalse) &&
2247 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2249 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2250 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2251 blob_support=MagickFalse;
2253 if (blob_support != MagickFalse)
2258 (void) CloseBlob(image);
2259 *image->filename=
'\0';
2260 (void) WriteImage(clone_info,image,exception);
2265 unique[MagickPathExtent];
2276 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2277 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2279 if (blob == (
unsigned char *) NULL)
2281 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2282 image_info->filename);
2283 clone_info=DestroyImageInfo(clone_info);
2286 file=AcquireUniqueFileResource(unique);
2289 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2290 image_info->filename);
2291 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2292 clone_info=DestroyImageInfo(clone_info);
2295 clone_info->file=fdopen(file,
"wb+");
2296 if (clone_info->file != (FILE *) NULL)
2301 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2302 image->magick,unique);
2303 status=WriteImage(clone_info,image,exception);
2304 if (status != MagickFalse)
2306 (void) fseek(clone_info->file,0,SEEK_SET);
2307 count=(ssize_t) MagickMaxBufferExtent;
2308 while (count == (ssize_t) MagickMaxBufferExtent)
2310 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2312 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2313 image_info->custom_stream->data);
2316 (void) fclose(clone_info->file);
2318 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2319 (void) RelinquishUniqueFileResource(unique);
2321 clone_info=DestroyImageInfo(clone_info);
2352MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2353 ExceptionInfo *exception)
2377 assert(image != (Image *) NULL);
2378 assert(image->signature == MagickCoreSignature);
2379 assert(image->blob != (BlobInfo *) NULL);
2380 assert(image->blob->type != UndefinedStream);
2381 assert(filename != (
const char *) NULL);
2382 if (IsEventLogging() != MagickFalse)
2383 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2384 if (*filename ==
'\0')
2385 file=AcquireUniqueFileResource(filename);
2387 if (LocaleCompare(filename,
"-") == 0)
2388 file=fileno(stdout);
2390 file=open_utf8(filename,O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2393 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2394 return(MagickFalse);
2396 quantum=(size_t) MagickMaxBufferExtent;
2397 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2398 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2399 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2400 if (buffer == (
unsigned char *) NULL)
2402 file=close_utf8(file)-1;
2403 (void) ThrowMagickException(exception,GetMagickModule(),
2404 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2405 return(MagickFalse);
2408 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2409 for (i=0; count > 0; )
2411 length=(size_t) count;
2412 for (i=0; i < length; i+=(size_t) count)
2414 count=MagickWrite(file,p+i,(
size_t) (length-i));
2420 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2422 if (LocaleCompare(filename,
"-") != 0)
2423 file=close_utf8(file);
2424 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2425 if ((file == -1) || (i < length))
2428 file=close_utf8(file);
2429 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2430 return(MagickFalse);
2470MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2471 size_t *length,ExceptionInfo *exception)
2485 assert(image_info != (
const ImageInfo *) NULL);
2486 assert(image_info->signature == MagickCoreSignature);
2487 assert(images != (Image *) NULL);
2488 assert(images->signature == MagickCoreSignature);
2489 assert(exception != (ExceptionInfo *) NULL);
2490 if (IsEventLogging() != MagickFalse)
2491 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2492 image_info->filename);
2494 blob=(
unsigned char *) NULL;
2495 blob_info=CloneImageInfo(image_info);
2496 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2498 if (*blob_info->magick !=
'\0')
2499 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2500 magick_info=GetMagickInfo(images->magick,exception);
2501 if (magick_info == (
const MagickInfo *) NULL)
2503 (void) ThrowMagickException(exception,GetMagickModule(),
2504 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2506 blob_info=DestroyImageInfo(blob_info);
2509 if (GetMagickAdjoin(magick_info) == MagickFalse)
2511 blob_info=DestroyImageInfo(blob_info);
2512 return(ImageToBlob(image_info,images,length,exception));
2514 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2515 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2520 blob_info->length=0;
2521 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2522 sizeof(
unsigned char));
2523 if (blob_info->blob == (
void *) NULL)
2524 (void) ThrowMagickException(exception,GetMagickModule(),
2525 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2528 (void) CloseBlob(images);
2529 images->blob->exempt=MagickTrue;
2530 images->blob->extent=0;
2531 *images->filename=
'\0';
2532 status=WriteImages(blob_info,images,images->filename,exception);
2533 SyncBlobStream(blob_info,images,images);
2534 *length=images->blob->length;
2535 blob=DetachBlob(images->blob);
2536 if (blob != (
void *) NULL)
2538 if (status == MagickFalse)
2539 blob=RelinquishMagickMemory(blob);
2541 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2543 else if ((status == MagickFalse) && (images->blob->extent == 0))
2544 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2550 filename[MagickPathExtent],
2551 unique[MagickPathExtent];
2559 file=AcquireUniqueFileResource(unique);
2562 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2563 image_info->filename);
2567 blob_info->file=fdopen(file,
"wb");
2568 if (blob_info->file != (FILE *) NULL)
2570 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2571 images->magick,unique);
2572 status=WriteImages(blob_info,images,filename,exception);
2573 (void) fclose(blob_info->file);
2574 if (status != MagickFalse)
2575 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2577 (void) RelinquishUniqueFileResource(unique);
2580 blob_info=DestroyImageInfo(blob_info);
2612MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2613 Image *images,ExceptionInfo *exception)
2625 assert(image_info != (
const ImageInfo *) NULL);
2626 assert(image_info->signature == MagickCoreSignature);
2627 assert(images != (Image *) NULL);
2628 assert(images->signature == MagickCoreSignature);
2629 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2630 assert(image_info->custom_stream->signature == MagickCoreSignature);
2631 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2632 assert(exception != (ExceptionInfo *) NULL);
2633 if (IsEventLogging() != MagickFalse)
2634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2635 image_info->filename);
2636 clone_info=CloneImageInfo(image_info);
2637 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2639 if (*clone_info->magick !=
'\0')
2640 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2641 magick_info=GetMagickInfo(images->magick,exception);
2642 if (magick_info == (
const MagickInfo *) NULL)
2644 (void) ThrowMagickException(exception,GetMagickModule(),
2645 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2647 clone_info=DestroyImageInfo(clone_info);
2650 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2651 blob_support=GetMagickBlobSupport(magick_info);
2652 if ((blob_support != MagickFalse) &&
2653 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2655 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2656 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2657 blob_support=MagickFalse;
2659 if (blob_support != MagickFalse)
2664 (void) CloseBlob(images);
2665 *images->filename=
'\0';
2666 (void) WriteImages(clone_info,images,images->filename,exception);
2671 filename[MagickPathExtent],
2672 unique[MagickPathExtent];
2683 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2684 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2686 if (blob == (
unsigned char *) NULL)
2688 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2689 image_info->filename);
2690 clone_info=DestroyImageInfo(clone_info);
2693 file=AcquireUniqueFileResource(unique);
2696 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2697 image_info->filename);
2698 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2699 clone_info=DestroyImageInfo(clone_info);
2702 clone_info->file=fdopen(file,
"wb+");
2703 if (clone_info->file != (FILE *) NULL)
2708 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2709 images->magick,unique);
2710 status=WriteImages(clone_info,images,filename,exception);
2711 if (status != MagickFalse)
2713 (void) fseek(clone_info->file,0,SEEK_SET);
2714 count=(ssize_t) MagickMaxBufferExtent;
2715 while (count == (ssize_t) MagickMaxBufferExtent)
2717 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2719 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2720 image_info->custom_stream->data);
2723 (void) fclose(clone_info->file);
2725 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2726 (void) RelinquishUniqueFileResource(unique);
2728 clone_info=DestroyImageInfo(clone_info);
2764MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2765 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2768 filename[MagickPathExtent];
2797 assert(image_info != (ImageInfo *) NULL);
2798 assert(image_info->signature == MagickCoreSignature);
2799 assert(image != (Image *) NULL);
2800 assert(image->signature == MagickCoreSignature);
2801 assert(inject_image != (Image *) NULL);
2802 assert(inject_image->signature == MagickCoreSignature);
2803 assert(exception != (ExceptionInfo *) NULL);
2804 if (IsEventLogging() != MagickFalse)
2805 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2806 unique_file=(FILE *) NULL;
2807 file=AcquireUniqueFileResource(filename);
2809 unique_file=fdopen(file,
"wb");
2810 if ((file == -1) || (unique_file == (FILE *) NULL))
2812 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2813 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2815 return(MagickFalse);
2817 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2818 if (byte_image == (Image *) NULL)
2820 (void) fclose(unique_file);
2821 (void) RelinquishUniqueFileResource(filename);
2822 return(MagickFalse);
2824 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2826 DestroyBlob(byte_image);
2827 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2828 write_info=CloneImageInfo(image_info);
2829 SetImageInfoFile(write_info,unique_file);
2830 status=WriteImage(write_info,byte_image,exception);
2831 write_info=DestroyImageInfo(write_info);
2832 byte_image=DestroyImage(byte_image);
2833 (void) fclose(unique_file);
2834 if (status == MagickFalse)
2836 (void) RelinquishUniqueFileResource(filename);
2837 return(MagickFalse);
2842 file=open_utf8(filename,O_RDONLY | O_CLOEXEC | O_BINARY,0);
2845 (void) RelinquishUniqueFileResource(filename);
2846 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2847 image_info->filename);
2848 return(MagickFalse);
2850 quantum=(size_t) MagickMaxBufferExtent;
2851 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2852 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2853 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2854 if (buffer == (
unsigned char *) NULL)
2856 (void) RelinquishUniqueFileResource(filename);
2857 file=close_utf8(file);
2858 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2863 ssize_t count = read(file,buffer,quantum);
2870 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2873 file=close_utf8(file);
2875 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2876 (void) RelinquishUniqueFileResource(filename);
2877 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2903MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2905 assert(image != (
const Image *) NULL);
2906 assert(image->signature == MagickCoreSignature);
2907 if (IsEventLogging() != MagickFalse)
2908 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2909 return(image->blob->exempt);
2934MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2937 *magick_restrict blob_info;
2939 assert(image != (
const Image *) NULL);
2940 assert(image->signature == MagickCoreSignature);
2941 if (IsEventLogging() != MagickFalse)
2942 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2943 blob_info=image->blob;
2944 switch (blob_info->type)
2953 if (blob_info->file_info.file == (FILE *) NULL)
2954 return(MagickFalse);
2955 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2956 return(status == -1 ? MagickFalse : MagickTrue);
2960#if defined(MAGICKCORE_ZLIB_DELEGATE)
2964 if (blob_info->file_info.gzfile == (gzFile) NULL)
2965 return(MagickFalse);
2966 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2967 return(offset < 0 ? MagickFalse : MagickTrue);
2972 case UndefinedStream:
2976 case StandardStream:
2980 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
2981 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
2988 return(MagickFalse);
3013MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3015 assert(image != (
const Image *) NULL);
3016 assert(image->signature == MagickCoreSignature);
3017 if (IsEventLogging() != MagickFalse)
3018 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3019 return(image->blob->temporary);
3051MagickExport
void *MapBlob(
int file,
const MapMode mode,
3052 const MagickOffsetType offset,
const size_t length)
3054#if defined(MAGICKCORE_HAVE_MMAP)
3067#if defined(MAP_ANONYMOUS)
3068 flags|=MAP_ANONYMOUS;
3077 protection=PROT_READ;
3083 protection=PROT_WRITE;
3089 protection=PROT_READ | PROT_WRITE;
3094#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3095 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3097 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3098 if (map == MAP_FAILED)
3099 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3101 if (map == MAP_FAILED)
3138MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3147 assert(buffer != (
unsigned char *) NULL);
3154 *buffer++=(
unsigned char) c;
3158 *buffer++=(
unsigned char) c;
3188MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3196 assert(p != (
unsigned char *) NULL);
3203 *p++=(
unsigned char) c;
3239static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3240 const BlobInfo *blob_info)
3251 size=MagickMinBufferExtent;
3252 option=GetImageOption(image_info,
"stream:buffer-size");
3253 if (option != (
const char *) NULL)
3254 size=StringToUnsignedLong(option);
3255 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3256 _IONBF : _IOFBF,size);
3257 return(status == 0 ? MagickTrue : MagickFalse);
3260#if defined(MAGICKCORE_ZLIB_DELEGATE)
3261static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3263#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3264 return(gzopen(path,mode));
3272 path_wide=NTCreateWidePath(path);
3273 if (path_wide == (
wchar_t *) NULL)
3274 return((gzFile) NULL);
3275 file=gzopen_w(path_wide,mode);
3276 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3282MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3283 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3286 *magick_restrict blob_info;
3289 extension[MagickPathExtent],
3290 filename[MagickPathExtent];
3296 flags = O_RDONLY | O_CLOEXEC;
3304 assert(image_info != (ImageInfo *) NULL);
3305 assert(image_info->signature == MagickCoreSignature);
3306 assert(image != (Image *) NULL);
3307 assert(image->signature == MagickCoreSignature);
3308 if (IsEventLogging() != MagickFalse)
3309 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3310 image_info->filename);
3311 blob_info=image->blob;
3312 if (image_info->blob != (
void *) NULL)
3314 if (image_info->stream != (StreamHandler) NULL)
3315 blob_info->stream=(StreamHandler) image_info->stream;
3316 AttachBlob(blob_info,image_info->blob,image_info->length);
3319 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3320 (*image->filename ==
'\0'))
3322 blob_info->type=CustomStream;
3323 blob_info->custom_stream=image_info->custom_stream;
3324 if (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL)
3325 blob_info->custom_stream->seeker(0,SEEK_SET,
3326 blob_info->custom_stream->data);
3329 (void) DetachBlob(blob_info);
3330 blob_info->mode=mode;
3335 flags=O_RDONLY | O_CLOEXEC;
3339 case ReadBinaryBlobMode:
3341 flags=O_RDONLY | O_CLOEXEC | O_BINARY;
3347 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC;
3351 case WriteBinaryBlobMode:
3353 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_TRUNC | O_BINARY;
3357 case AppendBlobMode:
3359 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_APPEND;
3363 case AppendBinaryBlobMode:
3365 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_APPEND | O_BINARY;
3371 flags=O_RDONLY | O_CLOEXEC;
3377 blob_info->synchronize=image_info->synchronize;
3378 if (image_info->stream != (StreamHandler) NULL)
3380 blob_info->stream=image_info->stream;
3383 blob_info->type=FifoStream;
3391 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3392 rights=ReadPolicyRights;
3394 rights=WritePolicyRights;
3395 if (IsPathAuthorized(rights,filename) == MagickFalse)
3396 ThrowPolicyException(filename,MagickFalse);
3397 if ((LocaleCompare(filename,
"-") == 0) ||
3398 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3400 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3401#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3402 if (strchr(type,
'b') != (
char *) NULL)
3403 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3405 blob_info->type=StandardStream;
3406 blob_info->exempt=MagickTrue;
3407 return(SetStreamBuffering(image_info,blob_info));
3409 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3410 (IsGeometry(filename+3) != MagickFalse))
3417 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3418 if (blob_info->file_info.file == (FILE *) NULL)
3420 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3421 return(MagickFalse);
3423#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3424 if (strchr(type,
'b') != (
char *) NULL)
3425 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3427 blob_info->type=FileStream;
3428 blob_info->exempt=MagickTrue;
3429 return(SetStreamBuffering(image_info,blob_info));
3431#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3432 if (*filename ==
'|')
3435 fileMode[MagickPathExtent],
3443 (void) signal(SIGPIPE,SIG_IGN);
3447 sanitize_command=SanitizeString(filename+1);
3448 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3449 sanitize_command=DestroyString(sanitize_command);
3450 if (blob_info->file_info.file == (FILE *) NULL)
3452 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3453 return(MagickFalse);
3455 blob_info->type=PipeStream;
3456 blob_info->exempt=MagickTrue;
3457 return(SetStreamBuffering(image_info,blob_info));
3460 status=GetPathAttributes(filename,&blob_info->properties);
3461#if defined(S_ISFIFO)
3462 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3464 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3465 if (blob_info->file_info.file == (FILE *) NULL)
3467 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3468 return(MagickFalse);
3470 blob_info->type=FileStream;
3471 blob_info->exempt=MagickTrue;
3472 return(SetStreamBuffering(image_info,blob_info));
3475 GetPathComponent(image->filename,ExtensionPath,extension);
3478 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3479 if ((image_info->adjoin == MagickFalse) ||
3480 (strchr(filename,
'%') != (
char *) NULL))
3485 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3486 image->scene,filename,exception);
3487 if ((LocaleCompare(filename,image->filename) == 0) &&
3488 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3489 (GetNextImageInList(image) != (Image *) NULL)))
3492 path[MagickPathExtent];
3494 GetPathComponent(image->filename,RootPath,path);
3495 if (*extension ==
'\0')
3496 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.17g",
3497 path,(
double) image->scene);
3499 (
void) FormatLocaleString(filename,MagickPathExtent,
3500 "%s-%.17g.%s",path,(
double) image->scene,extension);
3502 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3504 if (IsPathAuthorized(rights,filename) == MagickFalse)
3505 ThrowPolicyException(filename,MagickFalse);
3507 if (image_info->file != (FILE *) NULL)
3509 blob_info->file_info.file=image_info->file;
3510 blob_info->type=FileStream;
3511 blob_info->exempt=MagickTrue;
3519 blob_info->file_info.file=(FILE *) NULL;
3520 file=open_utf8(filename,flags,0);
3522 blob_info->file_info.file=fdopen(file,type);
3523 if (blob_info->file_info.file != (FILE *) NULL)
3531 blob_info->type=FileStream;
3532 (void) SetStreamBuffering(image_info,blob_info);
3533 (void) memset(magick,0,
sizeof(magick));
3534 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3535 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3536#if defined(MAGICKCORE_POSIX_SUPPORT)
3537 (void) fflush(blob_info->file_info.file);
3539 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3540 " read %.17g magic header bytes",(
double) count);
3541#if defined(MAGICKCORE_ZLIB_DELEGATE)
3542 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3543 ((
int) magick[2] == 0x08))
3546 gzfile = gzopen_utf8(filename,
"rb");
3548 if (gzfile != (gzFile) NULL)
3550 if (blob_info->file_info.file != (FILE *) NULL)
3551 (void) fclose(blob_info->file_info.file);
3552 blob_info->file_info.file=(FILE *) NULL;
3553 blob_info->file_info.gzfile=gzfile;
3554 blob_info->type=ZipStream;
3558#if defined(MAGICKCORE_BZLIB_DELEGATE)
3559 if (strncmp((
char *) magick,
"BZh",3) == 0)
3562 *bzfile = BZ2_bzopen(filename,
"r");
3564 if (bzfile != (BZFILE *) NULL)
3566 if (blob_info->file_info.file != (FILE *) NULL)
3567 (void) fclose(blob_info->file_info.file);
3568 blob_info->file_info.file=(FILE *) NULL;
3569 blob_info->file_info.bzfile=bzfile;
3570 blob_info->type=BZipStream;
3574 if (blob_info->type == FileStream)
3585 sans_exception=AcquireExceptionInfo();
3586 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3587 sans_exception=DestroyExceptionInfo(sans_exception);
3588 length=(size_t) blob_info->properties.st_size;
3589 if ((magick_info != (
const MagickInfo *) NULL) &&
3590 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3591 (AcquireMagickResource(MapResource,length) != MagickFalse))
3596 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3598 if (blob == (
void *) NULL)
3599 RelinquishMagickResource(MapResource,length);
3605 if (image_info->file != (FILE *) NULL)
3606 blob_info->exempt=MagickFalse;
3609 (void) fclose(blob_info->file_info.file);
3610 blob_info->file_info.file=(FILE *) NULL;
3612 AttachBlob(blob_info,blob,length);
3613 blob_info->mapped=MagickTrue;
3620#if defined(MAGICKCORE_ZLIB_DELEGATE)
3621 if ((LocaleCompare(extension,
"gz") == 0) ||
3622 (LocaleCompare(extension,
"wmz") == 0) ||
3623 (LocaleCompare(extension,
"svgz") == 0))
3625 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3626 if (blob_info->file_info.gzfile != (gzFile) NULL)
3627 blob_info->type=ZipStream;
3631#if defined(MAGICKCORE_BZLIB_DELEGATE)
3632 if (LocaleCompare(extension,
"bz2") == 0)
3634 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3635 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3636 blob_info->type=BZipStream;
3644 blob_info->file_info.file=(FILE *) NULL;
3645 file=open_utf8(filename,flags,P_MODE);
3647 blob_info->file_info.file=fdopen(file,type);
3648 if (blob_info->file_info.file != (FILE *) NULL)
3650 blob_info->type=FileStream;
3651 (void) SetStreamBuffering(image_info,blob_info);
3654 if (IsPathAuthorized(rights,filename) == MagickFalse)
3655 ThrowPolicyException(filename,MagickFalse);
3656 blob_info->status=0;
3657 blob_info->error_number=0;
3658 if (blob_info->type != UndefinedStream)
3659 blob_info->size=GetBlobSize(image);
3662 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3663 return(MagickFalse);
3702#if defined(__cplusplus) || defined(c_plusplus)
3706static size_t PingStream(
const Image *magick_unused(image),
3707 const void *magick_unused(pixels),
const size_t columns)
3709 magick_unreferenced(image);
3710 magick_unreferenced(pixels);
3714#if defined(__cplusplus) || defined(c_plusplus)
3718MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3719 const size_t length,ExceptionInfo *exception)
3734 assert(image_info != (ImageInfo *) NULL);
3735 assert(image_info->signature == MagickCoreSignature);
3736 assert(exception != (ExceptionInfo *) NULL);
3737 if (IsEventLogging() != MagickFalse)
3738 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3739 image_info->filename);
3740 if ((blob == (
const void *) NULL) || (length == 0))
3742 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3743 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3744 return((Image *) NULL);
3746 ping_info=CloneImageInfo(image_info);
3747 ping_info->blob=(
void *) blob;
3748 ping_info->length=length;
3749 ping_info->ping=MagickTrue;
3750 if (*ping_info->magick ==
'\0')
3751 (void) SetImageInfo(ping_info,0,exception);
3752 magick_info=GetMagickInfo(ping_info->magick,exception);
3753 if (magick_info == (
const MagickInfo *) NULL)
3755 (void) ThrowMagickException(exception,GetMagickModule(),
3756 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3758 ping_info=DestroyImageInfo(ping_info);
3759 return((Image *) NULL);
3761 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3764 filename[MagickPathExtent];
3769 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3770 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3771 ping_info->magick,filename);
3772 image=ReadStream(ping_info,&PingStream,exception);
3773 if (image != (Image *) NULL)
3774 (void) DetachBlob(image->blob);
3775 ping_info=DestroyImageInfo(ping_info);
3781 ping_info->blob=(
void *) NULL;
3782 ping_info->length=0;
3783 *ping_info->filename=
'\0';
3784 status=BlobToFile(ping_info->filename,blob,length,exception);
3785 if (status == MagickFalse)
3787 (void) RelinquishUniqueFileResource(ping_info->filename);
3788 ping_info=DestroyImageInfo(ping_info);
3789 return((Image *) NULL);
3791 clone_info=CloneImageInfo(ping_info);
3792 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3793 ping_info->magick,ping_info->filename);
3794 image=ReadStream(clone_info,&PingStream,exception);
3795 if (image != (Image *) NULL)
3803 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3805 (void) CopyMagickString(images->filename,image_info->filename,
3807 (void) CopyMagickString(images->magick_filename,image_info->filename,
3809 (void) CopyMagickString(images->magick,magick_info->name,
3811 images=GetNextImageInList(images);
3814 clone_info=DestroyImageInfo(clone_info);
3815 (void) RelinquishUniqueFileResource(ping_info->filename);
3816 ping_info=DestroyImageInfo(ping_info);
3851MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3854 *magick_restrict blob_info;
3865 assert(image != (Image *) NULL);
3866 assert(image->signature == MagickCoreSignature);
3867 assert(image->blob != (BlobInfo *) NULL);
3868 assert(image->blob->type != UndefinedStream);
3871 assert(data != (
void *) NULL);
3872 blob_info=image->blob;
3874 q=(
unsigned char *) data;
3875 switch (blob_info->type)
3877 case UndefinedStream:
3879 case StandardStream:
3887 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3892 c=getc(blob_info->file_info.file);
3895 *q++=(
unsigned char) c;
3901 c=getc(blob_info->file_info.file);
3904 *q++=(
unsigned char) c;
3910 c=getc(blob_info->file_info.file);
3913 *q++=(
unsigned char) c;
3919 c=getc(blob_info->file_info.file);
3922 *q++=(
unsigned char) c;
3929 if ((count != (ssize_t) length) &&
3930 (ferror(blob_info->file_info.file) != 0))
3931 ThrowBlobException(blob_info);
3936#if defined(MAGICKCORE_ZLIB_DELEGATE)
3947 for (i=0; i < length; i+=(size_t) count)
3949 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3950 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3963 c=gzgetc(blob_info->file_info.gzfile);
3966 *q++=(
unsigned char) c;
3972 c=gzgetc(blob_info->file_info.gzfile);
3975 *q++=(
unsigned char) c;
3981 c=gzgetc(blob_info->file_info.gzfile);
3984 *q++=(
unsigned char) c;
3990 c=gzgetc(blob_info->file_info.gzfile);
3993 *q++=(
unsigned char) c;
4000 (void) gzerror(blob_info->file_info.gzfile,&status);
4001 if ((count != (ssize_t) length) && (status != Z_OK))
4002 ThrowBlobException(blob_info);
4003 if (blob_info->eof == MagickFalse)
4004 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4011#if defined(MAGICKCORE_BZLIB_DELEGATE)
4018 for (i=0; i < length; i+=(size_t) count)
4020 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4021 MagickMin(length-i,MagickMaxBufferExtent));
4031 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4032 if ((count != (ssize_t) length) && (status != BZ_OK))
4033 ThrowBlobException(blob_info);
4044 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4046 blob_info->eof=MagickTrue;
4049 p=blob_info->data+blob_info->offset;
4050 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4051 blob_info->length-blob_info->offset);
4052 blob_info->offset+=count;
4053 if (count != (ssize_t) length)
4054 blob_info->eof=MagickTrue;
4055 (void) memcpy(q,p,(
size_t) count);
4060 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4062 count=blob_info->custom_stream->reader(q,length,
4063 blob_info->custom_stream->data);
4066 blob_info->eof=MagickTrue;
4098MagickExport
int ReadBlobByte(Image *image)
4101 *magick_restrict blob_info;
4106 assert(image != (Image *) NULL);
4107 assert(image->signature == MagickCoreSignature);
4108 assert(image->blob != (BlobInfo *) NULL);
4109 assert(image->blob->type != UndefinedStream);
4110 blob_info=image->blob;
4111 switch (blob_info->type)
4113 case StandardStream:
4117 c=getc(blob_info->file_info.file);
4120 if (ferror(blob_info->file_info.file) != 0)
4121 ThrowBlobException(blob_info);
4128 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4130 blob_info->eof=MagickTrue;
4133 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4134 blob_info->offset++;
4145 count=ReadBlob(image,1,buffer);
4178MagickExport
double ReadBlobDouble(Image *image)
4189 quantum.double_value=0.0;
4190 quantum.unsigned_value=ReadBlobLongLong(image);
4191 return(quantum.double_value);
4217MagickExport
float ReadBlobFloat(Image *image)
4228 quantum.float_value=0.0;
4229 quantum.unsigned_value=ReadBlobLong(image);
4230 return(quantum.float_value);
4256MagickExport
unsigned int ReadBlobLong(Image *image)
4270 assert(image != (Image *) NULL);
4271 assert(image->signature == MagickCoreSignature);
4273 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4276 if (image->endian == LSBEndian)
4278 value=(
unsigned int) (*p++);
4279 value|=(
unsigned int) (*p++) << 8;
4280 value|=(
unsigned int) (*p++) << 16;
4281 value|=(
unsigned int) (*p++) << 24;
4284 value=(
unsigned int) (*p++) << 24;
4285 value|=(
unsigned int) (*p++) << 16;
4286 value|=(
unsigned int) (*p++) << 8;
4287 value|=(
unsigned int) (*p++);
4314MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4328 assert(image != (Image *) NULL);
4329 assert(image->signature == MagickCoreSignature);
4331 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4333 return(MagickULLConstant(0));
4334 if (image->endian == LSBEndian)
4336 value=(MagickSizeType) (*p++);
4337 value|=(MagickSizeType) (*p++) << 8;
4338 value|=(MagickSizeType) (*p++) << 16;
4339 value|=(MagickSizeType) (*p++) << 24;
4340 value|=(MagickSizeType) (*p++) << 32;
4341 value|=(MagickSizeType) (*p++) << 40;
4342 value|=(MagickSizeType) (*p++) << 48;
4343 value|=(MagickSizeType) (*p++) << 56;
4346 value=(MagickSizeType) (*p++) << 56;
4347 value|=(MagickSizeType) (*p++) << 48;
4348 value|=(MagickSizeType) (*p++) << 40;
4349 value|=(MagickSizeType) (*p++) << 32;
4350 value|=(MagickSizeType) (*p++) << 24;
4351 value|=(MagickSizeType) (*p++) << 16;
4352 value|=(MagickSizeType) (*p++) << 8;
4353 value|=(MagickSizeType) (*p++);
4380MagickExport
unsigned short ReadBlobShort(Image *image)
4394 assert(image != (Image *) NULL);
4395 assert(image->signature == MagickCoreSignature);
4397 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4399 return((
unsigned short) 0U);
4400 if (image->endian == LSBEndian)
4402 value=(
unsigned short) (*p++);
4403 value|=(
unsigned short) (*p++) << 8;
4406 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4407 value|=(
unsigned short) (*p++);
4434MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4448 assert(image != (Image *) NULL);
4449 assert(image->signature == MagickCoreSignature);
4451 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4454 value=(
unsigned int) (*p++);
4455 value|=(
unsigned int) (*p++) << 8;
4456 value|=(
unsigned int) (*p++) << 16;
4457 value|=(
unsigned int) (*p++) << 24;
4484MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4495 quantum.unsigned_value=ReadBlobLSBLong(image);
4496 return(quantum.signed_value);
4522MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4536 assert(image != (Image *) NULL);
4537 assert(image->signature == MagickCoreSignature);
4539 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4541 return((
unsigned short) 0U);
4542 value=(
unsigned short) (*p++);
4543 value|=(
unsigned short) (*p++) << 8;
4570MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4581 quantum.unsigned_value=ReadBlobLSBShort(image);
4582 return(quantum.signed_value);
4608MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4622 assert(image != (Image *) NULL);
4623 assert(image->signature == MagickCoreSignature);
4625 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4628 value=(
unsigned int) (*p++) << 24;
4629 value|=(
unsigned int) (*p++) << 16;
4630 value|=(
unsigned int) (*p++) << 8;
4631 value|=(
unsigned int) (*p++);
4658MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4672 assert(image != (Image *) NULL);
4673 assert(image->signature == MagickCoreSignature);
4675 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4677 return(MagickULLConstant(0));
4678 value=(MagickSizeType) (*p++) << 56;
4679 value|=(MagickSizeType) (*p++) << 48;
4680 value|=(MagickSizeType) (*p++) << 40;
4681 value|=(MagickSizeType) (*p++) << 32;
4682 value|=(MagickSizeType) (*p++) << 24;
4683 value|=(MagickSizeType) (*p++) << 16;
4684 value|=(MagickSizeType) (*p++) << 8;
4685 value|=(MagickSizeType) (*p++);
4712MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4726 assert(image != (Image *) NULL);
4727 assert(image->signature == MagickCoreSignature);
4729 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4731 return((
unsigned short) 0U);
4732 value=(
unsigned short) ((*p++) << 8);
4733 value|=(
unsigned short) (*p++);
4734 return((
unsigned short) (value & 0xffff));
4760MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4771 quantum.unsigned_value=ReadBlobMSBLong(image);
4772 return(quantum.signed_value);
4798MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4809 quantum.unsigned_value=ReadBlobMSBShort(image);
4810 return(quantum.signed_value);
4836MagickExport
signed int ReadBlobSignedLong(Image *image)
4847 quantum.unsigned_value=ReadBlobLong(image);
4848 return(quantum.signed_value);
4874MagickExport
signed short ReadBlobSignedShort(Image *image)
4885 quantum.unsigned_value=ReadBlobShort(image);
4886 return(quantum.signed_value);
4924MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4925 const size_t length,
void *magick_restrict data,ssize_t *count)
4928 *magick_restrict blob_info;
4930 assert(image != (Image *) NULL);
4931 assert(image->signature == MagickCoreSignature);
4932 assert(image->blob != (BlobInfo *) NULL);
4933 assert(image->blob->type != UndefinedStream);
4934 assert(count != (ssize_t *) NULL);
4935 blob_info=image->blob;
4936 if (blob_info->type != BlobStream)
4938 assert(data != NULL);
4939 *count=ReadBlob(image,length,(
unsigned char *) data);
4942 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4945 blob_info->eof=MagickTrue;
4948 data=blob_info->data+blob_info->offset;
4949 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4950 blob_info->length-blob_info->offset);
4951 blob_info->offset+=(*count);
4952 if (*count != (ssize_t) length)
4953 blob_info->eof=MagickTrue;
4982MagickExport
char *ReadBlobString(Image *image,
char *
string)
4985 *magick_restrict blob_info;
4993 assert(image != (Image *) NULL);
4994 assert(image->signature == MagickCoreSignature);
4995 assert(image->blob != (BlobInfo *) NULL);
4996 assert(image->blob->type != UndefinedStream);
4997 if (IsEventLogging() != MagickFalse)
4998 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5000 blob_info=image->blob;
5001 switch (blob_info->type)
5003 case UndefinedStream:
5005 case StandardStream:
5008 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5009 if (p == (
char *) NULL)
5011 if (ferror(blob_info->file_info.file) != 0)
5012 ThrowBlobException(blob_info);
5013 return((
char *) NULL);
5020#if defined(MAGICKCORE_ZLIB_DELEGATE)
5021 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5022 if (p == (
char *) NULL)
5025 (void) gzerror(blob_info->file_info.gzfile,&status);
5027 ThrowBlobException(blob_info);
5028 return((
char *) NULL);
5038 c=ReadBlobByte(image);
5041 blob_info->eof=MagickTrue;
5044 string[i++]=(char) c;
5047 }
while (i < (MaxTextExtent-2));
5055 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5058 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5060 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5061 return((
char *) NULL);
5088MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5090 assert(blob != (BlobInfo *) NULL);
5091 assert(blob->signature == MagickCoreSignature);
5092 if (IsEventLogging() != MagickFalse)
5093 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5094 LockSemaphoreInfo(blob->semaphore);
5095 blob->reference_count++;
5096 UnlockSemaphoreInfo(blob->semaphore);
5133MagickExport MagickOffsetType SeekBlob(Image *image,
5134 const MagickOffsetType offset,
const int whence)
5137 *magick_restrict blob_info;
5139 assert(image != (Image *) NULL);
5140 assert(image->signature == MagickCoreSignature);
5141 assert(image->blob != (BlobInfo *) NULL);
5142 assert(image->blob->type != UndefinedStream);
5143 if (IsEventLogging() != MagickFalse)
5144 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5145 blob_info=image->blob;
5146 switch (blob_info->type)
5148 case UndefinedStream:
5150 case StandardStream:
5155 if ((offset < 0) && (whence == SEEK_SET))
5157 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5159 blob_info->offset=TellBlob(image);
5164#if defined(MAGICKCORE_ZLIB_DELEGATE)
5165 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5168 blob_info->offset=TellBlob(image);
5184 blob_info->offset=offset;
5189 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5190 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5195 if ((blob_info->offset+offset) < 0)
5197 blob_info->offset+=offset;
5202 if (((MagickOffsetType) blob_info->length+offset) < 0)
5204 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5208 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5210 blob_info->eof=MagickFalse;
5217 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5219 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5220 blob_info->custom_stream->data);
5224 return(blob_info->offset);
5252MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5254 assert(image != (
const Image *) NULL);
5255 assert(image->signature == MagickCoreSignature);
5256 if (IsEventLogging() != MagickFalse)
5257 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5258 image->blob->exempt=exempt;
5287MagickExport MagickBooleanType SetBlobExtent(Image *image,
5288 const MagickSizeType extent)
5291 *magick_restrict blob_info;
5293 assert(image != (Image *) NULL);
5294 assert(image->signature == MagickCoreSignature);
5295 assert(image->blob != (BlobInfo *) NULL);
5296 assert(image->blob->type != UndefinedStream);
5297 if (IsEventLogging() != MagickFalse)
5298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5299 blob_info=image->blob;
5300 switch (blob_info->type)
5302 case UndefinedStream:
5304 case StandardStream:
5305 return(MagickFalse);
5314 if (extent != (MagickSizeType) ((off_t) extent))
5315 return(MagickFalse);
5316 offset=SeekBlob(image,0,SEEK_END);
5318 return(MagickFalse);
5319 if ((MagickSizeType) offset >= extent)
5321 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5324 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5325 blob_info->file_info.file);
5326#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5327 if (blob_info->synchronize != MagickFalse)
5332 file=fileno(blob_info->file_info.file);
5333 if ((file == -1) || (offset < 0))
5334 return(MagickFalse);
5335 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5338 offset=SeekBlob(image,offset,SEEK_SET);
5340 return(MagickFalse);
5345 return(MagickFalse);
5347 return(MagickFalse);
5349 return(MagickFalse);
5352 if (extent != (MagickSizeType) ((
size_t) extent))
5353 return(MagickFalse);
5354 if (blob_info->mapped != MagickFalse)
5362 (void) UnmapBlob(blob_info->data,blob_info->length);
5363 RelinquishMagickResource(MapResource,blob_info->length);
5364 if (extent != (MagickSizeType) ((off_t) extent))
5365 return(MagickFalse);
5366 offset=SeekBlob(image,0,SEEK_END);
5368 return(MagickFalse);
5369 if ((MagickSizeType) offset >= extent)
5371 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5372 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5373 blob_info->file_info.file);
5374#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5375 if (blob_info->synchronize != MagickFalse)
5380 file=fileno(blob_info->file_info.file);
5381 if ((file == -1) || (offset < 0))
5382 return(MagickFalse);
5383 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5387 offset=SeekBlob(image,offset,SEEK_SET);
5389 return(MagickFalse);
5390 (void) AcquireMagickResource(MapResource,extent);
5391 blob_info->data=(
unsigned char*) MapBlob(fileno(
5392 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5393 blob_info->extent=(size_t) extent;
5394 blob_info->length=(size_t) extent;
5395 (void) SyncBlob(image);
5398 blob_info->extent=(size_t) extent;
5399 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5400 blob_info->extent+1,
sizeof(*blob_info->data));
5401 (void) SyncBlob(image);
5402 if (blob_info->data == (
unsigned char *) NULL)
5404 (void) DetachBlob(blob_info);
5405 return(MagickFalse);
5439MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5442 assert(custom_stream != (CustomStreamInfo *) NULL);
5443 assert(custom_stream->signature == MagickCoreSignature);
5444 custom_stream->data=data;
5472MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5473 CustomStreamHandler reader)
5475 assert(custom_stream != (CustomStreamInfo *) NULL);
5476 assert(custom_stream->signature == MagickCoreSignature);
5477 custom_stream->reader=reader;
5505MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5506 CustomStreamSeeker seeker)
5508 assert(custom_stream != (CustomStreamInfo *) NULL);
5509 assert(custom_stream->signature == MagickCoreSignature);
5510 custom_stream->seeker=seeker;
5538MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5539 CustomStreamTeller teller)
5541 assert(custom_stream != (CustomStreamInfo *) NULL);
5542 assert(custom_stream->signature == MagickCoreSignature);
5543 custom_stream->teller=teller;
5571MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5572 CustomStreamHandler writer)
5574 assert(custom_stream != (CustomStreamInfo *) NULL);
5575 assert(custom_stream->signature == MagickCoreSignature);
5576 custom_stream->writer=writer;
5603static int SyncBlob(
const Image *image)
5606 *magick_restrict blob_info;
5611 assert(image != (Image *) NULL);
5612 assert(image->signature == MagickCoreSignature);
5613 assert(image->blob != (BlobInfo *) NULL);
5614 if (IsEventLogging() != MagickFalse)
5615 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5616 if (EOFBlob(image) != 0)
5618 blob_info=image->blob;
5620 switch (blob_info->type)
5622 case UndefinedStream:
5624 case StandardStream:
5626 if ((blob_info->mode == WriteBlobMode) ||
5627 (blob_info->mode == WriteBinaryBlobMode) ||
5628 (blob_info->mode == AppendBlobMode) ||
5629 (blob_info->mode == AppendBinaryBlobMode))
5630 status=fflush(blob_info->file_info.file);
5636 status=fflush(blob_info->file_info.file);
5641#if defined(MAGICKCORE_ZLIB_DELEGATE)
5642 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5648#if defined(MAGICKCORE_BZLIB_DELEGATE)
5649 status=BZ2_bzflush(blob_info->file_info.bzfile);
5694MagickPrivate
void SyncBlobStream(ImageInfo *image_info,Image *image,
5697 assert(owner != (Image *) NULL);
5698 assert(image != (Image *) NULL);
5699 assert(image->signature == MagickCoreSignature);
5700 if (image->blob == (BlobInfo *) NULL)
5702 if (image->blob->data == (
unsigned char *) NULL)
5704 if (image_info != (ImageInfo *) NULL)
5706 image_info->blob=(
void *) image->blob->data;
5707 image_info->length=image->blob->length;
5709 if ((image == owner) || (owner->blob == (BlobInfo *) NULL))
5711 if (owner->blob == (BlobInfo *) NULL)
5717 owner->blob->data=image->blob->data;
5718 owner->blob->extent=image->blob->extent;
5719 owner->blob->length=image->blob->length;
5720 owner->blob->offset=image->blob->offset;
5721 owner->blob->quantum=image->blob->quantum;
5722 owner->blob->type=BlobStream;
5723 owner->blob->exempt=MagickTrue;
5724 (void) DetachBlob(image->blob);
5749MagickExport MagickOffsetType TellBlob(
const Image *image)
5752 *magick_restrict blob_info;
5757 assert(image != (Image *) NULL);
5758 assert(image->signature == MagickCoreSignature);
5759 assert(image->blob != (BlobInfo *) NULL);
5760 assert(image->blob->type != UndefinedStream);
5761 if (IsEventLogging() != MagickFalse)
5762 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5763 blob_info=image->blob;
5765 switch (blob_info->type)
5767 case UndefinedStream:
5768 case StandardStream:
5772 offset=ftell(blob_info->file_info.file);
5779#if defined(MAGICKCORE_ZLIB_DELEGATE)
5780 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5790 offset=blob_info->offset;
5795 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5796 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5828MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5830#if defined(MAGICKCORE_HAVE_MMAP)
5834 status=munmap(map,length);
5835 return(status == -1 ? MagickFalse : MagickTrue);
5839 return(MagickFalse);
5871MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5875 *magick_restrict blob_info;
5889 assert(image != (Image *) NULL);
5890 assert(image->signature == MagickCoreSignature);
5891 assert(image->blob != (BlobInfo *) NULL);
5892 assert(image->blob->type != UndefinedStream);
5895 assert(data != (
const void *) NULL);
5896 blob_info=image->blob;
5898 p=(
const unsigned char *) data;
5899 q=(
unsigned char *) data;
5900 switch (blob_info->type)
5902 case UndefinedStream:
5904 case StandardStream:
5912 count=(ssize_t) fwrite((
const char *) data,1,length,
5913 blob_info->file_info.file);
5918 c=putc((
int) *p++,blob_info->file_info.file);
5926 c=putc((
int) *p++,blob_info->file_info.file);
5934 c=putc((
int) *p++,blob_info->file_info.file);
5942 c=putc((
int) *p++,blob_info->file_info.file);
5951 if ((count != (ssize_t) length) &&
5952 (ferror(blob_info->file_info.file) != 0))
5953 ThrowBlobException(blob_info);
5958#if defined(MAGICKCORE_ZLIB_DELEGATE)
5969 for (i=0; i < length; i+=(size_t) count)
5971 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5972 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5985 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5993 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6001 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6009 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6019 (void) gzerror(blob_info->file_info.gzfile,&status);
6020 if ((count != (ssize_t) length) && (status != Z_OK))
6021 ThrowBlobException(blob_info);
6027#if defined(MAGICKCORE_BZLIB_DELEGATE)
6034 for (i=0; i < length; i+=(size_t) count)
6036 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6037 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6047 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6048 if ((count != (ssize_t) length) && (status != BZ_OK))
6049 ThrowBlobException(blob_info);
6055 count=(ssize_t) blob_info->stream(image,data,length);
6063 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6068 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6069 if (extent >= blob_info->extent)
6071 extent+=blob_info->quantum+length;
6072 blob_info->quantum<<=1;
6073 if (SetBlobExtent(image,extent) == MagickFalse)
6076 q=blob_info->data+blob_info->offset;
6077 (void) memcpy(q,p,length);
6078 blob_info->offset+=(MagickOffsetType) length;
6079 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6080 blob_info->length=(size_t) blob_info->offset;
6081 count=(ssize_t) length;
6086 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6087 count=blob_info->custom_stream->writer((
unsigned char *) data,
6088 length,blob_info->custom_stream->data);
6120MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6123 *magick_restrict blob_info;
6128 assert(image != (Image *) NULL);
6129 assert(image->signature == MagickCoreSignature);
6130 assert(image->blob != (BlobInfo *) NULL);
6131 assert(image->blob->type != UndefinedStream);
6132 blob_info=image->blob;
6134 switch (blob_info->type)
6136 case StandardStream:
6143 c=putc((
int) value,blob_info->file_info.file);
6146 if (ferror(blob_info->file_info.file) != 0)
6147 ThrowBlobException(blob_info);
6155 count=WriteBlobStream(image,1,&value);
6187MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6198 quantum.unsigned_value=0U;
6199 quantum.float_value=value;
6200 return(WriteBlobLong(image,quantum.unsigned_value));
6228MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6233 assert(image != (Image *) NULL);
6234 assert(image->signature == MagickCoreSignature);
6235 if (image->endian == LSBEndian)
6237 buffer[0]=(
unsigned char) value;
6238 buffer[1]=(
unsigned char) (value >> 8);
6239 buffer[2]=(
unsigned char) (value >> 16);
6240 buffer[3]=(
unsigned char) (value >> 24);
6241 return(WriteBlobStream(image,4,buffer));
6243 buffer[0]=(
unsigned char) (value >> 24);
6244 buffer[1]=(
unsigned char) (value >> 16);
6245 buffer[2]=(
unsigned char) (value >> 8);
6246 buffer[3]=(
unsigned char) value;
6247 return(WriteBlobStream(image,4,buffer));
6275MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6280 assert(image != (Image *) NULL);
6281 assert(image->signature == MagickCoreSignature);
6282 if (image->endian == LSBEndian)
6284 buffer[0]=(
unsigned char) value;
6285 buffer[1]=(
unsigned char) (value >> 8);
6286 buffer[2]=(
unsigned char) (value >> 16);
6287 buffer[3]=(
unsigned char) (value >> 24);
6288 buffer[4]=(
unsigned char) (value >> 32);
6289 buffer[5]=(
unsigned char) (value >> 40);
6290 buffer[6]=(
unsigned char) (value >> 48);
6291 buffer[7]=(
unsigned char) (value >> 56);
6292 return(WriteBlobStream(image,8,buffer));
6294 buffer[0]=(
unsigned char) (value >> 56);
6295 buffer[1]=(
unsigned char) (value >> 48);
6296 buffer[2]=(
unsigned char) (value >> 40);
6297 buffer[3]=(
unsigned char) (value >> 32);
6298 buffer[4]=(
unsigned char) (value >> 24);
6299 buffer[5]=(
unsigned char) (value >> 16);
6300 buffer[6]=(
unsigned char) (value >> 8);
6301 buffer[7]=(
unsigned char) value;
6302 return(WriteBlobStream(image,8,buffer));
6330MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6335 assert(image != (Image *) NULL);
6336 assert(image->signature == MagickCoreSignature);
6337 if (image->endian == LSBEndian)
6339 buffer[0]=(
unsigned char) value;
6340 buffer[1]=(
unsigned char) (value >> 8);
6341 return(WriteBlobStream(image,2,buffer));
6343 buffer[0]=(
unsigned char) (value >> 8);
6344 buffer[1]=(
unsigned char) value;
6345 return(WriteBlobStream(image,2,buffer));
6373MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6387 assert(image != (Image *) NULL);
6388 assert(image->signature == MagickCoreSignature);
6389 quantum.signed_value=value;
6390 if (image->endian == LSBEndian)
6392 buffer[0]=(
unsigned char) quantum.unsigned_value;
6393 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6394 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6395 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6396 return(WriteBlobStream(image,4,buffer));
6398 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6399 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6400 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6401 buffer[3]=(
unsigned char) quantum.unsigned_value;
6402 return(WriteBlobStream(image,4,buffer));
6430MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6435 assert(image != (Image *) NULL);
6436 assert(image->signature == MagickCoreSignature);
6437 buffer[0]=(
unsigned char) value;
6438 buffer[1]=(
unsigned char) (value >> 8);
6439 buffer[2]=(
unsigned char) (value >> 16);
6440 buffer[3]=(
unsigned char) (value >> 24);
6441 return(WriteBlobStream(image,4,buffer));
6469MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6474 assert(image != (Image *) NULL);
6475 assert(image->signature == MagickCoreSignature);
6476 buffer[0]=(
unsigned char) value;
6477 buffer[1]=(
unsigned char) (value >> 8);
6478 return(WriteBlobStream(image,2,buffer));
6506MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6520 assert(image != (Image *) NULL);
6521 assert(image->signature == MagickCoreSignature);
6522 quantum.signed_value=value;
6523 buffer[0]=(
unsigned char) quantum.unsigned_value;
6524 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6525 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6526 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6527 return(WriteBlobStream(image,4,buffer));
6555MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6556 const signed short value)
6570 assert(image != (Image *) NULL);
6571 assert(image->signature == MagickCoreSignature);
6572 quantum.signed_value=value;
6573 buffer[0]=(
unsigned char) quantum.unsigned_value;
6574 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6575 return(WriteBlobStream(image,2,buffer));
6603MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6608 assert(image != (Image *) NULL);
6609 assert(image->signature == MagickCoreSignature);
6610 buffer[0]=(
unsigned char) (value >> 24);
6611 buffer[1]=(
unsigned char) (value >> 16);
6612 buffer[2]=(
unsigned char) (value >> 8);
6613 buffer[3]=(
unsigned char) value;
6614 return(WriteBlobStream(image,4,buffer));
6642MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6643 const signed short value)
6657 assert(image != (Image *) NULL);
6658 assert(image->signature == MagickCoreSignature);
6659 quantum.signed_value=value;
6660 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6661 buffer[1]=(
unsigned char) quantum.unsigned_value;
6662 return(WriteBlobStream(image,2,buffer));
6690MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6695 assert(image != (Image *) NULL);
6696 assert(image->signature == MagickCoreSignature);
6697 buffer[0]=(
unsigned char) (value >> 8);
6698 buffer[1]=(
unsigned char) value;
6699 return(WriteBlobStream(image,2,buffer));
6727MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6729 assert(image != (Image *) NULL);
6730 assert(image->signature == MagickCoreSignature);
6731 assert(
string != (
const char *) NULL);
6732 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));