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 MagickMaxBlobExtent (8*8192)
86#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
87# define MAP_ANONYMOUS MAP_ANON
89#if !defined(MAP_FAILED)
90#define MAP_FAILED ((void *) -1)
94#define _O_BINARY O_BINARY
96#if defined(MAGICKCORE_WINDOWS_SUPPORT)
101# define MAGICKCORE_HAVE_MMAP 1
102# define mmap(address,length,protection,access,file,offset) \
103 NTMapMemory(address,length,protection,access,file,offset)
106# define munmap(address,length) NTUnmapMemory(address,length)
109# define pclose _pclose
124#if defined(MAGICKCORE_ZLIB_DELEGATE)
129#if defined(MAGICKCORE_BZLIB_DELEGATE)
221 SyncBlob(
const Image *);
245MagickExport CustomStreamInfo *AcquireCustomStreamInfo(
246 ExceptionInfo *magick_unused(exception))
251 magick_unreferenced(exception);
252 custom_stream=(CustomStreamInfo *) AcquireCriticalMemory(
253 sizeof(*custom_stream));
254 (void) memset(custom_stream,0,
sizeof(*custom_stream));
255 custom_stream->signature=MagickCoreSignature;
256 return(custom_stream);
286MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
289 assert(blob_info != (BlobInfo *) NULL);
290 if (IsEventLogging() != MagickFalse)
291 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
292 blob_info->length=length;
293 blob_info->extent=length;
294 blob_info->quantum=(size_t) MagickMaxBlobExtent;
296 blob_info->type=BlobStream;
297 blob_info->file_info.file=(FILE *) NULL;
298 blob_info->data=(
unsigned char *) blob;
299 blob_info->mapped=MagickFalse;
327MagickExport
void AttachCustomStream(BlobInfo *blob_info,
328 CustomStreamInfo *custom_stream)
330 assert(blob_info != (BlobInfo *) NULL);
331 assert(custom_stream != (CustomStreamInfo *) NULL);
332 assert(custom_stream->signature == MagickCoreSignature);
333 if (IsEventLogging() != MagickFalse)
334 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
335 blob_info->type=CustomStream;
336 blob_info->custom_stream=custom_stream;
369MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
370 const size_t length,ExceptionInfo *exception)
381 assert(filename != (
const char *) NULL);
382 assert(blob != (
const void *) NULL);
383 if (IsEventLogging() != MagickFalse)
384 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
385 if (*filename ==
'\0')
386 file=AcquireUniqueFileResource(filename);
388 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
391 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
394 for (i=0; i < length; i+=(size_t) count)
396 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
397 MagickMaxBufferExtent));
405 file=close_utf8(file);
406 if ((file == -1) || (i < length))
408 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
445MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
446 const size_t length,ExceptionInfo *exception)
461 assert(image_info != (ImageInfo *) NULL);
462 assert(image_info->signature == MagickCoreSignature);
463 assert(exception != (ExceptionInfo *) NULL);
464 if (IsEventLogging() != MagickFalse)
465 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
466 image_info->filename);
467 if ((blob == (
const void *) NULL) || (length == 0))
469 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
470 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
471 return((Image *) NULL);
473 blob_info=CloneImageInfo(image_info);
474 blob_info->blob=(
void *) blob;
475 blob_info->length=length;
476 if (*blob_info->magick ==
'\0')
477 (void) SetImageInfo(blob_info,0,exception);
478 magick_info=GetMagickInfo(blob_info->magick,exception);
479 if (magick_info == (
const MagickInfo *) NULL)
481 (void) ThrowMagickException(exception,GetMagickModule(),
482 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
484 blob_info=DestroyImageInfo(blob_info);
485 return((Image *) NULL);
487 if (GetMagickBlobSupport(magick_info) != MagickFalse)
490 filename[MagickPathExtent];
495 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
496 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
497 blob_info->magick,filename);
498 image=ReadImage(blob_info,exception);
499 if (image != (Image *) NULL)
500 (void) DetachBlob(image->blob);
501 blob_info=DestroyImageInfo(blob_info);
507 blob_info->blob=(
void *) NULL;
509 *blob_info->filename=
'\0';
510 status=BlobToFile(blob_info->filename,blob,length,exception);
511 if (status == MagickFalse)
513 (void) RelinquishUniqueFileResource(blob_info->filename);
514 blob_info=DestroyImageInfo(blob_info);
515 return((Image *) NULL);
517 clone_info=CloneImageInfo(blob_info);
518 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
519 blob_info->magick,blob_info->filename);
520 image=ReadImage(clone_info,exception);
521 if (image != (Image *) NULL)
529 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
531 (void) CopyMagickString(images->filename,image_info->filename,
533 (void) CopyMagickString(images->magick_filename,image_info->filename,
535 (void) CopyMagickString(images->magick,magick_info->name,
537 images=GetNextImageInList(images);
540 clone_info=DestroyImageInfo(clone_info);
541 (void) RelinquishUniqueFileResource(blob_info->filename);
542 blob_info=DestroyImageInfo(blob_info);
569MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
577 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
578 GetBlobInfo(clone_info);
579 if (blob_info == (BlobInfo *) NULL)
581 semaphore=clone_info->semaphore;
582 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
583 if (blob_info->mapped != MagickFalse)
584 (void) AcquireMagickResource(MapResource,blob_info->length);
585 clone_info->semaphore=semaphore;
586 LockSemaphoreInfo(clone_info->semaphore);
587 clone_info->reference_count=1;
588 UnlockSemaphoreInfo(clone_info->semaphore);
615static inline void ThrowBlobException(BlobInfo *blob_info)
617 if ((blob_info->status == 0) && (errno != 0))
618 blob_info->error_number=errno;
619 blob_info->status=(-1);
622MagickExport MagickBooleanType CloseBlob(Image *image)
625 *magick_restrict blob_info;
633 assert(image != (Image *) NULL);
634 assert(image->signature == MagickCoreSignature);
635 if (IsEventLogging() != MagickFalse)
636 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
637 blob_info=image->blob;
638 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
640 (void) SyncBlob(image);
641 status=blob_info->status;
642 switch (blob_info->type)
644 case UndefinedStream:
650 if (blob_info->synchronize != MagickFalse)
652 status=fflush(blob_info->file_info.file);
654 ThrowBlobException(blob_info);
655 status=fsync(fileno(blob_info->file_info.file));
657 ThrowBlobException(blob_info);
659 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
660 ThrowBlobException(blob_info);
665#if defined(MAGICKCORE_ZLIB_DELEGATE)
667 (void) gzerror(blob_info->file_info.gzfile,&status);
669 ThrowBlobException(blob_info);
675#if defined(MAGICKCORE_BZLIB_DELEGATE)
677 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
679 ThrowBlobException(blob_info);
687 if (blob_info->file_info.file != (FILE *) NULL)
689 if (blob_info->synchronize != MagickFalse)
691 status=fflush(blob_info->file_info.file);
693 ThrowBlobException(blob_info);
694 status=fsync(fileno(blob_info->file_info.file));
696 ThrowBlobException(blob_info);
698 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
699 ThrowBlobException(blob_info);
706 blob_info->size=GetBlobSize(image);
707 image->extent=blob_info->size;
708 blob_info->eof=MagickFalse;
710 blob_info->mode=UndefinedBlobMode;
711 if (blob_info->exempt != MagickFalse)
713 blob_info->type=UndefinedStream;
714 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
716 switch (blob_info->type)
718 case UndefinedStream:
723 if (blob_info->file_info.file != (FILE *) NULL)
725 status=fclose(blob_info->file_info.file);
727 ThrowBlobException(blob_info);
733#if defined(MAGICKCORE_HAVE_PCLOSE)
734 status=pclose(blob_info->file_info.file);
736 ThrowBlobException(blob_info);
742#if defined(MAGICKCORE_ZLIB_DELEGATE)
743 status=gzclose(blob_info->file_info.gzfile);
745 ThrowBlobException(blob_info);
751#if defined(MAGICKCORE_BZLIB_DELEGATE)
752 BZ2_bzclose(blob_info->file_info.bzfile);
760 if (blob_info->file_info.file != (FILE *) NULL)
762 status=fclose(blob_info->file_info.file);
764 ThrowBlobException(blob_info);
771 (void) DetachBlob(blob_info);
772 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
801MagickExport Image *CustomStreamToImage(
const ImageInfo *image_info,
802 ExceptionInfo *exception)
813 assert(image_info != (ImageInfo *) NULL);
814 assert(image_info->signature == MagickCoreSignature);
815 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
816 assert(image_info->custom_stream->signature == MagickCoreSignature);
817 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
818 assert(exception != (ExceptionInfo *) NULL);
819 if (IsEventLogging() != MagickFalse)
820 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
821 image_info->filename);
822 blob_info=CloneImageInfo(image_info);
823 if (*blob_info->magick ==
'\0')
824 (void) SetImageInfo(blob_info,0,exception);
825 magick_info=GetMagickInfo(blob_info->magick,exception);
826 if (magick_info == (
const MagickInfo *) NULL)
828 (void) ThrowMagickException(exception,GetMagickModule(),
829 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
831 blob_info=DestroyImageInfo(blob_info);
832 return((Image *) NULL);
834 image=(Image *) NULL;
835 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
836 (*blob_info->filename !=
'\0'))
839 filename[MagickPathExtent];
845 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
846 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
847 blob_info->magick,filename);
848 image=ReadImage(blob_info,exception);
853 unique[MagickPathExtent];
867 blob_info->custom_stream=(CustomStreamInfo *) NULL;
868 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
870 if (blob == (
unsigned char *) NULL)
872 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
873 image_info->filename);
874 blob_info=DestroyImageInfo(blob_info);
875 return((Image *) NULL);
877 file=AcquireUniqueFileResource(unique);
880 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
881 image_info->filename);
882 blob=(
unsigned char *) RelinquishMagickMemory(blob);
883 blob_info=DestroyImageInfo(blob_info);
884 return((Image *) NULL);
886 clone_info=CloneImageInfo(blob_info);
887 blob_info->file=fdopen(file,
"wb+");
888 if (blob_info->file != (FILE *) NULL)
893 count=(ssize_t) MagickMaxBufferExtent;
894 while (count == (ssize_t) MagickMaxBufferExtent)
896 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
897 image_info->custom_stream->data);
898 count=(ssize_t) write(file,(
const char *) blob,(
size_t) count);
900 (void) fclose(blob_info->file);
901 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
902 "%s:%s",blob_info->magick,unique);
903 image=ReadImage(clone_info,exception);
904 if (image != (Image *) NULL)
912 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
914 (void) CopyMagickString(images->filename,image_info->filename,
916 (void) CopyMagickString(images->magick_filename,
917 image_info->filename,MagickPathExtent);
918 (void) CopyMagickString(images->magick,magick_info->name,
920 images=GetNextImageInList(images);
924 clone_info=DestroyImageInfo(clone_info);
925 blob=(
unsigned char *) RelinquishMagickMemory(blob);
926 (void) RelinquishUniqueFileResource(unique);
928 blob_info=DestroyImageInfo(blob_info);
929 if (image != (Image *) NULL)
930 if (CloseBlob(image) == MagickFalse)
931 image=DestroyImageList(image);
957MagickExport
void DestroyBlob(Image *image)
960 *magick_restrict blob_info;
965 assert(image != (Image *) NULL);
966 assert(image->signature == MagickCoreSignature);
967 assert(image->blob != (BlobInfo *) NULL);
968 assert(image->blob->signature == MagickCoreSignature);
969 if (IsEventLogging() != MagickFalse)
970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
971 blob_info=image->blob;
973 LockSemaphoreInfo(blob_info->semaphore);
974 blob_info->reference_count--;
975 assert(blob_info->reference_count >= 0);
976 if (blob_info->reference_count == 0)
978 UnlockSemaphoreInfo(blob_info->semaphore);
979 if (destroy == MagickFalse)
981 image->blob=(BlobInfo *) NULL;
984 (void) CloseBlob(image);
985 if (blob_info->mapped != MagickFalse)
987 (void) UnmapBlob(blob_info->data,blob_info->length);
988 RelinquishMagickResource(MapResource,blob_info->length);
991 RelinquishSemaphoreInfo(&blob_info->semaphore);
992 blob_info->signature=(~MagickCoreSignature);
993 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
1019MagickExport CustomStreamInfo *DestroyCustomStreamInfo(
1020 CustomStreamInfo *custom_stream)
1022 assert(custom_stream != (CustomStreamInfo *) NULL);
1023 assert(custom_stream->signature == MagickCoreSignature);
1024 if (IsEventLogging() != MagickFalse)
1025 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1026 custom_stream->signature=(~MagickCoreSignature);
1027 custom_stream=(CustomStreamInfo *) RelinquishMagickMemory(custom_stream);
1028 return(custom_stream);
1053MagickExport
void *DetachBlob(BlobInfo *blob_info)
1058 assert(blob_info != (BlobInfo *) NULL);
1059 if (IsEventLogging() != MagickFalse)
1060 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1061 if (blob_info->mapped != MagickFalse)
1063 (void) UnmapBlob(blob_info->data,blob_info->length);
1064 blob_info->data=NULL;
1065 RelinquishMagickResource(MapResource,blob_info->length);
1067 blob_info->mapped=MagickFalse;
1068 blob_info->length=0;
1073 blob_info->offset=0;
1074 blob_info->mode=UndefinedBlobMode;
1075 blob_info->eof=MagickFalse;
1077 blob_info->exempt=MagickFalse;
1078 blob_info->type=UndefinedStream;
1079 blob_info->file_info.file=(FILE *) NULL;
1080 data=blob_info->data;
1081 blob_info->data=(
unsigned char *) NULL;
1082 blob_info->stream=(StreamHandler) NULL;
1083 blob_info->custom_stream=(CustomStreamInfo *) NULL;
1111MagickExport
void DisassociateBlob(Image *image)
1114 *magick_restrict blob_info,
1120 assert(image != (Image *) NULL);
1121 assert(image->signature == MagickCoreSignature);
1122 assert(image->blob != (BlobInfo *) NULL);
1123 assert(image->blob->signature == MagickCoreSignature);
1124 if (IsEventLogging() != MagickFalse)
1125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1126 blob_info=image->blob;
1128 LockSemaphoreInfo(blob_info->semaphore);
1129 assert(blob_info->reference_count >= 0);
1130 if (blob_info->reference_count > 1)
1132 UnlockSemaphoreInfo(blob_info->semaphore);
1133 if (clone == MagickFalse)
1135 clone_info=CloneBlobInfo(blob_info);
1137 image->blob=clone_info;
1165MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1166 const MagickSizeType length)
1178 buffer[MagickMinBufferExtent >> 1];
1180 assert(image != (Image *) NULL);
1181 assert(image->signature == MagickCoreSignature);
1182 if (length != (MagickSizeType) ((MagickOffsetType) length))
1183 return(MagickFalse);
1185 for (i=0; i < length; i+=(MagickSizeType) count)
1187 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1188 (void) ReadBlobStream(image,quantum,buffer,&count);
1196 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1223MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
1225 assert(image != (Image *) NULL);
1226 assert(image->signature == MagickCoreSignature);
1227 assert(duplicate != (Image *) NULL);
1228 assert(duplicate->signature == MagickCoreSignature);
1229 if (IsEventLogging() != MagickFalse)
1230 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1232 image->blob=ReferenceBlob(duplicate->blob);
1258MagickExport
int EOFBlob(
const Image *image)
1261 *magick_restrict blob_info;
1263 assert(image != (Image *) NULL);
1264 assert(image->signature == MagickCoreSignature);
1265 assert(image->blob != (BlobInfo *) NULL);
1266 assert(image->blob->type != UndefinedStream);
1267 if (IsEventLogging() != MagickFalse)
1268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1269 blob_info=image->blob;
1270 switch (blob_info->type)
1272 case UndefinedStream:
1273 case StandardStream:
1278 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1284#if defined(MAGICKCORE_ZLIB_DELEGATE)
1285 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1292#if defined(MAGICKCORE_BZLIB_DELEGATE)
1297 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1298 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1304 blob_info->eof=MagickFalse;
1312 return((
int) blob_info->eof);
1338MagickExport
int ErrorBlob(
const Image *image)
1341 *magick_restrict blob_info;
1343 assert(image != (Image *) NULL);
1344 assert(image->signature == MagickCoreSignature);
1345 assert(image->blob != (BlobInfo *) NULL);
1346 assert(image->blob->type != UndefinedStream);
1347 if (IsEventLogging() != MagickFalse)
1348 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1349 blob_info=image->blob;
1350 switch (blob_info->type)
1352 case UndefinedStream:
1353 case StandardStream:
1358 blob_info->error=ferror(blob_info->file_info.file);
1363#if defined(MAGICKCORE_ZLIB_DELEGATE)
1364 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1370#if defined(MAGICKCORE_BZLIB_DELEGATE)
1371 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1385 return(blob_info->error);
1423MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1424 size_t *length,ExceptionInfo *exception)
1450 assert(filename != (
const char *) NULL);
1451 assert(exception != (ExceptionInfo *) NULL);
1452 assert(exception->signature == MagickCoreSignature);
1453 if (IsEventLogging() != MagickFalse)
1454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1456 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1457 if (status == MagickFalse)
1460 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1461 "NotAuthorized",
"`%s'",filename);
1465 if (LocaleCompare(filename,
"-") != 0)
1468 flags = O_RDONLY | O_BINARY;
1470 status=GetPathAttributes(filename,&attributes);
1471 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1473 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1476#if defined(O_NOFOLLOW)
1477 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1478 if (status == MagickFalse)
1481 file=open_utf8(filename,flags,0);
1485 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1488 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1489 if (status == MagickFalse)
1491 file=close_utf8(file)-1;
1493 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1494 "NotAuthorized",
"`%s'",filename);
1497 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1499 if ((file == fileno(stdin)) || (offset < 0) ||
1500 (offset != (MagickOffsetType) ((ssize_t) offset)))
1511 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1512 quantum=(size_t) MagickMaxBufferExtent;
1513 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1514 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1515 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1516 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1518 count=read(file,blob+i,quantum);
1525 if (~i < ((
size_t) count+quantum+1))
1527 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1530 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1531 quantum+1,
sizeof(*blob));
1532 if ((i+(
size_t) count) >= extent)
1535 if (LocaleCompare(filename,
"-") != 0)
1536 file=close_utf8(file);
1537 if (blob == (
unsigned char *) NULL)
1539 (void) ThrowMagickException(exception,GetMagickModule(),
1540 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1545 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1546 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1549 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1553 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1554 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1555 blob=(
unsigned char *) NULL;
1556 if (~(*length) >= (MagickPathExtent-1))
1557 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1559 if (blob == (
unsigned char *) NULL)
1561 file=close_utf8(file);
1562 (void) ThrowMagickException(exception,GetMagickModule(),
1563 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1566 map=MapBlob(file,ReadMode,0,*length);
1567 if (map != (
unsigned char *) NULL)
1569 (void) memcpy(blob,map,*length);
1570 (void) UnmapBlob(map,*length);
1574 (void) lseek(file,0,SEEK_SET);
1575 for (i=0; i < *length; i+=(size_t) count)
1577 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1578 MagickMaxBufferExtent));
1588 file=close_utf8(file)-1;
1589 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1590 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1595 if (LocaleCompare(filename,
"-") != 0)
1596 file=close_utf8(file);
1599 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1600 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1630static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1631 const void *magick_restrict data)
1634 *magick_restrict blob_info;
1642 assert(image->blob != (BlobInfo *) NULL);
1643 assert(image->blob->type != UndefinedStream);
1644 assert(data != NULL);
1645 blob_info=image->blob;
1646 if (blob_info->type != BlobStream)
1647 return(WriteBlob(image,length,(
const unsigned char *) data));
1648 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1653 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1654 if (extent >= blob_info->extent)
1656 extent+=blob_info->quantum+length;
1657 blob_info->quantum<<=1;
1658 if (SetBlobExtent(image,extent) == MagickFalse)
1661 q=blob_info->data+blob_info->offset;
1662 (void) memcpy(q,data,length);
1663 blob_info->offset+=(MagickOffsetType) length;
1664 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1665 blob_info->length=(size_t) blob_info->offset;
1666 return((ssize_t) length);
1669MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1670 ExceptionInfo *exception)
1691 assert(image != (
const Image *) NULL);
1692 assert(image->signature == MagickCoreSignature);
1693 assert(filename != (
const char *) NULL);
1694 if (IsEventLogging() != MagickFalse)
1695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1696 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1697 if (status == MagickFalse)
1700 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1701 "NotAuthorized",
"`%s'",filename);
1702 return(MagickFalse);
1705 if (LocaleCompare(filename,
"-") != 0)
1708 flags = O_RDONLY | O_BINARY;
1710#if defined(O_NOFOLLOW)
1711 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1712 if (status == MagickFalse)
1715 file=open_utf8(filename,flags,0);
1719 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1720 return(MagickFalse);
1722 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1723 if (status == MagickFalse)
1725 file=close_utf8(file);
1727 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1728 "NotAuthorized",
"`%s'",filename);
1729 return(MagickFalse);
1731 quantum=(size_t) MagickMaxBufferExtent;
1732 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1733 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1734 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1735 if (blob == (
unsigned char *) NULL)
1737 file=close_utf8(file);
1738 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1740 return(MagickFalse);
1744 count=read(file,blob,quantum);
1751 length=(size_t) count;
1752 count=WriteBlobStream(image,length,blob);
1753 if (count != (ssize_t) length)
1755 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1759 file=close_utf8(file);
1761 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1762 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1789MagickExport MagickBooleanType GetBlobError(
const Image *image)
1791 assert(image != (
const Image *) NULL);
1792 assert(image->signature == MagickCoreSignature);
1793 if (IsEventLogging() != MagickFalse)
1794 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1795 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1796 errno=image->blob->error_number;
1797 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1822MagickExport FILE *GetBlobFileHandle(
const Image *image)
1824 assert(image != (
const Image *) NULL);
1825 assert(image->signature == MagickCoreSignature);
1826 return(image->blob->file_info.file);
1851MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1853 assert(blob_info != (BlobInfo *) NULL);
1854 (void) memset(blob_info,0,
sizeof(*blob_info));
1855 blob_info->type=UndefinedStream;
1856 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1857 blob_info->properties.st_mtime=GetMagickTime();
1858 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1859 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1860 blob_info->reference_count=1;
1861 blob_info->semaphore=AcquireSemaphoreInfo();
1862 blob_info->signature=MagickCoreSignature;
1887MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1889 assert(image != (Image *) NULL);
1890 assert(image->signature == MagickCoreSignature);
1891 if (IsEventLogging() != MagickFalse)
1892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1893 return(&image->blob->properties);
1919MagickExport MagickSizeType GetBlobSize(
const Image *image)
1922 *magick_restrict blob_info;
1927 assert(image != (Image *) NULL);
1928 assert(image->signature == MagickCoreSignature);
1929 assert(image->blob != (BlobInfo *) NULL);
1930 if (IsEventLogging() != MagickFalse)
1931 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1932 blob_info=image->blob;
1934 switch (blob_info->type)
1936 case UndefinedStream:
1937 case StandardStream:
1939 extent=blob_info->size;
1947 extent=(MagickSizeType) blob_info->properties.st_size;
1949 extent=blob_info->size;
1950 file_descriptor=fileno(blob_info->file_info.file);
1951 if (file_descriptor == -1)
1953 if (fstat(file_descriptor,&blob_info->properties) == 0)
1954 extent=(MagickSizeType) blob_info->properties.st_size;
1959 extent=blob_info->size;
1968 status=GetPathAttributes(image->filename,&blob_info->properties);
1969 if (status != MagickFalse)
1970 extent=(MagickSizeType) blob_info->properties.st_size;
1977 extent=(MagickSizeType) blob_info->length;
1982 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1983 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1988 offset=blob_info->custom_stream->teller(
1989 blob_info->custom_stream->data);
1990 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1991 blob_info->custom_stream->data);
1992 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1993 blob_info->custom_stream->data);
2023MagickExport
void *GetBlobStreamData(
const Image *image)
2025 assert(image != (
const Image *) NULL);
2026 assert(image->signature == MagickCoreSignature);
2027 return(image->blob->data);
2052MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2054 assert(image != (
const Image *) NULL);
2055 assert(image->signature == MagickCoreSignature);
2056 if (IsEventLogging() != MagickFalse)
2057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2058 return(image->blob->stream);
2094MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2095 Image *image,
size_t *length,ExceptionInfo *exception)
2109 assert(image_info != (
const ImageInfo *) NULL);
2110 assert(image_info->signature == MagickCoreSignature);
2111 assert(image != (Image *) NULL);
2112 assert(image->signature == MagickCoreSignature);
2113 assert(exception != (ExceptionInfo *) NULL);
2114 assert(exception->signature == MagickCoreSignature);
2115 if (IsEventLogging() != MagickFalse)
2116 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2117 image_info->filename);
2119 blob=(
unsigned char *) NULL;
2120 blob_info=CloneImageInfo(image_info);
2121 blob_info->adjoin=MagickFalse;
2122 (void) SetImageInfo(blob_info,1,exception);
2123 if (*blob_info->magick !=
'\0')
2124 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2125 magick_info=GetMagickInfo(image->magick,exception);
2126 if (magick_info == (
const MagickInfo *) NULL)
2128 (void) ThrowMagickException(exception,GetMagickModule(),
2129 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2131 blob_info=DestroyImageInfo(blob_info);
2134 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2135 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2140 blob_info->length=0;
2141 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2142 sizeof(
unsigned char));
2143 if (blob_info->blob == NULL)
2144 (void) ThrowMagickException(exception,GetMagickModule(),
2145 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2148 (void) CloseBlob(image);
2149 image->blob->exempt=MagickTrue;
2150 image->blob->extent=0;
2151 *image->filename=
'\0';
2152 status=WriteImage(blob_info,image,exception);
2153 *length=image->blob->length;
2154 blob=DetachBlob(image->blob);
2155 if (blob != (
void *) NULL)
2157 if (status == MagickFalse)
2158 blob=RelinquishMagickMemory(blob);
2160 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2162 else if ((status == MagickFalse) && (image->blob->extent == 0))
2163 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2169 unique[MagickPathExtent];
2177 file=AcquireUniqueFileResource(unique);
2180 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2181 image_info->filename);
2185 blob_info->file=fdopen(file,
"wb");
2186 if (blob_info->file != (FILE *) NULL)
2188 (void) FormatLocaleString(image->filename,MagickPathExtent,
2189 "%s:%s",image->magick,unique);
2190 status=WriteImage(blob_info,image,exception);
2191 (void) fclose(blob_info->file);
2192 if (status != MagickFalse)
2193 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2195 (void) RelinquishUniqueFileResource(unique);
2198 blob_info=DestroyImageInfo(blob_info);
2230MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2231 ExceptionInfo *exception)
2243 assert(image_info != (
const ImageInfo *) NULL);
2244 assert(image_info->signature == MagickCoreSignature);
2245 assert(image != (Image *) NULL);
2246 assert(image->signature == MagickCoreSignature);
2247 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2248 assert(image_info->custom_stream->signature == MagickCoreSignature);
2249 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2250 assert(exception != (ExceptionInfo *) NULL);
2251 if (IsEventLogging() != MagickFalse)
2252 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2253 image_info->filename);
2254 clone_info=CloneImageInfo(image_info);
2255 clone_info->adjoin=MagickFalse;
2256 (void) SetImageInfo(clone_info,1,exception);
2257 if (*clone_info->magick !=
'\0')
2258 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2259 magick_info=GetMagickInfo(image->magick,exception);
2260 if (magick_info == (
const MagickInfo *) NULL)
2262 (void) ThrowMagickException(exception,GetMagickModule(),
2263 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2265 clone_info=DestroyImageInfo(clone_info);
2268 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2269 blob_support=GetMagickBlobSupport(magick_info);
2270 if ((blob_support != MagickFalse) &&
2271 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2273 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2274 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2275 blob_support=MagickFalse;
2277 if (blob_support != MagickFalse)
2282 (void) CloseBlob(image);
2283 *image->filename=
'\0';
2284 (void) WriteImage(clone_info,image,exception);
2289 unique[MagickPathExtent];
2300 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2301 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2303 if (blob == (
unsigned char *) NULL)
2305 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2306 image_info->filename);
2307 clone_info=DestroyImageInfo(clone_info);
2310 file=AcquireUniqueFileResource(unique);
2313 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2314 image_info->filename);
2315 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2316 clone_info=DestroyImageInfo(clone_info);
2319 clone_info->file=fdopen(file,
"wb+");
2320 if (clone_info->file != (FILE *) NULL)
2325 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2326 image->magick,unique);
2327 status=WriteImage(clone_info,image,exception);
2328 if (status != MagickFalse)
2330 (void) fseek(clone_info->file,0,SEEK_SET);
2331 count=(ssize_t) MagickMaxBufferExtent;
2332 while (count == (ssize_t) MagickMaxBufferExtent)
2334 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2336 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2337 image_info->custom_stream->data);
2340 (void) fclose(clone_info->file);
2342 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2343 (void) RelinquishUniqueFileResource(unique);
2345 clone_info=DestroyImageInfo(clone_info);
2376MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2377 ExceptionInfo *exception)
2401 assert(image != (Image *) NULL);
2402 assert(image->signature == MagickCoreSignature);
2403 assert(image->blob != (BlobInfo *) NULL);
2404 assert(image->blob->type != UndefinedStream);
2405 assert(filename != (
const char *) NULL);
2406 if (IsEventLogging() != MagickFalse)
2407 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2408 if (*filename ==
'\0')
2409 file=AcquireUniqueFileResource(filename);
2411 if (LocaleCompare(filename,
"-") == 0)
2412 file=fileno(stdout);
2414 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2417 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2418 return(MagickFalse);
2420 quantum=(size_t) MagickMaxBufferExtent;
2421 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2422 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2423 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2424 if (buffer == (
unsigned char *) NULL)
2426 file=close_utf8(file)-1;
2427 (void) ThrowMagickException(exception,GetMagickModule(),
2428 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2429 return(MagickFalse);
2432 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2433 for (i=0; count > 0; )
2435 length=(size_t) count;
2436 for (i=0; i < length; i+=(size_t) count)
2438 count=write(file,p+i,(
size_t) (length-i));
2448 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2450 if (LocaleCompare(filename,
"-") != 0)
2451 file=close_utf8(file);
2452 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2453 if ((file == -1) || (i < length))
2456 file=close_utf8(file);
2457 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2458 return(MagickFalse);
2498MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2499 size_t *length,ExceptionInfo *exception)
2513 assert(image_info != (
const ImageInfo *) NULL);
2514 assert(image_info->signature == MagickCoreSignature);
2515 assert(images != (Image *) NULL);
2516 assert(images->signature == MagickCoreSignature);
2517 assert(exception != (ExceptionInfo *) NULL);
2518 if (IsEventLogging() != MagickFalse)
2519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2520 image_info->filename);
2522 blob=(
unsigned char *) NULL;
2523 blob_info=CloneImageInfo(image_info);
2524 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2526 if (*blob_info->magick !=
'\0')
2527 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2528 magick_info=GetMagickInfo(images->magick,exception);
2529 if (magick_info == (
const MagickInfo *) NULL)
2531 (void) ThrowMagickException(exception,GetMagickModule(),
2532 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2534 blob_info=DestroyImageInfo(blob_info);
2537 if (GetMagickAdjoin(magick_info) == MagickFalse)
2539 blob_info=DestroyImageInfo(blob_info);
2540 return(ImageToBlob(image_info,images,length,exception));
2542 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2543 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2548 blob_info->length=0;
2549 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2550 sizeof(
unsigned char));
2551 if (blob_info->blob == (
void *) NULL)
2552 (void) ThrowMagickException(exception,GetMagickModule(),
2553 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2556 (void) CloseBlob(images);
2557 images->blob->exempt=MagickTrue;
2558 images->blob->extent=0;
2559 *images->filename=
'\0';
2560 status=WriteImages(blob_info,images,images->filename,exception);
2561 *length=images->blob->length;
2562 blob=DetachBlob(images->blob);
2563 if (blob != (
void *) NULL)
2565 if (status == MagickFalse)
2566 blob=RelinquishMagickMemory(blob);
2568 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2570 else if ((status == MagickFalse) && (images->blob->extent == 0))
2571 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2577 filename[MagickPathExtent],
2578 unique[MagickPathExtent];
2586 file=AcquireUniqueFileResource(unique);
2589 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2590 image_info->filename);
2594 blob_info->file=fdopen(file,
"wb");
2595 if (blob_info->file != (FILE *) NULL)
2597 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2598 images->magick,unique);
2599 status=WriteImages(blob_info,images,filename,exception);
2600 (void) fclose(blob_info->file);
2601 if (status != MagickFalse)
2602 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2604 (void) RelinquishUniqueFileResource(unique);
2607 blob_info=DestroyImageInfo(blob_info);
2639MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2640 Image *images,ExceptionInfo *exception)
2652 assert(image_info != (
const ImageInfo *) NULL);
2653 assert(image_info->signature == MagickCoreSignature);
2654 assert(images != (Image *) NULL);
2655 assert(images->signature == MagickCoreSignature);
2656 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2657 assert(image_info->custom_stream->signature == MagickCoreSignature);
2658 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2659 assert(exception != (ExceptionInfo *) NULL);
2660 if (IsEventLogging() != MagickFalse)
2661 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2662 image_info->filename);
2663 clone_info=CloneImageInfo(image_info);
2664 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2666 if (*clone_info->magick !=
'\0')
2667 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2668 magick_info=GetMagickInfo(images->magick,exception);
2669 if (magick_info == (
const MagickInfo *) NULL)
2671 (void) ThrowMagickException(exception,GetMagickModule(),
2672 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2674 clone_info=DestroyImageInfo(clone_info);
2677 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2678 blob_support=GetMagickBlobSupport(magick_info);
2679 if ((blob_support != MagickFalse) &&
2680 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2682 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2683 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2684 blob_support=MagickFalse;
2686 if (blob_support != MagickFalse)
2691 (void) CloseBlob(images);
2692 *images->filename=
'\0';
2693 (void) WriteImages(clone_info,images,images->filename,exception);
2698 filename[MagickPathExtent],
2699 unique[MagickPathExtent];
2710 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2711 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2713 if (blob == (
unsigned char *) NULL)
2715 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2716 image_info->filename);
2717 clone_info=DestroyImageInfo(clone_info);
2720 file=AcquireUniqueFileResource(unique);
2723 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2724 image_info->filename);
2725 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2726 clone_info=DestroyImageInfo(clone_info);
2729 clone_info->file=fdopen(file,
"wb+");
2730 if (clone_info->file != (FILE *) NULL)
2735 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2736 images->magick,unique);
2737 status=WriteImages(clone_info,images,filename,exception);
2738 if (status != MagickFalse)
2740 (void) fseek(clone_info->file,0,SEEK_SET);
2741 count=(ssize_t) MagickMaxBufferExtent;
2742 while (count == (ssize_t) MagickMaxBufferExtent)
2744 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2746 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2747 image_info->custom_stream->data);
2750 (void) fclose(clone_info->file);
2752 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2753 (void) RelinquishUniqueFileResource(unique);
2755 clone_info=DestroyImageInfo(clone_info);
2791MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2792 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2795 filename[MagickPathExtent];
2824 assert(image_info != (ImageInfo *) NULL);
2825 assert(image_info->signature == MagickCoreSignature);
2826 assert(image != (Image *) NULL);
2827 assert(image->signature == MagickCoreSignature);
2828 assert(inject_image != (Image *) NULL);
2829 assert(inject_image->signature == MagickCoreSignature);
2830 assert(exception != (ExceptionInfo *) NULL);
2831 if (IsEventLogging() != MagickFalse)
2832 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2833 unique_file=(FILE *) NULL;
2834 file=AcquireUniqueFileResource(filename);
2836 unique_file=fdopen(file,
"wb");
2837 if ((file == -1) || (unique_file == (FILE *) NULL))
2839 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2840 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2842 return(MagickFalse);
2844 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2845 if (byte_image == (Image *) NULL)
2847 (void) fclose(unique_file);
2848 (void) RelinquishUniqueFileResource(filename);
2849 return(MagickFalse);
2851 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2853 DestroyBlob(byte_image);
2854 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2855 write_info=CloneImageInfo(image_info);
2856 SetImageInfoFile(write_info,unique_file);
2857 status=WriteImage(write_info,byte_image,exception);
2858 write_info=DestroyImageInfo(write_info);
2859 byte_image=DestroyImage(byte_image);
2860 (void) fclose(unique_file);
2861 if (status == MagickFalse)
2863 (void) RelinquishUniqueFileResource(filename);
2864 return(MagickFalse);
2869 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2872 (void) RelinquishUniqueFileResource(filename);
2873 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2874 image_info->filename);
2875 return(MagickFalse);
2877 quantum=(size_t) MagickMaxBufferExtent;
2878 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2879 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2880 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2881 if (buffer == (
unsigned char *) NULL)
2883 (void) RelinquishUniqueFileResource(filename);
2884 file=close_utf8(file);
2885 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2890 ssize_t count = read(file,buffer,quantum);
2897 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2900 file=close_utf8(file);
2902 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2903 (void) RelinquishUniqueFileResource(filename);
2904 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2930MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2932 assert(image != (
const Image *) NULL);
2933 assert(image->signature == MagickCoreSignature);
2934 if (IsEventLogging() != MagickFalse)
2935 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2936 return(image->blob->exempt);
2961MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2964 *magick_restrict blob_info;
2966 assert(image != (
const Image *) NULL);
2967 assert(image->signature == MagickCoreSignature);
2968 if (IsEventLogging() != MagickFalse)
2969 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2970 blob_info=image->blob;
2971 switch (blob_info->type)
2980 if (blob_info->file_info.file == (FILE *) NULL)
2981 return(MagickFalse);
2982 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2983 return(status == -1 ? MagickFalse : MagickTrue);
2987#if defined(MAGICKCORE_ZLIB_DELEGATE)
2991 if (blob_info->file_info.gzfile == (gzFile) NULL)
2992 return(MagickFalse);
2993 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2994 return(offset < 0 ? MagickFalse : MagickTrue);
2999 case UndefinedStream:
3003 case StandardStream:
3007 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3008 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3015 return(MagickFalse);
3040MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3042 assert(image != (
const Image *) NULL);
3043 assert(image->signature == MagickCoreSignature);
3044 if (IsEventLogging() != MagickFalse)
3045 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3046 return(image->blob->temporary);
3078MagickExport
void *MapBlob(
int file,
const MapMode mode,
3079 const MagickOffsetType offset,
const size_t length)
3081#if defined(MAGICKCORE_HAVE_MMAP)
3094#if defined(MAP_ANONYMOUS)
3095 flags|=MAP_ANONYMOUS;
3104 protection=PROT_READ;
3110 protection=PROT_WRITE;
3116 protection=PROT_READ | PROT_WRITE;
3121#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3122 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3124 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3125 if (map == MAP_FAILED)
3126 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3128 if (map == MAP_FAILED)
3165MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3174 assert(buffer != (
unsigned char *) NULL);
3181 *buffer++=(
unsigned char) c;
3185 *buffer++=(
unsigned char) c;
3215MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3223 assert(p != (
unsigned char *) NULL);
3230 *p++=(
unsigned char) c;
3266static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3267 const BlobInfo *blob_info)
3278 size=MagickMinBufferExtent;
3279 option=GetImageOption(image_info,
"stream:buffer-size");
3280 if (option != (
const char *) NULL)
3281 size=StringToUnsignedLong(option);
3282 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3283 _IONBF : _IOFBF,size);
3284 return(status == 0 ? MagickTrue : MagickFalse);
3287#if defined(MAGICKCORE_ZLIB_DELEGATE)
3288static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3290#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3291 return(gzopen(path,mode));
3299 path_wide=NTCreateWidePath(path);
3300 if (path_wide == (
wchar_t *) NULL)
3301 return((gzFile) NULL);
3302 file=gzopen_w(path_wide,mode);
3303 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3309MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3310 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3313 *magick_restrict blob_info;
3316 extension[MagickPathExtent],
3317 filename[MagickPathExtent];
3331 assert(image_info != (ImageInfo *) NULL);
3332 assert(image_info->signature == MagickCoreSignature);
3333 assert(image != (Image *) NULL);
3334 assert(image->signature == MagickCoreSignature);
3335 if (IsEventLogging() != MagickFalse)
3336 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3337 image_info->filename);
3338 blob_info=image->blob;
3339 if (image_info->blob != (
void *) NULL)
3341 if (image_info->stream != (StreamHandler) NULL)
3342 blob_info->stream=(StreamHandler) image_info->stream;
3343 AttachBlob(blob_info,image_info->blob,image_info->length);
3346 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3347 (*image->filename ==
'\0'))
3349 blob_info->type=CustomStream;
3350 blob_info->custom_stream=image_info->custom_stream;
3353 (void) DetachBlob(blob_info);
3354 blob_info->mode=mode;
3361 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3364 case ReadBinaryBlobMode:
3366 flags=O_RDONLY | O_BINARY;
3368 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3373 flags=O_WRONLY | O_CREAT | O_TRUNC;
3375 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
3378 case WriteBinaryBlobMode:
3380 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3382 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
3383 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
3384 MagickTrue : MagickFalse;
3387 case AppendBlobMode:
3389 flags=O_WRONLY | O_CREAT | O_APPEND;
3391 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
3394 case AppendBinaryBlobMode:
3396 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3398 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
3399 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
3400 MagickTrue : MagickFalse;
3407 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3411#if defined(O_NOFOLLOW)
3412 if (status == MagickFalse)
3416 blob_info->synchronize=image_info->synchronize;
3417 if (image_info->stream != (StreamHandler) NULL)
3419 blob_info->stream=image_info->stream;
3422 blob_info->type=FifoStream;
3430 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3431 rights=ReadPolicyRights;
3433 rights=WritePolicyRights;
3434 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3437 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3438 "NotAuthorized",
"`%s'",filename);
3439 return(MagickFalse);
3441 if ((LocaleCompare(filename,
"-") == 0) ||
3442 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3444 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3445#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3446 if (strchr(type,
'b') != (
char *) NULL)
3447 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3449 blob_info->type=StandardStream;
3450 blob_info->exempt=MagickTrue;
3451 return(SetStreamBuffering(image_info,blob_info));
3453 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3454 (IsGeometry(filename+3) != MagickFalse))
3461 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3462 if (blob_info->file_info.file == (FILE *) NULL)
3464 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3465 return(MagickFalse);
3467#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3468 if (strchr(type,
'b') != (
char *) NULL)
3469 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3471 blob_info->type=FileStream;
3472 blob_info->exempt=MagickTrue;
3473 return(SetStreamBuffering(image_info,blob_info));
3475#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3476 if (*filename ==
'|')
3479 fileMode[MagickPathExtent],
3487 (void) signal(SIGPIPE,SIG_IGN);
3491 sanitize_command=SanitizeString(filename+1);
3492 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3493 sanitize_command=DestroyString(sanitize_command);
3494 if (blob_info->file_info.file == (FILE *) NULL)
3496 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3497 return(MagickFalse);
3499 blob_info->type=PipeStream;
3500 blob_info->exempt=MagickTrue;
3501 return(SetStreamBuffering(image_info,blob_info));
3504 status=GetPathAttributes(filename,&blob_info->properties);
3505#if defined(S_ISFIFO)
3506 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3508 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3509 if (blob_info->file_info.file == (FILE *) NULL)
3511 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3512 return(MagickFalse);
3514 blob_info->type=FileStream;
3515 blob_info->exempt=MagickTrue;
3516 return(SetStreamBuffering(image_info,blob_info));
3519 GetPathComponent(image->filename,ExtensionPath,extension);
3522 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3523 if ((image_info->adjoin == MagickFalse) ||
3524 (strchr(filename,
'%') != (
char *) NULL))
3529 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3530 image->scene,filename,exception);
3531 if ((LocaleCompare(filename,image->filename) == 0) &&
3532 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3533 (GetNextImageInList(image) != (Image *) NULL)))
3536 path[MagickPathExtent];
3538 GetPathComponent(image->filename,RootPath,path);
3539 if (*extension ==
'\0')
3540 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3541 path,(
double) image->scene);
3543 (
void) FormatLocaleString(filename,MagickPathExtent,
3544 "%s-%.20g.%s",path,(
double) image->scene,extension);
3546 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3549 if (image_info->file != (FILE *) NULL)
3551 blob_info->file_info.file=image_info->file;
3552 blob_info->type=FileStream;
3553 blob_info->exempt=MagickTrue;
3561 blob_info->file_info.file=(FILE *) NULL;
3562 file=open_utf8(filename,flags,0);
3564 blob_info->file_info.file=fdopen(file,type);
3565 if (blob_info->file_info.file != (FILE *) NULL)
3573 blob_info->type=FileStream;
3574 (void) SetStreamBuffering(image_info,blob_info);
3575 (void) memset(magick,0,
sizeof(magick));
3576 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3577 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3578#if defined(MAGICKCORE_POSIX_SUPPORT)
3579 (void) fflush(blob_info->file_info.file);
3581 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3582 " read %.20g magic header bytes",(
double) count);
3583#if defined(MAGICKCORE_ZLIB_DELEGATE)
3584 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3585 ((
int) magick[2] == 0x08))
3588 gzfile = gzopen_utf8(filename,
"rb");
3590 if (gzfile != (gzFile) NULL)
3592 if (blob_info->file_info.file != (FILE *) NULL)
3593 (void) fclose(blob_info->file_info.file);
3594 blob_info->file_info.file=(FILE *) NULL;
3595 blob_info->file_info.gzfile=gzfile;
3596 blob_info->type=ZipStream;
3600#if defined(MAGICKCORE_BZLIB_DELEGATE)
3601 if (strncmp((
char *) magick,
"BZh",3) == 0)
3604 *bzfile = BZ2_bzopen(filename,
"r");
3606 if (bzfile != (BZFILE *) NULL)
3608 if (blob_info->file_info.file != (FILE *) NULL)
3609 (void) fclose(blob_info->file_info.file);
3610 blob_info->file_info.file=(FILE *) NULL;
3611 blob_info->file_info.bzfile=bzfile;
3612 blob_info->type=BZipStream;
3616 if (blob_info->type == FileStream)
3627 sans_exception=AcquireExceptionInfo();
3628 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3629 sans_exception=DestroyExceptionInfo(sans_exception);
3630 length=(size_t) blob_info->properties.st_size;
3631 if ((magick_info != (
const MagickInfo *) NULL) &&
3632 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3633 (length > MagickMaxBufferExtent) &&
3634 (AcquireMagickResource(MapResource,length) != MagickFalse))
3639 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3641 if (blob == (
void *) NULL)
3642 RelinquishMagickResource(MapResource,length);
3648 if (image_info->file != (FILE *) NULL)
3649 blob_info->exempt=MagickFalse;
3652 (void) fclose(blob_info->file_info.file);
3653 blob_info->file_info.file=(FILE *) NULL;
3655 AttachBlob(blob_info,blob,length);
3656 blob_info->mapped=MagickTrue;
3663#if defined(MAGICKCORE_ZLIB_DELEGATE)
3664 if ((LocaleCompare(extension,
"gz") == 0) ||
3665 (LocaleCompare(extension,
"wmz") == 0) ||
3666 (LocaleCompare(extension,
"svgz") == 0))
3668 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3669 if (blob_info->file_info.gzfile != (gzFile) NULL)
3670 blob_info->type=ZipStream;
3674#if defined(MAGICKCORE_BZLIB_DELEGATE)
3675 if (LocaleCompare(extension,
"bz2") == 0)
3677 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3678 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3679 blob_info->type=BZipStream;
3687 blob_info->file_info.file=(FILE *) NULL;
3688 file=open_utf8(filename,flags,P_MODE);
3690 blob_info->file_info.file=fdopen(file,type);
3691 if (blob_info->file_info.file != (FILE *) NULL)
3693 blob_info->type=FileStream;
3694 (void) SetStreamBuffering(image_info,blob_info);
3697 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3700 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3701 "NotAuthorized",
"`%s'",filename);
3702 return(MagickFalse);
3704 blob_info->status=0;
3705 blob_info->error_number=0;
3706 if (blob_info->type != UndefinedStream)
3707 blob_info->size=GetBlobSize(image);
3710 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3711 return(MagickFalse);
3750#if defined(__cplusplus) || defined(c_plusplus)
3754static size_t PingStream(
const Image *magick_unused(image),
3755 const void *magick_unused(pixels),
const size_t columns)
3757 magick_unreferenced(image);
3758 magick_unreferenced(pixels);
3762#if defined(__cplusplus) || defined(c_plusplus)
3766MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3767 const size_t length,ExceptionInfo *exception)
3782 assert(image_info != (ImageInfo *) NULL);
3783 assert(image_info->signature == MagickCoreSignature);
3784 assert(exception != (ExceptionInfo *) NULL);
3785 if (IsEventLogging() != MagickFalse)
3786 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3787 image_info->filename);
3788 if ((blob == (
const void *) NULL) || (length == 0))
3790 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3791 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3792 return((Image *) NULL);
3794 ping_info=CloneImageInfo(image_info);
3795 ping_info->blob=(
void *) blob;
3796 ping_info->length=length;
3797 ping_info->ping=MagickTrue;
3798 if (*ping_info->magick ==
'\0')
3799 (void) SetImageInfo(ping_info,0,exception);
3800 magick_info=GetMagickInfo(ping_info->magick,exception);
3801 if (magick_info == (
const MagickInfo *) NULL)
3803 (void) ThrowMagickException(exception,GetMagickModule(),
3804 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3806 ping_info=DestroyImageInfo(ping_info);
3807 return((Image *) NULL);
3809 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3812 filename[MagickPathExtent];
3817 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3818 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3819 ping_info->magick,filename);
3820 image=ReadStream(ping_info,&PingStream,exception);
3821 if (image != (Image *) NULL)
3822 (void) DetachBlob(image->blob);
3823 ping_info=DestroyImageInfo(ping_info);
3829 ping_info->blob=(
void *) NULL;
3830 ping_info->length=0;
3831 *ping_info->filename=
'\0';
3832 status=BlobToFile(ping_info->filename,blob,length,exception);
3833 if (status == MagickFalse)
3835 (void) RelinquishUniqueFileResource(ping_info->filename);
3836 ping_info=DestroyImageInfo(ping_info);
3837 return((Image *) NULL);
3839 clone_info=CloneImageInfo(ping_info);
3840 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3841 ping_info->magick,ping_info->filename);
3842 image=ReadStream(clone_info,&PingStream,exception);
3843 if (image != (Image *) NULL)
3851 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3853 (void) CopyMagickString(images->filename,image_info->filename,
3855 (void) CopyMagickString(images->magick_filename,image_info->filename,
3857 (void) CopyMagickString(images->magick,magick_info->name,
3859 images=GetNextImageInList(images);
3862 clone_info=DestroyImageInfo(clone_info);
3863 (void) RelinquishUniqueFileResource(ping_info->filename);
3864 ping_info=DestroyImageInfo(ping_info);
3899MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3902 *magick_restrict blob_info;
3913 assert(image != (Image *) NULL);
3914 assert(image->signature == MagickCoreSignature);
3915 assert(image->blob != (BlobInfo *) NULL);
3916 assert(image->blob->type != UndefinedStream);
3919 assert(data != (
void *) NULL);
3920 blob_info=image->blob;
3922 q=(
unsigned char *) data;
3923 switch (blob_info->type)
3925 case UndefinedStream:
3927 case StandardStream:
3935 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3940 c=getc(blob_info->file_info.file);
3943 *q++=(
unsigned char) c;
3949 c=getc(blob_info->file_info.file);
3952 *q++=(
unsigned char) c;
3958 c=getc(blob_info->file_info.file);
3961 *q++=(
unsigned char) c;
3967 c=getc(blob_info->file_info.file);
3970 *q++=(
unsigned char) c;
3977 if ((count != (ssize_t) length) &&
3978 (ferror(blob_info->file_info.file) != 0))
3979 ThrowBlobException(blob_info);
3984#if defined(MAGICKCORE_ZLIB_DELEGATE)
3995 for (i=0; i < length; i+=(size_t) count)
3997 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3998 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4011 c=gzgetc(blob_info->file_info.gzfile);
4014 *q++=(
unsigned char) c;
4020 c=gzgetc(blob_info->file_info.gzfile);
4023 *q++=(
unsigned char) c;
4029 c=gzgetc(blob_info->file_info.gzfile);
4032 *q++=(
unsigned char) c;
4038 c=gzgetc(blob_info->file_info.gzfile);
4041 *q++=(
unsigned char) c;
4048 (void) gzerror(blob_info->file_info.gzfile,&status);
4049 if ((count != (ssize_t) length) && (status != Z_OK))
4050 ThrowBlobException(blob_info);
4051 if (blob_info->eof == MagickFalse)
4052 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4059#if defined(MAGICKCORE_BZLIB_DELEGATE)
4066 for (i=0; i < length; i+=(size_t) count)
4068 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4069 MagickMin(length-i,MagickMaxBufferExtent));
4079 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4080 if ((count != (ssize_t) length) && (status != BZ_OK))
4081 ThrowBlobException(blob_info);
4092 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4094 blob_info->eof=MagickTrue;
4097 p=blob_info->data+blob_info->offset;
4098 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4099 blob_info->length-blob_info->offset);
4100 blob_info->offset+=count;
4101 if (count != (ssize_t) length)
4102 blob_info->eof=MagickTrue;
4103 (void) memcpy(q,p,(
size_t) count);
4108 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4109 count=blob_info->custom_stream->reader(q,length,
4110 blob_info->custom_stream->data);
4139MagickExport
int ReadBlobByte(Image *image)
4142 *magick_restrict blob_info;
4147 assert(image != (Image *) NULL);
4148 assert(image->signature == MagickCoreSignature);
4149 assert(image->blob != (BlobInfo *) NULL);
4150 assert(image->blob->type != UndefinedStream);
4151 blob_info=image->blob;
4152 switch (blob_info->type)
4154 case StandardStream:
4158 c=getc(blob_info->file_info.file);
4161 if (ferror(blob_info->file_info.file) != 0)
4162 ThrowBlobException(blob_info);
4169 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4171 blob_info->eof=MagickTrue;
4174 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4175 blob_info->offset++;
4186 count=ReadBlob(image,1,buffer);
4219MagickExport
double ReadBlobDouble(Image *image)
4230 quantum.double_value=0.0;
4231 quantum.unsigned_value=ReadBlobLongLong(image);
4232 return(quantum.double_value);
4258MagickExport
float ReadBlobFloat(Image *image)
4269 quantum.float_value=0.0;
4270 quantum.unsigned_value=ReadBlobLong(image);
4271 return(quantum.float_value);
4297MagickExport
unsigned int ReadBlobLong(Image *image)
4311 assert(image != (Image *) NULL);
4312 assert(image->signature == MagickCoreSignature);
4314 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4317 if (image->endian == LSBEndian)
4319 value=(
unsigned int) (*p++);
4320 value|=(
unsigned int) (*p++) << 8;
4321 value|=(
unsigned int) (*p++) << 16;
4322 value|=(
unsigned int) (*p++) << 24;
4325 value=(
unsigned int) (*p++) << 24;
4326 value|=(
unsigned int) (*p++) << 16;
4327 value|=(
unsigned int) (*p++) << 8;
4328 value|=(
unsigned int) (*p++);
4355MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4369 assert(image != (Image *) NULL);
4370 assert(image->signature == MagickCoreSignature);
4372 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4374 return(MagickULLConstant(0));
4375 if (image->endian == LSBEndian)
4377 value=(MagickSizeType) (*p++);
4378 value|=(MagickSizeType) (*p++) << 8;
4379 value|=(MagickSizeType) (*p++) << 16;
4380 value|=(MagickSizeType) (*p++) << 24;
4381 value|=(MagickSizeType) (*p++) << 32;
4382 value|=(MagickSizeType) (*p++) << 40;
4383 value|=(MagickSizeType) (*p++) << 48;
4384 value|=(MagickSizeType) (*p++) << 56;
4387 value=(MagickSizeType) (*p++) << 56;
4388 value|=(MagickSizeType) (*p++) << 48;
4389 value|=(MagickSizeType) (*p++) << 40;
4390 value|=(MagickSizeType) (*p++) << 32;
4391 value|=(MagickSizeType) (*p++) << 24;
4392 value|=(MagickSizeType) (*p++) << 16;
4393 value|=(MagickSizeType) (*p++) << 8;
4394 value|=(MagickSizeType) (*p++);
4421MagickExport
unsigned short ReadBlobShort(Image *image)
4435 assert(image != (Image *) NULL);
4436 assert(image->signature == MagickCoreSignature);
4438 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4440 return((
unsigned short) 0U);
4441 if (image->endian == LSBEndian)
4443 value=(
unsigned short) (*p++);
4444 value|=(
unsigned short) (*p++) << 8;
4447 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4448 value|=(
unsigned short) (*p++);
4475MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4489 assert(image != (Image *) NULL);
4490 assert(image->signature == MagickCoreSignature);
4492 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4495 value=(
unsigned int) (*p++);
4496 value|=(
unsigned int) (*p++) << 8;
4497 value|=(
unsigned int) (*p++) << 16;
4498 value|=(
unsigned int) (*p++) << 24;
4525MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4536 quantum.unsigned_value=ReadBlobLSBLong(image);
4537 return(quantum.signed_value);
4563MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4577 assert(image != (Image *) NULL);
4578 assert(image->signature == MagickCoreSignature);
4580 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4582 return((
unsigned short) 0U);
4583 value=(
unsigned short) (*p++);
4584 value|=(
unsigned short) (*p++) << 8;
4611MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4622 quantum.unsigned_value=ReadBlobLSBShort(image);
4623 return(quantum.signed_value);
4649MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4663 assert(image != (Image *) NULL);
4664 assert(image->signature == MagickCoreSignature);
4666 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4669 value=(
unsigned int) (*p++) << 24;
4670 value|=(
unsigned int) (*p++) << 16;
4671 value|=(
unsigned int) (*p++) << 8;
4672 value|=(
unsigned int) (*p++);
4699MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4713 assert(image != (Image *) NULL);
4714 assert(image->signature == MagickCoreSignature);
4716 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4718 return(MagickULLConstant(0));
4719 value=(MagickSizeType) (*p++) << 56;
4720 value|=(MagickSizeType) (*p++) << 48;
4721 value|=(MagickSizeType) (*p++) << 40;
4722 value|=(MagickSizeType) (*p++) << 32;
4723 value|=(MagickSizeType) (*p++) << 24;
4724 value|=(MagickSizeType) (*p++) << 16;
4725 value|=(MagickSizeType) (*p++) << 8;
4726 value|=(MagickSizeType) (*p++);
4753MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4767 assert(image != (Image *) NULL);
4768 assert(image->signature == MagickCoreSignature);
4770 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4772 return((
unsigned short) 0U);
4773 value=(
unsigned short) ((*p++) << 8);
4774 value|=(
unsigned short) (*p++);
4775 return((
unsigned short) (value & 0xffff));
4801MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4812 quantum.unsigned_value=ReadBlobMSBLong(image);
4813 return(quantum.signed_value);
4839MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4850 quantum.unsigned_value=ReadBlobMSBShort(image);
4851 return(quantum.signed_value);
4877MagickExport
signed int ReadBlobSignedLong(Image *image)
4888 quantum.unsigned_value=ReadBlobLong(image);
4889 return(quantum.signed_value);
4915MagickExport
signed short ReadBlobSignedShort(Image *image)
4926 quantum.unsigned_value=ReadBlobShort(image);
4927 return(quantum.signed_value);
4965MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4966 const size_t length,
void *magick_restrict data,ssize_t *count)
4969 *magick_restrict blob_info;
4971 assert(image != (Image *) NULL);
4972 assert(image->signature == MagickCoreSignature);
4973 assert(image->blob != (BlobInfo *) NULL);
4974 assert(image->blob->type != UndefinedStream);
4975 assert(count != (ssize_t *) NULL);
4976 blob_info=image->blob;
4977 if (blob_info->type != BlobStream)
4979 assert(data != NULL);
4980 *count=ReadBlob(image,length,(
unsigned char *) data);
4983 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4986 blob_info->eof=MagickTrue;
4989 data=blob_info->data+blob_info->offset;
4990 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4991 blob_info->length-blob_info->offset);
4992 blob_info->offset+=(*count);
4993 if (*count != (ssize_t) length)
4994 blob_info->eof=MagickTrue;
5023MagickExport
char *ReadBlobString(Image *image,
char *
string)
5026 *magick_restrict blob_info;
5034 assert(image != (Image *) NULL);
5035 assert(image->signature == MagickCoreSignature);
5036 assert(image->blob != (BlobInfo *) NULL);
5037 assert(image->blob->type != UndefinedStream);
5038 if (IsEventLogging() != MagickFalse)
5039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5041 blob_info=image->blob;
5042 switch (blob_info->type)
5044 case UndefinedStream:
5046 case StandardStream:
5049 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5050 if (p == (
char *) NULL)
5052 if (ferror(blob_info->file_info.file) != 0)
5053 ThrowBlobException(blob_info);
5054 return((
char *) NULL);
5061#if defined(MAGICKCORE_ZLIB_DELEGATE)
5062 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5063 if (p == (
char *) NULL)
5066 (void) gzerror(blob_info->file_info.gzfile,&status);
5068 ThrowBlobException(blob_info);
5069 return((
char *) NULL);
5079 c=ReadBlobByte(image);
5082 blob_info->eof=MagickTrue;
5085 string[i++]=(char) c;
5088 }
while (i < (MaxTextExtent-2));
5096 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5099 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5101 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5102 return((
char *) NULL);
5129MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5131 assert(blob != (BlobInfo *) NULL);
5132 assert(blob->signature == MagickCoreSignature);
5133 if (IsEventLogging() != MagickFalse)
5134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5135 LockSemaphoreInfo(blob->semaphore);
5136 blob->reference_count++;
5137 UnlockSemaphoreInfo(blob->semaphore);
5174MagickExport MagickOffsetType SeekBlob(Image *image,
5175 const MagickOffsetType offset,
const int whence)
5178 *magick_restrict blob_info;
5180 assert(image != (Image *) NULL);
5181 assert(image->signature == MagickCoreSignature);
5182 assert(image->blob != (BlobInfo *) NULL);
5183 assert(image->blob->type != UndefinedStream);
5184 if (IsEventLogging() != MagickFalse)
5185 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5186 blob_info=image->blob;
5187 switch (blob_info->type)
5189 case UndefinedStream:
5191 case StandardStream:
5196 if ((offset < 0) && (whence == SEEK_SET))
5198 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5200 blob_info->offset=TellBlob(image);
5205#if defined(MAGICKCORE_ZLIB_DELEGATE)
5206 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5209 blob_info->offset=TellBlob(image);
5225 blob_info->offset=offset;
5230 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5231 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5236 if ((blob_info->offset+offset) < 0)
5238 blob_info->offset+=offset;
5243 if (((MagickOffsetType) blob_info->length+offset) < 0)
5245 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5249 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5251 blob_info->eof=MagickFalse;
5258 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5260 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5261 blob_info->custom_stream->data);
5265 return(blob_info->offset);
5293MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5295 assert(image != (
const Image *) NULL);
5296 assert(image->signature == MagickCoreSignature);
5297 if (IsEventLogging() != MagickFalse)
5298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5299 image->blob->exempt=exempt;
5328MagickExport MagickBooleanType SetBlobExtent(Image *image,
5329 const MagickSizeType extent)
5332 *magick_restrict blob_info;
5334 assert(image != (Image *) NULL);
5335 assert(image->signature == MagickCoreSignature);
5336 assert(image->blob != (BlobInfo *) NULL);
5337 assert(image->blob->type != UndefinedStream);
5338 if (IsEventLogging() != MagickFalse)
5339 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5340 blob_info=image->blob;
5341 switch (blob_info->type)
5343 case UndefinedStream:
5345 case StandardStream:
5346 return(MagickFalse);
5355 if (extent != (MagickSizeType) ((off_t) extent))
5356 return(MagickFalse);
5357 offset=SeekBlob(image,0,SEEK_END);
5359 return(MagickFalse);
5360 if ((MagickSizeType) offset >= extent)
5362 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5365 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5366 blob_info->file_info.file);
5367#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5368 if (blob_info->synchronize != MagickFalse)
5373 file=fileno(blob_info->file_info.file);
5374 if ((file == -1) || (offset < 0))
5375 return(MagickFalse);
5376 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5379 offset=SeekBlob(image,offset,SEEK_SET);
5381 return(MagickFalse);
5386 return(MagickFalse);
5388 return(MagickFalse);
5390 return(MagickFalse);
5393 if (extent != (MagickSizeType) ((
size_t) extent))
5394 return(MagickFalse);
5395 if (blob_info->mapped != MagickFalse)
5403 (void) UnmapBlob(blob_info->data,blob_info->length);
5404 RelinquishMagickResource(MapResource,blob_info->length);
5405 if (extent != (MagickSizeType) ((off_t) extent))
5406 return(MagickFalse);
5407 offset=SeekBlob(image,0,SEEK_END);
5409 return(MagickFalse);
5410 if ((MagickSizeType) offset >= extent)
5412 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5413 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5414 blob_info->file_info.file);
5415#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5416 if (blob_info->synchronize != MagickFalse)
5421 file=fileno(blob_info->file_info.file);
5422 if ((file == -1) || (offset < 0))
5423 return(MagickFalse);
5424 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5428 offset=SeekBlob(image,offset,SEEK_SET);
5430 return(MagickFalse);
5431 (void) AcquireMagickResource(MapResource,extent);
5432 blob_info->data=(
unsigned char*) MapBlob(fileno(
5433 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5434 blob_info->extent=(size_t) extent;
5435 blob_info->length=(size_t) extent;
5436 (void) SyncBlob(image);
5439 blob_info->extent=(size_t) extent;
5440 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5441 blob_info->extent+1,
sizeof(*blob_info->data));
5442 (void) SyncBlob(image);
5443 if (blob_info->data == (
unsigned char *) NULL)
5445 (void) DetachBlob(blob_info);
5446 return(MagickFalse);
5480MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5483 assert(custom_stream != (CustomStreamInfo *) NULL);
5484 assert(custom_stream->signature == MagickCoreSignature);
5485 custom_stream->data=data;
5513MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5514 CustomStreamHandler reader)
5516 assert(custom_stream != (CustomStreamInfo *) NULL);
5517 assert(custom_stream->signature == MagickCoreSignature);
5518 custom_stream->reader=reader;
5546MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5547 CustomStreamSeeker seeker)
5549 assert(custom_stream != (CustomStreamInfo *) NULL);
5550 assert(custom_stream->signature == MagickCoreSignature);
5551 custom_stream->seeker=seeker;
5579MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5580 CustomStreamTeller teller)
5582 assert(custom_stream != (CustomStreamInfo *) NULL);
5583 assert(custom_stream->signature == MagickCoreSignature);
5584 custom_stream->teller=teller;
5612MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5613 CustomStreamHandler writer)
5615 assert(custom_stream != (CustomStreamInfo *) NULL);
5616 assert(custom_stream->signature == MagickCoreSignature);
5617 custom_stream->writer=writer;
5644static int SyncBlob(
const Image *image)
5647 *magick_restrict blob_info;
5652 assert(image != (Image *) NULL);
5653 assert(image->signature == MagickCoreSignature);
5654 assert(image->blob != (BlobInfo *) NULL);
5655 if (IsEventLogging() != MagickFalse)
5656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5657 if (EOFBlob(image) != 0)
5659 blob_info=image->blob;
5661 switch (blob_info->type)
5663 case UndefinedStream:
5664 case StandardStream:
5669 status=fflush(blob_info->file_info.file);
5674#if defined(MAGICKCORE_ZLIB_DELEGATE)
5675 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5681#if defined(MAGICKCORE_BZLIB_DELEGATE)
5682 status=BZ2_bzflush(blob_info->file_info.bzfile);
5718MagickExport MagickOffsetType TellBlob(
const Image *image)
5721 *magick_restrict blob_info;
5726 assert(image != (Image *) NULL);
5727 assert(image->signature == MagickCoreSignature);
5728 assert(image->blob != (BlobInfo *) NULL);
5729 assert(image->blob->type != UndefinedStream);
5730 if (IsEventLogging() != MagickFalse)
5731 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5732 blob_info=image->blob;
5734 switch (blob_info->type)
5736 case UndefinedStream:
5737 case StandardStream:
5741 offset=ftell(blob_info->file_info.file);
5748#if defined(MAGICKCORE_ZLIB_DELEGATE)
5749 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5759 offset=blob_info->offset;
5764 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5765 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5797MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5799#if defined(MAGICKCORE_HAVE_MMAP)
5803 status=munmap(map,length);
5804 return(status == -1 ? MagickFalse : MagickTrue);
5808 return(MagickFalse);
5840MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5844 *magick_restrict blob_info;
5858 assert(image != (Image *) NULL);
5859 assert(image->signature == MagickCoreSignature);
5860 assert(image->blob != (BlobInfo *) NULL);
5861 assert(image->blob->type != UndefinedStream);
5864 assert(data != (
const void *) NULL);
5865 blob_info=image->blob;
5867 p=(
const unsigned char *) data;
5868 q=(
unsigned char *) data;
5869 switch (blob_info->type)
5871 case UndefinedStream:
5873 case StandardStream:
5881 count=(ssize_t) fwrite((
const char *) data,1,length,
5882 blob_info->file_info.file);
5887 c=putc((
int) *p++,blob_info->file_info.file);
5895 c=putc((
int) *p++,blob_info->file_info.file);
5903 c=putc((
int) *p++,blob_info->file_info.file);
5911 c=putc((
int) *p++,blob_info->file_info.file);
5920 if ((count != (ssize_t) length) &&
5921 (ferror(blob_info->file_info.file) != 0))
5922 ThrowBlobException(blob_info);
5927#if defined(MAGICKCORE_ZLIB_DELEGATE)
5938 for (i=0; i < length; i+=(size_t) count)
5940 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5941 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5954 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5962 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5970 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5978 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5988 (void) gzerror(blob_info->file_info.gzfile,&status);
5989 if ((count != (ssize_t) length) && (status != Z_OK))
5990 ThrowBlobException(blob_info);
5996#if defined(MAGICKCORE_BZLIB_DELEGATE)
6003 for (i=0; i < length; i+=(size_t) count)
6005 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6006 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6016 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6017 if ((count != (ssize_t) length) && (status != BZ_OK))
6018 ThrowBlobException(blob_info);
6024 count=(ssize_t) blob_info->stream(image,data,length);
6032 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6037 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6038 if (extent >= blob_info->extent)
6040 extent+=blob_info->quantum+length;
6041 blob_info->quantum<<=1;
6042 if (SetBlobExtent(image,extent) == MagickFalse)
6045 q=blob_info->data+blob_info->offset;
6046 (void) memcpy(q,p,length);
6047 blob_info->offset+=(MagickOffsetType) length;
6048 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6049 blob_info->length=(size_t) blob_info->offset;
6050 count=(ssize_t) length;
6055 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6056 count=blob_info->custom_stream->writer((
unsigned char *) data,
6057 length,blob_info->custom_stream->data);
6089MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6092 *magick_restrict blob_info;
6097 assert(image != (Image *) NULL);
6098 assert(image->signature == MagickCoreSignature);
6099 assert(image->blob != (BlobInfo *) NULL);
6100 assert(image->blob->type != UndefinedStream);
6101 blob_info=image->blob;
6103 switch (blob_info->type)
6105 case StandardStream:
6112 c=putc((
int) value,blob_info->file_info.file);
6115 if (ferror(blob_info->file_info.file) != 0)
6116 ThrowBlobException(blob_info);
6124 count=WriteBlobStream(image,1,&value);
6156MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6167 quantum.unsigned_value=0U;
6168 quantum.float_value=value;
6169 return(WriteBlobLong(image,quantum.unsigned_value));
6197MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6202 assert(image != (Image *) NULL);
6203 assert(image->signature == MagickCoreSignature);
6204 if (image->endian == LSBEndian)
6206 buffer[0]=(
unsigned char) value;
6207 buffer[1]=(
unsigned char) (value >> 8);
6208 buffer[2]=(
unsigned char) (value >> 16);
6209 buffer[3]=(
unsigned char) (value >> 24);
6210 return(WriteBlobStream(image,4,buffer));
6212 buffer[0]=(
unsigned char) (value >> 24);
6213 buffer[1]=(
unsigned char) (value >> 16);
6214 buffer[2]=(
unsigned char) (value >> 8);
6215 buffer[3]=(
unsigned char) value;
6216 return(WriteBlobStream(image,4,buffer));
6244MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6249 assert(image != (Image *) NULL);
6250 assert(image->signature == MagickCoreSignature);
6251 if (image->endian == LSBEndian)
6253 buffer[0]=(
unsigned char) value;
6254 buffer[1]=(
unsigned char) (value >> 8);
6255 buffer[2]=(
unsigned char) (value >> 16);
6256 buffer[3]=(
unsigned char) (value >> 24);
6257 buffer[4]=(
unsigned char) (value >> 32);
6258 buffer[5]=(
unsigned char) (value >> 40);
6259 buffer[6]=(
unsigned char) (value >> 48);
6260 buffer[7]=(
unsigned char) (value >> 56);
6261 return(WriteBlobStream(image,8,buffer));
6263 buffer[0]=(
unsigned char) (value >> 56);
6264 buffer[1]=(
unsigned char) (value >> 48);
6265 buffer[2]=(
unsigned char) (value >> 40);
6266 buffer[3]=(
unsigned char) (value >> 32);
6267 buffer[4]=(
unsigned char) (value >> 24);
6268 buffer[5]=(
unsigned char) (value >> 16);
6269 buffer[6]=(
unsigned char) (value >> 8);
6270 buffer[7]=(
unsigned char) value;
6271 return(WriteBlobStream(image,8,buffer));
6299MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6304 assert(image != (Image *) NULL);
6305 assert(image->signature == MagickCoreSignature);
6306 if (image->endian == LSBEndian)
6308 buffer[0]=(
unsigned char) value;
6309 buffer[1]=(
unsigned char) (value >> 8);
6310 return(WriteBlobStream(image,2,buffer));
6312 buffer[0]=(
unsigned char) (value >> 8);
6313 buffer[1]=(
unsigned char) value;
6314 return(WriteBlobStream(image,2,buffer));
6342MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6356 assert(image != (Image *) NULL);
6357 assert(image->signature == MagickCoreSignature);
6358 quantum.signed_value=value;
6359 if (image->endian == LSBEndian)
6361 buffer[0]=(
unsigned char) quantum.unsigned_value;
6362 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6363 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6364 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6365 return(WriteBlobStream(image,4,buffer));
6367 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6368 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6369 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6370 buffer[3]=(
unsigned char) quantum.unsigned_value;
6371 return(WriteBlobStream(image,4,buffer));
6399MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6404 assert(image != (Image *) NULL);
6405 assert(image->signature == MagickCoreSignature);
6406 buffer[0]=(
unsigned char) value;
6407 buffer[1]=(
unsigned char) (value >> 8);
6408 buffer[2]=(
unsigned char) (value >> 16);
6409 buffer[3]=(
unsigned char) (value >> 24);
6410 return(WriteBlobStream(image,4,buffer));
6438MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6443 assert(image != (Image *) NULL);
6444 assert(image->signature == MagickCoreSignature);
6445 buffer[0]=(
unsigned char) value;
6446 buffer[1]=(
unsigned char) (value >> 8);
6447 return(WriteBlobStream(image,2,buffer));
6475MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6489 assert(image != (Image *) NULL);
6490 assert(image->signature == MagickCoreSignature);
6491 quantum.signed_value=value;
6492 buffer[0]=(
unsigned char) quantum.unsigned_value;
6493 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6494 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6495 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6496 return(WriteBlobStream(image,4,buffer));
6524MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6525 const signed short value)
6539 assert(image != (Image *) NULL);
6540 assert(image->signature == MagickCoreSignature);
6541 quantum.signed_value=value;
6542 buffer[0]=(
unsigned char) quantum.unsigned_value;
6543 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6544 return(WriteBlobStream(image,2,buffer));
6572MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6577 assert(image != (Image *) NULL);
6578 assert(image->signature == MagickCoreSignature);
6579 buffer[0]=(
unsigned char) (value >> 24);
6580 buffer[1]=(
unsigned char) (value >> 16);
6581 buffer[2]=(
unsigned char) (value >> 8);
6582 buffer[3]=(
unsigned char) value;
6583 return(WriteBlobStream(image,4,buffer));
6611MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6612 const signed short value)
6626 assert(image != (Image *) NULL);
6627 assert(image->signature == MagickCoreSignature);
6628 quantum.signed_value=value;
6629 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6630 buffer[1]=(
unsigned char) quantum.unsigned_value;
6631 return(WriteBlobStream(image,2,buffer));
6659MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6664 assert(image != (Image *) NULL);
6665 assert(image->signature == MagickCoreSignature);
6666 buffer[0]=(
unsigned char) (value >> 8);
6667 buffer[1]=(
unsigned char) value;
6668 return(WriteBlobStream(image,2,buffer));
6696MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6698 assert(image != (Image *) NULL);
6699 assert(image->signature == MagickCoreSignature);
6700 assert(
string != (
const char *) NULL);
6701 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));