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 SyncBlobStream(blob_info,image,image);
2104 *length=image->blob->length;
2105 blob=DetachBlob(image->blob);
2106 if (blob != (
void *) NULL)
2108 if (status == MagickFalse)
2109 blob=RelinquishMagickMemory(blob);
2111 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2114 if ((status == MagickFalse) && (image->blob->extent == 0))
2115 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2121 unique[MagickPathExtent];
2129 file=AcquireUniqueFileResource(unique);
2132 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2133 image_info->filename);
2137 blob_info->file=fdopen(file,
"wb");
2138 if (blob_info->file != (FILE *) NULL)
2140 (void) FormatLocaleString(image->filename,MagickPathExtent,
2141 "%s:%s",image->magick,unique);
2142 status=WriteImage(blob_info,image,exception);
2143 (void) fclose(blob_info->file);
2144 if (status != MagickFalse)
2145 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2147 (void) RelinquishUniqueFileResource(unique);
2150 blob_info=DestroyImageInfo(blob_info);
2182MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2183 ExceptionInfo *exception)
2195 assert(image_info != (
const ImageInfo *) NULL);
2196 assert(image_info->signature == MagickCoreSignature);
2197 assert(image != (Image *) NULL);
2198 assert(image->signature == MagickCoreSignature);
2199 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2200 assert(image_info->custom_stream->signature == MagickCoreSignature);
2201 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2202 assert(exception != (ExceptionInfo *) NULL);
2203 if (IsEventLogging() != MagickFalse)
2204 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2205 image_info->filename);
2206 clone_info=CloneImageInfo(image_info);
2207 clone_info->adjoin=MagickFalse;
2208 (void) SetImageInfo(clone_info,1,exception);
2209 if (*clone_info->magick !=
'\0')
2210 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2211 magick_info=GetMagickInfo(image->magick,exception);
2212 if (magick_info == (
const MagickInfo *) NULL)
2214 (void) ThrowMagickException(exception,GetMagickModule(),
2215 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2217 clone_info=DestroyImageInfo(clone_info);
2220 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2221 blob_support=GetMagickBlobSupport(magick_info);
2222 if ((blob_support != MagickFalse) &&
2223 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2225 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2226 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2227 blob_support=MagickFalse;
2229 if (blob_support != MagickFalse)
2234 (void) CloseBlob(image);
2235 *image->filename=
'\0';
2236 (void) WriteImage(clone_info,image,exception);
2241 unique[MagickPathExtent];
2252 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2253 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2255 if (blob == (
unsigned char *) NULL)
2257 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2258 image_info->filename);
2259 clone_info=DestroyImageInfo(clone_info);
2262 file=AcquireUniqueFileResource(unique);
2265 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2266 image_info->filename);
2267 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2268 clone_info=DestroyImageInfo(clone_info);
2271 clone_info->file=fdopen(file,
"wb+");
2272 if (clone_info->file != (FILE *) NULL)
2277 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2278 image->magick,unique);
2279 status=WriteImage(clone_info,image,exception);
2280 if (status != MagickFalse)
2282 (void) fseek(clone_info->file,0,SEEK_SET);
2283 count=(ssize_t) MagickMaxBufferExtent;
2284 while (count == (ssize_t) MagickMaxBufferExtent)
2286 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2288 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2289 image_info->custom_stream->data);
2292 (void) fclose(clone_info->file);
2294 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2295 (void) RelinquishUniqueFileResource(unique);
2297 clone_info=DestroyImageInfo(clone_info);
2328MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2329 ExceptionInfo *exception)
2353 assert(image != (Image *) NULL);
2354 assert(image->signature == MagickCoreSignature);
2355 assert(image->blob != (BlobInfo *) NULL);
2356 assert(image->blob->type != UndefinedStream);
2357 assert(filename != (
const char *) NULL);
2358 if (IsEventLogging() != MagickFalse)
2359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2360 if (*filename ==
'\0')
2361 file=AcquireUniqueFileResource(filename);
2363 if (LocaleCompare(filename,
"-") == 0)
2364 file=fileno(stdout);
2366 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2369 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2370 return(MagickFalse);
2372 quantum=(size_t) MagickMaxBufferExtent;
2373 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2374 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2375 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2376 if (buffer == (
unsigned char *) NULL)
2378 file=close_utf8(file)-1;
2379 (void) ThrowMagickException(exception,GetMagickModule(),
2380 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2381 return(MagickFalse);
2384 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2385 for (i=0; count > 0; )
2387 length=(size_t) count;
2388 for (i=0; i < length; i+=(size_t) count)
2390 count=MagickWrite(file,p+i,(
size_t) (length-i));
2396 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2398 if (LocaleCompare(filename,
"-") != 0)
2399 file=close_utf8(file);
2400 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2401 if ((file == -1) || (i < length))
2404 file=close_utf8(file);
2405 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2406 return(MagickFalse);
2446MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2447 size_t *length,ExceptionInfo *exception)
2461 assert(image_info != (
const ImageInfo *) NULL);
2462 assert(image_info->signature == MagickCoreSignature);
2463 assert(images != (Image *) NULL);
2464 assert(images->signature == MagickCoreSignature);
2465 assert(exception != (ExceptionInfo *) NULL);
2466 if (IsEventLogging() != MagickFalse)
2467 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2468 image_info->filename);
2470 blob=(
unsigned char *) NULL;
2471 blob_info=CloneImageInfo(image_info);
2472 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2474 if (*blob_info->magick !=
'\0')
2475 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2476 magick_info=GetMagickInfo(images->magick,exception);
2477 if (magick_info == (
const MagickInfo *) NULL)
2479 (void) ThrowMagickException(exception,GetMagickModule(),
2480 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2482 blob_info=DestroyImageInfo(blob_info);
2485 if (GetMagickAdjoin(magick_info) == MagickFalse)
2487 blob_info=DestroyImageInfo(blob_info);
2488 return(ImageToBlob(image_info,images,length,exception));
2490 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2491 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2496 blob_info->length=0;
2497 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2498 sizeof(
unsigned char));
2499 if (blob_info->blob == (
void *) NULL)
2500 (void) ThrowMagickException(exception,GetMagickModule(),
2501 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2504 (void) CloseBlob(images);
2505 images->blob->exempt=MagickTrue;
2506 images->blob->extent=0;
2507 *images->filename=
'\0';
2508 status=WriteImages(blob_info,images,images->filename,exception);
2509 SyncBlobStream(blob_info,images,images);
2510 *length=images->blob->length;
2511 blob=DetachBlob(images->blob);
2512 if (blob != (
void *) NULL)
2514 if (status == MagickFalse)
2515 blob=RelinquishMagickMemory(blob);
2517 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2519 else if ((status == MagickFalse) && (images->blob->extent == 0))
2520 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2526 filename[MagickPathExtent],
2527 unique[MagickPathExtent];
2535 file=AcquireUniqueFileResource(unique);
2538 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2539 image_info->filename);
2543 blob_info->file=fdopen(file,
"wb");
2544 if (blob_info->file != (FILE *) NULL)
2546 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2547 images->magick,unique);
2548 status=WriteImages(blob_info,images,filename,exception);
2549 (void) fclose(blob_info->file);
2550 if (status != MagickFalse)
2551 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2553 (void) RelinquishUniqueFileResource(unique);
2556 blob_info=DestroyImageInfo(blob_info);
2588MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2589 Image *images,ExceptionInfo *exception)
2601 assert(image_info != (
const ImageInfo *) NULL);
2602 assert(image_info->signature == MagickCoreSignature);
2603 assert(images != (Image *) NULL);
2604 assert(images->signature == MagickCoreSignature);
2605 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2606 assert(image_info->custom_stream->signature == MagickCoreSignature);
2607 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2608 assert(exception != (ExceptionInfo *) NULL);
2609 if (IsEventLogging() != MagickFalse)
2610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2611 image_info->filename);
2612 clone_info=CloneImageInfo(image_info);
2613 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2615 if (*clone_info->magick !=
'\0')
2616 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2617 magick_info=GetMagickInfo(images->magick,exception);
2618 if (magick_info == (
const MagickInfo *) NULL)
2620 (void) ThrowMagickException(exception,GetMagickModule(),
2621 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2623 clone_info=DestroyImageInfo(clone_info);
2626 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2627 blob_support=GetMagickBlobSupport(magick_info);
2628 if ((blob_support != MagickFalse) &&
2629 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2631 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2632 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2633 blob_support=MagickFalse;
2635 if (blob_support != MagickFalse)
2640 (void) CloseBlob(images);
2641 *images->filename=
'\0';
2642 (void) WriteImages(clone_info,images,images->filename,exception);
2647 filename[MagickPathExtent],
2648 unique[MagickPathExtent];
2659 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2660 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2662 if (blob == (
unsigned char *) NULL)
2664 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2665 image_info->filename);
2666 clone_info=DestroyImageInfo(clone_info);
2669 file=AcquireUniqueFileResource(unique);
2672 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2673 image_info->filename);
2674 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2675 clone_info=DestroyImageInfo(clone_info);
2678 clone_info->file=fdopen(file,
"wb+");
2679 if (clone_info->file != (FILE *) NULL)
2684 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2685 images->magick,unique);
2686 status=WriteImages(clone_info,images,filename,exception);
2687 if (status != MagickFalse)
2689 (void) fseek(clone_info->file,0,SEEK_SET);
2690 count=(ssize_t) MagickMaxBufferExtent;
2691 while (count == (ssize_t) MagickMaxBufferExtent)
2693 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2695 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2696 image_info->custom_stream->data);
2699 (void) fclose(clone_info->file);
2701 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2702 (void) RelinquishUniqueFileResource(unique);
2704 clone_info=DestroyImageInfo(clone_info);
2740MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2741 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2744 filename[MagickPathExtent];
2773 assert(image_info != (ImageInfo *) NULL);
2774 assert(image_info->signature == MagickCoreSignature);
2775 assert(image != (Image *) NULL);
2776 assert(image->signature == MagickCoreSignature);
2777 assert(inject_image != (Image *) NULL);
2778 assert(inject_image->signature == MagickCoreSignature);
2779 assert(exception != (ExceptionInfo *) NULL);
2780 if (IsEventLogging() != MagickFalse)
2781 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2782 unique_file=(FILE *) NULL;
2783 file=AcquireUniqueFileResource(filename);
2785 unique_file=fdopen(file,
"wb");
2786 if ((file == -1) || (unique_file == (FILE *) NULL))
2788 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2789 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2791 return(MagickFalse);
2793 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2794 if (byte_image == (Image *) NULL)
2796 (void) fclose(unique_file);
2797 (void) RelinquishUniqueFileResource(filename);
2798 return(MagickFalse);
2800 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2802 DestroyBlob(byte_image);
2803 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2804 write_info=CloneImageInfo(image_info);
2805 SetImageInfoFile(write_info,unique_file);
2806 status=WriteImage(write_info,byte_image,exception);
2807 write_info=DestroyImageInfo(write_info);
2808 byte_image=DestroyImage(byte_image);
2809 (void) fclose(unique_file);
2810 if (status == MagickFalse)
2812 (void) RelinquishUniqueFileResource(filename);
2813 return(MagickFalse);
2818 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2821 (void) RelinquishUniqueFileResource(filename);
2822 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2823 image_info->filename);
2824 return(MagickFalse);
2826 quantum=(size_t) MagickMaxBufferExtent;
2827 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2828 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2829 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2830 if (buffer == (
unsigned char *) NULL)
2832 (void) RelinquishUniqueFileResource(filename);
2833 file=close_utf8(file);
2834 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2839 ssize_t count = read(file,buffer,quantum);
2846 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2849 file=close_utf8(file);
2851 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2852 (void) RelinquishUniqueFileResource(filename);
2853 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2879MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2881 assert(image != (
const Image *) NULL);
2882 assert(image->signature == MagickCoreSignature);
2883 if (IsEventLogging() != MagickFalse)
2884 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2885 return(image->blob->exempt);
2910MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2913 *magick_restrict blob_info;
2915 assert(image != (
const Image *) NULL);
2916 assert(image->signature == MagickCoreSignature);
2917 if (IsEventLogging() != MagickFalse)
2918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2919 blob_info=image->blob;
2920 switch (blob_info->type)
2929 if (blob_info->file_info.file == (FILE *) NULL)
2930 return(MagickFalse);
2931 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2932 return(status == -1 ? MagickFalse : MagickTrue);
2936#if defined(MAGICKCORE_ZLIB_DELEGATE)
2940 if (blob_info->file_info.gzfile == (gzFile) NULL)
2941 return(MagickFalse);
2942 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2943 return(offset < 0 ? MagickFalse : MagickTrue);
2948 case UndefinedStream:
2952 case StandardStream:
2956 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
2957 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
2964 return(MagickFalse);
2989MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2991 assert(image != (
const Image *) NULL);
2992 assert(image->signature == MagickCoreSignature);
2993 if (IsEventLogging() != MagickFalse)
2994 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2995 return(image->blob->temporary);
3027MagickExport
void *MapBlob(
int file,
const MapMode mode,
3028 const MagickOffsetType offset,
const size_t length)
3030#if defined(MAGICKCORE_HAVE_MMAP)
3043#if defined(MAP_ANONYMOUS)
3044 flags|=MAP_ANONYMOUS;
3053 protection=PROT_READ;
3059 protection=PROT_WRITE;
3065 protection=PROT_READ | PROT_WRITE;
3070#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3071 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3073 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3074 if (map == MAP_FAILED)
3075 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3077 if (map == MAP_FAILED)
3114MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3123 assert(buffer != (
unsigned char *) NULL);
3130 *buffer++=(
unsigned char) c;
3134 *buffer++=(
unsigned char) c;
3164MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3172 assert(p != (
unsigned char *) NULL);
3179 *p++=(
unsigned char) c;
3215static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3216 const BlobInfo *blob_info)
3227 size=MagickMinBufferExtent;
3228 option=GetImageOption(image_info,
"stream:buffer-size");
3229 if (option != (
const char *) NULL)
3230 size=StringToUnsignedLong(option);
3231 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3232 _IONBF : _IOFBF,size);
3233 return(status == 0 ? MagickTrue : MagickFalse);
3236#if defined(MAGICKCORE_ZLIB_DELEGATE)
3237static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3239#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3240 return(gzopen(path,mode));
3248 path_wide=NTCreateWidePath(path);
3249 if (path_wide == (
wchar_t *) NULL)
3250 return((gzFile) NULL);
3251 file=gzopen_w(path_wide,mode);
3252 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3258MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3259 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3262 *magick_restrict blob_info;
3265 extension[MagickPathExtent],
3266 filename[MagickPathExtent];
3280 assert(image_info != (ImageInfo *) NULL);
3281 assert(image_info->signature == MagickCoreSignature);
3282 assert(image != (Image *) NULL);
3283 assert(image->signature == MagickCoreSignature);
3284 if (IsEventLogging() != MagickFalse)
3285 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3286 image_info->filename);
3287 blob_info=image->blob;
3288 if (image_info->blob != (
void *) NULL)
3290 if (image_info->stream != (StreamHandler) NULL)
3291 blob_info->stream=(StreamHandler) image_info->stream;
3292 AttachBlob(blob_info,image_info->blob,image_info->length);
3295 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3296 (*image->filename ==
'\0'))
3298 blob_info->type=CustomStream;
3299 blob_info->custom_stream=image_info->custom_stream;
3300 if (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL)
3301 blob_info->custom_stream->seeker(0,SEEK_SET,
3302 blob_info->custom_stream->data);
3305 (void) DetachBlob(blob_info);
3306 blob_info->mode=mode;
3315 case ReadBinaryBlobMode:
3317 flags=O_RDONLY | O_BINARY;
3323 flags=O_WRONLY | O_CREAT | O_TRUNC;
3327 case WriteBinaryBlobMode:
3329 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3333 case AppendBlobMode:
3335 flags=O_WRONLY | O_CREAT | O_APPEND;
3339 case AppendBinaryBlobMode:
3341 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3353 blob_info->synchronize=image_info->synchronize;
3354 if (image_info->stream != (StreamHandler) NULL)
3356 blob_info->stream=image_info->stream;
3359 blob_info->type=FifoStream;
3367 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3368 rights=ReadPolicyRights;
3370 rights=WritePolicyRights;
3371 if (IsPathAuthorized(rights,filename) == MagickFalse)
3372 ThrowPolicyException(filename,MagickFalse);
3373 if ((LocaleCompare(filename,
"-") == 0) ||
3374 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3376 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3377#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3378 if (strchr(type,
'b') != (
char *) NULL)
3379 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3381 blob_info->type=StandardStream;
3382 blob_info->exempt=MagickTrue;
3383 return(SetStreamBuffering(image_info,blob_info));
3385 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3386 (IsGeometry(filename+3) != MagickFalse))
3393 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3394 if (blob_info->file_info.file == (FILE *) NULL)
3396 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3397 return(MagickFalse);
3399#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3400 if (strchr(type,
'b') != (
char *) NULL)
3401 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3403 blob_info->type=FileStream;
3404 blob_info->exempt=MagickTrue;
3405 return(SetStreamBuffering(image_info,blob_info));
3407#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3408 if (*filename ==
'|')
3411 fileMode[MagickPathExtent],
3419 (void) signal(SIGPIPE,SIG_IGN);
3423 sanitize_command=SanitizeString(filename+1);
3424 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3425 sanitize_command=DestroyString(sanitize_command);
3426 if (blob_info->file_info.file == (FILE *) NULL)
3428 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3429 return(MagickFalse);
3431 blob_info->type=PipeStream;
3432 blob_info->exempt=MagickTrue;
3433 return(SetStreamBuffering(image_info,blob_info));
3436 status=GetPathAttributes(filename,&blob_info->properties);
3437#if defined(S_ISFIFO)
3438 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3440 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3441 if (blob_info->file_info.file == (FILE *) NULL)
3443 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3444 return(MagickFalse);
3446 blob_info->type=FileStream;
3447 blob_info->exempt=MagickTrue;
3448 return(SetStreamBuffering(image_info,blob_info));
3451 GetPathComponent(image->filename,ExtensionPath,extension);
3454 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3455 if ((image_info->adjoin == MagickFalse) ||
3456 (strchr(filename,
'%') != (
char *) NULL))
3461 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3462 image->scene,filename,exception);
3463 if ((LocaleCompare(filename,image->filename) == 0) &&
3464 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3465 (GetNextImageInList(image) != (Image *) NULL)))
3468 path[MagickPathExtent];
3470 GetPathComponent(image->filename,RootPath,path);
3471 if (*extension ==
'\0')
3472 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.17g",
3473 path,(
double) image->scene);
3475 (
void) FormatLocaleString(filename,MagickPathExtent,
3476 "%s-%.17g.%s",path,(
double) image->scene,extension);
3478 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3480 if (IsPathAuthorized(rights,filename) == MagickFalse)
3481 ThrowPolicyException(filename,MagickFalse);
3483 if (image_info->file != (FILE *) NULL)
3485 blob_info->file_info.file=image_info->file;
3486 blob_info->type=FileStream;
3487 blob_info->exempt=MagickTrue;
3495 blob_info->file_info.file=(FILE *) NULL;
3496 file=open_utf8(filename,flags,0);
3498 blob_info->file_info.file=fdopen(file,type);
3499 if (blob_info->file_info.file != (FILE *) NULL)
3507 blob_info->type=FileStream;
3508 (void) SetStreamBuffering(image_info,blob_info);
3509 (void) memset(magick,0,
sizeof(magick));
3510 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3511 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3512#if defined(MAGICKCORE_POSIX_SUPPORT)
3513 (void) fflush(blob_info->file_info.file);
3515 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3516 " read %.17g magic header bytes",(
double) count);
3517#if defined(MAGICKCORE_ZLIB_DELEGATE)
3518 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3519 ((
int) magick[2] == 0x08))
3522 gzfile = gzopen_utf8(filename,
"rb");
3524 if (gzfile != (gzFile) NULL)
3526 if (blob_info->file_info.file != (FILE *) NULL)
3527 (void) fclose(blob_info->file_info.file);
3528 blob_info->file_info.file=(FILE *) NULL;
3529 blob_info->file_info.gzfile=gzfile;
3530 blob_info->type=ZipStream;
3534#if defined(MAGICKCORE_BZLIB_DELEGATE)
3535 if (strncmp((
char *) magick,
"BZh",3) == 0)
3538 *bzfile = BZ2_bzopen(filename,
"r");
3540 if (bzfile != (BZFILE *) NULL)
3542 if (blob_info->file_info.file != (FILE *) NULL)
3543 (void) fclose(blob_info->file_info.file);
3544 blob_info->file_info.file=(FILE *) NULL;
3545 blob_info->file_info.bzfile=bzfile;
3546 blob_info->type=BZipStream;
3550 if (blob_info->type == FileStream)
3561 sans_exception=AcquireExceptionInfo();
3562 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3563 sans_exception=DestroyExceptionInfo(sans_exception);
3564 length=(size_t) blob_info->properties.st_size;
3565 if ((magick_info != (
const MagickInfo *) NULL) &&
3566 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3567 (AcquireMagickResource(MapResource,length) != MagickFalse))
3572 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3574 if (blob == (
void *) NULL)
3575 RelinquishMagickResource(MapResource,length);
3581 if (image_info->file != (FILE *) NULL)
3582 blob_info->exempt=MagickFalse;
3585 (void) fclose(blob_info->file_info.file);
3586 blob_info->file_info.file=(FILE *) NULL;
3588 AttachBlob(blob_info,blob,length);
3589 blob_info->mapped=MagickTrue;
3596#if defined(MAGICKCORE_ZLIB_DELEGATE)
3597 if ((LocaleCompare(extension,
"gz") == 0) ||
3598 (LocaleCompare(extension,
"wmz") == 0) ||
3599 (LocaleCompare(extension,
"svgz") == 0))
3601 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3602 if (blob_info->file_info.gzfile != (gzFile) NULL)
3603 blob_info->type=ZipStream;
3607#if defined(MAGICKCORE_BZLIB_DELEGATE)
3608 if (LocaleCompare(extension,
"bz2") == 0)
3610 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3611 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3612 blob_info->type=BZipStream;
3620 blob_info->file_info.file=(FILE *) NULL;
3621 file=open_utf8(filename,flags,P_MODE);
3623 blob_info->file_info.file=fdopen(file,type);
3624 if (blob_info->file_info.file != (FILE *) NULL)
3626 blob_info->type=FileStream;
3627 (void) SetStreamBuffering(image_info,blob_info);
3630 if (IsPathAuthorized(rights,filename) == MagickFalse)
3631 ThrowPolicyException(filename,MagickFalse);
3632 blob_info->status=0;
3633 blob_info->error_number=0;
3634 if (blob_info->type != UndefinedStream)
3635 blob_info->size=GetBlobSize(image);
3638 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3639 return(MagickFalse);
3678#if defined(__cplusplus) || defined(c_plusplus)
3682static size_t PingStream(
const Image *magick_unused(image),
3683 const void *magick_unused(pixels),
const size_t columns)
3685 magick_unreferenced(image);
3686 magick_unreferenced(pixels);
3690#if defined(__cplusplus) || defined(c_plusplus)
3694MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3695 const size_t length,ExceptionInfo *exception)
3710 assert(image_info != (ImageInfo *) NULL);
3711 assert(image_info->signature == MagickCoreSignature);
3712 assert(exception != (ExceptionInfo *) NULL);
3713 if (IsEventLogging() != MagickFalse)
3714 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3715 image_info->filename);
3716 if ((blob == (
const void *) NULL) || (length == 0))
3718 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3719 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3720 return((Image *) NULL);
3722 ping_info=CloneImageInfo(image_info);
3723 ping_info->blob=(
void *) blob;
3724 ping_info->length=length;
3725 ping_info->ping=MagickTrue;
3726 if (*ping_info->magick ==
'\0')
3727 (void) SetImageInfo(ping_info,0,exception);
3728 magick_info=GetMagickInfo(ping_info->magick,exception);
3729 if (magick_info == (
const MagickInfo *) NULL)
3731 (void) ThrowMagickException(exception,GetMagickModule(),
3732 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3734 ping_info=DestroyImageInfo(ping_info);
3735 return((Image *) NULL);
3737 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3740 filename[MagickPathExtent];
3745 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3746 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3747 ping_info->magick,filename);
3748 image=ReadStream(ping_info,&PingStream,exception);
3749 if (image != (Image *) NULL)
3750 (void) DetachBlob(image->blob);
3751 ping_info=DestroyImageInfo(ping_info);
3757 ping_info->blob=(
void *) NULL;
3758 ping_info->length=0;
3759 *ping_info->filename=
'\0';
3760 status=BlobToFile(ping_info->filename,blob,length,exception);
3761 if (status == MagickFalse)
3763 (void) RelinquishUniqueFileResource(ping_info->filename);
3764 ping_info=DestroyImageInfo(ping_info);
3765 return((Image *) NULL);
3767 clone_info=CloneImageInfo(ping_info);
3768 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3769 ping_info->magick,ping_info->filename);
3770 image=ReadStream(clone_info,&PingStream,exception);
3771 if (image != (Image *) NULL)
3779 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3781 (void) CopyMagickString(images->filename,image_info->filename,
3783 (void) CopyMagickString(images->magick_filename,image_info->filename,
3785 (void) CopyMagickString(images->magick,magick_info->name,
3787 images=GetNextImageInList(images);
3790 clone_info=DestroyImageInfo(clone_info);
3791 (void) RelinquishUniqueFileResource(ping_info->filename);
3792 ping_info=DestroyImageInfo(ping_info);
3827MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3830 *magick_restrict blob_info;
3841 assert(image != (Image *) NULL);
3842 assert(image->signature == MagickCoreSignature);
3843 assert(image->blob != (BlobInfo *) NULL);
3844 assert(image->blob->type != UndefinedStream);
3847 assert(data != (
void *) NULL);
3848 blob_info=image->blob;
3850 q=(
unsigned char *) data;
3851 switch (blob_info->type)
3853 case UndefinedStream:
3855 case StandardStream:
3863 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3868 c=getc(blob_info->file_info.file);
3871 *q++=(
unsigned char) c;
3877 c=getc(blob_info->file_info.file);
3880 *q++=(
unsigned char) c;
3886 c=getc(blob_info->file_info.file);
3889 *q++=(
unsigned char) c;
3895 c=getc(blob_info->file_info.file);
3898 *q++=(
unsigned char) c;
3905 if ((count != (ssize_t) length) &&
3906 (ferror(blob_info->file_info.file) != 0))
3907 ThrowBlobException(blob_info);
3912#if defined(MAGICKCORE_ZLIB_DELEGATE)
3923 for (i=0; i < length; i+=(size_t) count)
3925 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3926 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3939 c=gzgetc(blob_info->file_info.gzfile);
3942 *q++=(
unsigned char) c;
3948 c=gzgetc(blob_info->file_info.gzfile);
3951 *q++=(
unsigned char) c;
3957 c=gzgetc(blob_info->file_info.gzfile);
3960 *q++=(
unsigned char) c;
3966 c=gzgetc(blob_info->file_info.gzfile);
3969 *q++=(
unsigned char) c;
3976 (void) gzerror(blob_info->file_info.gzfile,&status);
3977 if ((count != (ssize_t) length) && (status != Z_OK))
3978 ThrowBlobException(blob_info);
3979 if (blob_info->eof == MagickFalse)
3980 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3987#if defined(MAGICKCORE_BZLIB_DELEGATE)
3994 for (i=0; i < length; i+=(size_t) count)
3996 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
3997 MagickMin(length-i,MagickMaxBufferExtent));
4007 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4008 if ((count != (ssize_t) length) && (status != BZ_OK))
4009 ThrowBlobException(blob_info);
4020 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4022 blob_info->eof=MagickTrue;
4025 p=blob_info->data+blob_info->offset;
4026 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4027 blob_info->length-blob_info->offset);
4028 blob_info->offset+=count;
4029 if (count != (ssize_t) length)
4030 blob_info->eof=MagickTrue;
4031 (void) memcpy(q,p,(
size_t) count);
4036 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4038 count=blob_info->custom_stream->reader(q,length,
4039 blob_info->custom_stream->data);
4042 blob_info->eof=MagickTrue;
4074MagickExport
int ReadBlobByte(Image *image)
4077 *magick_restrict blob_info;
4082 assert(image != (Image *) NULL);
4083 assert(image->signature == MagickCoreSignature);
4084 assert(image->blob != (BlobInfo *) NULL);
4085 assert(image->blob->type != UndefinedStream);
4086 blob_info=image->blob;
4087 switch (blob_info->type)
4089 case StandardStream:
4093 c=getc(blob_info->file_info.file);
4096 if (ferror(blob_info->file_info.file) != 0)
4097 ThrowBlobException(blob_info);
4104 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4106 blob_info->eof=MagickTrue;
4109 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4110 blob_info->offset++;
4121 count=ReadBlob(image,1,buffer);
4154MagickExport
double ReadBlobDouble(Image *image)
4165 quantum.double_value=0.0;
4166 quantum.unsigned_value=ReadBlobLongLong(image);
4167 return(quantum.double_value);
4193MagickExport
float ReadBlobFloat(Image *image)
4204 quantum.float_value=0.0;
4205 quantum.unsigned_value=ReadBlobLong(image);
4206 return(quantum.float_value);
4232MagickExport
unsigned int ReadBlobLong(Image *image)
4246 assert(image != (Image *) NULL);
4247 assert(image->signature == MagickCoreSignature);
4249 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4252 if (image->endian == LSBEndian)
4254 value=(
unsigned int) (*p++);
4255 value|=(
unsigned int) (*p++) << 8;
4256 value|=(
unsigned int) (*p++) << 16;
4257 value|=(
unsigned int) (*p++) << 24;
4260 value=(
unsigned int) (*p++) << 24;
4261 value|=(
unsigned int) (*p++) << 16;
4262 value|=(
unsigned int) (*p++) << 8;
4263 value|=(
unsigned int) (*p++);
4290MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4304 assert(image != (Image *) NULL);
4305 assert(image->signature == MagickCoreSignature);
4307 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4309 return(MagickULLConstant(0));
4310 if (image->endian == LSBEndian)
4312 value=(MagickSizeType) (*p++);
4313 value|=(MagickSizeType) (*p++) << 8;
4314 value|=(MagickSizeType) (*p++) << 16;
4315 value|=(MagickSizeType) (*p++) << 24;
4316 value|=(MagickSizeType) (*p++) << 32;
4317 value|=(MagickSizeType) (*p++) << 40;
4318 value|=(MagickSizeType) (*p++) << 48;
4319 value|=(MagickSizeType) (*p++) << 56;
4322 value=(MagickSizeType) (*p++) << 56;
4323 value|=(MagickSizeType) (*p++) << 48;
4324 value|=(MagickSizeType) (*p++) << 40;
4325 value|=(MagickSizeType) (*p++) << 32;
4326 value|=(MagickSizeType) (*p++) << 24;
4327 value|=(MagickSizeType) (*p++) << 16;
4328 value|=(MagickSizeType) (*p++) << 8;
4329 value|=(MagickSizeType) (*p++);
4356MagickExport
unsigned short ReadBlobShort(Image *image)
4370 assert(image != (Image *) NULL);
4371 assert(image->signature == MagickCoreSignature);
4373 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4375 return((
unsigned short) 0U);
4376 if (image->endian == LSBEndian)
4378 value=(
unsigned short) (*p++);
4379 value|=(
unsigned short) (*p++) << 8;
4382 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4383 value|=(
unsigned short) (*p++);
4410MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4424 assert(image != (Image *) NULL);
4425 assert(image->signature == MagickCoreSignature);
4427 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4430 value=(
unsigned int) (*p++);
4431 value|=(
unsigned int) (*p++) << 8;
4432 value|=(
unsigned int) (*p++) << 16;
4433 value|=(
unsigned int) (*p++) << 24;
4460MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4471 quantum.unsigned_value=ReadBlobLSBLong(image);
4472 return(quantum.signed_value);
4498MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4512 assert(image != (Image *) NULL);
4513 assert(image->signature == MagickCoreSignature);
4515 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4517 return((
unsigned short) 0U);
4518 value=(
unsigned short) (*p++);
4519 value|=(
unsigned short) (*p++) << 8;
4546MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4557 quantum.unsigned_value=ReadBlobLSBShort(image);
4558 return(quantum.signed_value);
4584MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4598 assert(image != (Image *) NULL);
4599 assert(image->signature == MagickCoreSignature);
4601 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4604 value=(
unsigned int) (*p++) << 24;
4605 value|=(
unsigned int) (*p++) << 16;
4606 value|=(
unsigned int) (*p++) << 8;
4607 value|=(
unsigned int) (*p++);
4634MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4648 assert(image != (Image *) NULL);
4649 assert(image->signature == MagickCoreSignature);
4651 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4653 return(MagickULLConstant(0));
4654 value=(MagickSizeType) (*p++) << 56;
4655 value|=(MagickSizeType) (*p++) << 48;
4656 value|=(MagickSizeType) (*p++) << 40;
4657 value|=(MagickSizeType) (*p++) << 32;
4658 value|=(MagickSizeType) (*p++) << 24;
4659 value|=(MagickSizeType) (*p++) << 16;
4660 value|=(MagickSizeType) (*p++) << 8;
4661 value|=(MagickSizeType) (*p++);
4688MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4702 assert(image != (Image *) NULL);
4703 assert(image->signature == MagickCoreSignature);
4705 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4707 return((
unsigned short) 0U);
4708 value=(
unsigned short) ((*p++) << 8);
4709 value|=(
unsigned short) (*p++);
4710 return((
unsigned short) (value & 0xffff));
4736MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4747 quantum.unsigned_value=ReadBlobMSBLong(image);
4748 return(quantum.signed_value);
4774MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4785 quantum.unsigned_value=ReadBlobMSBShort(image);
4786 return(quantum.signed_value);
4812MagickExport
signed int ReadBlobSignedLong(Image *image)
4823 quantum.unsigned_value=ReadBlobLong(image);
4824 return(quantum.signed_value);
4850MagickExport
signed short ReadBlobSignedShort(Image *image)
4861 quantum.unsigned_value=ReadBlobShort(image);
4862 return(quantum.signed_value);
4900MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4901 const size_t length,
void *magick_restrict data,ssize_t *count)
4904 *magick_restrict blob_info;
4906 assert(image != (Image *) NULL);
4907 assert(image->signature == MagickCoreSignature);
4908 assert(image->blob != (BlobInfo *) NULL);
4909 assert(image->blob->type != UndefinedStream);
4910 assert(count != (ssize_t *) NULL);
4911 blob_info=image->blob;
4912 if (blob_info->type != BlobStream)
4914 assert(data != NULL);
4915 *count=ReadBlob(image,length,(
unsigned char *) data);
4918 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4921 blob_info->eof=MagickTrue;
4924 data=blob_info->data+blob_info->offset;
4925 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4926 blob_info->length-blob_info->offset);
4927 blob_info->offset+=(*count);
4928 if (*count != (ssize_t) length)
4929 blob_info->eof=MagickTrue;
4958MagickExport
char *ReadBlobString(Image *image,
char *
string)
4961 *magick_restrict blob_info;
4969 assert(image != (Image *) NULL);
4970 assert(image->signature == MagickCoreSignature);
4971 assert(image->blob != (BlobInfo *) NULL);
4972 assert(image->blob->type != UndefinedStream);
4973 if (IsEventLogging() != MagickFalse)
4974 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4976 blob_info=image->blob;
4977 switch (blob_info->type)
4979 case UndefinedStream:
4981 case StandardStream:
4984 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4985 if (p == (
char *) NULL)
4987 if (ferror(blob_info->file_info.file) != 0)
4988 ThrowBlobException(blob_info);
4989 return((
char *) NULL);
4996#if defined(MAGICKCORE_ZLIB_DELEGATE)
4997 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4998 if (p == (
char *) NULL)
5001 (void) gzerror(blob_info->file_info.gzfile,&status);
5003 ThrowBlobException(blob_info);
5004 return((
char *) NULL);
5014 c=ReadBlobByte(image);
5017 blob_info->eof=MagickTrue;
5020 string[i++]=(char) c;
5023 }
while (i < (MaxTextExtent-2));
5031 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5034 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5036 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5037 return((
char *) NULL);
5064MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5066 assert(blob != (BlobInfo *) NULL);
5067 assert(blob->signature == MagickCoreSignature);
5068 if (IsEventLogging() != MagickFalse)
5069 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5070 LockSemaphoreInfo(blob->semaphore);
5071 blob->reference_count++;
5072 UnlockSemaphoreInfo(blob->semaphore);
5109MagickExport MagickOffsetType SeekBlob(Image *image,
5110 const MagickOffsetType offset,
const int whence)
5113 *magick_restrict blob_info;
5115 assert(image != (Image *) NULL);
5116 assert(image->signature == MagickCoreSignature);
5117 assert(image->blob != (BlobInfo *) NULL);
5118 assert(image->blob->type != UndefinedStream);
5119 if (IsEventLogging() != MagickFalse)
5120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5121 blob_info=image->blob;
5122 switch (blob_info->type)
5124 case UndefinedStream:
5126 case StandardStream:
5131 if ((offset < 0) && (whence == SEEK_SET))
5133 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5135 blob_info->offset=TellBlob(image);
5140#if defined(MAGICKCORE_ZLIB_DELEGATE)
5141 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5144 blob_info->offset=TellBlob(image);
5160 blob_info->offset=offset;
5165 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5166 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5171 if ((blob_info->offset+offset) < 0)
5173 blob_info->offset+=offset;
5178 if (((MagickOffsetType) blob_info->length+offset) < 0)
5180 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5184 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5186 blob_info->eof=MagickFalse;
5193 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5195 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5196 blob_info->custom_stream->data);
5200 return(blob_info->offset);
5228MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5230 assert(image != (
const Image *) NULL);
5231 assert(image->signature == MagickCoreSignature);
5232 if (IsEventLogging() != MagickFalse)
5233 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5234 image->blob->exempt=exempt;
5263MagickExport MagickBooleanType SetBlobExtent(Image *image,
5264 const MagickSizeType extent)
5267 *magick_restrict blob_info;
5269 assert(image != (Image *) NULL);
5270 assert(image->signature == MagickCoreSignature);
5271 assert(image->blob != (BlobInfo *) NULL);
5272 assert(image->blob->type != UndefinedStream);
5273 if (IsEventLogging() != MagickFalse)
5274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5275 blob_info=image->blob;
5276 switch (blob_info->type)
5278 case UndefinedStream:
5280 case StandardStream:
5281 return(MagickFalse);
5290 if (extent != (MagickSizeType) ((off_t) extent))
5291 return(MagickFalse);
5292 offset=SeekBlob(image,0,SEEK_END);
5294 return(MagickFalse);
5295 if ((MagickSizeType) offset >= extent)
5297 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5300 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5301 blob_info->file_info.file);
5302#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5303 if (blob_info->synchronize != MagickFalse)
5308 file=fileno(blob_info->file_info.file);
5309 if ((file == -1) || (offset < 0))
5310 return(MagickFalse);
5311 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5314 offset=SeekBlob(image,offset,SEEK_SET);
5316 return(MagickFalse);
5321 return(MagickFalse);
5323 return(MagickFalse);
5325 return(MagickFalse);
5328 if (extent != (MagickSizeType) ((
size_t) extent))
5329 return(MagickFalse);
5330 if (blob_info->mapped != MagickFalse)
5338 (void) UnmapBlob(blob_info->data,blob_info->length);
5339 RelinquishMagickResource(MapResource,blob_info->length);
5340 if (extent != (MagickSizeType) ((off_t) extent))
5341 return(MagickFalse);
5342 offset=SeekBlob(image,0,SEEK_END);
5344 return(MagickFalse);
5345 if ((MagickSizeType) offset >= extent)
5347 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5348 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5349 blob_info->file_info.file);
5350#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5351 if (blob_info->synchronize != MagickFalse)
5356 file=fileno(blob_info->file_info.file);
5357 if ((file == -1) || (offset < 0))
5358 return(MagickFalse);
5359 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5363 offset=SeekBlob(image,offset,SEEK_SET);
5365 return(MagickFalse);
5366 (void) AcquireMagickResource(MapResource,extent);
5367 blob_info->data=(
unsigned char*) MapBlob(fileno(
5368 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5369 blob_info->extent=(size_t) extent;
5370 blob_info->length=(size_t) extent;
5371 (void) SyncBlob(image);
5374 blob_info->extent=(size_t) extent;
5375 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5376 blob_info->extent+1,
sizeof(*blob_info->data));
5377 (void) SyncBlob(image);
5378 if (blob_info->data == (
unsigned char *) NULL)
5380 (void) DetachBlob(blob_info);
5381 return(MagickFalse);
5415MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5418 assert(custom_stream != (CustomStreamInfo *) NULL);
5419 assert(custom_stream->signature == MagickCoreSignature);
5420 custom_stream->data=data;
5448MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5449 CustomStreamHandler reader)
5451 assert(custom_stream != (CustomStreamInfo *) NULL);
5452 assert(custom_stream->signature == MagickCoreSignature);
5453 custom_stream->reader=reader;
5481MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5482 CustomStreamSeeker seeker)
5484 assert(custom_stream != (CustomStreamInfo *) NULL);
5485 assert(custom_stream->signature == MagickCoreSignature);
5486 custom_stream->seeker=seeker;
5514MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5515 CustomStreamTeller teller)
5517 assert(custom_stream != (CustomStreamInfo *) NULL);
5518 assert(custom_stream->signature == MagickCoreSignature);
5519 custom_stream->teller=teller;
5547MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5548 CustomStreamHandler writer)
5550 assert(custom_stream != (CustomStreamInfo *) NULL);
5551 assert(custom_stream->signature == MagickCoreSignature);
5552 custom_stream->writer=writer;
5579static int SyncBlob(
const Image *image)
5582 *magick_restrict blob_info;
5587 assert(image != (Image *) NULL);
5588 assert(image->signature == MagickCoreSignature);
5589 assert(image->blob != (BlobInfo *) NULL);
5590 if (IsEventLogging() != MagickFalse)
5591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5592 if (EOFBlob(image) != 0)
5594 blob_info=image->blob;
5596 switch (blob_info->type)
5598 case UndefinedStream:
5599 case StandardStream:
5604 status=fflush(blob_info->file_info.file);
5609#if defined(MAGICKCORE_ZLIB_DELEGATE)
5610 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5616#if defined(MAGICKCORE_BZLIB_DELEGATE)
5617 status=BZ2_bzflush(blob_info->file_info.bzfile);
5662MagickPrivate
void SyncBlobStream(ImageInfo *image_info,Image *image,
5665 assert(owner != (Image *) NULL);
5666 assert(image != (Image *) NULL);
5667 assert(image->signature == MagickCoreSignature);
5668 if (image->blob == (BlobInfo *) NULL)
5670 if (image->blob->data == (
unsigned char *) NULL)
5672 if (image_info != (ImageInfo *) NULL)
5674 image_info->blob=(
void *) image->blob->data;
5675 image_info->length=image->blob->length;
5677 if ((image == owner) || (owner->blob == (BlobInfo *) NULL))
5679 if (owner->blob == (BlobInfo *) NULL)
5685 owner->blob->data=image->blob->data;
5686 owner->blob->extent=image->blob->extent;
5687 owner->blob->length=image->blob->length;
5688 owner->blob->offset=image->blob->offset;
5689 owner->blob->quantum=image->blob->quantum;
5690 owner->blob->type=BlobStream;
5691 owner->blob->exempt=MagickTrue;
5692 (void) DetachBlob(image->blob);
5717MagickExport MagickOffsetType TellBlob(
const Image *image)
5720 *magick_restrict blob_info;
5725 assert(image != (Image *) NULL);
5726 assert(image->signature == MagickCoreSignature);
5727 assert(image->blob != (BlobInfo *) NULL);
5728 assert(image->blob->type != UndefinedStream);
5729 if (IsEventLogging() != MagickFalse)
5730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5731 blob_info=image->blob;
5733 switch (blob_info->type)
5735 case UndefinedStream:
5736 case StandardStream:
5740 offset=ftell(blob_info->file_info.file);
5747#if defined(MAGICKCORE_ZLIB_DELEGATE)
5748 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5758 offset=blob_info->offset;
5763 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5764 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5796MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5798#if defined(MAGICKCORE_HAVE_MMAP)
5802 status=munmap(map,length);
5803 return(status == -1 ? MagickFalse : MagickTrue);
5807 return(MagickFalse);
5839MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5843 *magick_restrict blob_info;
5857 assert(image != (Image *) NULL);
5858 assert(image->signature == MagickCoreSignature);
5859 assert(image->blob != (BlobInfo *) NULL);
5860 assert(image->blob->type != UndefinedStream);
5863 assert(data != (
const void *) NULL);
5864 blob_info=image->blob;
5866 p=(
const unsigned char *) data;
5867 q=(
unsigned char *) data;
5868 switch (blob_info->type)
5870 case UndefinedStream:
5872 case StandardStream:
5880 count=(ssize_t) fwrite((
const char *) data,1,length,
5881 blob_info->file_info.file);
5886 c=putc((
int) *p++,blob_info->file_info.file);
5894 c=putc((
int) *p++,blob_info->file_info.file);
5902 c=putc((
int) *p++,blob_info->file_info.file);
5910 c=putc((
int) *p++,blob_info->file_info.file);
5919 if ((count != (ssize_t) length) &&
5920 (ferror(blob_info->file_info.file) != 0))
5921 ThrowBlobException(blob_info);
5926#if defined(MAGICKCORE_ZLIB_DELEGATE)
5937 for (i=0; i < length; i+=(size_t) count)
5939 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5940 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5953 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5961 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5969 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5977 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5987 (void) gzerror(blob_info->file_info.gzfile,&status);
5988 if ((count != (ssize_t) length) && (status != Z_OK))
5989 ThrowBlobException(blob_info);
5995#if defined(MAGICKCORE_BZLIB_DELEGATE)
6002 for (i=0; i < length; i+=(size_t) count)
6004 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6005 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6015 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6016 if ((count != (ssize_t) length) && (status != BZ_OK))
6017 ThrowBlobException(blob_info);
6023 count=(ssize_t) blob_info->stream(image,data,length);
6031 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6036 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6037 if (extent >= blob_info->extent)
6039 extent+=blob_info->quantum+length;
6040 blob_info->quantum<<=1;
6041 if (SetBlobExtent(image,extent) == MagickFalse)
6044 q=blob_info->data+blob_info->offset;
6045 (void) memcpy(q,p,length);
6046 blob_info->offset+=(MagickOffsetType) length;
6047 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6048 blob_info->length=(size_t) blob_info->offset;
6049 count=(ssize_t) length;
6054 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6055 count=blob_info->custom_stream->writer((
unsigned char *) data,
6056 length,blob_info->custom_stream->data);
6088MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6091 *magick_restrict blob_info;
6096 assert(image != (Image *) NULL);
6097 assert(image->signature == MagickCoreSignature);
6098 assert(image->blob != (BlobInfo *) NULL);
6099 assert(image->blob->type != UndefinedStream);
6100 blob_info=image->blob;
6102 switch (blob_info->type)
6104 case StandardStream:
6111 c=putc((
int) value,blob_info->file_info.file);
6114 if (ferror(blob_info->file_info.file) != 0)
6115 ThrowBlobException(blob_info);
6123 count=WriteBlobStream(image,1,&value);
6155MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6166 quantum.unsigned_value=0U;
6167 quantum.float_value=value;
6168 return(WriteBlobLong(image,quantum.unsigned_value));
6196MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6201 assert(image != (Image *) NULL);
6202 assert(image->signature == MagickCoreSignature);
6203 if (image->endian == LSBEndian)
6205 buffer[0]=(
unsigned char) value;
6206 buffer[1]=(
unsigned char) (value >> 8);
6207 buffer[2]=(
unsigned char) (value >> 16);
6208 buffer[3]=(
unsigned char) (value >> 24);
6209 return(WriteBlobStream(image,4,buffer));
6211 buffer[0]=(
unsigned char) (value >> 24);
6212 buffer[1]=(
unsigned char) (value >> 16);
6213 buffer[2]=(
unsigned char) (value >> 8);
6214 buffer[3]=(
unsigned char) value;
6215 return(WriteBlobStream(image,4,buffer));
6243MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6248 assert(image != (Image *) NULL);
6249 assert(image->signature == MagickCoreSignature);
6250 if (image->endian == LSBEndian)
6252 buffer[0]=(
unsigned char) value;
6253 buffer[1]=(
unsigned char) (value >> 8);
6254 buffer[2]=(
unsigned char) (value >> 16);
6255 buffer[3]=(
unsigned char) (value >> 24);
6256 buffer[4]=(
unsigned char) (value >> 32);
6257 buffer[5]=(
unsigned char) (value >> 40);
6258 buffer[6]=(
unsigned char) (value >> 48);
6259 buffer[7]=(
unsigned char) (value >> 56);
6260 return(WriteBlobStream(image,8,buffer));
6262 buffer[0]=(
unsigned char) (value >> 56);
6263 buffer[1]=(
unsigned char) (value >> 48);
6264 buffer[2]=(
unsigned char) (value >> 40);
6265 buffer[3]=(
unsigned char) (value >> 32);
6266 buffer[4]=(
unsigned char) (value >> 24);
6267 buffer[5]=(
unsigned char) (value >> 16);
6268 buffer[6]=(
unsigned char) (value >> 8);
6269 buffer[7]=(
unsigned char) value;
6270 return(WriteBlobStream(image,8,buffer));
6298MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6303 assert(image != (Image *) NULL);
6304 assert(image->signature == MagickCoreSignature);
6305 if (image->endian == LSBEndian)
6307 buffer[0]=(
unsigned char) value;
6308 buffer[1]=(
unsigned char) (value >> 8);
6309 return(WriteBlobStream(image,2,buffer));
6311 buffer[0]=(
unsigned char) (value >> 8);
6312 buffer[1]=(
unsigned char) value;
6313 return(WriteBlobStream(image,2,buffer));
6341MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6355 assert(image != (Image *) NULL);
6356 assert(image->signature == MagickCoreSignature);
6357 quantum.signed_value=value;
6358 if (image->endian == LSBEndian)
6360 buffer[0]=(
unsigned char) quantum.unsigned_value;
6361 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6362 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6363 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6364 return(WriteBlobStream(image,4,buffer));
6366 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6367 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6368 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6369 buffer[3]=(
unsigned char) quantum.unsigned_value;
6370 return(WriteBlobStream(image,4,buffer));
6398MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6403 assert(image != (Image *) NULL);
6404 assert(image->signature == MagickCoreSignature);
6405 buffer[0]=(
unsigned char) value;
6406 buffer[1]=(
unsigned char) (value >> 8);
6407 buffer[2]=(
unsigned char) (value >> 16);
6408 buffer[3]=(
unsigned char) (value >> 24);
6409 return(WriteBlobStream(image,4,buffer));
6437MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6442 assert(image != (Image *) NULL);
6443 assert(image->signature == MagickCoreSignature);
6444 buffer[0]=(
unsigned char) value;
6445 buffer[1]=(
unsigned char) (value >> 8);
6446 return(WriteBlobStream(image,2,buffer));
6474MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6488 assert(image != (Image *) NULL);
6489 assert(image->signature == MagickCoreSignature);
6490 quantum.signed_value=value;
6491 buffer[0]=(
unsigned char) quantum.unsigned_value;
6492 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6493 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6494 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6495 return(WriteBlobStream(image,4,buffer));
6523MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6524 const signed short value)
6538 assert(image != (Image *) NULL);
6539 assert(image->signature == MagickCoreSignature);
6540 quantum.signed_value=value;
6541 buffer[0]=(
unsigned char) quantum.unsigned_value;
6542 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6543 return(WriteBlobStream(image,2,buffer));
6571MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6576 assert(image != (Image *) NULL);
6577 assert(image->signature == MagickCoreSignature);
6578 buffer[0]=(
unsigned char) (value >> 24);
6579 buffer[1]=(
unsigned char) (value >> 16);
6580 buffer[2]=(
unsigned char) (value >> 8);
6581 buffer[3]=(
unsigned char) value;
6582 return(WriteBlobStream(image,4,buffer));
6610MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6611 const signed short value)
6625 assert(image != (Image *) NULL);
6626 assert(image->signature == MagickCoreSignature);
6627 quantum.signed_value=value;
6628 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6629 buffer[1]=(
unsigned char) quantum.unsigned_value;
6630 return(WriteBlobStream(image,2,buffer));
6658MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6663 assert(image != (Image *) NULL);
6664 assert(image->signature == MagickCoreSignature);
6665 buffer[0]=(
unsigned char) (value >> 8);
6666 buffer[1]=(
unsigned char) value;
6667 return(WriteBlobStream(image,2,buffer));
6695MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6697 assert(image != (Image *) NULL);
6698 assert(image->signature == MagickCoreSignature);
6699 assert(
string != (
const char *) NULL);
6700 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));