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 IsPathAuthorized(rights,filename) \
87 ((IsRightsAuthorized(PathPolicyDomain,rights,filename) != MagickFalse) && \
88 ((IsRightsAuthorizedByName(SystemPolicyDomain,"symlink",rights,"follow") != MagickFalse) || \
89 (is_symlink_utf8(filename) == MagickFalse)))
90#define MagickMaxBlobExtent (8*8192)
91#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
92# define MAP_ANONYMOUS MAP_ANON
94#if !defined(MAP_FAILED)
95#define MAP_FAILED ((void *) -1)
99#define _O_BINARY O_BINARY
101#if defined(MAGICKCORE_WINDOWS_SUPPORT)
103# define fsync _commit
106# define MAGICKCORE_HAVE_MMAP 1
107# define mmap(address,length,protection,access,file,offset) \
108 NTMapMemory(address,length,protection,access,file,offset)
111# define munmap(address,length) NTUnmapMemory(address,length)
114# define pclose _pclose
129#if defined(MAGICKCORE_ZLIB_DELEGATE)
134#if defined(MAGICKCORE_BZLIB_DELEGATE)
226 SyncBlob(
const Image *);
250MagickExport CustomStreamInfo *AcquireCustomStreamInfo(
251 ExceptionInfo *magick_unused(exception))
256 magick_unreferenced(exception);
257 custom_stream=(CustomStreamInfo *) AcquireCriticalMemory(
258 sizeof(*custom_stream));
259 (void) memset(custom_stream,0,
sizeof(*custom_stream));
260 custom_stream->signature=MagickCoreSignature;
261 return(custom_stream);
291MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
294 assert(blob_info != (BlobInfo *) NULL);
295 if (IsEventLogging() != MagickFalse)
296 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
297 blob_info->length=length;
298 blob_info->extent=length;
299 blob_info->quantum=(size_t) MagickMaxBlobExtent;
301 blob_info->type=BlobStream;
302 blob_info->file_info.file=(FILE *) NULL;
303 blob_info->data=(
unsigned char *) blob;
304 blob_info->mapped=MagickFalse;
332MagickExport
void AttachCustomStream(BlobInfo *blob_info,
333 CustomStreamInfo *custom_stream)
335 assert(blob_info != (BlobInfo *) NULL);
336 assert(custom_stream != (CustomStreamInfo *) NULL);
337 assert(custom_stream->signature == MagickCoreSignature);
338 if (IsEventLogging() != MagickFalse)
339 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
340 blob_info->type=CustomStream;
341 blob_info->custom_stream=custom_stream;
375MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
376 const size_t length,ExceptionInfo *exception)
387 assert(filename != (
const char *) NULL);
388 assert(blob != (
const void *) NULL);
389 if (IsEventLogging() != MagickFalse)
390 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
391 if (*filename ==
'\0')
392 file=AcquireUniqueFileResource(filename);
394 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
397 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
400 for (i=0; i < length; i+=(size_t) count)
402 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
403 MagickMaxBufferExtent));
411 file=close_utf8(file);
412 if ((file == -1) || (i < length))
414 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
451MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
452 const size_t length,ExceptionInfo *exception)
467 assert(image_info != (ImageInfo *) NULL);
468 assert(image_info->signature == MagickCoreSignature);
469 assert(exception != (ExceptionInfo *) NULL);
470 if (IsEventLogging() != MagickFalse)
471 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
472 image_info->filename);
473 if ((blob == (
const void *) NULL) || (length == 0))
475 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
476 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
477 return((Image *) NULL);
479 blob_info=CloneImageInfo(image_info);
480 blob_info->blob=(
void *) blob;
481 blob_info->length=length;
482 if (*blob_info->magick ==
'\0')
483 (void) SetImageInfo(blob_info,0,exception);
484 magick_info=GetMagickInfo(blob_info->magick,exception);
485 if (magick_info == (
const MagickInfo *) NULL)
487 (void) ThrowMagickException(exception,GetMagickModule(),
488 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
490 blob_info=DestroyImageInfo(blob_info);
491 return((Image *) NULL);
493 if (GetMagickBlobSupport(magick_info) != MagickFalse)
496 filename[MagickPathExtent];
501 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
502 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
503 blob_info->magick,filename);
504 image=ReadImage(blob_info,exception);
505 if (image != (Image *) NULL)
506 (void) DetachBlob(image->blob);
507 blob_info=DestroyImageInfo(blob_info);
513 blob_info->blob=(
void *) NULL;
515 *blob_info->filename=
'\0';
516 status=BlobToFile(blob_info->filename,blob,length,exception);
517 if (status == MagickFalse)
519 (void) RelinquishUniqueFileResource(blob_info->filename);
520 blob_info=DestroyImageInfo(blob_info);
521 return((Image *) NULL);
523 clone_info=CloneImageInfo(blob_info);
524 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
525 blob_info->magick,blob_info->filename);
526 image=ReadImage(clone_info,exception);
527 if (image != (Image *) NULL)
535 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
537 (void) CopyMagickString(images->filename,image_info->filename,
539 (void) CopyMagickString(images->magick_filename,image_info->filename,
541 (void) CopyMagickString(images->magick,magick_info->name,
543 images=GetNextImageInList(images);
546 clone_info=DestroyImageInfo(clone_info);
547 (void) RelinquishUniqueFileResource(blob_info->filename);
548 blob_info=DestroyImageInfo(blob_info);
575MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
583 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
584 GetBlobInfo(clone_info);
585 if (blob_info == (BlobInfo *) NULL)
587 semaphore=clone_info->semaphore;
588 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
589 if (blob_info->mapped != MagickFalse)
590 (void) AcquireMagickResource(MapResource,blob_info->length);
591 clone_info->semaphore=semaphore;
592 LockSemaphoreInfo(clone_info->semaphore);
593 clone_info->reference_count=1;
594 UnlockSemaphoreInfo(clone_info->semaphore);
621static inline void ThrowBlobException(BlobInfo *blob_info)
623 if ((blob_info->status == 0) && (errno != 0))
624 blob_info->error_number=errno;
625 blob_info->status=(-1);
628MagickExport MagickBooleanType CloseBlob(Image *image)
631 *magick_restrict blob_info;
639 assert(image != (Image *) NULL);
640 assert(image->signature == MagickCoreSignature);
641 if (IsEventLogging() != MagickFalse)
642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
643 blob_info=image->blob;
644 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
646 (void) SyncBlob(image);
647 status=blob_info->status;
648 switch (blob_info->type)
650 case UndefinedStream:
656 if (blob_info->synchronize != MagickFalse)
658 status=fflush(blob_info->file_info.file);
660 ThrowBlobException(blob_info);
661 status=fsync(fileno(blob_info->file_info.file));
663 ThrowBlobException(blob_info);
665 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
666 ThrowBlobException(blob_info);
671#if defined(MAGICKCORE_ZLIB_DELEGATE)
673 (void) gzerror(blob_info->file_info.gzfile,&status);
675 ThrowBlobException(blob_info);
681#if defined(MAGICKCORE_BZLIB_DELEGATE)
683 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
685 ThrowBlobException(blob_info);
693 if (blob_info->file_info.file != (FILE *) NULL)
695 if (blob_info->synchronize != MagickFalse)
697 status=fflush(blob_info->file_info.file);
699 ThrowBlobException(blob_info);
700 status=fsync(fileno(blob_info->file_info.file));
702 ThrowBlobException(blob_info);
704 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
705 ThrowBlobException(blob_info);
712 blob_info->size=GetBlobSize(image);
713 image->extent=blob_info->size;
714 blob_info->eof=MagickFalse;
716 blob_info->mode=UndefinedBlobMode;
717 if (blob_info->exempt != MagickFalse)
719 blob_info->type=UndefinedStream;
720 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
722 switch (blob_info->type)
724 case UndefinedStream:
729 if (blob_info->file_info.file != (FILE *) NULL)
731 status=fclose(blob_info->file_info.file);
733 ThrowBlobException(blob_info);
739#if defined(MAGICKCORE_HAVE_PCLOSE)
740 status=pclose(blob_info->file_info.file);
742 ThrowBlobException(blob_info);
748#if defined(MAGICKCORE_ZLIB_DELEGATE)
749 status=gzclose(blob_info->file_info.gzfile);
751 ThrowBlobException(blob_info);
757#if defined(MAGICKCORE_BZLIB_DELEGATE)
758 BZ2_bzclose(blob_info->file_info.bzfile);
766 if (blob_info->file_info.file != (FILE *) NULL)
768 status=fclose(blob_info->file_info.file);
770 ThrowBlobException(blob_info);
777 (void) DetachBlob(blob_info);
778 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
807MagickExport Image *CustomStreamToImage(
const ImageInfo *image_info,
808 ExceptionInfo *exception)
819 assert(image_info != (ImageInfo *) NULL);
820 assert(image_info->signature == MagickCoreSignature);
821 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
822 assert(image_info->custom_stream->signature == MagickCoreSignature);
823 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
824 assert(exception != (ExceptionInfo *) NULL);
825 if (IsEventLogging() != MagickFalse)
826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
827 image_info->filename);
828 blob_info=CloneImageInfo(image_info);
829 if (*blob_info->magick ==
'\0')
830 (void) SetImageInfo(blob_info,0,exception);
831 magick_info=GetMagickInfo(blob_info->magick,exception);
832 if (magick_info == (
const MagickInfo *) NULL)
834 (void) ThrowMagickException(exception,GetMagickModule(),
835 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
837 blob_info=DestroyImageInfo(blob_info);
838 return((Image *) NULL);
840 image=(Image *) NULL;
841 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
842 (*blob_info->filename !=
'\0'))
845 filename[MagickPathExtent];
851 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
852 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
853 blob_info->magick,filename);
854 image=ReadImage(blob_info,exception);
859 unique[MagickPathExtent];
873 blob_info->custom_stream=(CustomStreamInfo *) NULL;
874 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
876 if (blob == (
unsigned char *) NULL)
878 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
879 image_info->filename);
880 blob_info=DestroyImageInfo(blob_info);
881 return((Image *) NULL);
883 file=AcquireUniqueFileResource(unique);
886 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
887 image_info->filename);
888 blob=(
unsigned char *) RelinquishMagickMemory(blob);
889 blob_info=DestroyImageInfo(blob_info);
890 return((Image *) NULL);
892 clone_info=CloneImageInfo(blob_info);
893 blob_info->file=fdopen(file,
"wb+");
894 if (blob_info->file != (FILE *) NULL)
899 count=(ssize_t) MagickMaxBufferExtent;
900 while (count == (ssize_t) MagickMaxBufferExtent)
902 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
903 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;
1117MagickExport
void DisassociateBlob(Image *image)
1120 *magick_restrict blob_info,
1126 assert(image != (Image *) NULL);
1127 assert(image->signature == MagickCoreSignature);
1128 assert(image->blob != (BlobInfo *) NULL);
1129 assert(image->blob->signature == MagickCoreSignature);
1130 if (IsEventLogging() != MagickFalse)
1131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1132 blob_info=image->blob;
1134 LockSemaphoreInfo(blob_info->semaphore);
1135 assert(blob_info->reference_count >= 0);
1136 if (blob_info->reference_count > 1)
1138 UnlockSemaphoreInfo(blob_info->semaphore);
1139 if (clone == MagickFalse)
1141 clone_info=CloneBlobInfo(blob_info);
1143 image->blob=clone_info;
1171MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1172 const MagickSizeType length)
1184 buffer[MagickMinBufferExtent >> 1];
1186 assert(image != (Image *) NULL);
1187 assert(image->signature == MagickCoreSignature);
1188 if (length != (MagickSizeType) ((MagickOffsetType) length))
1189 return(MagickFalse);
1191 for (i=0; i < length; i+=(MagickSizeType) count)
1193 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1194 (void) ReadBlobStream(image,quantum,buffer,&count);
1202 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1229MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
1231 assert(image != (Image *) NULL);
1232 assert(image->signature == MagickCoreSignature);
1233 assert(duplicate != (Image *) NULL);
1234 assert(duplicate->signature == MagickCoreSignature);
1235 if (IsEventLogging() != MagickFalse)
1236 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1238 image->blob=ReferenceBlob(duplicate->blob);
1264MagickExport
int EOFBlob(
const Image *image)
1267 *magick_restrict blob_info;
1269 assert(image != (Image *) NULL);
1270 assert(image->signature == MagickCoreSignature);
1271 assert(image->blob != (BlobInfo *) NULL);
1272 assert(image->blob->type != UndefinedStream);
1273 if (IsEventLogging() != MagickFalse)
1274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1275 blob_info=image->blob;
1276 switch (blob_info->type)
1278 case UndefinedStream:
1279 case StandardStream:
1284 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1290#if defined(MAGICKCORE_ZLIB_DELEGATE)
1291 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1298#if defined(MAGICKCORE_BZLIB_DELEGATE)
1303 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1304 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1310 blob_info->eof=MagickFalse;
1318 return((
int) blob_info->eof);
1344MagickExport
int ErrorBlob(
const Image *image)
1347 *magick_restrict blob_info;
1349 assert(image != (Image *) NULL);
1350 assert(image->signature == MagickCoreSignature);
1351 assert(image->blob != (BlobInfo *) NULL);
1352 assert(image->blob->type != UndefinedStream);
1353 if (IsEventLogging() != MagickFalse)
1354 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1355 blob_info=image->blob;
1356 switch (blob_info->type)
1358 case UndefinedStream:
1359 case StandardStream:
1364 blob_info->error=ferror(blob_info->file_info.file);
1369#if defined(MAGICKCORE_ZLIB_DELEGATE)
1370 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1376#if defined(MAGICKCORE_BZLIB_DELEGATE)
1377 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1391 return(blob_info->error);
1429MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1430 size_t *length,ExceptionInfo *exception)
1456 assert(filename != (
const char *) NULL);
1457 assert(exception != (ExceptionInfo *) NULL);
1458 assert(exception->signature == MagickCoreSignature);
1459 if (IsEventLogging() != MagickFalse)
1460 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1462 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1465 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1466 "NotAuthorized",
"`%s'",filename);
1470 if (LocaleCompare(filename,
"-") != 0)
1473 flags = O_RDONLY | O_BINARY;
1475 status=GetPathAttributes(filename,&attributes);
1476 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1478 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1481 file=open_utf8(filename,flags,0);
1485 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1488 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1490 file=close_utf8(file)-1;
1492 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1493 "NotAuthorized",
"`%s'",filename);
1496 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1498 if ((file == fileno(stdin)) || (offset < 0) ||
1499 (offset != (MagickOffsetType) ((ssize_t) offset)))
1510 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1511 quantum=(size_t) MagickMaxBufferExtent;
1512 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1513 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1514 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1515 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1517 count=read(file,blob+i,quantum);
1524 if (~i < ((
size_t) count+quantum+1))
1526 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1529 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1530 quantum+1,
sizeof(*blob));
1531 if ((i+(
size_t) count) >= extent)
1534 if (LocaleCompare(filename,
"-") != 0)
1535 file=close_utf8(file);
1536 if (blob == (
unsigned char *) NULL)
1538 (void) ThrowMagickException(exception,GetMagickModule(),
1539 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1544 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1545 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1548 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1552 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1553 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1554 blob=(
unsigned char *) NULL;
1555 if (~(*length) >= (MagickPathExtent-1))
1556 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1558 if (blob == (
unsigned char *) NULL)
1560 file=close_utf8(file);
1561 (void) ThrowMagickException(exception,GetMagickModule(),
1562 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1565 map=MapBlob(file,ReadMode,0,*length);
1566 if (map != (
unsigned char *) NULL)
1568 (void) memcpy(blob,map,*length);
1569 (void) UnmapBlob(map,*length);
1573 (void) lseek(file,0,SEEK_SET);
1574 for (i=0; i < *length; i+=(size_t) count)
1576 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1577 MagickMaxBufferExtent));
1587 file=close_utf8(file)-1;
1588 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1589 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1594 if (LocaleCompare(filename,
"-") != 0)
1595 file=close_utf8(file);
1598 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1599 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1629static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1630 const void *magick_restrict data)
1633 *magick_restrict blob_info;
1641 assert(image->blob != (BlobInfo *) NULL);
1642 assert(image->blob->type != UndefinedStream);
1643 assert(data != NULL);
1644 blob_info=image->blob;
1645 if (blob_info->type != BlobStream)
1646 return(WriteBlob(image,length,(
const unsigned char *) data));
1647 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1652 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1653 if (extent >= blob_info->extent)
1655 extent+=blob_info->quantum+length;
1656 blob_info->quantum<<=1;
1657 if (SetBlobExtent(image,extent) == MagickFalse)
1660 q=blob_info->data+blob_info->offset;
1661 (void) memcpy(q,data,length);
1662 blob_info->offset+=(MagickOffsetType) length;
1663 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1664 blob_info->length=(size_t) blob_info->offset;
1665 return((ssize_t) length);
1668MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1669 ExceptionInfo *exception)
1687 assert(image != (
const Image *) NULL);
1688 assert(image->signature == MagickCoreSignature);
1689 assert(filename != (
const char *) NULL);
1690 if (IsEventLogging() != MagickFalse)
1691 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1692 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1695 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1696 "NotAuthorized",
"`%s'",filename);
1697 return(MagickFalse);
1700 if (LocaleCompare(filename,
"-") != 0)
1703 flags = O_RDONLY | O_BINARY;
1705 file=open_utf8(filename,flags,0);
1709 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1710 return(MagickFalse);
1712 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1714 file=close_utf8(file);
1716 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1717 "NotAuthorized",
"`%s'",filename);
1718 return(MagickFalse);
1720 quantum=(size_t) MagickMaxBufferExtent;
1721 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1722 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1723 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1724 if (blob == (
unsigned char *) NULL)
1726 file=close_utf8(file);
1727 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1729 return(MagickFalse);
1733 count=read(file,blob,quantum);
1740 length=(size_t) count;
1741 count=WriteBlobStream(image,length,blob);
1742 if (count != (ssize_t) length)
1744 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1748 file=close_utf8(file);
1750 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1751 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1778MagickExport MagickBooleanType GetBlobError(
const Image *image)
1780 assert(image != (
const Image *) NULL);
1781 assert(image->signature == MagickCoreSignature);
1782 if (IsEventLogging() != MagickFalse)
1783 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1784 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1785 errno=image->blob->error_number;
1786 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1811MagickExport FILE *GetBlobFileHandle(
const Image *image)
1813 assert(image != (
const Image *) NULL);
1814 assert(image->signature == MagickCoreSignature);
1815 return(image->blob->file_info.file);
1840MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1842 assert(blob_info != (BlobInfo *) NULL);
1843 (void) memset(blob_info,0,
sizeof(*blob_info));
1844 blob_info->type=UndefinedStream;
1845 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1846 blob_info->properties.st_mtime=GetMagickTime();
1847 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1848 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1849 blob_info->reference_count=1;
1850 blob_info->semaphore=AcquireSemaphoreInfo();
1851 blob_info->signature=MagickCoreSignature;
1876MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1878 assert(image != (Image *) NULL);
1879 assert(image->signature == MagickCoreSignature);
1880 if (IsEventLogging() != MagickFalse)
1881 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1882 return(&image->blob->properties);
1908MagickExport MagickSizeType GetBlobSize(
const Image *image)
1911 *magick_restrict blob_info;
1916 assert(image != (Image *) NULL);
1917 assert(image->signature == MagickCoreSignature);
1918 assert(image->blob != (BlobInfo *) NULL);
1919 if (IsEventLogging() != MagickFalse)
1920 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1921 blob_info=image->blob;
1923 switch (blob_info->type)
1925 case UndefinedStream:
1926 case StandardStream:
1928 extent=blob_info->size;
1936 extent=(MagickSizeType) blob_info->properties.st_size;
1938 extent=blob_info->size;
1939 file_descriptor=fileno(blob_info->file_info.file);
1940 if (file_descriptor == -1)
1942 if (fstat(file_descriptor,&blob_info->properties) == 0)
1943 extent=(MagickSizeType) blob_info->properties.st_size;
1948 extent=blob_info->size;
1957 status=GetPathAttributes(image->filename,&blob_info->properties);
1958 if (status != MagickFalse)
1959 extent=(MagickSizeType) blob_info->properties.st_size;
1966 extent=(MagickSizeType) blob_info->length;
1971 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1972 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1977 offset=blob_info->custom_stream->teller(
1978 blob_info->custom_stream->data);
1979 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1980 blob_info->custom_stream->data);
1981 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1982 blob_info->custom_stream->data);
2012MagickExport
void *GetBlobStreamData(
const Image *image)
2014 assert(image != (
const Image *) NULL);
2015 assert(image->signature == MagickCoreSignature);
2016 return(image->blob->data);
2041MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2043 assert(image != (
const Image *) NULL);
2044 assert(image->signature == MagickCoreSignature);
2045 if (IsEventLogging() != MagickFalse)
2046 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2047 return(image->blob->stream);
2083MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2084 Image *image,
size_t *length,ExceptionInfo *exception)
2098 assert(image_info != (
const ImageInfo *) NULL);
2099 assert(image_info->signature == MagickCoreSignature);
2100 assert(image != (Image *) NULL);
2101 assert(image->signature == MagickCoreSignature);
2102 assert(exception != (ExceptionInfo *) NULL);
2103 assert(exception->signature == MagickCoreSignature);
2104 if (IsEventLogging() != MagickFalse)
2105 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2106 image_info->filename);
2108 blob=(
unsigned char *) NULL;
2109 blob_info=CloneImageInfo(image_info);
2110 blob_info->adjoin=MagickFalse;
2111 (void) SetImageInfo(blob_info,1,exception);
2112 if (*blob_info->magick !=
'\0')
2113 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2114 magick_info=GetMagickInfo(image->magick,exception);
2115 if (magick_info == (
const MagickInfo *) NULL)
2117 (void) ThrowMagickException(exception,GetMagickModule(),
2118 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2120 blob_info=DestroyImageInfo(blob_info);
2123 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2124 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2129 blob_info->length=0;
2130 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2131 sizeof(
unsigned char));
2132 if (blob_info->blob == NULL)
2133 (void) ThrowMagickException(exception,GetMagickModule(),
2134 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2137 (void) CloseBlob(image);
2138 image->blob->exempt=MagickTrue;
2139 image->blob->extent=0;
2140 *image->filename=
'\0';
2141 status=WriteImage(blob_info,image,exception);
2142 *length=image->blob->length;
2143 blob=DetachBlob(image->blob);
2144 if (blob != (
void *) NULL)
2146 if (status == MagickFalse)
2147 blob=RelinquishMagickMemory(blob);
2149 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2151 else if ((status == MagickFalse) && (image->blob->extent == 0))
2152 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2158 unique[MagickPathExtent];
2166 file=AcquireUniqueFileResource(unique);
2169 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2170 image_info->filename);
2174 blob_info->file=fdopen(file,
"wb");
2175 if (blob_info->file != (FILE *) NULL)
2177 (void) FormatLocaleString(image->filename,MagickPathExtent,
2178 "%s:%s",image->magick,unique);
2179 status=WriteImage(blob_info,image,exception);
2180 (void) fclose(blob_info->file);
2181 if (status != MagickFalse)
2182 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2184 (void) RelinquishUniqueFileResource(unique);
2187 blob_info=DestroyImageInfo(blob_info);
2219MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2220 ExceptionInfo *exception)
2232 assert(image_info != (
const ImageInfo *) NULL);
2233 assert(image_info->signature == MagickCoreSignature);
2234 assert(image != (Image *) NULL);
2235 assert(image->signature == MagickCoreSignature);
2236 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2237 assert(image_info->custom_stream->signature == MagickCoreSignature);
2238 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2239 assert(exception != (ExceptionInfo *) NULL);
2240 if (IsEventLogging() != MagickFalse)
2241 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2242 image_info->filename);
2243 clone_info=CloneImageInfo(image_info);
2244 clone_info->adjoin=MagickFalse;
2245 (void) SetImageInfo(clone_info,1,exception);
2246 if (*clone_info->magick !=
'\0')
2247 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2248 magick_info=GetMagickInfo(image->magick,exception);
2249 if (magick_info == (
const MagickInfo *) NULL)
2251 (void) ThrowMagickException(exception,GetMagickModule(),
2252 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2254 clone_info=DestroyImageInfo(clone_info);
2257 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2258 blob_support=GetMagickBlobSupport(magick_info);
2259 if ((blob_support != MagickFalse) &&
2260 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2262 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2263 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2264 blob_support=MagickFalse;
2266 if (blob_support != MagickFalse)
2271 (void) CloseBlob(image);
2272 *image->filename=
'\0';
2273 (void) WriteImage(clone_info,image,exception);
2278 unique[MagickPathExtent];
2289 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2290 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2292 if (blob == (
unsigned char *) NULL)
2294 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2295 image_info->filename);
2296 clone_info=DestroyImageInfo(clone_info);
2299 file=AcquireUniqueFileResource(unique);
2302 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2303 image_info->filename);
2304 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2305 clone_info=DestroyImageInfo(clone_info);
2308 clone_info->file=fdopen(file,
"wb+");
2309 if (clone_info->file != (FILE *) NULL)
2314 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2315 image->magick,unique);
2316 status=WriteImage(clone_info,image,exception);
2317 if (status != MagickFalse)
2319 (void) fseek(clone_info->file,0,SEEK_SET);
2320 count=(ssize_t) MagickMaxBufferExtent;
2321 while (count == (ssize_t) MagickMaxBufferExtent)
2323 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2325 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2326 image_info->custom_stream->data);
2329 (void) fclose(clone_info->file);
2331 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2332 (void) RelinquishUniqueFileResource(unique);
2334 clone_info=DestroyImageInfo(clone_info);
2365MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2366 ExceptionInfo *exception)
2390 assert(image != (Image *) NULL);
2391 assert(image->signature == MagickCoreSignature);
2392 assert(image->blob != (BlobInfo *) NULL);
2393 assert(image->blob->type != UndefinedStream);
2394 assert(filename != (
const char *) NULL);
2395 if (IsEventLogging() != MagickFalse)
2396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2397 if (*filename ==
'\0')
2398 file=AcquireUniqueFileResource(filename);
2400 if (LocaleCompare(filename,
"-") == 0)
2401 file=fileno(stdout);
2403 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2406 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2407 return(MagickFalse);
2409 quantum=(size_t) MagickMaxBufferExtent;
2410 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2411 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2412 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2413 if (buffer == (
unsigned char *) NULL)
2415 file=close_utf8(file)-1;
2416 (void) ThrowMagickException(exception,GetMagickModule(),
2417 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2418 return(MagickFalse);
2421 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2422 for (i=0; count > 0; )
2424 length=(size_t) count;
2425 for (i=0; i < length; i+=(size_t) count)
2427 count=write(file,p+i,(
size_t) (length-i));
2437 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2439 if (LocaleCompare(filename,
"-") != 0)
2440 file=close_utf8(file);
2441 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2442 if ((file == -1) || (i < length))
2445 file=close_utf8(file);
2446 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2447 return(MagickFalse);
2487MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2488 size_t *length,ExceptionInfo *exception)
2502 assert(image_info != (
const ImageInfo *) NULL);
2503 assert(image_info->signature == MagickCoreSignature);
2504 assert(images != (Image *) NULL);
2505 assert(images->signature == MagickCoreSignature);
2506 assert(exception != (ExceptionInfo *) NULL);
2507 if (IsEventLogging() != MagickFalse)
2508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2509 image_info->filename);
2511 blob=(
unsigned char *) NULL;
2512 blob_info=CloneImageInfo(image_info);
2513 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2515 if (*blob_info->magick !=
'\0')
2516 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2517 magick_info=GetMagickInfo(images->magick,exception);
2518 if (magick_info == (
const MagickInfo *) NULL)
2520 (void) ThrowMagickException(exception,GetMagickModule(),
2521 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2523 blob_info=DestroyImageInfo(blob_info);
2526 if (GetMagickAdjoin(magick_info) == MagickFalse)
2528 blob_info=DestroyImageInfo(blob_info);
2529 return(ImageToBlob(image_info,images,length,exception));
2531 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2532 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2537 blob_info->length=0;
2538 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2539 sizeof(
unsigned char));
2540 if (blob_info->blob == (
void *) NULL)
2541 (void) ThrowMagickException(exception,GetMagickModule(),
2542 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2545 (void) CloseBlob(images);
2546 images->blob->exempt=MagickTrue;
2547 images->blob->extent=0;
2548 *images->filename=
'\0';
2549 status=WriteImages(blob_info,images,images->filename,exception);
2550 *length=images->blob->length;
2551 blob=DetachBlob(images->blob);
2552 if (blob != (
void *) NULL)
2554 if (status == MagickFalse)
2555 blob=RelinquishMagickMemory(blob);
2557 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2559 else if ((status == MagickFalse) && (images->blob->extent == 0))
2560 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2566 filename[MagickPathExtent],
2567 unique[MagickPathExtent];
2575 file=AcquireUniqueFileResource(unique);
2578 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2579 image_info->filename);
2583 blob_info->file=fdopen(file,
"wb");
2584 if (blob_info->file != (FILE *) NULL)
2586 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2587 images->magick,unique);
2588 status=WriteImages(blob_info,images,filename,exception);
2589 (void) fclose(blob_info->file);
2590 if (status != MagickFalse)
2591 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2593 (void) RelinquishUniqueFileResource(unique);
2596 blob_info=DestroyImageInfo(blob_info);
2628MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2629 Image *images,ExceptionInfo *exception)
2641 assert(image_info != (
const ImageInfo *) NULL);
2642 assert(image_info->signature == MagickCoreSignature);
2643 assert(images != (Image *) NULL);
2644 assert(images->signature == MagickCoreSignature);
2645 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2646 assert(image_info->custom_stream->signature == MagickCoreSignature);
2647 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2648 assert(exception != (ExceptionInfo *) NULL);
2649 if (IsEventLogging() != MagickFalse)
2650 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2651 image_info->filename);
2652 clone_info=CloneImageInfo(image_info);
2653 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2655 if (*clone_info->magick !=
'\0')
2656 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2657 magick_info=GetMagickInfo(images->magick,exception);
2658 if (magick_info == (
const MagickInfo *) NULL)
2660 (void) ThrowMagickException(exception,GetMagickModule(),
2661 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2663 clone_info=DestroyImageInfo(clone_info);
2666 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2667 blob_support=GetMagickBlobSupport(magick_info);
2668 if ((blob_support != MagickFalse) &&
2669 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2671 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2672 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2673 blob_support=MagickFalse;
2675 if (blob_support != MagickFalse)
2680 (void) CloseBlob(images);
2681 *images->filename=
'\0';
2682 (void) WriteImages(clone_info,images,images->filename,exception);
2687 filename[MagickPathExtent],
2688 unique[MagickPathExtent];
2699 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2700 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2702 if (blob == (
unsigned char *) NULL)
2704 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2705 image_info->filename);
2706 clone_info=DestroyImageInfo(clone_info);
2709 file=AcquireUniqueFileResource(unique);
2712 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2713 image_info->filename);
2714 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2715 clone_info=DestroyImageInfo(clone_info);
2718 clone_info->file=fdopen(file,
"wb+");
2719 if (clone_info->file != (FILE *) NULL)
2724 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2725 images->magick,unique);
2726 status=WriteImages(clone_info,images,filename,exception);
2727 if (status != MagickFalse)
2729 (void) fseek(clone_info->file,0,SEEK_SET);
2730 count=(ssize_t) MagickMaxBufferExtent;
2731 while (count == (ssize_t) MagickMaxBufferExtent)
2733 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2735 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2736 image_info->custom_stream->data);
2739 (void) fclose(clone_info->file);
2741 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2742 (void) RelinquishUniqueFileResource(unique);
2744 clone_info=DestroyImageInfo(clone_info);
2780MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2781 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2784 filename[MagickPathExtent];
2813 assert(image_info != (ImageInfo *) NULL);
2814 assert(image_info->signature == MagickCoreSignature);
2815 assert(image != (Image *) NULL);
2816 assert(image->signature == MagickCoreSignature);
2817 assert(inject_image != (Image *) NULL);
2818 assert(inject_image->signature == MagickCoreSignature);
2819 assert(exception != (ExceptionInfo *) NULL);
2820 if (IsEventLogging() != MagickFalse)
2821 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2822 unique_file=(FILE *) NULL;
2823 file=AcquireUniqueFileResource(filename);
2825 unique_file=fdopen(file,
"wb");
2826 if ((file == -1) || (unique_file == (FILE *) NULL))
2828 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2829 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2831 return(MagickFalse);
2833 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2834 if (byte_image == (Image *) NULL)
2836 (void) fclose(unique_file);
2837 (void) RelinquishUniqueFileResource(filename);
2838 return(MagickFalse);
2840 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2842 DestroyBlob(byte_image);
2843 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2844 write_info=CloneImageInfo(image_info);
2845 SetImageInfoFile(write_info,unique_file);
2846 status=WriteImage(write_info,byte_image,exception);
2847 write_info=DestroyImageInfo(write_info);
2848 byte_image=DestroyImage(byte_image);
2849 (void) fclose(unique_file);
2850 if (status == MagickFalse)
2852 (void) RelinquishUniqueFileResource(filename);
2853 return(MagickFalse);
2858 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2861 (void) RelinquishUniqueFileResource(filename);
2862 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2863 image_info->filename);
2864 return(MagickFalse);
2866 quantum=(size_t) MagickMaxBufferExtent;
2867 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2868 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2869 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2870 if (buffer == (
unsigned char *) NULL)
2872 (void) RelinquishUniqueFileResource(filename);
2873 file=close_utf8(file);
2874 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2879 ssize_t count = read(file,buffer,quantum);
2886 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2889 file=close_utf8(file);
2891 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2892 (void) RelinquishUniqueFileResource(filename);
2893 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2919MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2921 assert(image != (
const Image *) NULL);
2922 assert(image->signature == MagickCoreSignature);
2923 if (IsEventLogging() != MagickFalse)
2924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2925 return(image->blob->exempt);
2950MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2953 *magick_restrict blob_info;
2955 assert(image != (
const Image *) NULL);
2956 assert(image->signature == MagickCoreSignature);
2957 if (IsEventLogging() != MagickFalse)
2958 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2959 blob_info=image->blob;
2960 switch (blob_info->type)
2969 if (blob_info->file_info.file == (FILE *) NULL)
2970 return(MagickFalse);
2971 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2972 return(status == -1 ? MagickFalse : MagickTrue);
2976#if defined(MAGICKCORE_ZLIB_DELEGATE)
2980 if (blob_info->file_info.gzfile == (gzFile) NULL)
2981 return(MagickFalse);
2982 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2983 return(offset < 0 ? MagickFalse : MagickTrue);
2988 case UndefinedStream:
2992 case StandardStream:
2996 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
2997 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3004 return(MagickFalse);
3029MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3031 assert(image != (
const Image *) NULL);
3032 assert(image->signature == MagickCoreSignature);
3033 if (IsEventLogging() != MagickFalse)
3034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3035 return(image->blob->temporary);
3067MagickExport
void *MapBlob(
int file,
const MapMode mode,
3068 const MagickOffsetType offset,
const size_t length)
3070#if defined(MAGICKCORE_HAVE_MMAP)
3083#if defined(MAP_ANONYMOUS)
3084 flags|=MAP_ANONYMOUS;
3093 protection=PROT_READ;
3099 protection=PROT_WRITE;
3105 protection=PROT_READ | PROT_WRITE;
3110#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3111 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3113 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3114 if (map == MAP_FAILED)
3115 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3117 if (map == MAP_FAILED)
3154MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3163 assert(buffer != (
unsigned char *) NULL);
3170 *buffer++=(
unsigned char) c;
3174 *buffer++=(
unsigned char) c;
3204MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3212 assert(p != (
unsigned char *) NULL);
3219 *p++=(
unsigned char) c;
3255static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3256 const BlobInfo *blob_info)
3267 size=MagickMinBufferExtent;
3268 option=GetImageOption(image_info,
"stream:buffer-size");
3269 if (option != (
const char *) NULL)
3270 size=StringToUnsignedLong(option);
3271 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3272 _IONBF : _IOFBF,size);
3273 return(status == 0 ? MagickTrue : MagickFalse);
3276#if defined(MAGICKCORE_ZLIB_DELEGATE)
3277static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3279#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3280 return(gzopen(path,mode));
3288 path_wide=NTCreateWidePath(path);
3289 if (path_wide == (
wchar_t *) NULL)
3290 return((gzFile) NULL);
3291 file=gzopen_w(path_wide,mode);
3292 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3298MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3299 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3302 *magick_restrict blob_info;
3305 extension[MagickPathExtent],
3306 filename[MagickPathExtent];
3320 assert(image_info != (ImageInfo *) NULL);
3321 assert(image_info->signature == MagickCoreSignature);
3322 assert(image != (Image *) NULL);
3323 assert(image->signature == MagickCoreSignature);
3324 if (IsEventLogging() != MagickFalse)
3325 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3326 image_info->filename);
3327 blob_info=image->blob;
3328 if (image_info->blob != (
void *) NULL)
3330 if (image_info->stream != (StreamHandler) NULL)
3331 blob_info->stream=(StreamHandler) image_info->stream;
3332 AttachBlob(blob_info,image_info->blob,image_info->length);
3335 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3336 (*image->filename ==
'\0'))
3338 blob_info->type=CustomStream;
3339 blob_info->custom_stream=image_info->custom_stream;
3342 (void) DetachBlob(blob_info);
3343 blob_info->mode=mode;
3352 case ReadBinaryBlobMode:
3354 flags=O_RDONLY | O_BINARY;
3360 flags=O_WRONLY | O_CREAT | O_TRUNC;
3364 case WriteBinaryBlobMode:
3366 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3370 case AppendBlobMode:
3372 flags=O_WRONLY | O_CREAT | O_APPEND;
3376 case AppendBinaryBlobMode:
3378 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3390 blob_info->synchronize=image_info->synchronize;
3391 if (image_info->stream != (StreamHandler) NULL)
3393 blob_info->stream=image_info->stream;
3396 blob_info->type=FifoStream;
3404 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3405 rights=ReadPolicyRights;
3407 rights=WritePolicyRights;
3408 if (IsPathAuthorized(rights,filename) == MagickFalse)
3411 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3412 "NotAuthorized",
"`%s'",filename);
3413 return(MagickFalse);
3415 if ((LocaleCompare(filename,
"-") == 0) ||
3416 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3418 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3419#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3420 if (strchr(type,
'b') != (
char *) NULL)
3421 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3423 blob_info->type=StandardStream;
3424 blob_info->exempt=MagickTrue;
3425 return(SetStreamBuffering(image_info,blob_info));
3427 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3428 (IsGeometry(filename+3) != MagickFalse))
3435 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3436 if (blob_info->file_info.file == (FILE *) NULL)
3438 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3439 return(MagickFalse);
3441#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3442 if (strchr(type,
'b') != (
char *) NULL)
3443 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3445 blob_info->type=FileStream;
3446 blob_info->exempt=MagickTrue;
3447 return(SetStreamBuffering(image_info,blob_info));
3449#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3450 if (*filename ==
'|')
3453 fileMode[MagickPathExtent],
3461 (void) signal(SIGPIPE,SIG_IGN);
3465 sanitize_command=SanitizeString(filename+1);
3466 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3467 sanitize_command=DestroyString(sanitize_command);
3468 if (blob_info->file_info.file == (FILE *) NULL)
3470 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3471 return(MagickFalse);
3473 blob_info->type=PipeStream;
3474 blob_info->exempt=MagickTrue;
3475 return(SetStreamBuffering(image_info,blob_info));
3478 status=GetPathAttributes(filename,&blob_info->properties);
3479#if defined(S_ISFIFO)
3480 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3482 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3483 if (blob_info->file_info.file == (FILE *) NULL)
3485 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3486 return(MagickFalse);
3488 blob_info->type=FileStream;
3489 blob_info->exempt=MagickTrue;
3490 return(SetStreamBuffering(image_info,blob_info));
3493 GetPathComponent(image->filename,ExtensionPath,extension);
3496 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3497 if ((image_info->adjoin == MagickFalse) ||
3498 (strchr(filename,
'%') != (
char *) NULL))
3503 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3504 image->scene,filename,exception);
3505 if ((LocaleCompare(filename,image->filename) == 0) &&
3506 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3507 (GetNextImageInList(image) != (Image *) NULL)))
3510 path[MagickPathExtent];
3512 GetPathComponent(image->filename,RootPath,path);
3513 if (*extension ==
'\0')
3514 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3515 path,(
double) image->scene);
3517 (
void) FormatLocaleString(filename,MagickPathExtent,
3518 "%s-%.20g.%s",path,(
double) image->scene,extension);
3520 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3522 if (IsPathAuthorized(rights,filename) == MagickFalse)
3525 (void) ThrowMagickException(exception,GetMagickModule(),
3526 PolicyError,
"NotAuthorized",
"`%s'",filename);
3527 return(MagickFalse);
3530 if (image_info->file != (FILE *) NULL)
3532 blob_info->file_info.file=image_info->file;
3533 blob_info->type=FileStream;
3534 blob_info->exempt=MagickTrue;
3542 blob_info->file_info.file=(FILE *) NULL;
3543 file=open_utf8(filename,flags,0);
3545 blob_info->file_info.file=fdopen(file,type);
3546 if (blob_info->file_info.file != (FILE *) NULL)
3554 blob_info->type=FileStream;
3555 (void) SetStreamBuffering(image_info,blob_info);
3556 (void) memset(magick,0,
sizeof(magick));
3557 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3558 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3559#if defined(MAGICKCORE_POSIX_SUPPORT)
3560 (void) fflush(blob_info->file_info.file);
3562 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3563 " read %.20g magic header bytes",(
double) count);
3564#if defined(MAGICKCORE_ZLIB_DELEGATE)
3565 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3566 ((
int) magick[2] == 0x08))
3569 gzfile = gzopen_utf8(filename,
"rb");
3571 if (gzfile != (gzFile) NULL)
3573 if (blob_info->file_info.file != (FILE *) NULL)
3574 (void) fclose(blob_info->file_info.file);
3575 blob_info->file_info.file=(FILE *) NULL;
3576 blob_info->file_info.gzfile=gzfile;
3577 blob_info->type=ZipStream;
3581#if defined(MAGICKCORE_BZLIB_DELEGATE)
3582 if (strncmp((
char *) magick,
"BZh",3) == 0)
3585 *bzfile = BZ2_bzopen(filename,
"r");
3587 if (bzfile != (BZFILE *) NULL)
3589 if (blob_info->file_info.file != (FILE *) NULL)
3590 (void) fclose(blob_info->file_info.file);
3591 blob_info->file_info.file=(FILE *) NULL;
3592 blob_info->file_info.bzfile=bzfile;
3593 blob_info->type=BZipStream;
3597 if (blob_info->type == FileStream)
3608 sans_exception=AcquireExceptionInfo();
3609 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3610 sans_exception=DestroyExceptionInfo(sans_exception);
3611 length=(size_t) blob_info->properties.st_size;
3612 if ((magick_info != (
const MagickInfo *) NULL) &&
3613 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3614 (AcquireMagickResource(MapResource,length) != MagickFalse))
3619 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3621 if (blob == (
void *) NULL)
3622 RelinquishMagickResource(MapResource,length);
3628 if (image_info->file != (FILE *) NULL)
3629 blob_info->exempt=MagickFalse;
3632 (void) fclose(blob_info->file_info.file);
3633 blob_info->file_info.file=(FILE *) NULL;
3635 AttachBlob(blob_info,blob,length);
3636 blob_info->mapped=MagickTrue;
3643#if defined(MAGICKCORE_ZLIB_DELEGATE)
3644 if ((LocaleCompare(extension,
"gz") == 0) ||
3645 (LocaleCompare(extension,
"wmz") == 0) ||
3646 (LocaleCompare(extension,
"svgz") == 0))
3648 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3649 if (blob_info->file_info.gzfile != (gzFile) NULL)
3650 blob_info->type=ZipStream;
3654#if defined(MAGICKCORE_BZLIB_DELEGATE)
3655 if (LocaleCompare(extension,
"bz2") == 0)
3657 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3658 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3659 blob_info->type=BZipStream;
3667 blob_info->file_info.file=(FILE *) NULL;
3668 file=open_utf8(filename,flags,P_MODE);
3670 blob_info->file_info.file=fdopen(file,type);
3671 if (blob_info->file_info.file != (FILE *) NULL)
3673 blob_info->type=FileStream;
3674 (void) SetStreamBuffering(image_info,blob_info);
3677 if (IsPathAuthorized(rights,filename) == MagickFalse)
3680 (void) ThrowMagickException(exception,GetMagickModule(),
3681 PolicyError,
"NotAuthorized",
"`%s'",filename);
3682 return(MagickFalse);
3684 blob_info->status=0;
3685 blob_info->error_number=0;
3686 if (blob_info->type != UndefinedStream)
3687 blob_info->size=GetBlobSize(image);
3690 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3691 return(MagickFalse);
3730#if defined(__cplusplus) || defined(c_plusplus)
3734static size_t PingStream(
const Image *magick_unused(image),
3735 const void *magick_unused(pixels),
const size_t columns)
3737 magick_unreferenced(image);
3738 magick_unreferenced(pixels);
3742#if defined(__cplusplus) || defined(c_plusplus)
3746MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3747 const size_t length,ExceptionInfo *exception)
3762 assert(image_info != (ImageInfo *) NULL);
3763 assert(image_info->signature == MagickCoreSignature);
3764 assert(exception != (ExceptionInfo *) NULL);
3765 if (IsEventLogging() != MagickFalse)
3766 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3767 image_info->filename);
3768 if ((blob == (
const void *) NULL) || (length == 0))
3770 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3771 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3772 return((Image *) NULL);
3774 ping_info=CloneImageInfo(image_info);
3775 ping_info->blob=(
void *) blob;
3776 ping_info->length=length;
3777 ping_info->ping=MagickTrue;
3778 if (*ping_info->magick ==
'\0')
3779 (void) SetImageInfo(ping_info,0,exception);
3780 magick_info=GetMagickInfo(ping_info->magick,exception);
3781 if (magick_info == (
const MagickInfo *) NULL)
3783 (void) ThrowMagickException(exception,GetMagickModule(),
3784 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3786 ping_info=DestroyImageInfo(ping_info);
3787 return((Image *) NULL);
3789 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3792 filename[MagickPathExtent];
3797 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3798 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3799 ping_info->magick,filename);
3800 image=ReadStream(ping_info,&PingStream,exception);
3801 if (image != (Image *) NULL)
3802 (void) DetachBlob(image->blob);
3803 ping_info=DestroyImageInfo(ping_info);
3809 ping_info->blob=(
void *) NULL;
3810 ping_info->length=0;
3811 *ping_info->filename=
'\0';
3812 status=BlobToFile(ping_info->filename,blob,length,exception);
3813 if (status == MagickFalse)
3815 (void) RelinquishUniqueFileResource(ping_info->filename);
3816 ping_info=DestroyImageInfo(ping_info);
3817 return((Image *) NULL);
3819 clone_info=CloneImageInfo(ping_info);
3820 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3821 ping_info->magick,ping_info->filename);
3822 image=ReadStream(clone_info,&PingStream,exception);
3823 if (image != (Image *) NULL)
3831 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3833 (void) CopyMagickString(images->filename,image_info->filename,
3835 (void) CopyMagickString(images->magick_filename,image_info->filename,
3837 (void) CopyMagickString(images->magick,magick_info->name,
3839 images=GetNextImageInList(images);
3842 clone_info=DestroyImageInfo(clone_info);
3843 (void) RelinquishUniqueFileResource(ping_info->filename);
3844 ping_info=DestroyImageInfo(ping_info);
3879MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3882 *magick_restrict blob_info;
3893 assert(image != (Image *) NULL);
3894 assert(image->signature == MagickCoreSignature);
3895 assert(image->blob != (BlobInfo *) NULL);
3896 assert(image->blob->type != UndefinedStream);
3899 assert(data != (
void *) NULL);
3900 blob_info=image->blob;
3902 q=(
unsigned char *) data;
3903 switch (blob_info->type)
3905 case UndefinedStream:
3907 case StandardStream:
3915 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3920 c=getc(blob_info->file_info.file);
3923 *q++=(
unsigned char) c;
3929 c=getc(blob_info->file_info.file);
3932 *q++=(
unsigned char) c;
3938 c=getc(blob_info->file_info.file);
3941 *q++=(
unsigned char) c;
3947 c=getc(blob_info->file_info.file);
3950 *q++=(
unsigned char) c;
3957 if ((count != (ssize_t) length) &&
3958 (ferror(blob_info->file_info.file) != 0))
3959 ThrowBlobException(blob_info);
3964#if defined(MAGICKCORE_ZLIB_DELEGATE)
3975 for (i=0; i < length; i+=(size_t) count)
3977 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3978 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3991 c=gzgetc(blob_info->file_info.gzfile);
3994 *q++=(
unsigned char) c;
4000 c=gzgetc(blob_info->file_info.gzfile);
4003 *q++=(
unsigned char) c;
4009 c=gzgetc(blob_info->file_info.gzfile);
4012 *q++=(
unsigned char) c;
4018 c=gzgetc(blob_info->file_info.gzfile);
4021 *q++=(
unsigned char) c;
4028 (void) gzerror(blob_info->file_info.gzfile,&status);
4029 if ((count != (ssize_t) length) && (status != Z_OK))
4030 ThrowBlobException(blob_info);
4031 if (blob_info->eof == MagickFalse)
4032 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4039#if defined(MAGICKCORE_BZLIB_DELEGATE)
4046 for (i=0; i < length; i+=(size_t) count)
4048 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4049 MagickMin(length-i,MagickMaxBufferExtent));
4059 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4060 if ((count != (ssize_t) length) && (status != BZ_OK))
4061 ThrowBlobException(blob_info);
4072 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4074 blob_info->eof=MagickTrue;
4077 p=blob_info->data+blob_info->offset;
4078 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4079 blob_info->length-blob_info->offset);
4080 blob_info->offset+=count;
4081 if (count != (ssize_t) length)
4082 blob_info->eof=MagickTrue;
4083 (void) memcpy(q,p,(
size_t) count);
4088 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4089 count=blob_info->custom_stream->reader(q,length,
4090 blob_info->custom_stream->data);
4119MagickExport
int ReadBlobByte(Image *image)
4122 *magick_restrict blob_info;
4127 assert(image != (Image *) NULL);
4128 assert(image->signature == MagickCoreSignature);
4129 assert(image->blob != (BlobInfo *) NULL);
4130 assert(image->blob->type != UndefinedStream);
4131 blob_info=image->blob;
4132 switch (blob_info->type)
4134 case StandardStream:
4138 c=getc(blob_info->file_info.file);
4141 if (ferror(blob_info->file_info.file) != 0)
4142 ThrowBlobException(blob_info);
4149 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4151 blob_info->eof=MagickTrue;
4154 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4155 blob_info->offset++;
4166 count=ReadBlob(image,1,buffer);
4199MagickExport
double ReadBlobDouble(Image *image)
4210 quantum.double_value=0.0;
4211 quantum.unsigned_value=ReadBlobLongLong(image);
4212 return(quantum.double_value);
4238MagickExport
float ReadBlobFloat(Image *image)
4249 quantum.float_value=0.0;
4250 quantum.unsigned_value=ReadBlobLong(image);
4251 return(quantum.float_value);
4277MagickExport
unsigned int ReadBlobLong(Image *image)
4291 assert(image != (Image *) NULL);
4292 assert(image->signature == MagickCoreSignature);
4294 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4297 if (image->endian == LSBEndian)
4299 value=(
unsigned int) (*p++);
4300 value|=(
unsigned int) (*p++) << 8;
4301 value|=(
unsigned int) (*p++) << 16;
4302 value|=(
unsigned int) (*p++) << 24;
4305 value=(
unsigned int) (*p++) << 24;
4306 value|=(
unsigned int) (*p++) << 16;
4307 value|=(
unsigned int) (*p++) << 8;
4308 value|=(
unsigned int) (*p++);
4335MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4349 assert(image != (Image *) NULL);
4350 assert(image->signature == MagickCoreSignature);
4352 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4354 return(MagickULLConstant(0));
4355 if (image->endian == LSBEndian)
4357 value=(MagickSizeType) (*p++);
4358 value|=(MagickSizeType) (*p++) << 8;
4359 value|=(MagickSizeType) (*p++) << 16;
4360 value|=(MagickSizeType) (*p++) << 24;
4361 value|=(MagickSizeType) (*p++) << 32;
4362 value|=(MagickSizeType) (*p++) << 40;
4363 value|=(MagickSizeType) (*p++) << 48;
4364 value|=(MagickSizeType) (*p++) << 56;
4367 value=(MagickSizeType) (*p++) << 56;
4368 value|=(MagickSizeType) (*p++) << 48;
4369 value|=(MagickSizeType) (*p++) << 40;
4370 value|=(MagickSizeType) (*p++) << 32;
4371 value|=(MagickSizeType) (*p++) << 24;
4372 value|=(MagickSizeType) (*p++) << 16;
4373 value|=(MagickSizeType) (*p++) << 8;
4374 value|=(MagickSizeType) (*p++);
4401MagickExport
unsigned short ReadBlobShort(Image *image)
4415 assert(image != (Image *) NULL);
4416 assert(image->signature == MagickCoreSignature);
4418 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4420 return((
unsigned short) 0U);
4421 if (image->endian == LSBEndian)
4423 value=(
unsigned short) (*p++);
4424 value|=(
unsigned short) (*p++) << 8;
4427 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4428 value|=(
unsigned short) (*p++);
4455MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4469 assert(image != (Image *) NULL);
4470 assert(image->signature == MagickCoreSignature);
4472 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4475 value=(
unsigned int) (*p++);
4476 value|=(
unsigned int) (*p++) << 8;
4477 value|=(
unsigned int) (*p++) << 16;
4478 value|=(
unsigned int) (*p++) << 24;
4505MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4516 quantum.unsigned_value=ReadBlobLSBLong(image);
4517 return(quantum.signed_value);
4543MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4557 assert(image != (Image *) NULL);
4558 assert(image->signature == MagickCoreSignature);
4560 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4562 return((
unsigned short) 0U);
4563 value=(
unsigned short) (*p++);
4564 value|=(
unsigned short) (*p++) << 8;
4591MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4602 quantum.unsigned_value=ReadBlobLSBShort(image);
4603 return(quantum.signed_value);
4629MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4643 assert(image != (Image *) NULL);
4644 assert(image->signature == MagickCoreSignature);
4646 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4649 value=(
unsigned int) (*p++) << 24;
4650 value|=(
unsigned int) (*p++) << 16;
4651 value|=(
unsigned int) (*p++) << 8;
4652 value|=(
unsigned int) (*p++);
4679MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4693 assert(image != (Image *) NULL);
4694 assert(image->signature == MagickCoreSignature);
4696 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4698 return(MagickULLConstant(0));
4699 value=(MagickSizeType) (*p++) << 56;
4700 value|=(MagickSizeType) (*p++) << 48;
4701 value|=(MagickSizeType) (*p++) << 40;
4702 value|=(MagickSizeType) (*p++) << 32;
4703 value|=(MagickSizeType) (*p++) << 24;
4704 value|=(MagickSizeType) (*p++) << 16;
4705 value|=(MagickSizeType) (*p++) << 8;
4706 value|=(MagickSizeType) (*p++);
4733MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4747 assert(image != (Image *) NULL);
4748 assert(image->signature == MagickCoreSignature);
4750 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4752 return((
unsigned short) 0U);
4753 value=(
unsigned short) ((*p++) << 8);
4754 value|=(
unsigned short) (*p++);
4755 return((
unsigned short) (value & 0xffff));
4781MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4792 quantum.unsigned_value=ReadBlobMSBLong(image);
4793 return(quantum.signed_value);
4819MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4830 quantum.unsigned_value=ReadBlobMSBShort(image);
4831 return(quantum.signed_value);
4857MagickExport
signed int ReadBlobSignedLong(Image *image)
4868 quantum.unsigned_value=ReadBlobLong(image);
4869 return(quantum.signed_value);
4895MagickExport
signed short ReadBlobSignedShort(Image *image)
4906 quantum.unsigned_value=ReadBlobShort(image);
4907 return(quantum.signed_value);
4945MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4946 const size_t length,
void *magick_restrict data,ssize_t *count)
4949 *magick_restrict blob_info;
4951 assert(image != (Image *) NULL);
4952 assert(image->signature == MagickCoreSignature);
4953 assert(image->blob != (BlobInfo *) NULL);
4954 assert(image->blob->type != UndefinedStream);
4955 assert(count != (ssize_t *) NULL);
4956 blob_info=image->blob;
4957 if (blob_info->type != BlobStream)
4959 assert(data != NULL);
4960 *count=ReadBlob(image,length,(
unsigned char *) data);
4963 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4966 blob_info->eof=MagickTrue;
4969 data=blob_info->data+blob_info->offset;
4970 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4971 blob_info->length-blob_info->offset);
4972 blob_info->offset+=(*count);
4973 if (*count != (ssize_t) length)
4974 blob_info->eof=MagickTrue;
5003MagickExport
char *ReadBlobString(Image *image,
char *
string)
5006 *magick_restrict blob_info;
5014 assert(image != (Image *) NULL);
5015 assert(image->signature == MagickCoreSignature);
5016 assert(image->blob != (BlobInfo *) NULL);
5017 assert(image->blob->type != UndefinedStream);
5018 if (IsEventLogging() != MagickFalse)
5019 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5021 blob_info=image->blob;
5022 switch (blob_info->type)
5024 case UndefinedStream:
5026 case StandardStream:
5029 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5030 if (p == (
char *) NULL)
5032 if (ferror(blob_info->file_info.file) != 0)
5033 ThrowBlobException(blob_info);
5034 return((
char *) NULL);
5041#if defined(MAGICKCORE_ZLIB_DELEGATE)
5042 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5043 if (p == (
char *) NULL)
5046 (void) gzerror(blob_info->file_info.gzfile,&status);
5048 ThrowBlobException(blob_info);
5049 return((
char *) NULL);
5059 c=ReadBlobByte(image);
5062 blob_info->eof=MagickTrue;
5065 string[i++]=(char) c;
5068 }
while (i < (MaxTextExtent-2));
5076 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5079 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5081 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5082 return((
char *) NULL);
5109MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5111 assert(blob != (BlobInfo *) NULL);
5112 assert(blob->signature == MagickCoreSignature);
5113 if (IsEventLogging() != MagickFalse)
5114 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5115 LockSemaphoreInfo(blob->semaphore);
5116 blob->reference_count++;
5117 UnlockSemaphoreInfo(blob->semaphore);
5154MagickExport MagickOffsetType SeekBlob(Image *image,
5155 const MagickOffsetType offset,
const int whence)
5158 *magick_restrict blob_info;
5160 assert(image != (Image *) NULL);
5161 assert(image->signature == MagickCoreSignature);
5162 assert(image->blob != (BlobInfo *) NULL);
5163 assert(image->blob->type != UndefinedStream);
5164 if (IsEventLogging() != MagickFalse)
5165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5166 blob_info=image->blob;
5167 switch (blob_info->type)
5169 case UndefinedStream:
5171 case StandardStream:
5176 if ((offset < 0) && (whence == SEEK_SET))
5178 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5180 blob_info->offset=TellBlob(image);
5185#if defined(MAGICKCORE_ZLIB_DELEGATE)
5186 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5189 blob_info->offset=TellBlob(image);
5205 blob_info->offset=offset;
5210 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5211 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5216 if ((blob_info->offset+offset) < 0)
5218 blob_info->offset+=offset;
5223 if (((MagickOffsetType) blob_info->length+offset) < 0)
5225 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5229 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5231 blob_info->eof=MagickFalse;
5238 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5240 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5241 blob_info->custom_stream->data);
5245 return(blob_info->offset);
5273MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5275 assert(image != (
const Image *) NULL);
5276 assert(image->signature == MagickCoreSignature);
5277 if (IsEventLogging() != MagickFalse)
5278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5279 image->blob->exempt=exempt;
5308MagickExport MagickBooleanType SetBlobExtent(Image *image,
5309 const MagickSizeType extent)
5312 *magick_restrict blob_info;
5314 assert(image != (Image *) NULL);
5315 assert(image->signature == MagickCoreSignature);
5316 assert(image->blob != (BlobInfo *) NULL);
5317 assert(image->blob->type != UndefinedStream);
5318 if (IsEventLogging() != MagickFalse)
5319 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5320 blob_info=image->blob;
5321 switch (blob_info->type)
5323 case UndefinedStream:
5325 case StandardStream:
5326 return(MagickFalse);
5335 if (extent != (MagickSizeType) ((off_t) extent))
5336 return(MagickFalse);
5337 offset=SeekBlob(image,0,SEEK_END);
5339 return(MagickFalse);
5340 if ((MagickSizeType) offset >= extent)
5342 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5345 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5346 blob_info->file_info.file);
5347#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5348 if (blob_info->synchronize != MagickFalse)
5353 file=fileno(blob_info->file_info.file);
5354 if ((file == -1) || (offset < 0))
5355 return(MagickFalse);
5356 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5359 offset=SeekBlob(image,offset,SEEK_SET);
5361 return(MagickFalse);
5366 return(MagickFalse);
5368 return(MagickFalse);
5370 return(MagickFalse);
5373 if (extent != (MagickSizeType) ((
size_t) extent))
5374 return(MagickFalse);
5375 if (blob_info->mapped != MagickFalse)
5383 (void) UnmapBlob(blob_info->data,blob_info->length);
5384 RelinquishMagickResource(MapResource,blob_info->length);
5385 if (extent != (MagickSizeType) ((off_t) extent))
5386 return(MagickFalse);
5387 offset=SeekBlob(image,0,SEEK_END);
5389 return(MagickFalse);
5390 if ((MagickSizeType) offset >= extent)
5392 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-
5408 offset=SeekBlob(image,offset,SEEK_SET);
5410 return(MagickFalse);
5411 (void) AcquireMagickResource(MapResource,extent);
5412 blob_info->data=(
unsigned char*) MapBlob(fileno(
5413 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5414 blob_info->extent=(size_t) extent;
5415 blob_info->length=(size_t) extent;
5416 (void) SyncBlob(image);
5419 blob_info->extent=(size_t) extent;
5420 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5421 blob_info->extent+1,
sizeof(*blob_info->data));
5422 (void) SyncBlob(image);
5423 if (blob_info->data == (
unsigned char *) NULL)
5425 (void) DetachBlob(blob_info);
5426 return(MagickFalse);
5460MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5463 assert(custom_stream != (CustomStreamInfo *) NULL);
5464 assert(custom_stream->signature == MagickCoreSignature);
5465 custom_stream->data=data;
5493MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5494 CustomStreamHandler reader)
5496 assert(custom_stream != (CustomStreamInfo *) NULL);
5497 assert(custom_stream->signature == MagickCoreSignature);
5498 custom_stream->reader=reader;
5526MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5527 CustomStreamSeeker seeker)
5529 assert(custom_stream != (CustomStreamInfo *) NULL);
5530 assert(custom_stream->signature == MagickCoreSignature);
5531 custom_stream->seeker=seeker;
5559MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5560 CustomStreamTeller teller)
5562 assert(custom_stream != (CustomStreamInfo *) NULL);
5563 assert(custom_stream->signature == MagickCoreSignature);
5564 custom_stream->teller=teller;
5592MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5593 CustomStreamHandler writer)
5595 assert(custom_stream != (CustomStreamInfo *) NULL);
5596 assert(custom_stream->signature == MagickCoreSignature);
5597 custom_stream->writer=writer;
5624static int SyncBlob(
const Image *image)
5627 *magick_restrict blob_info;
5632 assert(image != (Image *) NULL);
5633 assert(image->signature == MagickCoreSignature);
5634 assert(image->blob != (BlobInfo *) NULL);
5635 if (IsEventLogging() != MagickFalse)
5636 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5637 if (EOFBlob(image) != 0)
5639 blob_info=image->blob;
5641 switch (blob_info->type)
5643 case UndefinedStream:
5644 case StandardStream:
5649 status=fflush(blob_info->file_info.file);
5654#if defined(MAGICKCORE_ZLIB_DELEGATE)
5655 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5661#if defined(MAGICKCORE_BZLIB_DELEGATE)
5662 status=BZ2_bzflush(blob_info->file_info.bzfile);
5698MagickExport MagickOffsetType TellBlob(
const Image *image)
5701 *magick_restrict blob_info;
5706 assert(image != (Image *) NULL);
5707 assert(image->signature == MagickCoreSignature);
5708 assert(image->blob != (BlobInfo *) NULL);
5709 assert(image->blob->type != UndefinedStream);
5710 if (IsEventLogging() != MagickFalse)
5711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5712 blob_info=image->blob;
5714 switch (blob_info->type)
5716 case UndefinedStream:
5717 case StandardStream:
5721 offset=ftell(blob_info->file_info.file);
5728#if defined(MAGICKCORE_ZLIB_DELEGATE)
5729 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5739 offset=blob_info->offset;
5744 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5745 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5777MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5779#if defined(MAGICKCORE_HAVE_MMAP)
5783 status=munmap(map,length);
5784 return(status == -1 ? MagickFalse : MagickTrue);
5788 return(MagickFalse);
5820MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5824 *magick_restrict blob_info;
5838 assert(image != (Image *) NULL);
5839 assert(image->signature == MagickCoreSignature);
5840 assert(image->blob != (BlobInfo *) NULL);
5841 assert(image->blob->type != UndefinedStream);
5844 assert(data != (
const void *) NULL);
5845 blob_info=image->blob;
5847 p=(
const unsigned char *) data;
5848 q=(
unsigned char *) data;
5849 switch (blob_info->type)
5851 case UndefinedStream:
5853 case StandardStream:
5861 count=(ssize_t) fwrite((
const char *) data,1,length,
5862 blob_info->file_info.file);
5867 c=putc((
int) *p++,blob_info->file_info.file);
5875 c=putc((
int) *p++,blob_info->file_info.file);
5883 c=putc((
int) *p++,blob_info->file_info.file);
5891 c=putc((
int) *p++,blob_info->file_info.file);
5900 if ((count != (ssize_t) length) &&
5901 (ferror(blob_info->file_info.file) != 0))
5902 ThrowBlobException(blob_info);
5907#if defined(MAGICKCORE_ZLIB_DELEGATE)
5918 for (i=0; i < length; i+=(size_t) count)
5920 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5921 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5934 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5942 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5950 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5958 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5968 (void) gzerror(blob_info->file_info.gzfile,&status);
5969 if ((count != (ssize_t) length) && (status != Z_OK))
5970 ThrowBlobException(blob_info);
5976#if defined(MAGICKCORE_BZLIB_DELEGATE)
5983 for (i=0; i < length; i+=(size_t) count)
5985 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5986 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5996 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5997 if ((count != (ssize_t) length) && (status != BZ_OK))
5998 ThrowBlobException(blob_info);
6004 count=(ssize_t) blob_info->stream(image,data,length);
6012 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6017 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6018 if (extent >= blob_info->extent)
6020 extent+=blob_info->quantum+length;
6021 blob_info->quantum<<=1;
6022 if (SetBlobExtent(image,extent) == MagickFalse)
6025 q=blob_info->data+blob_info->offset;
6026 (void) memcpy(q,p,length);
6027 blob_info->offset+=(MagickOffsetType) length;
6028 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6029 blob_info->length=(size_t) blob_info->offset;
6030 count=(ssize_t) length;
6035 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6036 count=blob_info->custom_stream->writer((
unsigned char *) data,
6037 length,blob_info->custom_stream->data);
6069MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6072 *magick_restrict blob_info;
6077 assert(image != (Image *) NULL);
6078 assert(image->signature == MagickCoreSignature);
6079 assert(image->blob != (BlobInfo *) NULL);
6080 assert(image->blob->type != UndefinedStream);
6081 blob_info=image->blob;
6083 switch (blob_info->type)
6085 case StandardStream:
6092 c=putc((
int) value,blob_info->file_info.file);
6095 if (ferror(blob_info->file_info.file) != 0)
6096 ThrowBlobException(blob_info);
6104 count=WriteBlobStream(image,1,&value);
6136MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6147 quantum.unsigned_value=0U;
6148 quantum.float_value=value;
6149 return(WriteBlobLong(image,quantum.unsigned_value));
6177MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6182 assert(image != (Image *) NULL);
6183 assert(image->signature == MagickCoreSignature);
6184 if (image->endian == LSBEndian)
6186 buffer[0]=(
unsigned char) value;
6187 buffer[1]=(
unsigned char) (value >> 8);
6188 buffer[2]=(
unsigned char) (value >> 16);
6189 buffer[3]=(
unsigned char) (value >> 24);
6190 return(WriteBlobStream(image,4,buffer));
6192 buffer[0]=(
unsigned char) (value >> 24);
6193 buffer[1]=(
unsigned char) (value >> 16);
6194 buffer[2]=(
unsigned char) (value >> 8);
6195 buffer[3]=(
unsigned char) value;
6196 return(WriteBlobStream(image,4,buffer));
6224MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6229 assert(image != (Image *) NULL);
6230 assert(image->signature == MagickCoreSignature);
6231 if (image->endian == LSBEndian)
6233 buffer[0]=(
unsigned char) value;
6234 buffer[1]=(
unsigned char) (value >> 8);
6235 buffer[2]=(
unsigned char) (value >> 16);
6236 buffer[3]=(
unsigned char) (value >> 24);
6237 buffer[4]=(
unsigned char) (value >> 32);
6238 buffer[5]=(
unsigned char) (value >> 40);
6239 buffer[6]=(
unsigned char) (value >> 48);
6240 buffer[7]=(
unsigned char) (value >> 56);
6241 return(WriteBlobStream(image,8,buffer));
6243 buffer[0]=(
unsigned char) (value >> 56);
6244 buffer[1]=(
unsigned char) (value >> 48);
6245 buffer[2]=(
unsigned char) (value >> 40);
6246 buffer[3]=(
unsigned char) (value >> 32);
6247 buffer[4]=(
unsigned char) (value >> 24);
6248 buffer[5]=(
unsigned char) (value >> 16);
6249 buffer[6]=(
unsigned char) (value >> 8);
6250 buffer[7]=(
unsigned char) value;
6251 return(WriteBlobStream(image,8,buffer));
6279MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6284 assert(image != (Image *) NULL);
6285 assert(image->signature == MagickCoreSignature);
6286 if (image->endian == LSBEndian)
6288 buffer[0]=(
unsigned char) value;
6289 buffer[1]=(
unsigned char) (value >> 8);
6290 return(WriteBlobStream(image,2,buffer));
6292 buffer[0]=(
unsigned char) (value >> 8);
6293 buffer[1]=(
unsigned char) value;
6294 return(WriteBlobStream(image,2,buffer));
6322MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6336 assert(image != (Image *) NULL);
6337 assert(image->signature == MagickCoreSignature);
6338 quantum.signed_value=value;
6339 if (image->endian == LSBEndian)
6341 buffer[0]=(
unsigned char) quantum.unsigned_value;
6342 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6343 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6344 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6345 return(WriteBlobStream(image,4,buffer));
6347 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6348 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6349 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6350 buffer[3]=(
unsigned char) quantum.unsigned_value;
6351 return(WriteBlobStream(image,4,buffer));
6379MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6384 assert(image != (Image *) NULL);
6385 assert(image->signature == MagickCoreSignature);
6386 buffer[0]=(
unsigned char) value;
6387 buffer[1]=(
unsigned char) (value >> 8);
6388 buffer[2]=(
unsigned char) (value >> 16);
6389 buffer[3]=(
unsigned char) (value >> 24);
6390 return(WriteBlobStream(image,4,buffer));
6418MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6423 assert(image != (Image *) NULL);
6424 assert(image->signature == MagickCoreSignature);
6425 buffer[0]=(
unsigned char) value;
6426 buffer[1]=(
unsigned char) (value >> 8);
6427 return(WriteBlobStream(image,2,buffer));
6455MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6469 assert(image != (Image *) NULL);
6470 assert(image->signature == MagickCoreSignature);
6471 quantum.signed_value=value;
6472 buffer[0]=(
unsigned char) quantum.unsigned_value;
6473 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6474 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6475 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6476 return(WriteBlobStream(image,4,buffer));
6504MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6505 const signed short value)
6519 assert(image != (Image *) NULL);
6520 assert(image->signature == MagickCoreSignature);
6521 quantum.signed_value=value;
6522 buffer[0]=(
unsigned char) quantum.unsigned_value;
6523 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6524 return(WriteBlobStream(image,2,buffer));
6552MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6557 assert(image != (Image *) NULL);
6558 assert(image->signature == MagickCoreSignature);
6559 buffer[0]=(
unsigned char) (value >> 24);
6560 buffer[1]=(
unsigned char) (value >> 16);
6561 buffer[2]=(
unsigned char) (value >> 8);
6562 buffer[3]=(
unsigned char) value;
6563 return(WriteBlobStream(image,4,buffer));
6591MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6592 const signed short value)
6606 assert(image != (Image *) NULL);
6607 assert(image->signature == MagickCoreSignature);
6608 quantum.signed_value=value;
6609 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6610 buffer[1]=(
unsigned char) quantum.unsigned_value;
6611 return(WriteBlobStream(image,2,buffer));
6639MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6644 assert(image != (Image *) NULL);
6645 assert(image->signature == MagickCoreSignature);
6646 buffer[0]=(
unsigned char) (value >> 8);
6647 buffer[1]=(
unsigned char) value;
6648 return(WriteBlobStream(image,2,buffer));
6676MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6678 assert(image != (Image *) NULL);
6679 assert(image->signature == MagickCoreSignature);
6680 assert(
string != (
const char *) NULL);
6681 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));