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 if (blob_info->quantum <= (MAGICK_SIZE_MAX >> 1))
1662 blob_info->quantum<<=1;
1663 if (SetBlobExtent(image,extent) == MagickFalse)
1666 q=blob_info->data+blob_info->offset;
1667 (void) memcpy(q,data,length);
1668 blob_info->offset+=(MagickOffsetType) length;
1669 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1670 blob_info->length=(size_t) blob_info->offset;
1671 return((ssize_t) length);
1674MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1675 ExceptionInfo *exception)
1696 assert(image != (
const Image *) NULL);
1697 assert(image->signature == MagickCoreSignature);
1698 assert(filename != (
const char *) NULL);
1699 if (IsEventLogging() != MagickFalse)
1700 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1701 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1702 ThrowPolicyException(filename,MagickFalse);
1704 if (LocaleCompare(filename,
"-") != 0)
1707 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1709 file=open_utf8(filename,flags,0);
1713 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1714 return(MagickFalse);
1716 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1718 if (LocaleCompare(filename,
"-") != 0)
1719 (void) close_utf8(file);
1720 ThrowPolicyException(filename,MagickFalse);
1722 quantum=(size_t) MagickMaxBufferExtent;
1723 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1724 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1725 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1726 if (blob == (
unsigned char *) NULL)
1728 if (LocaleCompare(filename,
"-") != 0)
1729 (void) close_utf8(file);
1730 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1732 return(MagickFalse);
1737 count=MagickRead(file,blob,quantum);
1742 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1747 length=(size_t) count;
1748 count=WriteBlobStream(image,length,blob);
1749 if (count != (ssize_t) length)
1751 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1756 if (LocaleCompare(filename,
"-") != 0)
1758 if (close_utf8(file) == -1)
1760 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1764 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1791MagickExport MagickBooleanType GetBlobError(
const Image *image)
1793 assert(image != (
const Image *) NULL);
1794 assert(image->signature == MagickCoreSignature);
1795 if (IsEventLogging() != MagickFalse)
1796 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1797 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1798 errno=image->blob->error_number;
1799 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1825MagickExport FILE *GetBlobFileHandle(
const Image *image)
1827 assert(image != (
const Image *) NULL);
1828 assert(image->signature == MagickCoreSignature);
1829 assert(image->blob != (BlobInfo *) NULL);
1830 assert(image->blob->signature == MagickCoreSignature);
1831 switch (image->blob->type)
1833 case StandardStream:
1836 return(image->blob->file_info.file);
1837 case UndefinedStream:
1845 return((FILE *) NULL);
1870MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1872 assert(blob_info != (BlobInfo *) NULL);
1873 (void) memset(blob_info,0,
sizeof(*blob_info));
1874 blob_info->type=UndefinedStream;
1875 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1876 blob_info->properties.st_mtime=GetMagickTime();
1877 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1878 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1879 blob_info->reference_count=1;
1880 blob_info->semaphore=AcquireSemaphoreInfo();
1881 blob_info->signature=MagickCoreSignature;
1906MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1908 assert(image != (Image *) NULL);
1909 assert(image->signature == MagickCoreSignature);
1910 if (IsEventLogging() != MagickFalse)
1911 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1912 return(&image->blob->properties);
1938MagickExport MagickSizeType GetBlobSize(
const Image *image)
1941 *magick_restrict blob_info;
1946 assert(image != (Image *) NULL);
1947 assert(image->signature == MagickCoreSignature);
1948 assert(image->blob != (BlobInfo *) NULL);
1949 if (IsEventLogging() != MagickFalse)
1950 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1951 blob_info=image->blob;
1953 switch (blob_info->type)
1955 case UndefinedStream:
1956 case StandardStream:
1958 extent=blob_info->size;
1966 extent=(MagickSizeType) blob_info->properties.st_size;
1968 extent=blob_info->size;
1969 file_descriptor=fileno(blob_info->file_info.file);
1970 if (file_descriptor == -1)
1972 if (fstat(file_descriptor,&blob_info->properties) == 0)
1973 extent=(MagickSizeType) blob_info->properties.st_size;
1978 extent=blob_info->size;
1987 status=GetPathAttributes(image->filename,&blob_info->properties);
1988 if (status != MagickFalse)
1989 extent=(MagickSizeType) blob_info->properties.st_size;
1996 extent=(MagickSizeType) blob_info->length;
2001 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
2002 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
2007 offset=blob_info->custom_stream->teller(
2008 blob_info->custom_stream->data);
2009 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
2010 blob_info->custom_stream->data);
2011 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
2012 blob_info->custom_stream->data);
2042MagickExport
void *GetBlobStreamData(
const Image *image)
2044 assert(image != (
const Image *) NULL);
2045 assert(image->signature == MagickCoreSignature);
2046 return(image->blob->data);
2071MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2073 assert(image != (
const Image *) NULL);
2074 assert(image->signature == MagickCoreSignature);
2075 if (IsEventLogging() != MagickFalse)
2076 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2077 return(image->blob->stream);
2113MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2114 Image *image,
size_t *length,ExceptionInfo *exception)
2128 assert(image_info != (
const ImageInfo *) NULL);
2129 assert(image_info->signature == MagickCoreSignature);
2130 assert(image != (Image *) NULL);
2131 assert(image->signature == MagickCoreSignature);
2132 assert(exception != (ExceptionInfo *) NULL);
2133 assert(exception->signature == MagickCoreSignature);
2134 if (IsEventLogging() != MagickFalse)
2135 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2136 image_info->filename);
2138 blob=(
unsigned char *) NULL;
2139 blob_info=CloneImageInfo(image_info);
2140 blob_info->adjoin=MagickFalse;
2141 (void) SetImageInfo(blob_info,1,exception);
2142 if (*blob_info->magick !=
'\0')
2143 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2144 magick_info=GetMagickInfo(image->magick,exception);
2145 if (magick_info == (
const MagickInfo *) NULL)
2147 (void) ThrowMagickException(exception,GetMagickModule(),
2148 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2150 blob_info=DestroyImageInfo(blob_info);
2153 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2154 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2159 blob_info->length=0;
2160 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2161 sizeof(
unsigned char));
2162 if (blob_info->blob == NULL)
2163 (void) ThrowMagickException(exception,GetMagickModule(),
2164 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2167 (void) CloseBlob(image);
2168 image->blob->exempt=MagickTrue;
2169 image->blob->extent=0;
2170 *image->filename=
'\0';
2171 status=WriteImage(blob_info,image,exception);
2172 SyncBlobStream(blob_info,image,image);
2173 *length=image->blob->length;
2174 blob=DetachBlob(image->blob);
2175 if (blob != (
void *) NULL)
2177 if (status == MagickFalse)
2178 blob=RelinquishMagickMemory(blob);
2180 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2183 if ((status == MagickFalse) && (image->blob->extent == 0))
2184 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2190 unique[MagickPathExtent];
2198 file=AcquireUniqueFileResource(unique);
2201 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2202 image_info->filename);
2206 blob_info->file=fdopen(file,
"wb");
2207 if (blob_info->file != (FILE *) NULL)
2209 (void) FormatLocaleString(image->filename,MagickPathExtent,
2210 "%s:%s",image->magick,unique);
2211 status=WriteImage(blob_info,image,exception);
2212 (void) fclose(blob_info->file);
2213 if (status != MagickFalse)
2214 blob=FileToBlob(unique,MAGICK_SIZE_MAX,length,exception);
2216 (void) RelinquishUniqueFileResource(unique);
2219 blob_info=DestroyImageInfo(blob_info);
2251MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2252 ExceptionInfo *exception)
2264 assert(image_info != (
const ImageInfo *) NULL);
2265 assert(image_info->signature == MagickCoreSignature);
2266 assert(image != (Image *) NULL);
2267 assert(image->signature == MagickCoreSignature);
2268 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2269 assert(image_info->custom_stream->signature == MagickCoreSignature);
2270 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2271 assert(exception != (ExceptionInfo *) NULL);
2272 if (IsEventLogging() != MagickFalse)
2273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2274 image_info->filename);
2275 clone_info=CloneImageInfo(image_info);
2276 clone_info->adjoin=MagickFalse;
2277 (void) SetImageInfo(clone_info,1,exception);
2278 if (*clone_info->magick !=
'\0')
2279 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2280 magick_info=GetMagickInfo(image->magick,exception);
2281 if (magick_info == (
const MagickInfo *) NULL)
2283 (void) ThrowMagickException(exception,GetMagickModule(),
2284 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2286 clone_info=DestroyImageInfo(clone_info);
2289 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2290 blob_support=GetMagickBlobSupport(magick_info);
2291 if ((blob_support != MagickFalse) &&
2292 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2294 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2295 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2296 blob_support=MagickFalse;
2298 if (blob_support != MagickFalse)
2303 (void) CloseBlob(image);
2304 *image->filename=
'\0';
2305 if (WriteImage(clone_info,image,exception) == MagickFalse)
2307 (void) ThrowMagickException(exception,GetMagickModule(),
2308 BlobError,
"UnableToWriteBlob",
"`%s'",image_info->filename);
2314 unique[MagickPathExtent];
2325 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2326 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2328 if (blob == (
unsigned char *) NULL)
2330 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2331 image_info->filename);
2332 clone_info=DestroyImageInfo(clone_info);
2335 file=AcquireUniqueFileResource(unique);
2338 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2339 image_info->filename);
2340 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2341 clone_info=DestroyImageInfo(clone_info);
2344 clone_info->file=fdopen(file,
"wb+");
2345 if (clone_info->file != (FILE *) NULL)
2350 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2351 image->magick,unique);
2352 status=WriteImage(clone_info,image,exception);
2353 if (status != MagickFalse)
2355 (void) fseek(clone_info->file,0,SEEK_SET);
2356 count=(ssize_t) MagickMaxBufferExtent;
2357 while (count == (ssize_t) MagickMaxBufferExtent)
2359 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2361 if (image_info->custom_stream->writer(blob,(
size_t) count,
2362 image_info->custom_stream->data) != count)
2364 (void) ThrowMagickException(exception,GetMagickModule(),
2365 BlobError,
"UnableToWriteBlob",
"`%s'",image_info->filename);
2370 (void) fclose(clone_info->file);
2372 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2373 (void) RelinquishUniqueFileResource(unique);
2375 clone_info=DestroyImageInfo(clone_info);
2406MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2407 ExceptionInfo *exception)
2431 assert(image != (Image *) NULL);
2432 assert(image->signature == MagickCoreSignature);
2433 assert(image->blob != (BlobInfo *) NULL);
2434 assert(image->blob->type != UndefinedStream);
2435 assert(filename != (
const char *) NULL);
2436 if (IsEventLogging() != MagickFalse)
2437 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2438 if (*filename ==
'\0')
2439 file=AcquireUniqueFileResource(filename);
2441 if (LocaleCompare(filename,
"-") == 0)
2442 file=fileno(stdout);
2444 file=open_utf8(filename,O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2447 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2448 return(MagickFalse);
2450 quantum=(size_t) MagickMaxBufferExtent;
2451 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2452 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2453 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2454 if (buffer == (
unsigned char *) NULL)
2456 file=close_utf8(file)-1;
2457 (void) ThrowMagickException(exception,GetMagickModule(),
2458 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2459 return(MagickFalse);
2462 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2463 for (i=0; count > 0; )
2465 length=(size_t) count;
2466 for (i=0; i < length; i+=(size_t) count)
2468 count=MagickWrite(file,p+i,(
size_t) (length-i));
2474 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2476 if (LocaleCompare(filename,
"-") != 0)
2477 file=close_utf8(file);
2478 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2479 if ((file == -1) || (i < length))
2482 file=close_utf8(file);
2483 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2484 return(MagickFalse);
2524MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2525 size_t *length,ExceptionInfo *exception)
2539 assert(image_info != (
const ImageInfo *) NULL);
2540 assert(image_info->signature == MagickCoreSignature);
2541 assert(images != (Image *) NULL);
2542 assert(images->signature == MagickCoreSignature);
2543 assert(exception != (ExceptionInfo *) NULL);
2544 if (IsEventLogging() != MagickFalse)
2545 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2546 image_info->filename);
2548 blob=(
unsigned char *) NULL;
2549 blob_info=CloneImageInfo(image_info);
2550 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2552 if (*blob_info->magick !=
'\0')
2553 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2554 magick_info=GetMagickInfo(images->magick,exception);
2555 if (magick_info == (
const MagickInfo *) NULL)
2557 (void) ThrowMagickException(exception,GetMagickModule(),
2558 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2560 blob_info=DestroyImageInfo(blob_info);
2563 if (GetMagickAdjoin(magick_info) == MagickFalse)
2565 blob_info=DestroyImageInfo(blob_info);
2566 return(ImageToBlob(image_info,images,length,exception));
2568 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2569 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2574 blob_info->length=0;
2575 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2576 sizeof(
unsigned char));
2577 if (blob_info->blob == (
void *) NULL)
2578 (void) ThrowMagickException(exception,GetMagickModule(),
2579 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2582 (void) CloseBlob(images);
2583 images->blob->exempt=MagickTrue;
2584 images->blob->extent=0;
2585 *images->filename=
'\0';
2586 status=WriteImages(blob_info,images,images->filename,exception);
2587 SyncBlobStream(blob_info,images,images);
2588 *length=images->blob->length;
2589 blob=DetachBlob(images->blob);
2590 if (blob != (
void *) NULL)
2592 if (status == MagickFalse)
2593 blob=RelinquishMagickMemory(blob);
2595 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2597 else if ((status == MagickFalse) && (images->blob->extent == 0))
2598 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2604 filename[MagickPathExtent],
2605 unique[MagickPathExtent];
2613 file=AcquireUniqueFileResource(unique);
2616 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2617 image_info->filename);
2621 blob_info->file=fdopen(file,
"wb");
2622 if (blob_info->file != (FILE *) NULL)
2624 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2625 images->magick,unique);
2626 status=WriteImages(blob_info,images,filename,exception);
2627 (void) fclose(blob_info->file);
2628 if (status != MagickFalse)
2629 blob=FileToBlob(unique,MAGICK_SIZE_MAX,length,exception);
2631 (void) RelinquishUniqueFileResource(unique);
2634 blob_info=DestroyImageInfo(blob_info);
2666MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2667 Image *images,ExceptionInfo *exception)
2679 assert(image_info != (
const ImageInfo *) NULL);
2680 assert(image_info->signature == MagickCoreSignature);
2681 assert(images != (Image *) NULL);
2682 assert(images->signature == MagickCoreSignature);
2683 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2684 assert(image_info->custom_stream->signature == MagickCoreSignature);
2685 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2686 assert(exception != (ExceptionInfo *) NULL);
2687 if (IsEventLogging() != MagickFalse)
2688 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2689 image_info->filename);
2690 clone_info=CloneImageInfo(image_info);
2691 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2693 if (*clone_info->magick !=
'\0')
2694 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2695 magick_info=GetMagickInfo(images->magick,exception);
2696 if (magick_info == (
const MagickInfo *) NULL)
2698 (void) ThrowMagickException(exception,GetMagickModule(),
2699 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2701 clone_info=DestroyImageInfo(clone_info);
2704 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2705 blob_support=GetMagickBlobSupport(magick_info);
2706 if ((blob_support != MagickFalse) &&
2707 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2709 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2710 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2711 blob_support=MagickFalse;
2713 if (blob_support != MagickFalse)
2718 (void) CloseBlob(images);
2719 *images->filename=
'\0';
2720 (void) WriteImages(clone_info,images,images->filename,exception);
2725 filename[MagickPathExtent],
2726 unique[MagickPathExtent];
2737 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2738 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2740 if (blob == (
unsigned char *) NULL)
2742 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2743 image_info->filename);
2744 clone_info=DestroyImageInfo(clone_info);
2747 file=AcquireUniqueFileResource(unique);
2750 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2751 image_info->filename);
2752 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2753 clone_info=DestroyImageInfo(clone_info);
2756 clone_info->file=fdopen(file,
"wb+");
2757 if (clone_info->file != (FILE *) NULL)
2762 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2763 images->magick,unique);
2764 status=WriteImages(clone_info,images,filename,exception);
2765 if (status != MagickFalse)
2767 (void) fseek(clone_info->file,0,SEEK_SET);
2768 count=(ssize_t) MagickMaxBufferExtent;
2769 while (count == (ssize_t) MagickMaxBufferExtent)
2771 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2773 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2774 image_info->custom_stream->data);
2777 (void) fclose(clone_info->file);
2779 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2780 (void) RelinquishUniqueFileResource(unique);
2782 clone_info=DestroyImageInfo(clone_info);
2818MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2819 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2822 filename[MagickPathExtent];
2848 assert(image_info != (ImageInfo *) NULL);
2849 assert(image_info->signature == MagickCoreSignature);
2850 assert(image != (Image *) NULL);
2851 assert(image->signature == MagickCoreSignature);
2852 assert(inject_image != (Image *) NULL);
2853 assert(inject_image->signature == MagickCoreSignature);
2854 assert(exception != (ExceptionInfo *) NULL);
2855 if (IsEventLogging() != MagickFalse)
2856 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2857 unique_file=(FILE *) NULL;
2858 file=AcquireUniqueFileResource(filename);
2860 unique_file=fdopen(file,
"wb");
2861 if ((file == -1) || (unique_file == (FILE *) NULL))
2865 (void) close_utf8(file);
2866 (void) RelinquishUniqueFileResource(filename);
2868 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2869 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2871 return(MagickFalse);
2873 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2874 if (byte_image == (Image *) NULL)
2876 (void) fclose(unique_file);
2877 (void) RelinquishUniqueFileResource(filename);
2878 return(MagickFalse);
2880 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2882 DestroyBlob(byte_image);
2883 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2884 write_info=CloneImageInfo(image_info);
2885 SetImageInfoFile(write_info,unique_file);
2886 status=WriteImage(write_info,byte_image,exception);
2887 write_info=DestroyImageInfo(write_info);
2888 byte_image=DestroyImage(byte_image);
2889 (void) fclose(unique_file);
2890 if (status == MagickFalse)
2892 (void) RelinquishUniqueFileResource(filename);
2893 return(MagickFalse);
2895 file=open_utf8(filename,O_RDONLY | O_CLOEXEC | O_BINARY,0);
2898 (void) RelinquishUniqueFileResource(filename);
2899 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2900 image_info->filename);
2901 return(MagickFalse);
2903 quantum=(size_t) MagickMaxBufferExtent;
2904 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2905 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2906 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2907 if (buffer == (
unsigned char *) NULL)
2909 (void) close_utf8(file);
2910 (void) RelinquishUniqueFileResource(filename);
2911 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2920 count=read(file,buffer,quantum);
2930 if (WriteBlobStream(image,(
size_t) count,buffer) != count)
2936 if (close_utf8(file) == -1)
2938 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2941 (void) RelinquishUniqueFileResource(filename);
2942 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2968MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2970 assert(image != (
const Image *) NULL);
2971 assert(image->signature == MagickCoreSignature);
2972 if (IsEventLogging() != MagickFalse)
2973 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2974 return(image->blob->exempt);
2999MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
3002 *magick_restrict blob_info;
3004 assert(image != (
const Image *) NULL);
3005 assert(image->signature == MagickCoreSignature);
3006 if (IsEventLogging() != MagickFalse)
3007 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3008 blob_info=image->blob;
3009 switch (blob_info->type)
3018 if (blob_info->file_info.file == (FILE *) NULL)
3019 return(MagickFalse);
3020 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
3021 return(status == -1 ? MagickFalse : MagickTrue);
3025#if defined(MAGICKCORE_ZLIB_DELEGATE)
3029 if (blob_info->file_info.gzfile == (gzFile) NULL)
3030 return(MagickFalse);
3031 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
3032 return(offset < 0 ? MagickFalse : MagickTrue);
3037 case UndefinedStream:
3041 case StandardStream:
3045 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3046 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3053 return(MagickFalse);
3078MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3080 assert(image != (
const Image *) NULL);
3081 assert(image->signature == MagickCoreSignature);
3082 if (IsEventLogging() != MagickFalse)
3083 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3084 return(image->blob->temporary);
3116MagickExport
void *MapBlob(
int file,
const MapMode mode,
3117 const MagickOffsetType offset,
const size_t length)
3119#if defined(MAGICKCORE_HAVE_MMAP)
3132#if defined(MAP_ANONYMOUS)
3133 flags|=MAP_ANONYMOUS;
3142 protection=PROT_READ;
3148 protection=PROT_WRITE;
3154 protection=PROT_READ | PROT_WRITE;
3159#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3160 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3162 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3163 if (map == MAP_FAILED)
3164 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3166 if (map == MAP_FAILED)
3203MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3212 assert(buffer != (
unsigned char *) NULL);
3219 *buffer++=(
unsigned char) c;
3223 *buffer++=(
unsigned char) c;
3253MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3261 assert(p != (
unsigned char *) NULL);
3268 *p++=(
unsigned char) c;
3304static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3305 const BlobInfo *blob_info)
3316 size=MagickMinBufferExtent;
3317 option=GetImageOption(image_info,
"stream:buffer-size");
3318 if (option != (
const char *) NULL)
3319 size=StringToUnsignedLong(option);
3320 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3321 _IONBF : _IOFBF,size);
3322 return(status == 0 ? MagickTrue : MagickFalse);
3325#if defined(MAGICKCORE_ZLIB_DELEGATE)
3326static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3328#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3329 return(gzopen(path,mode));
3337 path_wide=NTCreateWidePath(path);
3338 if (path_wide == (
wchar_t *) NULL)
3339 return((gzFile) NULL);
3340 file=gzopen_w(path_wide,mode);
3341 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3347MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3348 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3351 *magick_restrict blob_info;
3354 extension[MagickPathExtent],
3355 filename[MagickPathExtent];
3361 flags = O_RDONLY | O_CLOEXEC;
3369 assert(image_info != (ImageInfo *) NULL);
3370 assert(image_info->signature == MagickCoreSignature);
3371 assert(image != (Image *) NULL);
3372 assert(image->signature == MagickCoreSignature);
3373 if (IsEventLogging() != MagickFalse)
3374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3375 image_info->filename);
3376 blob_info=image->blob;
3377 if (image_info->blob != (
void *) NULL)
3379 if (image_info->stream != (StreamHandler) NULL)
3380 blob_info->stream=(StreamHandler) image_info->stream;
3381 AttachBlob(blob_info,image_info->blob,image_info->length);
3384 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3385 (*image->filename ==
'\0'))
3387 blob_info->type=CustomStream;
3388 blob_info->custom_stream=image_info->custom_stream;
3389 if (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL)
3390 blob_info->custom_stream->seeker(0,SEEK_SET,
3391 blob_info->custom_stream->data);
3394 (void) DetachBlob(blob_info);
3395 blob_info->mode=mode;
3400 flags=O_RDONLY | O_CLOEXEC;
3404 case ReadBinaryBlobMode:
3406 flags=O_RDONLY | O_CLOEXEC | O_BINARY;
3412 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC;
3416 case WriteBinaryBlobMode:
3418 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_TRUNC | O_BINARY;
3422 case AppendBlobMode:
3424 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_APPEND;
3428 case AppendBinaryBlobMode:
3430 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_APPEND | O_BINARY;
3436 flags=O_RDONLY | O_CLOEXEC;
3442 blob_info->synchronize=image_info->synchronize;
3443 if (image_info->stream != (StreamHandler) NULL)
3445 blob_info->stream=image_info->stream;
3448 blob_info->type=FifoStream;
3456 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3457 rights=ReadPolicyRights;
3459 rights=WritePolicyRights;
3460 if (IsPathAuthorized(rights,filename) == MagickFalse)
3461 ThrowPolicyException(filename,MagickFalse);
3462 if ((LocaleCompare(filename,
"-") == 0) ||
3463 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3465 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3466#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3467 if (strchr(type,
'b') != (
char *) NULL)
3468 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3470 blob_info->type=StandardStream;
3471 blob_info->exempt=MagickTrue;
3472 return(SetStreamBuffering(image_info,blob_info));
3474 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3475 (IsGeometry(filename+3) != MagickFalse))
3482 blob_info->file_info.file=fdopen(StringToInteger(filename+3),fileMode);
3483 if (blob_info->file_info.file == (FILE *) NULL)
3485 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3486 return(MagickFalse);
3488#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3489 if (strchr(type,
'b') != (
char *) NULL)
3490 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3492 blob_info->type=FileStream;
3493 blob_info->exempt=MagickTrue;
3494 return(SetStreamBuffering(image_info,blob_info));
3496#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3497 if (*filename ==
'|')
3500 fileMode[MagickPathExtent],
3508 (void) signal(SIGPIPE,SIG_IGN);
3512 sanitize_command=SanitizeString(filename+1);
3513 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3514 sanitize_command=DestroyString(sanitize_command);
3515 if (blob_info->file_info.file == (FILE *) NULL)
3517 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3518 return(MagickFalse);
3520 blob_info->type=PipeStream;
3521 blob_info->exempt=MagickTrue;
3522 return(SetStreamBuffering(image_info,blob_info));
3525 status=GetPathAttributes(filename,&blob_info->properties);
3526#if defined(S_ISFIFO)
3527 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3529 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3530 if (blob_info->file_info.file == (FILE *) NULL)
3532 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3533 return(MagickFalse);
3535 blob_info->type=FileStream;
3536 blob_info->exempt=MagickTrue;
3537 return(SetStreamBuffering(image_info,blob_info));
3540 GetPathComponent(image->filename,ExtensionPath,extension);
3543 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3544 if ((image_info->adjoin == MagickFalse) ||
3545 (strchr(filename,
'%') != (
char *) NULL))
3550 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3551 image->scene,filename,exception);
3552 if ((LocaleCompare(filename,image->filename) == 0) &&
3553 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3554 (GetNextImageInList(image) != (Image *) NULL)))
3557 path[MagickPathExtent];
3559 GetPathComponent(image->filename,RootPath,path);
3560 if (*extension ==
'\0')
3561 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.17g",
3562 path,(
double) image->scene);
3564 (
void) FormatLocaleString(filename,MagickPathExtent,
3565 "%s-%.17g.%s",path,(
double) image->scene,extension);
3567 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3569 if (IsPathAuthorized(rights,filename) == MagickFalse)
3570 ThrowPolicyException(filename,MagickFalse);
3572 if (image_info->file != (FILE *) NULL)
3574 blob_info->file_info.file=image_info->file;
3575 blob_info->type=FileStream;
3576 blob_info->exempt=MagickTrue;
3584 blob_info->file_info.file=(FILE *) NULL;
3585 file=open_utf8(filename,flags,0);
3587 blob_info->file_info.file=fdopen(file,type);
3588 if (blob_info->file_info.file != (FILE *) NULL)
3596 blob_info->type=FileStream;
3597 (void) SetStreamBuffering(image_info,blob_info);
3598 (void) memset(magick,0,
sizeof(magick));
3599 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3600 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3601#if defined(MAGICKCORE_POSIX_SUPPORT)
3602 (void) fflush(blob_info->file_info.file);
3604 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3605 " read %.17g magic header bytes",(
double) count);
3606#if defined(MAGICKCORE_ZLIB_DELEGATE)
3607 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3608 ((
int) magick[2] == 0x08))
3611 gzfile = gzopen_utf8(filename,
"rb");
3613 if (gzfile != (gzFile) NULL)
3615 if (blob_info->file_info.file != (FILE *) NULL)
3616 (void) fclose(blob_info->file_info.file);
3617 blob_info->file_info.file=(FILE *) NULL;
3618 blob_info->file_info.gzfile=gzfile;
3619 blob_info->type=ZipStream;
3623#if defined(MAGICKCORE_BZLIB_DELEGATE)
3624 if (strncmp((
char *) magick,
"BZh",3) == 0)
3627 *bzfile = BZ2_bzopen(filename,
"r");
3629 if (bzfile != (BZFILE *) NULL)
3631 if (blob_info->file_info.file != (FILE *) NULL)
3632 (void) fclose(blob_info->file_info.file);
3633 blob_info->file_info.file=(FILE *) NULL;
3634 blob_info->file_info.bzfile=bzfile;
3635 blob_info->type=BZipStream;
3639 if (blob_info->type == FileStream)
3650 sans_exception=AcquireExceptionInfo();
3651 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3652 sans_exception=DestroyExceptionInfo(sans_exception);
3653 length=(size_t) blob_info->properties.st_size;
3654 if ((magick_info != (
const MagickInfo *) NULL) &&
3655 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3656 (AcquireMagickResource(MapResource,length) != MagickFalse))
3661 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3663 if (blob == (
void *) NULL)
3664 RelinquishMagickResource(MapResource,length);
3670 if (image_info->file != (FILE *) NULL)
3671 blob_info->exempt=MagickFalse;
3674 (void) fclose(blob_info->file_info.file);
3675 blob_info->file_info.file=(FILE *) NULL;
3677 AttachBlob(blob_info,blob,length);
3678 blob_info->mapped=MagickTrue;
3685#if defined(MAGICKCORE_ZLIB_DELEGATE)
3686 if ((LocaleCompare(extension,
"gz") == 0) ||
3687 (LocaleCompare(extension,
"wmz") == 0) ||
3688 (LocaleCompare(extension,
"svgz") == 0))
3690 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3691 if (blob_info->file_info.gzfile != (gzFile) NULL)
3692 blob_info->type=ZipStream;
3696#if defined(MAGICKCORE_BZLIB_DELEGATE)
3697 if (LocaleCompare(extension,
"bz2") == 0)
3699 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3700 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3701 blob_info->type=BZipStream;
3709 blob_info->file_info.file=(FILE *) NULL;
3710 file=open_utf8(filename,flags,P_MODE);
3712 blob_info->file_info.file=fdopen(file,type);
3713 if (blob_info->file_info.file != (FILE *) NULL)
3715 blob_info->type=FileStream;
3716 (void) SetStreamBuffering(image_info,blob_info);
3719 if (IsPathAuthorized(rights,filename) == MagickFalse)
3720 ThrowPolicyException(filename,MagickFalse);
3721 blob_info->status=0;
3722 blob_info->error_number=0;
3723 if (blob_info->type != UndefinedStream)
3724 blob_info->size=GetBlobSize(image);
3727 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3728 return(MagickFalse);
3767#if defined(__cplusplus) || defined(c_plusplus)
3771static size_t PingStream(
const Image *magick_unused(image),
3772 const void *magick_unused(pixels),
const size_t columns)
3774 magick_unreferenced(image);
3775 magick_unreferenced(pixels);
3779#if defined(__cplusplus) || defined(c_plusplus)
3783MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3784 const size_t length,ExceptionInfo *exception)
3799 assert(image_info != (ImageInfo *) NULL);
3800 assert(image_info->signature == MagickCoreSignature);
3801 assert(exception != (ExceptionInfo *) NULL);
3802 if (IsEventLogging() != MagickFalse)
3803 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3804 image_info->filename);
3805 if ((blob == (
const void *) NULL) || (length == 0))
3807 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3808 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3809 return((Image *) NULL);
3811 ping_info=CloneImageInfo(image_info);
3812 ping_info->blob=(
void *) blob;
3813 ping_info->length=length;
3814 ping_info->ping=MagickTrue;
3815 if (*ping_info->magick ==
'\0')
3816 (void) SetImageInfo(ping_info,0,exception);
3817 magick_info=GetMagickInfo(ping_info->magick,exception);
3818 if (magick_info == (
const MagickInfo *) NULL)
3820 (void) ThrowMagickException(exception,GetMagickModule(),
3821 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3823 ping_info=DestroyImageInfo(ping_info);
3824 return((Image *) NULL);
3826 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3829 filename[MagickPathExtent];
3834 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3835 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3836 ping_info->magick,filename);
3837 image=ReadStream(ping_info,&PingStream,exception);
3838 if (image != (Image *) NULL)
3839 (void) DetachBlob(image->blob);
3840 ping_info=DestroyImageInfo(ping_info);
3846 ping_info->blob=(
void *) NULL;
3847 ping_info->length=0;
3848 *ping_info->filename=
'\0';
3849 status=BlobToFile(ping_info->filename,blob,length,exception);
3850 if (status == MagickFalse)
3852 (void) RelinquishUniqueFileResource(ping_info->filename);
3853 ping_info=DestroyImageInfo(ping_info);
3854 return((Image *) NULL);
3856 clone_info=CloneImageInfo(ping_info);
3857 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3858 ping_info->magick,ping_info->filename);
3859 image=ReadStream(clone_info,&PingStream,exception);
3860 if (image != (Image *) NULL)
3868 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3870 (void) CopyMagickString(images->filename,image_info->filename,
3872 (void) CopyMagickString(images->magick_filename,image_info->filename,
3874 (void) CopyMagickString(images->magick,magick_info->name,
3876 images=GetNextImageInList(images);
3879 clone_info=DestroyImageInfo(clone_info);
3880 (void) RelinquishUniqueFileResource(ping_info->filename);
3881 ping_info=DestroyImageInfo(ping_info);
3916MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3919 *magick_restrict blob_info;
3930 assert(image != (Image *) NULL);
3931 assert(image->signature == MagickCoreSignature);
3932 assert(image->blob != (BlobInfo *) NULL);
3933 assert(image->blob->type != UndefinedStream);
3936 assert(data != (
void *) NULL);
3937 blob_info=image->blob;
3939 q=(
unsigned char *) data;
3940 switch (blob_info->type)
3942 case UndefinedStream:
3944 case StandardStream:
3952 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3957 c=getc(blob_info->file_info.file);
3960 *q++=(
unsigned char) c;
3966 c=getc(blob_info->file_info.file);
3969 *q++=(
unsigned char) c;
3975 c=getc(blob_info->file_info.file);
3978 *q++=(
unsigned char) c;
3984 c=getc(blob_info->file_info.file);
3987 *q++=(
unsigned char) c;
3994 if ((count != (ssize_t) length) &&
3995 (ferror(blob_info->file_info.file) != 0))
3996 ThrowBlobException(blob_info);
4001#if defined(MAGICKCORE_ZLIB_DELEGATE)
4012 for (i=0; i < length; i+=(size_t) count)
4014 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
4015 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4028 c=gzgetc(blob_info->file_info.gzfile);
4031 *q++=(
unsigned char) c;
4037 c=gzgetc(blob_info->file_info.gzfile);
4040 *q++=(
unsigned char) c;
4046 c=gzgetc(blob_info->file_info.gzfile);
4049 *q++=(
unsigned char) c;
4055 c=gzgetc(blob_info->file_info.gzfile);
4058 *q++=(
unsigned char) c;
4066 (void) gzerror(blob_info->file_info.gzfile,&status);
4067 if ((count != (ssize_t) length) && (status != Z_OK))
4068 ThrowBlobException(blob_info);
4069 if (blob_info->eof == MagickFalse)
4070 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4077#if defined(MAGICKCORE_BZLIB_DELEGATE)
4084 if (blob_info->status != 0)
4086 for (i=0; i < length; i+=(size_t) count)
4088 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4089 MagickMin(length-i,MagickMaxBufferExtent));
4099 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4100 if ((count != (ssize_t) length) && (status != BZ_OK))
4101 ThrowBlobException(blob_info);
4102 if ((count != (ssize_t) length) && (status == BZ_OK))
4103 blob_info->eof=MagickTrue;
4114 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4116 blob_info->eof=MagickTrue;
4119 p=blob_info->data+blob_info->offset;
4120 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4121 blob_info->length-blob_info->offset);
4122 blob_info->offset+=count;
4123 if (count != (ssize_t) length)
4124 blob_info->eof=MagickTrue;
4125 (void) memcpy(q,p,(
size_t) count);
4130 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4132 count=blob_info->custom_stream->reader(q,length,
4133 blob_info->custom_stream->data);
4136 blob_info->eof=MagickTrue;
4168MagickExport
int ReadBlobByte(Image *image)
4171 *magick_restrict blob_info;
4176 assert(image != (Image *) NULL);
4177 assert(image->signature == MagickCoreSignature);
4178 assert(image->blob != (BlobInfo *) NULL);
4179 assert(image->blob->type != UndefinedStream);
4180 blob_info=image->blob;
4181 switch (blob_info->type)
4183 case StandardStream:
4187 c=getc(blob_info->file_info.file);
4190 if (ferror(blob_info->file_info.file) != 0)
4191 ThrowBlobException(blob_info);
4198 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4200 blob_info->eof=MagickTrue;
4203 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4204 blob_info->offset++;
4215 count=ReadBlob(image,1,buffer);
4248MagickExport
double ReadBlobDouble(Image *image)
4259 quantum.double_value=0.0;
4260 quantum.unsigned_value=ReadBlobLongLong(image);
4261 return(quantum.double_value);
4287MagickExport
float ReadBlobFloat(Image *image)
4298 quantum.float_value=0.0;
4299 quantum.unsigned_value=ReadBlobLong(image);
4300 return(quantum.float_value);
4326MagickExport
unsigned int ReadBlobLong(Image *image)
4340 assert(image != (Image *) NULL);
4341 assert(image->signature == MagickCoreSignature);
4343 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4346 if (image->endian == LSBEndian)
4348 value=(
unsigned int) (*p++);
4349 value|=(
unsigned int) (*p++) << 8;
4350 value|=(
unsigned int) (*p++) << 16;
4351 value|=(
unsigned int) (*p++) << 24;
4354 value=(
unsigned int) (*p++) << 24;
4355 value|=(
unsigned int) (*p++) << 16;
4356 value|=(
unsigned int) (*p++) << 8;
4357 value|=(
unsigned int) (*p++);
4384MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4398 assert(image != (Image *) NULL);
4399 assert(image->signature == MagickCoreSignature);
4401 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4403 return(MagickULLConstant(0));
4404 if (image->endian == LSBEndian)
4406 value=(MagickSizeType) (*p++);
4407 value|=(MagickSizeType) (*p++) << 8;
4408 value|=(MagickSizeType) (*p++) << 16;
4409 value|=(MagickSizeType) (*p++) << 24;
4410 value|=(MagickSizeType) (*p++) << 32;
4411 value|=(MagickSizeType) (*p++) << 40;
4412 value|=(MagickSizeType) (*p++) << 48;
4413 value|=(MagickSizeType) (*p++) << 56;
4416 value=(MagickSizeType) (*p++) << 56;
4417 value|=(MagickSizeType) (*p++) << 48;
4418 value|=(MagickSizeType) (*p++) << 40;
4419 value|=(MagickSizeType) (*p++) << 32;
4420 value|=(MagickSizeType) (*p++) << 24;
4421 value|=(MagickSizeType) (*p++) << 16;
4422 value|=(MagickSizeType) (*p++) << 8;
4423 value|=(MagickSizeType) (*p++);
4450MagickExport
unsigned short ReadBlobShort(Image *image)
4464 assert(image != (Image *) NULL);
4465 assert(image->signature == MagickCoreSignature);
4467 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4469 return((
unsigned short) 0U);
4470 if (image->endian == LSBEndian)
4472 value=(
unsigned short) (*p++);
4473 value|=(
unsigned short) (*p++) << 8;
4476 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4477 value|=(
unsigned short) (*p++);
4504MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4518 assert(image != (Image *) NULL);
4519 assert(image->signature == MagickCoreSignature);
4521 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4524 value=(
unsigned int) (*p++);
4525 value|=(
unsigned int) (*p++) << 8;
4526 value|=(
unsigned int) (*p++) << 16;
4527 value|=(
unsigned int) (*p++) << 24;
4554MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4565 quantum.unsigned_value=ReadBlobLSBLong(image);
4566 return(quantum.signed_value);
4592MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4606 assert(image != (Image *) NULL);
4607 assert(image->signature == MagickCoreSignature);
4609 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4611 return((
unsigned short) 0U);
4612 value=(
unsigned short) (*p++);
4613 value|=(
unsigned short) (*p++) << 8;
4640MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4651 quantum.unsigned_value=ReadBlobLSBShort(image);
4652 return(quantum.signed_value);
4678MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4692 assert(image != (Image *) NULL);
4693 assert(image->signature == MagickCoreSignature);
4695 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4698 value=(
unsigned int) (*p++) << 24;
4699 value|=(
unsigned int) (*p++) << 16;
4700 value|=(
unsigned int) (*p++) << 8;
4701 value|=(
unsigned int) (*p++);
4728MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4742 assert(image != (Image *) NULL);
4743 assert(image->signature == MagickCoreSignature);
4745 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4747 return(MagickULLConstant(0));
4748 value=(MagickSizeType) (*p++) << 56;
4749 value|=(MagickSizeType) (*p++) << 48;
4750 value|=(MagickSizeType) (*p++) << 40;
4751 value|=(MagickSizeType) (*p++) << 32;
4752 value|=(MagickSizeType) (*p++) << 24;
4753 value|=(MagickSizeType) (*p++) << 16;
4754 value|=(MagickSizeType) (*p++) << 8;
4755 value|=(MagickSizeType) (*p++);
4782MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4796 assert(image != (Image *) NULL);
4797 assert(image->signature == MagickCoreSignature);
4799 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4801 return((
unsigned short) 0U);
4802 value=(
unsigned short) ((*p++) << 8);
4803 value|=(
unsigned short) (*p++);
4804 return((
unsigned short) (value & 0xffff));
4830MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4841 quantum.unsigned_value=ReadBlobMSBLong(image);
4842 return(quantum.signed_value);
4868MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4879 quantum.unsigned_value=ReadBlobMSBShort(image);
4880 return(quantum.signed_value);
4906MagickExport
signed int ReadBlobSignedLong(Image *image)
4917 quantum.unsigned_value=ReadBlobLong(image);
4918 return(quantum.signed_value);
4944MagickExport
signed short ReadBlobSignedShort(Image *image)
4955 quantum.unsigned_value=ReadBlobShort(image);
4956 return(quantum.signed_value);
4994MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4995 const size_t length,
void *magick_restrict data,ssize_t *count)
4998 *magick_restrict blob_info;
5000 assert(image != (Image *) NULL);
5001 assert(image->signature == MagickCoreSignature);
5002 assert(image->blob != (BlobInfo *) NULL);
5003 assert(image->blob->type != UndefinedStream);
5004 assert(count != (ssize_t *) NULL);
5005 blob_info=image->blob;
5006 if (blob_info->type != BlobStream)
5008 assert(data != NULL);
5009 *count=ReadBlob(image,length,(
unsigned char *) data);
5012 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5015 blob_info->eof=MagickTrue;
5018 data=blob_info->data+blob_info->offset;
5019 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
5020 blob_info->length-blob_info->offset);
5021 blob_info->offset+=(*count);
5022 if (*count != (ssize_t) length)
5023 blob_info->eof=MagickTrue;
5052MagickExport
char *ReadBlobString(Image *image,
char *
string)
5055 *magick_restrict blob_info;
5063 assert(image != (Image *) NULL);
5064 assert(image->signature == MagickCoreSignature);
5065 assert(image->blob != (BlobInfo *) NULL);
5066 assert(image->blob->type != UndefinedStream);
5067 if (IsEventLogging() != MagickFalse)
5068 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5070 blob_info=image->blob;
5071 switch (blob_info->type)
5073 case UndefinedStream:
5075 case StandardStream:
5078 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5079 if (p == (
char *) NULL)
5081 if (ferror(blob_info->file_info.file) != 0)
5082 ThrowBlobException(blob_info);
5083 return((
char *) NULL);
5090#if defined(MAGICKCORE_ZLIB_DELEGATE)
5091 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5092 if (p == (
char *) NULL)
5095 (void) gzerror(blob_info->file_info.gzfile,&status);
5097 ThrowBlobException(blob_info);
5098 return((
char *) NULL);
5108 c=ReadBlobByte(image);
5111 blob_info->eof=MagickTrue;
5114 string[i++]=(char) c;
5117 }
while (i < (MaxTextExtent-2));
5125 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5127 while ((i > 0) && ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n')))
5129 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5130 return((
char *) NULL);
5157MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5159 assert(blob != (BlobInfo *) NULL);
5160 assert(blob->signature == MagickCoreSignature);
5161 if (IsEventLogging() != MagickFalse)
5162 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5163 LockSemaphoreInfo(blob->semaphore);
5164 blob->reference_count++;
5165 UnlockSemaphoreInfo(blob->semaphore);
5202MagickExport MagickOffsetType SeekBlob(Image *image,
5203 const MagickOffsetType offset,
const int whence)
5206 *magick_restrict blob_info;
5208 assert(image != (Image *) NULL);
5209 assert(image->signature == MagickCoreSignature);
5210 assert(image->blob != (BlobInfo *) NULL);
5211 assert(image->blob->type != UndefinedStream);
5212 if (IsEventLogging() != MagickFalse)
5213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5214 blob_info=image->blob;
5215 switch (blob_info->type)
5217 case UndefinedStream:
5219 case StandardStream:
5224 if ((offset < 0) && (whence == SEEK_SET))
5226 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5228 blob_info->offset=TellBlob(image);
5233#if defined(MAGICKCORE_ZLIB_DELEGATE)
5234 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5237 blob_info->offset=TellBlob(image);
5253 blob_info->offset=offset;
5258 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5259 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5264 if ((blob_info->offset+offset) < 0)
5266 blob_info->offset+=offset;
5271 if (((MagickOffsetType) blob_info->length+offset) < 0)
5273 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5277 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5279 blob_info->eof=MagickFalse;
5286 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5288 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5289 blob_info->custom_stream->data);
5293 return(blob_info->offset);
5321MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5323 assert(image != (
const Image *) NULL);
5324 assert(image->signature == MagickCoreSignature);
5325 if (IsEventLogging() != MagickFalse)
5326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5327 image->blob->exempt=exempt;
5356MagickExport MagickBooleanType SetBlobExtent(Image *image,
5357 const MagickSizeType extent)
5360 *magick_restrict blob_info;
5362 assert(image != (Image *) NULL);
5363 assert(image->signature == MagickCoreSignature);
5364 assert(image->blob != (BlobInfo *) NULL);
5365 assert(image->blob->type != UndefinedStream);
5366 if (IsEventLogging() != MagickFalse)
5367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5368 blob_info=image->blob;
5369 switch (blob_info->type)
5371 case UndefinedStream:
5373 case StandardStream:
5374 return(MagickFalse);
5383 if (extent != (MagickSizeType) ((off_t) extent))
5384 return(MagickFalse);
5385 offset=SeekBlob(image,0,SEEK_END);
5387 return(MagickFalse);
5388 if ((MagickSizeType) offset >= extent)
5390 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5393 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5394 blob_info->file_info.file);
5395#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5396 if (blob_info->synchronize != MagickFalse)
5401 file=fileno(blob_info->file_info.file);
5402 if ((file == -1) || (offset < 0))
5403 return(MagickFalse);
5404 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5407 offset=SeekBlob(image,offset,SEEK_SET);
5409 return(MagickFalse);
5414 return(MagickFalse);
5416 return(MagickFalse);
5418 return(MagickFalse);
5421 if (extent != (MagickSizeType) ((
size_t) extent))
5422 return(MagickFalse);
5423 if (blob_info->mapped != MagickFalse)
5431 (void) UnmapBlob(blob_info->data,blob_info->length);
5432 RelinquishMagickResource(MapResource,blob_info->length);
5433 if (extent != (MagickSizeType) ((off_t) extent))
5434 return(MagickFalse);
5435 offset=SeekBlob(image,0,SEEK_END);
5437 return(MagickFalse);
5438 if ((MagickSizeType) offset >= extent)
5440 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5441 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5442 blob_info->file_info.file);
5443#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5444 if (blob_info->synchronize != MagickFalse)
5449 file=fileno(blob_info->file_info.file);
5450 if ((file == -1) || (offset < 0))
5451 return(MagickFalse);
5452 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5456 offset=SeekBlob(image,offset,SEEK_SET);
5458 return(MagickFalse);
5459 (void) AcquireMagickResource(MapResource,extent);
5460 blob_info->data=(
unsigned char*) MapBlob(fileno(
5461 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5462 blob_info->extent=(size_t) extent;
5463 blob_info->length=(size_t) extent;
5464 (void) SyncBlob(image);
5467 blob_info->extent=(size_t) extent;
5468 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5469 blob_info->extent+1,
sizeof(*blob_info->data));
5470 (void) SyncBlob(image);
5471 if (blob_info->data == (
unsigned char *) NULL)
5473 (void) DetachBlob(blob_info);
5474 return(MagickFalse);
5508MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5511 assert(custom_stream != (CustomStreamInfo *) NULL);
5512 assert(custom_stream->signature == MagickCoreSignature);
5513 custom_stream->data=data;
5541MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5542 CustomStreamHandler reader)
5544 assert(custom_stream != (CustomStreamInfo *) NULL);
5545 assert(custom_stream->signature == MagickCoreSignature);
5546 custom_stream->reader=reader;
5574MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5575 CustomStreamSeeker seeker)
5577 assert(custom_stream != (CustomStreamInfo *) NULL);
5578 assert(custom_stream->signature == MagickCoreSignature);
5579 custom_stream->seeker=seeker;
5607MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5608 CustomStreamTeller teller)
5610 assert(custom_stream != (CustomStreamInfo *) NULL);
5611 assert(custom_stream->signature == MagickCoreSignature);
5612 custom_stream->teller=teller;
5640MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5641 CustomStreamHandler writer)
5643 assert(custom_stream != (CustomStreamInfo *) NULL);
5644 assert(custom_stream->signature == MagickCoreSignature);
5645 custom_stream->writer=writer;
5672static int SyncBlob(
const Image *image)
5675 *magick_restrict blob_info;
5680 assert(image != (Image *) NULL);
5681 assert(image->signature == MagickCoreSignature);
5682 assert(image->blob != (BlobInfo *) NULL);
5683 if (IsEventLogging() != MagickFalse)
5684 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5685 if (EOFBlob(image) != 0)
5687 blob_info=image->blob;
5689 switch (blob_info->type)
5691 case UndefinedStream:
5693 case StandardStream:
5695 if ((blob_info->mode == WriteBlobMode) ||
5696 (blob_info->mode == WriteBinaryBlobMode) ||
5697 (blob_info->mode == AppendBlobMode) ||
5698 (blob_info->mode == AppendBinaryBlobMode))
5699 status=fflush(blob_info->file_info.file);
5705 status=fflush(blob_info->file_info.file);
5710#if defined(MAGICKCORE_ZLIB_DELEGATE)
5711 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5717#if defined(MAGICKCORE_BZLIB_DELEGATE)
5718 status=BZ2_bzflush(blob_info->file_info.bzfile);
5763MagickPrivate
void SyncBlobStream(ImageInfo *image_info,Image *image,
5766 assert(owner != (Image *) NULL);
5767 assert(image != (Image *) NULL);
5768 assert(image->signature == MagickCoreSignature);
5769 if (image->blob == (BlobInfo *) NULL)
5771 if (image->blob->data == (
unsigned char *) NULL)
5773 if (image_info != (ImageInfo *) NULL)
5775 image_info->blob=(
void *) image->blob->data;
5776 image_info->length=image->blob->length;
5778 if ((image == owner) || (owner->blob == (BlobInfo *) NULL))
5780 if (owner->blob == (BlobInfo *) NULL)
5786 owner->blob->data=image->blob->data;
5787 owner->blob->extent=image->blob->extent;
5788 owner->blob->length=image->blob->length;
5789 owner->blob->offset=image->blob->offset;
5790 owner->blob->quantum=image->blob->quantum;
5791 owner->blob->type=BlobStream;
5792 owner->blob->exempt=MagickTrue;
5793 (void) DetachBlob(image->blob);
5818MagickExport MagickOffsetType TellBlob(
const Image *image)
5821 *magick_restrict blob_info;
5826 assert(image != (Image *) NULL);
5827 assert(image->signature == MagickCoreSignature);
5828 assert(image->blob != (BlobInfo *) NULL);
5829 assert(image->blob->type != UndefinedStream);
5830 if (IsEventLogging() != MagickFalse)
5831 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5832 blob_info=image->blob;
5834 switch (blob_info->type)
5836 case UndefinedStream:
5837 case StandardStream:
5841 offset=ftell(blob_info->file_info.file);
5848#if defined(MAGICKCORE_ZLIB_DELEGATE)
5849 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5859 offset=blob_info->offset;
5864 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5865 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5897MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5899#if defined(MAGICKCORE_HAVE_MMAP)
5903 status=munmap(map,length);
5904 return(status == -1 ? MagickFalse : MagickTrue);
5908 return(MagickFalse);
5940MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5944 *magick_restrict blob_info;
5958 assert(image != (Image *) NULL);
5959 assert(image->signature == MagickCoreSignature);
5960 assert(image->blob != (BlobInfo *) NULL);
5961 assert(image->blob->type != UndefinedStream);
5964 assert(data != (
const void *) NULL);
5965 blob_info=image->blob;
5967 p=(
const unsigned char *) data;
5968 q=(
unsigned char *) data;
5969 switch (blob_info->type)
5971 case UndefinedStream:
5973 case StandardStream:
5981 count=(ssize_t) fwrite((
const char *) data,1,length,
5982 blob_info->file_info.file);
5987 c=putc((
int) *p++,blob_info->file_info.file);
5995 c=putc((
int) *p++,blob_info->file_info.file);
6003 c=putc((
int) *p++,blob_info->file_info.file);
6011 c=putc((
int) *p++,blob_info->file_info.file);
6020 if ((count != (ssize_t) length) &&
6021 (ferror(blob_info->file_info.file) != 0))
6022 ThrowBlobException(blob_info);
6027#if defined(MAGICKCORE_ZLIB_DELEGATE)
6038 for (i=0; i < length; i+=(size_t) count)
6040 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
6041 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
6054 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6062 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6070 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6078 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6088 (void) gzerror(blob_info->file_info.gzfile,&status);
6089 if ((count != (ssize_t) length) && (status != Z_OK))
6090 ThrowBlobException(blob_info);
6096#if defined(MAGICKCORE_BZLIB_DELEGATE)
6103 for (i=0; i < length; i+=(size_t) count)
6105 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6106 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6116 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6117 if ((count != (ssize_t) length) && (status != BZ_OK))
6118 ThrowBlobException(blob_info);
6124 count=(ssize_t) blob_info->stream(image,data,length);
6132 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6137 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6138 if (extent >= blob_info->extent)
6140 extent+=blob_info->quantum+length;
6141 blob_info->quantum<<=1;
6142 if (SetBlobExtent(image,extent) == MagickFalse)
6145 q=blob_info->data+blob_info->offset;
6146 (void) memcpy(q,p,length);
6147 blob_info->offset+=(MagickOffsetType) length;
6148 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6149 blob_info->length=(size_t) blob_info->offset;
6150 count=(ssize_t) length;
6155 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6156 count=blob_info->custom_stream->writer((
unsigned char *) data,
6157 length,blob_info->custom_stream->data);
6189MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6192 *magick_restrict blob_info;
6197 assert(image != (Image *) NULL);
6198 assert(image->signature == MagickCoreSignature);
6199 assert(image->blob != (BlobInfo *) NULL);
6200 assert(image->blob->type != UndefinedStream);
6201 blob_info=image->blob;
6203 switch (blob_info->type)
6205 case StandardStream:
6212 c=putc((
int) value,blob_info->file_info.file);
6215 if (ferror(blob_info->file_info.file) != 0)
6216 ThrowBlobException(blob_info);
6224 count=WriteBlobStream(image,1,&value);
6260MagickExport ssize_t WriteBlobPadding(Image *image,
const size_t length,
6273 assert(image != (Image *) NULL);
6274 assert(image->signature == MagickCoreSignature);
6275 assert(image->blob != (BlobInfo *) NULL);
6276 assert(image->blob->type != UndefinedStream);
6279 if (length > (
size_t) MAGICK_SSIZE_MAX)
6284 (void) memset(padding,value,
sizeof(padding));
6286 while ((
size_t) total < length)
6288 extent=MagickMin(
sizeof(padding),length-(
size_t) total);
6289 count=WriteBlob(image,extent,padding);
6293 if (count != (ssize_t) extent)
6324MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6335 quantum.unsigned_value=0U;
6336 quantum.float_value=value;
6337 return(WriteBlobLong(image,quantum.unsigned_value));
6365MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6370 assert(image != (Image *) NULL);
6371 assert(image->signature == MagickCoreSignature);
6372 if (image->endian == LSBEndian)
6374 buffer[0]=(
unsigned char) value;
6375 buffer[1]=(
unsigned char) (value >> 8);
6376 buffer[2]=(
unsigned char) (value >> 16);
6377 buffer[3]=(
unsigned char) (value >> 24);
6378 return(WriteBlobStream(image,4,buffer));
6380 buffer[0]=(
unsigned char) (value >> 24);
6381 buffer[1]=(
unsigned char) (value >> 16);
6382 buffer[2]=(
unsigned char) (value >> 8);
6383 buffer[3]=(
unsigned char) value;
6384 return(WriteBlobStream(image,4,buffer));
6412MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6417 assert(image != (Image *) NULL);
6418 assert(image->signature == MagickCoreSignature);
6419 if (image->endian == LSBEndian)
6421 buffer[0]=(
unsigned char) value;
6422 buffer[1]=(
unsigned char) (value >> 8);
6423 buffer[2]=(
unsigned char) (value >> 16);
6424 buffer[3]=(
unsigned char) (value >> 24);
6425 buffer[4]=(
unsigned char) (value >> 32);
6426 buffer[5]=(
unsigned char) (value >> 40);
6427 buffer[6]=(
unsigned char) (value >> 48);
6428 buffer[7]=(
unsigned char) (value >> 56);
6429 return(WriteBlobStream(image,8,buffer));
6431 buffer[0]=(
unsigned char) (value >> 56);
6432 buffer[1]=(
unsigned char) (value >> 48);
6433 buffer[2]=(
unsigned char) (value >> 40);
6434 buffer[3]=(
unsigned char) (value >> 32);
6435 buffer[4]=(
unsigned char) (value >> 24);
6436 buffer[5]=(
unsigned char) (value >> 16);
6437 buffer[6]=(
unsigned char) (value >> 8);
6438 buffer[7]=(
unsigned char) value;
6439 return(WriteBlobStream(image,8,buffer));
6467MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6472 assert(image != (Image *) NULL);
6473 assert(image->signature == MagickCoreSignature);
6474 if (image->endian == LSBEndian)
6476 buffer[0]=(
unsigned char) value;
6477 buffer[1]=(
unsigned char) (value >> 8);
6478 return(WriteBlobStream(image,2,buffer));
6480 buffer[0]=(
unsigned char) (value >> 8);
6481 buffer[1]=(
unsigned char) value;
6482 return(WriteBlobStream(image,2,buffer));
6510MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6524 assert(image != (Image *) NULL);
6525 assert(image->signature == MagickCoreSignature);
6526 quantum.signed_value=value;
6527 if (image->endian == LSBEndian)
6529 buffer[0]=(
unsigned char) quantum.unsigned_value;
6530 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6531 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6532 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6533 return(WriteBlobStream(image,4,buffer));
6535 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6536 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6537 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6538 buffer[3]=(
unsigned char) quantum.unsigned_value;
6539 return(WriteBlobStream(image,4,buffer));
6567MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6572 assert(image != (Image *) NULL);
6573 assert(image->signature == MagickCoreSignature);
6574 buffer[0]=(
unsigned char) value;
6575 buffer[1]=(
unsigned char) (value >> 8);
6576 buffer[2]=(
unsigned char) (value >> 16);
6577 buffer[3]=(
unsigned char) (value >> 24);
6578 return(WriteBlobStream(image,4,buffer));
6606MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6611 assert(image != (Image *) NULL);
6612 assert(image->signature == MagickCoreSignature);
6613 buffer[0]=(
unsigned char) value;
6614 buffer[1]=(
unsigned char) (value >> 8);
6615 return(WriteBlobStream(image,2,buffer));
6643MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6657 assert(image != (Image *) NULL);
6658 assert(image->signature == MagickCoreSignature);
6659 quantum.signed_value=value;
6660 buffer[0]=(
unsigned char) quantum.unsigned_value;
6661 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6662 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6663 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6664 return(WriteBlobStream(image,4,buffer));
6692MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6693 const signed short value)
6707 assert(image != (Image *) NULL);
6708 assert(image->signature == MagickCoreSignature);
6709 quantum.signed_value=value;
6710 buffer[0]=(
unsigned char) quantum.unsigned_value;
6711 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6712 return(WriteBlobStream(image,2,buffer));
6740MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6745 assert(image != (Image *) NULL);
6746 assert(image->signature == MagickCoreSignature);
6747 buffer[0]=(
unsigned char) (value >> 24);
6748 buffer[1]=(
unsigned char) (value >> 16);
6749 buffer[2]=(
unsigned char) (value >> 8);
6750 buffer[3]=(
unsigned char) value;
6751 return(WriteBlobStream(image,4,buffer));
6779MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6780 const signed short value)
6794 assert(image != (Image *) NULL);
6795 assert(image->signature == MagickCoreSignature);
6796 quantum.signed_value=value;
6797 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6798 buffer[1]=(
unsigned char) quantum.unsigned_value;
6799 return(WriteBlobStream(image,2,buffer));
6827MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6832 assert(image != (Image *) NULL);
6833 assert(image->signature == MagickCoreSignature);
6834 buffer[0]=(
unsigned char) (value >> 8);
6835 buffer[1]=(
unsigned char) value;
6836 return(WriteBlobStream(image,2,buffer));
6864MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6866 assert(image != (Image *) NULL);
6867 assert(image->signature == MagickCoreSignature);
6868 assert(
string != (
const char *) NULL);
6869 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));