47#include "MagickCore/studio.h"
48#include "MagickCore/blob.h"
49#include "MagickCore/blob-private.h"
50#include "MagickCore/cache.h"
51#include "MagickCore/client.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/delegate.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/locale_.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/memory-private.h"
64#include "MagickCore/nt-base-private.h"
65#include "MagickCore/option.h"
66#include "MagickCore/policy.h"
67#include "MagickCore/policy-private.h"
68#include "MagickCore/resource_.h"
69#include "MagickCore/semaphore.h"
70#include "MagickCore/string_.h"
71#include "MagickCore/string-private.h"
72#include "MagickCore/timer-private.h"
73#include "MagickCore/token.h"
74#include "MagickCore/utility.h"
75#include "MagickCore/utility-private.h"
76#if defined(MAGICKCORE_ZLIB_DELEGATE)
79#if defined(MAGICKCORE_BZLIB_DELEGATE)
86#define MagickMaxBlobExtent (8*8192)
87#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
88# define MAP_ANONYMOUS MAP_ANON
90#if !defined(MAP_FAILED)
91#define MAP_FAILED ((void *) -1)
95#define _O_BINARY O_BINARY
97#if defined(MAGICKCORE_WINDOWS_SUPPORT)
102# define MAGICKCORE_HAVE_MMAP 1
103# define mmap(address,length,protection,access,file,offset) \
104 NTMapMemory(address,length,protection,access,file,offset)
107# define munmap(address,length) NTUnmapMemory(address,length)
110# define pclose _pclose
125#if defined(MAGICKCORE_ZLIB_DELEGATE)
130#if defined(MAGICKCORE_BZLIB_DELEGATE)
222 SyncBlob(
const Image *);
246MagickExport CustomStreamInfo *AcquireCustomStreamInfo(
247 ExceptionInfo *magick_unused(exception))
252 magick_unreferenced(exception);
253 custom_stream=(CustomStreamInfo *) AcquireCriticalMemory(
254 sizeof(*custom_stream));
255 (void) memset(custom_stream,0,
sizeof(*custom_stream));
256 custom_stream->signature=MagickCoreSignature;
257 return(custom_stream);
287MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
290 assert(blob_info != (BlobInfo *) NULL);
291 if (IsEventLogging() != MagickFalse)
292 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
293 blob_info->length=length;
294 blob_info->extent=length;
295 blob_info->quantum=(size_t) MagickMaxBlobExtent;
297 blob_info->type=BlobStream;
298 blob_info->file_info.file=(FILE *) NULL;
299 blob_info->data=(
unsigned char *) blob;
300 blob_info->mapped=MagickFalse;
328MagickExport
void AttachCustomStream(BlobInfo *blob_info,
329 CustomStreamInfo *custom_stream)
331 assert(blob_info != (BlobInfo *) NULL);
332 assert(custom_stream != (CustomStreamInfo *) NULL);
333 assert(custom_stream->signature == MagickCoreSignature);
334 if (IsEventLogging() != MagickFalse)
335 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
336 blob_info->type=CustomStream;
337 blob_info->custom_stream=custom_stream;
371MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
372 const size_t length,ExceptionInfo *exception)
383 assert(filename != (
const char *) NULL);
384 assert(blob != (
const void *) NULL);
385 if (IsEventLogging() != MagickFalse)
386 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
387 if (*filename ==
'\0')
388 file=AcquireUniqueFileResource(filename);
390 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
393 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
396 for (i=0; i < length; i+=(size_t) count)
398 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
399 MagickMaxBufferExtent));
407 file=close_utf8(file);
408 if ((file == -1) || (i < length))
410 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
447MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
448 const size_t length,ExceptionInfo *exception)
463 assert(image_info != (ImageInfo *) NULL);
464 assert(image_info->signature == MagickCoreSignature);
465 assert(exception != (ExceptionInfo *) NULL);
466 if (IsEventLogging() != MagickFalse)
467 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
468 image_info->filename);
469 if ((blob == (
const void *) NULL) || (length == 0))
471 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
472 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
473 return((Image *) NULL);
475 blob_info=CloneImageInfo(image_info);
476 blob_info->blob=(
void *) blob;
477 blob_info->length=length;
478 if (*blob_info->magick ==
'\0')
479 (void) SetImageInfo(blob_info,0,exception);
480 magick_info=GetMagickInfo(blob_info->magick,exception);
481 if (magick_info == (
const MagickInfo *) NULL)
483 (void) ThrowMagickException(exception,GetMagickModule(),
484 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
486 blob_info=DestroyImageInfo(blob_info);
487 return((Image *) NULL);
489 if (GetMagickBlobSupport(magick_info) != MagickFalse)
492 filename[MagickPathExtent];
497 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
498 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
499 blob_info->magick,filename);
500 image=ReadImage(blob_info,exception);
501 if (image != (Image *) NULL)
502 (void) DetachBlob(image->blob);
503 blob_info=DestroyImageInfo(blob_info);
509 blob_info->blob=(
void *) NULL;
511 *blob_info->filename=
'\0';
512 status=BlobToFile(blob_info->filename,blob,length,exception);
513 if (status == MagickFalse)
515 (void) RelinquishUniqueFileResource(blob_info->filename);
516 blob_info=DestroyImageInfo(blob_info);
517 return((Image *) NULL);
519 clone_info=CloneImageInfo(blob_info);
520 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
521 blob_info->magick,blob_info->filename);
522 image=ReadImage(clone_info,exception);
523 if (image != (Image *) NULL)
531 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
533 (void) CopyMagickString(images->filename,image_info->filename,
535 (void) CopyMagickString(images->magick_filename,image_info->filename,
537 (void) CopyMagickString(images->magick,magick_info->name,
539 images=GetNextImageInList(images);
542 clone_info=DestroyImageInfo(clone_info);
543 (void) RelinquishUniqueFileResource(blob_info->filename);
544 blob_info=DestroyImageInfo(blob_info);
571MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
579 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
580 GetBlobInfo(clone_info);
581 if (blob_info == (BlobInfo *) NULL)
583 semaphore=clone_info->semaphore;
584 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
585 if (blob_info->mapped != MagickFalse)
586 (void) AcquireMagickResource(MapResource,blob_info->length);
587 clone_info->semaphore=semaphore;
588 LockSemaphoreInfo(clone_info->semaphore);
589 clone_info->reference_count=1;
590 UnlockSemaphoreInfo(clone_info->semaphore);
617static inline void ThrowBlobException(BlobInfo *blob_info)
619 if ((blob_info->status == 0) && (errno != 0))
620 blob_info->error_number=errno;
621 blob_info->status=(-1);
624MagickExport MagickBooleanType CloseBlob(Image *image)
627 *magick_restrict blob_info;
635 assert(image != (Image *) NULL);
636 assert(image->signature == MagickCoreSignature);
637 if (IsEventLogging() != MagickFalse)
638 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
639 blob_info=image->blob;
640 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
642 (void) SyncBlob(image);
643 status=blob_info->status;
644 switch (blob_info->type)
646 case UndefinedStream:
652 if (blob_info->synchronize != MagickFalse)
654 status=fflush(blob_info->file_info.file);
656 ThrowBlobException(blob_info);
657 status=fsync(fileno(blob_info->file_info.file));
659 ThrowBlobException(blob_info);
661 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
662 ThrowBlobException(blob_info);
667#if defined(MAGICKCORE_ZLIB_DELEGATE)
669 (void) gzerror(blob_info->file_info.gzfile,&status);
671 ThrowBlobException(blob_info);
677#if defined(MAGICKCORE_BZLIB_DELEGATE)
679 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
681 ThrowBlobException(blob_info);
689 if (blob_info->file_info.file != (FILE *) NULL)
691 if (blob_info->synchronize != MagickFalse)
693 status=fflush(blob_info->file_info.file);
695 ThrowBlobException(blob_info);
696 status=fsync(fileno(blob_info->file_info.file));
698 ThrowBlobException(blob_info);
700 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
701 ThrowBlobException(blob_info);
708 blob_info->size=GetBlobSize(image);
709 image->extent=blob_info->size;
710 blob_info->eof=MagickFalse;
712 blob_info->mode=UndefinedBlobMode;
713 if (blob_info->exempt != MagickFalse)
715 blob_info->type=UndefinedStream;
716 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
718 switch (blob_info->type)
720 case UndefinedStream:
725 if (blob_info->file_info.file != (FILE *) NULL)
727 status=fclose(blob_info->file_info.file);
729 ThrowBlobException(blob_info);
735#if defined(MAGICKCORE_HAVE_PCLOSE)
736 status=pclose(blob_info->file_info.file);
738 ThrowBlobException(blob_info);
744#if defined(MAGICKCORE_ZLIB_DELEGATE)
745 status=gzclose(blob_info->file_info.gzfile);
747 ThrowBlobException(blob_info);
753#if defined(MAGICKCORE_BZLIB_DELEGATE)
754 BZ2_bzclose(blob_info->file_info.bzfile);
762 if (blob_info->file_info.file != (FILE *) NULL)
764 status=fclose(blob_info->file_info.file);
766 ThrowBlobException(blob_info);
773 (void) DetachBlob(blob_info);
774 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
803MagickExport Image *CustomStreamToImage(
const ImageInfo *image_info,
804 ExceptionInfo *exception)
815 assert(image_info != (ImageInfo *) NULL);
816 assert(image_info->signature == MagickCoreSignature);
817 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
818 assert(image_info->custom_stream->signature == MagickCoreSignature);
819 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
820 assert(exception != (ExceptionInfo *) NULL);
821 if (IsEventLogging() != MagickFalse)
822 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
823 image_info->filename);
824 blob_info=CloneImageInfo(image_info);
825 if (*blob_info->magick ==
'\0')
826 (void) SetImageInfo(blob_info,0,exception);
827 magick_info=GetMagickInfo(blob_info->magick,exception);
828 if (magick_info == (
const MagickInfo *) NULL)
830 (void) ThrowMagickException(exception,GetMagickModule(),
831 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
833 blob_info=DestroyImageInfo(blob_info);
834 return((Image *) NULL);
836 image=(Image *) NULL;
837 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
838 (*blob_info->filename !=
'\0'))
841 filename[MagickPathExtent];
847 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
848 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
849 blob_info->magick,filename);
850 image=ReadImage(blob_info,exception);
855 unique[MagickPathExtent];
869 blob_info->custom_stream=(CustomStreamInfo *) NULL;
870 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
872 if (blob == (
unsigned char *) NULL)
874 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
875 image_info->filename);
876 blob_info=DestroyImageInfo(blob_info);
877 return((Image *) NULL);
879 file=AcquireUniqueFileResource(unique);
882 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
883 image_info->filename);
884 blob=(
unsigned char *) RelinquishMagickMemory(blob);
885 blob_info=DestroyImageInfo(blob_info);
886 return((Image *) NULL);
888 clone_info=CloneImageInfo(blob_info);
889 blob_info->file=fdopen(file,
"wb+");
890 if (blob_info->file != (FILE *) NULL)
895 count=(ssize_t) MagickMaxBufferExtent;
896 while (count == (ssize_t) MagickMaxBufferExtent)
898 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
899 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)
983 image->blob=(BlobInfo *) NULL;
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);
1021MagickExport CustomStreamInfo *DestroyCustomStreamInfo(
1022 CustomStreamInfo *custom_stream)
1024 assert(custom_stream != (CustomStreamInfo *) NULL);
1025 assert(custom_stream->signature == MagickCoreSignature);
1026 if (IsEventLogging() != MagickFalse)
1027 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1028 custom_stream->signature=(~MagickCoreSignature);
1029 custom_stream=(CustomStreamInfo *) RelinquishMagickMemory(custom_stream);
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;
1085 blob_info->custom_stream=(CustomStreamInfo *) 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 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
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,
1426 size_t *length,ExceptionInfo *exception)
1452 assert(filename != (
const char *) NULL);
1453 assert(exception != (ExceptionInfo *) NULL);
1454 assert(exception->signature == MagickCoreSignature);
1455 if (IsEventLogging() != MagickFalse)
1456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1458 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1459 ThrowPolicyException(filename,NULL);
1461 if (LocaleCompare(filename,
"-") != 0)
1464 flags = O_RDONLY | O_BINARY;
1466 status=GetPathAttributes(filename,&attributes);
1467 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1469 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1472 file=open_utf8(filename,flags,0);
1476 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1479 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1481 file=close_utf8(file)-1;
1482 ThrowPolicyException(filename,NULL);
1484 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1486 if ((file == fileno(stdin)) || (offset < 0) ||
1487 (offset != (MagickOffsetType) ((ssize_t) offset)))
1498 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1499 quantum=(size_t) MagickMaxBufferExtent;
1500 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1501 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1502 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1503 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1505 count=read(file,blob+i,quantum);
1512 if (~i < ((
size_t) count+quantum+1))
1514 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1517 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1518 quantum+1,
sizeof(*blob));
1519 if ((i+(
size_t) count) >= extent)
1522 if (LocaleCompare(filename,
"-") != 0)
1523 file=close_utf8(file);
1524 if (blob == (
unsigned char *) NULL)
1526 (void) ThrowMagickException(exception,GetMagickModule(),
1527 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1532 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1533 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1536 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1540 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1541 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1542 blob=(
unsigned char *) NULL;
1543 if (~(*length) >= (MagickPathExtent-1))
1544 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1546 if (blob == (
unsigned char *) NULL)
1548 file=close_utf8(file);
1549 (void) ThrowMagickException(exception,GetMagickModule(),
1550 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1553 map=MapBlob(file,ReadMode,0,*length);
1554 if (map != (
unsigned char *) NULL)
1556 (void) memcpy(blob,map,*length);
1557 (void) UnmapBlob(map,*length);
1561 (void) lseek(file,0,SEEK_SET);
1562 for (i=0; i < *length; i+=(size_t) count)
1564 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1565 MagickMaxBufferExtent));
1575 file=close_utf8(file)-1;
1576 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1577 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1582 if (LocaleCompare(filename,
"-") != 0)
1583 file=close_utf8(file);
1586 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1587 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1617static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1618 const void *magick_restrict data)
1621 *magick_restrict blob_info;
1629 assert(image->blob != (BlobInfo *) NULL);
1630 assert(image->blob->type != UndefinedStream);
1631 assert(data != NULL);
1632 blob_info=image->blob;
1633 if (blob_info->type != BlobStream)
1634 return(WriteBlob(image,length,(
const unsigned char *) data));
1635 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1640 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1641 if (extent >= blob_info->extent)
1643 extent+=blob_info->quantum+length;
1644 blob_info->quantum<<=1;
1645 if (SetBlobExtent(image,extent) == MagickFalse)
1648 q=blob_info->data+blob_info->offset;
1649 (void) memcpy(q,data,length);
1650 blob_info->offset+=(MagickOffsetType) length;
1651 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1652 blob_info->length=(size_t) blob_info->offset;
1653 return((ssize_t) length);
1656MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename,
1657 ExceptionInfo *exception)
1675 assert(image != (
const Image *) NULL);
1676 assert(image->signature == MagickCoreSignature);
1677 assert(filename != (
const char *) NULL);
1678 if (IsEventLogging() != MagickFalse)
1679 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1680 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1681 ThrowPolicyException(filename,MagickFalse);
1683 if (LocaleCompare(filename,
"-") != 0)
1686 flags = O_RDONLY | O_BINARY;
1688 file=open_utf8(filename,flags,0);
1692 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1693 return(MagickFalse);
1695 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1696 ThrowPolicyException(filename,MagickFalse);
1697 quantum=(size_t) MagickMaxBufferExtent;
1698 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1699 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1700 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1701 if (blob == (
unsigned char *) NULL)
1703 file=close_utf8(file);
1704 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1706 return(MagickFalse);
1710 count=read(file,blob,quantum);
1717 length=(size_t) count;
1718 count=WriteBlobStream(image,length,blob);
1719 if (count != (ssize_t) length)
1721 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1725 file=close_utf8(file);
1727 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1728 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1755MagickExport MagickBooleanType GetBlobError(
const Image *image)
1757 assert(image != (
const Image *) NULL);
1758 assert(image->signature == MagickCoreSignature);
1759 if (IsEventLogging() != MagickFalse)
1760 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1761 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1762 errno=image->blob->error_number;
1763 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1788MagickExport FILE *GetBlobFileHandle(
const Image *image)
1790 assert(image != (
const Image *) NULL);
1791 assert(image->signature == MagickCoreSignature);
1792 return(image->blob->file_info.file);
1817MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1819 assert(blob_info != (BlobInfo *) NULL);
1820 (void) memset(blob_info,0,
sizeof(*blob_info));
1821 blob_info->type=UndefinedStream;
1822 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1823 blob_info->properties.st_mtime=GetMagickTime();
1824 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1825 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1826 blob_info->reference_count=1;
1827 blob_info->semaphore=AcquireSemaphoreInfo();
1828 blob_info->signature=MagickCoreSignature;
1853MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1855 assert(image != (Image *) NULL);
1856 assert(image->signature == MagickCoreSignature);
1857 if (IsEventLogging() != MagickFalse)
1858 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1859 return(&image->blob->properties);
1885MagickExport MagickSizeType GetBlobSize(
const Image *image)
1888 *magick_restrict blob_info;
1893 assert(image != (Image *) NULL);
1894 assert(image->signature == MagickCoreSignature);
1895 assert(image->blob != (BlobInfo *) NULL);
1896 if (IsEventLogging() != MagickFalse)
1897 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1898 blob_info=image->blob;
1900 switch (blob_info->type)
1902 case UndefinedStream:
1903 case StandardStream:
1905 extent=blob_info->size;
1913 extent=(MagickSizeType) blob_info->properties.st_size;
1915 extent=blob_info->size;
1916 file_descriptor=fileno(blob_info->file_info.file);
1917 if (file_descriptor == -1)
1919 if (fstat(file_descriptor,&blob_info->properties) == 0)
1920 extent=(MagickSizeType) blob_info->properties.st_size;
1925 extent=blob_info->size;
1934 status=GetPathAttributes(image->filename,&blob_info->properties);
1935 if (status != MagickFalse)
1936 extent=(MagickSizeType) blob_info->properties.st_size;
1943 extent=(MagickSizeType) blob_info->length;
1948 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1949 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1954 offset=blob_info->custom_stream->teller(
1955 blob_info->custom_stream->data);
1956 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1957 blob_info->custom_stream->data);
1958 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1959 blob_info->custom_stream->data);
1989MagickExport
void *GetBlobStreamData(
const Image *image)
1991 assert(image != (
const Image *) NULL);
1992 assert(image->signature == MagickCoreSignature);
1993 return(image->blob->data);
2018MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
2020 assert(image != (
const Image *) NULL);
2021 assert(image->signature == MagickCoreSignature);
2022 if (IsEventLogging() != MagickFalse)
2023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2024 return(image->blob->stream);
2060MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2061 Image *image,
size_t *length,ExceptionInfo *exception)
2075 assert(image_info != (
const ImageInfo *) NULL);
2076 assert(image_info->signature == MagickCoreSignature);
2077 assert(image != (Image *) NULL);
2078 assert(image->signature == MagickCoreSignature);
2079 assert(exception != (ExceptionInfo *) NULL);
2080 assert(exception->signature == MagickCoreSignature);
2081 if (IsEventLogging() != MagickFalse)
2082 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2083 image_info->filename);
2085 blob=(
unsigned char *) NULL;
2086 blob_info=CloneImageInfo(image_info);
2087 blob_info->adjoin=MagickFalse;
2088 (void) SetImageInfo(blob_info,1,exception);
2089 if (*blob_info->magick !=
'\0')
2090 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2091 magick_info=GetMagickInfo(image->magick,exception);
2092 if (magick_info == (
const MagickInfo *) NULL)
2094 (void) ThrowMagickException(exception,GetMagickModule(),
2095 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2097 blob_info=DestroyImageInfo(blob_info);
2100 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2101 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2106 blob_info->length=0;
2107 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2108 sizeof(
unsigned char));
2109 if (blob_info->blob == NULL)
2110 (void) ThrowMagickException(exception,GetMagickModule(),
2111 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2114 (void) CloseBlob(image);
2115 image->blob->exempt=MagickTrue;
2116 image->blob->extent=0;
2117 *image->filename=
'\0';
2118 status=WriteImage(blob_info,image,exception);
2119 *length=image->blob->length;
2120 blob=DetachBlob(image->blob);
2121 if (blob != (
void *) NULL)
2123 if (status == MagickFalse)
2124 blob=RelinquishMagickMemory(blob);
2126 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2128 else if ((status == MagickFalse) && (image->blob->extent == 0))
2129 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2135 unique[MagickPathExtent];
2143 file=AcquireUniqueFileResource(unique);
2146 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2147 image_info->filename);
2151 blob_info->file=fdopen(file,
"wb");
2152 if (blob_info->file != (FILE *) NULL)
2154 (void) FormatLocaleString(image->filename,MagickPathExtent,
2155 "%s:%s",image->magick,unique);
2156 status=WriteImage(blob_info,image,exception);
2157 (void) fclose(blob_info->file);
2158 if (status != MagickFalse)
2159 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2161 (void) RelinquishUniqueFileResource(unique);
2164 blob_info=DestroyImageInfo(blob_info);
2196MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,Image *image,
2197 ExceptionInfo *exception)
2209 assert(image_info != (
const ImageInfo *) NULL);
2210 assert(image_info->signature == MagickCoreSignature);
2211 assert(image != (Image *) NULL);
2212 assert(image->signature == MagickCoreSignature);
2213 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2214 assert(image_info->custom_stream->signature == MagickCoreSignature);
2215 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2216 assert(exception != (ExceptionInfo *) NULL);
2217 if (IsEventLogging() != MagickFalse)
2218 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2219 image_info->filename);
2220 clone_info=CloneImageInfo(image_info);
2221 clone_info->adjoin=MagickFalse;
2222 (void) SetImageInfo(clone_info,1,exception);
2223 if (*clone_info->magick !=
'\0')
2224 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2225 magick_info=GetMagickInfo(image->magick,exception);
2226 if (magick_info == (
const MagickInfo *) NULL)
2228 (void) ThrowMagickException(exception,GetMagickModule(),
2229 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2231 clone_info=DestroyImageInfo(clone_info);
2234 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2235 blob_support=GetMagickBlobSupport(magick_info);
2236 if ((blob_support != MagickFalse) &&
2237 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2239 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2240 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2241 blob_support=MagickFalse;
2243 if (blob_support != MagickFalse)
2248 (void) CloseBlob(image);
2249 *image->filename=
'\0';
2250 (void) WriteImage(clone_info,image,exception);
2255 unique[MagickPathExtent];
2266 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2267 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2269 if (blob == (
unsigned char *) NULL)
2271 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2272 image_info->filename);
2273 clone_info=DestroyImageInfo(clone_info);
2276 file=AcquireUniqueFileResource(unique);
2279 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2280 image_info->filename);
2281 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2282 clone_info=DestroyImageInfo(clone_info);
2285 clone_info->file=fdopen(file,
"wb+");
2286 if (clone_info->file != (FILE *) NULL)
2291 (void) FormatLocaleString(image->filename,MagickPathExtent,
"%s:%s",
2292 image->magick,unique);
2293 status=WriteImage(clone_info,image,exception);
2294 if (status != MagickFalse)
2296 (void) fseek(clone_info->file,0,SEEK_SET);
2297 count=(ssize_t) MagickMaxBufferExtent;
2298 while (count == (ssize_t) MagickMaxBufferExtent)
2300 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2302 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2303 image_info->custom_stream->data);
2306 (void) fclose(clone_info->file);
2308 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2309 (void) RelinquishUniqueFileResource(unique);
2311 clone_info=DestroyImageInfo(clone_info);
2342MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
2343 ExceptionInfo *exception)
2367 assert(image != (Image *) NULL);
2368 assert(image->signature == MagickCoreSignature);
2369 assert(image->blob != (BlobInfo *) NULL);
2370 assert(image->blob->type != UndefinedStream);
2371 assert(filename != (
const char *) NULL);
2372 if (IsEventLogging() != MagickFalse)
2373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2374 if (*filename ==
'\0')
2375 file=AcquireUniqueFileResource(filename);
2377 if (LocaleCompare(filename,
"-") == 0)
2378 file=fileno(stdout);
2380 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2383 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2384 return(MagickFalse);
2386 quantum=(size_t) MagickMaxBufferExtent;
2387 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2388 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2389 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2390 if (buffer == (
unsigned char *) NULL)
2392 file=close_utf8(file)-1;
2393 (void) ThrowMagickException(exception,GetMagickModule(),
2394 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2395 return(MagickFalse);
2398 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2399 for (i=0; count > 0; )
2401 length=(size_t) count;
2402 for (i=0; i < length; i+=(size_t) count)
2404 count=write(file,p+i,(
size_t) (length-i));
2414 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2416 if (LocaleCompare(filename,
"-") != 0)
2417 file=close_utf8(file);
2418 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2419 if ((file == -1) || (i < length))
2422 file=close_utf8(file);
2423 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2424 return(MagickFalse);
2464MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,Image *images,
2465 size_t *length,ExceptionInfo *exception)
2479 assert(image_info != (
const ImageInfo *) NULL);
2480 assert(image_info->signature == MagickCoreSignature);
2481 assert(images != (Image *) NULL);
2482 assert(images->signature == MagickCoreSignature);
2483 assert(exception != (ExceptionInfo *) NULL);
2484 if (IsEventLogging() != MagickFalse)
2485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2486 image_info->filename);
2488 blob=(
unsigned char *) NULL;
2489 blob_info=CloneImageInfo(image_info);
2490 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2492 if (*blob_info->magick !=
'\0')
2493 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2494 magick_info=GetMagickInfo(images->magick,exception);
2495 if (magick_info == (
const MagickInfo *) NULL)
2497 (void) ThrowMagickException(exception,GetMagickModule(),
2498 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2500 blob_info=DestroyImageInfo(blob_info);
2503 if (GetMagickAdjoin(magick_info) == MagickFalse)
2505 blob_info=DestroyImageInfo(blob_info);
2506 return(ImageToBlob(image_info,images,length,exception));
2508 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2509 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2514 blob_info->length=0;
2515 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2516 sizeof(
unsigned char));
2517 if (blob_info->blob == (
void *) NULL)
2518 (void) ThrowMagickException(exception,GetMagickModule(),
2519 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2522 (void) CloseBlob(images);
2523 images->blob->exempt=MagickTrue;
2524 images->blob->extent=0;
2525 *images->filename=
'\0';
2526 status=WriteImages(blob_info,images,images->filename,exception);
2527 *length=images->blob->length;
2528 blob=DetachBlob(images->blob);
2529 if (blob != (
void *) NULL)
2531 if (status == MagickFalse)
2532 blob=RelinquishMagickMemory(blob);
2534 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2536 else if ((status == MagickFalse) && (images->blob->extent == 0))
2537 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2543 filename[MagickPathExtent],
2544 unique[MagickPathExtent];
2552 file=AcquireUniqueFileResource(unique);
2555 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2556 image_info->filename);
2560 blob_info->file=fdopen(file,
"wb");
2561 if (blob_info->file != (FILE *) NULL)
2563 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2564 images->magick,unique);
2565 status=WriteImages(blob_info,images,filename,exception);
2566 (void) fclose(blob_info->file);
2567 if (status != MagickFalse)
2568 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2570 (void) RelinquishUniqueFileResource(unique);
2573 blob_info=DestroyImageInfo(blob_info);
2605MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2606 Image *images,ExceptionInfo *exception)
2618 assert(image_info != (
const ImageInfo *) NULL);
2619 assert(image_info->signature == MagickCoreSignature);
2620 assert(images != (Image *) NULL);
2621 assert(images->signature == MagickCoreSignature);
2622 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2623 assert(image_info->custom_stream->signature == MagickCoreSignature);
2624 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2625 assert(exception != (ExceptionInfo *) NULL);
2626 if (IsEventLogging() != MagickFalse)
2627 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2628 image_info->filename);
2629 clone_info=CloneImageInfo(image_info);
2630 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2632 if (*clone_info->magick !=
'\0')
2633 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2634 magick_info=GetMagickInfo(images->magick,exception);
2635 if (magick_info == (
const MagickInfo *) NULL)
2637 (void) ThrowMagickException(exception,GetMagickModule(),
2638 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2640 clone_info=DestroyImageInfo(clone_info);
2643 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2644 blob_support=GetMagickBlobSupport(magick_info);
2645 if ((blob_support != MagickFalse) &&
2646 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2648 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2649 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2650 blob_support=MagickFalse;
2652 if (blob_support != MagickFalse)
2657 (void) CloseBlob(images);
2658 *images->filename=
'\0';
2659 (void) WriteImages(clone_info,images,images->filename,exception);
2664 filename[MagickPathExtent],
2665 unique[MagickPathExtent];
2676 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2677 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2679 if (blob == (
unsigned char *) NULL)
2681 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2682 image_info->filename);
2683 clone_info=DestroyImageInfo(clone_info);
2686 file=AcquireUniqueFileResource(unique);
2689 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2690 image_info->filename);
2691 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2692 clone_info=DestroyImageInfo(clone_info);
2695 clone_info->file=fdopen(file,
"wb+");
2696 if (clone_info->file != (FILE *) NULL)
2701 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2702 images->magick,unique);
2703 status=WriteImages(clone_info,images,filename,exception);
2704 if (status != MagickFalse)
2706 (void) fseek(clone_info->file,0,SEEK_SET);
2707 count=(ssize_t) MagickMaxBufferExtent;
2708 while (count == (ssize_t) MagickMaxBufferExtent)
2710 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2712 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2713 image_info->custom_stream->data);
2716 (void) fclose(clone_info->file);
2718 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2719 (void) RelinquishUniqueFileResource(unique);
2721 clone_info=DestroyImageInfo(clone_info);
2757MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2758 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2761 filename[MagickPathExtent];
2790 assert(image_info != (ImageInfo *) NULL);
2791 assert(image_info->signature == MagickCoreSignature);
2792 assert(image != (Image *) NULL);
2793 assert(image->signature == MagickCoreSignature);
2794 assert(inject_image != (Image *) NULL);
2795 assert(inject_image->signature == MagickCoreSignature);
2796 assert(exception != (ExceptionInfo *) NULL);
2797 if (IsEventLogging() != MagickFalse)
2798 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2799 unique_file=(FILE *) NULL;
2800 file=AcquireUniqueFileResource(filename);
2802 unique_file=fdopen(file,
"wb");
2803 if ((file == -1) || (unique_file == (FILE *) NULL))
2805 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2806 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2808 return(MagickFalse);
2810 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2811 if (byte_image == (Image *) NULL)
2813 (void) fclose(unique_file);
2814 (void) RelinquishUniqueFileResource(filename);
2815 return(MagickFalse);
2817 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2819 DestroyBlob(byte_image);
2820 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2821 write_info=CloneImageInfo(image_info);
2822 SetImageInfoFile(write_info,unique_file);
2823 status=WriteImage(write_info,byte_image,exception);
2824 write_info=DestroyImageInfo(write_info);
2825 byte_image=DestroyImage(byte_image);
2826 (void) fclose(unique_file);
2827 if (status == MagickFalse)
2829 (void) RelinquishUniqueFileResource(filename);
2830 return(MagickFalse);
2835 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2838 (void) RelinquishUniqueFileResource(filename);
2839 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2840 image_info->filename);
2841 return(MagickFalse);
2843 quantum=(size_t) MagickMaxBufferExtent;
2844 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2845 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2846 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2847 if (buffer == (
unsigned char *) NULL)
2849 (void) RelinquishUniqueFileResource(filename);
2850 file=close_utf8(file);
2851 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2856 ssize_t count = read(file,buffer,quantum);
2863 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2866 file=close_utf8(file);
2868 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2869 (void) RelinquishUniqueFileResource(filename);
2870 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2896MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2898 assert(image != (
const Image *) NULL);
2899 assert(image->signature == MagickCoreSignature);
2900 if (IsEventLogging() != MagickFalse)
2901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2902 return(image->blob->exempt);
2927MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2930 *magick_restrict blob_info;
2932 assert(image != (
const Image *) NULL);
2933 assert(image->signature == MagickCoreSignature);
2934 if (IsEventLogging() != MagickFalse)
2935 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2936 blob_info=image->blob;
2937 switch (blob_info->type)
2946 if (blob_info->file_info.file == (FILE *) NULL)
2947 return(MagickFalse);
2948 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2949 return(status == -1 ? MagickFalse : MagickTrue);
2953#if defined(MAGICKCORE_ZLIB_DELEGATE)
2957 if (blob_info->file_info.gzfile == (gzFile) NULL)
2958 return(MagickFalse);
2959 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2960 return(offset < 0 ? MagickFalse : MagickTrue);
2965 case UndefinedStream:
2969 case StandardStream:
2973 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
2974 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
2981 return(MagickFalse);
3006MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
3008 assert(image != (
const Image *) NULL);
3009 assert(image->signature == MagickCoreSignature);
3010 if (IsEventLogging() != MagickFalse)
3011 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3012 return(image->blob->temporary);
3044MagickExport
void *MapBlob(
int file,
const MapMode mode,
3045 const MagickOffsetType offset,
const size_t length)
3047#if defined(MAGICKCORE_HAVE_MMAP)
3060#if defined(MAP_ANONYMOUS)
3061 flags|=MAP_ANONYMOUS;
3070 protection=PROT_READ;
3076 protection=PROT_WRITE;
3082 protection=PROT_READ | PROT_WRITE;
3087#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3088 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3090 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3091 if (map == MAP_FAILED)
3092 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3094 if (map == MAP_FAILED)
3131MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3140 assert(buffer != (
unsigned char *) NULL);
3147 *buffer++=(
unsigned char) c;
3151 *buffer++=(
unsigned char) c;
3181MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3189 assert(p != (
unsigned char *) NULL);
3196 *p++=(
unsigned char) c;
3232static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3233 const BlobInfo *blob_info)
3244 size=MagickMinBufferExtent;
3245 option=GetImageOption(image_info,
"stream:buffer-size");
3246 if (option != (
const char *) NULL)
3247 size=StringToUnsignedLong(option);
3248 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3249 _IONBF : _IOFBF,size);
3250 return(status == 0 ? MagickTrue : MagickFalse);
3253#if defined(MAGICKCORE_ZLIB_DELEGATE)
3254static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3256#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3257 return(gzopen(path,mode));
3265 path_wide=NTCreateWidePath(path);
3266 if (path_wide == (
wchar_t *) NULL)
3267 return((gzFile) NULL);
3268 file=gzopen_w(path_wide,mode);
3269 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3275MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3276 Image *image,
const BlobMode mode,ExceptionInfo *exception)
3279 *magick_restrict blob_info;
3282 extension[MagickPathExtent],
3283 filename[MagickPathExtent];
3297 assert(image_info != (ImageInfo *) NULL);
3298 assert(image_info->signature == MagickCoreSignature);
3299 assert(image != (Image *) NULL);
3300 assert(image->signature == MagickCoreSignature);
3301 if (IsEventLogging() != MagickFalse)
3302 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3303 image_info->filename);
3304 blob_info=image->blob;
3305 if (image_info->blob != (
void *) NULL)
3307 if (image_info->stream != (StreamHandler) NULL)
3308 blob_info->stream=(StreamHandler) image_info->stream;
3309 AttachBlob(blob_info,image_info->blob,image_info->length);
3312 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3313 (*image->filename ==
'\0'))
3315 blob_info->type=CustomStream;
3316 blob_info->custom_stream=image_info->custom_stream;
3317 if (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL)
3318 blob_info->custom_stream->seeker(0,SEEK_SET,
3319 blob_info->custom_stream->data);
3322 (void) DetachBlob(blob_info);
3323 blob_info->mode=mode;
3332 case ReadBinaryBlobMode:
3334 flags=O_RDONLY | O_BINARY;
3340 flags=O_WRONLY | O_CREAT | O_TRUNC;
3344 case WriteBinaryBlobMode:
3346 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
3350 case AppendBlobMode:
3352 flags=O_WRONLY | O_CREAT | O_APPEND;
3356 case AppendBinaryBlobMode:
3358 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
3370 blob_info->synchronize=image_info->synchronize;
3371 if (image_info->stream != (StreamHandler) NULL)
3373 blob_info->stream=image_info->stream;
3376 blob_info->type=FifoStream;
3384 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3385 rights=ReadPolicyRights;
3387 rights=WritePolicyRights;
3388 if (IsPathAuthorized(rights,filename) == MagickFalse)
3389 ThrowPolicyException(filename,MagickFalse);
3390 if ((LocaleCompare(filename,
"-") == 0) ||
3391 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3393 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3394#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3395 if (strchr(type,
'b') != (
char *) NULL)
3396 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3398 blob_info->type=StandardStream;
3399 blob_info->exempt=MagickTrue;
3400 return(SetStreamBuffering(image_info,blob_info));
3402 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3403 (IsGeometry(filename+3) != MagickFalse))
3410 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3411 if (blob_info->file_info.file == (FILE *) NULL)
3413 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3414 return(MagickFalse);
3416#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3417 if (strchr(type,
'b') != (
char *) NULL)
3418 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3420 blob_info->type=FileStream;
3421 blob_info->exempt=MagickTrue;
3422 return(SetStreamBuffering(image_info,blob_info));
3424#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3425 if (*filename ==
'|')
3428 fileMode[MagickPathExtent],
3436 (void) signal(SIGPIPE,SIG_IGN);
3440 sanitize_command=SanitizeString(filename+1);
3441 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3442 sanitize_command=DestroyString(sanitize_command);
3443 if (blob_info->file_info.file == (FILE *) NULL)
3445 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3446 return(MagickFalse);
3448 blob_info->type=PipeStream;
3449 blob_info->exempt=MagickTrue;
3450 return(SetStreamBuffering(image_info,blob_info));
3453 status=GetPathAttributes(filename,&blob_info->properties);
3454#if defined(S_ISFIFO)
3455 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3457 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3458 if (blob_info->file_info.file == (FILE *) NULL)
3460 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3461 return(MagickFalse);
3463 blob_info->type=FileStream;
3464 blob_info->exempt=MagickTrue;
3465 return(SetStreamBuffering(image_info,blob_info));
3468 GetPathComponent(image->filename,ExtensionPath,extension);
3471 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3472 if ((image_info->adjoin == MagickFalse) ||
3473 (strchr(filename,
'%') != (
char *) NULL))
3478 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3479 image->scene,filename,exception);
3480 if ((LocaleCompare(filename,image->filename) == 0) &&
3481 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3482 (GetNextImageInList(image) != (Image *) NULL)))
3485 path[MagickPathExtent];
3487 GetPathComponent(image->filename,RootPath,path);
3488 if (*extension ==
'\0')
3489 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3490 path,(
double) image->scene);
3492 (
void) FormatLocaleString(filename,MagickPathExtent,
3493 "%s-%.20g.%s",path,(
double) image->scene,extension);
3495 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3497 if (IsPathAuthorized(rights,filename) == MagickFalse)
3498 ThrowPolicyException(filename,MagickFalse);
3500 if (image_info->file != (FILE *) NULL)
3502 blob_info->file_info.file=image_info->file;
3503 blob_info->type=FileStream;
3504 blob_info->exempt=MagickTrue;
3512 blob_info->file_info.file=(FILE *) NULL;
3513 file=open_utf8(filename,flags,0);
3515 blob_info->file_info.file=fdopen(file,type);
3516 if (blob_info->file_info.file != (FILE *) NULL)
3524 blob_info->type=FileStream;
3525 (void) SetStreamBuffering(image_info,blob_info);
3526 (void) memset(magick,0,
sizeof(magick));
3527 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3528 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3529#if defined(MAGICKCORE_POSIX_SUPPORT)
3530 (void) fflush(blob_info->file_info.file);
3532 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3533 " read %.20g magic header bytes",(
double) count);
3534#if defined(MAGICKCORE_ZLIB_DELEGATE)
3535 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3536 ((
int) magick[2] == 0x08))
3539 gzfile = gzopen_utf8(filename,
"rb");
3541 if (gzfile != (gzFile) NULL)
3543 if (blob_info->file_info.file != (FILE *) NULL)
3544 (void) fclose(blob_info->file_info.file);
3545 blob_info->file_info.file=(FILE *) NULL;
3546 blob_info->file_info.gzfile=gzfile;
3547 blob_info->type=ZipStream;
3551#if defined(MAGICKCORE_BZLIB_DELEGATE)
3552 if (strncmp((
char *) magick,
"BZh",3) == 0)
3555 *bzfile = BZ2_bzopen(filename,
"r");
3557 if (bzfile != (BZFILE *) NULL)
3559 if (blob_info->file_info.file != (FILE *) NULL)
3560 (void) fclose(blob_info->file_info.file);
3561 blob_info->file_info.file=(FILE *) NULL;
3562 blob_info->file_info.bzfile=bzfile;
3563 blob_info->type=BZipStream;
3567 if (blob_info->type == FileStream)
3578 sans_exception=AcquireExceptionInfo();
3579 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3580 sans_exception=DestroyExceptionInfo(sans_exception);
3581 length=(size_t) blob_info->properties.st_size;
3582 if ((magick_info != (
const MagickInfo *) NULL) &&
3583 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3584 (AcquireMagickResource(MapResource,length) != MagickFalse))
3589 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3591 if (blob == (
void *) NULL)
3592 RelinquishMagickResource(MapResource,length);
3598 if (image_info->file != (FILE *) NULL)
3599 blob_info->exempt=MagickFalse;
3602 (void) fclose(blob_info->file_info.file);
3603 blob_info->file_info.file=(FILE *) NULL;
3605 AttachBlob(blob_info,blob,length);
3606 blob_info->mapped=MagickTrue;
3613#if defined(MAGICKCORE_ZLIB_DELEGATE)
3614 if ((LocaleCompare(extension,
"gz") == 0) ||
3615 (LocaleCompare(extension,
"wmz") == 0) ||
3616 (LocaleCompare(extension,
"svgz") == 0))
3618 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3619 if (blob_info->file_info.gzfile != (gzFile) NULL)
3620 blob_info->type=ZipStream;
3624#if defined(MAGICKCORE_BZLIB_DELEGATE)
3625 if (LocaleCompare(extension,
"bz2") == 0)
3627 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3628 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3629 blob_info->type=BZipStream;
3637 blob_info->file_info.file=(FILE *) NULL;
3638 file=open_utf8(filename,flags,P_MODE);
3640 blob_info->file_info.file=fdopen(file,type);
3641 if (blob_info->file_info.file != (FILE *) NULL)
3643 blob_info->type=FileStream;
3644 (void) SetStreamBuffering(image_info,blob_info);
3647 if (IsPathAuthorized(rights,filename) == MagickFalse)
3648 ThrowPolicyException(filename,MagickFalse);
3649 blob_info->status=0;
3650 blob_info->error_number=0;
3651 if (blob_info->type != UndefinedStream)
3652 blob_info->size=GetBlobSize(image);
3655 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3656 return(MagickFalse);
3695#if defined(__cplusplus) || defined(c_plusplus)
3699static size_t PingStream(
const Image *magick_unused(image),
3700 const void *magick_unused(pixels),
const size_t columns)
3702 magick_unreferenced(image);
3703 magick_unreferenced(pixels);
3707#if defined(__cplusplus) || defined(c_plusplus)
3711MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3712 const size_t length,ExceptionInfo *exception)
3727 assert(image_info != (ImageInfo *) NULL);
3728 assert(image_info->signature == MagickCoreSignature);
3729 assert(exception != (ExceptionInfo *) NULL);
3730 if (IsEventLogging() != MagickFalse)
3731 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3732 image_info->filename);
3733 if ((blob == (
const void *) NULL) || (length == 0))
3735 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3736 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3737 return((Image *) NULL);
3739 ping_info=CloneImageInfo(image_info);
3740 ping_info->blob=(
void *) blob;
3741 ping_info->length=length;
3742 ping_info->ping=MagickTrue;
3743 if (*ping_info->magick ==
'\0')
3744 (void) SetImageInfo(ping_info,0,exception);
3745 magick_info=GetMagickInfo(ping_info->magick,exception);
3746 if (magick_info == (
const MagickInfo *) NULL)
3748 (void) ThrowMagickException(exception,GetMagickModule(),
3749 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3751 ping_info=DestroyImageInfo(ping_info);
3752 return((Image *) NULL);
3754 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3757 filename[MagickPathExtent];
3762 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3763 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3764 ping_info->magick,filename);
3765 image=ReadStream(ping_info,&PingStream,exception);
3766 if (image != (Image *) NULL)
3767 (void) DetachBlob(image->blob);
3768 ping_info=DestroyImageInfo(ping_info);
3774 ping_info->blob=(
void *) NULL;
3775 ping_info->length=0;
3776 *ping_info->filename=
'\0';
3777 status=BlobToFile(ping_info->filename,blob,length,exception);
3778 if (status == MagickFalse)
3780 (void) RelinquishUniqueFileResource(ping_info->filename);
3781 ping_info=DestroyImageInfo(ping_info);
3782 return((Image *) NULL);
3784 clone_info=CloneImageInfo(ping_info);
3785 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3786 ping_info->magick,ping_info->filename);
3787 image=ReadStream(clone_info,&PingStream,exception);
3788 if (image != (Image *) NULL)
3796 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3798 (void) CopyMagickString(images->filename,image_info->filename,
3800 (void) CopyMagickString(images->magick_filename,image_info->filename,
3802 (void) CopyMagickString(images->magick,magick_info->name,
3804 images=GetNextImageInList(images);
3807 clone_info=DestroyImageInfo(clone_info);
3808 (void) RelinquishUniqueFileResource(ping_info->filename);
3809 ping_info=DestroyImageInfo(ping_info);
3844MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
void *data)
3847 *magick_restrict blob_info;
3858 assert(image != (Image *) NULL);
3859 assert(image->signature == MagickCoreSignature);
3860 assert(image->blob != (BlobInfo *) NULL);
3861 assert(image->blob->type != UndefinedStream);
3864 assert(data != (
void *) NULL);
3865 blob_info=image->blob;
3867 q=(
unsigned char *) data;
3868 switch (blob_info->type)
3870 case UndefinedStream:
3872 case StandardStream:
3880 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3885 c=getc(blob_info->file_info.file);
3888 *q++=(
unsigned char) c;
3894 c=getc(blob_info->file_info.file);
3897 *q++=(
unsigned char) c;
3903 c=getc(blob_info->file_info.file);
3906 *q++=(
unsigned char) c;
3912 c=getc(blob_info->file_info.file);
3915 *q++=(
unsigned char) c;
3922 if ((count != (ssize_t) length) &&
3923 (ferror(blob_info->file_info.file) != 0))
3924 ThrowBlobException(blob_info);
3929#if defined(MAGICKCORE_ZLIB_DELEGATE)
3940 for (i=0; i < length; i+=(size_t) count)
3942 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3943 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3956 c=gzgetc(blob_info->file_info.gzfile);
3959 *q++=(
unsigned char) c;
3965 c=gzgetc(blob_info->file_info.gzfile);
3968 *q++=(
unsigned char) c;
3974 c=gzgetc(blob_info->file_info.gzfile);
3977 *q++=(
unsigned char) c;
3983 c=gzgetc(blob_info->file_info.gzfile);
3986 *q++=(
unsigned char) c;
3993 (void) gzerror(blob_info->file_info.gzfile,&status);
3994 if ((count != (ssize_t) length) && (status != Z_OK))
3995 ThrowBlobException(blob_info);
3996 if (blob_info->eof == MagickFalse)
3997 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4004#if defined(MAGICKCORE_BZLIB_DELEGATE)
4011 for (i=0; i < length; i+=(size_t) count)
4013 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
4014 MagickMin(length-i,MagickMaxBufferExtent));
4024 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4025 if ((count != (ssize_t) length) && (status != BZ_OK))
4026 ThrowBlobException(blob_info);
4037 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4039 blob_info->eof=MagickTrue;
4042 p=blob_info->data+blob_info->offset;
4043 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4044 blob_info->length-blob_info->offset);
4045 blob_info->offset+=count;
4046 if (count != (ssize_t) length)
4047 blob_info->eof=MagickTrue;
4048 (void) memcpy(q,p,(
size_t) count);
4053 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4054 count=blob_info->custom_stream->reader(q,length,
4055 blob_info->custom_stream->data);
4084MagickExport
int ReadBlobByte(Image *image)
4087 *magick_restrict blob_info;
4092 assert(image != (Image *) NULL);
4093 assert(image->signature == MagickCoreSignature);
4094 assert(image->blob != (BlobInfo *) NULL);
4095 assert(image->blob->type != UndefinedStream);
4096 blob_info=image->blob;
4097 switch (blob_info->type)
4099 case StandardStream:
4103 c=getc(blob_info->file_info.file);
4106 if (ferror(blob_info->file_info.file) != 0)
4107 ThrowBlobException(blob_info);
4114 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4116 blob_info->eof=MagickTrue;
4119 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4120 blob_info->offset++;
4131 count=ReadBlob(image,1,buffer);
4164MagickExport
double ReadBlobDouble(Image *image)
4175 quantum.double_value=0.0;
4176 quantum.unsigned_value=ReadBlobLongLong(image);
4177 return(quantum.double_value);
4203MagickExport
float ReadBlobFloat(Image *image)
4214 quantum.float_value=0.0;
4215 quantum.unsigned_value=ReadBlobLong(image);
4216 return(quantum.float_value);
4242MagickExport
unsigned int ReadBlobLong(Image *image)
4256 assert(image != (Image *) NULL);
4257 assert(image->signature == MagickCoreSignature);
4259 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4262 if (image->endian == LSBEndian)
4264 value=(
unsigned int) (*p++);
4265 value|=(
unsigned int) (*p++) << 8;
4266 value|=(
unsigned int) (*p++) << 16;
4267 value|=(
unsigned int) (*p++) << 24;
4270 value=(
unsigned int) (*p++) << 24;
4271 value|=(
unsigned int) (*p++) << 16;
4272 value|=(
unsigned int) (*p++) << 8;
4273 value|=(
unsigned int) (*p++);
4300MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4314 assert(image != (Image *) NULL);
4315 assert(image->signature == MagickCoreSignature);
4317 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4319 return(MagickULLConstant(0));
4320 if (image->endian == LSBEndian)
4322 value=(MagickSizeType) (*p++);
4323 value|=(MagickSizeType) (*p++) << 8;
4324 value|=(MagickSizeType) (*p++) << 16;
4325 value|=(MagickSizeType) (*p++) << 24;
4326 value|=(MagickSizeType) (*p++) << 32;
4327 value|=(MagickSizeType) (*p++) << 40;
4328 value|=(MagickSizeType) (*p++) << 48;
4329 value|=(MagickSizeType) (*p++) << 56;
4332 value=(MagickSizeType) (*p++) << 56;
4333 value|=(MagickSizeType) (*p++) << 48;
4334 value|=(MagickSizeType) (*p++) << 40;
4335 value|=(MagickSizeType) (*p++) << 32;
4336 value|=(MagickSizeType) (*p++) << 24;
4337 value|=(MagickSizeType) (*p++) << 16;
4338 value|=(MagickSizeType) (*p++) << 8;
4339 value|=(MagickSizeType) (*p++);
4366MagickExport
unsigned short ReadBlobShort(Image *image)
4380 assert(image != (Image *) NULL);
4381 assert(image->signature == MagickCoreSignature);
4383 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4385 return((
unsigned short) 0U);
4386 if (image->endian == LSBEndian)
4388 value=(
unsigned short) (*p++);
4389 value|=(
unsigned short) (*p++) << 8;
4392 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4393 value|=(
unsigned short) (*p++);
4420MagickExport
unsigned int ReadBlobLSBLong(Image *image)
4434 assert(image != (Image *) NULL);
4435 assert(image->signature == MagickCoreSignature);
4437 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4440 value=(
unsigned int) (*p++);
4441 value|=(
unsigned int) (*p++) << 8;
4442 value|=(
unsigned int) (*p++) << 16;
4443 value|=(
unsigned int) (*p++) << 24;
4470MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
4481 quantum.unsigned_value=ReadBlobLSBLong(image);
4482 return(quantum.signed_value);
4508MagickExport
unsigned short ReadBlobLSBShort(Image *image)
4522 assert(image != (Image *) NULL);
4523 assert(image->signature == MagickCoreSignature);
4525 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4527 return((
unsigned short) 0U);
4528 value=(
unsigned short) (*p++);
4529 value|=(
unsigned short) (*p++) << 8;
4556MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4567 quantum.unsigned_value=ReadBlobLSBShort(image);
4568 return(quantum.signed_value);
4594MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4608 assert(image != (Image *) NULL);
4609 assert(image->signature == MagickCoreSignature);
4611 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4614 value=(
unsigned int) (*p++) << 24;
4615 value|=(
unsigned int) (*p++) << 16;
4616 value|=(
unsigned int) (*p++) << 8;
4617 value|=(
unsigned int) (*p++);
4644MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4658 assert(image != (Image *) NULL);
4659 assert(image->signature == MagickCoreSignature);
4661 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4663 return(MagickULLConstant(0));
4664 value=(MagickSizeType) (*p++) << 56;
4665 value|=(MagickSizeType) (*p++) << 48;
4666 value|=(MagickSizeType) (*p++) << 40;
4667 value|=(MagickSizeType) (*p++) << 32;
4668 value|=(MagickSizeType) (*p++) << 24;
4669 value|=(MagickSizeType) (*p++) << 16;
4670 value|=(MagickSizeType) (*p++) << 8;
4671 value|=(MagickSizeType) (*p++);
4698MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4712 assert(image != (Image *) NULL);
4713 assert(image->signature == MagickCoreSignature);
4715 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4717 return((
unsigned short) 0U);
4718 value=(
unsigned short) ((*p++) << 8);
4719 value|=(
unsigned short) (*p++);
4720 return((
unsigned short) (value & 0xffff));
4746MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4757 quantum.unsigned_value=ReadBlobMSBLong(image);
4758 return(quantum.signed_value);
4784MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4795 quantum.unsigned_value=ReadBlobMSBShort(image);
4796 return(quantum.signed_value);
4822MagickExport
signed int ReadBlobSignedLong(Image *image)
4833 quantum.unsigned_value=ReadBlobLong(image);
4834 return(quantum.signed_value);
4860MagickExport
signed short ReadBlobSignedShort(Image *image)
4871 quantum.unsigned_value=ReadBlobShort(image);
4872 return(quantum.signed_value);
4910MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4911 const size_t length,
void *magick_restrict data,ssize_t *count)
4914 *magick_restrict blob_info;
4916 assert(image != (Image *) NULL);
4917 assert(image->signature == MagickCoreSignature);
4918 assert(image->blob != (BlobInfo *) NULL);
4919 assert(image->blob->type != UndefinedStream);
4920 assert(count != (ssize_t *) NULL);
4921 blob_info=image->blob;
4922 if (blob_info->type != BlobStream)
4924 assert(data != NULL);
4925 *count=ReadBlob(image,length,(
unsigned char *) data);
4928 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4931 blob_info->eof=MagickTrue;
4934 data=blob_info->data+blob_info->offset;
4935 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4936 blob_info->length-blob_info->offset);
4937 blob_info->offset+=(*count);
4938 if (*count != (ssize_t) length)
4939 blob_info->eof=MagickTrue;
4968MagickExport
char *ReadBlobString(Image *image,
char *
string)
4971 *magick_restrict blob_info;
4979 assert(image != (Image *) NULL);
4980 assert(image->signature == MagickCoreSignature);
4981 assert(image->blob != (BlobInfo *) NULL);
4982 assert(image->blob->type != UndefinedStream);
4983 if (IsEventLogging() != MagickFalse)
4984 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4986 blob_info=image->blob;
4987 switch (blob_info->type)
4989 case UndefinedStream:
4991 case StandardStream:
4994 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4995 if (p == (
char *) NULL)
4997 if (ferror(blob_info->file_info.file) != 0)
4998 ThrowBlobException(blob_info);
4999 return((
char *) NULL);
5006#if defined(MAGICKCORE_ZLIB_DELEGATE)
5007 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
5008 if (p == (
char *) NULL)
5011 (void) gzerror(blob_info->file_info.gzfile,&status);
5013 ThrowBlobException(blob_info);
5014 return((
char *) NULL);
5024 c=ReadBlobByte(image);
5027 blob_info->eof=MagickTrue;
5030 string[i++]=(char) c;
5033 }
while (i < (MaxTextExtent-2));
5041 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
5044 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
5046 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
5047 return((
char *) NULL);
5074MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5076 assert(blob != (BlobInfo *) NULL);
5077 assert(blob->signature == MagickCoreSignature);
5078 if (IsEventLogging() != MagickFalse)
5079 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5080 LockSemaphoreInfo(blob->semaphore);
5081 blob->reference_count++;
5082 UnlockSemaphoreInfo(blob->semaphore);
5119MagickExport MagickOffsetType SeekBlob(Image *image,
5120 const MagickOffsetType offset,
const int whence)
5123 *magick_restrict blob_info;
5125 assert(image != (Image *) NULL);
5126 assert(image->signature == MagickCoreSignature);
5127 assert(image->blob != (BlobInfo *) NULL);
5128 assert(image->blob->type != UndefinedStream);
5129 if (IsEventLogging() != MagickFalse)
5130 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5131 blob_info=image->blob;
5132 switch (blob_info->type)
5134 case UndefinedStream:
5136 case StandardStream:
5141 if ((offset < 0) && (whence == SEEK_SET))
5143 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5145 blob_info->offset=TellBlob(image);
5150#if defined(MAGICKCORE_ZLIB_DELEGATE)
5151 if (gzseek(blob_info->file_info.gzfile,(
long) offset,whence) < 0)
5154 blob_info->offset=TellBlob(image);
5170 blob_info->offset=offset;
5175 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5176 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5181 if ((blob_info->offset+offset) < 0)
5183 blob_info->offset+=offset;
5188 if (((MagickOffsetType) blob_info->length+offset) < 0)
5190 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5194 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5196 blob_info->eof=MagickFalse;
5203 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5205 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5206 blob_info->custom_stream->data);
5210 return(blob_info->offset);
5238MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
5240 assert(image != (
const Image *) NULL);
5241 assert(image->signature == MagickCoreSignature);
5242 if (IsEventLogging() != MagickFalse)
5243 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5244 image->blob->exempt=exempt;
5273MagickExport MagickBooleanType SetBlobExtent(Image *image,
5274 const MagickSizeType extent)
5277 *magick_restrict blob_info;
5279 assert(image != (Image *) NULL);
5280 assert(image->signature == MagickCoreSignature);
5281 assert(image->blob != (BlobInfo *) NULL);
5282 assert(image->blob->type != UndefinedStream);
5283 if (IsEventLogging() != MagickFalse)
5284 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5285 blob_info=image->blob;
5286 switch (blob_info->type)
5288 case UndefinedStream:
5290 case StandardStream:
5291 return(MagickFalse);
5300 if (extent != (MagickSizeType) ((off_t) extent))
5301 return(MagickFalse);
5302 offset=SeekBlob(image,0,SEEK_END);
5304 return(MagickFalse);
5305 if ((MagickSizeType) offset >= extent)
5307 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5310 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5311 blob_info->file_info.file);
5312#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5313 if (blob_info->synchronize != MagickFalse)
5318 file=fileno(blob_info->file_info.file);
5319 if ((file == -1) || (offset < 0))
5320 return(MagickFalse);
5321 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5324 offset=SeekBlob(image,offset,SEEK_SET);
5326 return(MagickFalse);
5331 return(MagickFalse);
5333 return(MagickFalse);
5335 return(MagickFalse);
5338 if (extent != (MagickSizeType) ((
size_t) extent))
5339 return(MagickFalse);
5340 if (blob_info->mapped != MagickFalse)
5348 (void) UnmapBlob(blob_info->data,blob_info->length);
5349 RelinquishMagickResource(MapResource,blob_info->length);
5350 if (extent != (MagickSizeType) ((off_t) extent))
5351 return(MagickFalse);
5352 offset=SeekBlob(image,0,SEEK_END);
5354 return(MagickFalse);
5355 if ((MagickSizeType) offset >= extent)
5357 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5358 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5359 blob_info->file_info.file);
5360#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5361 if (blob_info->synchronize != MagickFalse)
5366 file=fileno(blob_info->file_info.file);
5367 if ((file == -1) || (offset < 0))
5368 return(MagickFalse);
5369 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5373 offset=SeekBlob(image,offset,SEEK_SET);
5375 return(MagickFalse);
5376 (void) AcquireMagickResource(MapResource,extent);
5377 blob_info->data=(
unsigned char*) MapBlob(fileno(
5378 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
5379 blob_info->extent=(size_t) extent;
5380 blob_info->length=(size_t) extent;
5381 (void) SyncBlob(image);
5384 blob_info->extent=(size_t) extent;
5385 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5386 blob_info->extent+1,
sizeof(*blob_info->data));
5387 (void) SyncBlob(image);
5388 if (blob_info->data == (
unsigned char *) NULL)
5390 (void) DetachBlob(blob_info);
5391 return(MagickFalse);
5425MagickExport
void SetCustomStreamData(CustomStreamInfo *custom_stream,
5428 assert(custom_stream != (CustomStreamInfo *) NULL);
5429 assert(custom_stream->signature == MagickCoreSignature);
5430 custom_stream->data=data;
5458MagickExport
void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5459 CustomStreamHandler reader)
5461 assert(custom_stream != (CustomStreamInfo *) NULL);
5462 assert(custom_stream->signature == MagickCoreSignature);
5463 custom_stream->reader=reader;
5491MagickExport
void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5492 CustomStreamSeeker seeker)
5494 assert(custom_stream != (CustomStreamInfo *) NULL);
5495 assert(custom_stream->signature == MagickCoreSignature);
5496 custom_stream->seeker=seeker;
5524MagickExport
void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5525 CustomStreamTeller teller)
5527 assert(custom_stream != (CustomStreamInfo *) NULL);
5528 assert(custom_stream->signature == MagickCoreSignature);
5529 custom_stream->teller=teller;
5557MagickExport
void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5558 CustomStreamHandler writer)
5560 assert(custom_stream != (CustomStreamInfo *) NULL);
5561 assert(custom_stream->signature == MagickCoreSignature);
5562 custom_stream->writer=writer;
5589static int SyncBlob(
const Image *image)
5592 *magick_restrict blob_info;
5597 assert(image != (Image *) NULL);
5598 assert(image->signature == MagickCoreSignature);
5599 assert(image->blob != (BlobInfo *) NULL);
5600 if (IsEventLogging() != MagickFalse)
5601 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5602 if (EOFBlob(image) != 0)
5604 blob_info=image->blob;
5606 switch (blob_info->type)
5608 case UndefinedStream:
5609 case StandardStream:
5614 status=fflush(blob_info->file_info.file);
5619#if defined(MAGICKCORE_ZLIB_DELEGATE)
5620 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5626#if defined(MAGICKCORE_BZLIB_DELEGATE)
5627 status=BZ2_bzflush(blob_info->file_info.bzfile);
5663MagickExport MagickOffsetType TellBlob(
const Image *image)
5666 *magick_restrict blob_info;
5671 assert(image != (Image *) NULL);
5672 assert(image->signature == MagickCoreSignature);
5673 assert(image->blob != (BlobInfo *) NULL);
5674 assert(image->blob->type != UndefinedStream);
5675 if (IsEventLogging() != MagickFalse)
5676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5677 blob_info=image->blob;
5679 switch (blob_info->type)
5681 case UndefinedStream:
5682 case StandardStream:
5686 offset=ftell(blob_info->file_info.file);
5693#if defined(MAGICKCORE_ZLIB_DELEGATE)
5694 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5704 offset=blob_info->offset;
5709 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5710 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5742MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5744#if defined(MAGICKCORE_HAVE_MMAP)
5748 status=munmap(map,length);
5749 return(status == -1 ? MagickFalse : MagickTrue);
5753 return(MagickFalse);
5785MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5789 *magick_restrict blob_info;
5803 assert(image != (Image *) NULL);
5804 assert(image->signature == MagickCoreSignature);
5805 assert(image->blob != (BlobInfo *) NULL);
5806 assert(image->blob->type != UndefinedStream);
5809 assert(data != (
const void *) NULL);
5810 blob_info=image->blob;
5812 p=(
const unsigned char *) data;
5813 q=(
unsigned char *) data;
5814 switch (blob_info->type)
5816 case UndefinedStream:
5818 case StandardStream:
5826 count=(ssize_t) fwrite((
const char *) data,1,length,
5827 blob_info->file_info.file);
5832 c=putc((
int) *p++,blob_info->file_info.file);
5840 c=putc((
int) *p++,blob_info->file_info.file);
5848 c=putc((
int) *p++,blob_info->file_info.file);
5856 c=putc((
int) *p++,blob_info->file_info.file);
5865 if ((count != (ssize_t) length) &&
5866 (ferror(blob_info->file_info.file) != 0))
5867 ThrowBlobException(blob_info);
5872#if defined(MAGICKCORE_ZLIB_DELEGATE)
5883 for (i=0; i < length; i+=(size_t) count)
5885 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5886 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5899 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5907 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5915 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5923 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5933 (void) gzerror(blob_info->file_info.gzfile,&status);
5934 if ((count != (ssize_t) length) && (status != Z_OK))
5935 ThrowBlobException(blob_info);
5941#if defined(MAGICKCORE_BZLIB_DELEGATE)
5948 for (i=0; i < length; i+=(size_t) count)
5950 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5951 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5961 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5962 if ((count != (ssize_t) length) && (status != BZ_OK))
5963 ThrowBlobException(blob_info);
5969 count=(ssize_t) blob_info->stream(image,data,length);
5977 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5982 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5983 if (extent >= blob_info->extent)
5985 extent+=blob_info->quantum+length;
5986 blob_info->quantum<<=1;
5987 if (SetBlobExtent(image,extent) == MagickFalse)
5990 q=blob_info->data+blob_info->offset;
5991 (void) memcpy(q,p,length);
5992 blob_info->offset+=(MagickOffsetType) length;
5993 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5994 blob_info->length=(size_t) blob_info->offset;
5995 count=(ssize_t) length;
6000 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6001 count=blob_info->custom_stream->writer((
unsigned char *) data,
6002 length,blob_info->custom_stream->data);
6034MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
6037 *magick_restrict blob_info;
6042 assert(image != (Image *) NULL);
6043 assert(image->signature == MagickCoreSignature);
6044 assert(image->blob != (BlobInfo *) NULL);
6045 assert(image->blob->type != UndefinedStream);
6046 blob_info=image->blob;
6048 switch (blob_info->type)
6050 case StandardStream:
6057 c=putc((
int) value,blob_info->file_info.file);
6060 if (ferror(blob_info->file_info.file) != 0)
6061 ThrowBlobException(blob_info);
6069 count=WriteBlobStream(image,1,&value);
6101MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
6112 quantum.unsigned_value=0U;
6113 quantum.float_value=value;
6114 return(WriteBlobLong(image,quantum.unsigned_value));
6142MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
6147 assert(image != (Image *) NULL);
6148 assert(image->signature == MagickCoreSignature);
6149 if (image->endian == LSBEndian)
6151 buffer[0]=(
unsigned char) value;
6152 buffer[1]=(
unsigned char) (value >> 8);
6153 buffer[2]=(
unsigned char) (value >> 16);
6154 buffer[3]=(
unsigned char) (value >> 24);
6155 return(WriteBlobStream(image,4,buffer));
6157 buffer[0]=(
unsigned char) (value >> 24);
6158 buffer[1]=(
unsigned char) (value >> 16);
6159 buffer[2]=(
unsigned char) (value >> 8);
6160 buffer[3]=(
unsigned char) value;
6161 return(WriteBlobStream(image,4,buffer));
6189MagickExport ssize_t WriteBlobLongLong(Image *image,
const MagickSizeType value)
6194 assert(image != (Image *) NULL);
6195 assert(image->signature == MagickCoreSignature);
6196 if (image->endian == LSBEndian)
6198 buffer[0]=(
unsigned char) value;
6199 buffer[1]=(
unsigned char) (value >> 8);
6200 buffer[2]=(
unsigned char) (value >> 16);
6201 buffer[3]=(
unsigned char) (value >> 24);
6202 buffer[4]=(
unsigned char) (value >> 32);
6203 buffer[5]=(
unsigned char) (value >> 40);
6204 buffer[6]=(
unsigned char) (value >> 48);
6205 buffer[7]=(
unsigned char) (value >> 56);
6206 return(WriteBlobStream(image,8,buffer));
6208 buffer[0]=(
unsigned char) (value >> 56);
6209 buffer[1]=(
unsigned char) (value >> 48);
6210 buffer[2]=(
unsigned char) (value >> 40);
6211 buffer[3]=(
unsigned char) (value >> 32);
6212 buffer[4]=(
unsigned char) (value >> 24);
6213 buffer[5]=(
unsigned char) (value >> 16);
6214 buffer[6]=(
unsigned char) (value >> 8);
6215 buffer[7]=(
unsigned char) value;
6216 return(WriteBlobStream(image,8,buffer));
6244MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
6249 assert(image != (Image *) NULL);
6250 assert(image->signature == MagickCoreSignature);
6251 if (image->endian == LSBEndian)
6253 buffer[0]=(
unsigned char) value;
6254 buffer[1]=(
unsigned char) (value >> 8);
6255 return(WriteBlobStream(image,2,buffer));
6257 buffer[0]=(
unsigned char) (value >> 8);
6258 buffer[1]=(
unsigned char) value;
6259 return(WriteBlobStream(image,2,buffer));
6287MagickExport ssize_t WriteBlobSignedLong(Image *image,
const signed int value)
6301 assert(image != (Image *) NULL);
6302 assert(image->signature == MagickCoreSignature);
6303 quantum.signed_value=value;
6304 if (image->endian == LSBEndian)
6306 buffer[0]=(
unsigned char) quantum.unsigned_value;
6307 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6308 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6309 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6310 return(WriteBlobStream(image,4,buffer));
6312 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6313 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6314 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6315 buffer[3]=(
unsigned char) quantum.unsigned_value;
6316 return(WriteBlobStream(image,4,buffer));
6344MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
6349 assert(image != (Image *) NULL);
6350 assert(image->signature == MagickCoreSignature);
6351 buffer[0]=(
unsigned char) value;
6352 buffer[1]=(
unsigned char) (value >> 8);
6353 buffer[2]=(
unsigned char) (value >> 16);
6354 buffer[3]=(
unsigned char) (value >> 24);
6355 return(WriteBlobStream(image,4,buffer));
6383MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
6388 assert(image != (Image *) NULL);
6389 assert(image->signature == MagickCoreSignature);
6390 buffer[0]=(
unsigned char) value;
6391 buffer[1]=(
unsigned char) (value >> 8);
6392 return(WriteBlobStream(image,2,buffer));
6420MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
6434 assert(image != (Image *) NULL);
6435 assert(image->signature == MagickCoreSignature);
6436 quantum.signed_value=value;
6437 buffer[0]=(
unsigned char) quantum.unsigned_value;
6438 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6439 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6440 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6441 return(WriteBlobStream(image,4,buffer));
6469MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6470 const signed short value)
6484 assert(image != (Image *) NULL);
6485 assert(image->signature == MagickCoreSignature);
6486 quantum.signed_value=value;
6487 buffer[0]=(
unsigned char) quantum.unsigned_value;
6488 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6489 return(WriteBlobStream(image,2,buffer));
6517MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
6522 assert(image != (Image *) NULL);
6523 assert(image->signature == MagickCoreSignature);
6524 buffer[0]=(
unsigned char) (value >> 24);
6525 buffer[1]=(
unsigned char) (value >> 16);
6526 buffer[2]=(
unsigned char) (value >> 8);
6527 buffer[3]=(
unsigned char) value;
6528 return(WriteBlobStream(image,4,buffer));
6556MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6557 const signed short value)
6571 assert(image != (Image *) NULL);
6572 assert(image->signature == MagickCoreSignature);
6573 quantum.signed_value=value;
6574 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6575 buffer[1]=(
unsigned char) quantum.unsigned_value;
6576 return(WriteBlobStream(image,2,buffer));
6604MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
6609 assert(image != (Image *) NULL);
6610 assert(image->signature == MagickCoreSignature);
6611 buffer[0]=(
unsigned char) (value >> 8);
6612 buffer[1]=(
unsigned char) value;
6613 return(WriteBlobStream(image,2,buffer));
6641MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
6643 assert(image != (Image *) NULL);
6644 assert(image->signature == MagickCoreSignature);
6645 assert(
string != (
const char *) NULL);
6646 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));