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 SyncBlobStream(blob_info,image,image);
2154 *length=image->blob->length;
2155 blob=DetachBlob(image->blob);
2156 if (blob != (
void *) NULL)
2158 if (status == MagickFalse)
2159 blob=RelinquishMagickMemory(blob);
2161 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2164 if ((status == MagickFalse) && (image->blob->extent == 0))
2165 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2171 unique[MagickPathExtent];
2179 file=AcquireUniqueFileResource(unique);
2182 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2183 image_info->filename);
2187 blob_info->file=fdopen(file,
"wb");
2188 if (blob_info->file != (FILE *) NULL)
2190 (void) FormatLocaleString(image->filename,MagickPathExtent,
2191 "%s:%s",image->magick,unique);
2192 status=WriteImage(blob_info,image,exception);
2193 (void) fclose(blob_info->file);
2194 if (status != MagickFalse)
2195 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2197 (void) RelinquishUniqueFileResource(unique);
2200 blob_info=DestroyImageInfo(blob_info);
2232MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2233 ExceptionInfo *exception)
2245 assert(image_info != (
const ImageInfo *) NULL);
2246 assert(image_info->signature == MagickCoreSignature);
2247 assert(image != (Image *) NULL);
2248 assert(image->signature == MagickCoreSignature);
2249 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2250 assert(image_info->custom_stream->signature == MagickCoreSignature);
2251 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2252 assert(exception != (ExceptionInfo *) NULL);
2253 if (IsEventLogging() != MagickFalse)
2254 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2255 image_info->filename);
2256 clone_info=CloneImageInfo(image_info);
2257 clone_info->adjoin=MagickFalse;
2258 (void) SetImageInfo(clone_info,1,exception);
2259 if (*clone_info->magick !=
'\0')
2260 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2261 magick_info=GetMagickInfo(image->magick,exception);
2262 if (magick_info == (
const MagickInfo *) NULL)
2264 (void) ThrowMagickException(exception,GetMagickModule(),
2265 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2267 clone_info=DestroyImageInfo(clone_info);
2270 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2271 blob_support=GetMagickBlobSupport(magick_info);
2272 if ((blob_support != MagickFalse) &&
2273 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2275 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2276 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2277 blob_support=MagickFalse;
2279 if (blob_support != MagickFalse)
2284 (void) CloseBlob(image);
2285 *image->filename=
'\0';
2286 (void) WriteImage(clone_info,image,exception);
2291 unique[MagickPathExtent];
2302 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2303 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2305 if (blob == (
unsigned char *) NULL)
2307 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2308 image_info->filename);
2309 clone_info=DestroyImageInfo(clone_info);
2312 file=AcquireUniqueFileResource(unique);
2315 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2316 image_info->filename);
2317 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2318 clone_info=DestroyImageInfo(clone_info);
2321 clone_info->file=fdopen(file,
"wb+");
2322 if (clone_info->file != (FILE *) NULL)
2327 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2328 image->magick,unique);
2329 status=WriteImage(clone_info,image,exception);
2330 if (status != MagickFalse)
2332 (void) fseek(clone_info->file,0,SEEK_SET);
2333 count=(ssize_t) MagickMaxBufferExtent;
2334 while (count == (ssize_t) MagickMaxBufferExtent)
2336 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2338 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2339 image_info->custom_stream->data);
2342 (void) fclose(clone_info->file);
2344 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2345 (void) RelinquishUniqueFileResource(unique);
2347 clone_info=DestroyImageInfo(clone_info);
2378MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2379 ExceptionInfo *exception)
2403 assert(image != (Image *) NULL);
2404 assert(image->signature == MagickCoreSignature);
2405 assert(image->blob != (BlobInfo *) NULL);
2406 assert(image->blob->type != UndefinedStream);
2407 assert(filename != (
const char *) NULL);
2408 if (IsEventLogging() != MagickFalse)
2409 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2410 if (*filename ==
'\0')
2411 file=AcquireUniqueFileResource(filename);
2413 if (LocaleCompare(filename,
"-") == 0)
2414 file=fileno(stdout);
2416 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2419 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2420 return(MagickFalse);
2422 quantum=(size_t) MagickMaxBufferExtent;
2423 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2424 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2425 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2426 if (buffer == (
unsigned char *) NULL)
2428 file=close_utf8(file)-1;
2429 (void) ThrowMagickException(exception,GetMagickModule(),
2430 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2431 return(MagickFalse);
2434 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2435 for (i=0; count > 0; )
2437 length=(size_t) count;
2438 for (i=0; i < length; i+=(size_t) count)
2440 count=write(file,p+i,(
size_t) (length-i));
2450 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2452 if (LocaleCompare(filename,
"-") != 0)
2453 file=close_utf8(file);
2454 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2455 if ((file == -1) || (i < length))
2458 file=close_utf8(file);
2459 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2460 return(MagickFalse);
2500MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2501 size_t *length,ExceptionInfo *exception)
2515 assert(image_info != (
const ImageInfo *) NULL);
2516 assert(image_info->signature == MagickCoreSignature);
2517 assert(images != (Image *) NULL);
2518 assert(images->signature == MagickCoreSignature);
2519 assert(exception != (ExceptionInfo *) NULL);
2520 if (IsEventLogging() != MagickFalse)
2521 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2522 image_info->filename);
2524 blob=(
unsigned char *) NULL;
2525 blob_info=CloneImageInfo(image_info);
2526 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2528 if (*blob_info->magick !=
'\0')
2529 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2530 magick_info=GetMagickInfo(images->magick,exception);
2531 if (magick_info == (
const MagickInfo *) NULL)
2533 (void) ThrowMagickException(exception,GetMagickModule(),
2534 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2536 blob_info=DestroyImageInfo(blob_info);
2539 if (GetMagickAdjoin(magick_info) == MagickFalse)
2541 blob_info=DestroyImageInfo(blob_info);
2542 return(ImageToBlob(image_info,images,length,exception));
2544 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2545 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2550 blob_info->length=0;
2551 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2552 sizeof(
unsigned char));
2553 if (blob_info->blob == (
void *) NULL)
2554 (void) ThrowMagickException(exception,GetMagickModule(),
2555 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2558 (void) CloseBlob(images);
2559 images->blob->exempt=MagickTrue;
2560 images->blob->extent=0;
2561 *images->filename=
'\0';
2562 status=WriteImages(blob_info,images,images->filename,exception);
2563 SyncBlobStream(blob_info,images,images);
2564 *length=images->blob->length;
2565 blob=DetachBlob(images->blob);
2566 if (blob != (
void *) NULL)
2568 if (status == MagickFalse)
2569 blob=RelinquishMagickMemory(blob);
2571 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2573 else if ((status == MagickFalse) && (images->blob->extent == 0))
2574 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2580 filename[MagickPathExtent],
2581 unique[MagickPathExtent];
2589 file=AcquireUniqueFileResource(unique);
2592 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2593 image_info->filename);
2597 blob_info->file=fdopen(file,
"wb");
2598 if (blob_info->file != (FILE *) NULL)
2600 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2601 images->magick,unique);
2602 status=WriteImages(blob_info,images,filename,exception);
2603 (void) fclose(blob_info->file);
2604 if (status != MagickFalse)
2605 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2607 (void) RelinquishUniqueFileResource(unique);
2610 blob_info=DestroyImageInfo(blob_info);
2642MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2643 Image *images,ExceptionInfo *exception)
2655 assert(image_info != (
const ImageInfo *) NULL);
2656 assert(image_info->signature == MagickCoreSignature);
2657 assert(images != (Image *) NULL);
2658 assert(images->signature == MagickCoreSignature);
2659 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2660 assert(image_info->custom_stream->signature == MagickCoreSignature);
2661 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2662 assert(exception != (ExceptionInfo *) NULL);
2663 if (IsEventLogging() != MagickFalse)
2664 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2665 image_info->filename);
2666 clone_info=CloneImageInfo(image_info);
2667 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2669 if (*clone_info->magick !=
'\0')
2670 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2671 magick_info=GetMagickInfo(images->magick,exception);
2672 if (magick_info == (
const MagickInfo *) NULL)
2674 (void) ThrowMagickException(exception,GetMagickModule(),
2675 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2677 clone_info=DestroyImageInfo(clone_info);
2680 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2681 blob_support=GetMagickBlobSupport(magick_info);
2682 if ((blob_support != MagickFalse) &&
2683 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2685 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2686 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2687 blob_support=MagickFalse;
2689 if (blob_support != MagickFalse)
2694 (void) CloseBlob(images);
2695 *images->filename=
'\0';
2696 (void) WriteImages(clone_info,images,images->filename,exception);
2701 filename[MagickPathExtent],
2702 unique[MagickPathExtent];
2713 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2714 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2716 if (blob == (
unsigned char *) NULL)
2718 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2719 image_info->filename);
2720 clone_info=DestroyImageInfo(clone_info);
2723 file=AcquireUniqueFileResource(unique);
2726 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2727 image_info->filename);
2728 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2729 clone_info=DestroyImageInfo(clone_info);
2732 clone_info->file=fdopen(file,
"wb+");
2733 if (clone_info->file != (FILE *) NULL)
2738 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2739 images->magick,unique);
2740 status=WriteImages(clone_info,images,filename,exception);
2741 if (status != MagickFalse)
2743 (void) fseek(clone_info->file,0,SEEK_SET);
2744 count=(ssize_t) MagickMaxBufferExtent;
2745 while (count == (ssize_t) MagickMaxBufferExtent)
2747 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2749 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2750 image_info->custom_stream->data);
2753 (void) fclose(clone_info->file);
2755 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2756 (void) RelinquishUniqueFileResource(unique);
2758 clone_info=DestroyImageInfo(clone_info);
2794MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2795 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2798 filename[MagickPathExtent];
2827 assert(image_info != (ImageInfo *) NULL);
2828 assert(image_info->signature == MagickCoreSignature);
2829 assert(image != (Image *) NULL);
2830 assert(image->signature == MagickCoreSignature);
2831 assert(inject_image != (Image *) NULL);
2832 assert(inject_image->signature == MagickCoreSignature);
2833 assert(exception != (ExceptionInfo *) NULL);
2834 if (IsEventLogging() != MagickFalse)
2835 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2836 unique_file=(FILE *) NULL;
2837 file=AcquireUniqueFileResource(filename);
2839 unique_file=fdopen(file,
"wb");
2840 if ((file == -1) || (unique_file == (FILE *) NULL))
2842 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2843 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2845 return(MagickFalse);
2847 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2848 if (byte_image == (Image *) NULL)
2850 (void) fclose(unique_file);
2851 (void) RelinquishUniqueFileResource(filename);
2852 return(MagickFalse);
2854 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2856 DestroyBlob(byte_image);
2857 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2858 write_info=CloneImageInfo(image_info);
2859 SetImageInfoFile(write_info,unique_file);
2860 status=WriteImage(write_info,byte_image,exception);
2861 write_info=DestroyImageInfo(write_info);
2862 byte_image=DestroyImage(byte_image);
2863 (void) fclose(unique_file);
2864 if (status == MagickFalse)
2866 (void) RelinquishUniqueFileResource(filename);
2867 return(MagickFalse);
2872 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2875 (void) RelinquishUniqueFileResource(filename);
2876 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2877 image_info->filename);
2878 return(MagickFalse);
2880 quantum=(size_t) MagickMaxBufferExtent;
2881 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2882 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2883 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2884 if (buffer == (
unsigned char *) NULL)
2886 (void) RelinquishUniqueFileResource(filename);
2887 file=close_utf8(file);
2888 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2893 ssize_t count = read(file,buffer,quantum);
2900 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2903 file=close_utf8(file);
2905 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2906 (void) RelinquishUniqueFileResource(filename);
2907 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2933MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2935 assert(image != (
const Image *) NULL);
2936 assert(image->signature == MagickCoreSignature);
2937 if (IsEventLogging() != MagickFalse)
2938 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2939 return(image->blob->exempt);
2964MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2967 *magick_restrict blob_info;
2969 assert(image != (
const Image *) NULL);
2970 assert(image->signature == MagickCoreSignature);
2971 if (IsEventLogging() != MagickFalse)
2972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2973 blob_info=image->blob;
2974 switch (blob_info->type)
2983 if (blob_info->file_info.file == (FILE *) NULL)
2984 return(MagickFalse);
2985 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2986 return(status == -1 ? MagickFalse : MagickTrue);
2990#if defined(MAGICKCORE_ZLIB_DELEGATE)
2994 if (blob_info->file_info.gzfile == (gzFile) NULL)
2995 return(MagickFalse);
2996 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2997 return(offset < 0 ? MagickFalse : MagickTrue);
3002 case UndefinedStream:
3006 case StandardStream:
3010 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3011 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3018 return(MagickFalse);
3043MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3045 assert(image != (
const Image *) NULL);
3046 assert(image->signature == MagickCoreSignature);
3047 if (IsEventLogging() != MagickFalse)
3048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3049 return(image->blob->temporary);
3081MagickExport
void *MapBlob(
int file,
const MapMode mode,
3082 const MagickOffsetType offset,
const size_t length)
3084#if defined(MAGICKCORE_HAVE_MMAP)
3097#if defined(MAP_ANONYMOUS)
3098 flags|=MAP_ANONYMOUS;
3107 protection=PROT_READ;
3113 protection=PROT_WRITE;
3119 protection=PROT_READ | PROT_WRITE;
3124#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3125 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3127 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3128 if (map == MAP_FAILED)
3129 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3131 if (map == MAP_FAILED)
3168MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3177 assert(buffer != (
unsigned char *) NULL);
3184 *buffer++=(
unsigned char) c;
3188 *buffer++=(
unsigned char) c;
3218MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3226 assert(p != (
unsigned char *) NULL);
3233 *p++=(
unsigned char) c;
3269static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3270 const BlobInfo *blob_info)
3281 size=MagickMinBufferExtent;
3282 option=GetImageOption(image_info,
"stream:buffer-size");
3283 if (option != (
const char *) NULL)
3284 size=StringToUnsignedLong(option);
3285 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3286 _IONBF : _IOFBF,size);
3287 return(status == 0 ? MagickTrue : MagickFalse);
3290#if defined(MAGICKCORE_ZLIB_DELEGATE)
3291static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3293#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3294 return(gzopen(path,mode));
3302 path_wide=NTCreateWidePath(path);
3303 if (path_wide == (
wchar_t *) NULL)
3304 return((gzFile) NULL);
3305 file=gzopen_w(path_wide,mode);
3306 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3312MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3313 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3316 *magick_restrict blob_info;
3319 extension[MagickPathExtent],
3320 filename[MagickPathExtent];
3334 assert(image_info != (ImageInfo *) NULL);
3335 assert(image_info->signature == MagickCoreSignature);
3336 assert(image != (Image *) NULL);
3337 assert(image->signature == MagickCoreSignature);
3338 if (IsEventLogging() != MagickFalse)
3339 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3340 image_info->filename);
3341 blob_info=image->blob;
3342 if (image_info->blob != (
void *) NULL)
3344 if (image_info->stream != (StreamHandler) NULL)
3345 blob_info->stream=(StreamHandler) image_info->stream;
3346 AttachBlob(blob_info,image_info->blob,image_info->length);
3349 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3350 (*image->filename ==
'\0'))
3352 blob_info->type=CustomStream;
3353 blob_info->custom_stream=image_info->custom_stream;
3356 (void) DetachBlob(blob_info);
3357 blob_info->mode=mode;
3364 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3367 case ReadBinaryBlobMode:
3369 flags=O_RDONLY | O_BINARY;
3371 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3376 flags=O_WRONLY | O_CREAT | O_TRUNC;
3378 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
3381 case WriteBinaryBlobMode:
3383 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3385 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
3386 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
3387 MagickTrue : MagickFalse;
3390 case AppendBlobMode:
3392 flags=O_WRONLY | O_CREAT | O_APPEND;
3394 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
3397 case AppendBinaryBlobMode:
3399 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3401 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
3402 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
3403 MagickTrue : MagickFalse;
3410 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3414#if defined(O_NOFOLLOW)
3415 if (status == MagickFalse)
3419 blob_info->synchronize=image_info->synchronize;
3420 if (image_info->stream != (StreamHandler) NULL)
3422 blob_info->stream=image_info->stream;
3425 blob_info->type=FifoStream;
3433 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3434 rights=ReadPolicyRights;
3436 rights=WritePolicyRights;
3437 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3440 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3441 "NotAuthorized",
"`%s'",filename);
3442 return(MagickFalse);
3444 if ((LocaleCompare(filename,
"-") == 0) ||
3445 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3447 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3448#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3449 if (strchr(type,
'b') != (
char *) NULL)
3450 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3452 blob_info->type=StandardStream;
3453 blob_info->exempt=MagickTrue;
3454 return(SetStreamBuffering(image_info,blob_info));
3456 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3457 (IsGeometry(filename+3) != MagickFalse))
3464 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3465 if (blob_info->file_info.file == (FILE *) NULL)
3467 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3468 return(MagickFalse);
3470#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3471 if (strchr(type,
'b') != (
char *) NULL)
3472 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3474 blob_info->type=FileStream;
3475 blob_info->exempt=MagickTrue;
3476 return(SetStreamBuffering(image_info,blob_info));
3478#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3479 if (*filename ==
'|')
3482 fileMode[MagickPathExtent],
3490 (void) signal(SIGPIPE,SIG_IGN);
3494 sanitize_command=SanitizeString(filename+1);
3495 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3496 sanitize_command=DestroyString(sanitize_command);
3497 if (blob_info->file_info.file == (FILE *) NULL)
3499 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3500 return(MagickFalse);
3502 blob_info->type=PipeStream;
3503 blob_info->exempt=MagickTrue;
3504 return(SetStreamBuffering(image_info,blob_info));
3507 status=GetPathAttributes(filename,&blob_info->properties);
3508#if defined(S_ISFIFO)
3509 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3511 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3512 if (blob_info->file_info.file == (FILE *) NULL)
3514 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3515 return(MagickFalse);
3517 blob_info->type=FileStream;
3518 blob_info->exempt=MagickTrue;
3519 return(SetStreamBuffering(image_info,blob_info));
3522 GetPathComponent(image->filename,ExtensionPath,extension);
3525 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3526 if ((image_info->adjoin == MagickFalse) ||
3527 (strchr(filename,
'%') != (
char *) NULL))
3532 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3533 image->scene,filename,exception);
3534 if ((LocaleCompare(filename,image->filename) == 0) &&
3535 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3536 (GetNextImageInList(image) != (Image *) NULL)))
3539 path[MagickPathExtent];
3541 GetPathComponent(image->filename,RootPath,path);
3542 if (*extension ==
'\0')
3543 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3544 path,(
double) image->scene);
3546 (
void) FormatLocaleString(filename,MagickPathExtent,
3547 "%s-%.20g.%s",path,(
double) image->scene,extension);
3549 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3552 if (image_info->file != (FILE *) NULL)
3554 blob_info->file_info.file=image_info->file;
3555 blob_info->type=FileStream;
3556 blob_info->exempt=MagickTrue;
3564 blob_info->file_info.file=(FILE *) NULL;
3565 file=open_utf8(filename,flags,0);
3567 blob_info->file_info.file=fdopen(file,type);
3568 if (blob_info->file_info.file != (FILE *) NULL)
3576 blob_info->type=FileStream;
3577 (void) SetStreamBuffering(image_info,blob_info);
3578 (void) memset(magick,0,
sizeof(magick));
3579 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3580 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3581#if defined(MAGICKCORE_POSIX_SUPPORT)
3582 (void) fflush(blob_info->file_info.file);
3584 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3585 " read %.20g magic header bytes",(
double) count);
3586#if defined(MAGICKCORE_ZLIB_DELEGATE)
3587 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3588 ((
int) magick[2] == 0x08))
3591 gzfile = gzopen_utf8(filename,
"rb");
3593 if (gzfile != (gzFile) NULL)
3595 if (blob_info->file_info.file != (FILE *) NULL)
3596 (void) fclose(blob_info->file_info.file);
3597 blob_info->file_info.file=(FILE *) NULL;
3598 blob_info->file_info.gzfile=gzfile;
3599 blob_info->type=ZipStream;
3603#if defined(MAGICKCORE_BZLIB_DELEGATE)
3604 if (strncmp((
char *) magick,
"BZh",3) == 0)
3607 *bzfile = BZ2_bzopen(filename,
"r");
3609 if (bzfile != (BZFILE *) NULL)
3611 if (blob_info->file_info.file != (FILE *) NULL)
3612 (void) fclose(blob_info->file_info.file);
3613 blob_info->file_info.file=(FILE *) NULL;
3614 blob_info->file_info.bzfile=bzfile;
3615 blob_info->type=BZipStream;
3619 if (blob_info->type == FileStream)
3630 sans_exception=AcquireExceptionInfo();
3631 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3632 sans_exception=DestroyExceptionInfo(sans_exception);
3633 length=(size_t) blob_info->properties.st_size;
3634 if ((magick_info != (
const MagickInfo *) NULL) &&
3635 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3636 (length > MagickMaxBufferExtent) &&
3637 (AcquireMagickResource(MapResource,length) != MagickFalse))
3642 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3644 if (blob == (
void *) NULL)
3645 RelinquishMagickResource(MapResource,length);
3651 if (image_info->file != (FILE *) NULL)
3652 blob_info->exempt=MagickFalse;
3655 (void) fclose(blob_info->file_info.file);
3656 blob_info->file_info.file=(FILE *) NULL;
3658 AttachBlob(blob_info,blob,length);
3659 blob_info->mapped=MagickTrue;
3666#if defined(MAGICKCORE_ZLIB_DELEGATE)
3667 if ((LocaleCompare(extension,
"gz") == 0) ||
3668 (LocaleCompare(extension,
"wmz") == 0) ||
3669 (LocaleCompare(extension,
"svgz") == 0))
3671 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3672 if (blob_info->file_info.gzfile != (gzFile) NULL)
3673 blob_info->type=ZipStream;
3677#if defined(MAGICKCORE_BZLIB_DELEGATE)
3678 if (LocaleCompare(extension,
"bz2") == 0)
3680 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3681 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3682 blob_info->type=BZipStream;
3690 blob_info->file_info.file=(FILE *) NULL;
3691 file=open_utf8(filename,flags,P_MODE);
3693 blob_info->file_info.file=fdopen(file,type);
3694 if (blob_info->file_info.file != (FILE *) NULL)
3696 blob_info->type=FileStream;
3697 (void) SetStreamBuffering(image_info,blob_info);
3700 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3703 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3704 "NotAuthorized",
"`%s'",filename);
3705 return(MagickFalse);
3707 blob_info->status=0;
3708 blob_info->error_number=0;
3709 if (blob_info->type != UndefinedStream)
3710 blob_info->size=GetBlobSize(image);
3713 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3714 return(MagickFalse);
3753#if defined(__cplusplus) || defined(c_plusplus)
3757static size_t PingStream(
const Image *magick_unused(image),
3758 const void *magick_unused(pixels),
const size_t columns)
3760 magick_unreferenced(image);
3761 magick_unreferenced(pixels);
3765#if defined(__cplusplus) || defined(c_plusplus)
3769MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3770 const size_t length,ExceptionInfo *exception)
3785 assert(image_info != (ImageInfo *) NULL);
3786 assert(image_info->signature == MagickCoreSignature);
3787 assert(exception != (ExceptionInfo *) NULL);
3788 if (IsEventLogging() != MagickFalse)
3789 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3790 image_info->filename);
3791 if ((blob == (
const void *) NULL) || (length == 0))
3793 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3794 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3795 return((Image *) NULL);
3797 ping_info=CloneImageInfo(image_info);
3798 ping_info->blob=(
void *) blob;
3799 ping_info->length=length;
3800 ping_info->ping=MagickTrue;
3801 if (*ping_info->magick ==
'\0')
3802 (void) SetImageInfo(ping_info,0,exception);
3803 magick_info=GetMagickInfo(ping_info->magick,exception);
3804 if (magick_info == (
const MagickInfo *) NULL)
3806 (void) ThrowMagickException(exception,GetMagickModule(),
3807 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3809 ping_info=DestroyImageInfo(ping_info);
3810 return((Image *) NULL);
3812 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3815 filename[MagickPathExtent];
3820 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3821 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3822 ping_info->magick,filename);
3823 image=ReadStream(ping_info,&PingStream,exception);
3824 if (image != (Image *) NULL)
3825 (void) DetachBlob(image->blob);
3826 ping_info=DestroyImageInfo(ping_info);
3832 ping_info->blob=(
void *) NULL;
3833 ping_info->length=0;
3834 *ping_info->filename=
'\0';
3835 status=BlobToFile(ping_info->filename,blob,length,exception);
3836 if (status == MagickFalse)
3838 (void) RelinquishUniqueFileResource(ping_info->filename);
3839 ping_info=DestroyImageInfo(ping_info);
3840 return((Image *) NULL);
3842 clone_info=CloneImageInfo(ping_info);
3843 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3844 ping_info->magick,ping_info->filename);
3845 image=ReadStream(clone_info,&PingStream,exception);
3846 if (image != (Image *) NULL)
3854 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3856 (void) CopyMagickString(images->filename,image_info->filename,
3858 (void) CopyMagickString(images->magick_filename,image_info->filename,
3860 (void) CopyMagickString(images->magick,magick_info->name,
3862 images=GetNextImageInList(images);
3865 clone_info=DestroyImageInfo(clone_info);
3866 (void) RelinquishUniqueFileResource(ping_info->filename);
3867 ping_info=DestroyImageInfo(ping_info);
3902MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3905 *magick_restrict blob_info;
3916 assert(image != (Image *) NULL);
3917 assert(image->signature == MagickCoreSignature);
3918 assert(image->blob != (BlobInfo *) NULL);
3919 assert(image->blob->type != UndefinedStream);
3922 assert(data != (
void *) NULL);
3923 blob_info=image->blob;
3925 q=(
unsigned char *) data;
3926 switch (blob_info->type)
3928 case UndefinedStream:
3930 case StandardStream:
3938 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3943 c=getc(blob_info->file_info.file);
3946 *q++=(
unsigned char) c;
3952 c=getc(blob_info->file_info.file);
3955 *q++=(
unsigned char) c;
3961 c=getc(blob_info->file_info.file);
3964 *q++=(
unsigned char) c;
3970 c=getc(blob_info->file_info.file);
3973 *q++=(
unsigned char) c;
3980 if ((count != (ssize_t) length) &&
3981 (ferror(blob_info->file_info.file) != 0))
3982 ThrowBlobException(blob_info);
3987#if defined(MAGICKCORE_ZLIB_DELEGATE)
3998 for (i=0; i < length; i+=(size_t) count)
4000 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
4001 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4014 c=gzgetc(blob_info->file_info.gzfile);
4017 *q++=(
unsigned char) c;
4023 c=gzgetc(blob_info->file_info.gzfile);
4026 *q++=(
unsigned char) c;
4032 c=gzgetc(blob_info->file_info.gzfile);
4035 *q++=(
unsigned char) c;
4041 c=gzgetc(blob_info->file_info.gzfile);
4044 *q++=(
unsigned char) c;
4051 (void) gzerror(blob_info->file_info.gzfile,&status);
4052 if ((count != (ssize_t) length) && (status != Z_OK))
4053 ThrowBlobException(blob_info);
4054 if (blob_info->eof == MagickFalse)
4055 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4062#if defined(MAGICKCORE_BZLIB_DELEGATE)
4069 for (i=0; i < length; i+=(size_t) count)
4071 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4072 MagickMin(length-i,MagickMaxBufferExtent));
4082 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4083 if ((count != (ssize_t) length) && (status != BZ_OK))
4084 ThrowBlobException(blob_info);
4095 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4097 blob_info->eof=MagickTrue;
4100 p=blob_info->data+blob_info->offset;
4101 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4102 blob_info->length-blob_info->offset);
4103 blob_info->offset+=count;
4104 if (count != (ssize_t) length)
4105 blob_info->eof=MagickTrue;
4106 (void) memcpy(q,p,(
size_t) count);
4111 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4112 count=blob_info->custom_stream->reader(q,length,
4113 blob_info->custom_stream->data);
4142MagickExport
int ReadBlobByte(Image *image)
4145 *magick_restrict blob_info;
4150 assert(image != (Image *) NULL);
4151 assert(image->signature == MagickCoreSignature);
4152 assert(image->blob != (BlobInfo *) NULL);
4153 assert(image->blob->type != UndefinedStream);
4154 blob_info=image->blob;
4155 switch (blob_info->type)
4157 case StandardStream:
4161 c=getc(blob_info->file_info.file);
4164 if (ferror(blob_info->file_info.file) != 0)
4165 ThrowBlobException(blob_info);
4172 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4174 blob_info->eof=MagickTrue;
4177 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4178 blob_info->offset++;
4189 count=ReadBlob(image,1,buffer);
4222MagickExport
double ReadBlobDouble(Image *image)
4233 quantum.double_value=0.0;
4234 quantum.unsigned_value=ReadBlobLongLong(image);
4235 return(quantum.double_value);
4261MagickExport
float ReadBlobFloat(Image *image)
4272 quantum.float_value=0.0;
4273 quantum.unsigned_value=ReadBlobLong(image);
4274 return(quantum.float_value);
4300MagickExport
unsigned int ReadBlobLong(Image *image)
4314 assert(image != (Image *) NULL);
4315 assert(image->signature == MagickCoreSignature);
4317 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4320 if (image->endian == LSBEndian)
4322 value=(
unsigned int) (*p++);
4323 value|=(
unsigned int) (*p++) << 8;
4324 value|=(
unsigned int) (*p++) << 16;
4325 value|=(
unsigned int) (*p++) << 24;
4328 value=(
unsigned int) (*p++) << 24;
4329 value|=(
unsigned int) (*p++) << 16;
4330 value|=(
unsigned int) (*p++) << 8;
4331 value|=(
unsigned int) (*p++);
4358MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4372 assert(image != (Image *) NULL);
4373 assert(image->signature == MagickCoreSignature);
4375 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4377 return(MagickULLConstant(0));
4378 if (image->endian == LSBEndian)
4380 value=(MagickSizeType) (*p++);
4381 value|=(MagickSizeType) (*p++) << 8;
4382 value|=(MagickSizeType) (*p++) << 16;
4383 value|=(MagickSizeType) (*p++) << 24;
4384 value|=(MagickSizeType) (*p++) << 32;
4385 value|=(MagickSizeType) (*p++) << 40;
4386 value|=(MagickSizeType) (*p++) << 48;
4387 value|=(MagickSizeType) (*p++) << 56;
4390 value=(MagickSizeType) (*p++) << 56;
4391 value|=(MagickSizeType) (*p++) << 48;
4392 value|=(MagickSizeType) (*p++) << 40;
4393 value|=(MagickSizeType) (*p++) << 32;
4394 value|=(MagickSizeType) (*p++) << 24;
4395 value|=(MagickSizeType) (*p++) << 16;
4396 value|=(MagickSizeType) (*p++) << 8;
4397 value|=(MagickSizeType) (*p++);
4424MagickExport
unsigned short ReadBlobShort(Image *image)
4438 assert(image != (Image *) NULL);
4439 assert(image->signature == MagickCoreSignature);
4441 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4443 return((
unsigned short) 0U);
4444 if (image->endian == LSBEndian)
4446 value=(
unsigned short) (*p++);
4447 value|=(
unsigned short) (*p++) << 8;
4450 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4451 value|=(
unsigned short) (*p++);
4478MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4492 assert(image != (Image *) NULL);
4493 assert(image->signature == MagickCoreSignature);
4495 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4498 value=(
unsigned int) (*p++);
4499 value|=(
unsigned int) (*p++) << 8;
4500 value|=(
unsigned int) (*p++) << 16;
4501 value|=(
unsigned int) (*p++) << 24;
4528MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4539 quantum.unsigned_value=ReadBlobLSBLong(image);
4540 return(quantum.signed_value);
4566MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4580 assert(image != (Image *) NULL);
4581 assert(image->signature == MagickCoreSignature);
4583 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4585 return((
unsigned short) 0U);
4586 value=(
unsigned short) (*p++);
4587 value|=(
unsigned short) (*p++) << 8;
4614MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4625 quantum.unsigned_value=ReadBlobLSBShort(image);
4626 return(quantum.signed_value);
4652MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4666 assert(image != (Image *) NULL);
4667 assert(image->signature == MagickCoreSignature);
4669 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4672 value=(
unsigned int) (*p++) << 24;
4673 value|=(
unsigned int) (*p++) << 16;
4674 value|=(
unsigned int) (*p++) << 8;
4675 value|=(
unsigned int) (*p++);
4702MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4716 assert(image != (Image *) NULL);
4717 assert(image->signature == MagickCoreSignature);
4719 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4721 return(MagickULLConstant(0));
4722 value=(MagickSizeType) (*p++) << 56;
4723 value|=(MagickSizeType) (*p++) << 48;
4724 value|=(MagickSizeType) (*p++) << 40;
4725 value|=(MagickSizeType) (*p++) << 32;
4726 value|=(MagickSizeType) (*p++) << 24;
4727 value|=(MagickSizeType) (*p++) << 16;
4728 value|=(MagickSizeType) (*p++) << 8;
4729 value|=(MagickSizeType) (*p++);
4756MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4770 assert(image != (Image *) NULL);
4771 assert(image->signature == MagickCoreSignature);
4773 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4775 return((
unsigned short) 0U);
4776 value=(
unsigned short) ((*p++) << 8);
4777 value|=(
unsigned short) (*p++);
4778 return((
unsigned short) (value & 0xffff));
4804MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4815 quantum.unsigned_value=ReadBlobMSBLong(image);
4816 return(quantum.signed_value);
4842MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4853 quantum.unsigned_value=ReadBlobMSBShort(image);
4854 return(quantum.signed_value);
4880MagickExport
signed int ReadBlobSignedLong(Image *image)
4891 quantum.unsigned_value=ReadBlobLong(image);
4892 return(quantum.signed_value);
4918MagickExport
signed short ReadBlobSignedShort(Image *image)
4929 quantum.unsigned_value=ReadBlobShort(image);
4930 return(quantum.signed_value);
4968MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4969 const size_t length,
void *magick_restrict data,ssize_t *count)
4972 *magick_restrict blob_info;
4974 assert(image != (Image *) NULL);
4975 assert(image->signature == MagickCoreSignature);
4976 assert(image->blob != (BlobInfo *) NULL);
4977 assert(image->blob->type != UndefinedStream);
4978 assert(count != (ssize_t *) NULL);
4979 blob_info=image->blob;
4980 if (blob_info->type != BlobStream)
4982 assert(data != NULL);
4983 *count=ReadBlob(image,length,(
unsigned char *) data);
4986 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4989 blob_info->eof=MagickTrue;
4992 data=blob_info->data+blob_info->offset;
4993 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4994 blob_info->length-blob_info->offset);
4995 blob_info->offset+=(*count);
4996 if (*count != (ssize_t) length)
4997 blob_info->eof=MagickTrue;
5026MagickExport
char *ReadBlobString(Image *image,
char *
string)
5029 *magick_restrict blob_info;
5037 assert(image != (Image *) NULL);
5038 assert(image->signature == MagickCoreSignature);
5039 assert(image->blob != (BlobInfo *) NULL);
5040 assert(image->blob->type != UndefinedStream);
5041 if (IsEventLogging() != MagickFalse)
5042 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5044 blob_info=image->blob;
5045 switch (blob_info->type)
5047 case UndefinedStream:
5049 case StandardStream:
5052 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5053 if (p == (
char *) NULL)
5055 if (ferror(blob_info->file_info.file) != 0)
5056 ThrowBlobException(blob_info);
5057 return((
char *) NULL);
5064#if defined(MAGICKCORE_ZLIB_DELEGATE)
5065 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5066 if (p == (
char *) NULL)
5069 (void) gzerror(blob_info->file_info.gzfile,&status);
5071 ThrowBlobException(blob_info);
5072 return((
char *) NULL);
5082 c=ReadBlobByte(image);
5085 blob_info->eof=MagickTrue;
5088 string[i++]=(char) c;
5091 }
while (i < (MaxTextExtent-2));
5099 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5102 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5104 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5105 return((
char *) NULL);
5132MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5134 assert(blob != (BlobInfo *) NULL);
5135 assert(blob->signature == MagickCoreSignature);
5136 if (IsEventLogging() != MagickFalse)
5137 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5138 LockSemaphoreInfo(blob->semaphore);
5139 blob->reference_count++;
5140 UnlockSemaphoreInfo(blob->semaphore);
5177MagickExport MagickOffsetType SeekBlob(Image *image,
5178 const MagickOffsetType offset,
const int whence)
5181 *magick_restrict blob_info;
5183 assert(image != (Image *) NULL);
5184 assert(image->signature == MagickCoreSignature);
5185 assert(image->blob != (BlobInfo *) NULL);
5186 assert(image->blob->type != UndefinedStream);
5187 if (IsEventLogging() != MagickFalse)
5188 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5189 blob_info=image->blob;
5190 switch (blob_info->type)
5192 case UndefinedStream:
5194 case StandardStream:
5199 if ((offset < 0) && (whence == SEEK_SET))
5201 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5203 blob_info->offset=TellBlob(image);
5208#if defined(MAGICKCORE_ZLIB_DELEGATE)
5209 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5212 blob_info->offset=TellBlob(image);
5228 blob_info->offset=offset;
5233 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5234 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5239 if ((blob_info->offset+offset) < 0)
5241 blob_info->offset+=offset;
5246 if (((MagickOffsetType) blob_info->length+offset) < 0)
5248 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5252 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5254 blob_info->eof=MagickFalse;
5261 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5263 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5264 blob_info->custom_stream->data);
5268 return(blob_info->offset);
5296MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5298 assert(image != (
const Image *) NULL);
5299 assert(image->signature == MagickCoreSignature);
5300 if (IsEventLogging() != MagickFalse)
5301 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5302 image->blob->exempt=exempt;
5331MagickExport MagickBooleanType SetBlobExtent(Image *image,
5332 const MagickSizeType extent)
5335 *magick_restrict blob_info;
5337 assert(image != (Image *) NULL);
5338 assert(image->signature == MagickCoreSignature);
5339 assert(image->blob != (BlobInfo *) NULL);
5340 assert(image->blob->type != UndefinedStream);
5341 if (IsEventLogging() != MagickFalse)
5342 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5343 blob_info=image->blob;
5344 switch (blob_info->type)
5346 case UndefinedStream:
5348 case StandardStream:
5349 return(MagickFalse);
5358 if (extent != (MagickSizeType) ((off_t) extent))
5359 return(MagickFalse);
5360 offset=SeekBlob(image,0,SEEK_END);
5362 return(MagickFalse);
5363 if ((MagickSizeType) offset >= extent)
5365 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5368 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5369 blob_info->file_info.file);
5370#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5371 if (blob_info->synchronize != MagickFalse)
5376 file=fileno(blob_info->file_info.file);
5377 if ((file == -1) || (offset < 0))
5378 return(MagickFalse);
5379 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5382 offset=SeekBlob(image,offset,SEEK_SET);
5384 return(MagickFalse);
5389 return(MagickFalse);
5391 return(MagickFalse);
5393 return(MagickFalse);
5396 if (extent != (MagickSizeType) ((
size_t) extent))
5397 return(MagickFalse);
5398 if (blob_info->mapped != MagickFalse)
5406 (void) UnmapBlob(blob_info->data,blob_info->length);
5407 RelinquishMagickResource(MapResource,blob_info->length);
5408 if (extent != (MagickSizeType) ((off_t) extent))
5409 return(MagickFalse);
5410 offset=SeekBlob(image,0,SEEK_END);
5412 return(MagickFalse);
5413 if ((MagickSizeType) offset >= extent)
5415 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5416 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5417 blob_info->file_info.file);
5418#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5419 if (blob_info->synchronize != MagickFalse)
5424 file=fileno(blob_info->file_info.file);
5425 if ((file == -1) || (offset < 0))
5426 return(MagickFalse);
5427 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5431 offset=SeekBlob(image,offset,SEEK_SET);
5433 return(MagickFalse);
5434 (void) AcquireMagickResource(MapResource,extent);
5435 blob_info->data=(
unsigned char*) MapBlob(fileno(
5436 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5437 blob_info->extent=(size_t) extent;
5438 blob_info->length=(size_t) extent;
5439 (void) SyncBlob(image);
5442 blob_info->extent=(size_t) extent;
5443 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5444 blob_info->extent+1,
sizeof(*blob_info->data));
5445 (void) SyncBlob(image);
5446 if (blob_info->data == (
unsigned char *) NULL)
5448 (void) DetachBlob(blob_info);
5449 return(MagickFalse);
5483MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5486 assert(custom_stream != (CustomStreamInfo *) NULL);
5487 assert(custom_stream->signature == MagickCoreSignature);
5488 custom_stream->data=data;
5516MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5517 CustomStreamHandler reader)
5519 assert(custom_stream != (CustomStreamInfo *) NULL);
5520 assert(custom_stream->signature == MagickCoreSignature);
5521 custom_stream->reader=reader;
5549MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5550 CustomStreamSeeker seeker)
5552 assert(custom_stream != (CustomStreamInfo *) NULL);
5553 assert(custom_stream->signature == MagickCoreSignature);
5554 custom_stream->seeker=seeker;
5582MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5583 CustomStreamTeller teller)
5585 assert(custom_stream != (CustomStreamInfo *) NULL);
5586 assert(custom_stream->signature == MagickCoreSignature);
5587 custom_stream->teller=teller;
5615MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5616 CustomStreamHandler writer)
5618 assert(custom_stream != (CustomStreamInfo *) NULL);
5619 assert(custom_stream->signature == MagickCoreSignature);
5620 custom_stream->writer=writer;
5647static int SyncBlob(
const Image *image)
5650 *magick_restrict blob_info;
5655 assert(image != (Image *) NULL);
5656 assert(image->signature == MagickCoreSignature);
5657 assert(image->blob != (BlobInfo *) NULL);
5658 if (IsEventLogging() != MagickFalse)
5659 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5660 if (EOFBlob(image) != 0)
5662 blob_info=image->blob;
5664 switch (blob_info->type)
5666 case UndefinedStream:
5667 case StandardStream:
5672 status=fflush(blob_info->file_info.file);
5677#if defined(MAGICKCORE_ZLIB_DELEGATE)
5678 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5684#if defined(MAGICKCORE_BZLIB_DELEGATE)
5685 status=BZ2_bzflush(blob_info->file_info.bzfile);
5730MagickPrivate
void SyncBlobStream(ImageInfo *image_info,Image *image,
5733 assert(owner != (Image *) NULL);
5734 assert(image != (Image *) NULL);
5735 assert(image->signature == MagickCoreSignature);
5736 if (image->blob == (BlobInfo *) NULL)
5738 if (image->blob->data == (
unsigned char *) NULL)
5740 if (image_info != (ImageInfo *) NULL)
5742 image_info->blob=(
void *) image->blob->data;
5743 image_info->length=image->blob->length;
5745 if ((image == owner) || (owner->blob == (BlobInfo *) NULL))
5747 if (owner->blob == (BlobInfo *) NULL)
5753 owner->blob->data=image->blob->data;
5754 owner->blob->extent=image->blob->extent;
5755 owner->blob->length=image->blob->length;
5756 owner->blob->offset=image->blob->offset;
5757 owner->blob->quantum=image->blob->quantum;
5758 owner->blob->type=BlobStream;
5759 owner->blob->exempt=MagickTrue;
5760 (void) DetachBlob(image->blob);
5785MagickExport MagickOffsetType TellBlob(
const Image *image)
5788 *magick_restrict blob_info;
5793 assert(image != (Image *) NULL);
5794 assert(image->signature == MagickCoreSignature);
5795 assert(image->blob != (BlobInfo *) NULL);
5796 assert(image->blob->type != UndefinedStream);
5797 if (IsEventLogging() != MagickFalse)
5798 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5799 blob_info=image->blob;
5801 switch (blob_info->type)
5803 case UndefinedStream:
5804 case StandardStream:
5808 offset=ftell(blob_info->file_info.file);
5815#if defined(MAGICKCORE_ZLIB_DELEGATE)
5816 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5826 offset=blob_info->offset;
5831 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5832 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5864MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5866#if defined(MAGICKCORE_HAVE_MMAP)
5870 status=munmap(map,length);
5871 return(status == -1 ? MagickFalse : MagickTrue);
5875 return(MagickFalse);
5907MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5911 *magick_restrict blob_info;
5925 assert(image != (Image *) NULL);
5926 assert(image->signature == MagickCoreSignature);
5927 assert(image->blob != (BlobInfo *) NULL);
5928 assert(image->blob->type != UndefinedStream);
5931 assert(data != (
const void *) NULL);
5932 blob_info=image->blob;
5934 p=(
const unsigned char *) data;
5935 q=(
unsigned char *) data;
5936 switch (blob_info->type)
5938 case UndefinedStream:
5940 case StandardStream:
5948 count=(ssize_t) fwrite((
const char *) data,1,length,
5949 blob_info->file_info.file);
5954 c=putc((
int) *p++,blob_info->file_info.file);
5962 c=putc((
int) *p++,blob_info->file_info.file);
5970 c=putc((
int) *p++,blob_info->file_info.file);
5978 c=putc((
int) *p++,blob_info->file_info.file);
5987 if ((count != (ssize_t) length) &&
5988 (ferror(blob_info->file_info.file) != 0))
5989 ThrowBlobException(blob_info);
5994#if defined(MAGICKCORE_ZLIB_DELEGATE)
6005 for (i=0; i < length; i+=(size_t) count)
6007 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
6008 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
6021 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6029 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6037 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6045 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6055 (void) gzerror(blob_info->file_info.gzfile,&status);
6056 if ((count != (ssize_t) length) && (status != Z_OK))
6057 ThrowBlobException(blob_info);
6063#if defined(MAGICKCORE_BZLIB_DELEGATE)
6070 for (i=0; i < length; i+=(size_t) count)
6072 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6073 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6083 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6084 if ((count != (ssize_t) length) && (status != BZ_OK))
6085 ThrowBlobException(blob_info);
6091 count=(ssize_t) blob_info->stream(image,data,length);
6099 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6104 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6105 if (extent >= blob_info->extent)
6107 extent+=blob_info->quantum+length;
6108 blob_info->quantum<<=1;
6109 if (SetBlobExtent(image,extent) == MagickFalse)
6112 q=blob_info->data+blob_info->offset;
6113 (void) memcpy(q,p,length);
6114 blob_info->offset+=(MagickOffsetType) length;
6115 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6116 blob_info->length=(size_t) blob_info->offset;
6117 count=(ssize_t) length;
6122 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6123 count=blob_info->custom_stream->writer((
unsigned char *) data,
6124 length,blob_info->custom_stream->data);
6156MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6159 *magick_restrict blob_info;
6164 assert(image != (Image *) NULL);
6165 assert(image->signature == MagickCoreSignature);
6166 assert(image->blob != (BlobInfo *) NULL);
6167 assert(image->blob->type != UndefinedStream);
6168 blob_info=image->blob;
6170 switch (blob_info->type)
6172 case StandardStream:
6179 c=putc((
int) value,blob_info->file_info.file);
6182 if (ferror(blob_info->file_info.file) != 0)
6183 ThrowBlobException(blob_info);
6191 count=WriteBlobStream(image,1,&value);
6223MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6234 quantum.unsigned_value=0U;
6235 quantum.float_value=value;
6236 return(WriteBlobLong(image,quantum.unsigned_value));
6264MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6269 assert(image != (Image *) NULL);
6270 assert(image->signature == MagickCoreSignature);
6271 if (image->endian == LSBEndian)
6273 buffer[0]=(
unsigned char) value;
6274 buffer[1]=(
unsigned char) (value >> 8);
6275 buffer[2]=(
unsigned char) (value >> 16);
6276 buffer[3]=(
unsigned char) (value >> 24);
6277 return(WriteBlobStream(image,4,buffer));
6279 buffer[0]=(
unsigned char) (value >> 24);
6280 buffer[1]=(
unsigned char) (value >> 16);
6281 buffer[2]=(
unsigned char) (value >> 8);
6282 buffer[3]=(
unsigned char) value;
6283 return(WriteBlobStream(image,4,buffer));
6311MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6316 assert(image != (Image *) NULL);
6317 assert(image->signature == MagickCoreSignature);
6318 if (image->endian == LSBEndian)
6320 buffer[0]=(
unsigned char) value;
6321 buffer[1]=(
unsigned char) (value >> 8);
6322 buffer[2]=(
unsigned char) (value >> 16);
6323 buffer[3]=(
unsigned char) (value >> 24);
6324 buffer[4]=(
unsigned char) (value >> 32);
6325 buffer[5]=(
unsigned char) (value >> 40);
6326 buffer[6]=(
unsigned char) (value >> 48);
6327 buffer[7]=(
unsigned char) (value >> 56);
6328 return(WriteBlobStream(image,8,buffer));
6330 buffer[0]=(
unsigned char) (value >> 56);
6331 buffer[1]=(
unsigned char) (value >> 48);
6332 buffer[2]=(
unsigned char) (value >> 40);
6333 buffer[3]=(
unsigned char) (value >> 32);
6334 buffer[4]=(
unsigned char) (value >> 24);
6335 buffer[5]=(
unsigned char) (value >> 16);
6336 buffer[6]=(
unsigned char) (value >> 8);
6337 buffer[7]=(
unsigned char) value;
6338 return(WriteBlobStream(image,8,buffer));
6366MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6371 assert(image != (Image *) NULL);
6372 assert(image->signature == MagickCoreSignature);
6373 if (image->endian == LSBEndian)
6375 buffer[0]=(
unsigned char) value;
6376 buffer[1]=(
unsigned char) (value >> 8);
6377 return(WriteBlobStream(image,2,buffer));
6379 buffer[0]=(
unsigned char) (value >> 8);
6380 buffer[1]=(
unsigned char) value;
6381 return(WriteBlobStream(image,2,buffer));
6409MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6423 assert(image != (Image *) NULL);
6424 assert(image->signature == MagickCoreSignature);
6425 quantum.signed_value=value;
6426 if (image->endian == LSBEndian)
6428 buffer[0]=(
unsigned char) quantum.unsigned_value;
6429 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6430 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6431 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6432 return(WriteBlobStream(image,4,buffer));
6434 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6435 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6436 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6437 buffer[3]=(
unsigned char) quantum.unsigned_value;
6438 return(WriteBlobStream(image,4,buffer));
6466MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6471 assert(image != (Image *) NULL);
6472 assert(image->signature == MagickCoreSignature);
6473 buffer[0]=(
unsigned char) value;
6474 buffer[1]=(
unsigned char) (value >> 8);
6475 buffer[2]=(
unsigned char) (value >> 16);
6476 buffer[3]=(
unsigned char) (value >> 24);
6477 return(WriteBlobStream(image,4,buffer));
6505MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6510 assert(image != (Image *) NULL);
6511 assert(image->signature == MagickCoreSignature);
6512 buffer[0]=(
unsigned char) value;
6513 buffer[1]=(
unsigned char) (value >> 8);
6514 return(WriteBlobStream(image,2,buffer));
6542MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6556 assert(image != (Image *) NULL);
6557 assert(image->signature == MagickCoreSignature);
6558 quantum.signed_value=value;
6559 buffer[0]=(
unsigned char) quantum.unsigned_value;
6560 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6561 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6562 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6563 return(WriteBlobStream(image,4,buffer));
6591MagickExport ssize_t WriteBlobLSBSignedShort(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;
6610 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6611 return(WriteBlobStream(image,2,buffer));
6639MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6644 assert(image != (Image *) NULL);
6645 assert(image->signature == MagickCoreSignature);
6646 buffer[0]=(
unsigned char) (value >> 24);
6647 buffer[1]=(
unsigned char) (value >> 16);
6648 buffer[2]=(
unsigned char) (value >> 8);
6649 buffer[3]=(
unsigned char) value;
6650 return(WriteBlobStream(image,4,buffer));
6678MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6679 const signed short value)
6693 assert(image != (Image *) NULL);
6694 assert(image->signature == MagickCoreSignature);
6695 quantum.signed_value=value;
6696 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6697 buffer[1]=(
unsigned char) quantum.unsigned_value;
6698 return(WriteBlobStream(image,2,buffer));
6726MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6731 assert(image != (Image *) NULL);
6732 assert(image->signature == MagickCoreSignature);
6733 buffer[0]=(
unsigned char) (value >> 8);
6734 buffer[1]=(
unsigned char) value;
6735 return(WriteBlobStream(image,2,buffer));
6763MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6765 assert(image != (Image *) NULL);
6766 assert(image->signature == MagickCoreSignature);
6767 assert(
string != (
const char *) NULL);
6768 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));