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);
900 count=(ssize_t) write(file,(
const char *) blob,(size_t) count);
902 (void) fclose(blob_info->file);
903 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
904 "%s:%s",blob_info->magick,unique);
905 image=ReadImage(clone_info,exception);
906 if (image != (
Image *) NULL)
914 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
916 (void) CopyMagickString(images->filename,image_info->filename,
918 (void) CopyMagickString(images->magick_filename,
919 image_info->filename,MagickPathExtent);
920 (void) CopyMagickString(images->magick,magick_info->name,
922 images=GetNextImageInList(images);
926 clone_info=DestroyImageInfo(clone_info);
927 blob=(
unsigned char *) RelinquishMagickMemory(blob);
928 (void) RelinquishUniqueFileResource(unique);
930 blob_info=DestroyImageInfo(blob_info);
931 if (image != (
Image *) NULL)
932 if (CloseBlob(image) == MagickFalse)
933 image=DestroyImageList(image);
959MagickExport
void DestroyBlob(
Image *image)
962 *magick_restrict blob_info;
967 assert(image != (
Image *) NULL);
968 assert(image->signature == MagickCoreSignature);
969 assert(image->blob != (
BlobInfo *) NULL);
970 assert(image->blob->signature == MagickCoreSignature);
971 if (IsEventLogging() != MagickFalse)
972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
973 blob_info=image->blob;
975 LockSemaphoreInfo(blob_info->semaphore);
976 blob_info->reference_count--;
977 assert(blob_info->reference_count >= 0);
978 if (blob_info->reference_count == 0)
980 UnlockSemaphoreInfo(blob_info->semaphore);
981 if (destroy == MagickFalse)
986 (void) CloseBlob(image);
987 if (blob_info->mapped != MagickFalse)
989 (void) UnmapBlob(blob_info->data,blob_info->length);
990 RelinquishMagickResource(MapResource,blob_info->length);
993 RelinquishSemaphoreInfo(&blob_info->semaphore);
994 blob_info->signature=(~MagickCoreSignature);
995 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
1025 assert(custom_stream->signature == MagickCoreSignature);
1026 if (IsEventLogging() != MagickFalse)
1027 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1028 custom_stream->signature=(~MagickCoreSignature);
1030 return(custom_stream);
1055MagickExport
void *DetachBlob(
BlobInfo *blob_info)
1060 assert(blob_info != (
BlobInfo *) NULL);
1061 if (IsEventLogging() != MagickFalse)
1062 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1063 if (blob_info->mapped != MagickFalse)
1065 (void) UnmapBlob(blob_info->data,blob_info->length);
1066 blob_info->data=NULL;
1067 RelinquishMagickResource(MapResource,blob_info->length);
1069 blob_info->mapped=MagickFalse;
1070 blob_info->length=0;
1075 blob_info->offset=0;
1076 blob_info->mode=UndefinedBlobMode;
1077 blob_info->eof=MagickFalse;
1079 blob_info->exempt=MagickFalse;
1080 blob_info->type=UndefinedStream;
1081 blob_info->file_info.file=(FILE *) NULL;
1082 data=blob_info->data;
1083 blob_info->data=(
unsigned char *) NULL;
1084 blob_info->stream=(StreamHandler) NULL;
1113MagickExport
void DisassociateBlob(
Image *image)
1116 *magick_restrict blob_info,
1122 assert(image != (
Image *) NULL);
1123 assert(image->signature == MagickCoreSignature);
1124 assert(image->blob != (
BlobInfo *) NULL);
1125 assert(image->blob->signature == MagickCoreSignature);
1126 if (IsEventLogging() != MagickFalse)
1127 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1128 blob_info=image->blob;
1130 LockSemaphoreInfo(blob_info->semaphore);
1131 assert(blob_info->reference_count >= 0);
1132 if (blob_info->reference_count > 1)
1134 UnlockSemaphoreInfo(blob_info->semaphore);
1135 if (clone == MagickFalse)
1137 clone_info=CloneBlobInfo(blob_info);
1139 image->blob=clone_info;
1167MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
1168 const MagickSizeType length)
1180 buffer[MagickMinBufferExtent >> 1];
1182 assert(image != (
Image *) NULL);
1183 assert(image->signature == MagickCoreSignature);
1184 if (length != (MagickSizeType) ((MagickOffsetType) length))
1185 return(MagickFalse);
1187 for (i=0; i < length; i+=(MagickSizeType) count)
1189 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1190 (void) ReadBlobStream(image,quantum,buffer,&count);
1198 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1225MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
1227 assert(image != (
Image *) NULL);
1228 assert(image->signature == MagickCoreSignature);
1229 assert(duplicate != (
Image *) NULL);
1230 assert(duplicate->signature == MagickCoreSignature);
1231 if (IsEventLogging() != MagickFalse)
1232 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1234 image->blob=ReferenceBlob(duplicate->blob);
1260MagickExport
int EOFBlob(
const Image *image)
1263 *magick_restrict blob_info;
1265 assert(image != (
Image *) NULL);
1266 assert(image->signature == MagickCoreSignature);
1267 assert(image->blob != (
BlobInfo *) NULL);
1268 assert(image->blob->type != UndefinedStream);
1269 if (IsEventLogging() != MagickFalse)
1270 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1271 blob_info=image->blob;
1272 switch (blob_info->type)
1274 case UndefinedStream:
1275 case StandardStream:
1280 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1286#if defined(MAGICKCORE_ZLIB_DELEGATE)
1287 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1294#if defined(MAGICKCORE_BZLIB_DELEGATE)
1299 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1300 if (status == BZ_UNEXPECTED_EOF)
1301 blob_info->eof=MagickTrue;
1307 blob_info->eof=MagickFalse;
1315 return((
int) blob_info->eof);
1341MagickExport
int ErrorBlob(
const Image *image)
1344 *magick_restrict blob_info;
1346 assert(image != (
Image *) NULL);
1347 assert(image->signature == MagickCoreSignature);
1348 assert(image->blob != (
BlobInfo *) NULL);
1349 assert(image->blob->type != UndefinedStream);
1350 if (IsEventLogging() != MagickFalse)
1351 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1352 blob_info=image->blob;
1353 switch (blob_info->type)
1355 case UndefinedStream:
1356 case StandardStream:
1361 blob_info->error=ferror(blob_info->file_info.file);
1366#if defined(MAGICKCORE_ZLIB_DELEGATE)
1367 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1373#if defined(MAGICKCORE_BZLIB_DELEGATE)
1374 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1388 return(blob_info->error);
1426MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1453 assert(filename != (
const char *) NULL);
1455 assert(exception->signature == MagickCoreSignature);
1456 if (IsEventLogging() != MagickFalse)
1457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1459 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1460 if (status == MagickFalse)
1463 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1464 "NotAuthorized",
"`%s'",filename);
1468 if (LocaleCompare(filename,
"-") != 0)
1471 flags = O_RDONLY | O_BINARY;
1473 status=GetPathAttributes(filename,&attributes);
1474 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1476 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1479#if defined(O_NOFOLLOW)
1480 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1481 if (status == MagickFalse)
1484 file=open_utf8(filename,flags,0);
1488 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1491 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1492 if (status == MagickFalse)
1494 file=close_utf8(file)-1;
1496 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1497 "NotAuthorized",
"`%s'",filename);
1500 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1502 if ((file == fileno(stdin)) || (offset < 0) ||
1503 (offset != (MagickOffsetType) ((ssize_t) offset)))
1514 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1515 quantum=(size_t) MagickMaxBufferExtent;
1516 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1517 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1518 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1519 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1521 count=read(file,blob+i,quantum);
1528 if (~i < ((
size_t) count+quantum+1))
1530 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1533 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1534 quantum+1,
sizeof(*blob));
1535 if ((i+(
size_t) count) >= extent)
1538 if (LocaleCompare(filename,
"-") != 0)
1539 file=close_utf8(file);
1540 if (blob == (
unsigned char *) NULL)
1542 (void) ThrowMagickException(exception,GetMagickModule(),
1543 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1548 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1549 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1552 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1556 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1557 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1558 blob=(
unsigned char *) NULL;
1559 if (~(*length) >= (MagickPathExtent-1))
1560 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1562 if (blob == (
unsigned char *) NULL)
1564 file=close_utf8(file);
1565 (void) ThrowMagickException(exception,GetMagickModule(),
1566 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1569 map=MapBlob(file,ReadMode,0,*length);
1570 if (map != (
unsigned char *) NULL)
1572 (void) memcpy(blob,map,*length);
1573 (void) UnmapBlob(map,*length);
1577 (void) lseek(file,0,SEEK_SET);
1578 for (i=0; i < *length; i+=(size_t) count)
1580 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1581 MagickMaxBufferExtent));
1591 file=close_utf8(file)-1;
1592 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1593 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1598 if (LocaleCompare(filename,
"-") != 0)
1599 file=close_utf8(file);
1602 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1603 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1632static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1633 const void *magick_restrict data)
1636 *magick_restrict blob_info;
1644 assert(image->blob != (
BlobInfo *) NULL);
1645 assert(image->blob->type != UndefinedStream);
1646 assert(data != NULL);
1647 blob_info=image->blob;
1648 if (blob_info->type != BlobStream)
1649 return(WriteBlob(image,length,(
const unsigned char *) data));
1650 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1651 if (extent >= blob_info->extent)
1653 extent+=blob_info->quantum+length;
1654 blob_info->quantum<<=1;
1655 if (SetBlobExtent(image,extent) == MagickFalse)
1658 q=blob_info->data+blob_info->offset;
1659 (void) memcpy(q,data,length);
1660 blob_info->offset+=(MagickOffsetType) length;
1661 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1662 blob_info->length=(
size_t) blob_info->offset;
1663 return((ssize_t) length);
1666MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename,
1688 assert(image != (
const Image *) NULL);
1689 assert(image->signature == MagickCoreSignature);
1690 assert(filename != (
const char *) NULL);
1691 if (IsEventLogging() != MagickFalse)
1692 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1693 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1694 if (status == MagickFalse)
1697 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1698 "NotAuthorized",
"`%s'",filename);
1699 return(MagickFalse);
1702 if (LocaleCompare(filename,
"-") != 0)
1705 flags = O_RDONLY | O_BINARY;
1707#if defined(O_NOFOLLOW)
1708 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1709 if (status == MagickFalse)
1712 file=open_utf8(filename,flags,0);
1716 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1717 return(MagickFalse);
1719 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1720 if (status == MagickFalse)
1722 file=close_utf8(file);
1724 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1725 "NotAuthorized",
"`%s'",filename);
1726 return(MagickFalse);
1728 quantum=(size_t) MagickMaxBufferExtent;
1729 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1730 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1731 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1732 if (blob == (
unsigned char *) NULL)
1734 file=close_utf8(file);
1735 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1737 return(MagickFalse);
1741 count=read(file,blob,quantum);
1748 length=(size_t) count;
1749 count=WriteBlobStream(image,length,blob);
1750 if (count != (ssize_t) length)
1752 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1756 file=close_utf8(file);
1758 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1759 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1786MagickExport MagickBooleanType GetBlobError(
const Image *image)
1788 assert(image != (
const Image *) NULL);
1789 assert(image->signature == MagickCoreSignature);
1790 if (IsEventLogging() != MagickFalse)
1791 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1792 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1793 errno=image->blob->error_number;
1794 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1820MagickExport FILE *GetBlobFileHandle(
const Image *image)
1822 assert(image != (
const Image *) NULL);
1823 assert(image->signature == MagickCoreSignature);
1824 assert(image->blob != (
BlobInfo *) NULL);
1825 assert(image->blob->signature == MagickCoreSignature);
1826 switch (image->blob->type)
1828 case StandardStream:
1831 return(image->blob->file_info.file);
1832 case UndefinedStream:
1840 return((FILE *) NULL);
1865MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1867 assert(blob_info != (
BlobInfo *) NULL);
1868 (void) memset(blob_info,0,
sizeof(*blob_info));
1869 blob_info->type=UndefinedStream;
1870 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1871 blob_info->properties.st_mtime=GetMagickTime();
1872 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1873 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1874 blob_info->reference_count=1;
1875 blob_info->semaphore=AcquireSemaphoreInfo();
1876 blob_info->signature=MagickCoreSignature;
1901MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1903 assert(image != (
Image *) NULL);
1904 assert(image->signature == MagickCoreSignature);
1905 if (IsEventLogging() != MagickFalse)
1906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1907 return(&image->blob->properties);
1933MagickExport MagickSizeType GetBlobSize(
const Image *image)
1936 *magick_restrict blob_info;
1941 assert(image != (
Image *) NULL);
1942 assert(image->signature == MagickCoreSignature);
1943 assert(image->blob != (
BlobInfo *) NULL);
1944 if (IsEventLogging() != MagickFalse)
1945 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1946 blob_info=image->blob;
1948 switch (blob_info->type)
1950 case UndefinedStream:
1951 case StandardStream:
1953 extent=blob_info->size;
1961 extent=(MagickSizeType) blob_info->properties.st_size;
1963 extent=blob_info->size;
1964 file_descriptor=fileno(blob_info->file_info.file);
1965 if (file_descriptor == -1)
1967 if (fstat(file_descriptor,&blob_info->properties) == 0)
1968 extent=(MagickSizeType) blob_info->properties.st_size;
1973 extent=blob_info->size;
1982 status=GetPathAttributes(image->filename,&blob_info->properties);
1983 if (status != MagickFalse)
1984 extent=(MagickSizeType) blob_info->properties.st_size;
1991 extent=(MagickSizeType) blob_info->length;
1996 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1997 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
2002 offset=blob_info->custom_stream->teller(
2003 blob_info->custom_stream->data);
2004 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
2005 blob_info->custom_stream->data);
2006 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
2007 blob_info->custom_stream->data);
2037MagickExport
void *GetBlobStreamData(
const Image *image)
2039 assert(image != (
const Image *) NULL);
2040 assert(image->signature == MagickCoreSignature);
2041 return(image->blob->data);
2066MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2068 assert(image != (
const Image *) NULL);
2069 assert(image->signature == MagickCoreSignature);
2070 if (IsEventLogging() != MagickFalse)
2071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2072 return(image->blob->stream);
2108MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2123 assert(image_info != (
const ImageInfo *) NULL);
2124 assert(image_info->signature == MagickCoreSignature);
2125 assert(image != (
Image *) NULL);
2126 assert(image->signature == MagickCoreSignature);
2128 assert(exception->signature == MagickCoreSignature);
2129 if (IsEventLogging() != MagickFalse)
2130 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2131 image_info->filename);
2133 blob=(
unsigned char *) NULL;
2134 blob_info=CloneImageInfo(image_info);
2135 blob_info->adjoin=MagickFalse;
2136 (void) SetImageInfo(blob_info,1,exception);
2137 if (*blob_info->magick !=
'\0')
2138 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2139 magick_info=GetMagickInfo(image->magick,exception);
2140 if (magick_info == (
const MagickInfo *) NULL)
2142 (void) ThrowMagickException(exception,GetMagickModule(),
2143 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2145 blob_info=DestroyImageInfo(blob_info);
2148 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2149 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2154 blob_info->length=0;
2155 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2156 sizeof(
unsigned char));
2157 if (blob_info->blob == NULL)
2158 (void) ThrowMagickException(exception,GetMagickModule(),
2159 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2162 (void) CloseBlob(image);
2163 image->blob->exempt=MagickTrue;
2164 image->blob->extent=0;
2165 *image->filename=
'\0';
2166 status=WriteImage(blob_info,image,exception);
2167 SyncBlobStream(blob_info,image,image);
2168 *length=image->blob->length;
2169 blob=DetachBlob(image->blob);
2170 if (blob != (
void *) NULL)
2172 if (status == MagickFalse)
2173 blob=RelinquishMagickMemory(blob);
2175 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2178 if ((status == MagickFalse) && (image->blob->extent == 0))
2179 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2185 unique[MagickPathExtent];
2193 file=AcquireUniqueFileResource(unique);
2196 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2197 image_info->filename);
2201 blob_info->file=fdopen(file,
"wb");
2202 if (blob_info->file != (FILE *) NULL)
2204 (void) FormatLocaleString(image->filename,MagickPathExtent,
2205 "%s:%s",image->magick,unique);
2206 status=WriteImage(blob_info,image,exception);
2207 (void) fclose(blob_info->file);
2208 if (status != MagickFalse)
2209 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2211 (void) RelinquishUniqueFileResource(unique);
2214 blob_info=DestroyImageInfo(blob_info);
2246MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,
Image *image,
2259 assert(image_info != (
const ImageInfo *) NULL);
2260 assert(image_info->signature == MagickCoreSignature);
2261 assert(image != (
Image *) NULL);
2262 assert(image->signature == MagickCoreSignature);
2264 assert(image_info->custom_stream->signature == MagickCoreSignature);
2265 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2267 if (IsEventLogging() != MagickFalse)
2268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2269 image_info->filename);
2270 clone_info=CloneImageInfo(image_info);
2271 clone_info->adjoin=MagickFalse;
2272 (void) SetImageInfo(clone_info,1,exception);
2273 if (*clone_info->magick !=
'\0')
2274 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2275 magick_info=GetMagickInfo(image->magick,exception);
2276 if (magick_info == (
const MagickInfo *) NULL)
2278 (void) ThrowMagickException(exception,GetMagickModule(),
2279 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2281 clone_info=DestroyImageInfo(clone_info);
2284 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2285 blob_support=GetMagickBlobSupport(magick_info);
2286 if ((blob_support != MagickFalse) &&
2287 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2289 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2290 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2291 blob_support=MagickFalse;
2293 if (blob_support != MagickFalse)
2298 (void) CloseBlob(image);
2299 *image->filename=
'\0';
2300 (void) WriteImage(clone_info,image,exception);
2305 unique[MagickPathExtent];
2317 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2319 if (blob == (
unsigned char *) NULL)
2321 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2322 image_info->filename);
2323 clone_info=DestroyImageInfo(clone_info);
2326 file=AcquireUniqueFileResource(unique);
2329 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2330 image_info->filename);
2331 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2332 clone_info=DestroyImageInfo(clone_info);
2335 clone_info->file=fdopen(file,
"wb+");
2336 if (clone_info->file != (FILE *) NULL)
2341 (void) FormatLocaleString(image->filename,MagickPathExtent,
2342 "%s:%s",image->magick,unique);
2343 status=WriteImage(clone_info,image,exception);
2344 if (status != MagickFalse)
2346 (void) fseek(clone_info->file,0,SEEK_SET);
2347 count=(ssize_t) MagickMaxBufferExtent;
2348 while (count == (ssize_t) MagickMaxBufferExtent)
2350 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2352 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2353 image_info->custom_stream->data);
2356 (void) fclose(clone_info->file);
2358 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2359 (void) RelinquishUniqueFileResource(unique);
2361 clone_info=DestroyImageInfo(clone_info);
2392MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
2417 assert(image != (
Image *) NULL);
2418 assert(image->signature == MagickCoreSignature);
2419 assert(image->blob != (
BlobInfo *) NULL);
2420 assert(image->blob->type != UndefinedStream);
2421 assert(filename != (
const char *) NULL);
2422 if (IsEventLogging() != MagickFalse)
2423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2424 if (*filename ==
'\0')
2425 file=AcquireUniqueFileResource(filename);
2427 if (LocaleCompare(filename,
"-") == 0)
2428 file=fileno(stdout);
2430 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
2433 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2434 return(MagickFalse);
2436 quantum=(size_t) MagickMaxBufferExtent;
2437 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2438 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2439 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2440 if (buffer == (
unsigned char *) NULL)
2442 file=close_utf8(file)-1;
2443 (void) ThrowMagickException(exception,GetMagickModule(),
2444 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2445 return(MagickFalse);
2448 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2449 for (i=0; count > 0; )
2451 length=(size_t) count;
2452 for (i=0; i < length; i+=(size_t) count)
2454 count=write(file,p+i,(
size_t) (length-i));
2464 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2466 if (LocaleCompare(filename,
"-") != 0)
2467 file=close_utf8(file);
2468 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2469 if ((file == -1) || (i < length))
2472 file=close_utf8(file);
2473 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2474 return(MagickFalse);
2514MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,
Image *images,
2529 assert(image_info != (
const ImageInfo *) NULL);
2530 assert(image_info->signature == MagickCoreSignature);
2531 assert(images != (
Image *) NULL);
2532 assert(images->signature == MagickCoreSignature);
2534 if (IsEventLogging() != MagickFalse)
2535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2536 image_info->filename);
2538 blob=(
unsigned char *) NULL;
2539 blob_info=CloneImageInfo(image_info);
2540 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2542 if (*blob_info->magick !=
'\0')
2543 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2544 magick_info=GetMagickInfo(images->magick,exception);
2545 if (magick_info == (
const MagickInfo *) NULL)
2547 (void) ThrowMagickException(exception,GetMagickModule(),
2548 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2550 blob_info=DestroyImageInfo(blob_info);
2553 if (GetMagickAdjoin(magick_info) == MagickFalse)
2555 blob_info=DestroyImageInfo(blob_info);
2556 return(ImageToBlob(image_info,images,length,exception));
2558 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2559 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2564 blob_info->length=0;
2565 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2566 sizeof(
unsigned char));
2567 if (blob_info->blob == (
void *) NULL)
2568 (void) ThrowMagickException(exception,GetMagickModule(),
2569 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2572 (void) CloseBlob(images);
2573 images->blob->exempt=MagickTrue;
2574 images->blob->extent=0;
2575 *images->filename=
'\0';
2576 status=WriteImages(blob_info,images,images->filename,exception);
2577 SyncBlobStream(blob_info,images,images);
2578 *length=images->blob->length;
2579 blob=DetachBlob(images->blob);
2580 if (blob != (
void *) NULL)
2582 if (status == MagickFalse)
2583 blob=RelinquishMagickMemory(blob);
2585 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2587 else if ((status == MagickFalse) && (images->blob->extent == 0))
2588 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2594 filename[MagickPathExtent],
2595 unique[MagickPathExtent];
2603 file=AcquireUniqueFileResource(unique);
2606 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2607 image_info->filename);
2611 blob_info->file=fdopen(file,
"wb");
2612 if (blob_info->file != (FILE *) NULL)
2614 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2615 images->magick,unique);
2616 status=WriteImages(blob_info,images,filename,exception);
2617 (void) fclose(blob_info->file);
2618 if (status != MagickFalse)
2619 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2621 (void) RelinquishUniqueFileResource(unique);
2624 blob_info=DestroyImageInfo(blob_info);
2656MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2669 assert(image_info != (
const ImageInfo *) NULL);
2670 assert(image_info->signature == MagickCoreSignature);
2671 assert(images != (
Image *) NULL);
2672 assert(images->signature == MagickCoreSignature);
2674 assert(image_info->custom_stream->signature == MagickCoreSignature);
2675 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2677 if (IsEventLogging() != MagickFalse)
2678 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2679 image_info->filename);
2680 clone_info=CloneImageInfo(image_info);
2681 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2683 if (*clone_info->magick !=
'\0')
2684 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2685 magick_info=GetMagickInfo(images->magick,exception);
2686 if (magick_info == (
const MagickInfo *) NULL)
2688 (void) ThrowMagickException(exception,GetMagickModule(),
2689 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2691 clone_info=DestroyImageInfo(clone_info);
2694 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2695 blob_support=GetMagickBlobSupport(magick_info);
2696 if ((blob_support != MagickFalse) &&
2697 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2699 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2700 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2701 blob_support=MagickFalse;
2703 if (blob_support != MagickFalse)
2708 (void) CloseBlob(images);
2709 *images->filename=
'\0';
2710 (void) WriteImages(clone_info,images,images->filename,exception);
2715 filename[MagickPathExtent],
2716 unique[MagickPathExtent];
2728 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2730 if (blob == (
unsigned char *) NULL)
2732 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2733 image_info->filename);
2734 clone_info=DestroyImageInfo(clone_info);
2737 file=AcquireUniqueFileResource(unique);
2740 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2741 image_info->filename);
2742 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2743 clone_info=DestroyImageInfo(clone_info);
2746 clone_info->file=fdopen(file,
"wb+");
2747 if (clone_info->file != (FILE *) NULL)
2752 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2753 images->magick,unique);
2754 status=WriteImages(clone_info,images,filename,exception);
2755 if (status != MagickFalse)
2757 (void) fseek(clone_info->file,0,SEEK_SET);
2758 count=(ssize_t) MagickMaxBufferExtent;
2759 while (count == (ssize_t) MagickMaxBufferExtent)
2761 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2763 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2764 image_info->custom_stream->data);
2767 (void) fclose(clone_info->file);
2769 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2770 (void) RelinquishUniqueFileResource(unique);
2772 clone_info=DestroyImageInfo(clone_info);
2808MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2812 filename[MagickPathExtent];
2841 assert(image_info != (
ImageInfo *) NULL);
2842 assert(image_info->signature == MagickCoreSignature);
2843 assert(image != (
Image *) NULL);
2844 assert(image->signature == MagickCoreSignature);
2845 assert(inject_image != (
Image *) NULL);
2846 assert(inject_image->signature == MagickCoreSignature);
2848 if (IsEventLogging() != MagickFalse)
2849 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2850 unique_file=(FILE *) NULL;
2851 file=AcquireUniqueFileResource(filename);
2853 unique_file=fdopen(file,
"wb");
2854 if ((file == -1) || (unique_file == (FILE *) NULL))
2856 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2857 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2859 return(MagickFalse);
2861 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2862 if (byte_image == (
Image *) NULL)
2864 (void) fclose(unique_file);
2865 (void) RelinquishUniqueFileResource(filename);
2866 return(MagickFalse);
2868 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2870 DestroyBlob(byte_image);
2871 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2872 write_info=CloneImageInfo(image_info);
2873 SetImageInfoFile(write_info,unique_file);
2874 status=WriteImage(write_info,byte_image,exception);
2875 write_info=DestroyImageInfo(write_info);
2876 byte_image=DestroyImage(byte_image);
2877 (void) fclose(unique_file);
2878 if (status == MagickFalse)
2880 (void) RelinquishUniqueFileResource(filename);
2881 return(MagickFalse);
2886 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2889 (void) RelinquishUniqueFileResource(filename);
2890 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2891 image_info->filename);
2892 return(MagickFalse);
2894 quantum=(size_t) MagickMaxBufferExtent;
2895 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2896 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2897 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2898 if (buffer == (
unsigned char *) NULL)
2900 (void) RelinquishUniqueFileResource(filename);
2901 file=close_utf8(file);
2902 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2907 ssize_t count = read(file,buffer,quantum);
2914 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2917 file=close_utf8(file);
2919 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2920 (void) RelinquishUniqueFileResource(filename);
2921 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2947MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2949 assert(image != (
const Image *) NULL);
2950 assert(image->signature == MagickCoreSignature);
2951 if (IsEventLogging() != MagickFalse)
2952 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2953 return(image->blob->exempt);
2978MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2981 *magick_restrict blob_info;
2983 assert(image != (
const Image *) NULL);
2984 assert(image->signature == MagickCoreSignature);
2985 if (IsEventLogging() != MagickFalse)
2986 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2987 blob_info=image->blob;
2988 switch (blob_info->type)
2997 if (blob_info->file_info.file == (FILE *) NULL)
2998 return(MagickFalse);
2999 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
3000 return(status == -1 ? MagickFalse : MagickTrue);
3004#if defined(MAGICKCORE_ZLIB_DELEGATE)
3008 if (blob_info->file_info.gzfile == (gzFile) NULL)
3009 return(MagickFalse);
3010 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
3011 return(offset < 0 ? MagickFalse : MagickTrue);
3016 case UndefinedStream:
3020 case StandardStream:
3024 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3025 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3032 return(MagickFalse);
3057MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3059 assert(image != (
const Image *) NULL);
3060 assert(image->signature == MagickCoreSignature);
3061 if (IsEventLogging() != MagickFalse)
3062 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3063 return(image->blob->temporary);
3095MagickExport
void *MapBlob(
int file,
const MapMode mode,
3096 const MagickOffsetType offset,
const size_t length)
3098#if defined(MAGICKCORE_HAVE_MMAP)
3111#if defined(MAP_ANONYMOUS)
3112 flags|=MAP_ANONYMOUS;
3121 protection=PROT_READ;
3127 protection=PROT_WRITE;
3133 protection=PROT_READ | PROT_WRITE;
3138#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3139 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3141 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3142 if (map == MAP_FAILED)
3143 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3145 if (map == MAP_FAILED)
3182MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3191 assert(buffer != (
unsigned char *) NULL);
3198 *buffer++=(
unsigned char) c;
3202 *buffer++=(
unsigned char) c;
3232MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3240 assert(p != (
unsigned char *) NULL);
3247 *p++=(
unsigned char) c;
3283static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3295 size=MagickMinBufferExtent;
3296 option=GetImageOption(image_info,
"stream:buffer-size");
3297 if (option != (
const char *) NULL)
3298 size=StringToUnsignedLong(option);
3299 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3300 _IONBF : _IOFBF,size);
3301 return(status == 0 ? MagickTrue : MagickFalse);
3304#if defined(MAGICKCORE_ZLIB_DELEGATE)
3305static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3307#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3308 return(gzopen(path,mode));
3316 path_wide=create_wchar_path(path);
3317 if (path_wide == (
wchar_t *) NULL)
3318 return((gzFile) NULL);
3319 file=gzopen_w(path_wide,mode);
3320 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3326MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3330 *magick_restrict blob_info;
3333 extension[MagickPathExtent],
3334 filename[MagickPathExtent];
3348 assert(image_info != (
ImageInfo *) NULL);
3349 assert(image_info->signature == MagickCoreSignature);
3350 assert(image != (
Image *) NULL);
3351 assert(image->signature == MagickCoreSignature);
3352 if (IsEventLogging() != MagickFalse)
3353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3354 image_info->filename);
3355 blob_info=image->blob;
3356 if (image_info->blob != (
void *) NULL)
3358 if (image_info->stream != (StreamHandler) NULL)
3359 blob_info->stream=(StreamHandler) image_info->stream;
3360 AttachBlob(blob_info,image_info->blob,image_info->length);
3364 (*image->filename ==
'\0'))
3366 blob_info->type=CustomStream;
3367 blob_info->custom_stream=image_info->custom_stream;
3370 (void) DetachBlob(blob_info);
3371 blob_info->mode=mode;
3380 case ReadBinaryBlobMode:
3382 flags=O_RDONLY | O_BINARY;
3388 flags=O_WRONLY | O_CREAT | O_TRUNC;
3392 case WriteBinaryBlobMode:
3394 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3398 case AppendBlobMode:
3400 flags=O_WRONLY | O_CREAT | O_APPEND;
3404 case AppendBinaryBlobMode:
3406 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3418 blob_info->synchronize=image_info->synchronize;
3419 if (image_info->stream != (StreamHandler) NULL)
3421 blob_info->stream=image_info->stream;
3424 blob_info->type=FifoStream;
3432 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3433 rights=ReadPolicyRights;
3435 rights=WritePolicyRights;
3436 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3439 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3440 "NotAuthorized",
"`%s'",filename);
3441 return(MagickFalse);
3443 if ((LocaleCompare(filename,
"-") == 0) ||
3444 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3446 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3447#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3448 if (strchr(type,
'b') != (
char *) NULL)
3449 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3451 blob_info->type=StandardStream;
3452 blob_info->exempt=MagickTrue;
3453 return(SetStreamBuffering(image_info,blob_info));
3455 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3456 (IsGeometry(filename+3) != MagickFalse))
3463 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3464 if (blob_info->file_info.file == (FILE *) NULL)
3466 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3467 return(MagickFalse);
3469#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3470 if (strchr(type,
'b') != (
char *) NULL)
3471 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3473 blob_info->type=FileStream;
3474 blob_info->exempt=MagickTrue;
3475 return(SetStreamBuffering(image_info,blob_info));
3477#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3478 if (*filename ==
'|')
3481 fileMode[MagickPathExtent],
3489 (void) signal(SIGPIPE,SIG_IGN);
3493 sanitize_command=SanitizeString(filename+1);
3494 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3495 sanitize_command=DestroyString(sanitize_command);
3496 if (blob_info->file_info.file == (FILE *) NULL)
3498 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3499 return(MagickFalse);
3501 blob_info->type=PipeStream;
3502 blob_info->exempt=MagickTrue;
3503 return(SetStreamBuffering(image_info,blob_info));
3506 status=GetPathAttributes(filename,&blob_info->properties);
3507#if defined(S_ISFIFO)
3508 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3510 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3511 if (blob_info->file_info.file == (FILE *) NULL)
3513 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3514 return(MagickFalse);
3516 blob_info->type=FileStream;
3517 blob_info->exempt=MagickTrue;
3518 return(SetStreamBuffering(image_info,blob_info));
3521 GetPathComponent(image->filename,ExtensionPath,extension);
3524 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3525 if ((image_info->adjoin == MagickFalse) ||
3526 (strchr(filename,
'%') != (
char *) NULL))
3531 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3532 image->scene,filename,exception);
3533 if ((LocaleCompare(filename,image->filename) == 0) &&
3534 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
3535 (GetNextImageInList(image) != (
Image *) NULL)))
3538 path[MagickPathExtent];
3540 GetPathComponent(image->filename,RootPath,path);
3541 if (*extension ==
'\0')
3542 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3543 path,(
double) image->scene);
3545 (
void) FormatLocaleString(filename,MagickPathExtent,
3546 "%s-%.20g.%s",path,(
double) image->scene,extension);
3548 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3550 if (IsPathAuthorized(rights,filename) == MagickFalse)
3553 (void) ThrowMagickException(exception,GetMagickModule(),
3554 PolicyError,
"NotAuthorized",
"`%s'",filename);
3555 return(MagickFalse);
3558 if (image_info->file != (FILE *) NULL)
3560 blob_info->file_info.file=image_info->file;
3561 blob_info->type=FileStream;
3562 blob_info->exempt=MagickTrue;
3570 blob_info->file_info.file=(FILE *) NULL;
3571#if defined(O_NOFOLLOW)
3572 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
3573 if (status == MagickFalse)
3576 file=open_utf8(filename,flags,0);
3578 blob_info->file_info.file=fdopen(file,type);
3579 if (blob_info->file_info.file != (FILE *) NULL)
3587 blob_info->type=FileStream;
3588 (void) SetStreamBuffering(image_info,blob_info);
3589 (void) memset(magick,0,
sizeof(magick));
3590 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3591 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3592#if defined(MAGICKCORE_POSIX_SUPPORT)
3593 (void) fflush(blob_info->file_info.file);
3595 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3596 " read %.20g magic header bytes",(double) count);
3597#if defined(MAGICKCORE_ZLIB_DELEGATE)
3598 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3599 ((
int) magick[2] == 0x08))
3602 gzfile = gzopen_utf8(filename,
"rb");
3604 if (gzfile != (gzFile) NULL)
3606 if (blob_info->file_info.file != (FILE *) NULL)
3607 (void) fclose(blob_info->file_info.file);
3608 blob_info->file_info.file=(FILE *) NULL;
3609 blob_info->file_info.gzfile=gzfile;
3610 blob_info->type=ZipStream;
3614#if defined(MAGICKCORE_BZLIB_DELEGATE)
3615 if (strncmp((
char *) magick,
"BZh",3) == 0)
3618 *bzfile = BZ2_bzopen(filename,
"r");
3620 if (bzfile != (BZFILE *) NULL)
3622 if (blob_info->file_info.file != (FILE *) NULL)
3623 (void) fclose(blob_info->file_info.file);
3624 blob_info->file_info.file=(FILE *) NULL;
3625 blob_info->file_info.bzfile=bzfile;
3626 blob_info->type=BZipStream;
3630 if (blob_info->type == FileStream)
3641 sans_exception=AcquireExceptionInfo();
3642 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3643 sans_exception=DestroyExceptionInfo(sans_exception);
3644 length=(size_t) blob_info->properties.st_size;
3645 if ((magick_info != (
const MagickInfo *) NULL) &&
3646 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3647 (length > MagickMaxBufferExtent) &&
3648 (AcquireMagickResource(MapResource,length) != MagickFalse))
3653 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3655 if (blob == (
void *) NULL)
3656 RelinquishMagickResource(MapResource,length);
3662 if (image_info->file != (FILE *) NULL)
3663 blob_info->exempt=MagickFalse;
3666 (void) fclose(blob_info->file_info.file);
3667 blob_info->file_info.file=(FILE *) NULL;
3669 AttachBlob(blob_info,blob,length);
3670 blob_info->mapped=MagickTrue;
3677#if defined(MAGICKCORE_ZLIB_DELEGATE)
3678 if ((LocaleCompare(extension,
"gz") == 0) ||
3679 (LocaleCompare(extension,
"wmz") == 0) ||
3680 (LocaleCompare(extension,
"svgz") == 0))
3682 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3683 if (blob_info->file_info.gzfile != (gzFile) NULL)
3684 blob_info->type=ZipStream;
3688#if defined(MAGICKCORE_BZLIB_DELEGATE)
3689 if (LocaleCompare(extension,
"bz2") == 0)
3691 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3692 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3693 blob_info->type=BZipStream;
3701 blob_info->file_info.file=(FILE *) NULL;
3702#if defined(O_NOFOLLOW)
3703 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
3705 if (status == MagickFalse)
3708 file=open_utf8(filename,flags,0666);
3710 blob_info->file_info.file=fdopen(file,type);
3711 if (blob_info->file_info.file != (FILE *) NULL)
3713 blob_info->type=FileStream;
3714 (void) SetStreamBuffering(image_info,blob_info);
3717 blob_info->status=0;
3718 blob_info->error_number=0;
3719 if (blob_info->type != UndefinedStream)
3720 blob_info->size=GetBlobSize(image);
3723 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3724 return(MagickFalse);
3763#if defined(__cplusplus) || defined(c_plusplus)
3767static size_t PingStream(
const Image *magick_unused(image),
3768 const void *magick_unused(pixels),
const size_t columns)
3770 magick_unreferenced(image);
3771 magick_unreferenced(pixels);
3775#if defined(__cplusplus) || defined(c_plusplus)
3779MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3795 assert(image_info != (
ImageInfo *) NULL);
3796 assert(image_info->signature == MagickCoreSignature);
3798 if (IsEventLogging() != MagickFalse)
3799 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3800 image_info->filename);
3801 if ((blob == (
const void *) NULL) || (length == 0))
3803 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3804 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3805 return((
Image *) NULL);
3807 ping_info=CloneImageInfo(image_info);
3808 ping_info->blob=(
void *) blob;
3809 ping_info->length=length;
3810 ping_info->ping=MagickTrue;
3811 if (*ping_info->magick ==
'\0')
3812 (void) SetImageInfo(ping_info,0,exception);
3813 magick_info=GetMagickInfo(ping_info->magick,exception);
3814 if (magick_info == (
const MagickInfo *) NULL)
3816 (void) ThrowMagickException(exception,GetMagickModule(),
3817 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3819 ping_info=DestroyImageInfo(ping_info);
3820 return((
Image *) NULL);
3822 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3825 filename[MagickPathExtent];
3830 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3831 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3832 ping_info->magick,filename);
3833 image=ReadStream(ping_info,&PingStream,exception);
3834 if (image != (
Image *) NULL)
3835 (void) DetachBlob(image->blob);
3836 ping_info=DestroyImageInfo(ping_info);
3842 ping_info->blob=(
void *) NULL;
3843 ping_info->length=0;
3844 *ping_info->filename=
'\0';
3845 status=BlobToFile(ping_info->filename,blob,length,exception);
3846 if (status == MagickFalse)
3848 (void) RelinquishUniqueFileResource(ping_info->filename);
3849 ping_info=DestroyImageInfo(ping_info);
3850 return((
Image *) NULL);
3852 clone_info=CloneImageInfo(ping_info);
3853 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3854 ping_info->magick,ping_info->filename);
3855 image=ReadStream(clone_info,&PingStream,exception);
3856 if (image != (
Image *) NULL)
3864 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3866 (void) CopyMagickString(images->filename,image_info->filename,
3868 (void) CopyMagickString(images->magick_filename,image_info->filename,
3870 (void) CopyMagickString(images->magick,magick_info->name,
3872 images=GetNextImageInList(images);
3875 clone_info=DestroyImageInfo(clone_info);
3876 (void) RelinquishUniqueFileResource(ping_info->filename);
3877 ping_info=DestroyImageInfo(ping_info);
3912MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
void *data)
3915 *magick_restrict blob_info;
3926 assert(image != (
Image *) NULL);
3927 assert(image->signature == MagickCoreSignature);
3928 assert(image->blob != (
BlobInfo *) NULL);
3929 assert(image->blob->type != UndefinedStream);
3932 assert(data != (
void *) NULL);
3933 blob_info=image->blob;
3935 q=(
unsigned char *) data;
3936 switch (blob_info->type)
3938 case UndefinedStream:
3940 case StandardStream:
3948 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
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;
3971 c=getc(blob_info->file_info.file);
3974 *q++=(
unsigned char) c;
3980 c=getc(blob_info->file_info.file);
3983 *q++=(
unsigned char) c;
3990 if ((count != (ssize_t) length) &&
3991 (ferror(blob_info->file_info.file) != 0))
3992 ThrowBlobException(blob_info);
3997#if defined(MAGICKCORE_ZLIB_DELEGATE)
4008 for (i=0; i < length; i+=(size_t) count)
4010 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
4011 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
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;
4042 c=gzgetc(blob_info->file_info.gzfile);
4045 *q++=(
unsigned char) c;
4051 c=gzgetc(blob_info->file_info.gzfile);
4054 *q++=(
unsigned char) c;
4061 (void) gzerror(blob_info->file_info.gzfile,&status);
4062 if ((count != (ssize_t) length) && (status != Z_OK))
4063 ThrowBlobException(blob_info);
4064 if (blob_info->eof == MagickFalse)
4065 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4072#if defined(MAGICKCORE_BZLIB_DELEGATE)
4079 if (blob_info->status != 0)
4081 for (i=0; i < length; i+=(size_t) count)
4083 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4084 MagickMin(length-i,MagickMaxBufferExtent));
4094 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4095 if ((count != (ssize_t) length) && (status != BZ_OK))
4096 ThrowBlobException(blob_info);
4097 if ((count != (ssize_t) length) && (status == BZ_OK))
4098 blob_info->eof=MagickTrue;
4109 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4111 blob_info->eof=MagickTrue;
4114 p=blob_info->data+blob_info->offset;
4115 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4116 blob_info->length-blob_info->offset);
4117 blob_info->offset+=count;
4118 if (count != (ssize_t) length)
4119 blob_info->eof=MagickTrue;
4120 (void) memcpy(q,p,(
size_t) count);
4125 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4126 count=blob_info->custom_stream->reader(q,length,
4127 blob_info->custom_stream->data);
4156MagickExport
int ReadBlobByte(
Image *image)
4159 *magick_restrict blob_info;
4164 assert(image != (
Image *) NULL);
4165 assert(image->signature == MagickCoreSignature);
4166 assert(image->blob != (
BlobInfo *) NULL);
4167 assert(image->blob->type != UndefinedStream);
4168 blob_info=image->blob;
4169 switch (blob_info->type)
4171 case StandardStream:
4175 c=getc(blob_info->file_info.file);
4178 if (ferror(blob_info->file_info.file) != 0)
4179 ThrowBlobException(blob_info);
4186 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4188 blob_info->eof=MagickTrue;
4191 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4192 blob_info->offset++;
4203 count=ReadBlob(image,1,buffer);
4236MagickExport
double ReadBlobDouble(
Image *image)
4247 quantum.double_value=0.0;
4248 quantum.unsigned_value=ReadBlobLongLong(image);
4249 return(quantum.double_value);
4275MagickExport
float ReadBlobFloat(
Image *image)
4286 quantum.float_value=0.0;
4287 quantum.unsigned_value=ReadBlobLong(image);
4288 return(quantum.float_value);
4314MagickExport
unsigned int ReadBlobLong(
Image *image)
4328 assert(image != (
Image *) NULL);
4329 assert(image->signature == MagickCoreSignature);
4331 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4334 if (image->endian == LSBEndian)
4336 value=(
unsigned int) (*p++);
4337 value|=(
unsigned int) (*p++) << 8;
4338 value|=(
unsigned int) (*p++) << 16;
4339 value|=(
unsigned int) (*p++) << 24;
4342 value=(
unsigned int) (*p++) << 24;
4343 value|=(
unsigned int) (*p++) << 16;
4344 value|=(
unsigned int) (*p++) << 8;
4345 value|=(
unsigned int) (*p++);
4372MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
4386 assert(image != (
Image *) NULL);
4387 assert(image->signature == MagickCoreSignature);
4389 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4391 return(MagickULLConstant(0));
4392 if (image->endian == LSBEndian)
4394 value=(MagickSizeType) (*p++);
4395 value|=(MagickSizeType) (*p++) << 8;
4396 value|=(MagickSizeType) (*p++) << 16;
4397 value|=(MagickSizeType) (*p++) << 24;
4398 value|=(MagickSizeType) (*p++) << 32;
4399 value|=(MagickSizeType) (*p++) << 40;
4400 value|=(MagickSizeType) (*p++) << 48;
4401 value|=(MagickSizeType) (*p++) << 56;
4404 value=(MagickSizeType) (*p++) << 56;
4405 value|=(MagickSizeType) (*p++) << 48;
4406 value|=(MagickSizeType) (*p++) << 40;
4407 value|=(MagickSizeType) (*p++) << 32;
4408 value|=(MagickSizeType) (*p++) << 24;
4409 value|=(MagickSizeType) (*p++) << 16;
4410 value|=(MagickSizeType) (*p++) << 8;
4411 value|=(MagickSizeType) (*p++);
4438MagickExport
unsigned short ReadBlobShort(
Image *image)
4452 assert(image != (
Image *) NULL);
4453 assert(image->signature == MagickCoreSignature);
4455 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4457 return((
unsigned short) 0U);
4458 if (image->endian == LSBEndian)
4460 value=(
unsigned short) (*p++);
4461 value|=(
unsigned short) (*p++) << 8;
4464 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4465 value|=(
unsigned short) (*p++);
4492MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
4506 assert(image != (
Image *) NULL);
4507 assert(image->signature == MagickCoreSignature);
4509 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4512 value=(
unsigned int) (*p++);
4513 value|=(
unsigned int) (*p++) << 8;
4514 value|=(
unsigned int) (*p++) << 16;
4515 value|=(
unsigned int) (*p++) << 24;
4542MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
4553 quantum.unsigned_value=ReadBlobLSBLong(image);
4554 return(quantum.signed_value);
4580MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
4594 assert(image != (
Image *) NULL);
4595 assert(image->signature == MagickCoreSignature);
4597 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4599 return((
unsigned short) 0U);
4600 value=(
unsigned short) (*p++);
4601 value|=(
unsigned short) (*p++) << 8;
4628MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
4639 quantum.unsigned_value=ReadBlobLSBShort(image);
4640 return(quantum.signed_value);
4666MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
4680 assert(image != (
Image *) NULL);
4681 assert(image->signature == MagickCoreSignature);
4683 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4686 value=(
unsigned int) (*p++) << 24;
4687 value|=(
unsigned int) (*p++) << 16;
4688 value|=(
unsigned int) (*p++) << 8;
4689 value|=(
unsigned int) (*p++);
4716MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
4730 assert(image != (
Image *) NULL);
4731 assert(image->signature == MagickCoreSignature);
4733 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4735 return(MagickULLConstant(0));
4736 value=(MagickSizeType) (*p++) << 56;
4737 value|=(MagickSizeType) (*p++) << 48;
4738 value|=(MagickSizeType) (*p++) << 40;
4739 value|=(MagickSizeType) (*p++) << 32;
4740 value|=(MagickSizeType) (*p++) << 24;
4741 value|=(MagickSizeType) (*p++) << 16;
4742 value|=(MagickSizeType) (*p++) << 8;
4743 value|=(MagickSizeType) (*p++);
4770MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
4784 assert(image != (
Image *) NULL);
4785 assert(image->signature == MagickCoreSignature);
4787 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4789 return((
unsigned short) 0U);
4790 value=(
unsigned short) ((*p++) << 8);
4791 value|=(
unsigned short) (*p++);
4792 return((
unsigned short) (value & 0xffff));
4818MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4829 quantum.unsigned_value=ReadBlobMSBLong(image);
4830 return(quantum.signed_value);
4856MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4867 quantum.unsigned_value=ReadBlobMSBShort(image);
4868 return(quantum.signed_value);
4894MagickExport
signed int ReadBlobSignedLong(
Image *image)
4905 quantum.unsigned_value=ReadBlobLong(image);
4906 return(quantum.signed_value);
4932MagickExport
signed short ReadBlobSignedShort(
Image *image)
4943 quantum.unsigned_value=ReadBlobShort(image);
4944 return(quantum.signed_value);
4982MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4983 const size_t length,
void *magick_restrict data,ssize_t *count)
4986 *magick_restrict blob_info;
4988 assert(image != (
Image *) NULL);
4989 assert(image->signature == MagickCoreSignature);
4990 assert(image->blob != (
BlobInfo *) NULL);
4991 assert(image->blob->type != UndefinedStream);
4992 assert(count != (ssize_t *) NULL);
4993 blob_info=image->blob;
4994 if (blob_info->type != BlobStream)
4996 assert(data != NULL);
4997 *count=ReadBlob(image,length,(
unsigned char *) data);
5000 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5003 blob_info->eof=MagickTrue;
5006 data=blob_info->data+blob_info->offset;
5007 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
5008 blob_info->length-blob_info->offset);
5009 blob_info->offset+=(*count);
5010 if (*count != (ssize_t) length)
5011 blob_info->eof=MagickTrue;
5040MagickExport
char *ReadBlobString(
Image *image,
char *
string)
5043 *magick_restrict blob_info;
5051 assert(image != (
Image *) NULL);
5052 assert(image->signature == MagickCoreSignature);
5053 assert(image->blob != (
BlobInfo *) NULL);
5054 assert(image->blob->type != UndefinedStream);
5055 if (IsEventLogging() != MagickFalse)
5056 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5058 blob_info=image->blob;
5059 switch (blob_info->type)
5061 case UndefinedStream:
5063 case StandardStream:
5066 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
5067 if (p == (
char *) NULL)
5069 if (ferror(blob_info->file_info.file) != 0)
5070 ThrowBlobException(blob_info);
5071 return((
char *) NULL);
5078#if defined(MAGICKCORE_ZLIB_DELEGATE)
5079 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5080 if (p == (
char *) NULL)
5083 (void) gzerror(blob_info->file_info.gzfile,&status);
5085 ThrowBlobException(blob_info);
5086 return((
char *) NULL);
5096 c=ReadBlobByte(image);
5099 blob_info->eof=MagickTrue;
5105 }
while (i < (MaxTextExtent-2));
5113 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5116 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5118 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5119 return((
char *) NULL);
5149 assert(blob->signature == MagickCoreSignature);
5150 if (IsEventLogging() != MagickFalse)
5151 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5152 LockSemaphoreInfo(blob->semaphore);
5153 blob->reference_count++;
5154 UnlockSemaphoreInfo(blob->semaphore);
5191MagickExport MagickOffsetType SeekBlob(
Image *image,
5192 const MagickOffsetType offset,
const int whence)
5195 *magick_restrict blob_info;
5197 assert(image != (
Image *) NULL);
5198 assert(image->signature == MagickCoreSignature);
5199 assert(image->blob != (
BlobInfo *) NULL);
5200 assert(image->blob->type != UndefinedStream);
5201 if (IsEventLogging() != MagickFalse)
5202 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5203 blob_info=image->blob;
5204 switch (blob_info->type)
5206 case UndefinedStream:
5208 case StandardStream:
5213 if ((offset < 0) && (whence == SEEK_SET))
5215 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5217 blob_info->offset=TellBlob(image);
5222#if defined(MAGICKCORE_ZLIB_DELEGATE)
5223 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
5226 blob_info->offset=TellBlob(image);
5242 blob_info->offset=offset;
5247 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5248 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5253 if ((blob_info->offset+offset) < 0)
5255 blob_info->offset+=offset;
5260 if (((MagickOffsetType) blob_info->length+offset) < 0)
5262 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5266 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5268 blob_info->eof=MagickFalse;
5275 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5277 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5278 blob_info->custom_stream->data);
5282 return(blob_info->offset);
5310MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
5312 assert(image != (
const Image *) NULL);
5313 assert(image->signature == MagickCoreSignature);
5314 if (IsEventLogging() != MagickFalse)
5315 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5316 image->blob->exempt=exempt;
5345MagickExport MagickBooleanType SetBlobExtent(
Image *image,
5346 const MagickSizeType extent)
5349 *magick_restrict blob_info;
5351 assert(image != (
Image *) NULL);
5352 assert(image->signature == MagickCoreSignature);
5353 assert(image->blob != (
BlobInfo *) NULL);
5354 assert(image->blob->type != UndefinedStream);
5355 if (IsEventLogging() != MagickFalse)
5356 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5357 blob_info=image->blob;
5358 switch (blob_info->type)
5360 case UndefinedStream:
5362 case StandardStream:
5363 return(MagickFalse);
5372 if (extent != (MagickSizeType) ((off_t) extent))
5373 return(MagickFalse);
5374 offset=SeekBlob(image,0,SEEK_END);
5376 return(MagickFalse);
5377 if ((MagickSizeType) offset >= extent)
5379 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5382 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5383 blob_info->file_info.file);
5384#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5385 if (blob_info->synchronize != MagickFalse)
5390 file=fileno(blob_info->file_info.file);
5391 if ((file == -1) || (offset < 0))
5392 return(MagickFalse);
5393 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5396 offset=SeekBlob(image,offset,SEEK_SET);
5398 return(MagickFalse);
5403 return(MagickFalse);
5405 return(MagickFalse);
5407 return(MagickFalse);
5410 if (extent != (MagickSizeType) ((
size_t) extent))
5411 return(MagickFalse);
5412 if (blob_info->mapped != MagickFalse)
5420 (void) UnmapBlob(blob_info->data,blob_info->length);
5421 RelinquishMagickResource(MapResource,blob_info->length);
5422 if (extent != (MagickSizeType) ((off_t) extent))
5423 return(MagickFalse);
5424 offset=SeekBlob(image,0,SEEK_END);
5426 return(MagickFalse);
5427 if ((MagickSizeType) offset >= extent)
5429 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5430 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5431 blob_info->file_info.file);
5432#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5433 if (blob_info->synchronize != MagickFalse)
5438 file=fileno(blob_info->file_info.file);
5439 if ((file == -1) || (offset < 0))
5440 return(MagickFalse);
5441 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5445 offset=SeekBlob(image,offset,SEEK_SET);
5447 return(MagickFalse);
5448 (void) AcquireMagickResource(MapResource,extent);
5449 blob_info->data=(
unsigned char*) MapBlob(fileno(
5450 blob_info->file_info.file),WriteMode,0,(size_t) extent);
5451 blob_info->extent=(size_t) extent;
5452 blob_info->length=(size_t) extent;
5453 (void) SyncBlob(image);
5456 blob_info->extent=(size_t) extent;
5457 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5458 blob_info->extent+1,
sizeof(*blob_info->data));
5459 (void) SyncBlob(image);
5460 if (blob_info->data == (
unsigned char *) NULL)
5462 (void) DetachBlob(blob_info);
5463 return(MagickFalse);
5501 assert(custom_stream->signature == MagickCoreSignature);
5502 custom_stream->data=data;
5531 CustomStreamHandler reader)
5534 assert(custom_stream->signature == MagickCoreSignature);
5535 custom_stream->reader=reader;
5564 CustomStreamSeeker seeker)
5567 assert(custom_stream->signature == MagickCoreSignature);
5568 custom_stream->seeker=seeker;
5597 CustomStreamTeller teller)
5600 assert(custom_stream->signature == MagickCoreSignature);
5601 custom_stream->teller=teller;
5630 CustomStreamHandler writer)
5633 assert(custom_stream->signature == MagickCoreSignature);
5634 custom_stream->writer=writer;
5661static int SyncBlob(
const Image *image)
5664 *magick_restrict blob_info;
5669 assert(image != (
Image *) NULL);
5670 assert(image->signature == MagickCoreSignature);
5671 assert(image->blob != (
BlobInfo *) NULL);
5672 if (IsEventLogging() != MagickFalse)
5673 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5674 if (EOFBlob(image) != 0)
5676 blob_info=image->blob;
5678 switch (blob_info->type)
5680 case UndefinedStream:
5681 case StandardStream:
5686 status=fflush(blob_info->file_info.file);
5691#if defined(MAGICKCORE_ZLIB_DELEGATE)
5692 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5698#if defined(MAGICKCORE_BZLIB_DELEGATE)
5699 status=BZ2_bzflush(blob_info->file_info.bzfile);
5744MagickPrivate
void SyncBlobStream(
ImageInfo *image_info,
Image *image,
5747 assert(owner != (
Image *) NULL);
5748 assert(image != (
Image *) NULL);
5749 assert(image->signature == MagickCoreSignature);
5750 if (image->blob == (
BlobInfo *) NULL)
5752 if (image->blob->data == (
unsigned char *) NULL)
5756 image_info->blob=(
void *) image->blob->data;
5757 image_info->length=image->blob->length;
5759 if ((image == owner) || (owner->blob == (
BlobInfo *) NULL))
5761 if (owner->blob == (
BlobInfo *) NULL)
5767 owner->blob->data=image->blob->data;
5768 owner->blob->extent=image->blob->extent;
5769 owner->blob->length=image->blob->length;
5770 owner->blob->offset=image->blob->offset;
5771 owner->blob->quantum=image->blob->quantum;
5772 owner->blob->type=BlobStream;
5773 owner->blob->exempt=MagickTrue;
5774 (void) DetachBlob(image->blob);
5799MagickExport MagickOffsetType TellBlob(
const Image *image)
5802 *magick_restrict blob_info;
5807 assert(image != (
Image *) NULL);
5808 assert(image->signature == MagickCoreSignature);
5809 assert(image->blob != (
BlobInfo *) NULL);
5810 assert(image->blob->type != UndefinedStream);
5811 if (IsEventLogging() != MagickFalse)
5812 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5813 blob_info=image->blob;
5815 switch (blob_info->type)
5817 case UndefinedStream:
5818 case StandardStream:
5822 offset=ftell(blob_info->file_info.file);
5829#if defined(MAGICKCORE_ZLIB_DELEGATE)
5830 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5840 offset=blob_info->offset;
5845 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5846 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5878MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5880#if defined(MAGICKCORE_HAVE_MMAP)
5884 status=munmap(map,length);
5885 return(status == -1 ? MagickFalse : MagickTrue);
5889 return(MagickFalse);
5921MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
5925 *magick_restrict blob_info;
5939 assert(image != (
Image *) NULL);
5940 assert(image->signature == MagickCoreSignature);
5941 assert(image->blob != (
BlobInfo *) NULL);
5942 assert(image->blob->type != UndefinedStream);
5945 assert(data != (
const void *) NULL);
5946 blob_info=image->blob;
5948 p=(
const unsigned char *) data;
5949 q=(
unsigned char *) data;
5950 switch (blob_info->type)
5952 case UndefinedStream:
5954 case StandardStream:
5962 count=(ssize_t) fwrite((
const char *) data,1,length,
5963 blob_info->file_info.file);
5968 c=putc((
int) *p++,blob_info->file_info.file);
5976 c=putc((
int) *p++,blob_info->file_info.file);
5984 c=putc((
int) *p++,blob_info->file_info.file);
5992 c=putc((
int) *p++,blob_info->file_info.file);
6001 if ((count != (ssize_t) length) &&
6002 (ferror(blob_info->file_info.file) != 0))
6003 ThrowBlobException(blob_info);
6008#if defined(MAGICKCORE_ZLIB_DELEGATE)
6019 for (i=0; i < length; i+=(size_t) count)
6021 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
6022 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
6035 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6043 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6051 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6059 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
6069 (void) gzerror(blob_info->file_info.gzfile,&status);
6070 if ((count != (ssize_t) length) && (status != Z_OK))
6071 ThrowBlobException(blob_info);
6077#if defined(MAGICKCORE_BZLIB_DELEGATE)
6084 for (i=0; i < length; i+=(size_t) count)
6086 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6087 (
int) MagickMin(length-i,MagickMaxBufferExtent));
6097 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6098 if ((count != (ssize_t) length) && (status != BZ_OK))
6099 ThrowBlobException(blob_info);
6105 count=(ssize_t) blob_info->stream(image,data,length);
6113 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6114 if (extent >= blob_info->extent)
6116 if (blob_info->mapped != MagickFalse)
6118 blob_info->extent=extent+blob_info->quantum+length;
6119 blob_info->quantum<<=1;
6120 blob_info->data=(
unsigned char *) ResizeQuantumMemory(
6121 blob_info->data,blob_info->extent+1,
sizeof(*blob_info->data));
6122 (void) SyncBlob(image);
6123 if (blob_info->data == (
unsigned char *) NULL)
6125 (void) DetachBlob(blob_info);
6129 q=blob_info->data+blob_info->offset;
6130 (void) memcpy(q,p,length);
6131 blob_info->offset+=(MagickOffsetType) length;
6132 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6133 blob_info->length=(
size_t) blob_info->offset;
6134 count=(ssize_t) length;
6139 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6140 count=blob_info->custom_stream->writer((
unsigned char *) data,
6141 length,blob_info->custom_stream->data);
6173MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
6176 *magick_restrict blob_info;
6181 assert(image != (
Image *) NULL);
6182 assert(image->signature == MagickCoreSignature);
6183 assert(image->blob != (
BlobInfo *) NULL);
6184 assert(image->blob->type != UndefinedStream);
6185 blob_info=image->blob;
6187 switch (blob_info->type)
6189 case StandardStream:
6196 c=putc((
int) value,blob_info->file_info.file);
6199 if (ferror(blob_info->file_info.file) != 0)
6200 ThrowBlobException(blob_info);
6208 count=WriteBlobStream(image,1,&value);
6240MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
6251 quantum.unsigned_value=0U;
6252 quantum.float_value=value;
6253 return(WriteBlobLong(image,quantum.unsigned_value));
6281MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
6286 assert(image != (
Image *) NULL);
6287 assert(image->signature == MagickCoreSignature);
6288 if (image->endian == LSBEndian)
6290 buffer[0]=(
unsigned char) value;
6291 buffer[1]=(
unsigned char) (value >> 8);
6292 buffer[2]=(
unsigned char) (value >> 16);
6293 buffer[3]=(
unsigned char) (value >> 24);
6294 return(WriteBlobStream(image,4,buffer));
6296 buffer[0]=(
unsigned char) (value >> 24);
6297 buffer[1]=(
unsigned char) (value >> 16);
6298 buffer[2]=(
unsigned char) (value >> 8);
6299 buffer[3]=(
unsigned char) value;
6300 return(WriteBlobStream(image,4,buffer));
6328MagickExport ssize_t WriteBlobLongLong(
Image *image,
const MagickSizeType value)
6333 assert(image != (
Image *) NULL);
6334 assert(image->signature == MagickCoreSignature);
6335 if (image->endian == LSBEndian)
6337 buffer[0]=(
unsigned char) value;
6338 buffer[1]=(
unsigned char) (value >> 8);
6339 buffer[2]=(
unsigned char) (value >> 16);
6340 buffer[3]=(
unsigned char) (value >> 24);
6341 buffer[4]=(
unsigned char) (value >> 32);
6342 buffer[5]=(
unsigned char) (value >> 40);
6343 buffer[6]=(
unsigned char) (value >> 48);
6344 buffer[7]=(
unsigned char) (value >> 56);
6345 return(WriteBlobStream(image,8,buffer));
6347 buffer[0]=(
unsigned char) (value >> 56);
6348 buffer[1]=(
unsigned char) (value >> 48);
6349 buffer[2]=(
unsigned char) (value >> 40);
6350 buffer[3]=(
unsigned char) (value >> 32);
6351 buffer[4]=(
unsigned char) (value >> 24);
6352 buffer[5]=(
unsigned char) (value >> 16);
6353 buffer[6]=(
unsigned char) (value >> 8);
6354 buffer[7]=(
unsigned char) value;
6355 return(WriteBlobStream(image,8,buffer));
6383MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
6388 assert(image != (
Image *) NULL);
6389 assert(image->signature == MagickCoreSignature);
6390 if (image->endian == LSBEndian)
6392 buffer[0]=(
unsigned char) value;
6393 buffer[1]=(
unsigned char) (value >> 8);
6394 return(WriteBlobStream(image,2,buffer));
6396 buffer[0]=(
unsigned char) (value >> 8);
6397 buffer[1]=(
unsigned char) value;
6398 return(WriteBlobStream(image,2,buffer));
6426MagickExport ssize_t WriteBlobSignedLong(
Image *image,
const signed int value)
6440 assert(image != (
Image *) NULL);
6441 assert(image->signature == MagickCoreSignature);
6442 quantum.signed_value=value;
6443 if (image->endian == LSBEndian)
6445 buffer[0]=(
unsigned char) quantum.unsigned_value;
6446 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6447 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6448 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6449 return(WriteBlobStream(image,4,buffer));
6451 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6452 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6453 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6454 buffer[3]=(
unsigned char) quantum.unsigned_value;
6455 return(WriteBlobStream(image,4,buffer));
6483MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
6488 assert(image != (
Image *) NULL);
6489 assert(image->signature == MagickCoreSignature);
6490 buffer[0]=(
unsigned char) value;
6491 buffer[1]=(
unsigned char) (value >> 8);
6492 buffer[2]=(
unsigned char) (value >> 16);
6493 buffer[3]=(
unsigned char) (value >> 24);
6494 return(WriteBlobStream(image,4,buffer));
6522MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
6527 assert(image != (
Image *) NULL);
6528 assert(image->signature == MagickCoreSignature);
6529 buffer[0]=(
unsigned char) value;
6530 buffer[1]=(
unsigned char) (value >> 8);
6531 return(WriteBlobStream(image,2,buffer));
6559MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
6573 assert(image != (
Image *) NULL);
6574 assert(image->signature == MagickCoreSignature);
6575 quantum.signed_value=value;
6576 buffer[0]=(
unsigned char) quantum.unsigned_value;
6577 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6578 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6579 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6580 return(WriteBlobStream(image,4,buffer));
6608MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
6609 const signed short value)
6623 assert(image != (
Image *) NULL);
6624 assert(image->signature == MagickCoreSignature);
6625 quantum.signed_value=value;
6626 buffer[0]=(
unsigned char) quantum.unsigned_value;
6627 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6628 return(WriteBlobStream(image,2,buffer));
6656MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
6661 assert(image != (
Image *) NULL);
6662 assert(image->signature == MagickCoreSignature);
6663 buffer[0]=(
unsigned char) (value >> 24);
6664 buffer[1]=(
unsigned char) (value >> 16);
6665 buffer[2]=(
unsigned char) (value >> 8);
6666 buffer[3]=(
unsigned char) value;
6667 return(WriteBlobStream(image,4,buffer));
6695MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
6696 const signed short value)
6710 assert(image != (
Image *) NULL);
6711 assert(image->signature == MagickCoreSignature);
6712 quantum.signed_value=value;
6713 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6714 buffer[1]=(
unsigned char) quantum.unsigned_value;
6715 return(WriteBlobStream(image,2,buffer));
6743MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
6748 assert(image != (
Image *) NULL);
6749 assert(image->signature == MagickCoreSignature);
6750 buffer[0]=(
unsigned char) (value >> 8);
6751 buffer[1]=(
unsigned char) value;
6752 return(WriteBlobStream(image,2,buffer));
6780MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
6782 assert(image != (
Image *) NULL);
6783 assert(image->signature == MagickCoreSignature);
6784 assert(
string != (
const char *) NULL);
6785 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));