47#include "MagickCore/studio.h"
48#include "MagickCore/blob.h"
49#include "MagickCore/blob-private.h"
50#include "MagickCore/cache.h"
51#include "MagickCore/client.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/delegate.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/locale_.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/memory-private.h"
64#include "MagickCore/nt-base-private.h"
65#include "MagickCore/option.h"
66#include "MagickCore/policy.h"
67#include "MagickCore/resource_.h"
68#include "MagickCore/semaphore.h"
69#include "MagickCore/string_.h"
70#include "MagickCore/string-private.h"
71#include "MagickCore/timer-private.h"
72#include "MagickCore/token.h"
73#include "MagickCore/utility.h"
74#include "MagickCore/utility-private.h"
75#if defined(MAGICKCORE_ZLIB_DELEGATE)
78#if defined(MAGICKCORE_BZLIB_DELEGATE)
85#define IsPathAuthorized(rights,filename) \
86 ((IsRightsAuthorized(PathPolicyDomain,rights,filename) != MagickFalse) && \
87 ((IsRightsAuthorized(SystemPolicyDomain,rights,"symlink::follow") != MagickFalse) || \
88 (is_symlink_utf8(filename) == MagickFalse)))
89#define MagickMaxBlobExtent (8*8192)
90#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
91# define MAP_ANONYMOUS MAP_ANON
93#if !defined(MAP_FAILED)
94#define MAP_FAILED ((void *) -1)
98#define _O_BINARY O_BINARY
100#if defined(MAGICKCORE_WINDOWS_SUPPORT)
102# define fsync _commit
105# define MAGICKCORE_HAVE_MMAP 1
106# define mmap(address,length,protection,access,file,offset) \
107 NTMapMemory(address,length,protection,access,file,offset)
110# define munmap(address,length) NTUnmapMemory(address,length)
113# define pclose _pclose
128#if defined(MAGICKCORE_ZLIB_DELEGATE)
133#if defined(MAGICKCORE_BZLIB_DELEGATE)
225 SyncBlob(
const Image *);
249MagickExport CustomStreamInfo *AcquireCustomStreamInfo(
250 ExceptionInfo *magick_unused(exception))
255 magick_unreferenced(exception);
256 custom_stream=(CustomStreamInfo *) AcquireCriticalMemory(
257 sizeof(*custom_stream));
258 (void) memset(custom_stream,0,
sizeof(*custom_stream));
259 custom_stream->signature=MagickCoreSignature;
260 return(custom_stream);
290MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
293 assert(blob_info != (BlobInfo *) NULL);
294 if (IsEventLogging() != MagickFalse)
295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
296 blob_info->length=length;
297 blob_info->extent=length;
298 blob_info->quantum=(size_t) MagickMaxBlobExtent;
300 blob_info->type=BlobStream;
301 blob_info->file_info.file=(FILE *) NULL;
302 blob_info->data=(
unsigned char *) blob;
303 blob_info->mapped=MagickFalse;
331MagickExport
void AttachCustomStream(BlobInfo *blob_info,
332 CustomStreamInfo *custom_stream)
334 assert(blob_info != (BlobInfo *) NULL);
335 assert(custom_stream != (CustomStreamInfo *) NULL);
336 assert(custom_stream->signature == MagickCoreSignature);
337 if (IsEventLogging() != MagickFalse)
338 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
339 blob_info->type=CustomStream;
340 blob_info->custom_stream=custom_stream;
374MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
375 const size_t length,ExceptionInfo *exception)
386 assert(filename != (
const char *) NULL);
387 assert(blob != (
const void *) NULL);
388 if (IsEventLogging() != MagickFalse)
389 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
390 if (*filename ==
'\0')
391 file=AcquireUniqueFileResource(filename);
393 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
396 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
399 for (i=0; i < length; i+=(size_t) count)
401 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
402 MagickMaxBufferExtent));
410 file=close_utf8(file);
411 if ((file == -1) || (i < length))
413 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
450MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
451 const size_t length,ExceptionInfo *exception)
466 assert(image_info != (ImageInfo *) NULL);
467 assert(image_info->signature == MagickCoreSignature);
468 assert(exception != (ExceptionInfo *) NULL);
469 if (IsEventLogging() != MagickFalse)
470 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
471 image_info->filename);
472 if ((blob == (
const void *) NULL) || (length == 0))
474 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
475 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
476 return((Image *) NULL);
478 blob_info=CloneImageInfo(image_info);
479 blob_info->blob=(
void *) blob;
480 blob_info->length=length;
481 if (*blob_info->magick ==
'\0')
482 (void) SetImageInfo(blob_info,0,exception);
483 magick_info=GetMagickInfo(blob_info->magick,exception);
484 if (magick_info == (
const MagickInfo *) NULL)
486 (void) ThrowMagickException(exception,GetMagickModule(),
487 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
489 blob_info=DestroyImageInfo(blob_info);
490 return((Image *) NULL);
492 if (GetMagickBlobSupport(magick_info) != MagickFalse)
495 filename[MagickPathExtent];
500 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
501 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
502 blob_info->magick,filename);
503 image=ReadImage(blob_info,exception);
504 if (image != (Image *) NULL)
505 (void) DetachBlob(image->blob);
506 blob_info=DestroyImageInfo(blob_info);
512 blob_info->blob=(
void *) NULL;
514 *blob_info->filename=
'\0';
515 status=BlobToFile(blob_info->filename,blob,length,exception);
516 if (status == MagickFalse)
518 (void) RelinquishUniqueFileResource(blob_info->filename);
519 blob_info=DestroyImageInfo(blob_info);
520 return((Image *) NULL);
522 clone_info=CloneImageInfo(blob_info);
523 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
524 blob_info->magick,blob_info->filename);
525 image=ReadImage(clone_info,exception);
526 if (image != (Image *) NULL)
534 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
536 (void) CopyMagickString(images->filename,image_info->filename,
538 (void) CopyMagickString(images->magick_filename,image_info->filename,
540 (void) CopyMagickString(images->magick,magick_info->name,
542 images=GetNextImageInList(images);
545 clone_info=DestroyImageInfo(clone_info);
546 (void) RelinquishUniqueFileResource(blob_info->filename);
547 blob_info=DestroyImageInfo(blob_info);
574MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
582 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
583 GetBlobInfo(clone_info);
584 if (blob_info == (BlobInfo *) NULL)
586 semaphore=clone_info->semaphore;
587 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
588 if (blob_info->mapped != MagickFalse)
589 (void) AcquireMagickResource(MapResource,blob_info->length);
590 clone_info->semaphore=semaphore;
591 LockSemaphoreInfo(clone_info->semaphore);
592 clone_info->reference_count=1;
593 UnlockSemaphoreInfo(clone_info->semaphore);
620static inline void ThrowBlobException(BlobInfo *blob_info)
622 if ((blob_info->status == 0) && (errno != 0))
623 blob_info->error_number=errno;
624 blob_info->status=(-1);
627MagickExport MagickBooleanType CloseBlob(Image *image)
630 *magick_restrict blob_info;
638 assert(image != (Image *) NULL);
639 assert(image->signature == MagickCoreSignature);
640 if (IsEventLogging() != MagickFalse)
641 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
642 blob_info=image->blob;
643 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
645 (void) SyncBlob(image);
646 status=blob_info->status;
647 switch (blob_info->type)
649 case UndefinedStream:
655 if (blob_info->synchronize != MagickFalse)
657 status=fflush(blob_info->file_info.file);
659 ThrowBlobException(blob_info);
660 status=fsync(fileno(blob_info->file_info.file));
662 ThrowBlobException(blob_info);
664 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
665 ThrowBlobException(blob_info);
670#if defined(MAGICKCORE_ZLIB_DELEGATE)
672 (void) gzerror(blob_info->file_info.gzfile,&status);
674 ThrowBlobException(blob_info);
680#if defined(MAGICKCORE_BZLIB_DELEGATE)
682 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
684 ThrowBlobException(blob_info);
692 if (blob_info->file_info.file != (FILE *) NULL)
694 if (blob_info->synchronize != MagickFalse)
696 status=fflush(blob_info->file_info.file);
698 ThrowBlobException(blob_info);
699 status=fsync(fileno(blob_info->file_info.file));
701 ThrowBlobException(blob_info);
703 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
704 ThrowBlobException(blob_info);
711 blob_info->size=GetBlobSize(image);
712 image->extent=blob_info->size;
713 blob_info->eof=MagickFalse;
715 blob_info->mode=UndefinedBlobMode;
716 if (blob_info->exempt != MagickFalse)
718 blob_info->type=UndefinedStream;
719 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
721 switch (blob_info->type)
723 case UndefinedStream:
728 if (blob_info->file_info.file != (FILE *) NULL)
730 status=fclose(blob_info->file_info.file);
732 ThrowBlobException(blob_info);
738#if defined(MAGICKCORE_HAVE_PCLOSE)
739 status=pclose(blob_info->file_info.file);
741 ThrowBlobException(blob_info);
747#if defined(MAGICKCORE_ZLIB_DELEGATE)
748 status=gzclose(blob_info->file_info.gzfile);
750 ThrowBlobException(blob_info);
756#if defined(MAGICKCORE_BZLIB_DELEGATE)
757 BZ2_bzclose(blob_info->file_info.bzfile);
765 if (blob_info->file_info.file != (FILE *) NULL)
767 status=fclose(blob_info->file_info.file);
769 ThrowBlobException(blob_info);
776 (void) DetachBlob(blob_info);
777 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
806MagickExport Image *CustomStreamToImage(
const ImageInfo *image_info,
807 ExceptionInfo *exception)
818 assert(image_info != (ImageInfo *) NULL);
819 assert(image_info->signature == MagickCoreSignature);
820 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
821 assert(image_info->custom_stream->signature == MagickCoreSignature);
822 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
823 assert(exception != (ExceptionInfo *) NULL);
824 if (IsEventLogging() != MagickFalse)
825 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
826 image_info->filename);
827 blob_info=CloneImageInfo(image_info);
828 if (*blob_info->magick ==
'\0')
829 (void) SetImageInfo(blob_info,0,exception);
830 magick_info=GetMagickInfo(blob_info->magick,exception);
831 if (magick_info == (
const MagickInfo *) NULL)
833 (void) ThrowMagickException(exception,GetMagickModule(),
834 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
836 blob_info=DestroyImageInfo(blob_info);
837 return((Image *) NULL);
839 image=(Image *) NULL;
840 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
841 (*blob_info->filename !=
'\0'))
844 filename[MagickPathExtent];
850 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
851 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
852 blob_info->magick,filename);
853 image=ReadImage(blob_info,exception);
858 unique[MagickPathExtent];
872 blob_info->custom_stream=(CustomStreamInfo *) NULL;
873 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
875 if (blob == (
unsigned char *) NULL)
877 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
878 image_info->filename);
879 blob_info=DestroyImageInfo(blob_info);
880 return((Image *) NULL);
882 file=AcquireUniqueFileResource(unique);
885 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
886 image_info->filename);
887 blob=(
unsigned char *) RelinquishMagickMemory(blob);
888 blob_info=DestroyImageInfo(blob_info);
889 return((Image *) NULL);
891 clone_info=CloneImageInfo(blob_info);
892 blob_info->file=fdopen(file,
"wb+");
893 if (blob_info->file != (FILE *) NULL)
898 count=(ssize_t) MagickMaxBufferExtent;
899 while (count == (ssize_t) MagickMaxBufferExtent)
901 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
902 image_info->custom_stream->data);
903 count=(ssize_t) write(file,(
const char *) blob,(
size_t) count);
905 (void) fclose(blob_info->file);
906 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
907 "%s:%s",blob_info->magick,unique);
908 image=ReadImage(clone_info,exception);
909 if (image != (Image *) NULL)
917 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
919 (void) CopyMagickString(images->filename,image_info->filename,
921 (void) CopyMagickString(images->magick_filename,
922 image_info->filename,MagickPathExtent);
923 (void) CopyMagickString(images->magick,magick_info->name,
925 images=GetNextImageInList(images);
929 clone_info=DestroyImageInfo(clone_info);
930 blob=(
unsigned char *) RelinquishMagickMemory(blob);
931 (void) RelinquishUniqueFileResource(unique);
933 blob_info=DestroyImageInfo(blob_info);
934 if (image != (Image *) NULL)
935 if (CloseBlob(image) == MagickFalse)
936 image=DestroyImageList(image);
962MagickExport
void DestroyBlob(Image *image)
965 *magick_restrict blob_info;
970 assert(image != (Image *) NULL);
971 assert(image->signature == MagickCoreSignature);
972 assert(image->blob != (BlobInfo *) NULL);
973 assert(image->blob->signature == MagickCoreSignature);
974 if (IsEventLogging() != MagickFalse)
975 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
976 blob_info=image->blob;
978 LockSemaphoreInfo(blob_info->semaphore);
979 blob_info->reference_count--;
980 assert(blob_info->reference_count >= 0);
981 if (blob_info->reference_count == 0)
983 UnlockSemaphoreInfo(blob_info->semaphore);
984 if (destroy == MagickFalse)
986 image->blob=(BlobInfo *) NULL;
989 (void) CloseBlob(image);
990 if (blob_info->mapped != MagickFalse)
992 (void) UnmapBlob(blob_info->data,blob_info->length);
993 RelinquishMagickResource(MapResource,blob_info->length);
996 RelinquishSemaphoreInfo(&blob_info->semaphore);
997 blob_info->signature=(~MagickCoreSignature);
998 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
1024MagickExport CustomStreamInfo *DestroyCustomStreamInfo(
1025 CustomStreamInfo *custom_stream)
1027 assert(custom_stream != (CustomStreamInfo *) NULL);
1028 assert(custom_stream->signature == MagickCoreSignature);
1029 if (IsEventLogging() != MagickFalse)
1030 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1031 custom_stream->signature=(~MagickCoreSignature);
1032 custom_stream=(CustomStreamInfo *) RelinquishMagickMemory(custom_stream);
1033 return(custom_stream);
1058MagickExport
void *DetachBlob(BlobInfo *blob_info)
1063 assert(blob_info != (BlobInfo *) NULL);
1064 if (IsEventLogging() != MagickFalse)
1065 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1066 if (blob_info->mapped != MagickFalse)
1068 (void) UnmapBlob(blob_info->data,blob_info->length);
1069 blob_info->data=NULL;
1070 RelinquishMagickResource(MapResource,blob_info->length);
1072 blob_info->mapped=MagickFalse;
1073 blob_info->length=0;
1078 blob_info->offset=0;
1079 blob_info->mode=UndefinedBlobMode;
1080 blob_info->eof=MagickFalse;
1082 blob_info->exempt=MagickFalse;
1083 blob_info->type=UndefinedStream;
1084 blob_info->file_info.file=(FILE *) NULL;
1085 data=blob_info->data;
1086 blob_info->data=(
unsigned char *) NULL;
1087 blob_info->stream=(StreamHandler) NULL;
1088 blob_info->custom_stream=(CustomStreamInfo *) NULL;
1116MagickExport
void DisassociateBlob(Image *image)
1119 *magick_restrict blob_info,
1125 assert(image != (Image *) NULL);
1126 assert(image->signature == MagickCoreSignature);
1127 assert(image->blob != (BlobInfo *) NULL);
1128 assert(image->blob->signature == MagickCoreSignature);
1129 if (IsEventLogging() != MagickFalse)
1130 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1131 blob_info=image->blob;
1133 LockSemaphoreInfo(blob_info->semaphore);
1134 assert(blob_info->reference_count >= 0);
1135 if (blob_info->reference_count > 1)
1137 UnlockSemaphoreInfo(blob_info->semaphore);
1138 if (clone == MagickFalse)
1140 clone_info=CloneBlobInfo(blob_info);
1142 image->blob=clone_info;
1170MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1171 const MagickSizeType length)
1183 buffer[MagickMinBufferExtent >> 1];
1185 assert(image != (Image *) NULL);
1186 assert(image->signature == MagickCoreSignature);
1187 if (length != (MagickSizeType) ((MagickOffsetType) length))
1188 return(MagickFalse);
1190 for (i=0; i < length; i+=(MagickSizeType) count)
1192 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1193 (void) ReadBlobStream(image,quantum,buffer,&count);
1201 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1228MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
1230 assert(image != (Image *) NULL);
1231 assert(image->signature == MagickCoreSignature);
1232 assert(duplicate != (Image *) NULL);
1233 assert(duplicate->signature == MagickCoreSignature);
1234 if (IsEventLogging() != MagickFalse)
1235 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1237 image->blob=ReferenceBlob(duplicate->blob);
1263MagickExport
int EOFBlob(
const Image *image)
1266 *magick_restrict blob_info;
1268 assert(image != (Image *) NULL);
1269 assert(image->signature == MagickCoreSignature);
1270 assert(image->blob != (BlobInfo *) NULL);
1271 assert(image->blob->type != UndefinedStream);
1272 if (IsEventLogging() != MagickFalse)
1273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1274 blob_info=image->blob;
1275 switch (blob_info->type)
1277 case UndefinedStream:
1278 case StandardStream:
1283 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1289#if defined(MAGICKCORE_ZLIB_DELEGATE)
1290 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1297#if defined(MAGICKCORE_BZLIB_DELEGATE)
1302 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1303 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1309 blob_info->eof=MagickFalse;
1317 return((
int) blob_info->eof);
1343MagickExport
int ErrorBlob(
const Image *image)
1346 *magick_restrict blob_info;
1348 assert(image != (Image *) NULL);
1349 assert(image->signature == MagickCoreSignature);
1350 assert(image->blob != (BlobInfo *) NULL);
1351 assert(image->blob->type != UndefinedStream);
1352 if (IsEventLogging() != MagickFalse)
1353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1354 blob_info=image->blob;
1355 switch (blob_info->type)
1357 case UndefinedStream:
1358 case StandardStream:
1363 blob_info->error=ferror(blob_info->file_info.file);
1368#if defined(MAGICKCORE_ZLIB_DELEGATE)
1369 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1375#if defined(MAGICKCORE_BZLIB_DELEGATE)
1376 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1390 return(blob_info->error);
1428MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1429 size_t *length,ExceptionInfo *exception)
1455 assert(filename != (
const char *) NULL);
1456 assert(exception != (ExceptionInfo *) NULL);
1457 assert(exception->signature == MagickCoreSignature);
1458 if (IsEventLogging() != MagickFalse)
1459 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1461 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1464 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1465 "NotAuthorized",
"`%s'",filename);
1469 if (LocaleCompare(filename,
"-") != 0)
1472 flags = O_RDONLY | O_BINARY;
1474 status=GetPathAttributes(filename,&attributes);
1475 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1477 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1480 file=open_utf8(filename,flags,0);
1484 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1487 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1489 file=close_utf8(file)-1;
1491 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1492 "NotAuthorized",
"`%s'",filename);
1495 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1497 if ((file == fileno(stdin)) || (offset < 0) ||
1498 (offset != (MagickOffsetType) ((ssize_t) offset)))
1509 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1510 quantum=(size_t) MagickMaxBufferExtent;
1511 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1512 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1513 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1514 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1516 count=read(file,blob+i,quantum);
1523 if (~i < ((
size_t) count+quantum+1))
1525 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1528 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1529 quantum+1,
sizeof(*blob));
1530 if ((i+(
size_t) count) >= extent)
1533 if (LocaleCompare(filename,
"-") != 0)
1534 file=close_utf8(file);
1535 if (blob == (
unsigned char *) NULL)
1537 (void) ThrowMagickException(exception,GetMagickModule(),
1538 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1543 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1544 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1547 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1551 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1552 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1553 blob=(
unsigned char *) NULL;
1554 if (~(*length) >= (MagickPathExtent-1))
1555 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1557 if (blob == (
unsigned char *) NULL)
1559 file=close_utf8(file);
1560 (void) ThrowMagickException(exception,GetMagickModule(),
1561 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1564 map=MapBlob(file,ReadMode,0,*length);
1565 if (map != (
unsigned char *) NULL)
1567 (void) memcpy(blob,map,*length);
1568 (void) UnmapBlob(map,*length);
1572 (void) lseek(file,0,SEEK_SET);
1573 for (i=0; i < *length; i+=(size_t) count)
1575 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1576 MagickMaxBufferExtent));
1586 file=close_utf8(file)-1;
1587 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1588 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1593 if (LocaleCompare(filename,
"-") != 0)
1594 file=close_utf8(file);
1597 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1598 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1628static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1629 const void *magick_restrict data)
1632 *magick_restrict blob_info;
1640 assert(image->blob != (BlobInfo *) NULL);
1641 assert(image->blob->type != UndefinedStream);
1642 assert(data != NULL);
1643 blob_info=image->blob;
1644 if (blob_info->type != BlobStream)
1645 return(WriteBlob(image,length,(
const unsigned char *) data));
1646 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1651 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1652 if (extent >= blob_info->extent)
1654 extent+=blob_info->quantum+length;
1655 blob_info->quantum<<=1;
1656 if (SetBlobExtent(image,extent) == MagickFalse)
1659 q=blob_info->data+blob_info->offset;
1660 (void) memcpy(q,data,length);
1661 blob_info->offset+=(MagickOffsetType) length;
1662 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1663 blob_info->length=(size_t) blob_info->offset;
1664 return((ssize_t) length);
1667MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1668 ExceptionInfo *exception)
1686 assert(image != (
const Image *) NULL);
1687 assert(image->signature == MagickCoreSignature);
1688 assert(filename != (
const char *) NULL);
1689 if (IsEventLogging() != MagickFalse)
1690 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1691 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1694 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1695 "NotAuthorized",
"`%s'",filename);
1696 return(MagickFalse);
1699 if (LocaleCompare(filename,
"-") != 0)
1702 flags = O_RDONLY | O_BINARY;
1704 file=open_utf8(filename,flags,0);
1708 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1709 return(MagickFalse);
1711 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1713 file=close_utf8(file);
1715 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1716 "NotAuthorized",
"`%s'",filename);
1717 return(MagickFalse);
1719 quantum=(size_t) MagickMaxBufferExtent;
1720 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1721 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1722 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1723 if (blob == (
unsigned char *) NULL)
1725 file=close_utf8(file);
1726 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1728 return(MagickFalse);
1732 count=read(file,blob,quantum);
1739 length=(size_t) count;
1740 count=WriteBlobStream(image,length,blob);
1741 if (count != (ssize_t) length)
1743 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1747 file=close_utf8(file);
1749 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1750 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1777MagickExport MagickBooleanType GetBlobError(
const Image *image)
1779 assert(image != (
const Image *) NULL);
1780 assert(image->signature == MagickCoreSignature);
1781 if (IsEventLogging() != MagickFalse)
1782 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1783 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1784 errno=image->blob->error_number;
1785 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1810MagickExport FILE *GetBlobFileHandle(
const Image *image)
1812 assert(image != (
const Image *) NULL);
1813 assert(image->signature == MagickCoreSignature);
1814 return(image->blob->file_info.file);
1839MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1841 assert(blob_info != (BlobInfo *) NULL);
1842 (void) memset(blob_info,0,
sizeof(*blob_info));
1843 blob_info->type=UndefinedStream;
1844 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1845 blob_info->properties.st_mtime=GetMagickTime();
1846 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1847 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1848 blob_info->reference_count=1;
1849 blob_info->semaphore=AcquireSemaphoreInfo();
1850 blob_info->signature=MagickCoreSignature;
1875MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1877 assert(image != (Image *) NULL);
1878 assert(image->signature == MagickCoreSignature);
1879 if (IsEventLogging() != MagickFalse)
1880 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1881 return(&image->blob->properties);
1907MagickExport MagickSizeType GetBlobSize(
const Image *image)
1910 *magick_restrict blob_info;
1915 assert(image != (Image *) NULL);
1916 assert(image->signature == MagickCoreSignature);
1917 assert(image->blob != (BlobInfo *) NULL);
1918 if (IsEventLogging() != MagickFalse)
1919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1920 blob_info=image->blob;
1922 switch (blob_info->type)
1924 case UndefinedStream:
1925 case StandardStream:
1927 extent=blob_info->size;
1935 extent=(MagickSizeType) blob_info->properties.st_size;
1937 extent=blob_info->size;
1938 file_descriptor=fileno(blob_info->file_info.file);
1939 if (file_descriptor == -1)
1941 if (fstat(file_descriptor,&blob_info->properties) == 0)
1942 extent=(MagickSizeType) blob_info->properties.st_size;
1947 extent=blob_info->size;
1956 status=GetPathAttributes(image->filename,&blob_info->properties);
1957 if (status != MagickFalse)
1958 extent=(MagickSizeType) blob_info->properties.st_size;
1965 extent=(MagickSizeType) blob_info->length;
1970 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1971 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1976 offset=blob_info->custom_stream->teller(
1977 blob_info->custom_stream->data);
1978 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1979 blob_info->custom_stream->data);
1980 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1981 blob_info->custom_stream->data);
2011MagickExport
void *GetBlobStreamData(
const Image *image)
2013 assert(image != (
const Image *) NULL);
2014 assert(image->signature == MagickCoreSignature);
2015 return(image->blob->data);
2040MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2042 assert(image != (
const Image *) NULL);
2043 assert(image->signature == MagickCoreSignature);
2044 if (IsEventLogging() != MagickFalse)
2045 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2046 return(image->blob->stream);
2082MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2083 Image *image,
size_t *length,ExceptionInfo *exception)
2097 assert(image_info != (
const ImageInfo *) NULL);
2098 assert(image_info->signature == MagickCoreSignature);
2099 assert(image != (Image *) NULL);
2100 assert(image->signature == MagickCoreSignature);
2101 assert(exception != (ExceptionInfo *) NULL);
2102 assert(exception->signature == MagickCoreSignature);
2103 if (IsEventLogging() != MagickFalse)
2104 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2105 image_info->filename);
2107 blob=(
unsigned char *) NULL;
2108 blob_info=CloneImageInfo(image_info);
2109 blob_info->adjoin=MagickFalse;
2110 (void) SetImageInfo(blob_info,1,exception);
2111 if (*blob_info->magick !=
'\0')
2112 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2113 magick_info=GetMagickInfo(image->magick,exception);
2114 if (magick_info == (
const MagickInfo *) NULL)
2116 (void) ThrowMagickException(exception,GetMagickModule(),
2117 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2119 blob_info=DestroyImageInfo(blob_info);
2122 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2123 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2128 blob_info->length=0;
2129 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2130 sizeof(
unsigned char));
2131 if (blob_info->blob == NULL)
2132 (void) ThrowMagickException(exception,GetMagickModule(),
2133 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2136 (void) CloseBlob(image);
2137 image->blob->exempt=MagickTrue;
2138 image->blob->extent=0;
2139 *image->filename=
'\0';
2140 status=WriteImage(blob_info,image,exception);
2141 *length=image->blob->length;
2142 blob=DetachBlob(image->blob);
2143 if (blob != (
void *) NULL)
2145 if (status == MagickFalse)
2146 blob=RelinquishMagickMemory(blob);
2148 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2150 else if ((status == MagickFalse) && (image->blob->extent == 0))
2151 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2157 unique[MagickPathExtent];
2165 file=AcquireUniqueFileResource(unique);
2168 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2169 image_info->filename);
2173 blob_info->file=fdopen(file,
"wb");
2174 if (blob_info->file != (FILE *) NULL)
2176 (void) FormatLocaleString(image->filename,MagickPathExtent,
2177 "%s:%s",image->magick,unique);
2178 status=WriteImage(blob_info,image,exception);
2179 (void) fclose(blob_info->file);
2180 if (status != MagickFalse)
2181 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2183 (void) RelinquishUniqueFileResource(unique);
2186 blob_info=DestroyImageInfo(blob_info);
2218MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2219 ExceptionInfo *exception)
2231 assert(image_info != (
const ImageInfo *) NULL);
2232 assert(image_info->signature == MagickCoreSignature);
2233 assert(image != (Image *) NULL);
2234 assert(image->signature == MagickCoreSignature);
2235 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2236 assert(image_info->custom_stream->signature == MagickCoreSignature);
2237 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2238 assert(exception != (ExceptionInfo *) NULL);
2239 if (IsEventLogging() != MagickFalse)
2240 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2241 image_info->filename);
2242 clone_info=CloneImageInfo(image_info);
2243 clone_info->adjoin=MagickFalse;
2244 (void) SetImageInfo(clone_info,1,exception);
2245 if (*clone_info->magick !=
'\0')
2246 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2247 magick_info=GetMagickInfo(image->magick,exception);
2248 if (magick_info == (
const MagickInfo *) NULL)
2250 (void) ThrowMagickException(exception,GetMagickModule(),
2251 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2253 clone_info=DestroyImageInfo(clone_info);
2256 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2257 blob_support=GetMagickBlobSupport(magick_info);
2258 if ((blob_support != MagickFalse) &&
2259 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2261 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2262 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2263 blob_support=MagickFalse;
2265 if (blob_support != MagickFalse)
2270 (void) CloseBlob(image);
2271 *image->filename=
'\0';
2272 (void) WriteImage(clone_info,image,exception);
2277 unique[MagickPathExtent];
2288 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2289 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2291 if (blob == (
unsigned char *) NULL)
2293 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2294 image_info->filename);
2295 clone_info=DestroyImageInfo(clone_info);
2298 file=AcquireUniqueFileResource(unique);
2301 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2302 image_info->filename);
2303 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2304 clone_info=DestroyImageInfo(clone_info);
2307 clone_info->file=fdopen(file,
"wb+");
2308 if (clone_info->file != (FILE *) NULL)
2313 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2314 image->magick,unique);
2315 status=WriteImage(clone_info,image,exception);
2316 if (status != MagickFalse)
2318 (void) fseek(clone_info->file,0,SEEK_SET);
2319 count=(ssize_t) MagickMaxBufferExtent;
2320 while (count == (ssize_t) MagickMaxBufferExtent)
2322 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2324 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2325 image_info->custom_stream->data);
2328 (void) fclose(clone_info->file);
2330 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2331 (void) RelinquishUniqueFileResource(unique);
2333 clone_info=DestroyImageInfo(clone_info);
2364MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2365 ExceptionInfo *exception)
2389 assert(image != (Image *) NULL);
2390 assert(image->signature == MagickCoreSignature);
2391 assert(image->blob != (BlobInfo *) NULL);
2392 assert(image->blob->type != UndefinedStream);
2393 assert(filename != (
const char *) NULL);
2394 if (IsEventLogging() != MagickFalse)
2395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2396 if (*filename ==
'\0')
2397 file=AcquireUniqueFileResource(filename);
2399 if (LocaleCompare(filename,
"-") == 0)
2400 file=fileno(stdout);
2402 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2405 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2406 return(MagickFalse);
2408 quantum=(size_t) MagickMaxBufferExtent;
2409 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2410 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2411 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2412 if (buffer == (
unsigned char *) NULL)
2414 file=close_utf8(file)-1;
2415 (void) ThrowMagickException(exception,GetMagickModule(),
2416 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2417 return(MagickFalse);
2420 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2421 for (i=0; count > 0; )
2423 length=(size_t) count;
2424 for (i=0; i < length; i+=(size_t) count)
2426 count=write(file,p+i,(
size_t) (length-i));
2436 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2438 if (LocaleCompare(filename,
"-") != 0)
2439 file=close_utf8(file);
2440 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2441 if ((file == -1) || (i < length))
2444 file=close_utf8(file);
2445 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2446 return(MagickFalse);
2486MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2487 size_t *length,ExceptionInfo *exception)
2501 assert(image_info != (
const ImageInfo *) NULL);
2502 assert(image_info->signature == MagickCoreSignature);
2503 assert(images != (Image *) NULL);
2504 assert(images->signature == MagickCoreSignature);
2505 assert(exception != (ExceptionInfo *) NULL);
2506 if (IsEventLogging() != MagickFalse)
2507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2508 image_info->filename);
2510 blob=(
unsigned char *) NULL;
2511 blob_info=CloneImageInfo(image_info);
2512 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2514 if (*blob_info->magick !=
'\0')
2515 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2516 magick_info=GetMagickInfo(images->magick,exception);
2517 if (magick_info == (
const MagickInfo *) NULL)
2519 (void) ThrowMagickException(exception,GetMagickModule(),
2520 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2522 blob_info=DestroyImageInfo(blob_info);
2525 if (GetMagickAdjoin(magick_info) == MagickFalse)
2527 blob_info=DestroyImageInfo(blob_info);
2528 return(ImageToBlob(image_info,images,length,exception));
2530 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2531 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2536 blob_info->length=0;
2537 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2538 sizeof(
unsigned char));
2539 if (blob_info->blob == (
void *) NULL)
2540 (void) ThrowMagickException(exception,GetMagickModule(),
2541 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2544 (void) CloseBlob(images);
2545 images->blob->exempt=MagickTrue;
2546 images->blob->extent=0;
2547 *images->filename=
'\0';
2548 status=WriteImages(blob_info,images,images->filename,exception);
2549 *length=images->blob->length;
2550 blob=DetachBlob(images->blob);
2551 if (blob != (
void *) NULL)
2553 if (status == MagickFalse)
2554 blob=RelinquishMagickMemory(blob);
2556 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2558 else if ((status == MagickFalse) && (images->blob->extent == 0))
2559 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2565 filename[MagickPathExtent],
2566 unique[MagickPathExtent];
2574 file=AcquireUniqueFileResource(unique);
2577 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2578 image_info->filename);
2582 blob_info->file=fdopen(file,
"wb");
2583 if (blob_info->file != (FILE *) NULL)
2585 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2586 images->magick,unique);
2587 status=WriteImages(blob_info,images,filename,exception);
2588 (void) fclose(blob_info->file);
2589 if (status != MagickFalse)
2590 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2592 (void) RelinquishUniqueFileResource(unique);
2595 blob_info=DestroyImageInfo(blob_info);
2627MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2628 Image *images,ExceptionInfo *exception)
2640 assert(image_info != (
const ImageInfo *) NULL);
2641 assert(image_info->signature == MagickCoreSignature);
2642 assert(images != (Image *) NULL);
2643 assert(images->signature == MagickCoreSignature);
2644 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2645 assert(image_info->custom_stream->signature == MagickCoreSignature);
2646 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2647 assert(exception != (ExceptionInfo *) NULL);
2648 if (IsEventLogging() != MagickFalse)
2649 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2650 image_info->filename);
2651 clone_info=CloneImageInfo(image_info);
2652 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2654 if (*clone_info->magick !=
'\0')
2655 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2656 magick_info=GetMagickInfo(images->magick,exception);
2657 if (magick_info == (
const MagickInfo *) NULL)
2659 (void) ThrowMagickException(exception,GetMagickModule(),
2660 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2662 clone_info=DestroyImageInfo(clone_info);
2665 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2666 blob_support=GetMagickBlobSupport(magick_info);
2667 if ((blob_support != MagickFalse) &&
2668 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2670 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2671 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2672 blob_support=MagickFalse;
2674 if (blob_support != MagickFalse)
2679 (void) CloseBlob(images);
2680 *images->filename=
'\0';
2681 (void) WriteImages(clone_info,images,images->filename,exception);
2686 filename[MagickPathExtent],
2687 unique[MagickPathExtent];
2698 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2699 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2701 if (blob == (
unsigned char *) NULL)
2703 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2704 image_info->filename);
2705 clone_info=DestroyImageInfo(clone_info);
2708 file=AcquireUniqueFileResource(unique);
2711 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2712 image_info->filename);
2713 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2714 clone_info=DestroyImageInfo(clone_info);
2717 clone_info->file=fdopen(file,
"wb+");
2718 if (clone_info->file != (FILE *) NULL)
2723 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2724 images->magick,unique);
2725 status=WriteImages(clone_info,images,filename,exception);
2726 if (status != MagickFalse)
2728 (void) fseek(clone_info->file,0,SEEK_SET);
2729 count=(ssize_t) MagickMaxBufferExtent;
2730 while (count == (ssize_t) MagickMaxBufferExtent)
2732 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2734 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2735 image_info->custom_stream->data);
2738 (void) fclose(clone_info->file);
2740 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2741 (void) RelinquishUniqueFileResource(unique);
2743 clone_info=DestroyImageInfo(clone_info);
2779MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2780 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2783 filename[MagickPathExtent];
2812 assert(image_info != (ImageInfo *) NULL);
2813 assert(image_info->signature == MagickCoreSignature);
2814 assert(image != (Image *) NULL);
2815 assert(image->signature == MagickCoreSignature);
2816 assert(inject_image != (Image *) NULL);
2817 assert(inject_image->signature == MagickCoreSignature);
2818 assert(exception != (ExceptionInfo *) NULL);
2819 if (IsEventLogging() != MagickFalse)
2820 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2821 unique_file=(FILE *) NULL;
2822 file=AcquireUniqueFileResource(filename);
2824 unique_file=fdopen(file,
"wb");
2825 if ((file == -1) || (unique_file == (FILE *) NULL))
2827 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2828 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2830 return(MagickFalse);
2832 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2833 if (byte_image == (Image *) NULL)
2835 (void) fclose(unique_file);
2836 (void) RelinquishUniqueFileResource(filename);
2837 return(MagickFalse);
2839 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2841 DestroyBlob(byte_image);
2842 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2843 write_info=CloneImageInfo(image_info);
2844 SetImageInfoFile(write_info,unique_file);
2845 status=WriteImage(write_info,byte_image,exception);
2846 write_info=DestroyImageInfo(write_info);
2847 byte_image=DestroyImage(byte_image);
2848 (void) fclose(unique_file);
2849 if (status == MagickFalse)
2851 (void) RelinquishUniqueFileResource(filename);
2852 return(MagickFalse);
2857 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2860 (void) RelinquishUniqueFileResource(filename);
2861 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2862 image_info->filename);
2863 return(MagickFalse);
2865 quantum=(size_t) MagickMaxBufferExtent;
2866 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2867 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2868 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2869 if (buffer == (
unsigned char *) NULL)
2871 (void) RelinquishUniqueFileResource(filename);
2872 file=close_utf8(file);
2873 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2878 ssize_t count = read(file,buffer,quantum);
2885 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2888 file=close_utf8(file);
2890 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2891 (void) RelinquishUniqueFileResource(filename);
2892 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2918MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2920 assert(image != (
const Image *) NULL);
2921 assert(image->signature == MagickCoreSignature);
2922 if (IsEventLogging() != MagickFalse)
2923 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2924 return(image->blob->exempt);
2949MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2952 *magick_restrict blob_info;
2954 assert(image != (
const Image *) NULL);
2955 assert(image->signature == MagickCoreSignature);
2956 if (IsEventLogging() != MagickFalse)
2957 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2958 blob_info=image->blob;
2959 switch (blob_info->type)
2968 if (blob_info->file_info.file == (FILE *) NULL)
2969 return(MagickFalse);
2970 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2971 return(status == -1 ? MagickFalse : MagickTrue);
2975#if defined(MAGICKCORE_ZLIB_DELEGATE)
2979 if (blob_info->file_info.gzfile == (gzFile) NULL)
2980 return(MagickFalse);
2981 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2982 return(offset < 0 ? MagickFalse : MagickTrue);
2987 case UndefinedStream:
2991 case StandardStream:
2995 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
2996 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3003 return(MagickFalse);
3028MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3030 assert(image != (
const Image *) NULL);
3031 assert(image->signature == MagickCoreSignature);
3032 if (IsEventLogging() != MagickFalse)
3033 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3034 return(image->blob->temporary);
3066MagickExport
void *MapBlob(
int file,
const MapMode mode,
3067 const MagickOffsetType offset,
const size_t length)
3069#if defined(MAGICKCORE_HAVE_MMAP)
3082#if defined(MAP_ANONYMOUS)
3083 flags|=MAP_ANONYMOUS;
3092 protection=PROT_READ;
3098 protection=PROT_WRITE;
3104 protection=PROT_READ | PROT_WRITE;
3109#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3110 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3112 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3113 if (map == MAP_FAILED)
3114 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3116 if (map == MAP_FAILED)
3153MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3162 assert(buffer != (
unsigned char *) NULL);
3169 *buffer++=(
unsigned char) c;
3173 *buffer++=(
unsigned char) c;
3203MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3211 assert(p != (
unsigned char *) NULL);
3218 *p++=(
unsigned char) c;
3254static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3255 const BlobInfo *blob_info)
3266 size=MagickMinBufferExtent;
3267 option=GetImageOption(image_info,
"stream:buffer-size");
3268 if (option != (
const char *) NULL)
3269 size=StringToUnsignedLong(option);
3270 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3271 _IONBF : _IOFBF,size);
3272 return(status == 0 ? MagickTrue : MagickFalse);
3275#if defined(MAGICKCORE_ZLIB_DELEGATE)
3276static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3278#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3279 return(gzopen(path,mode));
3287 path_wide=NTCreateWidePath(path);
3288 if (path_wide == (
wchar_t *) NULL)
3289 return((gzFile) NULL);
3290 file=gzopen_w(path_wide,mode);
3291 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3297MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3298 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3301 *magick_restrict blob_info;
3304 extension[MagickPathExtent],
3305 filename[MagickPathExtent];
3319 assert(image_info != (ImageInfo *) NULL);
3320 assert(image_info->signature == MagickCoreSignature);
3321 assert(image != (Image *) NULL);
3322 assert(image->signature == MagickCoreSignature);
3323 if (IsEventLogging() != MagickFalse)
3324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3325 image_info->filename);
3326 blob_info=image->blob;
3327 if (image_info->blob != (
void *) NULL)
3329 if (image_info->stream != (StreamHandler) NULL)
3330 blob_info->stream=(StreamHandler) image_info->stream;
3331 AttachBlob(blob_info,image_info->blob,image_info->length);
3334 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3335 (*image->filename ==
'\0'))
3337 blob_info->type=CustomStream;
3338 blob_info->custom_stream=image_info->custom_stream;
3341 (void) DetachBlob(blob_info);
3342 blob_info->mode=mode;
3351 case ReadBinaryBlobMode:
3353 flags=O_RDONLY | O_BINARY;
3359 flags=O_WRONLY | O_CREAT | O_TRUNC;
3363 case WriteBinaryBlobMode:
3365 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3369 case AppendBlobMode:
3371 flags=O_WRONLY | O_CREAT | O_APPEND;
3375 case AppendBinaryBlobMode:
3377 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3389 blob_info->synchronize=image_info->synchronize;
3390 if (image_info->stream != (StreamHandler) NULL)
3392 blob_info->stream=image_info->stream;
3395 blob_info->type=FifoStream;
3403 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3404 rights=ReadPolicyRights;
3406 rights=WritePolicyRights;
3407 if (IsPathAuthorized(rights,filename) == MagickFalse)
3410 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3411 "NotAuthorized",
"`%s'",filename);
3412 return(MagickFalse);
3414 if ((LocaleCompare(filename,
"-") == 0) ||
3415 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3417 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3418#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3419 if (strchr(type,
'b') != (
char *) NULL)
3420 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3422 blob_info->type=StandardStream;
3423 blob_info->exempt=MagickTrue;
3424 return(SetStreamBuffering(image_info,blob_info));
3426 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3427 (IsGeometry(filename+3) != MagickFalse))
3434 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3435 if (blob_info->file_info.file == (FILE *) NULL)
3437 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3438 return(MagickFalse);
3440#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3441 if (strchr(type,
'b') != (
char *) NULL)
3442 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3444 blob_info->type=FileStream;
3445 blob_info->exempt=MagickTrue;
3446 return(SetStreamBuffering(image_info,blob_info));
3448#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3449 if (*filename ==
'|')
3452 fileMode[MagickPathExtent],
3460 (void) signal(SIGPIPE,SIG_IGN);
3464 sanitize_command=SanitizeString(filename+1);
3465 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3466 sanitize_command=DestroyString(sanitize_command);
3467 if (blob_info->file_info.file == (FILE *) NULL)
3469 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3470 return(MagickFalse);
3472 blob_info->type=PipeStream;
3473 blob_info->exempt=MagickTrue;
3474 return(SetStreamBuffering(image_info,blob_info));
3477 status=GetPathAttributes(filename,&blob_info->properties);
3478#if defined(S_ISFIFO)
3479 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3481 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3482 if (blob_info->file_info.file == (FILE *) NULL)
3484 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3485 return(MagickFalse);
3487 blob_info->type=FileStream;
3488 blob_info->exempt=MagickTrue;
3489 return(SetStreamBuffering(image_info,blob_info));
3492 GetPathComponent(image->filename,ExtensionPath,extension);
3495 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3496 if ((image_info->adjoin == MagickFalse) ||
3497 (strchr(filename,
'%') != (
char *) NULL))
3502 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3503 image->scene,filename,exception);
3504 if ((LocaleCompare(filename,image->filename) == 0) &&
3505 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3506 (GetNextImageInList(image) != (Image *) NULL)))
3509 path[MagickPathExtent];
3511 GetPathComponent(image->filename,RootPath,path);
3512 if (*extension ==
'\0')
3513 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3514 path,(
double) image->scene);
3516 (
void) FormatLocaleString(filename,MagickPathExtent,
3517 "%s-%.20g.%s",path,(
double) image->scene,extension);
3519 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3522 if (image_info->file != (FILE *) NULL)
3524 blob_info->file_info.file=image_info->file;
3525 blob_info->type=FileStream;
3526 blob_info->exempt=MagickTrue;
3534 blob_info->file_info.file=(FILE *) NULL;
3535 file=open_utf8(filename,flags,0);
3537 blob_info->file_info.file=fdopen(file,type);
3538 if (blob_info->file_info.file != (FILE *) NULL)
3546 blob_info->type=FileStream;
3547 (void) SetStreamBuffering(image_info,blob_info);
3548 (void) memset(magick,0,
sizeof(magick));
3549 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3550 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3551#if defined(MAGICKCORE_POSIX_SUPPORT)
3552 (void) fflush(blob_info->file_info.file);
3554 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3555 " read %.20g magic header bytes",(
double) count);
3556#if defined(MAGICKCORE_ZLIB_DELEGATE)
3557 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3558 ((
int) magick[2] == 0x08))
3561 gzfile = gzopen_utf8(filename,
"rb");
3563 if (gzfile != (gzFile) NULL)
3565 if (blob_info->file_info.file != (FILE *) NULL)
3566 (void) fclose(blob_info->file_info.file);
3567 blob_info->file_info.file=(FILE *) NULL;
3568 blob_info->file_info.gzfile=gzfile;
3569 blob_info->type=ZipStream;
3573#if defined(MAGICKCORE_BZLIB_DELEGATE)
3574 if (strncmp((
char *) magick,
"BZh",3) == 0)
3577 *bzfile = BZ2_bzopen(filename,
"r");
3579 if (bzfile != (BZFILE *) NULL)
3581 if (blob_info->file_info.file != (FILE *) NULL)
3582 (void) fclose(blob_info->file_info.file);
3583 blob_info->file_info.file=(FILE *) NULL;
3584 blob_info->file_info.bzfile=bzfile;
3585 blob_info->type=BZipStream;
3589 if (blob_info->type == FileStream)
3600 sans_exception=AcquireExceptionInfo();
3601 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3602 sans_exception=DestroyExceptionInfo(sans_exception);
3603 length=(size_t) blob_info->properties.st_size;
3604 if ((magick_info != (
const MagickInfo *) NULL) &&
3605 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3606 (length > MagickMaxBufferExtent) &&
3607 (AcquireMagickResource(MapResource,length) != MagickFalse))
3612 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3614 if (blob == (
void *) NULL)
3615 RelinquishMagickResource(MapResource,length);
3621 if (image_info->file != (FILE *) NULL)
3622 blob_info->exempt=MagickFalse;
3625 (void) fclose(blob_info->file_info.file);
3626 blob_info->file_info.file=(FILE *) NULL;
3628 AttachBlob(blob_info,blob,length);
3629 blob_info->mapped=MagickTrue;
3636#if defined(MAGICKCORE_ZLIB_DELEGATE)
3637 if ((LocaleCompare(extension,
"gz") == 0) ||
3638 (LocaleCompare(extension,
"wmz") == 0) ||
3639 (LocaleCompare(extension,
"svgz") == 0))
3641 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3642 if (blob_info->file_info.gzfile != (gzFile) NULL)
3643 blob_info->type=ZipStream;
3647#if defined(MAGICKCORE_BZLIB_DELEGATE)
3648 if (LocaleCompare(extension,
"bz2") == 0)
3650 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3651 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3652 blob_info->type=BZipStream;
3660 blob_info->file_info.file=(FILE *) NULL;
3661 file=open_utf8(filename,flags,P_MODE);
3663 blob_info->file_info.file=fdopen(file,type);
3664 if (blob_info->file_info.file != (FILE *) NULL)
3666 blob_info->type=FileStream;
3667 (void) SetStreamBuffering(image_info,blob_info);
3670 if (IsPathAuthorized(rights,filename) == MagickFalse)
3673 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3674 "NotAuthorized",
"`%s'",filename);
3675 return(MagickFalse);
3677 blob_info->status=0;
3678 blob_info->error_number=0;
3679 if (blob_info->type != UndefinedStream)
3680 blob_info->size=GetBlobSize(image);
3683 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3684 return(MagickFalse);
3723#if defined(__cplusplus) || defined(c_plusplus)
3727static size_t PingStream(
const Image *magick_unused(image),
3728 const void *magick_unused(pixels),
const size_t columns)
3730 magick_unreferenced(image);
3731 magick_unreferenced(pixels);
3735#if defined(__cplusplus) || defined(c_plusplus)
3739MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3740 const size_t length,ExceptionInfo *exception)
3755 assert(image_info != (ImageInfo *) NULL);
3756 assert(image_info->signature == MagickCoreSignature);
3757 assert(exception != (ExceptionInfo *) NULL);
3758 if (IsEventLogging() != MagickFalse)
3759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3760 image_info->filename);
3761 if ((blob == (
const void *) NULL) || (length == 0))
3763 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3764 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3765 return((Image *) NULL);
3767 ping_info=CloneImageInfo(image_info);
3768 ping_info->blob=(
void *) blob;
3769 ping_info->length=length;
3770 ping_info->ping=MagickTrue;
3771 if (*ping_info->magick ==
'\0')
3772 (void) SetImageInfo(ping_info,0,exception);
3773 magick_info=GetMagickInfo(ping_info->magick,exception);
3774 if (magick_info == (
const MagickInfo *) NULL)
3776 (void) ThrowMagickException(exception,GetMagickModule(),
3777 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3779 ping_info=DestroyImageInfo(ping_info);
3780 return((Image *) NULL);
3782 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3785 filename[MagickPathExtent];
3790 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3791 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3792 ping_info->magick,filename);
3793 image=ReadStream(ping_info,&PingStream,exception);
3794 if (image != (Image *) NULL)
3795 (void) DetachBlob(image->blob);
3796 ping_info=DestroyImageInfo(ping_info);
3802 ping_info->blob=(
void *) NULL;
3803 ping_info->length=0;
3804 *ping_info->filename=
'\0';
3805 status=BlobToFile(ping_info->filename,blob,length,exception);
3806 if (status == MagickFalse)
3808 (void) RelinquishUniqueFileResource(ping_info->filename);
3809 ping_info=DestroyImageInfo(ping_info);
3810 return((Image *) NULL);
3812 clone_info=CloneImageInfo(ping_info);
3813 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3814 ping_info->magick,ping_info->filename);
3815 image=ReadStream(clone_info,&PingStream,exception);
3816 if (image != (Image *) NULL)
3824 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3826 (void) CopyMagickString(images->filename,image_info->filename,
3828 (void) CopyMagickString(images->magick_filename,image_info->filename,
3830 (void) CopyMagickString(images->magick,magick_info->name,
3832 images=GetNextImageInList(images);
3835 clone_info=DestroyImageInfo(clone_info);
3836 (void) RelinquishUniqueFileResource(ping_info->filename);
3837 ping_info=DestroyImageInfo(ping_info);
3872MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3875 *magick_restrict blob_info;
3886 assert(image != (Image *) NULL);
3887 assert(image->signature == MagickCoreSignature);
3888 assert(image->blob != (BlobInfo *) NULL);
3889 assert(image->blob->type != UndefinedStream);
3892 assert(data != (
void *) NULL);
3893 blob_info=image->blob;
3895 q=(
unsigned char *) data;
3896 switch (blob_info->type)
3898 case UndefinedStream:
3900 case StandardStream:
3908 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3913 c=getc(blob_info->file_info.file);
3916 *q++=(
unsigned char) c;
3922 c=getc(blob_info->file_info.file);
3925 *q++=(
unsigned char) c;
3931 c=getc(blob_info->file_info.file);
3934 *q++=(
unsigned char) c;
3940 c=getc(blob_info->file_info.file);
3943 *q++=(
unsigned char) c;
3950 if ((count != (ssize_t) length) &&
3951 (ferror(blob_info->file_info.file) != 0))
3952 ThrowBlobException(blob_info);
3957#if defined(MAGICKCORE_ZLIB_DELEGATE)
3968 for (i=0; i < length; i+=(size_t) count)
3970 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3971 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3984 c=gzgetc(blob_info->file_info.gzfile);
3987 *q++=(
unsigned char) c;
3993 c=gzgetc(blob_info->file_info.gzfile);
3996 *q++=(
unsigned char) c;
4002 c=gzgetc(blob_info->file_info.gzfile);
4005 *q++=(
unsigned char) c;
4011 c=gzgetc(blob_info->file_info.gzfile);
4014 *q++=(
unsigned char) c;
4021 (void) gzerror(blob_info->file_info.gzfile,&status);
4022 if ((count != (ssize_t) length) && (status != Z_OK))
4023 ThrowBlobException(blob_info);
4024 if (blob_info->eof == MagickFalse)
4025 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4032#if defined(MAGICKCORE_BZLIB_DELEGATE)
4039 for (i=0; i < length; i+=(size_t) count)
4041 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4042 MagickMin(length-i,MagickMaxBufferExtent));
4052 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4053 if ((count != (ssize_t) length) && (status != BZ_OK))
4054 ThrowBlobException(blob_info);
4065 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4067 blob_info->eof=MagickTrue;
4070 p=blob_info->data+blob_info->offset;
4071 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4072 blob_info->length-blob_info->offset);
4073 blob_info->offset+=count;
4074 if (count != (ssize_t) length)
4075 blob_info->eof=MagickTrue;
4076 (void) memcpy(q,p,(
size_t) count);
4081 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4082 count=blob_info->custom_stream->reader(q,length,
4083 blob_info->custom_stream->data);
4112MagickExport
int ReadBlobByte(Image *image)
4115 *magick_restrict blob_info;
4120 assert(image != (Image *) NULL);
4121 assert(image->signature == MagickCoreSignature);
4122 assert(image->blob != (BlobInfo *) NULL);
4123 assert(image->blob->type != UndefinedStream);
4124 blob_info=image->blob;
4125 switch (blob_info->type)
4127 case StandardStream:
4131 c=getc(blob_info->file_info.file);
4134 if (ferror(blob_info->file_info.file) != 0)
4135 ThrowBlobException(blob_info);
4142 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4144 blob_info->eof=MagickTrue;
4147 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4148 blob_info->offset++;
4159 count=ReadBlob(image,1,buffer);
4192MagickExport
double ReadBlobDouble(Image *image)
4203 quantum.double_value=0.0;
4204 quantum.unsigned_value=ReadBlobLongLong(image);
4205 return(quantum.double_value);
4231MagickExport
float ReadBlobFloat(Image *image)
4242 quantum.float_value=0.0;
4243 quantum.unsigned_value=ReadBlobLong(image);
4244 return(quantum.float_value);
4270MagickExport
unsigned int ReadBlobLong(Image *image)
4284 assert(image != (Image *) NULL);
4285 assert(image->signature == MagickCoreSignature);
4287 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4290 if (image->endian == LSBEndian)
4292 value=(
unsigned int) (*p++);
4293 value|=(
unsigned int) (*p++) << 8;
4294 value|=(
unsigned int) (*p++) << 16;
4295 value|=(
unsigned int) (*p++) << 24;
4298 value=(
unsigned int) (*p++) << 24;
4299 value|=(
unsigned int) (*p++) << 16;
4300 value|=(
unsigned int) (*p++) << 8;
4301 value|=(
unsigned int) (*p++);
4328MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4342 assert(image != (Image *) NULL);
4343 assert(image->signature == MagickCoreSignature);
4345 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4347 return(MagickULLConstant(0));
4348 if (image->endian == LSBEndian)
4350 value=(MagickSizeType) (*p++);
4351 value|=(MagickSizeType) (*p++) << 8;
4352 value|=(MagickSizeType) (*p++) << 16;
4353 value|=(MagickSizeType) (*p++) << 24;
4354 value|=(MagickSizeType) (*p++) << 32;
4355 value|=(MagickSizeType) (*p++) << 40;
4356 value|=(MagickSizeType) (*p++) << 48;
4357 value|=(MagickSizeType) (*p++) << 56;
4360 value=(MagickSizeType) (*p++) << 56;
4361 value|=(MagickSizeType) (*p++) << 48;
4362 value|=(MagickSizeType) (*p++) << 40;
4363 value|=(MagickSizeType) (*p++) << 32;
4364 value|=(MagickSizeType) (*p++) << 24;
4365 value|=(MagickSizeType) (*p++) << 16;
4366 value|=(MagickSizeType) (*p++) << 8;
4367 value|=(MagickSizeType) (*p++);
4394MagickExport
unsigned short ReadBlobShort(Image *image)
4408 assert(image != (Image *) NULL);
4409 assert(image->signature == MagickCoreSignature);
4411 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4413 return((
unsigned short) 0U);
4414 if (image->endian == LSBEndian)
4416 value=(
unsigned short) (*p++);
4417 value|=(
unsigned short) (*p++) << 8;
4420 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4421 value|=(
unsigned short) (*p++);
4448MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4462 assert(image != (Image *) NULL);
4463 assert(image->signature == MagickCoreSignature);
4465 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4468 value=(
unsigned int) (*p++);
4469 value|=(
unsigned int) (*p++) << 8;
4470 value|=(
unsigned int) (*p++) << 16;
4471 value|=(
unsigned int) (*p++) << 24;
4498MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4509 quantum.unsigned_value=ReadBlobLSBLong(image);
4510 return(quantum.signed_value);
4536MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4550 assert(image != (Image *) NULL);
4551 assert(image->signature == MagickCoreSignature);
4553 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4555 return((
unsigned short) 0U);
4556 value=(
unsigned short) (*p++);
4557 value|=(
unsigned short) (*p++) << 8;
4584MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4595 quantum.unsigned_value=ReadBlobLSBShort(image);
4596 return(quantum.signed_value);
4622MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4636 assert(image != (Image *) NULL);
4637 assert(image->signature == MagickCoreSignature);
4639 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4642 value=(
unsigned int) (*p++) << 24;
4643 value|=(
unsigned int) (*p++) << 16;
4644 value|=(
unsigned int) (*p++) << 8;
4645 value|=(
unsigned int) (*p++);
4672MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4686 assert(image != (Image *) NULL);
4687 assert(image->signature == MagickCoreSignature);
4689 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4691 return(MagickULLConstant(0));
4692 value=(MagickSizeType) (*p++) << 56;
4693 value|=(MagickSizeType) (*p++) << 48;
4694 value|=(MagickSizeType) (*p++) << 40;
4695 value|=(MagickSizeType) (*p++) << 32;
4696 value|=(MagickSizeType) (*p++) << 24;
4697 value|=(MagickSizeType) (*p++) << 16;
4698 value|=(MagickSizeType) (*p++) << 8;
4699 value|=(MagickSizeType) (*p++);
4726MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4740 assert(image != (Image *) NULL);
4741 assert(image->signature == MagickCoreSignature);
4743 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4745 return((
unsigned short) 0U);
4746 value=(
unsigned short) ((*p++) << 8);
4747 value|=(
unsigned short) (*p++);
4748 return((
unsigned short) (value & 0xffff));
4774MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4785 quantum.unsigned_value=ReadBlobMSBLong(image);
4786 return(quantum.signed_value);
4812MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4823 quantum.unsigned_value=ReadBlobMSBShort(image);
4824 return(quantum.signed_value);
4850MagickExport
signed int ReadBlobSignedLong(Image *image)
4861 quantum.unsigned_value=ReadBlobLong(image);
4862 return(quantum.signed_value);
4888MagickExport
signed short ReadBlobSignedShort(Image *image)
4899 quantum.unsigned_value=ReadBlobShort(image);
4900 return(quantum.signed_value);
4938MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4939 const size_t length,
void *magick_restrict data,ssize_t *count)
4942 *magick_restrict blob_info;
4944 assert(image != (Image *) NULL);
4945 assert(image->signature == MagickCoreSignature);
4946 assert(image->blob != (BlobInfo *) NULL);
4947 assert(image->blob->type != UndefinedStream);
4948 assert(count != (ssize_t *) NULL);
4949 blob_info=image->blob;
4950 if (blob_info->type != BlobStream)
4952 assert(data != NULL);
4953 *count=ReadBlob(image,length,(
unsigned char *) data);
4956 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4959 blob_info->eof=MagickTrue;
4962 data=blob_info->data+blob_info->offset;
4963 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4964 blob_info->length-blob_info->offset);
4965 blob_info->offset+=(*count);
4966 if (*count != (ssize_t) length)
4967 blob_info->eof=MagickTrue;
4996MagickExport
char *ReadBlobString(Image *image,
char *
string)
4999 *magick_restrict blob_info;
5007 assert(image != (Image *) NULL);
5008 assert(image->signature == MagickCoreSignature);
5009 assert(image->blob != (BlobInfo *) NULL);
5010 assert(image->blob->type != UndefinedStream);
5011 if (IsEventLogging() != MagickFalse)
5012 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5014 blob_info=image->blob;
5015 switch (blob_info->type)
5017 case UndefinedStream:
5019 case StandardStream:
5022 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5023 if (p == (
char *) NULL)
5025 if (ferror(blob_info->file_info.file) != 0)
5026 ThrowBlobException(blob_info);
5027 return((
char *) NULL);
5034#if defined(MAGICKCORE_ZLIB_DELEGATE)
5035 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5036 if (p == (
char *) NULL)
5039 (void) gzerror(blob_info->file_info.gzfile,&status);
5041 ThrowBlobException(blob_info);
5042 return((
char *) NULL);
5052 c=ReadBlobByte(image);
5055 blob_info->eof=MagickTrue;
5058 string[i++]=(char) c;
5061 }
while (i < (MaxTextExtent-2));
5069 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5072 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5074 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5075 return((
char *) NULL);
5102MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5104 assert(blob != (BlobInfo *) NULL);
5105 assert(blob->signature == MagickCoreSignature);
5106 if (IsEventLogging() != MagickFalse)
5107 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5108 LockSemaphoreInfo(blob->semaphore);
5109 blob->reference_count++;
5110 UnlockSemaphoreInfo(blob->semaphore);
5147MagickExport MagickOffsetType SeekBlob(Image *image,
5148 const MagickOffsetType offset,
const int whence)
5151 *magick_restrict blob_info;
5153 assert(image != (Image *) NULL);
5154 assert(image->signature == MagickCoreSignature);
5155 assert(image->blob != (BlobInfo *) NULL);
5156 assert(image->blob->type != UndefinedStream);
5157 if (IsEventLogging() != MagickFalse)
5158 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5159 blob_info=image->blob;
5160 switch (blob_info->type)
5162 case UndefinedStream:
5164 case StandardStream:
5169 if ((offset < 0) && (whence == SEEK_SET))
5171 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5173 blob_info->offset=TellBlob(image);
5178#if defined(MAGICKCORE_ZLIB_DELEGATE)
5179 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5182 blob_info->offset=TellBlob(image);
5198 blob_info->offset=offset;
5203 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5204 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5209 if ((blob_info->offset+offset) < 0)
5211 blob_info->offset+=offset;
5216 if (((MagickOffsetType) blob_info->length+offset) < 0)
5218 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5222 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5224 blob_info->eof=MagickFalse;
5231 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5233 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5234 blob_info->custom_stream->data);
5238 return(blob_info->offset);
5266MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5268 assert(image != (
const Image *) NULL);
5269 assert(image->signature == MagickCoreSignature);
5270 if (IsEventLogging() != MagickFalse)
5271 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5272 image->blob->exempt=exempt;
5301MagickExport MagickBooleanType SetBlobExtent(Image *image,
5302 const MagickSizeType extent)
5305 *magick_restrict blob_info;
5307 assert(image != (Image *) NULL);
5308 assert(image->signature == MagickCoreSignature);
5309 assert(image->blob != (BlobInfo *) NULL);
5310 assert(image->blob->type != UndefinedStream);
5311 if (IsEventLogging() != MagickFalse)
5312 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5313 blob_info=image->blob;
5314 switch (blob_info->type)
5316 case UndefinedStream:
5318 case StandardStream:
5319 return(MagickFalse);
5328 if (extent != (MagickSizeType) ((off_t) extent))
5329 return(MagickFalse);
5330 offset=SeekBlob(image,0,SEEK_END);
5332 return(MagickFalse);
5333 if ((MagickSizeType) offset >= extent)
5335 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5338 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5339 blob_info->file_info.file);
5340#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5341 if (blob_info->synchronize != MagickFalse)
5346 file=fileno(blob_info->file_info.file);
5347 if ((file == -1) || (offset < 0))
5348 return(MagickFalse);
5349 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5352 offset=SeekBlob(image,offset,SEEK_SET);
5354 return(MagickFalse);
5359 return(MagickFalse);
5361 return(MagickFalse);
5363 return(MagickFalse);
5366 if (extent != (MagickSizeType) ((
size_t) extent))
5367 return(MagickFalse);
5368 if (blob_info->mapped != MagickFalse)
5376 (void) UnmapBlob(blob_info->data,blob_info->length);
5377 RelinquishMagickResource(MapResource,blob_info->length);
5378 if (extent != (MagickSizeType) ((off_t) extent))
5379 return(MagickFalse);
5380 offset=SeekBlob(image,0,SEEK_END);
5382 return(MagickFalse);
5383 if ((MagickSizeType) offset >= extent)
5385 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5386 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5387 blob_info->file_info.file);
5388#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5389 if (blob_info->synchronize != MagickFalse)
5394 file=fileno(blob_info->file_info.file);
5395 if ((file == -1) || (offset < 0))
5396 return(MagickFalse);
5397 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5401 offset=SeekBlob(image,offset,SEEK_SET);
5403 return(MagickFalse);
5404 (void) AcquireMagickResource(MapResource,extent);
5405 blob_info->data=(
unsigned char*) MapBlob(fileno(
5406 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5407 blob_info->extent=(size_t) extent;
5408 blob_info->length=(size_t) extent;
5409 (void) SyncBlob(image);
5412 blob_info->extent=(size_t) extent;
5413 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5414 blob_info->extent+1,
sizeof(*blob_info->data));
5415 (void) SyncBlob(image);
5416 if (blob_info->data == (
unsigned char *) NULL)
5418 (void) DetachBlob(blob_info);
5419 return(MagickFalse);
5453MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5456 assert(custom_stream != (CustomStreamInfo *) NULL);
5457 assert(custom_stream->signature == MagickCoreSignature);
5458 custom_stream->data=data;
5486MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5487 CustomStreamHandler reader)
5489 assert(custom_stream != (CustomStreamInfo *) NULL);
5490 assert(custom_stream->signature == MagickCoreSignature);
5491 custom_stream->reader=reader;
5519MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5520 CustomStreamSeeker seeker)
5522 assert(custom_stream != (CustomStreamInfo *) NULL);
5523 assert(custom_stream->signature == MagickCoreSignature);
5524 custom_stream->seeker=seeker;
5552MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5553 CustomStreamTeller teller)
5555 assert(custom_stream != (CustomStreamInfo *) NULL);
5556 assert(custom_stream->signature == MagickCoreSignature);
5557 custom_stream->teller=teller;
5585MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5586 CustomStreamHandler writer)
5588 assert(custom_stream != (CustomStreamInfo *) NULL);
5589 assert(custom_stream->signature == MagickCoreSignature);
5590 custom_stream->writer=writer;
5617static int SyncBlob(
const Image *image)
5620 *magick_restrict blob_info;
5625 assert(image != (Image *) NULL);
5626 assert(image->signature == MagickCoreSignature);
5627 assert(image->blob != (BlobInfo *) NULL);
5628 if (IsEventLogging() != MagickFalse)
5629 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5630 if (EOFBlob(image) != 0)
5632 blob_info=image->blob;
5634 switch (blob_info->type)
5636 case UndefinedStream:
5637 case StandardStream:
5642 status=fflush(blob_info->file_info.file);
5647#if defined(MAGICKCORE_ZLIB_DELEGATE)
5648 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5654#if defined(MAGICKCORE_BZLIB_DELEGATE)
5655 status=BZ2_bzflush(blob_info->file_info.bzfile);
5691MagickExport MagickOffsetType TellBlob(
const Image *image)
5694 *magick_restrict blob_info;
5699 assert(image != (Image *) NULL);
5700 assert(image->signature == MagickCoreSignature);
5701 assert(image->blob != (BlobInfo *) NULL);
5702 assert(image->blob->type != UndefinedStream);
5703 if (IsEventLogging() != MagickFalse)
5704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5705 blob_info=image->blob;
5707 switch (blob_info->type)
5709 case UndefinedStream:
5710 case StandardStream:
5714 offset=ftell(blob_info->file_info.file);
5721#if defined(MAGICKCORE_ZLIB_DELEGATE)
5722 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5732 offset=blob_info->offset;
5737 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5738 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5770MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5772#if defined(MAGICKCORE_HAVE_MMAP)
5776 status=munmap(map,length);
5777 return(status == -1 ? MagickFalse : MagickTrue);
5781 return(MagickFalse);
5813MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5817 *magick_restrict blob_info;
5831 assert(image != (Image *) NULL);
5832 assert(image->signature == MagickCoreSignature);
5833 assert(image->blob != (BlobInfo *) NULL);
5834 assert(image->blob->type != UndefinedStream);
5837 assert(data != (
const void *) NULL);
5838 blob_info=image->blob;
5840 p=(
const unsigned char *) data;
5841 q=(
unsigned char *) data;
5842 switch (blob_info->type)
5844 case UndefinedStream:
5846 case StandardStream:
5854 count=(ssize_t) fwrite((
const char *) data,1,length,
5855 blob_info->file_info.file);
5860 c=putc((
int) *p++,blob_info->file_info.file);
5868 c=putc((
int) *p++,blob_info->file_info.file);
5876 c=putc((
int) *p++,blob_info->file_info.file);
5884 c=putc((
int) *p++,blob_info->file_info.file);
5893 if ((count != (ssize_t) length) &&
5894 (ferror(blob_info->file_info.file) != 0))
5895 ThrowBlobException(blob_info);
5900#if defined(MAGICKCORE_ZLIB_DELEGATE)
5911 for (i=0; i < length; i+=(size_t) count)
5913 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5914 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5927 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5935 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5943 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5951 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5961 (void) gzerror(blob_info->file_info.gzfile,&status);
5962 if ((count != (ssize_t) length) && (status != Z_OK))
5963 ThrowBlobException(blob_info);
5969#if defined(MAGICKCORE_BZLIB_DELEGATE)
5976 for (i=0; i < length; i+=(size_t) count)
5978 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5979 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5989 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5990 if ((count != (ssize_t) length) && (status != BZ_OK))
5991 ThrowBlobException(blob_info);
5997 count=(ssize_t) blob_info->stream(image,data,length);
6005 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6010 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6011 if (extent >= blob_info->extent)
6013 extent+=blob_info->quantum+length;
6014 blob_info->quantum<<=1;
6015 if (SetBlobExtent(image,extent) == MagickFalse)
6018 q=blob_info->data+blob_info->offset;
6019 (void) memcpy(q,p,length);
6020 blob_info->offset+=(MagickOffsetType) length;
6021 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6022 blob_info->length=(size_t) blob_info->offset;
6023 count=(ssize_t) length;
6028 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6029 count=blob_info->custom_stream->writer((
unsigned char *) data,
6030 length,blob_info->custom_stream->data);
6062MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6065 *magick_restrict blob_info;
6070 assert(image != (Image *) NULL);
6071 assert(image->signature == MagickCoreSignature);
6072 assert(image->blob != (BlobInfo *) NULL);
6073 assert(image->blob->type != UndefinedStream);
6074 blob_info=image->blob;
6076 switch (blob_info->type)
6078 case StandardStream:
6085 c=putc((
int) value,blob_info->file_info.file);
6088 if (ferror(blob_info->file_info.file) != 0)
6089 ThrowBlobException(blob_info);
6097 count=WriteBlobStream(image,1,&value);
6129MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6140 quantum.unsigned_value=0U;
6141 quantum.float_value=value;
6142 return(WriteBlobLong(image,quantum.unsigned_value));
6170MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6175 assert(image != (Image *) NULL);
6176 assert(image->signature == MagickCoreSignature);
6177 if (image->endian == LSBEndian)
6179 buffer[0]=(
unsigned char) value;
6180 buffer[1]=(
unsigned char) (value >> 8);
6181 buffer[2]=(
unsigned char) (value >> 16);
6182 buffer[3]=(
unsigned char) (value >> 24);
6183 return(WriteBlobStream(image,4,buffer));
6185 buffer[0]=(
unsigned char) (value >> 24);
6186 buffer[1]=(
unsigned char) (value >> 16);
6187 buffer[2]=(
unsigned char) (value >> 8);
6188 buffer[3]=(
unsigned char) value;
6189 return(WriteBlobStream(image,4,buffer));
6217MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6222 assert(image != (Image *) NULL);
6223 assert(image->signature == MagickCoreSignature);
6224 if (image->endian == LSBEndian)
6226 buffer[0]=(
unsigned char) value;
6227 buffer[1]=(
unsigned char) (value >> 8);
6228 buffer[2]=(
unsigned char) (value >> 16);
6229 buffer[3]=(
unsigned char) (value >> 24);
6230 buffer[4]=(
unsigned char) (value >> 32);
6231 buffer[5]=(
unsigned char) (value >> 40);
6232 buffer[6]=(
unsigned char) (value >> 48);
6233 buffer[7]=(
unsigned char) (value >> 56);
6234 return(WriteBlobStream(image,8,buffer));
6236 buffer[0]=(
unsigned char) (value >> 56);
6237 buffer[1]=(
unsigned char) (value >> 48);
6238 buffer[2]=(
unsigned char) (value >> 40);
6239 buffer[3]=(
unsigned char) (value >> 32);
6240 buffer[4]=(
unsigned char) (value >> 24);
6241 buffer[5]=(
unsigned char) (value >> 16);
6242 buffer[6]=(
unsigned char) (value >> 8);
6243 buffer[7]=(
unsigned char) value;
6244 return(WriteBlobStream(image,8,buffer));
6272MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6277 assert(image != (Image *) NULL);
6278 assert(image->signature == MagickCoreSignature);
6279 if (image->endian == LSBEndian)
6281 buffer[0]=(
unsigned char) value;
6282 buffer[1]=(
unsigned char) (value >> 8);
6283 return(WriteBlobStream(image,2,buffer));
6285 buffer[0]=(
unsigned char) (value >> 8);
6286 buffer[1]=(
unsigned char) value;
6287 return(WriteBlobStream(image,2,buffer));
6315MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6329 assert(image != (Image *) NULL);
6330 assert(image->signature == MagickCoreSignature);
6331 quantum.signed_value=value;
6332 if (image->endian == LSBEndian)
6334 buffer[0]=(
unsigned char) quantum.unsigned_value;
6335 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6336 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6337 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6338 return(WriteBlobStream(image,4,buffer));
6340 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6341 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6342 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6343 buffer[3]=(
unsigned char) quantum.unsigned_value;
6344 return(WriteBlobStream(image,4,buffer));
6372MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6377 assert(image != (Image *) NULL);
6378 assert(image->signature == MagickCoreSignature);
6379 buffer[0]=(
unsigned char) value;
6380 buffer[1]=(
unsigned char) (value >> 8);
6381 buffer[2]=(
unsigned char) (value >> 16);
6382 buffer[3]=(
unsigned char) (value >> 24);
6383 return(WriteBlobStream(image,4,buffer));
6411MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6416 assert(image != (Image *) NULL);
6417 assert(image->signature == MagickCoreSignature);
6418 buffer[0]=(
unsigned char) value;
6419 buffer[1]=(
unsigned char) (value >> 8);
6420 return(WriteBlobStream(image,2,buffer));
6448MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6462 assert(image != (Image *) NULL);
6463 assert(image->signature == MagickCoreSignature);
6464 quantum.signed_value=value;
6465 buffer[0]=(
unsigned char) quantum.unsigned_value;
6466 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6467 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6468 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6469 return(WriteBlobStream(image,4,buffer));
6497MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6498 const signed short value)
6512 assert(image != (Image *) NULL);
6513 assert(image->signature == MagickCoreSignature);
6514 quantum.signed_value=value;
6515 buffer[0]=(
unsigned char) quantum.unsigned_value;
6516 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6517 return(WriteBlobStream(image,2,buffer));
6545MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6550 assert(image != (Image *) NULL);
6551 assert(image->signature == MagickCoreSignature);
6552 buffer[0]=(
unsigned char) (value >> 24);
6553 buffer[1]=(
unsigned char) (value >> 16);
6554 buffer[2]=(
unsigned char) (value >> 8);
6555 buffer[3]=(
unsigned char) value;
6556 return(WriteBlobStream(image,4,buffer));
6584MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6585 const signed short value)
6599 assert(image != (Image *) NULL);
6600 assert(image->signature == MagickCoreSignature);
6601 quantum.signed_value=value;
6602 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6603 buffer[1]=(
unsigned char) quantum.unsigned_value;
6604 return(WriteBlobStream(image,2,buffer));
6632MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6637 assert(image != (Image *) NULL);
6638 assert(image->signature == MagickCoreSignature);
6639 buffer[0]=(
unsigned char) (value >> 8);
6640 buffer[1]=(
unsigned char) value;
6641 return(WriteBlobStream(image,2,buffer));
6669MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6671 assert(image != (Image *) NULL);
6672 assert(image->signature == MagickCoreSignature);
6673 assert(
string != (
const char *) NULL);
6674 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));