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);
904 count=(ssize_t) write(file,(
const char *) blob,(
size_t) count);
906 (void) fclose(blob_info->file);
907 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
908 "%s:%s",blob_info->magick,unique);
909 image=ReadImage(clone_info,exception);
910 if (image != (Image *) NULL)
918 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
920 (void) CopyMagickString(images->filename,image_info->filename,
922 (void) CopyMagickString(images->magick_filename,
923 image_info->filename,MagickPathExtent);
924 (void) CopyMagickString(images->magick,magick_info->name,
926 images=GetNextImageInList(images);
930 clone_info=DestroyImageInfo(clone_info);
931 blob=(
unsigned char *) RelinquishMagickMemory(blob);
932 (void) RelinquishUniqueFileResource(unique);
934 blob_info=DestroyImageInfo(blob_info);
935 if (image != (Image *) NULL)
936 if (CloseBlob(image) == MagickFalse)
937 image=DestroyImageList(image);
963MagickExport
void DestroyBlob(Image *image)
966 *magick_restrict blob_info;
971 assert(image != (Image *) NULL);
972 assert(image->signature == MagickCoreSignature);
973 assert(image->blob != (BlobInfo *) NULL);
974 assert(image->blob->signature == MagickCoreSignature);
975 if (IsEventLogging() != MagickFalse)
976 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
977 blob_info=image->blob;
979 LockSemaphoreInfo(blob_info->semaphore);
980 blob_info->reference_count--;
981 assert(blob_info->reference_count >= 0);
982 if (blob_info->reference_count == 0)
984 UnlockSemaphoreInfo(blob_info->semaphore);
985 if (destroy == MagickFalse)
987 image->blob=(BlobInfo *) NULL;
990 (void) CloseBlob(image);
991 if (blob_info->mapped != MagickFalse)
993 (void) UnmapBlob(blob_info->data,blob_info->length);
994 RelinquishMagickResource(MapResource,blob_info->length);
997 RelinquishSemaphoreInfo(&blob_info->semaphore);
998 blob_info->signature=(~MagickCoreSignature);
999 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
1025MagickExport CustomStreamInfo *DestroyCustomStreamInfo(
1026 CustomStreamInfo *custom_stream)
1028 assert(custom_stream != (CustomStreamInfo *) NULL);
1029 assert(custom_stream->signature == MagickCoreSignature);
1030 if (IsEventLogging() != MagickFalse)
1031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1032 custom_stream->signature=(~MagickCoreSignature);
1033 custom_stream=(CustomStreamInfo *) RelinquishMagickMemory(custom_stream);
1034 return(custom_stream);
1059MagickExport
void *DetachBlob(BlobInfo *blob_info)
1064 assert(blob_info != (BlobInfo *) NULL);
1065 if (IsEventLogging() != MagickFalse)
1066 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1067 if (blob_info->mapped != MagickFalse)
1069 (void) UnmapBlob(blob_info->data,blob_info->length);
1070 blob_info->data=NULL;
1071 RelinquishMagickResource(MapResource,blob_info->length);
1073 blob_info->mapped=MagickFalse;
1074 blob_info->length=0;
1079 blob_info->offset=0;
1080 blob_info->mode=UndefinedBlobMode;
1081 blob_info->eof=MagickFalse;
1083 blob_info->exempt=MagickFalse;
1084 blob_info->type=UndefinedStream;
1085 blob_info->file_info.file=(FILE *) NULL;
1086 data=blob_info->data;
1087 blob_info->data=(
unsigned char *) NULL;
1088 blob_info->stream=(StreamHandler) NULL;
1089 blob_info->custom_stream=(CustomStreamInfo *) NULL;
1118static inline void ResetDisassociatedBlobStorage(BlobInfo *blob_info)
1120 assert(blob_info != (BlobInfo *) NULL);
1121 assert(blob_info->signature == MagickCoreSignature);
1122 if (blob_info->mapped != MagickFalse)
1124 RelinquishMagickResource(MapResource,blob_info->length);
1125 blob_info->mapped=MagickFalse;
1127 blob_info->data=(
unsigned char *) NULL;
1128 blob_info->length=0;
1129 blob_info->offset=0;
1130 blob_info->mode=UndefinedBlobMode;
1131 blob_info->type=UndefinedStream;
1134MagickExport
void DisassociateBlob(Image *image)
1137 *magick_restrict blob_info,
1143 assert(image != (Image *) NULL);
1144 assert(image->signature == MagickCoreSignature);
1145 assert(image->blob != (BlobInfo *) NULL);
1146 assert(image->blob->signature == MagickCoreSignature);
1147 if (IsEventLogging() != MagickFalse)
1148 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1149 blob_info=image->blob;
1151 LockSemaphoreInfo(blob_info->semaphore);
1152 assert(blob_info->reference_count >= 0);
1153 if (blob_info->reference_count > 1)
1155 UnlockSemaphoreInfo(blob_info->semaphore);
1156 if (clone == MagickFalse)
1158 clone_info=CloneBlobInfo(blob_info);
1159 ResetDisassociatedBlobStorage(clone_info);
1161 image->blob=clone_info;
1189MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1190 const MagickSizeType length)
1202 buffer[MagickMinBufferExtent >> 1];
1204 assert(image != (Image *) NULL);
1205 assert(image->signature == MagickCoreSignature);
1206 if (length != (MagickSizeType) ((MagickOffsetType) length))
1207 return(MagickFalse);
1209 for (i=0; i < length; i+=(MagickSizeType) count)
1211 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1212 (void) ReadBlobStream(image,quantum,buffer,&count);
1222 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1249MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
1251 assert(image != (Image *) NULL);
1252 assert(image->signature == MagickCoreSignature);
1253 assert(duplicate != (Image *) NULL);
1254 assert(duplicate->signature == MagickCoreSignature);
1255 if (IsEventLogging() != MagickFalse)
1256 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1258 image->blob=ReferenceBlob(duplicate->blob);
1284MagickExport
int EOFBlob(
const Image *image)
1287 *magick_restrict blob_info;
1289 assert(image != (Image *) NULL);
1290 assert(image->signature == MagickCoreSignature);
1291 assert(image->blob != (BlobInfo *) NULL);
1292 assert(image->blob->type != UndefinedStream);
1293 if (IsEventLogging() != MagickFalse)
1294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1295 blob_info=image->blob;
1296 switch (blob_info->type)
1298 case UndefinedStream:
1299 case StandardStream:
1304 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1310#if defined(MAGICKCORE_ZLIB_DELEGATE)
1311 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1318#if defined(MAGICKCORE_BZLIB_DELEGATE)
1323 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1324 if (status == BZ_UNEXPECTED_EOF)
1325 blob_info->eof=MagickTrue;
1331 blob_info->eof=MagickFalse;
1339 return((
int) blob_info->eof);
1365MagickExport
int ErrorBlob(
const Image *image)
1368 *magick_restrict blob_info;
1370 assert(image != (Image *) NULL);
1371 assert(image->signature == MagickCoreSignature);
1372 assert(image->blob != (BlobInfo *) NULL);
1373 assert(image->blob->type != UndefinedStream);
1374 if (IsEventLogging() != MagickFalse)
1375 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1376 blob_info=image->blob;
1377 switch (blob_info->type)
1379 case UndefinedStream:
1380 case StandardStream:
1385 blob_info->error=ferror(blob_info->file_info.file);
1390#if defined(MAGICKCORE_ZLIB_DELEGATE)
1391 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1397#if defined(MAGICKCORE_BZLIB_DELEGATE)
1398 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1412 return(blob_info->error);
1450MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1451 size_t *length,ExceptionInfo *exception)
1477 assert(filename != (
const char *) NULL);
1478 assert(exception != (ExceptionInfo *) NULL);
1479 assert(exception->signature == MagickCoreSignature);
1480 if (IsEventLogging() != MagickFalse)
1481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1483 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1484 ThrowPolicyException(filename,NULL);
1486 if (LocaleCompare(filename,
"-") != 0)
1489 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1491 status=GetPathAttributes(filename,&attributes);
1492 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1494 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1497 file=open_utf8(filename,flags,0);
1501 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1504 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1506 file=close_utf8(file)-1;
1507 ThrowPolicyException(filename,NULL);
1509 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1511 if ((file == fileno(stdin)) || (offset < 0) ||
1512 (offset != (MagickOffsetType) ((ssize_t) offset)))
1523 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1524 quantum=(size_t) MagickMaxBufferExtent;
1525 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1526 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1527 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1528 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1530 count=MagickRead(file,blob+i,quantum);
1533 if (~i < ((
size_t) count+quantum+1))
1535 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1538 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1539 quantum+1,
sizeof(*blob));
1540 if ((i+(
size_t) count) >= extent)
1543 if (LocaleCompare(filename,
"-") != 0)
1544 file=close_utf8(file);
1545 if (blob == (
unsigned char *) NULL)
1547 (void) ThrowMagickException(exception,GetMagickModule(),
1548 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1551 if ((count < 0) || (file == -1))
1553 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1554 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1557 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1561 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1562 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1563 blob=(
unsigned char *) NULL;
1564 if (~(*length) >= (MagickPathExtent-1))
1565 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1567 if (blob == (
unsigned char *) NULL)
1569 file=close_utf8(file);
1570 (void) ThrowMagickException(exception,GetMagickModule(),
1571 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1574 map=MapBlob(file,ReadMode,0,*length);
1575 if (map != (
unsigned char *) NULL)
1577 (void) memcpy(blob,map,*length);
1578 (void) UnmapBlob(map,*length);
1582 (void) lseek(file,0,SEEK_SET);
1583 for (i=0; i < *length; i+=(size_t) count)
1585 count=MagickRead(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1586 MagickMaxBufferExtent));
1592 file=close_utf8(file)-1;
1593 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1594 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1599 if (LocaleCompare(filename,
"-") != 0)
1600 file=close_utf8(file);
1603 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1604 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1634static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1635 const void *magick_restrict data)
1638 *magick_restrict blob_info;
1646 assert(image->blob != (BlobInfo *) NULL);
1647 assert(image->blob->type != UndefinedStream);
1648 assert(data != NULL);
1649 blob_info=image->blob;
1650 if (blob_info->type != BlobStream)
1651 return(WriteBlob(image,length,(
const unsigned char *) data));
1652 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1657 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1658 if (extent >= blob_info->extent)
1660 extent+=blob_info->quantum+length;
1661 blob_info->quantum<<=1;
1662 if (SetBlobExtent(image,extent) == MagickFalse)
1665 q=blob_info->data+blob_info->offset;
1666 (void) memcpy(q,data,length);
1667 blob_info->offset+=(MagickOffsetType) length;
1668 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1669 blob_info->length=(size_t) blob_info->offset;
1670 return((ssize_t) length);
1673MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1674 ExceptionInfo *exception)
1692 assert(image != (
const Image *) NULL);
1693 assert(image->signature == MagickCoreSignature);
1694 assert(filename != (
const char *) NULL);
1695 if (IsEventLogging() != MagickFalse)
1696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1697 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1698 ThrowPolicyException(filename,MagickFalse);
1700 if (LocaleCompare(filename,
"-") != 0)
1703 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1705 file=open_utf8(filename,flags,0);
1709 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1710 return(MagickFalse);
1712 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1713 ThrowPolicyException(filename,MagickFalse);
1714 quantum=(size_t) MagickMaxBufferExtent;
1715 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1716 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1717 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1718 if (blob == (
unsigned char *) NULL)
1720 file=close_utf8(file);
1721 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1723 return(MagickFalse);
1727 count=MagickRead(file,blob,quantum);
1730 length=(size_t) count;
1731 count=WriteBlobStream(image,length,blob);
1732 if (count != (ssize_t) length)
1734 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1738 file=close_utf8(file);
1740 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1741 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1768MagickExport MagickBooleanType GetBlobError(
const Image *image)
1770 assert(image != (
const Image *) NULL);
1771 assert(image->signature == MagickCoreSignature);
1772 if (IsEventLogging() != MagickFalse)
1773 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1774 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1775 errno=image->blob->error_number;
1776 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1802MagickExport FILE *GetBlobFileHandle(
const Image *image)
1804 assert(image != (
const Image *) NULL);
1805 assert(image->signature == MagickCoreSignature);
1806 assert(image->blob != (BlobInfo *) NULL);
1807 assert(image->blob->signature == MagickCoreSignature);
1808 switch (image->blob->type)
1810 case StandardStream:
1813 return(image->blob->file_info.file);
1814 case UndefinedStream:
1822 return((FILE *) NULL);
1847MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1849 assert(blob_info != (BlobInfo *) NULL);
1850 (void) memset(blob_info,0,
sizeof(*blob_info));
1851 blob_info->type=UndefinedStream;
1852 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1853 blob_info->properties.st_mtime=GetMagickTime();
1854 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1855 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1856 blob_info->reference_count=1;
1857 blob_info->semaphore=AcquireSemaphoreInfo();
1858 blob_info->signature=MagickCoreSignature;
1883MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1885 assert(image != (Image *) NULL);
1886 assert(image->signature == MagickCoreSignature);
1887 if (IsEventLogging() != MagickFalse)
1888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1889 return(&image->blob->properties);
1915MagickExport MagickSizeType GetBlobSize(
const Image *image)
1918 *magick_restrict blob_info;
1923 assert(image != (Image *) NULL);
1924 assert(image->signature == MagickCoreSignature);
1925 assert(image->blob != (BlobInfo *) NULL);
1926 if (IsEventLogging() != MagickFalse)
1927 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1928 blob_info=image->blob;
1930 switch (blob_info->type)
1932 case UndefinedStream:
1933 case StandardStream:
1935 extent=blob_info->size;
1943 extent=(MagickSizeType) blob_info->properties.st_size;
1945 extent=blob_info->size;
1946 file_descriptor=fileno(blob_info->file_info.file);
1947 if (file_descriptor == -1)
1949 if (fstat(file_descriptor,&blob_info->properties) == 0)
1950 extent=(MagickSizeType) blob_info->properties.st_size;
1955 extent=blob_info->size;
1964 status=GetPathAttributes(image->filename,&blob_info->properties);
1965 if (status != MagickFalse)
1966 extent=(MagickSizeType) blob_info->properties.st_size;
1973 extent=(MagickSizeType) blob_info->length;
1978 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1979 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1984 offset=blob_info->custom_stream->teller(
1985 blob_info->custom_stream->data);
1986 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1987 blob_info->custom_stream->data);
1988 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1989 blob_info->custom_stream->data);
2019MagickExport
void *GetBlobStreamData(
const Image *image)
2021 assert(image != (
const Image *) NULL);
2022 assert(image->signature == MagickCoreSignature);
2023 return(image->blob->data);
2048MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2050 assert(image != (
const Image *) NULL);
2051 assert(image->signature == MagickCoreSignature);
2052 if (IsEventLogging() != MagickFalse)
2053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2054 return(image->blob->stream);
2090MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2091 Image *image,
size_t *length,ExceptionInfo *exception)
2105 assert(image_info != (
const ImageInfo *) NULL);
2106 assert(image_info->signature == MagickCoreSignature);
2107 assert(image != (Image *) NULL);
2108 assert(image->signature == MagickCoreSignature);
2109 assert(exception != (ExceptionInfo *) NULL);
2110 assert(exception->signature == MagickCoreSignature);
2111 if (IsEventLogging() != MagickFalse)
2112 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2113 image_info->filename);
2115 blob=(
unsigned char *) NULL;
2116 blob_info=CloneImageInfo(image_info);
2117 blob_info->adjoin=MagickFalse;
2118 (void) SetImageInfo(blob_info,1,exception);
2119 if (*blob_info->magick !=
'\0')
2120 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2121 magick_info=GetMagickInfo(image->magick,exception);
2122 if (magick_info == (
const MagickInfo *) NULL)
2124 (void) ThrowMagickException(exception,GetMagickModule(),
2125 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2127 blob_info=DestroyImageInfo(blob_info);
2130 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2131 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2136 blob_info->length=0;
2137 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2138 sizeof(
unsigned char));
2139 if (blob_info->blob == NULL)
2140 (void) ThrowMagickException(exception,GetMagickModule(),
2141 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2144 (void) CloseBlob(image);
2145 image->blob->exempt=MagickTrue;
2146 image->blob->extent=0;
2147 *image->filename=
'\0';
2148 status=WriteImage(blob_info,image,exception);
2149 SyncBlobStream(blob_info,image,image);
2150 *length=image->blob->length;
2151 blob=DetachBlob(image->blob);
2152 if (blob != (
void *) NULL)
2154 if (status == MagickFalse)
2155 blob=RelinquishMagickMemory(blob);
2157 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2160 if ((status == MagickFalse) && (image->blob->extent == 0))
2161 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2167 unique[MagickPathExtent];
2175 file=AcquireUniqueFileResource(unique);
2178 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2179 image_info->filename);
2183 blob_info->file=fdopen(file,
"wb");
2184 if (blob_info->file != (FILE *) NULL)
2186 (void) FormatLocaleString(image->filename,MagickPathExtent,
2187 "%s:%s",image->magick,unique);
2188 status=WriteImage(blob_info,image,exception);
2189 (void) fclose(blob_info->file);
2190 if (status != MagickFalse)
2191 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2193 (void) RelinquishUniqueFileResource(unique);
2196 blob_info=DestroyImageInfo(blob_info);
2228MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2229 ExceptionInfo *exception)
2241 assert(image_info != (
const ImageInfo *) NULL);
2242 assert(image_info->signature == MagickCoreSignature);
2243 assert(image != (Image *) NULL);
2244 assert(image->signature == MagickCoreSignature);
2245 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2246 assert(image_info->custom_stream->signature == MagickCoreSignature);
2247 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2248 assert(exception != (ExceptionInfo *) NULL);
2249 if (IsEventLogging() != MagickFalse)
2250 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2251 image_info->filename);
2252 clone_info=CloneImageInfo(image_info);
2253 clone_info->adjoin=MagickFalse;
2254 (void) SetImageInfo(clone_info,1,exception);
2255 if (*clone_info->magick !=
'\0')
2256 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2257 magick_info=GetMagickInfo(image->magick,exception);
2258 if (magick_info == (
const MagickInfo *) NULL)
2260 (void) ThrowMagickException(exception,GetMagickModule(),
2261 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2263 clone_info=DestroyImageInfo(clone_info);
2266 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2267 blob_support=GetMagickBlobSupport(magick_info);
2268 if ((blob_support != MagickFalse) &&
2269 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2271 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2272 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2273 blob_support=MagickFalse;
2275 if (blob_support != MagickFalse)
2280 (void) CloseBlob(image);
2281 *image->filename=
'\0';
2282 (void) WriteImage(clone_info,image,exception);
2287 unique[MagickPathExtent];
2298 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2299 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2301 if (blob == (
unsigned char *) NULL)
2303 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2304 image_info->filename);
2305 clone_info=DestroyImageInfo(clone_info);
2308 file=AcquireUniqueFileResource(unique);
2311 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2312 image_info->filename);
2313 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2314 clone_info=DestroyImageInfo(clone_info);
2317 clone_info->file=fdopen(file,
"wb+");
2318 if (clone_info->file != (FILE *) NULL)
2323 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2324 image->magick,unique);
2325 status=WriteImage(clone_info,image,exception);
2326 if (status != MagickFalse)
2328 (void) fseek(clone_info->file,0,SEEK_SET);
2329 count=(ssize_t) MagickMaxBufferExtent;
2330 while (count == (ssize_t) MagickMaxBufferExtent)
2332 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2334 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2335 image_info->custom_stream->data);
2338 (void) fclose(clone_info->file);
2340 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2341 (void) RelinquishUniqueFileResource(unique);
2343 clone_info=DestroyImageInfo(clone_info);
2374MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2375 ExceptionInfo *exception)
2399 assert(image != (Image *) NULL);
2400 assert(image->signature == MagickCoreSignature);
2401 assert(image->blob != (BlobInfo *) NULL);
2402 assert(image->blob->type != UndefinedStream);
2403 assert(filename != (
const char *) NULL);
2404 if (IsEventLogging() != MagickFalse)
2405 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2406 if (*filename ==
'\0')
2407 file=AcquireUniqueFileResource(filename);
2409 if (LocaleCompare(filename,
"-") == 0)
2410 file=fileno(stdout);
2412 file=open_utf8(filename,O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2415 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2416 return(MagickFalse);
2418 quantum=(size_t) MagickMaxBufferExtent;
2419 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2420 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2421 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2422 if (buffer == (
unsigned char *) NULL)
2424 file=close_utf8(file)-1;
2425 (void) ThrowMagickException(exception,GetMagickModule(),
2426 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2427 return(MagickFalse);
2430 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2431 for (i=0; count > 0; )
2433 length=(size_t) count;
2434 for (i=0; i < length; i+=(size_t) count)
2436 count=MagickWrite(file,p+i,(
size_t) (length-i));
2442 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2444 if (LocaleCompare(filename,
"-") != 0)
2445 file=close_utf8(file);
2446 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2447 if ((file == -1) || (i < length))
2450 file=close_utf8(file);
2451 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2452 return(MagickFalse);
2492MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2493 size_t *length,ExceptionInfo *exception)
2507 assert(image_info != (
const ImageInfo *) NULL);
2508 assert(image_info->signature == MagickCoreSignature);
2509 assert(images != (Image *) NULL);
2510 assert(images->signature == MagickCoreSignature);
2511 assert(exception != (ExceptionInfo *) NULL);
2512 if (IsEventLogging() != MagickFalse)
2513 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2514 image_info->filename);
2516 blob=(
unsigned char *) NULL;
2517 blob_info=CloneImageInfo(image_info);
2518 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2520 if (*blob_info->magick !=
'\0')
2521 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2522 magick_info=GetMagickInfo(images->magick,exception);
2523 if (magick_info == (
const MagickInfo *) NULL)
2525 (void) ThrowMagickException(exception,GetMagickModule(),
2526 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2528 blob_info=DestroyImageInfo(blob_info);
2531 if (GetMagickAdjoin(magick_info) == MagickFalse)
2533 blob_info=DestroyImageInfo(blob_info);
2534 return(ImageToBlob(image_info,images,length,exception));
2536 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2537 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2542 blob_info->length=0;
2543 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2544 sizeof(
unsigned char));
2545 if (blob_info->blob == (
void *) NULL)
2546 (void) ThrowMagickException(exception,GetMagickModule(),
2547 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2550 (void) CloseBlob(images);
2551 images->blob->exempt=MagickTrue;
2552 images->blob->extent=0;
2553 *images->filename=
'\0';
2554 status=WriteImages(blob_info,images,images->filename,exception);
2555 SyncBlobStream(blob_info,images,images);
2556 *length=images->blob->length;
2557 blob=DetachBlob(images->blob);
2558 if (blob != (
void *) NULL)
2560 if (status == MagickFalse)
2561 blob=RelinquishMagickMemory(blob);
2563 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2565 else if ((status == MagickFalse) && (images->blob->extent == 0))
2566 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2572 filename[MagickPathExtent],
2573 unique[MagickPathExtent];
2581 file=AcquireUniqueFileResource(unique);
2584 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2585 image_info->filename);
2589 blob_info->file=fdopen(file,
"wb");
2590 if (blob_info->file != (FILE *) NULL)
2592 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2593 images->magick,unique);
2594 status=WriteImages(blob_info,images,filename,exception);
2595 (void) fclose(blob_info->file);
2596 if (status != MagickFalse)
2597 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2599 (void) RelinquishUniqueFileResource(unique);
2602 blob_info=DestroyImageInfo(blob_info);
2634MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2635 Image *images,ExceptionInfo *exception)
2647 assert(image_info != (
const ImageInfo *) NULL);
2648 assert(image_info->signature == MagickCoreSignature);
2649 assert(images != (Image *) NULL);
2650 assert(images->signature == MagickCoreSignature);
2651 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2652 assert(image_info->custom_stream->signature == MagickCoreSignature);
2653 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2654 assert(exception != (ExceptionInfo *) NULL);
2655 if (IsEventLogging() != MagickFalse)
2656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2657 image_info->filename);
2658 clone_info=CloneImageInfo(image_info);
2659 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2661 if (*clone_info->magick !=
'\0')
2662 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2663 magick_info=GetMagickInfo(images->magick,exception);
2664 if (magick_info == (
const MagickInfo *) NULL)
2666 (void) ThrowMagickException(exception,GetMagickModule(),
2667 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2669 clone_info=DestroyImageInfo(clone_info);
2672 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2673 blob_support=GetMagickBlobSupport(magick_info);
2674 if ((blob_support != MagickFalse) &&
2675 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2677 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2678 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2679 blob_support=MagickFalse;
2681 if (blob_support != MagickFalse)
2686 (void) CloseBlob(images);
2687 *images->filename=
'\0';
2688 (void) WriteImages(clone_info,images,images->filename,exception);
2693 filename[MagickPathExtent],
2694 unique[MagickPathExtent];
2705 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2706 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2708 if (blob == (
unsigned char *) NULL)
2710 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2711 image_info->filename);
2712 clone_info=DestroyImageInfo(clone_info);
2715 file=AcquireUniqueFileResource(unique);
2718 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2719 image_info->filename);
2720 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2721 clone_info=DestroyImageInfo(clone_info);
2724 clone_info->file=fdopen(file,
"wb+");
2725 if (clone_info->file != (FILE *) NULL)
2730 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2731 images->magick,unique);
2732 status=WriteImages(clone_info,images,filename,exception);
2733 if (status != MagickFalse)
2735 (void) fseek(clone_info->file,0,SEEK_SET);
2736 count=(ssize_t) MagickMaxBufferExtent;
2737 while (count == (ssize_t) MagickMaxBufferExtent)
2739 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2741 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2742 image_info->custom_stream->data);
2745 (void) fclose(clone_info->file);
2747 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2748 (void) RelinquishUniqueFileResource(unique);
2750 clone_info=DestroyImageInfo(clone_info);
2786MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2787 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2790 filename[MagickPathExtent];
2819 assert(image_info != (ImageInfo *) NULL);
2820 assert(image_info->signature == MagickCoreSignature);
2821 assert(image != (Image *) NULL);
2822 assert(image->signature == MagickCoreSignature);
2823 assert(inject_image != (Image *) NULL);
2824 assert(inject_image->signature == MagickCoreSignature);
2825 assert(exception != (ExceptionInfo *) NULL);
2826 if (IsEventLogging() != MagickFalse)
2827 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2828 unique_file=(FILE *) NULL;
2829 file=AcquireUniqueFileResource(filename);
2831 unique_file=fdopen(file,
"wb");
2832 if ((file == -1) || (unique_file == (FILE *) NULL))
2834 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2835 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2837 return(MagickFalse);
2839 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2840 if (byte_image == (Image *) NULL)
2842 (void) fclose(unique_file);
2843 (void) RelinquishUniqueFileResource(filename);
2844 return(MagickFalse);
2846 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2848 DestroyBlob(byte_image);
2849 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2850 write_info=CloneImageInfo(image_info);
2851 SetImageInfoFile(write_info,unique_file);
2852 status=WriteImage(write_info,byte_image,exception);
2853 write_info=DestroyImageInfo(write_info);
2854 byte_image=DestroyImage(byte_image);
2855 (void) fclose(unique_file);
2856 if (status == MagickFalse)
2858 (void) RelinquishUniqueFileResource(filename);
2859 return(MagickFalse);
2864 file=open_utf8(filename,O_RDONLY | O_CLOEXEC | O_BINARY,0);
2867 (void) RelinquishUniqueFileResource(filename);
2868 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2869 image_info->filename);
2870 return(MagickFalse);
2872 quantum=(size_t) MagickMaxBufferExtent;
2873 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2874 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2875 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2876 if (buffer == (
unsigned char *) NULL)
2878 (void) RelinquishUniqueFileResource(filename);
2879 file=close_utf8(file);
2880 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2885 ssize_t count = read(file,buffer,quantum);
2892 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2895 file=close_utf8(file);
2897 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2898 (void) RelinquishUniqueFileResource(filename);
2899 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2925MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2927 assert(image != (
const Image *) NULL);
2928 assert(image->signature == MagickCoreSignature);
2929 if (IsEventLogging() != MagickFalse)
2930 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2931 return(image->blob->exempt);
2956MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2959 *magick_restrict blob_info;
2961 assert(image != (
const Image *) NULL);
2962 assert(image->signature == MagickCoreSignature);
2963 if (IsEventLogging() != MagickFalse)
2964 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2965 blob_info=image->blob;
2966 switch (blob_info->type)
2975 if (blob_info->file_info.file == (FILE *) NULL)
2976 return(MagickFalse);
2977 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2978 return(status == -1 ? MagickFalse : MagickTrue);
2982#if defined(MAGICKCORE_ZLIB_DELEGATE)
2986 if (blob_info->file_info.gzfile == (gzFile) NULL)
2987 return(MagickFalse);
2988 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2989 return(offset < 0 ? MagickFalse : MagickTrue);
2994 case UndefinedStream:
2998 case StandardStream:
3002 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3003 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3010 return(MagickFalse);
3035MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3037 assert(image != (
const Image *) NULL);
3038 assert(image->signature == MagickCoreSignature);
3039 if (IsEventLogging() != MagickFalse)
3040 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3041 return(image->blob->temporary);
3073MagickExport
void *MapBlob(
int file,
const MapMode mode,
3074 const MagickOffsetType offset,
const size_t length)
3076#if defined(MAGICKCORE_HAVE_MMAP)
3089#if defined(MAP_ANONYMOUS)
3090 flags|=MAP_ANONYMOUS;
3099 protection=PROT_READ;
3105 protection=PROT_WRITE;
3111 protection=PROT_READ | PROT_WRITE;
3116#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3117 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3119 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3120 if (map == MAP_FAILED)
3121 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3123 if (map == MAP_FAILED)
3160MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3169 assert(buffer != (
unsigned char *) NULL);
3176 *buffer++=(
unsigned char) c;
3180 *buffer++=(
unsigned char) c;
3210MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3218 assert(p != (
unsigned char *) NULL);
3225 *p++=(
unsigned char) c;
3261static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3262 const BlobInfo *blob_info)
3273 size=MagickMinBufferExtent;
3274 option=GetImageOption(image_info,
"stream:buffer-size");
3275 if (option != (
const char *) NULL)
3276 size=StringToUnsignedLong(option);
3277 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3278 _IONBF : _IOFBF,size);
3279 return(status == 0 ? MagickTrue : MagickFalse);
3282#if defined(MAGICKCORE_ZLIB_DELEGATE)
3283static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3285#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3286 return(gzopen(path,mode));
3294 path_wide=NTCreateWidePath(path);
3295 if (path_wide == (
wchar_t *) NULL)
3296 return((gzFile) NULL);
3297 file=gzopen_w(path_wide,mode);
3298 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3304MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3305 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3308 *magick_restrict blob_info;
3311 extension[MagickPathExtent],
3312 filename[MagickPathExtent];
3318 flags = O_RDONLY | O_CLOEXEC;
3326 assert(image_info != (ImageInfo *) NULL);
3327 assert(image_info->signature == MagickCoreSignature);
3328 assert(image != (Image *) NULL);
3329 assert(image->signature == MagickCoreSignature);
3330 if (IsEventLogging() != MagickFalse)
3331 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3332 image_info->filename);
3333 blob_info=image->blob;
3334 if (image_info->blob != (
void *) NULL)
3336 if (image_info->stream != (StreamHandler) NULL)
3337 blob_info->stream=(StreamHandler) image_info->stream;
3338 AttachBlob(blob_info,image_info->blob,image_info->length);
3341 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3342 (*image->filename ==
'\0'))
3344 blob_info->type=CustomStream;
3345 blob_info->custom_stream=image_info->custom_stream;
3346 if (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL)
3347 blob_info->custom_stream->seeker(0,SEEK_SET,
3348 blob_info->custom_stream->data);
3351 (void) DetachBlob(blob_info);
3352 blob_info->mode=mode;
3357 flags=O_RDONLY | O_CLOEXEC;
3361 case ReadBinaryBlobMode:
3363 flags=O_RDONLY | O_CLOEXEC | O_BINARY;
3369 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC;
3373 case WriteBinaryBlobMode:
3375 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_TRUNC | O_BINARY;
3379 case AppendBlobMode:
3381 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_APPEND;
3385 case AppendBinaryBlobMode:
3387 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_APPEND | O_BINARY;
3393 flags=O_RDONLY | O_CLOEXEC;
3399 blob_info->synchronize=image_info->synchronize;
3400 if (image_info->stream != (StreamHandler) NULL)
3402 blob_info->stream=image_info->stream;
3405 blob_info->type=FifoStream;
3413 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3414 rights=ReadPolicyRights;
3416 rights=WritePolicyRights;
3417 if (IsPathAuthorized(rights,filename) == MagickFalse)
3418 ThrowPolicyException(filename,MagickFalse);
3419 if ((LocaleCompare(filename,
"-") == 0) ||
3420 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3422 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
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=StandardStream;
3428 blob_info->exempt=MagickTrue;
3429 return(SetStreamBuffering(image_info,blob_info));
3431 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3432 (IsGeometry(filename+3) != MagickFalse))
3439 blob_info->file_info.file=fdopen(StringToInteger(filename+3),fileMode);
3440 if (blob_info->file_info.file == (FILE *) NULL)
3442 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3443 return(MagickFalse);
3445#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3446 if (strchr(type,
'b') != (
char *) NULL)
3447 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3449 blob_info->type=FileStream;
3450 blob_info->exempt=MagickTrue;
3451 return(SetStreamBuffering(image_info,blob_info));
3453#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3454 if (*filename ==
'|')
3457 fileMode[MagickPathExtent],
3465 (void) signal(SIGPIPE,SIG_IGN);
3469 sanitize_command=SanitizeString(filename+1);
3470 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3471 sanitize_command=DestroyString(sanitize_command);
3472 if (blob_info->file_info.file == (FILE *) NULL)
3474 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3475 return(MagickFalse);
3477 blob_info->type=PipeStream;
3478 blob_info->exempt=MagickTrue;
3479 return(SetStreamBuffering(image_info,blob_info));
3482 status=GetPathAttributes(filename,&blob_info->properties);
3483#if defined(S_ISFIFO)
3484 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3486 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3487 if (blob_info->file_info.file == (FILE *) NULL)
3489 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3490 return(MagickFalse);
3492 blob_info->type=FileStream;
3493 blob_info->exempt=MagickTrue;
3494 return(SetStreamBuffering(image_info,blob_info));
3497 GetPathComponent(image->filename,ExtensionPath,extension);
3500 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3501 if ((image_info->adjoin == MagickFalse) ||
3502 (strchr(filename,
'%') != (
char *) NULL))
3507 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3508 image->scene,filename,exception);
3509 if ((LocaleCompare(filename,image->filename) == 0) &&
3510 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3511 (GetNextImageInList(image) != (Image *) NULL)))
3514 path[MagickPathExtent];
3516 GetPathComponent(image->filename,RootPath,path);
3517 if (*extension ==
'\0')
3518 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.17g",
3519 path,(
double) image->scene);
3521 (
void) FormatLocaleString(filename,MagickPathExtent,
3522 "%s-%.17g.%s",path,(
double) image->scene,extension);
3524 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3526 if (IsPathAuthorized(rights,filename) == MagickFalse)
3527 ThrowPolicyException(filename,MagickFalse);
3529 if (image_info->file != (FILE *) NULL)
3531 blob_info->file_info.file=image_info->file;
3532 blob_info->type=FileStream;
3533 blob_info->exempt=MagickTrue;
3541 blob_info->file_info.file=(FILE *) NULL;
3542 file=open_utf8(filename,flags,0);
3544 blob_info->file_info.file=fdopen(file,type);
3545 if (blob_info->file_info.file != (FILE *) NULL)
3553 blob_info->type=FileStream;
3554 (void) SetStreamBuffering(image_info,blob_info);
3555 (void) memset(magick,0,
sizeof(magick));
3556 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3557 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3558#if defined(MAGICKCORE_POSIX_SUPPORT)
3559 (void) fflush(blob_info->file_info.file);
3561 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3562 " read %.17g magic header bytes",(
double) count);
3563#if defined(MAGICKCORE_ZLIB_DELEGATE)
3564 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3565 ((
int) magick[2] == 0x08))
3568 gzfile = gzopen_utf8(filename,
"rb");
3570 if (gzfile != (gzFile) NULL)
3572 if (blob_info->file_info.file != (FILE *) NULL)
3573 (void) fclose(blob_info->file_info.file);
3574 blob_info->file_info.file=(FILE *) NULL;
3575 blob_info->file_info.gzfile=gzfile;
3576 blob_info->type=ZipStream;
3580#if defined(MAGICKCORE_BZLIB_DELEGATE)
3581 if (strncmp((
char *) magick,
"BZh",3) == 0)
3584 *bzfile = BZ2_bzopen(filename,
"r");
3586 if (bzfile != (BZFILE *) NULL)
3588 if (blob_info->file_info.file != (FILE *) NULL)
3589 (void) fclose(blob_info->file_info.file);
3590 blob_info->file_info.file=(FILE *) NULL;
3591 blob_info->file_info.bzfile=bzfile;
3592 blob_info->type=BZipStream;
3596 if (blob_info->type == FileStream)
3607 sans_exception=AcquireExceptionInfo();
3608 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3609 sans_exception=DestroyExceptionInfo(sans_exception);
3610 length=(size_t) blob_info->properties.st_size;
3611 if ((magick_info != (
const MagickInfo *) NULL) &&
3612 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3613 (AcquireMagickResource(MapResource,length) != MagickFalse))
3618 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3620 if (blob == (
void *) NULL)
3621 RelinquishMagickResource(MapResource,length);
3627 if (image_info->file != (FILE *) NULL)
3628 blob_info->exempt=MagickFalse;
3631 (void) fclose(blob_info->file_info.file);
3632 blob_info->file_info.file=(FILE *) NULL;
3634 AttachBlob(blob_info,blob,length);
3635 blob_info->mapped=MagickTrue;
3642#if defined(MAGICKCORE_ZLIB_DELEGATE)
3643 if ((LocaleCompare(extension,
"gz") == 0) ||
3644 (LocaleCompare(extension,
"wmz") == 0) ||
3645 (LocaleCompare(extension,
"svgz") == 0))
3647 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3648 if (blob_info->file_info.gzfile != (gzFile) NULL)
3649 blob_info->type=ZipStream;
3653#if defined(MAGICKCORE_BZLIB_DELEGATE)
3654 if (LocaleCompare(extension,
"bz2") == 0)
3656 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3657 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3658 blob_info->type=BZipStream;
3666 blob_info->file_info.file=(FILE *) NULL;
3667 file=open_utf8(filename,flags,P_MODE);
3669 blob_info->file_info.file=fdopen(file,type);
3670 if (blob_info->file_info.file != (FILE *) NULL)
3672 blob_info->type=FileStream;
3673 (void) SetStreamBuffering(image_info,blob_info);
3676 if (IsPathAuthorized(rights,filename) == MagickFalse)
3677 ThrowPolicyException(filename,MagickFalse);
3678 blob_info->status=0;
3679 blob_info->error_number=0;
3680 if (blob_info->type != UndefinedStream)
3681 blob_info->size=GetBlobSize(image);
3684 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3685 return(MagickFalse);
3724#if defined(__cplusplus) || defined(c_plusplus)
3728static size_t PingStream(
const Image *magick_unused(image),
3729 const void *magick_unused(pixels),
const size_t columns)
3731 magick_unreferenced(image);
3732 magick_unreferenced(pixels);
3736#if defined(__cplusplus) || defined(c_plusplus)
3740MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3741 const size_t length,ExceptionInfo *exception)
3756 assert(image_info != (ImageInfo *) NULL);
3757 assert(image_info->signature == MagickCoreSignature);
3758 assert(exception != (ExceptionInfo *) NULL);
3759 if (IsEventLogging() != MagickFalse)
3760 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3761 image_info->filename);
3762 if ((blob == (
const void *) NULL) || (length == 0))
3764 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3765 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3766 return((Image *) NULL);
3768 ping_info=CloneImageInfo(image_info);
3769 ping_info->blob=(
void *) blob;
3770 ping_info->length=length;
3771 ping_info->ping=MagickTrue;
3772 if (*ping_info->magick ==
'\0')
3773 (void) SetImageInfo(ping_info,0,exception);
3774 magick_info=GetMagickInfo(ping_info->magick,exception);
3775 if (magick_info == (
const MagickInfo *) NULL)
3777 (void) ThrowMagickException(exception,GetMagickModule(),
3778 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3780 ping_info=DestroyImageInfo(ping_info);
3781 return((Image *) NULL);
3783 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3786 filename[MagickPathExtent];
3791 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3792 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3793 ping_info->magick,filename);
3794 image=ReadStream(ping_info,&PingStream,exception);
3795 if (image != (Image *) NULL)
3796 (void) DetachBlob(image->blob);
3797 ping_info=DestroyImageInfo(ping_info);
3803 ping_info->blob=(
void *) NULL;
3804 ping_info->length=0;
3805 *ping_info->filename=
'\0';
3806 status=BlobToFile(ping_info->filename,blob,length,exception);
3807 if (status == MagickFalse)
3809 (void) RelinquishUniqueFileResource(ping_info->filename);
3810 ping_info=DestroyImageInfo(ping_info);
3811 return((Image *) NULL);
3813 clone_info=CloneImageInfo(ping_info);
3814 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3815 ping_info->magick,ping_info->filename);
3816 image=ReadStream(clone_info,&PingStream,exception);
3817 if (image != (Image *) NULL)
3825 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3827 (void) CopyMagickString(images->filename,image_info->filename,
3829 (void) CopyMagickString(images->magick_filename,image_info->filename,
3831 (void) CopyMagickString(images->magick,magick_info->name,
3833 images=GetNextImageInList(images);
3836 clone_info=DestroyImageInfo(clone_info);
3837 (void) RelinquishUniqueFileResource(ping_info->filename);
3838 ping_info=DestroyImageInfo(ping_info);
3873MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3876 *magick_restrict blob_info;
3887 assert(image != (Image *) NULL);
3888 assert(image->signature == MagickCoreSignature);
3889 assert(image->blob != (BlobInfo *) NULL);
3890 assert(image->blob->type != UndefinedStream);
3893 assert(data != (
void *) NULL);
3894 blob_info=image->blob;
3896 q=(
unsigned char *) data;
3897 switch (blob_info->type)
3899 case UndefinedStream:
3901 case StandardStream:
3909 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3914 c=getc(blob_info->file_info.file);
3917 *q++=(
unsigned char) c;
3923 c=getc(blob_info->file_info.file);
3926 *q++=(
unsigned char) c;
3932 c=getc(blob_info->file_info.file);
3935 *q++=(
unsigned char) c;
3941 c=getc(blob_info->file_info.file);
3944 *q++=(
unsigned char) c;
3951 if ((count != (ssize_t) length) &&
3952 (ferror(blob_info->file_info.file) != 0))
3953 ThrowBlobException(blob_info);
3958#if defined(MAGICKCORE_ZLIB_DELEGATE)
3969 for (i=0; i < length; i+=(size_t) count)
3971 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3972 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3985 c=gzgetc(blob_info->file_info.gzfile);
3988 *q++=(
unsigned char) c;
3994 c=gzgetc(blob_info->file_info.gzfile);
3997 *q++=(
unsigned char) c;
4003 c=gzgetc(blob_info->file_info.gzfile);
4006 *q++=(
unsigned char) c;
4012 c=gzgetc(blob_info->file_info.gzfile);
4015 *q++=(
unsigned char) c;
4022 (void) gzerror(blob_info->file_info.gzfile,&status);
4023 if ((count != (ssize_t) length) && (status != Z_OK))
4024 ThrowBlobException(blob_info);
4025 if (blob_info->eof == MagickFalse)
4026 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4033#if defined(MAGICKCORE_BZLIB_DELEGATE)
4040 if (blob_info->status != 0)
4042 for (i=0; i < length; i+=(size_t) count)
4044 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4045 MagickMin(length-i,MagickMaxBufferExtent));
4055 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4056 if ((count != (ssize_t) length) && (status != BZ_OK))
4057 ThrowBlobException(blob_info);
4058 if ((count != (ssize_t) length) && (status == BZ_OK))
4059 blob_info->eof=MagickTrue;
4070 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4072 blob_info->eof=MagickTrue;
4075 p=blob_info->data+blob_info->offset;
4076 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4077 blob_info->length-blob_info->offset);
4078 blob_info->offset+=count;
4079 if (count != (ssize_t) length)
4080 blob_info->eof=MagickTrue;
4081 (void) memcpy(q,p,(
size_t) count);
4086 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4088 count=blob_info->custom_stream->reader(q,length,
4089 blob_info->custom_stream->data);
4092 blob_info->eof=MagickTrue;
4124MagickExport
int ReadBlobByte(Image *image)
4127 *magick_restrict blob_info;
4132 assert(image != (Image *) NULL);
4133 assert(image->signature == MagickCoreSignature);
4134 assert(image->blob != (BlobInfo *) NULL);
4135 assert(image->blob->type != UndefinedStream);
4136 blob_info=image->blob;
4137 switch (blob_info->type)
4139 case StandardStream:
4143 c=getc(blob_info->file_info.file);
4146 if (ferror(blob_info->file_info.file) != 0)
4147 ThrowBlobException(blob_info);
4154 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4156 blob_info->eof=MagickTrue;
4159 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4160 blob_info->offset++;
4171 count=ReadBlob(image,1,buffer);
4204MagickExport
double ReadBlobDouble(Image *image)
4215 quantum.double_value=0.0;
4216 quantum.unsigned_value=ReadBlobLongLong(image);
4217 return(quantum.double_value);
4243MagickExport
float ReadBlobFloat(Image *image)
4254 quantum.float_value=0.0;
4255 quantum.unsigned_value=ReadBlobLong(image);
4256 return(quantum.float_value);
4282MagickExport
unsigned int ReadBlobLong(Image *image)
4296 assert(image != (Image *) NULL);
4297 assert(image->signature == MagickCoreSignature);
4299 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4302 if (image->endian == LSBEndian)
4304 value=(
unsigned int) (*p++);
4305 value|=(
unsigned int) (*p++) << 8;
4306 value|=(
unsigned int) (*p++) << 16;
4307 value|=(
unsigned int) (*p++) << 24;
4310 value=(
unsigned int) (*p++) << 24;
4311 value|=(
unsigned int) (*p++) << 16;
4312 value|=(
unsigned int) (*p++) << 8;
4313 value|=(
unsigned int) (*p++);
4340MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4354 assert(image != (Image *) NULL);
4355 assert(image->signature == MagickCoreSignature);
4357 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4359 return(MagickULLConstant(0));
4360 if (image->endian == LSBEndian)
4362 value=(MagickSizeType) (*p++);
4363 value|=(MagickSizeType) (*p++) << 8;
4364 value|=(MagickSizeType) (*p++) << 16;
4365 value|=(MagickSizeType) (*p++) << 24;
4366 value|=(MagickSizeType) (*p++) << 32;
4367 value|=(MagickSizeType) (*p++) << 40;
4368 value|=(MagickSizeType) (*p++) << 48;
4369 value|=(MagickSizeType) (*p++) << 56;
4372 value=(MagickSizeType) (*p++) << 56;
4373 value|=(MagickSizeType) (*p++) << 48;
4374 value|=(MagickSizeType) (*p++) << 40;
4375 value|=(MagickSizeType) (*p++) << 32;
4376 value|=(MagickSizeType) (*p++) << 24;
4377 value|=(MagickSizeType) (*p++) << 16;
4378 value|=(MagickSizeType) (*p++) << 8;
4379 value|=(MagickSizeType) (*p++);
4406MagickExport
unsigned short ReadBlobShort(Image *image)
4420 assert(image != (Image *) NULL);
4421 assert(image->signature == MagickCoreSignature);
4423 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4425 return((
unsigned short) 0U);
4426 if (image->endian == LSBEndian)
4428 value=(
unsigned short) (*p++);
4429 value|=(
unsigned short) (*p++) << 8;
4432 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4433 value|=(
unsigned short) (*p++);
4460MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4474 assert(image != (Image *) NULL);
4475 assert(image->signature == MagickCoreSignature);
4477 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4480 value=(
unsigned int) (*p++);
4481 value|=(
unsigned int) (*p++) << 8;
4482 value|=(
unsigned int) (*p++) << 16;
4483 value|=(
unsigned int) (*p++) << 24;
4510MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4521 quantum.unsigned_value=ReadBlobLSBLong(image);
4522 return(quantum.signed_value);
4548MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4562 assert(image != (Image *) NULL);
4563 assert(image->signature == MagickCoreSignature);
4565 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4567 return((
unsigned short) 0U);
4568 value=(
unsigned short) (*p++);
4569 value|=(
unsigned short) (*p++) << 8;
4596MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4607 quantum.unsigned_value=ReadBlobLSBShort(image);
4608 return(quantum.signed_value);
4634MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4648 assert(image != (Image *) NULL);
4649 assert(image->signature == MagickCoreSignature);
4651 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4654 value=(
unsigned int) (*p++) << 24;
4655 value|=(
unsigned int) (*p++) << 16;
4656 value|=(
unsigned int) (*p++) << 8;
4657 value|=(
unsigned int) (*p++);
4684MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4698 assert(image != (Image *) NULL);
4699 assert(image->signature == MagickCoreSignature);
4701 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4703 return(MagickULLConstant(0));
4704 value=(MagickSizeType) (*p++) << 56;
4705 value|=(MagickSizeType) (*p++) << 48;
4706 value|=(MagickSizeType) (*p++) << 40;
4707 value|=(MagickSizeType) (*p++) << 32;
4708 value|=(MagickSizeType) (*p++) << 24;
4709 value|=(MagickSizeType) (*p++) << 16;
4710 value|=(MagickSizeType) (*p++) << 8;
4711 value|=(MagickSizeType) (*p++);
4738MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4752 assert(image != (Image *) NULL);
4753 assert(image->signature == MagickCoreSignature);
4755 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4757 return((
unsigned short) 0U);
4758 value=(
unsigned short) ((*p++) << 8);
4759 value|=(
unsigned short) (*p++);
4760 return((
unsigned short) (value & 0xffff));
4786MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4797 quantum.unsigned_value=ReadBlobMSBLong(image);
4798 return(quantum.signed_value);
4824MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4835 quantum.unsigned_value=ReadBlobMSBShort(image);
4836 return(quantum.signed_value);
4862MagickExport
signed int ReadBlobSignedLong(Image *image)
4873 quantum.unsigned_value=ReadBlobLong(image);
4874 return(quantum.signed_value);
4900MagickExport
signed short ReadBlobSignedShort(Image *image)
4911 quantum.unsigned_value=ReadBlobShort(image);
4912 return(quantum.signed_value);
4950MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4951 const size_t length,
void *magick_restrict data,ssize_t *count)
4954 *magick_restrict blob_info;
4956 assert(image != (Image *) NULL);
4957 assert(image->signature == MagickCoreSignature);
4958 assert(image->blob != (BlobInfo *) NULL);
4959 assert(image->blob->type != UndefinedStream);
4960 assert(count != (ssize_t *) NULL);
4961 blob_info=image->blob;
4962 if (blob_info->type != BlobStream)
4964 assert(data != NULL);
4965 *count=ReadBlob(image,length,(
unsigned char *) data);
4968 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4971 blob_info->eof=MagickTrue;
4974 data=blob_info->data+blob_info->offset;
4975 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4976 blob_info->length-blob_info->offset);
4977 blob_info->offset+=(*count);
4978 if (*count != (ssize_t) length)
4979 blob_info->eof=MagickTrue;
5008MagickExport
char *ReadBlobString(Image *image,
char *
string)
5011 *magick_restrict blob_info;
5019 assert(image != (Image *) NULL);
5020 assert(image->signature == MagickCoreSignature);
5021 assert(image->blob != (BlobInfo *) NULL);
5022 assert(image->blob->type != UndefinedStream);
5023 if (IsEventLogging() != MagickFalse)
5024 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5026 blob_info=image->blob;
5027 switch (blob_info->type)
5029 case UndefinedStream:
5031 case StandardStream:
5034 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5035 if (p == (
char *) NULL)
5037 if (ferror(blob_info->file_info.file) != 0)
5038 ThrowBlobException(blob_info);
5039 return((
char *) NULL);
5046#if defined(MAGICKCORE_ZLIB_DELEGATE)
5047 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5048 if (p == (
char *) NULL)
5051 (void) gzerror(blob_info->file_info.gzfile,&status);
5053 ThrowBlobException(blob_info);
5054 return((
char *) NULL);
5064 c=ReadBlobByte(image);
5067 blob_info->eof=MagickTrue;
5070 string[i++]=(char) c;
5073 }
while (i < (MaxTextExtent-2));
5081 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5084 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5086 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5087 return((
char *) NULL);
5114MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5116 assert(blob != (BlobInfo *) NULL);
5117 assert(blob->signature == MagickCoreSignature);
5118 if (IsEventLogging() != MagickFalse)
5119 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5120 LockSemaphoreInfo(blob->semaphore);
5121 blob->reference_count++;
5122 UnlockSemaphoreInfo(blob->semaphore);
5159MagickExport MagickOffsetType SeekBlob(Image *image,
5160 const MagickOffsetType offset,
const int whence)
5163 *magick_restrict blob_info;
5165 assert(image != (Image *) NULL);
5166 assert(image->signature == MagickCoreSignature);
5167 assert(image->blob != (BlobInfo *) NULL);
5168 assert(image->blob->type != UndefinedStream);
5169 if (IsEventLogging() != MagickFalse)
5170 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5171 blob_info=image->blob;
5172 switch (blob_info->type)
5174 case UndefinedStream:
5176 case StandardStream:
5181 if ((offset < 0) && (whence == SEEK_SET))
5183 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5185 blob_info->offset=TellBlob(image);
5190#if defined(MAGICKCORE_ZLIB_DELEGATE)
5191 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5194 blob_info->offset=TellBlob(image);
5210 blob_info->offset=offset;
5215 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5216 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5221 if ((blob_info->offset+offset) < 0)
5223 blob_info->offset+=offset;
5228 if (((MagickOffsetType) blob_info->length+offset) < 0)
5230 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5234 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5236 blob_info->eof=MagickFalse;
5243 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5245 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5246 blob_info->custom_stream->data);
5250 return(blob_info->offset);
5278MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5280 assert(image != (
const Image *) NULL);
5281 assert(image->signature == MagickCoreSignature);
5282 if (IsEventLogging() != MagickFalse)
5283 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5284 image->blob->exempt=exempt;
5313MagickExport MagickBooleanType SetBlobExtent(Image *image,
5314 const MagickSizeType extent)
5317 *magick_restrict blob_info;
5319 assert(image != (Image *) NULL);
5320 assert(image->signature == MagickCoreSignature);
5321 assert(image->blob != (BlobInfo *) NULL);
5322 assert(image->blob->type != UndefinedStream);
5323 if (IsEventLogging() != MagickFalse)
5324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5325 blob_info=image->blob;
5326 switch (blob_info->type)
5328 case UndefinedStream:
5330 case StandardStream:
5331 return(MagickFalse);
5340 if (extent != (MagickSizeType) ((off_t) extent))
5341 return(MagickFalse);
5342 offset=SeekBlob(image,0,SEEK_END);
5344 return(MagickFalse);
5345 if ((MagickSizeType) offset >= extent)
5347 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5350 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5351 blob_info->file_info.file);
5352#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5353 if (blob_info->synchronize != MagickFalse)
5358 file=fileno(blob_info->file_info.file);
5359 if ((file == -1) || (offset < 0))
5360 return(MagickFalse);
5361 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5364 offset=SeekBlob(image,offset,SEEK_SET);
5366 return(MagickFalse);
5371 return(MagickFalse);
5373 return(MagickFalse);
5375 return(MagickFalse);
5378 if (extent != (MagickSizeType) ((
size_t) extent))
5379 return(MagickFalse);
5380 if (blob_info->mapped != MagickFalse)
5388 (void) UnmapBlob(blob_info->data,blob_info->length);
5389 RelinquishMagickResource(MapResource,blob_info->length);
5390 if (extent != (MagickSizeType) ((off_t) extent))
5391 return(MagickFalse);
5392 offset=SeekBlob(image,0,SEEK_END);
5394 return(MagickFalse);
5395 if ((MagickSizeType) offset >= extent)
5397 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5398 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5399 blob_info->file_info.file);
5400#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5401 if (blob_info->synchronize != MagickFalse)
5406 file=fileno(blob_info->file_info.file);
5407 if ((file == -1) || (offset < 0))
5408 return(MagickFalse);
5409 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5413 offset=SeekBlob(image,offset,SEEK_SET);
5415 return(MagickFalse);
5416 (void) AcquireMagickResource(MapResource,extent);
5417 blob_info->data=(
unsigned char*) MapBlob(fileno(
5418 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5419 blob_info->extent=(size_t) extent;
5420 blob_info->length=(size_t) extent;
5421 (void) SyncBlob(image);
5424 blob_info->extent=(size_t) extent;
5425 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5426 blob_info->extent+1,
sizeof(*blob_info->data));
5427 (void) SyncBlob(image);
5428 if (blob_info->data == (
unsigned char *) NULL)
5430 (void) DetachBlob(blob_info);
5431 return(MagickFalse);
5465MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5468 assert(custom_stream != (CustomStreamInfo *) NULL);
5469 assert(custom_stream->signature == MagickCoreSignature);
5470 custom_stream->data=data;
5498MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5499 CustomStreamHandler reader)
5501 assert(custom_stream != (CustomStreamInfo *) NULL);
5502 assert(custom_stream->signature == MagickCoreSignature);
5503 custom_stream->reader=reader;
5531MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5532 CustomStreamSeeker seeker)
5534 assert(custom_stream != (CustomStreamInfo *) NULL);
5535 assert(custom_stream->signature == MagickCoreSignature);
5536 custom_stream->seeker=seeker;
5564MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5565 CustomStreamTeller teller)
5567 assert(custom_stream != (CustomStreamInfo *) NULL);
5568 assert(custom_stream->signature == MagickCoreSignature);
5569 custom_stream->teller=teller;
5597MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5598 CustomStreamHandler writer)
5600 assert(custom_stream != (CustomStreamInfo *) NULL);
5601 assert(custom_stream->signature == MagickCoreSignature);
5602 custom_stream->writer=writer;
5629static int SyncBlob(
const Image *image)
5632 *magick_restrict blob_info;
5637 assert(image != (Image *) NULL);
5638 assert(image->signature == MagickCoreSignature);
5639 assert(image->blob != (BlobInfo *) NULL);
5640 if (IsEventLogging() != MagickFalse)
5641 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5642 if (EOFBlob(image) != 0)
5644 blob_info=image->blob;
5646 switch (blob_info->type)
5648 case UndefinedStream:
5650 case StandardStream:
5652 if ((blob_info->mode == WriteBlobMode) ||
5653 (blob_info->mode == WriteBinaryBlobMode) ||
5654 (blob_info->mode == AppendBlobMode) ||
5655 (blob_info->mode == AppendBinaryBlobMode))
5656 status=fflush(blob_info->file_info.file);
5662 status=fflush(blob_info->file_info.file);
5667#if defined(MAGICKCORE_ZLIB_DELEGATE)
5668 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5674#if defined(MAGICKCORE_BZLIB_DELEGATE)
5675 status=BZ2_bzflush(blob_info->file_info.bzfile);
5720MagickPrivate
void SyncBlobStream(ImageInfo *image_info,Image *image,
5723 assert(owner != (Image *) NULL);
5724 assert(image != (Image *) NULL);
5725 assert(image->signature == MagickCoreSignature);
5726 if (image->blob == (BlobInfo *) NULL)
5728 if (image->blob->data == (
unsigned char *) NULL)
5730 if (image_info != (ImageInfo *) NULL)
5732 image_info->blob=(
void *) image->blob->data;
5733 image_info->length=image->blob->length;
5735 if ((image == owner) || (owner->blob == (BlobInfo *) NULL))
5737 if (owner->blob == (BlobInfo *) NULL)
5743 owner->blob->data=image->blob->data;
5744 owner->blob->extent=image->blob->extent;
5745 owner->blob->length=image->blob->length;
5746 owner->blob->offset=image->blob->offset;
5747 owner->blob->quantum=image->blob->quantum;
5748 owner->blob->type=BlobStream;
5749 owner->blob->exempt=MagickTrue;
5750 (void) DetachBlob(image->blob);
5775MagickExport MagickOffsetType TellBlob(
const Image *image)
5778 *magick_restrict blob_info;
5783 assert(image != (Image *) NULL);
5784 assert(image->signature == MagickCoreSignature);
5785 assert(image->blob != (BlobInfo *) NULL);
5786 assert(image->blob->type != UndefinedStream);
5787 if (IsEventLogging() != MagickFalse)
5788 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5789 blob_info=image->blob;
5791 switch (blob_info->type)
5793 case UndefinedStream:
5794 case StandardStream:
5798 offset=ftell(blob_info->file_info.file);
5805#if defined(MAGICKCORE_ZLIB_DELEGATE)
5806 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5816 offset=blob_info->offset;
5821 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5822 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5854MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5856#if defined(MAGICKCORE_HAVE_MMAP)
5860 status=munmap(map,length);
5861 return(status == -1 ? MagickFalse : MagickTrue);
5865 return(MagickFalse);
5897MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5901 *magick_restrict blob_info;
5915 assert(image != (Image *) NULL);
5916 assert(image->signature == MagickCoreSignature);
5917 assert(image->blob != (BlobInfo *) NULL);
5918 assert(image->blob->type != UndefinedStream);
5921 assert(data != (
const void *) NULL);
5922 blob_info=image->blob;
5924 p=(
const unsigned char *) data;
5925 q=(
unsigned char *) data;
5926 switch (blob_info->type)
5928 case UndefinedStream:
5930 case StandardStream:
5938 count=(ssize_t) fwrite((
const char *) data,1,length,
5939 blob_info->file_info.file);
5944 c=putc((
int) *p++,blob_info->file_info.file);
5952 c=putc((
int) *p++,blob_info->file_info.file);
5960 c=putc((
int) *p++,blob_info->file_info.file);
5968 c=putc((
int) *p++,blob_info->file_info.file);
5977 if ((count != (ssize_t) length) &&
5978 (ferror(blob_info->file_info.file) != 0))
5979 ThrowBlobException(blob_info);
5984#if defined(MAGICKCORE_ZLIB_DELEGATE)
5995 for (i=0; i < length; i+=(size_t) count)
5997 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5998 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
6011 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6019 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6027 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6035 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6045 (void) gzerror(blob_info->file_info.gzfile,&status);
6046 if ((count != (ssize_t) length) && (status != Z_OK))
6047 ThrowBlobException(blob_info);
6053#if defined(MAGICKCORE_BZLIB_DELEGATE)
6060 for (i=0; i < length; i+=(size_t) count)
6062 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6063 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6073 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6074 if ((count != (ssize_t) length) && (status != BZ_OK))
6075 ThrowBlobException(blob_info);
6081 count=(ssize_t) blob_info->stream(image,data,length);
6089 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6094 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6095 if (extent >= blob_info->extent)
6097 extent+=blob_info->quantum+length;
6098 blob_info->quantum<<=1;
6099 if (SetBlobExtent(image,extent) == MagickFalse)
6102 q=blob_info->data+blob_info->offset;
6103 (void) memcpy(q,p,length);
6104 blob_info->offset+=(MagickOffsetType) length;
6105 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6106 blob_info->length=(size_t) blob_info->offset;
6107 count=(ssize_t) length;
6112 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6113 count=blob_info->custom_stream->writer((
unsigned char *) data,
6114 length,blob_info->custom_stream->data);
6146MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6149 *magick_restrict blob_info;
6154 assert(image != (Image *) NULL);
6155 assert(image->signature == MagickCoreSignature);
6156 assert(image->blob != (BlobInfo *) NULL);
6157 assert(image->blob->type != UndefinedStream);
6158 blob_info=image->blob;
6160 switch (blob_info->type)
6162 case StandardStream:
6169 c=putc((
int) value,blob_info->file_info.file);
6172 if (ferror(blob_info->file_info.file) != 0)
6173 ThrowBlobException(blob_info);
6181 count=WriteBlobStream(image,1,&value);
6213MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6224 quantum.unsigned_value=0U;
6225 quantum.float_value=value;
6226 return(WriteBlobLong(image,quantum.unsigned_value));
6254MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6259 assert(image != (Image *) NULL);
6260 assert(image->signature == MagickCoreSignature);
6261 if (image->endian == LSBEndian)
6263 buffer[0]=(
unsigned char) value;
6264 buffer[1]=(
unsigned char) (value >> 8);
6265 buffer[2]=(
unsigned char) (value >> 16);
6266 buffer[3]=(
unsigned char) (value >> 24);
6267 return(WriteBlobStream(image,4,buffer));
6269 buffer[0]=(
unsigned char) (value >> 24);
6270 buffer[1]=(
unsigned char) (value >> 16);
6271 buffer[2]=(
unsigned char) (value >> 8);
6272 buffer[3]=(
unsigned char) value;
6273 return(WriteBlobStream(image,4,buffer));
6301MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6306 assert(image != (Image *) NULL);
6307 assert(image->signature == MagickCoreSignature);
6308 if (image->endian == LSBEndian)
6310 buffer[0]=(
unsigned char) value;
6311 buffer[1]=(
unsigned char) (value >> 8);
6312 buffer[2]=(
unsigned char) (value >> 16);
6313 buffer[3]=(
unsigned char) (value >> 24);
6314 buffer[4]=(
unsigned char) (value >> 32);
6315 buffer[5]=(
unsigned char) (value >> 40);
6316 buffer[6]=(
unsigned char) (value >> 48);
6317 buffer[7]=(
unsigned char) (value >> 56);
6318 return(WriteBlobStream(image,8,buffer));
6320 buffer[0]=(
unsigned char) (value >> 56);
6321 buffer[1]=(
unsigned char) (value >> 48);
6322 buffer[2]=(
unsigned char) (value >> 40);
6323 buffer[3]=(
unsigned char) (value >> 32);
6324 buffer[4]=(
unsigned char) (value >> 24);
6325 buffer[5]=(
unsigned char) (value >> 16);
6326 buffer[6]=(
unsigned char) (value >> 8);
6327 buffer[7]=(
unsigned char) value;
6328 return(WriteBlobStream(image,8,buffer));
6356MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6361 assert(image != (Image *) NULL);
6362 assert(image->signature == MagickCoreSignature);
6363 if (image->endian == LSBEndian)
6365 buffer[0]=(
unsigned char) value;
6366 buffer[1]=(
unsigned char) (value >> 8);
6367 return(WriteBlobStream(image,2,buffer));
6369 buffer[0]=(
unsigned char) (value >> 8);
6370 buffer[1]=(
unsigned char) value;
6371 return(WriteBlobStream(image,2,buffer));
6399MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6413 assert(image != (Image *) NULL);
6414 assert(image->signature == MagickCoreSignature);
6415 quantum.signed_value=value;
6416 if (image->endian == LSBEndian)
6418 buffer[0]=(
unsigned char) quantum.unsigned_value;
6419 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6420 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6421 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6422 return(WriteBlobStream(image,4,buffer));
6424 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6425 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6426 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6427 buffer[3]=(
unsigned char) quantum.unsigned_value;
6428 return(WriteBlobStream(image,4,buffer));
6456MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6461 assert(image != (Image *) NULL);
6462 assert(image->signature == MagickCoreSignature);
6463 buffer[0]=(
unsigned char) value;
6464 buffer[1]=(
unsigned char) (value >> 8);
6465 buffer[2]=(
unsigned char) (value >> 16);
6466 buffer[3]=(
unsigned char) (value >> 24);
6467 return(WriteBlobStream(image,4,buffer));
6495MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6500 assert(image != (Image *) NULL);
6501 assert(image->signature == MagickCoreSignature);
6502 buffer[0]=(
unsigned char) value;
6503 buffer[1]=(
unsigned char) (value >> 8);
6504 return(WriteBlobStream(image,2,buffer));
6532MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6546 assert(image != (Image *) NULL);
6547 assert(image->signature == MagickCoreSignature);
6548 quantum.signed_value=value;
6549 buffer[0]=(
unsigned char) quantum.unsigned_value;
6550 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6551 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6552 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6553 return(WriteBlobStream(image,4,buffer));
6581MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6582 const signed short value)
6596 assert(image != (Image *) NULL);
6597 assert(image->signature == MagickCoreSignature);
6598 quantum.signed_value=value;
6599 buffer[0]=(
unsigned char) quantum.unsigned_value;
6600 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6601 return(WriteBlobStream(image,2,buffer));
6629MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6634 assert(image != (Image *) NULL);
6635 assert(image->signature == MagickCoreSignature);
6636 buffer[0]=(
unsigned char) (value >> 24);
6637 buffer[1]=(
unsigned char) (value >> 16);
6638 buffer[2]=(
unsigned char) (value >> 8);
6639 buffer[3]=(
unsigned char) value;
6640 return(WriteBlobStream(image,4,buffer));
6668MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6669 const signed short value)
6683 assert(image != (Image *) NULL);
6684 assert(image->signature == MagickCoreSignature);
6685 quantum.signed_value=value;
6686 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6687 buffer[1]=(
unsigned char) quantum.unsigned_value;
6688 return(WriteBlobStream(image,2,buffer));
6716MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6721 assert(image != (Image *) NULL);
6722 assert(image->signature == MagickCoreSignature);
6723 buffer[0]=(
unsigned char) (value >> 8);
6724 buffer[1]=(
unsigned char) value;
6725 return(WriteBlobStream(image,2,buffer));
6753MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6755 assert(image != (Image *) NULL);
6756 assert(image->signature == MagickCoreSignature);
6757 assert(
string != (
const char *) NULL);
6758 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));