47#include "MagickCore/studio.h"
48#include "MagickCore/blob.h"
49#include "MagickCore/blob-private.h"
50#include "MagickCore/cache.h"
51#include "MagickCore/client.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/delegate.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/locale_.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/memory-private.h"
64#include "MagickCore/nt-base-private.h"
65#include "MagickCore/option.h"
66#include "MagickCore/policy.h"
67#include "MagickCore/resource_.h"
68#include "MagickCore/semaphore.h"
69#include "MagickCore/string_.h"
70#include "MagickCore/string-private.h"
71#include "MagickCore/timer-private.h"
72#include "MagickCore/token.h"
73#include "MagickCore/utility.h"
74#include "MagickCore/utility-private.h"
75#if defined(MAGICKCORE_ZLIB_DELEGATE)
78#if defined(MAGICKCORE_BZLIB_DELEGATE)
81#include "MagickCore/policy-private.h"
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 *);
252 magick_unreferenced(exception);
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,
331 assert(blob_info != (
BlobInfo *) 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;
370MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
382 assert(filename != (
const char *) NULL);
383 assert(blob != (
const void *) NULL);
384 if (IsEventLogging() != MagickFalse)
385 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
386 if (*filename ==
'\0')
387 file=AcquireUniqueFileResource(filename);
389 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
392 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
395 for (i=0; i < length; i+=(size_t) count)
397 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
398 MagickMaxBufferExtent));
406 file=close_utf8(file);
407 if ((file == -1) || (i < length))
409 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
446MagickExport
Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
462 assert(image_info != (
ImageInfo *) NULL);
463 assert(image_info->signature == MagickCoreSignature);
465 if (IsEventLogging() != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
467 image_info->filename);
468 if ((blob == (
const void *) NULL) || (length == 0))
470 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
471 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
472 return((
Image *) NULL);
474 blob_info=CloneImageInfo(image_info);
475 blob_info->blob=(
void *) blob;
476 blob_info->length=length;
477 if (*blob_info->magick ==
'\0')
478 (void) SetImageInfo(blob_info,0,exception);
479 magick_info=GetMagickInfo(blob_info->magick,exception);
482 (void) ThrowMagickException(exception,GetMagickModule(),
483 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
485 blob_info=DestroyImageInfo(blob_info);
486 return((
Image *) NULL);
488 if (GetMagickBlobSupport(magick_info) != MagickFalse)
491 filename[MagickPathExtent];
496 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
497 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
498 blob_info->magick,filename);
499 image=ReadImage(blob_info,exception);
500 if (image != (
Image *) NULL)
501 (void) DetachBlob(image->blob);
502 blob_info=DestroyImageInfo(blob_info);
508 blob_info->blob=(
void *) NULL;
510 *blob_info->filename=
'\0';
511 status=BlobToFile(blob_info->filename,blob,length,exception);
512 if (status == MagickFalse)
514 (void) RelinquishUniqueFileResource(blob_info->filename);
515 blob_info=DestroyImageInfo(blob_info);
516 return((
Image *) NULL);
518 clone_info=CloneImageInfo(blob_info);
519 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
520 blob_info->magick,blob_info->filename);
521 image=ReadImage(clone_info,exception);
522 if (image != (
Image *) NULL)
530 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
532 (void) CopyMagickString(images->filename,image_info->filename,
534 (void) CopyMagickString(images->magick_filename,image_info->filename,
536 (void) CopyMagickString(images->magick,magick_info->name,
538 images=GetNextImageInList(images);
541 clone_info=DestroyImageInfo(clone_info);
542 (void) RelinquishUniqueFileResource(blob_info->filename);
543 blob_info=DestroyImageInfo(blob_info);
578 clone_info=(
BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
579 GetBlobInfo(clone_info);
582 semaphore=clone_info->semaphore;
583 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
584 if (blob_info->mapped != MagickFalse)
585 (void) AcquireMagickResource(MapResource,blob_info->length);
586 clone_info->semaphore=semaphore;
587 LockSemaphoreInfo(clone_info->semaphore);
588 clone_info->reference_count=1;
589 UnlockSemaphoreInfo(clone_info->semaphore);
616static inline void ThrowBlobException(
BlobInfo *blob_info)
618 if ((blob_info->status == 0) && (errno != 0))
619 blob_info->error_number=errno;
620 blob_info->status=(-1);
623MagickExport MagickBooleanType CloseBlob(
Image *image)
626 *magick_restrict blob_info;
634 assert(image != (
Image *) NULL);
635 assert(image->signature == MagickCoreSignature);
636 if (IsEventLogging() != MagickFalse)
637 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
638 blob_info=image->blob;
639 if ((blob_info == (
BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
641 (void) SyncBlob(image);
642 status=blob_info->status;
643 switch (blob_info->type)
645 case UndefinedStream:
651 if (blob_info->synchronize != MagickFalse)
653 status=fflush(blob_info->file_info.file);
655 ThrowBlobException(blob_info);
656 status=fsync(fileno(blob_info->file_info.file));
658 ThrowBlobException(blob_info);
660 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
661 ThrowBlobException(blob_info);
666#if defined(MAGICKCORE_ZLIB_DELEGATE)
668 (void) gzerror(blob_info->file_info.gzfile,&status);
670 ThrowBlobException(blob_info);
676#if defined(MAGICKCORE_BZLIB_DELEGATE)
678 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
680 ThrowBlobException(blob_info);
688 if (blob_info->file_info.file != (FILE *) NULL)
690 if (blob_info->synchronize != MagickFalse)
692 status=fflush(blob_info->file_info.file);
694 ThrowBlobException(blob_info);
695 status=fsync(fileno(blob_info->file_info.file));
697 ThrowBlobException(blob_info);
699 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
700 ThrowBlobException(blob_info);
707 blob_info->size=GetBlobSize(image);
708 image->extent=blob_info->size;
709 blob_info->eof=MagickFalse;
711 blob_info->mode=UndefinedBlobMode;
712 if (blob_info->exempt != MagickFalse)
714 blob_info->type=UndefinedStream;
715 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
717 switch (blob_info->type)
719 case UndefinedStream:
724 if (blob_info->file_info.file != (FILE *) NULL)
726 status=fclose(blob_info->file_info.file);
728 ThrowBlobException(blob_info);
734#if defined(MAGICKCORE_HAVE_PCLOSE)
735 status=pclose(blob_info->file_info.file);
737 ThrowBlobException(blob_info);
743#if defined(MAGICKCORE_ZLIB_DELEGATE)
744 status=gzclose(blob_info->file_info.gzfile);
746 ThrowBlobException(blob_info);
752#if defined(MAGICKCORE_BZLIB_DELEGATE)
753 BZ2_bzclose(blob_info->file_info.bzfile);
761 if (blob_info->file_info.file != (FILE *) NULL)
763 status=fclose(blob_info->file_info.file);
765 ThrowBlobException(blob_info);
772 (void) DetachBlob(blob_info);
773 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
802MagickExport
Image *CustomStreamToImage(
const ImageInfo *image_info,
814 assert(image_info != (
ImageInfo *) NULL);
815 assert(image_info->signature == MagickCoreSignature);
817 assert(image_info->custom_stream->signature == MagickCoreSignature);
818 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
820 if (IsEventLogging() != MagickFalse)
821 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
822 image_info->filename);
823 blob_info=CloneImageInfo(image_info);
824 if (*blob_info->magick ==
'\0')
825 (void) SetImageInfo(blob_info,0,exception);
826 magick_info=GetMagickInfo(blob_info->magick,exception);
829 (void) ThrowMagickException(exception,GetMagickModule(),
830 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
832 blob_info=DestroyImageInfo(blob_info);
833 return((
Image *) NULL);
835 image=(
Image *) NULL;
836 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
837 (*blob_info->filename !=
'\0'))
840 filename[MagickPathExtent];
846 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
847 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
848 blob_info->magick,filename);
849 image=ReadImage(blob_info,exception);
854 unique[MagickPathExtent];
869 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
871 if (blob == (
unsigned char *) NULL)
873 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
874 image_info->filename);
875 blob_info=DestroyImageInfo(blob_info);
876 return((
Image *) NULL);
878 file=AcquireUniqueFileResource(unique);
881 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
882 image_info->filename);
883 blob=(
unsigned char *) RelinquishMagickMemory(blob);
884 blob_info=DestroyImageInfo(blob_info);
885 return((
Image *) NULL);
887 clone_info=CloneImageInfo(blob_info);
888 blob_info->file=fdopen(file,
"wb+");
889 if (blob_info->file != (FILE *) NULL)
894 count=(ssize_t) MagickMaxBufferExtent;
895 while (count == (ssize_t) MagickMaxBufferExtent)
897 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
898 image_info->custom_stream->data);
899 count=(ssize_t) write(file,(
const char *) blob,(size_t) count);
901 (void) fclose(blob_info->file);
902 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
903 "%s:%s",blob_info->magick,unique);
904 image=ReadImage(clone_info,exception);
905 if (image != (
Image *) NULL)
913 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
915 (void) CopyMagickString(images->filename,image_info->filename,
917 (void) CopyMagickString(images->magick_filename,
918 image_info->filename,MagickPathExtent);
919 (void) CopyMagickString(images->magick,magick_info->name,
921 images=GetNextImageInList(images);
925 clone_info=DestroyImageInfo(clone_info);
926 blob=(
unsigned char *) RelinquishMagickMemory(blob);
927 (void) RelinquishUniqueFileResource(unique);
929 blob_info=DestroyImageInfo(blob_info);
930 if (image != (
Image *) NULL)
931 if (CloseBlob(image) == MagickFalse)
932 image=DestroyImageList(image);
958MagickExport
void DestroyBlob(
Image *image)
961 *magick_restrict blob_info;
966 assert(image != (
Image *) NULL);
967 assert(image->signature == MagickCoreSignature);
968 assert(image->blob != (
BlobInfo *) NULL);
969 assert(image->blob->signature == MagickCoreSignature);
970 if (IsEventLogging() != MagickFalse)
971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
972 blob_info=image->blob;
974 LockSemaphoreInfo(blob_info->semaphore);
975 blob_info->reference_count--;
976 assert(blob_info->reference_count >= 0);
977 if (blob_info->reference_count == 0)
979 UnlockSemaphoreInfo(blob_info->semaphore);
980 if (destroy == MagickFalse)
985 (void) CloseBlob(image);
986 if (blob_info->mapped != MagickFalse)
988 (void) UnmapBlob(blob_info->data,blob_info->length);
989 RelinquishMagickResource(MapResource,blob_info->length);
992 RelinquishSemaphoreInfo(&blob_info->semaphore);
993 blob_info->signature=(~MagickCoreSignature);
994 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
1024 assert(custom_stream->signature == MagickCoreSignature);
1025 if (IsEventLogging() != MagickFalse)
1026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1027 custom_stream->signature=(~MagickCoreSignature);
1029 return(custom_stream);
1054MagickExport
void *DetachBlob(
BlobInfo *blob_info)
1059 assert(blob_info != (
BlobInfo *) NULL);
1060 if (IsEventLogging() != MagickFalse)
1061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1062 if (blob_info->mapped != MagickFalse)
1064 (void) UnmapBlob(blob_info->data,blob_info->length);
1065 blob_info->data=NULL;
1066 RelinquishMagickResource(MapResource,blob_info->length);
1068 blob_info->mapped=MagickFalse;
1069 blob_info->length=0;
1074 blob_info->offset=0;
1075 blob_info->mode=UndefinedBlobMode;
1076 blob_info->eof=MagickFalse;
1078 blob_info->exempt=MagickFalse;
1079 blob_info->type=UndefinedStream;
1080 blob_info->file_info.file=(FILE *) NULL;
1081 data=blob_info->data;
1082 blob_info->data=(
unsigned char *) NULL;
1083 blob_info->stream=(StreamHandler) NULL;
1112MagickExport
void DisassociateBlob(
Image *image)
1115 *magick_restrict blob_info,
1121 assert(image != (
Image *) NULL);
1122 assert(image->signature == MagickCoreSignature);
1123 assert(image->blob != (
BlobInfo *) NULL);
1124 assert(image->blob->signature == MagickCoreSignature);
1125 if (IsEventLogging() != MagickFalse)
1126 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1127 blob_info=image->blob;
1129 LockSemaphoreInfo(blob_info->semaphore);
1130 assert(blob_info->reference_count >= 0);
1131 if (blob_info->reference_count > 1)
1133 UnlockSemaphoreInfo(blob_info->semaphore);
1134 if (clone == MagickFalse)
1136 clone_info=CloneBlobInfo(blob_info);
1138 image->blob=clone_info;
1166MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
1167 const MagickSizeType length)
1179 buffer[MagickMinBufferExtent >> 1];
1181 assert(image != (
Image *) NULL);
1182 assert(image->signature == MagickCoreSignature);
1183 if (length != (MagickSizeType) ((MagickOffsetType) length))
1184 return(MagickFalse);
1186 for (i=0; i < length; i+=(MagickSizeType) count)
1188 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1189 (void) ReadBlobStream(image,quantum,buffer,&count);
1197 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1224MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
1226 assert(image != (
Image *) NULL);
1227 assert(image->signature == MagickCoreSignature);
1228 assert(duplicate != (
Image *) NULL);
1229 assert(duplicate->signature == MagickCoreSignature);
1230 if (IsEventLogging() != MagickFalse)
1231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1233 image->blob=ReferenceBlob(duplicate->blob);
1259MagickExport
int EOFBlob(
const Image *image)
1262 *magick_restrict blob_info;
1264 assert(image != (
Image *) NULL);
1265 assert(image->signature == MagickCoreSignature);
1266 assert(image->blob != (
BlobInfo *) NULL);
1267 assert(image->blob->type != UndefinedStream);
1268 if (IsEventLogging() != MagickFalse)
1269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1270 blob_info=image->blob;
1271 switch (blob_info->type)
1273 case UndefinedStream:
1274 case StandardStream:
1279 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1285#if defined(MAGICKCORE_ZLIB_DELEGATE)
1286 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1293#if defined(MAGICKCORE_BZLIB_DELEGATE)
1298 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1299 if (status == BZ_UNEXPECTED_EOF)
1300 blob_info->eof=MagickTrue;
1306 blob_info->eof=MagickFalse;
1314 return((
int) blob_info->eof);
1340MagickExport
int ErrorBlob(
const Image *image)
1343 *magick_restrict blob_info;
1345 assert(image != (
Image *) NULL);
1346 assert(image->signature == MagickCoreSignature);
1347 assert(image->blob != (
BlobInfo *) NULL);
1348 assert(image->blob->type != UndefinedStream);
1349 if (IsEventLogging() != MagickFalse)
1350 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1351 blob_info=image->blob;
1352 switch (blob_info->type)
1354 case UndefinedStream:
1355 case StandardStream:
1360 blob_info->error=ferror(blob_info->file_info.file);
1365#if defined(MAGICKCORE_ZLIB_DELEGATE)
1366 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1372#if defined(MAGICKCORE_BZLIB_DELEGATE)
1373 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1387 return(blob_info->error);
1425MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1452 assert(filename != (
const char *) NULL);
1454 assert(exception->signature == MagickCoreSignature);
1455 if (IsEventLogging() != MagickFalse)
1456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1458 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1459 if (status == MagickFalse)
1462 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1463 "NotAuthorized",
"`%s'",filename);
1467 if (LocaleCompare(filename,
"-") != 0)
1470 flags = O_RDONLY | O_BINARY;
1472 status=GetPathAttributes(filename,&attributes);
1473 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1475 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1478#if defined(O_NOFOLLOW)
1479 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1480 if (status == MagickFalse)
1483 file=open_utf8(filename,flags,0);
1487 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1490 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1491 if (status == MagickFalse)
1493 file=close_utf8(file)-1;
1495 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1496 "NotAuthorized",
"`%s'",filename);
1499 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1501 if ((file == fileno(stdin)) || (offset < 0) ||
1502 (offset != (MagickOffsetType) ((ssize_t) offset)))
1513 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1514 quantum=(size_t) MagickMaxBufferExtent;
1515 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1516 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1517 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1518 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1520 count=read(file,blob+i,quantum);
1527 if (~i < ((
size_t) count+quantum+1))
1529 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1532 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1533 quantum+1,
sizeof(*blob));
1534 if ((i+(
size_t) count) >= extent)
1537 if (LocaleCompare(filename,
"-") != 0)
1538 file=close_utf8(file);
1539 if (blob == (
unsigned char *) NULL)
1541 (void) ThrowMagickException(exception,GetMagickModule(),
1542 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1547 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1548 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1551 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1555 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1556 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1557 blob=(
unsigned char *) NULL;
1558 if (~(*length) >= (MagickPathExtent-1))
1559 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1561 if (blob == (
unsigned char *) NULL)
1563 file=close_utf8(file);
1564 (void) ThrowMagickException(exception,GetMagickModule(),
1565 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1568 map=MapBlob(file,ReadMode,0,*length);
1569 if (map != (
unsigned char *) NULL)
1571 (void) memcpy(blob,map,*length);
1572 (void) UnmapBlob(map,*length);
1576 (void) lseek(file,0,SEEK_SET);
1577 for (i=0; i < *length; i+=(size_t) count)
1579 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1580 MagickMaxBufferExtent));
1590 file=close_utf8(file)-1;
1591 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1592 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1597 if (LocaleCompare(filename,
"-") != 0)
1598 file=close_utf8(file);
1601 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1602 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1631static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1632 const void *magick_restrict data)
1635 *magick_restrict blob_info;
1643 assert(image->blob != (
BlobInfo *) NULL);
1644 assert(image->blob->type != UndefinedStream);
1645 assert(data != NULL);
1646 blob_info=image->blob;
1647 if (blob_info->type != BlobStream)
1648 return(WriteBlob(image,length,(
const unsigned char *) data));
1649 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1650 if (extent >= blob_info->extent)
1652 extent+=blob_info->quantum+length;
1653 blob_info->quantum<<=1;
1654 if (SetBlobExtent(image,extent) == MagickFalse)
1657 q=blob_info->data+blob_info->offset;
1658 (void) memcpy(q,data,length);
1659 blob_info->offset+=(MagickOffsetType) length;
1660 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1661 blob_info->length=(
size_t) blob_info->offset;
1662 return((ssize_t) length);
1665MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename,
1687 assert(image != (
const Image *) NULL);
1688 assert(image->signature == MagickCoreSignature);
1689 assert(filename != (
const char *) NULL);
1690 if (IsEventLogging() != MagickFalse)
1691 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1692 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1693 if (status == MagickFalse)
1696 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1697 "NotAuthorized",
"`%s'",filename);
1698 return(MagickFalse);
1701 if (LocaleCompare(filename,
"-") != 0)
1704 flags = O_RDONLY | O_BINARY;
1706#if defined(O_NOFOLLOW)
1707 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1708 if (status == MagickFalse)
1711 file=open_utf8(filename,flags,0);
1715 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1716 return(MagickFalse);
1718 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1719 if (status == MagickFalse)
1721 file=close_utf8(file);
1723 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1724 "NotAuthorized",
"`%s'",filename);
1725 return(MagickFalse);
1727 quantum=(size_t) MagickMaxBufferExtent;
1728 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1729 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1730 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1731 if (blob == (
unsigned char *) NULL)
1733 file=close_utf8(file);
1734 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1736 return(MagickFalse);
1740 count=read(file,blob,quantum);
1747 length=(size_t) count;
1748 count=WriteBlobStream(image,length,blob);
1749 if (count != (ssize_t) length)
1751 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1755 file=close_utf8(file);
1757 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1758 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1785MagickExport MagickBooleanType GetBlobError(
const Image *image)
1787 assert(image != (
const Image *) NULL);
1788 assert(image->signature == MagickCoreSignature);
1789 if (IsEventLogging() != MagickFalse)
1790 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1791 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1792 errno=image->blob->error_number;
1793 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1818MagickExport FILE *GetBlobFileHandle(
const Image *image)
1820 assert(image != (
const Image *) NULL);
1821 assert(image->signature == MagickCoreSignature);
1822 return(image->blob->file_info.file);
1847MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1849 assert(blob_info != (
BlobInfo *) NULL);
1850 (void) memset(blob_info,0,
sizeof(*blob_info));
1851 blob_info->type=UndefinedStream;
1852 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1853 blob_info->properties.st_mtime=GetMagickTime();
1854 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1855 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1856 blob_info->reference_count=1;
1857 blob_info->semaphore=AcquireSemaphoreInfo();
1858 blob_info->signature=MagickCoreSignature;
1883MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1885 assert(image != (
Image *) NULL);
1886 assert(image->signature == MagickCoreSignature);
1887 if (IsEventLogging() != MagickFalse)
1888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1889 return(&image->blob->properties);
1915MagickExport MagickSizeType GetBlobSize(
const Image *image)
1918 *magick_restrict blob_info;
1923 assert(image != (
Image *) NULL);
1924 assert(image->signature == MagickCoreSignature);
1925 assert(image->blob != (
BlobInfo *) NULL);
1926 if (IsEventLogging() != MagickFalse)
1927 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1928 blob_info=image->blob;
1930 switch (blob_info->type)
1932 case UndefinedStream:
1933 case StandardStream:
1935 extent=blob_info->size;
1943 extent=(MagickSizeType) blob_info->properties.st_size;
1945 extent=blob_info->size;
1946 file_descriptor=fileno(blob_info->file_info.file);
1947 if (file_descriptor == -1)
1949 if (fstat(file_descriptor,&blob_info->properties) == 0)
1950 extent=(MagickSizeType) blob_info->properties.st_size;
1955 extent=blob_info->size;
1964 status=GetPathAttributes(image->filename,&blob_info->properties);
1965 if (status != MagickFalse)
1966 extent=(MagickSizeType) blob_info->properties.st_size;
1973 extent=(MagickSizeType) blob_info->length;
1978 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1979 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1984 offset=blob_info->custom_stream->teller(
1985 blob_info->custom_stream->data);
1986 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1987 blob_info->custom_stream->data);
1988 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1989 blob_info->custom_stream->data);
2019MagickExport
void *GetBlobStreamData(
const Image *image)
2021 assert(image != (
const Image *) NULL);
2022 assert(image->signature == MagickCoreSignature);
2023 return(image->blob->data);
2048MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2050 assert(image != (
const Image *) NULL);
2051 assert(image->signature == MagickCoreSignature);
2052 if (IsEventLogging() != MagickFalse)
2053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2054 return(image->blob->stream);
2090MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2105 assert(image_info != (
const ImageInfo *) NULL);
2106 assert(image_info->signature == MagickCoreSignature);
2107 assert(image != (
Image *) NULL);
2108 assert(image->signature == MagickCoreSignature);
2110 assert(exception->signature == MagickCoreSignature);
2111 if (IsEventLogging() != MagickFalse)
2112 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2113 image_info->filename);
2115 blob=(
unsigned char *) NULL;
2116 blob_info=CloneImageInfo(image_info);
2117 blob_info->adjoin=MagickFalse;
2118 (void) SetImageInfo(blob_info,1,exception);
2119 if (*blob_info->magick !=
'\0')
2120 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2121 magick_info=GetMagickInfo(image->magick,exception);
2122 if (magick_info == (
const MagickInfo *) NULL)
2124 (void) ThrowMagickException(exception,GetMagickModule(),
2125 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2127 blob_info=DestroyImageInfo(blob_info);
2130 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2131 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2136 blob_info->length=0;
2137 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2138 sizeof(
unsigned char));
2139 if (blob_info->blob == NULL)
2140 (void) ThrowMagickException(exception,GetMagickModule(),
2141 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2144 (void) CloseBlob(image);
2145 image->blob->exempt=MagickTrue;
2146 image->blob->extent=0;
2147 *image->filename=
'\0';
2148 status=WriteImage(blob_info,image,exception);
2149 SyncBlobStream(blob_info,image,image);
2150 *length=image->blob->length;
2151 blob=DetachBlob(image->blob);
2152 if (blob != (
void *) NULL)
2154 if (status == MagickFalse)
2155 blob=RelinquishMagickMemory(blob);
2157 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2160 if ((status == MagickFalse) && (image->blob->extent == 0))
2161 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2167 unique[MagickPathExtent];
2175 file=AcquireUniqueFileResource(unique);
2178 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2179 image_info->filename);
2183 blob_info->file=fdopen(file,
"wb");
2184 if (blob_info->file != (FILE *) NULL)
2186 (void) FormatLocaleString(image->filename,MagickPathExtent,
2187 "%s:%s",image->magick,unique);
2188 status=WriteImage(blob_info,image,exception);
2189 (void) fclose(blob_info->file);
2190 if (status != MagickFalse)
2191 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2193 (void) RelinquishUniqueFileResource(unique);
2196 blob_info=DestroyImageInfo(blob_info);
2228MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,
Image *image,
2241 assert(image_info != (
const ImageInfo *) NULL);
2242 assert(image_info->signature == MagickCoreSignature);
2243 assert(image != (
Image *) NULL);
2244 assert(image->signature == MagickCoreSignature);
2246 assert(image_info->custom_stream->signature == MagickCoreSignature);
2247 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2249 if (IsEventLogging() != MagickFalse)
2250 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2251 image_info->filename);
2252 clone_info=CloneImageInfo(image_info);
2253 clone_info->adjoin=MagickFalse;
2254 (void) SetImageInfo(clone_info,1,exception);
2255 if (*clone_info->magick !=
'\0')
2256 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2257 magick_info=GetMagickInfo(image->magick,exception);
2258 if (magick_info == (
const MagickInfo *) NULL)
2260 (void) ThrowMagickException(exception,GetMagickModule(),
2261 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2263 clone_info=DestroyImageInfo(clone_info);
2266 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2267 blob_support=GetMagickBlobSupport(magick_info);
2268 if ((blob_support != MagickFalse) &&
2269 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2271 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2272 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2273 blob_support=MagickFalse;
2275 if (blob_support != MagickFalse)
2280 (void) CloseBlob(image);
2281 *image->filename=
'\0';
2282 (void) WriteImage(clone_info,image,exception);
2287 unique[MagickPathExtent];
2299 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2301 if (blob == (
unsigned char *) NULL)
2303 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2304 image_info->filename);
2305 clone_info=DestroyImageInfo(clone_info);
2308 file=AcquireUniqueFileResource(unique);
2311 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2312 image_info->filename);
2313 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2314 clone_info=DestroyImageInfo(clone_info);
2317 clone_info->file=fdopen(file,
"wb+");
2318 if (clone_info->file != (FILE *) NULL)
2323 (void) FormatLocaleString(image->filename,MagickPathExtent,
2324 "%s:%s",image->magick,unique);
2325 status=WriteImage(clone_info,image,exception);
2326 if (status != MagickFalse)
2328 (void) fseek(clone_info->file,0,SEEK_SET);
2329 count=(ssize_t) MagickMaxBufferExtent;
2330 while (count == (ssize_t) MagickMaxBufferExtent)
2332 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2334 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2335 image_info->custom_stream->data);
2338 (void) fclose(clone_info->file);
2340 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2341 (void) RelinquishUniqueFileResource(unique);
2343 clone_info=DestroyImageInfo(clone_info);
2374MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
2399 assert(image != (
Image *) NULL);
2400 assert(image->signature == MagickCoreSignature);
2401 assert(image->blob != (
BlobInfo *) NULL);
2402 assert(image->blob->type != UndefinedStream);
2403 assert(filename != (
const char *) NULL);
2404 if (IsEventLogging() != MagickFalse)
2405 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2406 if (*filename ==
'\0')
2407 file=AcquireUniqueFileResource(filename);
2409 if (LocaleCompare(filename,
"-") == 0)
2410 file=fileno(stdout);
2412 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
2415 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2416 return(MagickFalse);
2418 quantum=(size_t) MagickMaxBufferExtent;
2419 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2420 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2421 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2422 if (buffer == (
unsigned char *) NULL)
2424 file=close_utf8(file)-1;
2425 (void) ThrowMagickException(exception,GetMagickModule(),
2426 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2427 return(MagickFalse);
2430 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2431 for (i=0; count > 0; )
2433 length=(size_t) count;
2434 for (i=0; i < length; i+=(size_t) count)
2436 count=write(file,p+i,(
size_t) (length-i));
2446 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2448 if (LocaleCompare(filename,
"-") != 0)
2449 file=close_utf8(file);
2450 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2451 if ((file == -1) || (i < length))
2454 file=close_utf8(file);
2455 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2456 return(MagickFalse);
2496MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,
Image *images,
2511 assert(image_info != (
const ImageInfo *) NULL);
2512 assert(image_info->signature == MagickCoreSignature);
2513 assert(images != (
Image *) NULL);
2514 assert(images->signature == MagickCoreSignature);
2516 if (IsEventLogging() != MagickFalse)
2517 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2518 image_info->filename);
2520 blob=(
unsigned char *) NULL;
2521 blob_info=CloneImageInfo(image_info);
2522 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2524 if (*blob_info->magick !=
'\0')
2525 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2526 magick_info=GetMagickInfo(images->magick,exception);
2527 if (magick_info == (
const MagickInfo *) NULL)
2529 (void) ThrowMagickException(exception,GetMagickModule(),
2530 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2532 blob_info=DestroyImageInfo(blob_info);
2535 if (GetMagickAdjoin(magick_info) == MagickFalse)
2537 blob_info=DestroyImageInfo(blob_info);
2538 return(ImageToBlob(image_info,images,length,exception));
2540 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2541 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2546 blob_info->length=0;
2547 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2548 sizeof(
unsigned char));
2549 if (blob_info->blob == (
void *) NULL)
2550 (void) ThrowMagickException(exception,GetMagickModule(),
2551 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2554 (void) CloseBlob(images);
2555 images->blob->exempt=MagickTrue;
2556 images->blob->extent=0;
2557 *images->filename=
'\0';
2558 status=WriteImages(blob_info,images,images->filename,exception);
2559 SyncBlobStream(blob_info,images,images);
2560 *length=images->blob->length;
2561 blob=DetachBlob(images->blob);
2562 if (blob != (
void *) NULL)
2564 if (status == MagickFalse)
2565 blob=RelinquishMagickMemory(blob);
2567 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2569 else if ((status == MagickFalse) && (images->blob->extent == 0))
2570 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2576 filename[MagickPathExtent],
2577 unique[MagickPathExtent];
2585 file=AcquireUniqueFileResource(unique);
2588 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2589 image_info->filename);
2593 blob_info->file=fdopen(file,
"wb");
2594 if (blob_info->file != (FILE *) NULL)
2596 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2597 images->magick,unique);
2598 status=WriteImages(blob_info,images,filename,exception);
2599 (void) fclose(blob_info->file);
2600 if (status != MagickFalse)
2601 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2603 (void) RelinquishUniqueFileResource(unique);
2606 blob_info=DestroyImageInfo(blob_info);
2638MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2651 assert(image_info != (
const ImageInfo *) NULL);
2652 assert(image_info->signature == MagickCoreSignature);
2653 assert(images != (
Image *) NULL);
2654 assert(images->signature == MagickCoreSignature);
2656 assert(image_info->custom_stream->signature == MagickCoreSignature);
2657 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2659 if (IsEventLogging() != MagickFalse)
2660 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2661 image_info->filename);
2662 clone_info=CloneImageInfo(image_info);
2663 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2665 if (*clone_info->magick !=
'\0')
2666 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2667 magick_info=GetMagickInfo(images->magick,exception);
2668 if (magick_info == (
const MagickInfo *) NULL)
2670 (void) ThrowMagickException(exception,GetMagickModule(),
2671 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2673 clone_info=DestroyImageInfo(clone_info);
2676 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2677 blob_support=GetMagickBlobSupport(magick_info);
2678 if ((blob_support != MagickFalse) &&
2679 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2681 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2682 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2683 blob_support=MagickFalse;
2685 if (blob_support != MagickFalse)
2690 (void) CloseBlob(images);
2691 *images->filename=
'\0';
2692 (void) WriteImages(clone_info,images,images->filename,exception);
2697 filename[MagickPathExtent],
2698 unique[MagickPathExtent];
2710 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2712 if (blob == (
unsigned char *) NULL)
2714 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2715 image_info->filename);
2716 clone_info=DestroyImageInfo(clone_info);
2719 file=AcquireUniqueFileResource(unique);
2722 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2723 image_info->filename);
2724 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2725 clone_info=DestroyImageInfo(clone_info);
2728 clone_info->file=fdopen(file,
"wb+");
2729 if (clone_info->file != (FILE *) NULL)
2734 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2735 images->magick,unique);
2736 status=WriteImages(clone_info,images,filename,exception);
2737 if (status != MagickFalse)
2739 (void) fseek(clone_info->file,0,SEEK_SET);
2740 count=(ssize_t) MagickMaxBufferExtent;
2741 while (count == (ssize_t) MagickMaxBufferExtent)
2743 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2745 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2746 image_info->custom_stream->data);
2749 (void) fclose(clone_info->file);
2751 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2752 (void) RelinquishUniqueFileResource(unique);
2754 clone_info=DestroyImageInfo(clone_info);
2790MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2794 filename[MagickPathExtent];
2823 assert(image_info != (
ImageInfo *) NULL);
2824 assert(image_info->signature == MagickCoreSignature);
2825 assert(image != (
Image *) NULL);
2826 assert(image->signature == MagickCoreSignature);
2827 assert(inject_image != (
Image *) NULL);
2828 assert(inject_image->signature == MagickCoreSignature);
2830 if (IsEventLogging() != MagickFalse)
2831 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2832 unique_file=(FILE *) NULL;
2833 file=AcquireUniqueFileResource(filename);
2835 unique_file=fdopen(file,
"wb");
2836 if ((file == -1) || (unique_file == (FILE *) NULL))
2838 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2839 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2841 return(MagickFalse);
2843 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2844 if (byte_image == (
Image *) NULL)
2846 (void) fclose(unique_file);
2847 (void) RelinquishUniqueFileResource(filename);
2848 return(MagickFalse);
2850 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2852 DestroyBlob(byte_image);
2853 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2854 write_info=CloneImageInfo(image_info);
2855 SetImageInfoFile(write_info,unique_file);
2856 status=WriteImage(write_info,byte_image,exception);
2857 write_info=DestroyImageInfo(write_info);
2858 byte_image=DestroyImage(byte_image);
2859 (void) fclose(unique_file);
2860 if (status == MagickFalse)
2862 (void) RelinquishUniqueFileResource(filename);
2863 return(MagickFalse);
2868 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2871 (void) RelinquishUniqueFileResource(filename);
2872 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2873 image_info->filename);
2874 return(MagickFalse);
2876 quantum=(size_t) MagickMaxBufferExtent;
2877 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2878 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2879 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2880 if (buffer == (
unsigned char *) NULL)
2882 (void) RelinquishUniqueFileResource(filename);
2883 file=close_utf8(file);
2884 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2889 ssize_t count = read(file,buffer,quantum);
2896 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2899 file=close_utf8(file);
2901 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2902 (void) RelinquishUniqueFileResource(filename);
2903 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2929MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2931 assert(image != (
const Image *) NULL);
2932 assert(image->signature == MagickCoreSignature);
2933 if (IsEventLogging() != MagickFalse)
2934 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2935 return(image->blob->exempt);
2960MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2963 *magick_restrict blob_info;
2965 assert(image != (
const Image *) NULL);
2966 assert(image->signature == MagickCoreSignature);
2967 if (IsEventLogging() != MagickFalse)
2968 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2969 blob_info=image->blob;
2970 switch (blob_info->type)
2979 if (blob_info->file_info.file == (FILE *) NULL)
2980 return(MagickFalse);
2981 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2982 return(status == -1 ? MagickFalse : MagickTrue);
2986#if defined(MAGICKCORE_ZLIB_DELEGATE)
2990 if (blob_info->file_info.gzfile == (gzFile) NULL)
2991 return(MagickFalse);
2992 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2993 return(offset < 0 ? MagickFalse : MagickTrue);
2998 case UndefinedStream:
3002 case StandardStream:
3006 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3007 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3014 return(MagickFalse);
3039MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3041 assert(image != (
const Image *) NULL);
3042 assert(image->signature == MagickCoreSignature);
3043 if (IsEventLogging() != MagickFalse)
3044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3045 return(image->blob->temporary);
3077MagickExport
void *MapBlob(
int file,
const MapMode mode,
3078 const MagickOffsetType offset,
const size_t length)
3080#if defined(MAGICKCORE_HAVE_MMAP)
3093#if defined(MAP_ANONYMOUS)
3094 flags|=MAP_ANONYMOUS;
3103 protection=PROT_READ;
3109 protection=PROT_WRITE;
3115 protection=PROT_READ | PROT_WRITE;
3120#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3121 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3123 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3124 if (map == MAP_FAILED)
3125 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3127 if (map == MAP_FAILED)
3164MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3173 assert(buffer != (
unsigned char *) NULL);
3180 *buffer++=(
unsigned char) c;
3184 *buffer++=(
unsigned char) c;
3214MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3222 assert(p != (
unsigned char *) NULL);
3229 *p++=(
unsigned char) c;
3265static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3277 size=MagickMinBufferExtent;
3278 option=GetImageOption(image_info,
"stream:buffer-size");
3279 if (option != (
const char *) NULL)
3280 size=StringToUnsignedLong(option);
3281 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3282 _IONBF : _IOFBF,size);
3283 return(status == 0 ? MagickTrue : MagickFalse);
3286#if defined(MAGICKCORE_ZLIB_DELEGATE)
3287static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3289#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3290 return(gzopen(path,mode));
3298 path_wide=create_wchar_path(path);
3299 if (path_wide == (
wchar_t *) NULL)
3300 return((gzFile) NULL);
3301 file=gzopen_w(path_wide,mode);
3302 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3308MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3312 *magick_restrict blob_info;
3315 extension[MagickPathExtent],
3316 filename[MagickPathExtent];
3330 assert(image_info != (
ImageInfo *) NULL);
3331 assert(image_info->signature == MagickCoreSignature);
3332 assert(image != (
Image *) NULL);
3333 assert(image->signature == MagickCoreSignature);
3334 if (IsEventLogging() != MagickFalse)
3335 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3336 image_info->filename);
3337 blob_info=image->blob;
3338 if (image_info->blob != (
void *) NULL)
3340 if (image_info->stream != (StreamHandler) NULL)
3341 blob_info->stream=(StreamHandler) image_info->stream;
3342 AttachBlob(blob_info,image_info->blob,image_info->length);
3346 (*image->filename ==
'\0'))
3348 blob_info->type=CustomStream;
3349 blob_info->custom_stream=image_info->custom_stream;
3352 (void) DetachBlob(blob_info);
3353 blob_info->mode=mode;
3362 case ReadBinaryBlobMode:
3364 flags=O_RDONLY | O_BINARY;
3370 flags=O_WRONLY | O_CREAT | O_TRUNC;
3374 case WriteBinaryBlobMode:
3376 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3380 case AppendBlobMode:
3382 flags=O_WRONLY | O_CREAT | O_APPEND;
3386 case AppendBinaryBlobMode:
3388 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3400 blob_info->synchronize=image_info->synchronize;
3401 if (image_info->stream != (StreamHandler) NULL)
3403 blob_info->stream=image_info->stream;
3406 blob_info->type=FifoStream;
3414 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3415 rights=ReadPolicyRights;
3417 rights=WritePolicyRights;
3418 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3421 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3422 "NotAuthorized",
"`%s'",filename);
3423 return(MagickFalse);
3425 if ((LocaleCompare(filename,
"-") == 0) ||
3426 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3428 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3429#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3430 if (strchr(type,
'b') != (
char *) NULL)
3431 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3433 blob_info->type=StandardStream;
3434 blob_info->exempt=MagickTrue;
3435 return(SetStreamBuffering(image_info,blob_info));
3437 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3438 (IsGeometry(filename+3) != MagickFalse))
3445 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3446 if (blob_info->file_info.file == (FILE *) NULL)
3448 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3449 return(MagickFalse);
3451#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3452 if (strchr(type,
'b') != (
char *) NULL)
3453 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3455 blob_info->type=FileStream;
3456 blob_info->exempt=MagickTrue;
3457 return(SetStreamBuffering(image_info,blob_info));
3459#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3460 if (*filename ==
'|')
3463 fileMode[MagickPathExtent],
3471 (void) signal(SIGPIPE,SIG_IGN);
3475 sanitize_command=SanitizeString(filename+1);
3476 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3477 sanitize_command=DestroyString(sanitize_command);
3478 if (blob_info->file_info.file == (FILE *) NULL)
3480 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3481 return(MagickFalse);
3483 blob_info->type=PipeStream;
3484 blob_info->exempt=MagickTrue;
3485 return(SetStreamBuffering(image_info,blob_info));
3488 status=GetPathAttributes(filename,&blob_info->properties);
3489#if defined(S_ISFIFO)
3490 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3492 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3493 if (blob_info->file_info.file == (FILE *) NULL)
3495 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3496 return(MagickFalse);
3498 blob_info->type=FileStream;
3499 blob_info->exempt=MagickTrue;
3500 return(SetStreamBuffering(image_info,blob_info));
3503 GetPathComponent(image->filename,ExtensionPath,extension);
3506 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3507 if ((image_info->adjoin == MagickFalse) ||
3508 (strchr(filename,
'%') != (
char *) NULL))
3513 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3514 image->scene,filename,exception);
3515 if ((LocaleCompare(filename,image->filename) == 0) &&
3516 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
3517 (GetNextImageInList(image) != (
Image *) NULL)))
3520 path[MagickPathExtent];
3522 GetPathComponent(image->filename,RootPath,path);
3523 if (*extension ==
'\0')
3524 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3525 path,(
double) image->scene);
3527 (
void) FormatLocaleString(filename,MagickPathExtent,
3528 "%s-%.20g.%s",path,(
double) image->scene,extension);
3530 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3532 if (IsPathAuthorized(rights,filename) == MagickFalse)
3535 (void) ThrowMagickException(exception,GetMagickModule(),
3536 PolicyError,
"NotAuthorized",
"`%s'",filename);
3537 return(MagickFalse);
3540 if (image_info->file != (FILE *) NULL)
3542 blob_info->file_info.file=image_info->file;
3543 blob_info->type=FileStream;
3544 blob_info->exempt=MagickTrue;
3552 blob_info->file_info.file=(FILE *) NULL;
3553#if defined(O_NOFOLLOW)
3554 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3555 if (status == MagickFalse)
3558 file=open_utf8(filename,flags,0);
3560 blob_info->file_info.file=fdopen(file,type);
3561 if (blob_info->file_info.file != (FILE *) NULL)
3569 blob_info->type=FileStream;
3570 (void) SetStreamBuffering(image_info,blob_info);
3571 (void) memset(magick,0,
sizeof(magick));
3572 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3573 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3574#if defined(MAGICKCORE_POSIX_SUPPORT)
3575 (void) fflush(blob_info->file_info.file);
3577 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3578 " read %.20g magic header bytes",(double) count);
3579#if defined(MAGICKCORE_ZLIB_DELEGATE)
3580 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3581 ((
int) magick[2] == 0x08))
3584 gzfile = gzopen_utf8(filename,
"rb");
3586 if (gzfile != (gzFile) NULL)
3588 if (blob_info->file_info.file != (FILE *) NULL)
3589 (void) fclose(blob_info->file_info.file);
3590 blob_info->file_info.file=(FILE *) NULL;
3591 blob_info->file_info.gzfile=gzfile;
3592 blob_info->type=ZipStream;
3596#if defined(MAGICKCORE_BZLIB_DELEGATE)
3597 if (strncmp((
char *) magick,
"BZh",3) == 0)
3600 *bzfile = BZ2_bzopen(filename,
"r");
3602 if (bzfile != (BZFILE *) NULL)
3604 if (blob_info->file_info.file != (FILE *) NULL)
3605 (void) fclose(blob_info->file_info.file);
3606 blob_info->file_info.file=(FILE *) NULL;
3607 blob_info->file_info.bzfile=bzfile;
3608 blob_info->type=BZipStream;
3612 if (blob_info->type == FileStream)
3623 sans_exception=AcquireExceptionInfo();
3624 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3625 sans_exception=DestroyExceptionInfo(sans_exception);
3626 length=(size_t) blob_info->properties.st_size;
3627 if ((magick_info != (
const MagickInfo *) NULL) &&
3628 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3629 (length > MagickMaxBufferExtent) &&
3630 (AcquireMagickResource(MapResource,length) != MagickFalse))
3635 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3637 if (blob == (
void *) NULL)
3638 RelinquishMagickResource(MapResource,length);
3644 if (image_info->file != (FILE *) NULL)
3645 blob_info->exempt=MagickFalse;
3648 (void) fclose(blob_info->file_info.file);
3649 blob_info->file_info.file=(FILE *) NULL;
3651 AttachBlob(blob_info,blob,length);
3652 blob_info->mapped=MagickTrue;
3659#if defined(MAGICKCORE_ZLIB_DELEGATE)
3660 if ((LocaleCompare(extension,
"gz") == 0) ||
3661 (LocaleCompare(extension,
"wmz") == 0) ||
3662 (LocaleCompare(extension,
"svgz") == 0))
3664 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3665 if (blob_info->file_info.gzfile != (gzFile) NULL)
3666 blob_info->type=ZipStream;
3670#if defined(MAGICKCORE_BZLIB_DELEGATE)
3671 if (LocaleCompare(extension,
"bz2") == 0)
3673 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3674 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3675 blob_info->type=BZipStream;
3683 blob_info->file_info.file=(FILE *) NULL;
3684#if defined(O_NOFOLLOW)
3685 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
3687 if (status == MagickFalse)
3690 file=open_utf8(filename,flags,0666);
3692 blob_info->file_info.file=fdopen(file,type);
3693 if (blob_info->file_info.file != (FILE *) NULL)
3695 blob_info->type=FileStream;
3696 (void) SetStreamBuffering(image_info,blob_info);
3699 blob_info->status=0;
3700 blob_info->error_number=0;
3701 if (blob_info->type != UndefinedStream)
3702 blob_info->size=GetBlobSize(image);
3705 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3706 return(MagickFalse);
3745#if defined(__cplusplus) || defined(c_plusplus)
3749static size_t PingStream(
const Image *magick_unused(image),
3750 const void *magick_unused(pixels),
const size_t columns)
3752 magick_unreferenced(image);
3753 magick_unreferenced(pixels);
3757#if defined(__cplusplus) || defined(c_plusplus)
3761MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3777 assert(image_info != (
ImageInfo *) NULL);
3778 assert(image_info->signature == MagickCoreSignature);
3780 if (IsEventLogging() != MagickFalse)
3781 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3782 image_info->filename);
3783 if ((blob == (
const void *) NULL) || (length == 0))
3785 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3786 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3787 return((
Image *) NULL);
3789 ping_info=CloneImageInfo(image_info);
3790 ping_info->blob=(
void *) blob;
3791 ping_info->length=length;
3792 ping_info->ping=MagickTrue;
3793 if (*ping_info->magick ==
'\0')
3794 (void) SetImageInfo(ping_info,0,exception);
3795 magick_info=GetMagickInfo(ping_info->magick,exception);
3796 if (magick_info == (
const MagickInfo *) NULL)
3798 (void) ThrowMagickException(exception,GetMagickModule(),
3799 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3801 ping_info=DestroyImageInfo(ping_info);
3802 return((
Image *) NULL);
3804 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3807 filename[MagickPathExtent];
3812 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3813 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3814 ping_info->magick,filename);
3815 image=ReadStream(ping_info,&PingStream,exception);
3816 if (image != (
Image *) NULL)
3817 (void) DetachBlob(image->blob);
3818 ping_info=DestroyImageInfo(ping_info);
3824 ping_info->blob=(
void *) NULL;
3825 ping_info->length=0;
3826 *ping_info->filename=
'\0';
3827 status=BlobToFile(ping_info->filename,blob,length,exception);
3828 if (status == MagickFalse)
3830 (void) RelinquishUniqueFileResource(ping_info->filename);
3831 ping_info=DestroyImageInfo(ping_info);
3832 return((
Image *) NULL);
3834 clone_info=CloneImageInfo(ping_info);
3835 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3836 ping_info->magick,ping_info->filename);
3837 image=ReadStream(clone_info,&PingStream,exception);
3838 if (image != (
Image *) NULL)
3846 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3848 (void) CopyMagickString(images->filename,image_info->filename,
3850 (void) CopyMagickString(images->magick_filename,image_info->filename,
3852 (void) CopyMagickString(images->magick,magick_info->name,
3854 images=GetNextImageInList(images);
3857 clone_info=DestroyImageInfo(clone_info);
3858 (void) RelinquishUniqueFileResource(ping_info->filename);
3859 ping_info=DestroyImageInfo(ping_info);
3894MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
void *data)
3897 *magick_restrict blob_info;
3908 assert(image != (
Image *) NULL);
3909 assert(image->signature == MagickCoreSignature);
3910 assert(image->blob != (
BlobInfo *) NULL);
3911 assert(image->blob->type != UndefinedStream);
3914 assert(data != (
void *) NULL);
3915 blob_info=image->blob;
3917 q=(
unsigned char *) data;
3918 switch (blob_info->type)
3920 case UndefinedStream:
3922 case StandardStream:
3930 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3935 c=getc(blob_info->file_info.file);
3938 *q++=(
unsigned char) c;
3944 c=getc(blob_info->file_info.file);
3947 *q++=(
unsigned char) c;
3953 c=getc(blob_info->file_info.file);
3956 *q++=(
unsigned char) c;
3962 c=getc(blob_info->file_info.file);
3965 *q++=(
unsigned char) c;
3972 if ((count != (ssize_t) length) &&
3973 (ferror(blob_info->file_info.file) != 0))
3974 ThrowBlobException(blob_info);
3979#if defined(MAGICKCORE_ZLIB_DELEGATE)
3990 for (i=0; i < length; i+=(size_t) count)
3992 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3993 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4006 c=gzgetc(blob_info->file_info.gzfile);
4009 *q++=(
unsigned char) c;
4015 c=gzgetc(blob_info->file_info.gzfile);
4018 *q++=(
unsigned char) c;
4024 c=gzgetc(blob_info->file_info.gzfile);
4027 *q++=(
unsigned char) c;
4033 c=gzgetc(blob_info->file_info.gzfile);
4036 *q++=(
unsigned char) c;
4043 (void) gzerror(blob_info->file_info.gzfile,&status);
4044 if ((count != (ssize_t) length) && (status != Z_OK))
4045 ThrowBlobException(blob_info);
4046 if (blob_info->eof == MagickFalse)
4047 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4054#if defined(MAGICKCORE_BZLIB_DELEGATE)
4061 for (i=0; i < length; i+=(size_t) count)
4063 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4064 MagickMin(length-i,MagickMaxBufferExtent));
4074 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4075 if ((count != (ssize_t) length) && (status != BZ_OK))
4076 ThrowBlobException(blob_info);
4077 if ((count != (ssize_t) length) && (status == BZ_OK))
4078 blob_info->eof=MagickTrue;
4089 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4091 blob_info->eof=MagickTrue;
4094 p=blob_info->data+blob_info->offset;
4095 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4096 blob_info->length-blob_info->offset);
4097 blob_info->offset+=count;
4098 if (count != (ssize_t) length)
4099 blob_info->eof=MagickTrue;
4100 (void) memcpy(q,p,(
size_t) count);
4105 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4106 count=blob_info->custom_stream->reader(q,length,
4107 blob_info->custom_stream->data);
4136MagickExport
int ReadBlobByte(
Image *image)
4139 *magick_restrict blob_info;
4144 assert(image != (
Image *) NULL);
4145 assert(image->signature == MagickCoreSignature);
4146 assert(image->blob != (
BlobInfo *) NULL);
4147 assert(image->blob->type != UndefinedStream);
4148 blob_info=image->blob;
4149 switch (blob_info->type)
4151 case StandardStream:
4155 c=getc(blob_info->file_info.file);
4158 if (ferror(blob_info->file_info.file) != 0)
4159 ThrowBlobException(blob_info);
4166 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4168 blob_info->eof=MagickTrue;
4171 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4172 blob_info->offset++;
4183 count=ReadBlob(image,1,buffer);
4216MagickExport
double ReadBlobDouble(
Image *image)
4227 quantum.double_value=0.0;
4228 quantum.unsigned_value=ReadBlobLongLong(image);
4229 return(quantum.double_value);
4255MagickExport
float ReadBlobFloat(
Image *image)
4266 quantum.float_value=0.0;
4267 quantum.unsigned_value=ReadBlobLong(image);
4268 return(quantum.float_value);
4294MagickExport
unsigned int ReadBlobLong(
Image *image)
4308 assert(image != (
Image *) NULL);
4309 assert(image->signature == MagickCoreSignature);
4311 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4314 if (image->endian == LSBEndian)
4316 value=(
unsigned int) (*p++);
4317 value|=(
unsigned int) (*p++) << 8;
4318 value|=(
unsigned int) (*p++) << 16;
4319 value|=(
unsigned int) (*p++) << 24;
4322 value=(
unsigned int) (*p++) << 24;
4323 value|=(
unsigned int) (*p++) << 16;
4324 value|=(
unsigned int) (*p++) << 8;
4325 value|=(
unsigned int) (*p++);
4352MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
4366 assert(image != (
Image *) NULL);
4367 assert(image->signature == MagickCoreSignature);
4369 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4371 return(MagickULLConstant(0));
4372 if (image->endian == LSBEndian)
4374 value=(MagickSizeType) (*p++);
4375 value|=(MagickSizeType) (*p++) << 8;
4376 value|=(MagickSizeType) (*p++) << 16;
4377 value|=(MagickSizeType) (*p++) << 24;
4378 value|=(MagickSizeType) (*p++) << 32;
4379 value|=(MagickSizeType) (*p++) << 40;
4380 value|=(MagickSizeType) (*p++) << 48;
4381 value|=(MagickSizeType) (*p++) << 56;
4384 value=(MagickSizeType) (*p++) << 56;
4385 value|=(MagickSizeType) (*p++) << 48;
4386 value|=(MagickSizeType) (*p++) << 40;
4387 value|=(MagickSizeType) (*p++) << 32;
4388 value|=(MagickSizeType) (*p++) << 24;
4389 value|=(MagickSizeType) (*p++) << 16;
4390 value|=(MagickSizeType) (*p++) << 8;
4391 value|=(MagickSizeType) (*p++);
4418MagickExport
unsigned short ReadBlobShort(
Image *image)
4432 assert(image != (
Image *) NULL);
4433 assert(image->signature == MagickCoreSignature);
4435 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4437 return((
unsigned short) 0U);
4438 if (image->endian == LSBEndian)
4440 value=(
unsigned short) (*p++);
4441 value|=(
unsigned short) (*p++) << 8;
4444 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4445 value|=(
unsigned short) (*p++);
4472MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
4486 assert(image != (
Image *) NULL);
4487 assert(image->signature == MagickCoreSignature);
4489 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4492 value=(
unsigned int) (*p++);
4493 value|=(
unsigned int) (*p++) << 8;
4494 value|=(
unsigned int) (*p++) << 16;
4495 value|=(
unsigned int) (*p++) << 24;
4522MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
4533 quantum.unsigned_value=ReadBlobLSBLong(image);
4534 return(quantum.signed_value);
4560MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
4574 assert(image != (
Image *) NULL);
4575 assert(image->signature == MagickCoreSignature);
4577 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4579 return((
unsigned short) 0U);
4580 value=(
unsigned short) (*p++);
4581 value|=(
unsigned short) (*p++) << 8;
4608MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
4619 quantum.unsigned_value=ReadBlobLSBShort(image);
4620 return(quantum.signed_value);
4646MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
4660 assert(image != (
Image *) NULL);
4661 assert(image->signature == MagickCoreSignature);
4663 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4666 value=(
unsigned int) (*p++) << 24;
4667 value|=(
unsigned int) (*p++) << 16;
4668 value|=(
unsigned int) (*p++) << 8;
4669 value|=(
unsigned int) (*p++);
4696MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
4710 assert(image != (
Image *) NULL);
4711 assert(image->signature == MagickCoreSignature);
4713 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4715 return(MagickULLConstant(0));
4716 value=(MagickSizeType) (*p++) << 56;
4717 value|=(MagickSizeType) (*p++) << 48;
4718 value|=(MagickSizeType) (*p++) << 40;
4719 value|=(MagickSizeType) (*p++) << 32;
4720 value|=(MagickSizeType) (*p++) << 24;
4721 value|=(MagickSizeType) (*p++) << 16;
4722 value|=(MagickSizeType) (*p++) << 8;
4723 value|=(MagickSizeType) (*p++);
4750MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
4764 assert(image != (
Image *) NULL);
4765 assert(image->signature == MagickCoreSignature);
4767 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4769 return((
unsigned short) 0U);
4770 value=(
unsigned short) ((*p++) << 8);
4771 value|=(
unsigned short) (*p++);
4772 return((
unsigned short) (value & 0xffff));
4798MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4809 quantum.unsigned_value=ReadBlobMSBLong(image);
4810 return(quantum.signed_value);
4836MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4847 quantum.unsigned_value=ReadBlobMSBShort(image);
4848 return(quantum.signed_value);
4874MagickExport
signed int ReadBlobSignedLong(
Image *image)
4885 quantum.unsigned_value=ReadBlobLong(image);
4886 return(quantum.signed_value);
4912MagickExport
signed short ReadBlobSignedShort(
Image *image)
4923 quantum.unsigned_value=ReadBlobShort(image);
4924 return(quantum.signed_value);
4962MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4963 const size_t length,
void *magick_restrict data,ssize_t *count)
4966 *magick_restrict blob_info;
4968 assert(image != (
Image *) NULL);
4969 assert(image->signature == MagickCoreSignature);
4970 assert(image->blob != (
BlobInfo *) NULL);
4971 assert(image->blob->type != UndefinedStream);
4972 assert(count != (ssize_t *) NULL);
4973 blob_info=image->blob;
4974 if (blob_info->type != BlobStream)
4976 assert(data != NULL);
4977 *count=ReadBlob(image,length,(
unsigned char *) data);
4980 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4983 blob_info->eof=MagickTrue;
4986 data=blob_info->data+blob_info->offset;
4987 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4988 blob_info->length-blob_info->offset);
4989 blob_info->offset+=(*count);
4990 if (*count != (ssize_t) length)
4991 blob_info->eof=MagickTrue;
5020MagickExport
char *ReadBlobString(
Image *image,
char *
string)
5023 *magick_restrict blob_info;
5031 assert(image != (
Image *) NULL);
5032 assert(image->signature == MagickCoreSignature);
5033 assert(image->blob != (
BlobInfo *) NULL);
5034 assert(image->blob->type != UndefinedStream);
5035 if (IsEventLogging() != MagickFalse)
5036 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5038 blob_info=image->blob;
5039 switch (blob_info->type)
5041 case UndefinedStream:
5043 case StandardStream:
5046 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5047 if (p == (
char *) NULL)
5049 if (ferror(blob_info->file_info.file) != 0)
5050 ThrowBlobException(blob_info);
5051 return((
char *) NULL);
5058#if defined(MAGICKCORE_ZLIB_DELEGATE)
5059 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5060 if (p == (
char *) NULL)
5063 (void) gzerror(blob_info->file_info.gzfile,&status);
5065 ThrowBlobException(blob_info);
5066 return((
char *) NULL);
5076 c=ReadBlobByte(image);
5079 blob_info->eof=MagickTrue;
5085 }
while (i < (MaxTextExtent-2));
5093 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5096 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5098 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5099 return((
char *) NULL);
5129 assert(blob->signature == MagickCoreSignature);
5130 if (IsEventLogging() != MagickFalse)
5131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5132 LockSemaphoreInfo(blob->semaphore);
5133 blob->reference_count++;
5134 UnlockSemaphoreInfo(blob->semaphore);
5171MagickExport MagickOffsetType SeekBlob(
Image *image,
5172 const MagickOffsetType offset,
const int whence)
5175 *magick_restrict blob_info;
5177 assert(image != (
Image *) NULL);
5178 assert(image->signature == MagickCoreSignature);
5179 assert(image->blob != (
BlobInfo *) NULL);
5180 assert(image->blob->type != UndefinedStream);
5181 if (IsEventLogging() != MagickFalse)
5182 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5183 blob_info=image->blob;
5184 switch (blob_info->type)
5186 case UndefinedStream:
5188 case StandardStream:
5193 if ((offset < 0) && (whence == SEEK_SET))
5195 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5197 blob_info->offset=TellBlob(image);
5202#if defined(MAGICKCORE_ZLIB_DELEGATE)
5203 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
5206 blob_info->offset=TellBlob(image);
5222 blob_info->offset=offset;
5227 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5228 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5233 if ((blob_info->offset+offset) < 0)
5235 blob_info->offset+=offset;
5240 if (((MagickOffsetType) blob_info->length+offset) < 0)
5242 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5246 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5248 blob_info->eof=MagickFalse;
5255 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5257 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5258 blob_info->custom_stream->data);
5262 return(blob_info->offset);
5290MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
5292 assert(image != (
const Image *) NULL);
5293 assert(image->signature == MagickCoreSignature);
5294 if (IsEventLogging() != MagickFalse)
5295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5296 image->blob->exempt=exempt;
5325MagickExport MagickBooleanType SetBlobExtent(
Image *image,
5326 const MagickSizeType extent)
5329 *magick_restrict blob_info;
5331 assert(image != (
Image *) NULL);
5332 assert(image->signature == MagickCoreSignature);
5333 assert(image->blob != (
BlobInfo *) NULL);
5334 assert(image->blob->type != UndefinedStream);
5335 if (IsEventLogging() != MagickFalse)
5336 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5337 blob_info=image->blob;
5338 switch (blob_info->type)
5340 case UndefinedStream:
5342 case StandardStream:
5343 return(MagickFalse);
5352 if (extent != (MagickSizeType) ((off_t) extent))
5353 return(MagickFalse);
5354 offset=SeekBlob(image,0,SEEK_END);
5356 return(MagickFalse);
5357 if ((MagickSizeType) offset >= extent)
5359 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5362 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5363 blob_info->file_info.file);
5364#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5365 if (blob_info->synchronize != MagickFalse)
5370 file=fileno(blob_info->file_info.file);
5371 if ((file == -1) || (offset < 0))
5372 return(MagickFalse);
5373 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5376 offset=SeekBlob(image,offset,SEEK_SET);
5378 return(MagickFalse);
5383 return(MagickFalse);
5385 return(MagickFalse);
5387 return(MagickFalse);
5390 if (extent != (MagickSizeType) ((
size_t) extent))
5391 return(MagickFalse);
5392 if (blob_info->mapped != MagickFalse)
5400 (void) UnmapBlob(blob_info->data,blob_info->length);
5401 RelinquishMagickResource(MapResource,blob_info->length);
5402 if (extent != (MagickSizeType) ((off_t) extent))
5403 return(MagickFalse);
5404 offset=SeekBlob(image,0,SEEK_END);
5406 return(MagickFalse);
5407 if ((MagickSizeType) offset >= extent)
5409 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5410 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5411 blob_info->file_info.file);
5412#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5413 if (blob_info->synchronize != MagickFalse)
5418 file=fileno(blob_info->file_info.file);
5419 if ((file == -1) || (offset < 0))
5420 return(MagickFalse);
5421 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5425 offset=SeekBlob(image,offset,SEEK_SET);
5427 return(MagickFalse);
5428 (void) AcquireMagickResource(MapResource,extent);
5429 blob_info->data=(
unsigned char*) MapBlob(fileno(
5430 blob_info->file_info.file),WriteMode,0,(size_t) extent);
5431 blob_info->extent=(size_t) extent;
5432 blob_info->length=(size_t) extent;
5433 (void) SyncBlob(image);
5436 blob_info->extent=(size_t) extent;
5437 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5438 blob_info->extent+1,
sizeof(*blob_info->data));
5439 (void) SyncBlob(image);
5440 if (blob_info->data == (
unsigned char *) NULL)
5442 (void) DetachBlob(blob_info);
5443 return(MagickFalse);
5481 assert(custom_stream->signature == MagickCoreSignature);
5482 custom_stream->data=data;
5511 CustomStreamHandler reader)
5514 assert(custom_stream->signature == MagickCoreSignature);
5515 custom_stream->reader=reader;
5544 CustomStreamSeeker seeker)
5547 assert(custom_stream->signature == MagickCoreSignature);
5548 custom_stream->seeker=seeker;
5577 CustomStreamTeller teller)
5580 assert(custom_stream->signature == MagickCoreSignature);
5581 custom_stream->teller=teller;
5610 CustomStreamHandler writer)
5613 assert(custom_stream->signature == MagickCoreSignature);
5614 custom_stream->writer=writer;
5641static int SyncBlob(
const Image *image)
5644 *magick_restrict blob_info;
5649 assert(image != (
Image *) NULL);
5650 assert(image->signature == MagickCoreSignature);
5651 assert(image->blob != (
BlobInfo *) NULL);
5652 if (IsEventLogging() != MagickFalse)
5653 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5654 if (EOFBlob(image) != 0)
5656 blob_info=image->blob;
5658 switch (blob_info->type)
5660 case UndefinedStream:
5661 case StandardStream:
5666 status=fflush(blob_info->file_info.file);
5671#if defined(MAGICKCORE_ZLIB_DELEGATE)
5672 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5678#if defined(MAGICKCORE_BZLIB_DELEGATE)
5679 status=BZ2_bzflush(blob_info->file_info.bzfile);
5724MagickPrivate
void SyncBlobStream(
ImageInfo *image_info,
Image *image,
5727 assert(owner != (
Image *) NULL);
5728 assert(image != (
Image *) NULL);
5729 assert(image->signature == MagickCoreSignature);
5730 if (image->blob == (
BlobInfo *) NULL)
5732 if (image->blob->data == (
unsigned char *) NULL)
5736 image_info->blob=(
void *) image->blob->data;
5737 image_info->length=image->blob->length;
5739 if ((image == owner) || (owner->blob == (
BlobInfo *) NULL))
5741 if (owner->blob == (
BlobInfo *) NULL)
5747 owner->blob->data=image->blob->data;
5748 owner->blob->extent=image->blob->extent;
5749 owner->blob->length=image->blob->length;
5750 owner->blob->offset=image->blob->offset;
5751 owner->blob->quantum=image->blob->quantum;
5752 owner->blob->type=BlobStream;
5753 owner->blob->exempt=MagickTrue;
5754 (void) DetachBlob(image->blob);
5779MagickExport MagickOffsetType TellBlob(
const Image *image)
5782 *magick_restrict blob_info;
5787 assert(image != (
Image *) NULL);
5788 assert(image->signature == MagickCoreSignature);
5789 assert(image->blob != (
BlobInfo *) NULL);
5790 assert(image->blob->type != UndefinedStream);
5791 if (IsEventLogging() != MagickFalse)
5792 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5793 blob_info=image->blob;
5795 switch (blob_info->type)
5797 case UndefinedStream:
5798 case StandardStream:
5802 offset=ftell(blob_info->file_info.file);
5809#if defined(MAGICKCORE_ZLIB_DELEGATE)
5810 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5820 offset=blob_info->offset;
5825 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5826 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5858MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5860#if defined(MAGICKCORE_HAVE_MMAP)
5864 status=munmap(map,length);
5865 return(status == -1 ? MagickFalse : MagickTrue);
5869 return(MagickFalse);
5901MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
5905 *magick_restrict blob_info;
5919 assert(image != (
Image *) NULL);
5920 assert(image->signature == MagickCoreSignature);
5921 assert(image->blob != (
BlobInfo *) NULL);
5922 assert(image->blob->type != UndefinedStream);
5925 assert(data != (
const void *) NULL);
5926 blob_info=image->blob;
5928 p=(
const unsigned char *) data;
5929 q=(
unsigned char *) data;
5930 switch (blob_info->type)
5932 case UndefinedStream:
5934 case StandardStream:
5942 count=(ssize_t) fwrite((
const char *) data,1,length,
5943 blob_info->file_info.file);
5948 c=putc((
int) *p++,blob_info->file_info.file);
5956 c=putc((
int) *p++,blob_info->file_info.file);
5964 c=putc((
int) *p++,blob_info->file_info.file);
5972 c=putc((
int) *p++,blob_info->file_info.file);
5981 if ((count != (ssize_t) length) &&
5982 (ferror(blob_info->file_info.file) != 0))
5983 ThrowBlobException(blob_info);
5988#if defined(MAGICKCORE_ZLIB_DELEGATE)
5999 for (i=0; i < length; i+=(size_t) count)
6001 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
6002 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
6015 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6023 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6031 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6039 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6049 (void) gzerror(blob_info->file_info.gzfile,&status);
6050 if ((count != (ssize_t) length) && (status != Z_OK))
6051 ThrowBlobException(blob_info);
6057#if defined(MAGICKCORE_BZLIB_DELEGATE)
6064 for (i=0; i < length; i+=(size_t) count)
6066 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6067 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6077 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6078 if ((count != (ssize_t) length) && (status != BZ_OK))
6079 ThrowBlobException(blob_info);
6085 count=(ssize_t) blob_info->stream(image,data,length);
6093 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6094 if (extent >= blob_info->extent)
6096 if (blob_info->mapped != MagickFalse)
6098 blob_info->extent=extent+blob_info->quantum+length;
6099 blob_info->quantum<<=1;
6100 blob_info->data=(
unsigned char *) ResizeQuantumMemory(
6101 blob_info->data,blob_info->extent+1,
sizeof(*blob_info->data));
6102 (void) SyncBlob(image);
6103 if (blob_info->data == (
unsigned char *) NULL)
6105 (void) DetachBlob(blob_info);
6109 q=blob_info->data+blob_info->offset;
6110 (void) memcpy(q,p,length);
6111 blob_info->offset+=(MagickOffsetType) length;
6112 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6113 blob_info->length=(
size_t) blob_info->offset;
6114 count=(ssize_t) length;
6119 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6120 count=blob_info->custom_stream->writer((
unsigned char *) data,
6121 length,blob_info->custom_stream->data);
6153MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
6156 *magick_restrict blob_info;
6161 assert(image != (
Image *) NULL);
6162 assert(image->signature == MagickCoreSignature);
6163 assert(image->blob != (
BlobInfo *) NULL);
6164 assert(image->blob->type != UndefinedStream);
6165 blob_info=image->blob;
6167 switch (blob_info->type)
6169 case StandardStream:
6176 c=putc((
int) value,blob_info->file_info.file);
6179 if (ferror(blob_info->file_info.file) != 0)
6180 ThrowBlobException(blob_info);
6188 count=WriteBlobStream(image,1,&value);
6220MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
6231 quantum.unsigned_value=0U;
6232 quantum.float_value=value;
6233 return(WriteBlobLong(image,quantum.unsigned_value));
6261MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
6266 assert(image != (
Image *) NULL);
6267 assert(image->signature == MagickCoreSignature);
6268 if (image->endian == LSBEndian)
6270 buffer[0]=(
unsigned char) value;
6271 buffer[1]=(
unsigned char) (value >> 8);
6272 buffer[2]=(
unsigned char) (value >> 16);
6273 buffer[3]=(
unsigned char) (value >> 24);
6274 return(WriteBlobStream(image,4,buffer));
6276 buffer[0]=(
unsigned char) (value >> 24);
6277 buffer[1]=(
unsigned char) (value >> 16);
6278 buffer[2]=(
unsigned char) (value >> 8);
6279 buffer[3]=(
unsigned char) value;
6280 return(WriteBlobStream(image,4,buffer));
6308MagickExport ssize_t WriteBlobLongLong(
Image *image,
const MagickSizeType value)
6313 assert(image != (
Image *) NULL);
6314 assert(image->signature == MagickCoreSignature);
6315 if (image->endian == LSBEndian)
6317 buffer[0]=(
unsigned char) value;
6318 buffer[1]=(
unsigned char) (value >> 8);
6319 buffer[2]=(
unsigned char) (value >> 16);
6320 buffer[3]=(
unsigned char) (value >> 24);
6321 buffer[4]=(
unsigned char) (value >> 32);
6322 buffer[5]=(
unsigned char) (value >> 40);
6323 buffer[6]=(
unsigned char) (value >> 48);
6324 buffer[7]=(
unsigned char) (value >> 56);
6325 return(WriteBlobStream(image,8,buffer));
6327 buffer[0]=(
unsigned char) (value >> 56);
6328 buffer[1]=(
unsigned char) (value >> 48);
6329 buffer[2]=(
unsigned char) (value >> 40);
6330 buffer[3]=(
unsigned char) (value >> 32);
6331 buffer[4]=(
unsigned char) (value >> 24);
6332 buffer[5]=(
unsigned char) (value >> 16);
6333 buffer[6]=(
unsigned char) (value >> 8);
6334 buffer[7]=(
unsigned char) value;
6335 return(WriteBlobStream(image,8,buffer));
6363MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
6368 assert(image != (
Image *) NULL);
6369 assert(image->signature == MagickCoreSignature);
6370 if (image->endian == LSBEndian)
6372 buffer[0]=(
unsigned char) value;
6373 buffer[1]=(
unsigned char) (value >> 8);
6374 return(WriteBlobStream(image,2,buffer));
6376 buffer[0]=(
unsigned char) (value >> 8);
6377 buffer[1]=(
unsigned char) value;
6378 return(WriteBlobStream(image,2,buffer));
6406MagickExport ssize_t WriteBlobSignedLong(
Image *image,
const signed int value)
6420 assert(image != (
Image *) NULL);
6421 assert(image->signature == MagickCoreSignature);
6422 quantum.signed_value=value;
6423 if (image->endian == LSBEndian)
6425 buffer[0]=(
unsigned char) quantum.unsigned_value;
6426 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6427 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6428 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6429 return(WriteBlobStream(image,4,buffer));
6431 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6432 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6433 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6434 buffer[3]=(
unsigned char) quantum.unsigned_value;
6435 return(WriteBlobStream(image,4,buffer));
6463MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
6468 assert(image != (
Image *) NULL);
6469 assert(image->signature == MagickCoreSignature);
6470 buffer[0]=(
unsigned char) value;
6471 buffer[1]=(
unsigned char) (value >> 8);
6472 buffer[2]=(
unsigned char) (value >> 16);
6473 buffer[3]=(
unsigned char) (value >> 24);
6474 return(WriteBlobStream(image,4,buffer));
6502MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
6507 assert(image != (
Image *) NULL);
6508 assert(image->signature == MagickCoreSignature);
6509 buffer[0]=(
unsigned char) value;
6510 buffer[1]=(
unsigned char) (value >> 8);
6511 return(WriteBlobStream(image,2,buffer));
6539MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
6553 assert(image != (
Image *) NULL);
6554 assert(image->signature == MagickCoreSignature);
6555 quantum.signed_value=value;
6556 buffer[0]=(
unsigned char) quantum.unsigned_value;
6557 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6558 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6559 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6560 return(WriteBlobStream(image,4,buffer));
6588MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
6589 const signed short value)
6603 assert(image != (
Image *) NULL);
6604 assert(image->signature == MagickCoreSignature);
6605 quantum.signed_value=value;
6606 buffer[0]=(
unsigned char) quantum.unsigned_value;
6607 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6608 return(WriteBlobStream(image,2,buffer));
6636MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
6641 assert(image != (
Image *) NULL);
6642 assert(image->signature == MagickCoreSignature);
6643 buffer[0]=(
unsigned char) (value >> 24);
6644 buffer[1]=(
unsigned char) (value >> 16);
6645 buffer[2]=(
unsigned char) (value >> 8);
6646 buffer[3]=(
unsigned char) value;
6647 return(WriteBlobStream(image,4,buffer));
6675MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
6676 const signed short value)
6690 assert(image != (
Image *) NULL);
6691 assert(image->signature == MagickCoreSignature);
6692 quantum.signed_value=value;
6693 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6694 buffer[1]=(
unsigned char) quantum.unsigned_value;
6695 return(WriteBlobStream(image,2,buffer));
6723MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
6728 assert(image != (
Image *) NULL);
6729 assert(image->signature == MagickCoreSignature);
6730 buffer[0]=(
unsigned char) (value >> 8);
6731 buffer[1]=(
unsigned char) value;
6732 return(WriteBlobStream(image,2,buffer));
6760MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
6762 assert(image != (
Image *) NULL);
6763 assert(image->signature == MagickCoreSignature);
6764 assert(
string != (
const char *) NULL);
6765 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));