47#include "MagickCore/studio.h"
48#include "MagickCore/blob.h"
49#include "MagickCore/blob-private.h"
50#include "MagickCore/cache.h"
51#include "MagickCore/client.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/delegate.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/locale_.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/memory-private.h"
64#include "MagickCore/nt-base-private.h"
65#include "MagickCore/option.h"
66#include "MagickCore/policy.h"
67#include "MagickCore/policy-private.h"
68#include "MagickCore/resource_.h"
69#include "MagickCore/semaphore.h"
70#include "MagickCore/string_.h"
71#include "MagickCore/string-private.h"
72#include "MagickCore/timer-private.h"
73#include "MagickCore/token.h"
74#include "MagickCore/utility.h"
75#include "MagickCore/utility-private.h"
76#if defined(MAGICKCORE_ZLIB_DELEGATE)
79#if defined(MAGICKCORE_BZLIB_DELEGATE)
86#define MagickMaxBlobExtent (8*8192)
87#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
88# define MAP_ANONYMOUS MAP_ANON
90#if !defined(MAP_FAILED)
91#define MAP_FAILED ((void *) -1)
95#define _O_BINARY O_BINARY
97#if defined(MAGICKCORE_WINDOWS_SUPPORT)
102# define MAGICKCORE_HAVE_MMAP 1
103# define mmap(address,length,protection,access,file,offset) \
104 NTMapMemory(address,length,protection,access,file,offset)
107# define munmap(address,length) NTUnmapMemory(address,length)
110# define pclose _pclose
125#if defined(MAGICKCORE_ZLIB_DELEGATE)
130#if defined(MAGICKCORE_BZLIB_DELEGATE)
222 SyncBlob(
const Image *);
246MagickExport CustomStreamInfo *AcquireCustomStreamInfo(
247 ExceptionInfo *magick_unused(exception))
252 magick_unreferenced(exception);
253 custom_stream=(CustomStreamInfo *) AcquireCriticalMemory(
254 sizeof(*custom_stream));
255 (void) memset(custom_stream,0,
sizeof(*custom_stream));
256 custom_stream->signature=MagickCoreSignature;
257 return(custom_stream);
287MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
290 assert(blob_info != (BlobInfo *) NULL);
291 if (IsEventLogging() != MagickFalse)
292 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
293 blob_info->length=length;
294 blob_info->extent=length;
295 blob_info->quantum=(size_t) MagickMaxBlobExtent;
297 blob_info->type=BlobStream;
298 blob_info->file_info.file=(FILE *) NULL;
299 blob_info->data=(
unsigned char *) blob;
300 blob_info->mapped=MagickFalse;
328MagickExport
void AttachCustomStream(BlobInfo *blob_info,
329 CustomStreamInfo *custom_stream)
331 assert(blob_info != (BlobInfo *) NULL);
332 assert(custom_stream != (CustomStreamInfo *) NULL);
333 assert(custom_stream->signature == MagickCoreSignature);
334 if (IsEventLogging() != MagickFalse)
335 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
336 blob_info->type=CustomStream;
337 blob_info->custom_stream=custom_stream;
371MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
372 const size_t length,ExceptionInfo *exception)
383 assert(filename != (
const char *) NULL);
384 assert(blob != (
const void *) NULL);
385 if (IsEventLogging() != MagickFalse)
386 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
387 if (*filename ==
'\0')
388 file=AcquireUniqueFileResource(filename);
390 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
393 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
396 for (i=0; i < length; i+=(size_t) count)
398 count=MagickWrite(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
399 MagickMaxBufferExtent));
403 file=close_utf8(file);
404 if ((file == -1) || (i < length))
406 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
443MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
444 const size_t length,ExceptionInfo *exception)
459 assert(image_info != (ImageInfo *) NULL);
460 assert(image_info->signature == MagickCoreSignature);
461 assert(exception != (ExceptionInfo *) NULL);
462 if (IsEventLogging() != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
464 image_info->filename);
465 if ((blob == (
const void *) NULL) || (length == 0))
467 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
468 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
469 return((Image *) NULL);
471 blob_info=CloneImageInfo(image_info);
472 blob_info->blob=(
void *) blob;
473 blob_info->length=length;
474 if (*blob_info->magick ==
'\0')
475 (void) SetImageInfo(blob_info,0,exception);
476 magick_info=GetMagickInfo(blob_info->magick,exception);
477 if (magick_info == (
const MagickInfo *) NULL)
479 (void) ThrowMagickException(exception,GetMagickModule(),
480 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
482 blob_info=DestroyImageInfo(blob_info);
483 return((Image *) NULL);
485 if (GetMagickBlobSupport(magick_info) != MagickFalse)
488 filename[MagickPathExtent];
493 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
494 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
495 blob_info->magick,filename);
496 image=ReadImage(blob_info,exception);
497 if (image != (Image *) NULL)
498 (void) DetachBlob(image->blob);
499 blob_info=DestroyImageInfo(blob_info);
505 blob_info->blob=(
void *) NULL;
507 *blob_info->filename=
'\0';
508 status=BlobToFile(blob_info->filename,blob,length,exception);
509 if (status == MagickFalse)
511 (void) RelinquishUniqueFileResource(blob_info->filename);
512 blob_info=DestroyImageInfo(blob_info);
513 return((Image *) NULL);
515 clone_info=CloneImageInfo(blob_info);
516 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
517 blob_info->magick,blob_info->filename);
518 image=ReadImage(clone_info,exception);
519 if (image != (Image *) NULL)
527 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
529 (void) CopyMagickString(images->filename,image_info->filename,
531 (void) CopyMagickString(images->magick_filename,image_info->filename,
533 (void) CopyMagickString(images->magick,magick_info->name,
535 images=GetNextImageInList(images);
538 clone_info=DestroyImageInfo(clone_info);
539 (void) RelinquishUniqueFileResource(blob_info->filename);
540 blob_info=DestroyImageInfo(blob_info);
567MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
575 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
576 GetBlobInfo(clone_info);
577 if (blob_info == (BlobInfo *) NULL)
579 semaphore=clone_info->semaphore;
580 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
581 if (blob_info->mapped != MagickFalse)
582 (void) AcquireMagickResource(MapResource,blob_info->length);
583 clone_info->semaphore=semaphore;
584 LockSemaphoreInfo(clone_info->semaphore);
585 clone_info->reference_count=1;
586 UnlockSemaphoreInfo(clone_info->semaphore);
613static inline void ThrowBlobException(BlobInfo *blob_info)
615 if ((blob_info->status == 0) && (errno != 0))
616 blob_info->error_number=errno;
617 blob_info->status=(-1);
620MagickExport MagickBooleanType CloseBlob(Image *image)
623 *magick_restrict blob_info;
631 assert(image != (Image *) NULL);
632 assert(image->signature == MagickCoreSignature);
633 if (IsEventLogging() != MagickFalse)
634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
635 blob_info=image->blob;
636 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
638 (void) SyncBlob(image);
639 status=blob_info->status;
640 switch (blob_info->type)
642 case UndefinedStream:
648 if (blob_info->synchronize != MagickFalse)
650 status=fflush(blob_info->file_info.file);
652 ThrowBlobException(blob_info);
653 status=fsync(fileno(blob_info->file_info.file));
655 ThrowBlobException(blob_info);
657 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
658 ThrowBlobException(blob_info);
663#if defined(MAGICKCORE_ZLIB_DELEGATE)
665 (void) gzerror(blob_info->file_info.gzfile,&status);
667 ThrowBlobException(blob_info);
673#if defined(MAGICKCORE_BZLIB_DELEGATE)
675 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
677 ThrowBlobException(blob_info);
685 if (blob_info->file_info.file != (FILE *) NULL)
687 if (blob_info->synchronize != MagickFalse)
689 status=fflush(blob_info->file_info.file);
691 ThrowBlobException(blob_info);
692 status=fsync(fileno(blob_info->file_info.file));
694 ThrowBlobException(blob_info);
696 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
697 ThrowBlobException(blob_info);
704 blob_info->size=GetBlobSize(image);
705 image->extent=blob_info->size;
706 blob_info->eof=MagickFalse;
708 blob_info->mode=UndefinedBlobMode;
709 if (blob_info->exempt != MagickFalse)
711 blob_info->type=UndefinedStream;
712 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
714 switch (blob_info->type)
716 case UndefinedStream:
721 if (blob_info->file_info.file != (FILE *) NULL)
723 status=fclose(blob_info->file_info.file);
725 ThrowBlobException(blob_info);
731#if defined(MAGICKCORE_HAVE_PCLOSE)
732 status=pclose(blob_info->file_info.file);
734 ThrowBlobException(blob_info);
740#if defined(MAGICKCORE_ZLIB_DELEGATE)
741 status=gzclose(blob_info->file_info.gzfile);
743 ThrowBlobException(blob_info);
749#if defined(MAGICKCORE_BZLIB_DELEGATE)
750 BZ2_bzclose(blob_info->file_info.bzfile);
758 if (blob_info->file_info.file != (FILE *) NULL)
760 status=fclose(blob_info->file_info.file);
762 ThrowBlobException(blob_info);
769 (void) DetachBlob(blob_info);
770 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
799MagickExport Image *CustomStreamToImage(
const ImageInfo *image_info,
800 ExceptionInfo *exception)
811 assert(image_info != (ImageInfo *) NULL);
812 assert(image_info->signature == MagickCoreSignature);
813 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
814 assert(image_info->custom_stream->signature == MagickCoreSignature);
815 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
816 assert(exception != (ExceptionInfo *) NULL);
817 if (IsEventLogging() != MagickFalse)
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
819 image_info->filename);
820 blob_info=CloneImageInfo(image_info);
821 if (*blob_info->magick ==
'\0')
822 (void) SetImageInfo(blob_info,0,exception);
823 magick_info=GetMagickInfo(blob_info->magick,exception);
824 if (magick_info == (
const MagickInfo *) NULL)
826 (void) ThrowMagickException(exception,GetMagickModule(),
827 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
829 blob_info=DestroyImageInfo(blob_info);
830 return((Image *) NULL);
832 image=(Image *) NULL;
833 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
834 (*blob_info->filename !=
'\0'))
837 filename[MagickPathExtent];
843 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
844 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
845 blob_info->magick,filename);
846 image=ReadImage(blob_info,exception);
851 unique[MagickPathExtent];
865 blob_info->custom_stream=(CustomStreamInfo *) NULL;
866 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
868 if (blob == (
unsigned char *) NULL)
870 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
871 image_info->filename);
872 blob_info=DestroyImageInfo(blob_info);
873 return((Image *) NULL);
875 file=AcquireUniqueFileResource(unique);
878 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
879 image_info->filename);
880 blob=(
unsigned char *) RelinquishMagickMemory(blob);
881 blob_info=DestroyImageInfo(blob_info);
882 return((Image *) NULL);
884 clone_info=CloneImageInfo(blob_info);
885 blob_info->file=fdopen(file,
"wb+");
886 if (blob_info->file != (FILE *) NULL)
891 count=(ssize_t) MagickMaxBufferExtent;
892 while (count == (ssize_t) MagickMaxBufferExtent)
894 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
895 image_info->custom_stream->data);
896 count=(ssize_t) write(file,(
const char *) blob,(
size_t) count);
898 (void) fclose(blob_info->file);
899 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
900 "%s:%s",blob_info->magick,unique);
901 image=ReadImage(clone_info,exception);
902 if (image != (Image *) NULL)
910 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
912 (void) CopyMagickString(images->filename,image_info->filename,
914 (void) CopyMagickString(images->magick_filename,
915 image_info->filename,MagickPathExtent);
916 (void) CopyMagickString(images->magick,magick_info->name,
918 images=GetNextImageInList(images);
922 clone_info=DestroyImageInfo(clone_info);
923 blob=(
unsigned char *) RelinquishMagickMemory(blob);
924 (void) RelinquishUniqueFileResource(unique);
926 blob_info=DestroyImageInfo(blob_info);
927 if (image != (Image *) NULL)
928 if (CloseBlob(image) == MagickFalse)
929 image=DestroyImageList(image);
955MagickExport
void DestroyBlob(Image *image)
958 *magick_restrict blob_info;
963 assert(image != (Image *) NULL);
964 assert(image->signature == MagickCoreSignature);
965 assert(image->blob != (BlobInfo *) NULL);
966 assert(image->blob->signature == MagickCoreSignature);
967 if (IsEventLogging() != MagickFalse)
968 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
969 blob_info=image->blob;
971 LockSemaphoreInfo(blob_info->semaphore);
972 blob_info->reference_count--;
973 assert(blob_info->reference_count >= 0);
974 if (blob_info->reference_count == 0)
976 UnlockSemaphoreInfo(blob_info->semaphore);
977 if (destroy == MagickFalse)
979 image->blob=(BlobInfo *) NULL;
982 (void) CloseBlob(image);
983 if (blob_info->mapped != MagickFalse)
985 (void) UnmapBlob(blob_info->data,blob_info->length);
986 RelinquishMagickResource(MapResource,blob_info->length);
989 RelinquishSemaphoreInfo(&blob_info->semaphore);
990 blob_info->signature=(~MagickCoreSignature);
991 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
1017MagickExport CustomStreamInfo *DestroyCustomStreamInfo(
1018 CustomStreamInfo *custom_stream)
1020 assert(custom_stream != (CustomStreamInfo *) NULL);
1021 assert(custom_stream->signature == MagickCoreSignature);
1022 if (IsEventLogging() != MagickFalse)
1023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1024 custom_stream->signature=(~MagickCoreSignature);
1025 custom_stream=(CustomStreamInfo *) RelinquishMagickMemory(custom_stream);
1026 return(custom_stream);
1051MagickExport
void *DetachBlob(BlobInfo *blob_info)
1056 assert(blob_info != (BlobInfo *) NULL);
1057 if (IsEventLogging() != MagickFalse)
1058 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1059 if (blob_info->mapped != MagickFalse)
1061 (void) UnmapBlob(blob_info->data,blob_info->length);
1062 blob_info->data=NULL;
1063 RelinquishMagickResource(MapResource,blob_info->length);
1065 blob_info->mapped=MagickFalse;
1066 blob_info->length=0;
1071 blob_info->offset=0;
1072 blob_info->mode=UndefinedBlobMode;
1073 blob_info->eof=MagickFalse;
1075 blob_info->exempt=MagickFalse;
1076 blob_info->type=UndefinedStream;
1077 blob_info->file_info.file=(FILE *) NULL;
1078 data=blob_info->data;
1079 blob_info->data=(
unsigned char *) NULL;
1080 blob_info->stream=(StreamHandler) NULL;
1081 blob_info->custom_stream=(CustomStreamInfo *) NULL;
1109MagickExport
void DisassociateBlob(Image *image)
1112 *magick_restrict blob_info,
1118 assert(image != (Image *) NULL);
1119 assert(image->signature == MagickCoreSignature);
1120 assert(image->blob != (BlobInfo *) NULL);
1121 assert(image->blob->signature == MagickCoreSignature);
1122 if (IsEventLogging() != MagickFalse)
1123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1124 blob_info=image->blob;
1126 LockSemaphoreInfo(blob_info->semaphore);
1127 assert(blob_info->reference_count >= 0);
1128 if (blob_info->reference_count > 1)
1130 UnlockSemaphoreInfo(blob_info->semaphore);
1131 if (clone == MagickFalse)
1133 clone_info=CloneBlobInfo(blob_info);
1135 image->blob=clone_info;
1163MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1164 const MagickSizeType length)
1176 buffer[MagickMinBufferExtent >> 1];
1178 assert(image != (Image *) NULL);
1179 assert(image->signature == MagickCoreSignature);
1180 if (length != (MagickSizeType) ((MagickOffsetType) length))
1181 return(MagickFalse);
1183 for (i=0; i < length; i+=(MagickSizeType) count)
1185 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1186 (void) ReadBlobStream(image,quantum,buffer,&count);
1194 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1221MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
1223 assert(image != (Image *) NULL);
1224 assert(image->signature == MagickCoreSignature);
1225 assert(duplicate != (Image *) NULL);
1226 assert(duplicate->signature == MagickCoreSignature);
1227 if (IsEventLogging() != MagickFalse)
1228 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1230 image->blob=ReferenceBlob(duplicate->blob);
1256MagickExport
int EOFBlob(
const Image *image)
1259 *magick_restrict blob_info;
1261 assert(image != (Image *) NULL);
1262 assert(image->signature == MagickCoreSignature);
1263 assert(image->blob != (BlobInfo *) NULL);
1264 assert(image->blob->type != UndefinedStream);
1265 if (IsEventLogging() != MagickFalse)
1266 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1267 blob_info=image->blob;
1268 switch (blob_info->type)
1270 case UndefinedStream:
1271 case StandardStream:
1276 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1282#if defined(MAGICKCORE_ZLIB_DELEGATE)
1283 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1290#if defined(MAGICKCORE_BZLIB_DELEGATE)
1295 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1296 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1302 blob_info->eof=MagickFalse;
1310 return((
int) blob_info->eof);
1336MagickExport
int ErrorBlob(
const Image *image)
1339 *magick_restrict blob_info;
1341 assert(image != (Image *) NULL);
1342 assert(image->signature == MagickCoreSignature);
1343 assert(image->blob != (BlobInfo *) NULL);
1344 assert(image->blob->type != UndefinedStream);
1345 if (IsEventLogging() != MagickFalse)
1346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1347 blob_info=image->blob;
1348 switch (blob_info->type)
1350 case UndefinedStream:
1351 case StandardStream:
1356 blob_info->error=ferror(blob_info->file_info.file);
1361#if defined(MAGICKCORE_ZLIB_DELEGATE)
1362 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1368#if defined(MAGICKCORE_BZLIB_DELEGATE)
1369 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1383 return(blob_info->error);
1421MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1422 size_t *length,ExceptionInfo *exception)
1448 assert(filename != (
const char *) NULL);
1449 assert(exception != (ExceptionInfo *) NULL);
1450 assert(exception->signature == MagickCoreSignature);
1451 if (IsEventLogging() != MagickFalse)
1452 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1454 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1455 ThrowPolicyException(filename,NULL);
1457 if (LocaleCompare(filename,
"-") != 0)
1460 flags = O_RDONLY | O_BINARY;
1462 status=GetPathAttributes(filename,&attributes);
1463 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1465 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1468 file=open_utf8(filename,flags,0);
1472 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1475 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1477 file=close_utf8(file)-1;
1478 ThrowPolicyException(filename,NULL);
1480 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1482 if ((file == fileno(stdin)) || (offset < 0) ||
1483 (offset != (MagickOffsetType) ((ssize_t) offset)))
1494 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1495 quantum=(size_t) MagickMaxBufferExtent;
1496 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1497 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1498 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1499 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1501 count=MagickRead(file,blob+i,quantum);
1504 if (~i < ((
size_t) count+quantum+1))
1506 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1509 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1510 quantum+1,
sizeof(*blob));
1511 if ((i+(
size_t) count) >= extent)
1514 if (LocaleCompare(filename,
"-") != 0)
1515 file=close_utf8(file);
1516 if (blob == (
unsigned char *) NULL)
1518 (void) ThrowMagickException(exception,GetMagickModule(),
1519 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1524 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1525 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1528 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1532 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1533 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1534 blob=(
unsigned char *) NULL;
1535 if (~(*length) >= (MagickPathExtent-1))
1536 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1538 if (blob == (
unsigned char *) NULL)
1540 file=close_utf8(file);
1541 (void) ThrowMagickException(exception,GetMagickModule(),
1542 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1545 map=MapBlob(file,ReadMode,0,*length);
1546 if (map != (
unsigned char *) NULL)
1548 (void) memcpy(blob,map,*length);
1549 (void) UnmapBlob(map,*length);
1553 (void) lseek(file,0,SEEK_SET);
1554 for (i=0; i < *length; i+=(size_t) count)
1556 count=MagickRead(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1557 MagickMaxBufferExtent));
1563 file=close_utf8(file)-1;
1564 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1565 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1570 if (LocaleCompare(filename,
"-") != 0)
1571 file=close_utf8(file);
1574 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1575 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1605static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1606 const void *magick_restrict data)
1609 *magick_restrict blob_info;
1617 assert(image->blob != (BlobInfo *) NULL);
1618 assert(image->blob->type != UndefinedStream);
1619 assert(data != NULL);
1620 blob_info=image->blob;
1621 if (blob_info->type != BlobStream)
1622 return(WriteBlob(image,length,(
const unsigned char *) data));
1623 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1628 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1629 if (extent >= blob_info->extent)
1631 extent+=blob_info->quantum+length;
1632 blob_info->quantum<<=1;
1633 if (SetBlobExtent(image,extent) == MagickFalse)
1636 q=blob_info->data+blob_info->offset;
1637 (void) memcpy(q,data,length);
1638 blob_info->offset+=(MagickOffsetType) length;
1639 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1640 blob_info->length=(size_t) blob_info->offset;
1641 return((ssize_t) length);
1644MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1645 ExceptionInfo *exception)
1663 assert(image != (
const Image *) NULL);
1664 assert(image->signature == MagickCoreSignature);
1665 assert(filename != (
const char *) NULL);
1666 if (IsEventLogging() != MagickFalse)
1667 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1668 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1669 ThrowPolicyException(filename,MagickFalse);
1671 if (LocaleCompare(filename,
"-") != 0)
1674 flags = O_RDONLY | O_BINARY;
1676 file=open_utf8(filename,flags,0);
1680 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1681 return(MagickFalse);
1683 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1684 ThrowPolicyException(filename,MagickFalse);
1685 quantum=(size_t) MagickMaxBufferExtent;
1686 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1687 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1688 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1689 if (blob == (
unsigned char *) NULL)
1691 file=close_utf8(file);
1692 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1694 return(MagickFalse);
1698 count=MagickRead(file,blob,quantum);
1701 length=(size_t) count;
1702 count=WriteBlobStream(image,length,blob);
1703 if (count != (ssize_t) length)
1705 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1709 file=close_utf8(file);
1711 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1712 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1739MagickExport MagickBooleanType GetBlobError(
const Image *image)
1741 assert(image != (
const Image *) NULL);
1742 assert(image->signature == MagickCoreSignature);
1743 if (IsEventLogging() != MagickFalse)
1744 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1745 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1746 errno=image->blob->error_number;
1747 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1772MagickExport FILE *GetBlobFileHandle(
const Image *image)
1774 assert(image != (
const Image *) NULL);
1775 assert(image->signature == MagickCoreSignature);
1776 return(image->blob->file_info.file);
1801MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1803 assert(blob_info != (BlobInfo *) NULL);
1804 (void) memset(blob_info,0,
sizeof(*blob_info));
1805 blob_info->type=UndefinedStream;
1806 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1807 blob_info->properties.st_mtime=GetMagickTime();
1808 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1809 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1810 blob_info->reference_count=1;
1811 blob_info->semaphore=AcquireSemaphoreInfo();
1812 blob_info->signature=MagickCoreSignature;
1837MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1839 assert(image != (Image *) NULL);
1840 assert(image->signature == MagickCoreSignature);
1841 if (IsEventLogging() != MagickFalse)
1842 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1843 return(&image->blob->properties);
1869MagickExport MagickSizeType GetBlobSize(
const Image *image)
1872 *magick_restrict blob_info;
1877 assert(image != (Image *) NULL);
1878 assert(image->signature == MagickCoreSignature);
1879 assert(image->blob != (BlobInfo *) NULL);
1880 if (IsEventLogging() != MagickFalse)
1881 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1882 blob_info=image->blob;
1884 switch (blob_info->type)
1886 case UndefinedStream:
1887 case StandardStream:
1889 extent=blob_info->size;
1897 extent=(MagickSizeType) blob_info->properties.st_size;
1899 extent=blob_info->size;
1900 file_descriptor=fileno(blob_info->file_info.file);
1901 if (file_descriptor == -1)
1903 if (fstat(file_descriptor,&blob_info->properties) == 0)
1904 extent=(MagickSizeType) blob_info->properties.st_size;
1909 extent=blob_info->size;
1918 status=GetPathAttributes(image->filename,&blob_info->properties);
1919 if (status != MagickFalse)
1920 extent=(MagickSizeType) blob_info->properties.st_size;
1927 extent=(MagickSizeType) blob_info->length;
1932 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1933 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1938 offset=blob_info->custom_stream->teller(
1939 blob_info->custom_stream->data);
1940 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1941 blob_info->custom_stream->data);
1942 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1943 blob_info->custom_stream->data);
1973MagickExport
void *GetBlobStreamData(
const Image *image)
1975 assert(image != (
const Image *) NULL);
1976 assert(image->signature == MagickCoreSignature);
1977 return(image->blob->data);
2002MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2004 assert(image != (
const Image *) NULL);
2005 assert(image->signature == MagickCoreSignature);
2006 if (IsEventLogging() != MagickFalse)
2007 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2008 return(image->blob->stream);
2044MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2045 Image *image,
size_t *length,ExceptionInfo *exception)
2059 assert(image_info != (
const ImageInfo *) NULL);
2060 assert(image_info->signature == MagickCoreSignature);
2061 assert(image != (Image *) NULL);
2062 assert(image->signature == MagickCoreSignature);
2063 assert(exception != (ExceptionInfo *) NULL);
2064 assert(exception->signature == MagickCoreSignature);
2065 if (IsEventLogging() != MagickFalse)
2066 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2067 image_info->filename);
2069 blob=(
unsigned char *) NULL;
2070 blob_info=CloneImageInfo(image_info);
2071 blob_info->adjoin=MagickFalse;
2072 (void) SetImageInfo(blob_info,1,exception);
2073 if (*blob_info->magick !=
'\0')
2074 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2075 magick_info=GetMagickInfo(image->magick,exception);
2076 if (magick_info == (
const MagickInfo *) NULL)
2078 (void) ThrowMagickException(exception,GetMagickModule(),
2079 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2081 blob_info=DestroyImageInfo(blob_info);
2084 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2085 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2090 blob_info->length=0;
2091 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2092 sizeof(
unsigned char));
2093 if (blob_info->blob == NULL)
2094 (void) ThrowMagickException(exception,GetMagickModule(),
2095 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2098 (void) CloseBlob(image);
2099 image->blob->exempt=MagickTrue;
2100 image->blob->extent=0;
2101 *image->filename=
'\0';
2102 status=WriteImage(blob_info,image,exception);
2103 *length=image->blob->length;
2104 blob=DetachBlob(image->blob);
2105 if (blob != (
void *) NULL)
2107 if (status == MagickFalse)
2108 blob=RelinquishMagickMemory(blob);
2110 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2112 else if ((status == MagickFalse) && (image->blob->extent == 0))
2113 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2119 unique[MagickPathExtent];
2127 file=AcquireUniqueFileResource(unique);
2130 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2131 image_info->filename);
2135 blob_info->file=fdopen(file,
"wb");
2136 if (blob_info->file != (FILE *) NULL)
2138 (void) FormatLocaleString(image->filename,MagickPathExtent,
2139 "%s:%s",image->magick,unique);
2140 status=WriteImage(blob_info,image,exception);
2141 (void) fclose(blob_info->file);
2142 if (status != MagickFalse)
2143 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2145 (void) RelinquishUniqueFileResource(unique);
2148 blob_info=DestroyImageInfo(blob_info);
2180MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2181 ExceptionInfo *exception)
2193 assert(image_info != (
const ImageInfo *) NULL);
2194 assert(image_info->signature == MagickCoreSignature);
2195 assert(image != (Image *) NULL);
2196 assert(image->signature == MagickCoreSignature);
2197 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2198 assert(image_info->custom_stream->signature == MagickCoreSignature);
2199 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2200 assert(exception != (ExceptionInfo *) NULL);
2201 if (IsEventLogging() != MagickFalse)
2202 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2203 image_info->filename);
2204 clone_info=CloneImageInfo(image_info);
2205 clone_info->adjoin=MagickFalse;
2206 (void) SetImageInfo(clone_info,1,exception);
2207 if (*clone_info->magick !=
'\0')
2208 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2209 magick_info=GetMagickInfo(image->magick,exception);
2210 if (magick_info == (
const MagickInfo *) NULL)
2212 (void) ThrowMagickException(exception,GetMagickModule(),
2213 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2215 clone_info=DestroyImageInfo(clone_info);
2218 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2219 blob_support=GetMagickBlobSupport(magick_info);
2220 if ((blob_support != MagickFalse) &&
2221 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2223 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2224 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2225 blob_support=MagickFalse;
2227 if (blob_support != MagickFalse)
2232 (void) CloseBlob(image);
2233 *image->filename=
'\0';
2234 (void) WriteImage(clone_info,image,exception);
2239 unique[MagickPathExtent];
2250 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2251 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2253 if (blob == (
unsigned char *) NULL)
2255 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2256 image_info->filename);
2257 clone_info=DestroyImageInfo(clone_info);
2260 file=AcquireUniqueFileResource(unique);
2263 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2264 image_info->filename);
2265 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2266 clone_info=DestroyImageInfo(clone_info);
2269 clone_info->file=fdopen(file,
"wb+");
2270 if (clone_info->file != (FILE *) NULL)
2275 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2276 image->magick,unique);
2277 status=WriteImage(clone_info,image,exception);
2278 if (status != MagickFalse)
2280 (void) fseek(clone_info->file,0,SEEK_SET);
2281 count=(ssize_t) MagickMaxBufferExtent;
2282 while (count == (ssize_t) MagickMaxBufferExtent)
2284 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2286 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2287 image_info->custom_stream->data);
2290 (void) fclose(clone_info->file);
2292 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2293 (void) RelinquishUniqueFileResource(unique);
2295 clone_info=DestroyImageInfo(clone_info);
2326MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2327 ExceptionInfo *exception)
2351 assert(image != (Image *) NULL);
2352 assert(image->signature == MagickCoreSignature);
2353 assert(image->blob != (BlobInfo *) NULL);
2354 assert(image->blob->type != UndefinedStream);
2355 assert(filename != (
const char *) NULL);
2356 if (IsEventLogging() != MagickFalse)
2357 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2358 if (*filename ==
'\0')
2359 file=AcquireUniqueFileResource(filename);
2361 if (LocaleCompare(filename,
"-") == 0)
2362 file=fileno(stdout);
2364 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2367 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2368 return(MagickFalse);
2370 quantum=(size_t) MagickMaxBufferExtent;
2371 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2372 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2373 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2374 if (buffer == (
unsigned char *) NULL)
2376 file=close_utf8(file)-1;
2377 (void) ThrowMagickException(exception,GetMagickModule(),
2378 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2379 return(MagickFalse);
2382 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2383 for (i=0; count > 0; )
2385 length=(size_t) count;
2386 for (i=0; i < length; i+=(size_t) count)
2388 count=MagickWrite(file,p+i,(
size_t) (length-i));
2394 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2396 if (LocaleCompare(filename,
"-") != 0)
2397 file=close_utf8(file);
2398 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2399 if ((file == -1) || (i < length))
2402 file=close_utf8(file);
2403 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2404 return(MagickFalse);
2444MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2445 size_t *length,ExceptionInfo *exception)
2459 assert(image_info != (
const ImageInfo *) NULL);
2460 assert(image_info->signature == MagickCoreSignature);
2461 assert(images != (Image *) NULL);
2462 assert(images->signature == MagickCoreSignature);
2463 assert(exception != (ExceptionInfo *) NULL);
2464 if (IsEventLogging() != MagickFalse)
2465 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2466 image_info->filename);
2468 blob=(
unsigned char *) NULL;
2469 blob_info=CloneImageInfo(image_info);
2470 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2472 if (*blob_info->magick !=
'\0')
2473 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2474 magick_info=GetMagickInfo(images->magick,exception);
2475 if (magick_info == (
const MagickInfo *) NULL)
2477 (void) ThrowMagickException(exception,GetMagickModule(),
2478 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2480 blob_info=DestroyImageInfo(blob_info);
2483 if (GetMagickAdjoin(magick_info) == MagickFalse)
2485 blob_info=DestroyImageInfo(blob_info);
2486 return(ImageToBlob(image_info,images,length,exception));
2488 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2489 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2494 blob_info->length=0;
2495 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2496 sizeof(
unsigned char));
2497 if (blob_info->blob == (
void *) NULL)
2498 (void) ThrowMagickException(exception,GetMagickModule(),
2499 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2502 (void) CloseBlob(images);
2503 images->blob->exempt=MagickTrue;
2504 images->blob->extent=0;
2505 *images->filename=
'\0';
2506 status=WriteImages(blob_info,images,images->filename,exception);
2507 *length=images->blob->length;
2508 blob=DetachBlob(images->blob);
2509 if (blob != (
void *) NULL)
2511 if (status == MagickFalse)
2512 blob=RelinquishMagickMemory(blob);
2514 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2516 else if ((status == MagickFalse) && (images->blob->extent == 0))
2517 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2523 filename[MagickPathExtent],
2524 unique[MagickPathExtent];
2532 file=AcquireUniqueFileResource(unique);
2535 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2536 image_info->filename);
2540 blob_info->file=fdopen(file,
"wb");
2541 if (blob_info->file != (FILE *) NULL)
2543 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2544 images->magick,unique);
2545 status=WriteImages(blob_info,images,filename,exception);
2546 (void) fclose(blob_info->file);
2547 if (status != MagickFalse)
2548 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2550 (void) RelinquishUniqueFileResource(unique);
2553 blob_info=DestroyImageInfo(blob_info);
2585MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2586 Image *images,ExceptionInfo *exception)
2598 assert(image_info != (
const ImageInfo *) NULL);
2599 assert(image_info->signature == MagickCoreSignature);
2600 assert(images != (Image *) NULL);
2601 assert(images->signature == MagickCoreSignature);
2602 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2603 assert(image_info->custom_stream->signature == MagickCoreSignature);
2604 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2605 assert(exception != (ExceptionInfo *) NULL);
2606 if (IsEventLogging() != MagickFalse)
2607 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2608 image_info->filename);
2609 clone_info=CloneImageInfo(image_info);
2610 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2612 if (*clone_info->magick !=
'\0')
2613 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2614 magick_info=GetMagickInfo(images->magick,exception);
2615 if (magick_info == (
const MagickInfo *) NULL)
2617 (void) ThrowMagickException(exception,GetMagickModule(),
2618 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2620 clone_info=DestroyImageInfo(clone_info);
2623 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2624 blob_support=GetMagickBlobSupport(magick_info);
2625 if ((blob_support != MagickFalse) &&
2626 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2628 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2629 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2630 blob_support=MagickFalse;
2632 if (blob_support != MagickFalse)
2637 (void) CloseBlob(images);
2638 *images->filename=
'\0';
2639 (void) WriteImages(clone_info,images,images->filename,exception);
2644 filename[MagickPathExtent],
2645 unique[MagickPathExtent];
2656 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2657 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2659 if (blob == (
unsigned char *) NULL)
2661 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2662 image_info->filename);
2663 clone_info=DestroyImageInfo(clone_info);
2666 file=AcquireUniqueFileResource(unique);
2669 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2670 image_info->filename);
2671 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2672 clone_info=DestroyImageInfo(clone_info);
2675 clone_info->file=fdopen(file,
"wb+");
2676 if (clone_info->file != (FILE *) NULL)
2681 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2682 images->magick,unique);
2683 status=WriteImages(clone_info,images,filename,exception);
2684 if (status != MagickFalse)
2686 (void) fseek(clone_info->file,0,SEEK_SET);
2687 count=(ssize_t) MagickMaxBufferExtent;
2688 while (count == (ssize_t) MagickMaxBufferExtent)
2690 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2692 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2693 image_info->custom_stream->data);
2696 (void) fclose(clone_info->file);
2698 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2699 (void) RelinquishUniqueFileResource(unique);
2701 clone_info=DestroyImageInfo(clone_info);
2737MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2738 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2741 filename[MagickPathExtent];
2770 assert(image_info != (ImageInfo *) NULL);
2771 assert(image_info->signature == MagickCoreSignature);
2772 assert(image != (Image *) NULL);
2773 assert(image->signature == MagickCoreSignature);
2774 assert(inject_image != (Image *) NULL);
2775 assert(inject_image->signature == MagickCoreSignature);
2776 assert(exception != (ExceptionInfo *) NULL);
2777 if (IsEventLogging() != MagickFalse)
2778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2779 unique_file=(FILE *) NULL;
2780 file=AcquireUniqueFileResource(filename);
2782 unique_file=fdopen(file,
"wb");
2783 if ((file == -1) || (unique_file == (FILE *) NULL))
2785 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2786 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2788 return(MagickFalse);
2790 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2791 if (byte_image == (Image *) NULL)
2793 (void) fclose(unique_file);
2794 (void) RelinquishUniqueFileResource(filename);
2795 return(MagickFalse);
2797 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2799 DestroyBlob(byte_image);
2800 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2801 write_info=CloneImageInfo(image_info);
2802 SetImageInfoFile(write_info,unique_file);
2803 status=WriteImage(write_info,byte_image,exception);
2804 write_info=DestroyImageInfo(write_info);
2805 byte_image=DestroyImage(byte_image);
2806 (void) fclose(unique_file);
2807 if (status == MagickFalse)
2809 (void) RelinquishUniqueFileResource(filename);
2810 return(MagickFalse);
2815 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2818 (void) RelinquishUniqueFileResource(filename);
2819 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2820 image_info->filename);
2821 return(MagickFalse);
2823 quantum=(size_t) MagickMaxBufferExtent;
2824 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2825 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2826 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2827 if (buffer == (
unsigned char *) NULL)
2829 (void) RelinquishUniqueFileResource(filename);
2830 file=close_utf8(file);
2831 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2836 ssize_t count = read(file,buffer,quantum);
2843 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2846 file=close_utf8(file);
2848 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2849 (void) RelinquishUniqueFileResource(filename);
2850 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2876MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2878 assert(image != (
const Image *) NULL);
2879 assert(image->signature == MagickCoreSignature);
2880 if (IsEventLogging() != MagickFalse)
2881 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2882 return(image->blob->exempt);
2907MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2910 *magick_restrict blob_info;
2912 assert(image != (
const Image *) NULL);
2913 assert(image->signature == MagickCoreSignature);
2914 if (IsEventLogging() != MagickFalse)
2915 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2916 blob_info=image->blob;
2917 switch (blob_info->type)
2926 if (blob_info->file_info.file == (FILE *) NULL)
2927 return(MagickFalse);
2928 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2929 return(status == -1 ? MagickFalse : MagickTrue);
2933#if defined(MAGICKCORE_ZLIB_DELEGATE)
2937 if (blob_info->file_info.gzfile == (gzFile) NULL)
2938 return(MagickFalse);
2939 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2940 return(offset < 0 ? MagickFalse : MagickTrue);
2945 case UndefinedStream:
2949 case StandardStream:
2953 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
2954 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
2961 return(MagickFalse);
2986MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2988 assert(image != (
const Image *) NULL);
2989 assert(image->signature == MagickCoreSignature);
2990 if (IsEventLogging() != MagickFalse)
2991 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2992 return(image->blob->temporary);
3024MagickExport
void *MapBlob(
int file,
const MapMode mode,
3025 const MagickOffsetType offset,
const size_t length)
3027#if defined(MAGICKCORE_HAVE_MMAP)
3040#if defined(MAP_ANONYMOUS)
3041 flags|=MAP_ANONYMOUS;
3050 protection=PROT_READ;
3056 protection=PROT_WRITE;
3062 protection=PROT_READ | PROT_WRITE;
3067#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3068 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3070 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3071 if (map == MAP_FAILED)
3072 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3074 if (map == MAP_FAILED)
3111MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3120 assert(buffer != (
unsigned char *) NULL);
3127 *buffer++=(
unsigned char) c;
3131 *buffer++=(
unsigned char) c;
3161MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3169 assert(p != (
unsigned char *) NULL);
3176 *p++=(
unsigned char) c;
3212static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3213 const BlobInfo *blob_info)
3224 size=MagickMinBufferExtent;
3225 option=GetImageOption(image_info,
"stream:buffer-size");
3226 if (option != (
const char *) NULL)
3227 size=StringToUnsignedLong(option);
3228 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3229 _IONBF : _IOFBF,size);
3230 return(status == 0 ? MagickTrue : MagickFalse);
3233#if defined(MAGICKCORE_ZLIB_DELEGATE)
3234static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3236#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3237 return(gzopen(path,mode));
3245 path_wide=NTCreateWidePath(path);
3246 if (path_wide == (
wchar_t *) NULL)
3247 return((gzFile) NULL);
3248 file=gzopen_w(path_wide,mode);
3249 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3255MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3256 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3259 *magick_restrict blob_info;
3262 extension[MagickPathExtent],
3263 filename[MagickPathExtent];
3277 assert(image_info != (ImageInfo *) NULL);
3278 assert(image_info->signature == MagickCoreSignature);
3279 assert(image != (Image *) NULL);
3280 assert(image->signature == MagickCoreSignature);
3281 if (IsEventLogging() != MagickFalse)
3282 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3283 image_info->filename);
3284 blob_info=image->blob;
3285 if (image_info->blob != (
void *) NULL)
3287 if (image_info->stream != (StreamHandler) NULL)
3288 blob_info->stream=(StreamHandler) image_info->stream;
3289 AttachBlob(blob_info,image_info->blob,image_info->length);
3292 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3293 (*image->filename ==
'\0'))
3295 blob_info->type=CustomStream;
3296 blob_info->custom_stream=image_info->custom_stream;
3297 if (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL)
3298 blob_info->custom_stream->seeker(0,SEEK_SET,
3299 blob_info->custom_stream->data);
3302 (void) DetachBlob(blob_info);
3303 blob_info->mode=mode;
3312 case ReadBinaryBlobMode:
3314 flags=O_RDONLY | O_BINARY;
3320 flags=O_WRONLY | O_CREAT | O_TRUNC;
3324 case WriteBinaryBlobMode:
3326 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3330 case AppendBlobMode:
3332 flags=O_WRONLY | O_CREAT | O_APPEND;
3336 case AppendBinaryBlobMode:
3338 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3350 blob_info->synchronize=image_info->synchronize;
3351 if (image_info->stream != (StreamHandler) NULL)
3353 blob_info->stream=image_info->stream;
3356 blob_info->type=FifoStream;
3364 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3365 rights=ReadPolicyRights;
3367 rights=WritePolicyRights;
3368 if (IsPathAuthorized(rights,filename) == MagickFalse)
3369 ThrowPolicyException(filename,MagickFalse);
3370 if ((LocaleCompare(filename,
"-") == 0) ||
3371 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3373 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3374#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3375 if (strchr(type,
'b') != (
char *) NULL)
3376 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3378 blob_info->type=StandardStream;
3379 blob_info->exempt=MagickTrue;
3380 return(SetStreamBuffering(image_info,blob_info));
3382 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3383 (IsGeometry(filename+3) != MagickFalse))
3390 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3391 if (blob_info->file_info.file == (FILE *) NULL)
3393 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3394 return(MagickFalse);
3396#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3397 if (strchr(type,
'b') != (
char *) NULL)
3398 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3400 blob_info->type=FileStream;
3401 blob_info->exempt=MagickTrue;
3402 return(SetStreamBuffering(image_info,blob_info));
3404#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3405 if (*filename ==
'|')
3408 fileMode[MagickPathExtent],
3416 (void) signal(SIGPIPE,SIG_IGN);
3420 sanitize_command=SanitizeString(filename+1);
3421 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3422 sanitize_command=DestroyString(sanitize_command);
3423 if (blob_info->file_info.file == (FILE *) NULL)
3425 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3426 return(MagickFalse);
3428 blob_info->type=PipeStream;
3429 blob_info->exempt=MagickTrue;
3430 return(SetStreamBuffering(image_info,blob_info));
3433 status=GetPathAttributes(filename,&blob_info->properties);
3434#if defined(S_ISFIFO)
3435 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3437 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3438 if (blob_info->file_info.file == (FILE *) NULL)
3440 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3441 return(MagickFalse);
3443 blob_info->type=FileStream;
3444 blob_info->exempt=MagickTrue;
3445 return(SetStreamBuffering(image_info,blob_info));
3448 GetPathComponent(image->filename,ExtensionPath,extension);
3451 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3452 if ((image_info->adjoin == MagickFalse) ||
3453 (strchr(filename,
'%') != (
char *) NULL))
3458 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3459 image->scene,filename,exception);
3460 if ((LocaleCompare(filename,image->filename) == 0) &&
3461 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3462 (GetNextImageInList(image) != (Image *) NULL)))
3465 path[MagickPathExtent];
3467 GetPathComponent(image->filename,RootPath,path);
3468 if (*extension ==
'\0')
3469 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.17g",
3470 path,(
double) image->scene);
3472 (
void) FormatLocaleString(filename,MagickPathExtent,
3473 "%s-%.17g.%s",path,(
double) image->scene,extension);
3475 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3477 if (IsPathAuthorized(rights,filename) == MagickFalse)
3478 ThrowPolicyException(filename,MagickFalse);
3480 if (image_info->file != (FILE *) NULL)
3482 blob_info->file_info.file=image_info->file;
3483 blob_info->type=FileStream;
3484 blob_info->exempt=MagickTrue;
3492 blob_info->file_info.file=(FILE *) NULL;
3493 file=open_utf8(filename,flags,0);
3495 blob_info->file_info.file=fdopen(file,type);
3496 if (blob_info->file_info.file != (FILE *) NULL)
3504 blob_info->type=FileStream;
3505 (void) SetStreamBuffering(image_info,blob_info);
3506 (void) memset(magick,0,
sizeof(magick));
3507 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3508 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3509#if defined(MAGICKCORE_POSIX_SUPPORT)
3510 (void) fflush(blob_info->file_info.file);
3512 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3513 " read %.17g magic header bytes",(
double) count);
3514#if defined(MAGICKCORE_ZLIB_DELEGATE)
3515 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3516 ((
int) magick[2] == 0x08))
3519 gzfile = gzopen_utf8(filename,
"rb");
3521 if (gzfile != (gzFile) NULL)
3523 if (blob_info->file_info.file != (FILE *) NULL)
3524 (void) fclose(blob_info->file_info.file);
3525 blob_info->file_info.file=(FILE *) NULL;
3526 blob_info->file_info.gzfile=gzfile;
3527 blob_info->type=ZipStream;
3531#if defined(MAGICKCORE_BZLIB_DELEGATE)
3532 if (strncmp((
char *) magick,
"BZh",3) == 0)
3535 *bzfile = BZ2_bzopen(filename,
"r");
3537 if (bzfile != (BZFILE *) NULL)
3539 if (blob_info->file_info.file != (FILE *) NULL)
3540 (void) fclose(blob_info->file_info.file);
3541 blob_info->file_info.file=(FILE *) NULL;
3542 blob_info->file_info.bzfile=bzfile;
3543 blob_info->type=BZipStream;
3547 if (blob_info->type == FileStream)
3558 sans_exception=AcquireExceptionInfo();
3559 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3560 sans_exception=DestroyExceptionInfo(sans_exception);
3561 length=(size_t) blob_info->properties.st_size;
3562 if ((magick_info != (
const MagickInfo *) NULL) &&
3563 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3564 (AcquireMagickResource(MapResource,length) != MagickFalse))
3569 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3571 if (blob == (
void *) NULL)
3572 RelinquishMagickResource(MapResource,length);
3578 if (image_info->file != (FILE *) NULL)
3579 blob_info->exempt=MagickFalse;
3582 (void) fclose(blob_info->file_info.file);
3583 blob_info->file_info.file=(FILE *) NULL;
3585 AttachBlob(blob_info,blob,length);
3586 blob_info->mapped=MagickTrue;
3593#if defined(MAGICKCORE_ZLIB_DELEGATE)
3594 if ((LocaleCompare(extension,
"gz") == 0) ||
3595 (LocaleCompare(extension,
"wmz") == 0) ||
3596 (LocaleCompare(extension,
"svgz") == 0))
3598 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3599 if (blob_info->file_info.gzfile != (gzFile) NULL)
3600 blob_info->type=ZipStream;
3604#if defined(MAGICKCORE_BZLIB_DELEGATE)
3605 if (LocaleCompare(extension,
"bz2") == 0)
3607 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3608 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3609 blob_info->type=BZipStream;
3617 blob_info->file_info.file=(FILE *) NULL;
3618 file=open_utf8(filename,flags,P_MODE);
3620 blob_info->file_info.file=fdopen(file,type);
3621 if (blob_info->file_info.file != (FILE *) NULL)
3623 blob_info->type=FileStream;
3624 (void) SetStreamBuffering(image_info,blob_info);
3627 if (IsPathAuthorized(rights,filename) == MagickFalse)
3628 ThrowPolicyException(filename,MagickFalse);
3629 blob_info->status=0;
3630 blob_info->error_number=0;
3631 if (blob_info->type != UndefinedStream)
3632 blob_info->size=GetBlobSize(image);
3635 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3636 return(MagickFalse);
3675#if defined(__cplusplus) || defined(c_plusplus)
3679static size_t PingStream(
const Image *magick_unused(image),
3680 const void *magick_unused(pixels),
const size_t columns)
3682 magick_unreferenced(image);
3683 magick_unreferenced(pixels);
3687#if defined(__cplusplus) || defined(c_plusplus)
3691MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3692 const size_t length,ExceptionInfo *exception)
3707 assert(image_info != (ImageInfo *) NULL);
3708 assert(image_info->signature == MagickCoreSignature);
3709 assert(exception != (ExceptionInfo *) NULL);
3710 if (IsEventLogging() != MagickFalse)
3711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3712 image_info->filename);
3713 if ((blob == (
const void *) NULL) || (length == 0))
3715 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3716 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3717 return((Image *) NULL);
3719 ping_info=CloneImageInfo(image_info);
3720 ping_info->blob=(
void *) blob;
3721 ping_info->length=length;
3722 ping_info->ping=MagickTrue;
3723 if (*ping_info->magick ==
'\0')
3724 (void) SetImageInfo(ping_info,0,exception);
3725 magick_info=GetMagickInfo(ping_info->magick,exception);
3726 if (magick_info == (
const MagickInfo *) NULL)
3728 (void) ThrowMagickException(exception,GetMagickModule(),
3729 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3731 ping_info=DestroyImageInfo(ping_info);
3732 return((Image *) NULL);
3734 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3737 filename[MagickPathExtent];
3742 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3743 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3744 ping_info->magick,filename);
3745 image=ReadStream(ping_info,&PingStream,exception);
3746 if (image != (Image *) NULL)
3747 (void) DetachBlob(image->blob);
3748 ping_info=DestroyImageInfo(ping_info);
3754 ping_info->blob=(
void *) NULL;
3755 ping_info->length=0;
3756 *ping_info->filename=
'\0';
3757 status=BlobToFile(ping_info->filename,blob,length,exception);
3758 if (status == MagickFalse)
3760 (void) RelinquishUniqueFileResource(ping_info->filename);
3761 ping_info=DestroyImageInfo(ping_info);
3762 return((Image *) NULL);
3764 clone_info=CloneImageInfo(ping_info);
3765 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3766 ping_info->magick,ping_info->filename);
3767 image=ReadStream(clone_info,&PingStream,exception);
3768 if (image != (Image *) NULL)
3776 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3778 (void) CopyMagickString(images->filename,image_info->filename,
3780 (void) CopyMagickString(images->magick_filename,image_info->filename,
3782 (void) CopyMagickString(images->magick,magick_info->name,
3784 images=GetNextImageInList(images);
3787 clone_info=DestroyImageInfo(clone_info);
3788 (void) RelinquishUniqueFileResource(ping_info->filename);
3789 ping_info=DestroyImageInfo(ping_info);
3824MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3827 *magick_restrict blob_info;
3838 assert(image != (Image *) NULL);
3839 assert(image->signature == MagickCoreSignature);
3840 assert(image->blob != (BlobInfo *) NULL);
3841 assert(image->blob->type != UndefinedStream);
3844 assert(data != (
void *) NULL);
3845 blob_info=image->blob;
3847 q=(
unsigned char *) data;
3848 switch (blob_info->type)
3850 case UndefinedStream:
3852 case StandardStream:
3860 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3865 c=getc(blob_info->file_info.file);
3868 *q++=(
unsigned char) c;
3874 c=getc(blob_info->file_info.file);
3877 *q++=(
unsigned char) c;
3883 c=getc(blob_info->file_info.file);
3886 *q++=(
unsigned char) c;
3892 c=getc(blob_info->file_info.file);
3895 *q++=(
unsigned char) c;
3902 if ((count != (ssize_t) length) &&
3903 (ferror(blob_info->file_info.file) != 0))
3904 ThrowBlobException(blob_info);
3909#if defined(MAGICKCORE_ZLIB_DELEGATE)
3920 for (i=0; i < length; i+=(size_t) count)
3922 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3923 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3936 c=gzgetc(blob_info->file_info.gzfile);
3939 *q++=(
unsigned char) c;
3945 c=gzgetc(blob_info->file_info.gzfile);
3948 *q++=(
unsigned char) c;
3954 c=gzgetc(blob_info->file_info.gzfile);
3957 *q++=(
unsigned char) c;
3963 c=gzgetc(blob_info->file_info.gzfile);
3966 *q++=(
unsigned char) c;
3973 (void) gzerror(blob_info->file_info.gzfile,&status);
3974 if ((count != (ssize_t) length) && (status != Z_OK))
3975 ThrowBlobException(blob_info);
3976 if (blob_info->eof == MagickFalse)
3977 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3984#if defined(MAGICKCORE_BZLIB_DELEGATE)
3991 for (i=0; i < length; i+=(size_t) count)
3993 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
3994 MagickMin(length-i,MagickMaxBufferExtent));
4004 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4005 if ((count != (ssize_t) length) && (status != BZ_OK))
4006 ThrowBlobException(blob_info);
4017 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4019 blob_info->eof=MagickTrue;
4022 p=blob_info->data+blob_info->offset;
4023 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4024 blob_info->length-blob_info->offset);
4025 blob_info->offset+=count;
4026 if (count != (ssize_t) length)
4027 blob_info->eof=MagickTrue;
4028 (void) memcpy(q,p,(
size_t) count);
4033 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4034 count=blob_info->custom_stream->reader(q,length,
4035 blob_info->custom_stream->data);
4064MagickExport
int ReadBlobByte(Image *image)
4067 *magick_restrict blob_info;
4072 assert(image != (Image *) NULL);
4073 assert(image->signature == MagickCoreSignature);
4074 assert(image->blob != (BlobInfo *) NULL);
4075 assert(image->blob->type != UndefinedStream);
4076 blob_info=image->blob;
4077 switch (blob_info->type)
4079 case StandardStream:
4083 c=getc(blob_info->file_info.file);
4086 if (ferror(blob_info->file_info.file) != 0)
4087 ThrowBlobException(blob_info);
4094 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4096 blob_info->eof=MagickTrue;
4099 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4100 blob_info->offset++;
4111 count=ReadBlob(image,1,buffer);
4144MagickExport
double ReadBlobDouble(Image *image)
4155 quantum.double_value=0.0;
4156 quantum.unsigned_value=ReadBlobLongLong(image);
4157 return(quantum.double_value);
4183MagickExport
float ReadBlobFloat(Image *image)
4194 quantum.float_value=0.0;
4195 quantum.unsigned_value=ReadBlobLong(image);
4196 return(quantum.float_value);
4222MagickExport
unsigned int ReadBlobLong(Image *image)
4236 assert(image != (Image *) NULL);
4237 assert(image->signature == MagickCoreSignature);
4239 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4242 if (image->endian == LSBEndian)
4244 value=(
unsigned int) (*p++);
4245 value|=(
unsigned int) (*p++) << 8;
4246 value|=(
unsigned int) (*p++) << 16;
4247 value|=(
unsigned int) (*p++) << 24;
4250 value=(
unsigned int) (*p++) << 24;
4251 value|=(
unsigned int) (*p++) << 16;
4252 value|=(
unsigned int) (*p++) << 8;
4253 value|=(
unsigned int) (*p++);
4280MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4294 assert(image != (Image *) NULL);
4295 assert(image->signature == MagickCoreSignature);
4297 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4299 return(MagickULLConstant(0));
4300 if (image->endian == LSBEndian)
4302 value=(MagickSizeType) (*p++);
4303 value|=(MagickSizeType) (*p++) << 8;
4304 value|=(MagickSizeType) (*p++) << 16;
4305 value|=(MagickSizeType) (*p++) << 24;
4306 value|=(MagickSizeType) (*p++) << 32;
4307 value|=(MagickSizeType) (*p++) << 40;
4308 value|=(MagickSizeType) (*p++) << 48;
4309 value|=(MagickSizeType) (*p++) << 56;
4312 value=(MagickSizeType) (*p++) << 56;
4313 value|=(MagickSizeType) (*p++) << 48;
4314 value|=(MagickSizeType) (*p++) << 40;
4315 value|=(MagickSizeType) (*p++) << 32;
4316 value|=(MagickSizeType) (*p++) << 24;
4317 value|=(MagickSizeType) (*p++) << 16;
4318 value|=(MagickSizeType) (*p++) << 8;
4319 value|=(MagickSizeType) (*p++);
4346MagickExport
unsigned short ReadBlobShort(Image *image)
4360 assert(image != (Image *) NULL);
4361 assert(image->signature == MagickCoreSignature);
4363 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4365 return((
unsigned short) 0U);
4366 if (image->endian == LSBEndian)
4368 value=(
unsigned short) (*p++);
4369 value|=(
unsigned short) (*p++) << 8;
4372 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4373 value|=(
unsigned short) (*p++);
4400MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4414 assert(image != (Image *) NULL);
4415 assert(image->signature == MagickCoreSignature);
4417 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4420 value=(
unsigned int) (*p++);
4421 value|=(
unsigned int) (*p++) << 8;
4422 value|=(
unsigned int) (*p++) << 16;
4423 value|=(
unsigned int) (*p++) << 24;
4450MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4461 quantum.unsigned_value=ReadBlobLSBLong(image);
4462 return(quantum.signed_value);
4488MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4502 assert(image != (Image *) NULL);
4503 assert(image->signature == MagickCoreSignature);
4505 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4507 return((
unsigned short) 0U);
4508 value=(
unsigned short) (*p++);
4509 value|=(
unsigned short) (*p++) << 8;
4536MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4547 quantum.unsigned_value=ReadBlobLSBShort(image);
4548 return(quantum.signed_value);
4574MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4588 assert(image != (Image *) NULL);
4589 assert(image->signature == MagickCoreSignature);
4591 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4594 value=(
unsigned int) (*p++) << 24;
4595 value|=(
unsigned int) (*p++) << 16;
4596 value|=(
unsigned int) (*p++) << 8;
4597 value|=(
unsigned int) (*p++);
4624MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4638 assert(image != (Image *) NULL);
4639 assert(image->signature == MagickCoreSignature);
4641 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4643 return(MagickULLConstant(0));
4644 value=(MagickSizeType) (*p++) << 56;
4645 value|=(MagickSizeType) (*p++) << 48;
4646 value|=(MagickSizeType) (*p++) << 40;
4647 value|=(MagickSizeType) (*p++) << 32;
4648 value|=(MagickSizeType) (*p++) << 24;
4649 value|=(MagickSizeType) (*p++) << 16;
4650 value|=(MagickSizeType) (*p++) << 8;
4651 value|=(MagickSizeType) (*p++);
4678MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4692 assert(image != (Image *) NULL);
4693 assert(image->signature == MagickCoreSignature);
4695 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4697 return((
unsigned short) 0U);
4698 value=(
unsigned short) ((*p++) << 8);
4699 value|=(
unsigned short) (*p++);
4700 return((
unsigned short) (value & 0xffff));
4726MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4737 quantum.unsigned_value=ReadBlobMSBLong(image);
4738 return(quantum.signed_value);
4764MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4775 quantum.unsigned_value=ReadBlobMSBShort(image);
4776 return(quantum.signed_value);
4802MagickExport
signed int ReadBlobSignedLong(Image *image)
4813 quantum.unsigned_value=ReadBlobLong(image);
4814 return(quantum.signed_value);
4840MagickExport
signed short ReadBlobSignedShort(Image *image)
4851 quantum.unsigned_value=ReadBlobShort(image);
4852 return(quantum.signed_value);
4890MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4891 const size_t length,
void *magick_restrict data,ssize_t *count)
4894 *magick_restrict blob_info;
4896 assert(image != (Image *) NULL);
4897 assert(image->signature == MagickCoreSignature);
4898 assert(image->blob != (BlobInfo *) NULL);
4899 assert(image->blob->type != UndefinedStream);
4900 assert(count != (ssize_t *) NULL);
4901 blob_info=image->blob;
4902 if (blob_info->type != BlobStream)
4904 assert(data != NULL);
4905 *count=ReadBlob(image,length,(
unsigned char *) data);
4908 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4911 blob_info->eof=MagickTrue;
4914 data=blob_info->data+blob_info->offset;
4915 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4916 blob_info->length-blob_info->offset);
4917 blob_info->offset+=(*count);
4918 if (*count != (ssize_t) length)
4919 blob_info->eof=MagickTrue;
4948MagickExport
char *ReadBlobString(Image *image,
char *
string)
4951 *magick_restrict blob_info;
4959 assert(image != (Image *) NULL);
4960 assert(image->signature == MagickCoreSignature);
4961 assert(image->blob != (BlobInfo *) NULL);
4962 assert(image->blob->type != UndefinedStream);
4963 if (IsEventLogging() != MagickFalse)
4964 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4966 blob_info=image->blob;
4967 switch (blob_info->type)
4969 case UndefinedStream:
4971 case StandardStream:
4974 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4975 if (p == (
char *) NULL)
4977 if (ferror(blob_info->file_info.file) != 0)
4978 ThrowBlobException(blob_info);
4979 return((
char *) NULL);
4986#if defined(MAGICKCORE_ZLIB_DELEGATE)
4987 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4988 if (p == (
char *) NULL)
4991 (void) gzerror(blob_info->file_info.gzfile,&status);
4993 ThrowBlobException(blob_info);
4994 return((
char *) NULL);
5004 c=ReadBlobByte(image);
5007 blob_info->eof=MagickTrue;
5010 string[i++]=(char) c;
5013 }
while (i < (MaxTextExtent-2));
5021 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5024 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5026 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5027 return((
char *) NULL);
5054MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5056 assert(blob != (BlobInfo *) NULL);
5057 assert(blob->signature == MagickCoreSignature);
5058 if (IsEventLogging() != MagickFalse)
5059 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5060 LockSemaphoreInfo(blob->semaphore);
5061 blob->reference_count++;
5062 UnlockSemaphoreInfo(blob->semaphore);
5099MagickExport MagickOffsetType SeekBlob(Image *image,
5100 const MagickOffsetType offset,
const int whence)
5103 *magick_restrict blob_info;
5105 assert(image != (Image *) NULL);
5106 assert(image->signature == MagickCoreSignature);
5107 assert(image->blob != (BlobInfo *) NULL);
5108 assert(image->blob->type != UndefinedStream);
5109 if (IsEventLogging() != MagickFalse)
5110 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5111 blob_info=image->blob;
5112 switch (blob_info->type)
5114 case UndefinedStream:
5116 case StandardStream:
5121 if ((offset < 0) && (whence == SEEK_SET))
5123 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5125 blob_info->offset=TellBlob(image);
5130#if defined(MAGICKCORE_ZLIB_DELEGATE)
5131 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5134 blob_info->offset=TellBlob(image);
5150 blob_info->offset=offset;
5155 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5156 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5161 if ((blob_info->offset+offset) < 0)
5163 blob_info->offset+=offset;
5168 if (((MagickOffsetType) blob_info->length+offset) < 0)
5170 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5174 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5176 blob_info->eof=MagickFalse;
5183 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5185 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5186 blob_info->custom_stream->data);
5190 return(blob_info->offset);
5218MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5220 assert(image != (
const Image *) NULL);
5221 assert(image->signature == MagickCoreSignature);
5222 if (IsEventLogging() != MagickFalse)
5223 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5224 image->blob->exempt=exempt;
5253MagickExport MagickBooleanType SetBlobExtent(Image *image,
5254 const MagickSizeType extent)
5257 *magick_restrict blob_info;
5259 assert(image != (Image *) NULL);
5260 assert(image->signature == MagickCoreSignature);
5261 assert(image->blob != (BlobInfo *) NULL);
5262 assert(image->blob->type != UndefinedStream);
5263 if (IsEventLogging() != MagickFalse)
5264 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5265 blob_info=image->blob;
5266 switch (blob_info->type)
5268 case UndefinedStream:
5270 case StandardStream:
5271 return(MagickFalse);
5280 if (extent != (MagickSizeType) ((off_t) extent))
5281 return(MagickFalse);
5282 offset=SeekBlob(image,0,SEEK_END);
5284 return(MagickFalse);
5285 if ((MagickSizeType) offset >= extent)
5287 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5290 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5291 blob_info->file_info.file);
5292#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5293 if (blob_info->synchronize != MagickFalse)
5298 file=fileno(blob_info->file_info.file);
5299 if ((file == -1) || (offset < 0))
5300 return(MagickFalse);
5301 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5304 offset=SeekBlob(image,offset,SEEK_SET);
5306 return(MagickFalse);
5311 return(MagickFalse);
5313 return(MagickFalse);
5315 return(MagickFalse);
5318 if (extent != (MagickSizeType) ((
size_t) extent))
5319 return(MagickFalse);
5320 if (blob_info->mapped != MagickFalse)
5328 (void) UnmapBlob(blob_info->data,blob_info->length);
5329 RelinquishMagickResource(MapResource,blob_info->length);
5330 if (extent != (MagickSizeType) ((off_t) extent))
5331 return(MagickFalse);
5332 offset=SeekBlob(image,0,SEEK_END);
5334 return(MagickFalse);
5335 if ((MagickSizeType) offset >= extent)
5337 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5338 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5339 blob_info->file_info.file);
5340#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5341 if (blob_info->synchronize != MagickFalse)
5346 file=fileno(blob_info->file_info.file);
5347 if ((file == -1) || (offset < 0))
5348 return(MagickFalse);
5349 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5353 offset=SeekBlob(image,offset,SEEK_SET);
5355 return(MagickFalse);
5356 (void) AcquireMagickResource(MapResource,extent);
5357 blob_info->data=(
unsigned char*) MapBlob(fileno(
5358 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5359 blob_info->extent=(size_t) extent;
5360 blob_info->length=(size_t) extent;
5361 (void) SyncBlob(image);
5364 blob_info->extent=(size_t) extent;
5365 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5366 blob_info->extent+1,
sizeof(*blob_info->data));
5367 (void) SyncBlob(image);
5368 if (blob_info->data == (
unsigned char *) NULL)
5370 (void) DetachBlob(blob_info);
5371 return(MagickFalse);
5405MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5408 assert(custom_stream != (CustomStreamInfo *) NULL);
5409 assert(custom_stream->signature == MagickCoreSignature);
5410 custom_stream->data=data;
5438MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5439 CustomStreamHandler reader)
5441 assert(custom_stream != (CustomStreamInfo *) NULL);
5442 assert(custom_stream->signature == MagickCoreSignature);
5443 custom_stream->reader=reader;
5471MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5472 CustomStreamSeeker seeker)
5474 assert(custom_stream != (CustomStreamInfo *) NULL);
5475 assert(custom_stream->signature == MagickCoreSignature);
5476 custom_stream->seeker=seeker;
5504MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5505 CustomStreamTeller teller)
5507 assert(custom_stream != (CustomStreamInfo *) NULL);
5508 assert(custom_stream->signature == MagickCoreSignature);
5509 custom_stream->teller=teller;
5537MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5538 CustomStreamHandler writer)
5540 assert(custom_stream != (CustomStreamInfo *) NULL);
5541 assert(custom_stream->signature == MagickCoreSignature);
5542 custom_stream->writer=writer;
5569static int SyncBlob(
const Image *image)
5572 *magick_restrict blob_info;
5577 assert(image != (Image *) NULL);
5578 assert(image->signature == MagickCoreSignature);
5579 assert(image->blob != (BlobInfo *) NULL);
5580 if (IsEventLogging() != MagickFalse)
5581 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5582 if (EOFBlob(image) != 0)
5584 blob_info=image->blob;
5586 switch (blob_info->type)
5588 case UndefinedStream:
5589 case StandardStream:
5594 status=fflush(blob_info->file_info.file);
5599#if defined(MAGICKCORE_ZLIB_DELEGATE)
5600 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5606#if defined(MAGICKCORE_BZLIB_DELEGATE)
5607 status=BZ2_bzflush(blob_info->file_info.bzfile);
5643MagickExport MagickOffsetType TellBlob(
const Image *image)
5646 *magick_restrict blob_info;
5651 assert(image != (Image *) NULL);
5652 assert(image->signature == MagickCoreSignature);
5653 assert(image->blob != (BlobInfo *) NULL);
5654 assert(image->blob->type != UndefinedStream);
5655 if (IsEventLogging() != MagickFalse)
5656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5657 blob_info=image->blob;
5659 switch (blob_info->type)
5661 case UndefinedStream:
5662 case StandardStream:
5666 offset=ftell(blob_info->file_info.file);
5673#if defined(MAGICKCORE_ZLIB_DELEGATE)
5674 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5684 offset=blob_info->offset;
5689 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5690 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5722MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5724#if defined(MAGICKCORE_HAVE_MMAP)
5728 status=munmap(map,length);
5729 return(status == -1 ? MagickFalse : MagickTrue);
5733 return(MagickFalse);
5765MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5769 *magick_restrict blob_info;
5783 assert(image != (Image *) NULL);
5784 assert(image->signature == MagickCoreSignature);
5785 assert(image->blob != (BlobInfo *) NULL);
5786 assert(image->blob->type != UndefinedStream);
5789 assert(data != (
const void *) NULL);
5790 blob_info=image->blob;
5792 p=(
const unsigned char *) data;
5793 q=(
unsigned char *) data;
5794 switch (blob_info->type)
5796 case UndefinedStream:
5798 case StandardStream:
5806 count=(ssize_t) fwrite((
const char *) data,1,length,
5807 blob_info->file_info.file);
5812 c=putc((
int) *p++,blob_info->file_info.file);
5820 c=putc((
int) *p++,blob_info->file_info.file);
5828 c=putc((
int) *p++,blob_info->file_info.file);
5836 c=putc((
int) *p++,blob_info->file_info.file);
5845 if ((count != (ssize_t) length) &&
5846 (ferror(blob_info->file_info.file) != 0))
5847 ThrowBlobException(blob_info);
5852#if defined(MAGICKCORE_ZLIB_DELEGATE)
5863 for (i=0; i < length; i+=(size_t) count)
5865 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5866 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5879 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5887 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5895 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5903 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5913 (void) gzerror(blob_info->file_info.gzfile,&status);
5914 if ((count != (ssize_t) length) && (status != Z_OK))
5915 ThrowBlobException(blob_info);
5921#if defined(MAGICKCORE_BZLIB_DELEGATE)
5928 for (i=0; i < length; i+=(size_t) count)
5930 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5931 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5941 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5942 if ((count != (ssize_t) length) && (status != BZ_OK))
5943 ThrowBlobException(blob_info);
5949 count=(ssize_t) blob_info->stream(image,data,length);
5957 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5962 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5963 if (extent >= blob_info->extent)
5965 extent+=blob_info->quantum+length;
5966 blob_info->quantum<<=1;
5967 if (SetBlobExtent(image,extent) == MagickFalse)
5970 q=blob_info->data+blob_info->offset;
5971 (void) memcpy(q,p,length);
5972 blob_info->offset+=(MagickOffsetType) length;
5973 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5974 blob_info->length=(size_t) blob_info->offset;
5975 count=(ssize_t) length;
5980 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
5981 count=blob_info->custom_stream->writer((
unsigned char *) data,
5982 length,blob_info->custom_stream->data);
6014MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6017 *magick_restrict blob_info;
6022 assert(image != (Image *) NULL);
6023 assert(image->signature == MagickCoreSignature);
6024 assert(image->blob != (BlobInfo *) NULL);
6025 assert(image->blob->type != UndefinedStream);
6026 blob_info=image->blob;
6028 switch (blob_info->type)
6030 case StandardStream:
6037 c=putc((
int) value,blob_info->file_info.file);
6040 if (ferror(blob_info->file_info.file) != 0)
6041 ThrowBlobException(blob_info);
6049 count=WriteBlobStream(image,1,&value);
6081MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6092 quantum.unsigned_value=0U;
6093 quantum.float_value=value;
6094 return(WriteBlobLong(image,quantum.unsigned_value));
6122MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6127 assert(image != (Image *) NULL);
6128 assert(image->signature == MagickCoreSignature);
6129 if (image->endian == LSBEndian)
6131 buffer[0]=(
unsigned char) value;
6132 buffer[1]=(
unsigned char) (value >> 8);
6133 buffer[2]=(
unsigned char) (value >> 16);
6134 buffer[3]=(
unsigned char) (value >> 24);
6135 return(WriteBlobStream(image,4,buffer));
6137 buffer[0]=(
unsigned char) (value >> 24);
6138 buffer[1]=(
unsigned char) (value >> 16);
6139 buffer[2]=(
unsigned char) (value >> 8);
6140 buffer[3]=(
unsigned char) value;
6141 return(WriteBlobStream(image,4,buffer));
6169MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6174 assert(image != (Image *) NULL);
6175 assert(image->signature == MagickCoreSignature);
6176 if (image->endian == LSBEndian)
6178 buffer[0]=(
unsigned char) value;
6179 buffer[1]=(
unsigned char) (value >> 8);
6180 buffer[2]=(
unsigned char) (value >> 16);
6181 buffer[3]=(
unsigned char) (value >> 24);
6182 buffer[4]=(
unsigned char) (value >> 32);
6183 buffer[5]=(
unsigned char) (value >> 40);
6184 buffer[6]=(
unsigned char) (value >> 48);
6185 buffer[7]=(
unsigned char) (value >> 56);
6186 return(WriteBlobStream(image,8,buffer));
6188 buffer[0]=(
unsigned char) (value >> 56);
6189 buffer[1]=(
unsigned char) (value >> 48);
6190 buffer[2]=(
unsigned char) (value >> 40);
6191 buffer[3]=(
unsigned char) (value >> 32);
6192 buffer[4]=(
unsigned char) (value >> 24);
6193 buffer[5]=(
unsigned char) (value >> 16);
6194 buffer[6]=(
unsigned char) (value >> 8);
6195 buffer[7]=(
unsigned char) value;
6196 return(WriteBlobStream(image,8,buffer));
6224MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6229 assert(image != (Image *) NULL);
6230 assert(image->signature == MagickCoreSignature);
6231 if (image->endian == LSBEndian)
6233 buffer[0]=(
unsigned char) value;
6234 buffer[1]=(
unsigned char) (value >> 8);
6235 return(WriteBlobStream(image,2,buffer));
6237 buffer[0]=(
unsigned char) (value >> 8);
6238 buffer[1]=(
unsigned char) value;
6239 return(WriteBlobStream(image,2,buffer));
6267MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6281 assert(image != (Image *) NULL);
6282 assert(image->signature == MagickCoreSignature);
6283 quantum.signed_value=value;
6284 if (image->endian == LSBEndian)
6286 buffer[0]=(
unsigned char) quantum.unsigned_value;
6287 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6288 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6289 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6290 return(WriteBlobStream(image,4,buffer));
6292 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6293 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6294 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6295 buffer[3]=(
unsigned char) quantum.unsigned_value;
6296 return(WriteBlobStream(image,4,buffer));
6324MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6329 assert(image != (Image *) NULL);
6330 assert(image->signature == MagickCoreSignature);
6331 buffer[0]=(
unsigned char) value;
6332 buffer[1]=(
unsigned char) (value >> 8);
6333 buffer[2]=(
unsigned char) (value >> 16);
6334 buffer[3]=(
unsigned char) (value >> 24);
6335 return(WriteBlobStream(image,4,buffer));
6363MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6368 assert(image != (Image *) NULL);
6369 assert(image->signature == MagickCoreSignature);
6370 buffer[0]=(
unsigned char) value;
6371 buffer[1]=(
unsigned char) (value >> 8);
6372 return(WriteBlobStream(image,2,buffer));
6400MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6414 assert(image != (Image *) NULL);
6415 assert(image->signature == MagickCoreSignature);
6416 quantum.signed_value=value;
6417 buffer[0]=(
unsigned char) quantum.unsigned_value;
6418 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6419 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6420 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6421 return(WriteBlobStream(image,4,buffer));
6449MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6450 const signed short value)
6464 assert(image != (Image *) NULL);
6465 assert(image->signature == MagickCoreSignature);
6466 quantum.signed_value=value;
6467 buffer[0]=(
unsigned char) quantum.unsigned_value;
6468 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6469 return(WriteBlobStream(image,2,buffer));
6497MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6502 assert(image != (Image *) NULL);
6503 assert(image->signature == MagickCoreSignature);
6504 buffer[0]=(
unsigned char) (value >> 24);
6505 buffer[1]=(
unsigned char) (value >> 16);
6506 buffer[2]=(
unsigned char) (value >> 8);
6507 buffer[3]=(
unsigned char) value;
6508 return(WriteBlobStream(image,4,buffer));
6536MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6537 const signed short value)
6551 assert(image != (Image *) NULL);
6552 assert(image->signature == MagickCoreSignature);
6553 quantum.signed_value=value;
6554 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6555 buffer[1]=(
unsigned char) quantum.unsigned_value;
6556 return(WriteBlobStream(image,2,buffer));
6584MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6589 assert(image != (Image *) NULL);
6590 assert(image->signature == MagickCoreSignature);
6591 buffer[0]=(
unsigned char) (value >> 8);
6592 buffer[1]=(
unsigned char) value;
6593 return(WriteBlobStream(image,2,buffer));
6621MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6623 assert(image != (Image *) NULL);
6624 assert(image->signature == MagickCoreSignature);
6625 assert(
string != (
const char *) NULL);
6626 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));