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)
85#define MagickMaxBlobExtent (8*8192)
86#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
87# define MAP_ANONYMOUS MAP_ANON
89#if !defined(MAP_FAILED)
90#define MAP_FAILED ((void *) -1)
94#define _O_BINARY O_BINARY
105#if defined(MAGICKCORE_ZLIB_DELEGATE)
110#if defined(MAGICKCORE_BZLIB_DELEGATE)
202 SyncBlob(
const Image *);
232 magick_unreferenced(exception);
234 sizeof(*custom_stream));
235 (void) memset(custom_stream,0,
sizeof(*custom_stream));
236 custom_stream->signature=MagickCoreSignature;
237 return(custom_stream);
267MagickExport
void AttachBlob(
BlobInfo *blob_info,
const void *blob,
270 assert(blob_info != (
BlobInfo *) NULL);
271 if (IsEventLogging() != MagickFalse)
272 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
273 blob_info->length=length;
274 blob_info->extent=length;
275 blob_info->quantum=(size_t) MagickMaxBlobExtent;
277 blob_info->type=BlobStream;
278 blob_info->file_info.file=(FILE *) NULL;
279 blob_info->data=(
unsigned char *) blob;
280 blob_info->mapped=MagickFalse;
308MagickExport
void AttachCustomStream(
BlobInfo *blob_info,
311 assert(blob_info != (
BlobInfo *) NULL);
313 assert(custom_stream->signature == MagickCoreSignature);
314 if (IsEventLogging() != MagickFalse)
315 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
316 blob_info->type=CustomStream;
317 blob_info->custom_stream=custom_stream;
350MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
362 assert(filename != (
const char *) NULL);
363 assert(blob != (
const void *) NULL);
364 if (IsEventLogging() != MagickFalse)
365 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
366 if (*filename ==
'\0')
367 file=AcquireUniqueFileResource(filename);
369 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
372 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
375 for (i=0; i < length; i+=(size_t) count)
377 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
387 if ((file == -1) || (i < length))
389 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
426MagickExport
Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
442 assert(image_info != (
ImageInfo *) NULL);
443 assert(image_info->signature == MagickCoreSignature);
445 if (IsEventLogging() != MagickFalse)
446 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
447 image_info->filename);
448 if ((blob == (
const void *) NULL) || (length == 0))
450 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
451 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
452 return((
Image *) NULL);
454 blob_info=CloneImageInfo(image_info);
455 blob_info->blob=(
void *) blob;
456 blob_info->length=length;
457 if (*blob_info->magick ==
'\0')
458 (void) SetImageInfo(blob_info,0,exception);
459 magick_info=GetMagickInfo(blob_info->magick,exception);
462 (void) ThrowMagickException(exception,GetMagickModule(),
463 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
465 blob_info=DestroyImageInfo(blob_info);
466 return((
Image *) NULL);
468 if (GetMagickBlobSupport(magick_info) != MagickFalse)
471 filename[MagickPathExtent];
476 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
477 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
478 blob_info->magick,filename);
479 image=ReadImage(blob_info,exception);
480 if (image != (
Image *) NULL)
481 (void) DetachBlob(image->blob);
482 blob_info=DestroyImageInfo(blob_info);
488 blob_info->blob=(
void *) NULL;
490 *blob_info->filename=
'\0';
491 status=BlobToFile(blob_info->filename,blob,length,exception);
492 if (status == MagickFalse)
494 (void) RelinquishUniqueFileResource(blob_info->filename);
495 blob_info=DestroyImageInfo(blob_info);
496 return((
Image *) NULL);
498 clone_info=CloneImageInfo(blob_info);
499 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
500 blob_info->magick,blob_info->filename);
501 image=ReadImage(clone_info,exception);
502 if (image != (
Image *) NULL)
510 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
512 (void) CopyMagickString(images->filename,image_info->filename,
514 (void) CopyMagickString(images->magick_filename,image_info->filename,
516 (void) CopyMagickString(images->magick,magick_info->name,
518 images=GetNextImageInList(images);
521 clone_info=DestroyImageInfo(clone_info);
522 (void) RelinquishUniqueFileResource(blob_info->filename);
523 blob_info=DestroyImageInfo(blob_info);
558 clone_info=(
BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
559 GetBlobInfo(clone_info);
562 semaphore=clone_info->semaphore;
563 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
564 if (blob_info->mapped != MagickFalse)
565 (void) AcquireMagickResource(MapResource,blob_info->length);
566 clone_info->semaphore=semaphore;
567 LockSemaphoreInfo(clone_info->semaphore);
568 clone_info->reference_count=1;
569 UnlockSemaphoreInfo(clone_info->semaphore);
596static inline void ThrowBlobException(
BlobInfo *blob_info)
598 if ((blob_info->status == 0) && (errno != 0))
599 blob_info->error_number=errno;
600 blob_info->status=(-1);
603MagickExport MagickBooleanType CloseBlob(
Image *image)
606 *magick_restrict blob_info;
614 assert(image != (
Image *) NULL);
615 assert(image->signature == MagickCoreSignature);
616 if (IsEventLogging() != MagickFalse)
617 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
618 blob_info=image->blob;
619 if ((blob_info == (
BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
621 (void) SyncBlob(image);
622 status=blob_info->status;
623 switch (blob_info->type)
625 case UndefinedStream:
631 if (blob_info->synchronize != MagickFalse)
633 status=fflush(blob_info->file_info.file);
635 ThrowBlobException(blob_info);
636 status=fsync(fileno(blob_info->file_info.file));
638 ThrowBlobException(blob_info);
640 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
641 ThrowBlobException(blob_info);
646#if defined(MAGICKCORE_ZLIB_DELEGATE)
648 (void) gzerror(blob_info->file_info.gzfile,&status);
650 ThrowBlobException(blob_info);
656#if defined(MAGICKCORE_BZLIB_DELEGATE)
658 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
660 ThrowBlobException(blob_info);
668 if (blob_info->file_info.file != (FILE *) NULL)
670 if (blob_info->synchronize != MagickFalse)
672 status=fflush(blob_info->file_info.file);
674 ThrowBlobException(blob_info);
675 status=fsync(fileno(blob_info->file_info.file));
677 ThrowBlobException(blob_info);
679 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
680 ThrowBlobException(blob_info);
687 blob_info->size=GetBlobSize(image);
688 image->extent=blob_info->size;
689 blob_info->eof=MagickFalse;
691 blob_info->mode=UndefinedBlobMode;
692 if (blob_info->exempt != MagickFalse)
694 blob_info->type=UndefinedStream;
695 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
697 switch (blob_info->type)
699 case UndefinedStream:
704 if (blob_info->file_info.file != (FILE *) NULL)
706 status=fclose(blob_info->file_info.file);
708 ThrowBlobException(blob_info);
714#if defined(MAGICKCORE_HAVE_PCLOSE)
715 status=pclose(blob_info->file_info.file);
717 ThrowBlobException(blob_info);
723#if defined(MAGICKCORE_ZLIB_DELEGATE)
724 status=gzclose(blob_info->file_info.gzfile);
726 ThrowBlobException(blob_info);
732#if defined(MAGICKCORE_BZLIB_DELEGATE)
733 BZ2_bzclose(blob_info->file_info.bzfile);
741 if (blob_info->file_info.file != (FILE *) NULL)
743 status=fclose(blob_info->file_info.file);
745 ThrowBlobException(blob_info);
752 (void) DetachBlob(blob_info);
753 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
782MagickExport
Image *CustomStreamToImage(
const ImageInfo *image_info,
794 assert(image_info != (
ImageInfo *) NULL);
795 assert(image_info->signature == MagickCoreSignature);
797 assert(image_info->custom_stream->signature == MagickCoreSignature);
798 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
800 if (IsEventLogging() != MagickFalse)
801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
802 image_info->filename);
803 blob_info=CloneImageInfo(image_info);
804 if (*blob_info->magick ==
'\0')
805 (void) SetImageInfo(blob_info,0,exception);
806 magick_info=GetMagickInfo(blob_info->magick,exception);
809 (void) ThrowMagickException(exception,GetMagickModule(),
810 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
812 blob_info=DestroyImageInfo(blob_info);
813 return((
Image *) NULL);
815 image=(
Image *) NULL;
816 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
817 (*blob_info->filename !=
'\0'))
820 filename[MagickPathExtent];
826 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
827 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
828 blob_info->magick,filename);
829 image=ReadImage(blob_info,exception);
834 unique[MagickPathExtent];
849 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
851 if (blob == (
unsigned char *) NULL)
853 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
854 image_info->filename);
855 blob_info=DestroyImageInfo(blob_info);
856 return((
Image *) NULL);
858 file=AcquireUniqueFileResource(unique);
861 ThrowFileException(exception,BlobError,
"UnableToReadBlob",
862 image_info->filename);
863 blob=(
unsigned char *) RelinquishMagickMemory(blob);
864 blob_info=DestroyImageInfo(blob_info);
865 return((
Image *) NULL);
867 clone_info=CloneImageInfo(blob_info);
868 blob_info->file=fdopen(file,
"wb+");
869 if (blob_info->file != (FILE *) NULL)
874 count=(ssize_t) MagickMaxBufferExtent;
875 while (count == (ssize_t) MagickMaxBufferExtent)
877 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
878 image_info->custom_stream->data);
879 count=(ssize_t) write(file,(
const char *) blob,(size_t) count);
881 (void) fclose(blob_info->file);
882 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
883 "%s:%s",blob_info->magick,unique);
884 image=ReadImage(clone_info,exception);
885 if (image != (
Image *) NULL)
893 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
895 (void) CopyMagickString(images->filename,image_info->filename,
897 (void) CopyMagickString(images->magick_filename,
898 image_info->filename,MagickPathExtent);
899 (void) CopyMagickString(images->magick,magick_info->name,
901 images=GetNextImageInList(images);
905 clone_info=DestroyImageInfo(clone_info);
906 blob=(
unsigned char *) RelinquishMagickMemory(blob);
907 (void) RelinquishUniqueFileResource(unique);
909 blob_info=DestroyImageInfo(blob_info);
910 if (image != (
Image *) NULL)
911 if (CloseBlob(image) == MagickFalse)
912 image=DestroyImageList(image);
938MagickExport
void DestroyBlob(
Image *image)
941 *magick_restrict blob_info;
946 assert(image != (
Image *) NULL);
947 assert(image->signature == MagickCoreSignature);
948 assert(image->blob != (
BlobInfo *) NULL);
949 assert(image->blob->signature == MagickCoreSignature);
950 if (IsEventLogging() != MagickFalse)
951 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
952 blob_info=image->blob;
954 LockSemaphoreInfo(blob_info->semaphore);
955 blob_info->reference_count--;
956 assert(blob_info->reference_count >= 0);
957 if (blob_info->reference_count == 0)
959 UnlockSemaphoreInfo(blob_info->semaphore);
960 if (destroy == MagickFalse)
965 (void) CloseBlob(image);
966 if (blob_info->mapped != MagickFalse)
968 (void) UnmapBlob(blob_info->data,blob_info->length);
969 RelinquishMagickResource(MapResource,blob_info->length);
972 RelinquishSemaphoreInfo(&blob_info->semaphore);
973 blob_info->signature=(~MagickCoreSignature);
974 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
1004 assert(custom_stream->signature == MagickCoreSignature);
1005 if (IsEventLogging() != MagickFalse)
1006 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1007 custom_stream->signature=(~MagickCoreSignature);
1009 return(custom_stream);
1034MagickExport
void *DetachBlob(
BlobInfo *blob_info)
1039 assert(blob_info != (
BlobInfo *) NULL);
1040 if (IsEventLogging() != MagickFalse)
1041 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1042 if (blob_info->mapped != MagickFalse)
1044 (void) UnmapBlob(blob_info->data,blob_info->length);
1045 blob_info->data=NULL;
1046 RelinquishMagickResource(MapResource,blob_info->length);
1048 blob_info->mapped=MagickFalse;
1049 blob_info->length=0;
1050 blob_info->offset=0;
1051 blob_info->mode=UndefinedBlobMode;
1052 blob_info->eof=MagickFalse;
1054 blob_info->exempt=MagickFalse;
1055 blob_info->type=UndefinedStream;
1056 blob_info->file_info.file=(FILE *) NULL;
1057 data=blob_info->data;
1058 blob_info->data=(
unsigned char *) NULL;
1059 blob_info->stream=(StreamHandler) NULL;
1088MagickExport
void DisassociateBlob(
Image *image)
1091 *magick_restrict blob_info,
1097 assert(image != (
Image *) NULL);
1098 assert(image->signature == MagickCoreSignature);
1099 assert(image->blob != (
BlobInfo *) NULL);
1100 assert(image->blob->signature == MagickCoreSignature);
1101 if (IsEventLogging() != MagickFalse)
1102 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1103 blob_info=image->blob;
1105 LockSemaphoreInfo(blob_info->semaphore);
1106 assert(blob_info->reference_count >= 0);
1107 if (blob_info->reference_count > 1)
1109 UnlockSemaphoreInfo(blob_info->semaphore);
1110 if (clone == MagickFalse)
1112 clone_info=CloneBlobInfo(blob_info);
1114 image->blob=clone_info;
1142MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
1143 const MagickSizeType length)
1155 buffer[MagickMinBufferExtent >> 1];
1157 assert(image != (
Image *) NULL);
1158 assert(image->signature == MagickCoreSignature);
1159 if (length != (MagickSizeType) ((MagickOffsetType) length))
1160 return(MagickFalse);
1162 for (i=0; i < length; i+=(MagickSizeType) count)
1164 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
1165 (void) ReadBlobStream(image,quantum,buffer,&count);
1173 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1200MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
1202 assert(image != (
Image *) NULL);
1203 assert(image->signature == MagickCoreSignature);
1204 assert(duplicate != (
Image *) NULL);
1205 assert(duplicate->signature == MagickCoreSignature);
1206 if (IsEventLogging() != MagickFalse)
1207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1209 image->blob=ReferenceBlob(duplicate->blob);
1235MagickExport
int EOFBlob(
const Image *image)
1238 *magick_restrict blob_info;
1240 assert(image != (
Image *) NULL);
1241 assert(image->signature == MagickCoreSignature);
1242 assert(image->blob != (
BlobInfo *) NULL);
1243 assert(image->blob->type != UndefinedStream);
1244 if (IsEventLogging() != MagickFalse)
1245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1246 blob_info=image->blob;
1247 switch (blob_info->type)
1249 case UndefinedStream:
1250 case StandardStream:
1255 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1261#if defined(MAGICKCORE_ZLIB_DELEGATE)
1262 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1269#if defined(MAGICKCORE_BZLIB_DELEGATE)
1274 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1275 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1281 blob_info->eof=MagickFalse;
1289 return((
int) blob_info->eof);
1315MagickExport
int ErrorBlob(
const Image *image)
1318 *magick_restrict blob_info;
1320 assert(image != (
Image *) NULL);
1321 assert(image->signature == MagickCoreSignature);
1322 assert(image->blob != (
BlobInfo *) NULL);
1323 assert(image->blob->type != UndefinedStream);
1324 if (IsEventLogging() != MagickFalse)
1325 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1326 blob_info=image->blob;
1327 switch (blob_info->type)
1329 case UndefinedStream:
1330 case StandardStream:
1335 blob_info->error=ferror(blob_info->file_info.file);
1340#if defined(MAGICKCORE_ZLIB_DELEGATE)
1341 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1347#if defined(MAGICKCORE_BZLIB_DELEGATE)
1348 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1362 return(blob_info->error);
1400MagickExport
void *FileToBlob(
const char *filename,
const size_t extent,
1427 assert(filename != (
const char *) NULL);
1429 assert(exception->signature == MagickCoreSignature);
1430 if (IsEventLogging() != MagickFalse)
1431 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1433 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1434 if (status == MagickFalse)
1437 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1438 "NotAuthorized",
"`%s'",filename);
1442 if (LocaleCompare(filename,
"-") != 0)
1444 status=GetPathAttributes(filename,&attributes);
1445 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1447 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1450 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1454 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1457 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1459 if ((file == fileno(stdin)) || (offset < 0) ||
1460 (offset != (MagickOffsetType) ((ssize_t) offset)))
1471 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1472 quantum=(size_t) MagickMaxBufferExtent;
1473 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1474 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1475 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1476 for (i=0; blob != (
unsigned char *) NULL; i+=(size_t) count)
1478 count=read(file,blob+i,quantum);
1485 if (~i < ((
size_t) count+quantum+1))
1487 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1490 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+(
size_t) count+
1491 quantum+1,
sizeof(*blob));
1492 if ((i+(
size_t) count) >= extent)
1495 if (LocaleCompare(filename,
"-") != 0)
1497 if (blob == (
unsigned char *) NULL)
1499 (void) ThrowMagickException(exception,GetMagickModule(),
1500 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1505 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1506 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1509 *length=(size_t) MagickMin(i+(
size_t) count,extent);
1513 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1514 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1515 blob=(
unsigned char *) NULL;
1516 if (~(*length) >= (MagickPathExtent-1))
1517 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1519 if (blob == (
unsigned char *) NULL)
1522 (void) ThrowMagickException(exception,GetMagickModule(),
1523 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1526 map=MapBlob(file,ReadMode,0,*length);
1527 if (map != (
unsigned char *) NULL)
1529 (void) memcpy(blob,map,*length);
1530 (void) UnmapBlob(map,*length);
1534 (void) lseek(file,0,SEEK_SET);
1535 for (i=0; i < *length; i+=(size_t) count)
1537 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1549 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1550 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1555 if (LocaleCompare(filename,
"-") != 0)
1559 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1560 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1589static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1590 const void *magick_restrict data)
1593 *magick_restrict blob_info;
1601 assert(image->blob != (
BlobInfo *) NULL);
1602 assert(image->blob->type != UndefinedStream);
1603 assert(data != NULL);
1604 blob_info=image->blob;
1605 if (blob_info->type != BlobStream)
1606 return(WriteBlob(image,length,(
const unsigned char *) data));
1607 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1608 if (extent >= blob_info->extent)
1610 extent=blob_info->extent+blob_info->quantum+length;
1611 blob_info->quantum<<=1;
1612 if (SetBlobExtent(image,extent) == MagickFalse)
1615 q=blob_info->data+blob_info->offset;
1616 (void) memcpy(q,data,length);
1617 blob_info->offset+=(MagickOffsetType) length;
1618 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1619 blob_info->length=(
size_t) blob_info->offset;
1620 return((ssize_t) length);
1623MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename,
1645 assert(image != (
const Image *) NULL);
1646 assert(image->signature == MagickCoreSignature);
1647 assert(filename != (
const char *) NULL);
1648 if (IsEventLogging() != MagickFalse)
1649 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1650 status=IsRightsAuthorized(PathPolicyDomain,WritePolicyRights,filename);
1651 if (status == MagickFalse)
1654 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1655 "NotAuthorized",
"`%s'",filename);
1656 return(MagickFalse);
1659 if (LocaleCompare(filename,
"-") != 0)
1660 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1663 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1664 return(MagickFalse);
1666 quantum=(size_t) MagickMaxBufferExtent;
1667 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1668 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1669 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1670 if (blob == (
unsigned char *) NULL)
1673 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1675 return(MagickFalse);
1679 count=read(file,blob,quantum);
1686 length=(size_t) count;
1687 count=WriteBlobStream(image,length,blob);
1688 if (count != (ssize_t) length)
1690 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1696 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1697 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1724MagickExport MagickBooleanType GetBlobError(
const Image *image)
1726 assert(image != (
const Image *) NULL);
1727 assert(image->signature == MagickCoreSignature);
1728 if (IsEventLogging() != MagickFalse)
1729 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1730 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1731 errno=image->blob->error_number;
1732 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1757MagickExport FILE *GetBlobFileHandle(
const Image *image)
1759 assert(image != (
const Image *) NULL);
1760 assert(image->signature == MagickCoreSignature);
1761 return(image->blob->file_info.file);
1786MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1788 assert(blob_info != (
BlobInfo *) NULL);
1789 (void) memset(blob_info,0,
sizeof(*blob_info));
1790 blob_info->type=UndefinedStream;
1791 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1792 blob_info->properties.st_mtime=GetMagickTime();
1793 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1794 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1795 blob_info->reference_count=1;
1796 blob_info->semaphore=AcquireSemaphoreInfo();
1797 blob_info->signature=MagickCoreSignature;
1822MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1824 assert(image != (
Image *) NULL);
1825 assert(image->signature == MagickCoreSignature);
1826 if (IsEventLogging() != MagickFalse)
1827 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1828 return(&image->blob->properties);
1854MagickExport MagickSizeType GetBlobSize(
const Image *image)
1857 *magick_restrict blob_info;
1862 assert(image != (
Image *) NULL);
1863 assert(image->signature == MagickCoreSignature);
1864 assert(image->blob != (
BlobInfo *) NULL);
1865 if (IsEventLogging() != MagickFalse)
1866 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1867 blob_info=image->blob;
1869 switch (blob_info->type)
1871 case UndefinedStream:
1872 case StandardStream:
1874 extent=blob_info->size;
1882 extent=(MagickSizeType) blob_info->properties.st_size;
1884 extent=blob_info->size;
1885 file_descriptor=fileno(blob_info->file_info.file);
1886 if (file_descriptor == -1)
1888 if (fstat(file_descriptor,&blob_info->properties) == 0)
1889 extent=(MagickSizeType) blob_info->properties.st_size;
1894 extent=blob_info->size;
1903 status=GetPathAttributes(image->filename,&blob_info->properties);
1904 if (status != MagickFalse)
1905 extent=(MagickSizeType) blob_info->properties.st_size;
1912 extent=(MagickSizeType) blob_info->length;
1917 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
1918 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
1923 offset=blob_info->custom_stream->teller(
1924 blob_info->custom_stream->data);
1925 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
1926 blob_info->custom_stream->data);
1927 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
1928 blob_info->custom_stream->data);
1958MagickExport
void *GetBlobStreamData(
const Image *image)
1960 assert(image != (
const Image *) NULL);
1961 assert(image->signature == MagickCoreSignature);
1962 return(image->blob->data);
1987MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1989 assert(image != (
const Image *) NULL);
1990 assert(image->signature == MagickCoreSignature);
1991 if (IsEventLogging() != MagickFalse)
1992 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1993 return(image->blob->stream);
2029MagickExport
void *ImageToBlob(
const ImageInfo *image_info,
2044 assert(image_info != (
const ImageInfo *) NULL);
2045 assert(image_info->signature == MagickCoreSignature);
2046 assert(image != (
Image *) NULL);
2047 assert(image->signature == MagickCoreSignature);
2049 assert(exception->signature == MagickCoreSignature);
2050 if (IsEventLogging() != MagickFalse)
2051 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2052 image_info->filename);
2054 blob=(
unsigned char *) NULL;
2055 blob_info=CloneImageInfo(image_info);
2056 blob_info->adjoin=MagickFalse;
2057 (void) SetImageInfo(blob_info,1,exception);
2058 if (*blob_info->magick !=
'\0')
2059 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2060 magick_info=GetMagickInfo(image->magick,exception);
2061 if (magick_info == (
const MagickInfo *) NULL)
2063 (void) ThrowMagickException(exception,GetMagickModule(),
2064 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2066 blob_info=DestroyImageInfo(blob_info);
2069 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2070 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2075 blob_info->length=0;
2076 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2077 sizeof(
unsigned char));
2078 if (blob_info->blob == NULL)
2079 (void) ThrowMagickException(exception,GetMagickModule(),
2080 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
2083 (void) CloseBlob(image);
2084 image->blob->exempt=MagickTrue;
2085 *image->filename=
'\0';
2086 status=WriteImage(blob_info,image,exception);
2087 *length=image->blob->length;
2088 blob=DetachBlob(image->blob);
2089 if (blob != (
void *) NULL)
2091 if (status == MagickFalse)
2092 blob=RelinquishMagickMemory(blob);
2094 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2096 else if (status == MagickFalse)
2097 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2103 unique[MagickPathExtent];
2111 file=AcquireUniqueFileResource(unique);
2114 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2115 image_info->filename);
2119 blob_info->file=fdopen(file,
"wb");
2120 if (blob_info->file != (FILE *) NULL)
2122 (void) FormatLocaleString(image->filename,MagickPathExtent,
2123 "%s:%s",image->magick,unique);
2124 status=WriteImage(blob_info,image,exception);
2125 (void) fclose(blob_info->file);
2126 if (status != MagickFalse)
2127 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2129 (void) RelinquishUniqueFileResource(unique);
2132 blob_info=DestroyImageInfo(blob_info);
2164MagickExport
void ImageToCustomStream(
const ImageInfo *image_info,
Image *image,
2177 assert(image_info != (
const ImageInfo *) NULL);
2178 assert(image_info->signature == MagickCoreSignature);
2179 assert(image != (
Image *) NULL);
2180 assert(image->signature == MagickCoreSignature);
2182 assert(image_info->custom_stream->signature == MagickCoreSignature);
2183 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2185 if (IsEventLogging() != MagickFalse)
2186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2187 image_info->filename);
2188 clone_info=CloneImageInfo(image_info);
2189 clone_info->adjoin=MagickFalse;
2190 (void) SetImageInfo(clone_info,1,exception);
2191 if (*clone_info->magick !=
'\0')
2192 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2193 magick_info=GetMagickInfo(image->magick,exception);
2194 if (magick_info == (
const MagickInfo *) NULL)
2196 (void) ThrowMagickException(exception,GetMagickModule(),
2197 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2199 clone_info=DestroyImageInfo(clone_info);
2202 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2203 blob_support=GetMagickBlobSupport(magick_info);
2204 if ((blob_support != MagickFalse) &&
2205 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2207 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2208 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2209 blob_support=MagickFalse;
2211 if (blob_support != MagickFalse)
2216 (void) CloseBlob(image);
2217 *image->filename=
'\0';
2218 (void) WriteImage(clone_info,image,exception);
2223 unique[MagickPathExtent];
2235 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2237 if (blob == (
unsigned char *) NULL)
2239 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2240 image_info->filename);
2241 clone_info=DestroyImageInfo(clone_info);
2244 file=AcquireUniqueFileResource(unique);
2247 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2248 image_info->filename);
2249 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2250 clone_info=DestroyImageInfo(clone_info);
2253 clone_info->file=fdopen(file,
"wb+");
2254 if (clone_info->file != (FILE *) NULL)
2259 (void) FormatLocaleString(image->filename,MagickPathExtent,
2260 "%s:%s",image->magick,unique);
2261 status=WriteImage(clone_info,image,exception);
2262 if (status != MagickFalse)
2264 (void) fseek(clone_info->file,0,SEEK_SET);
2265 count=(ssize_t) MagickMaxBufferExtent;
2266 while (count == (ssize_t) MagickMaxBufferExtent)
2268 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2270 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2271 image_info->custom_stream->data);
2274 (void) fclose(clone_info->file);
2276 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2277 (void) RelinquishUniqueFileResource(unique);
2279 clone_info=DestroyImageInfo(clone_info);
2310MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
2335 assert(image != (
Image *) NULL);
2336 assert(image->signature == MagickCoreSignature);
2337 assert(image->blob != (
BlobInfo *) NULL);
2338 assert(image->blob->type != UndefinedStream);
2339 assert(filename != (
const char *) NULL);
2340 if (IsEventLogging() != MagickFalse)
2341 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
2342 if (*filename ==
'\0')
2343 file=AcquireUniqueFileResource(filename);
2345 if (LocaleCompare(filename,
"-") == 0)
2346 file=fileno(stdout);
2348 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
2351 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2352 return(MagickFalse);
2354 quantum=(size_t) MagickMaxBufferExtent;
2355 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2356 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2357 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2358 if (buffer == (
unsigned char *) NULL)
2361 (void) ThrowMagickException(exception,GetMagickModule(),
2362 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
2363 return(MagickFalse);
2366 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2367 for (i=0; count > 0; )
2369 length=(size_t) count;
2370 for (i=0; i < length; i+=(size_t) count)
2372 count=write(file,p+i,(
size_t) (length-i));
2382 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2384 if (LocaleCompare(filename,
"-") != 0)
2386 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2387 if ((file == -1) || (i < length))
2391 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
2392 return(MagickFalse);
2432MagickExport
void *ImagesToBlob(
const ImageInfo *image_info,
Image *images,
2447 assert(image_info != (
const ImageInfo *) NULL);
2448 assert(image_info->signature == MagickCoreSignature);
2449 assert(images != (
Image *) NULL);
2450 assert(images->signature == MagickCoreSignature);
2452 if (IsEventLogging() != MagickFalse)
2453 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2454 image_info->filename);
2456 blob=(
unsigned char *) NULL;
2457 blob_info=CloneImageInfo(image_info);
2458 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2460 if (*blob_info->magick !=
'\0')
2461 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2462 magick_info=GetMagickInfo(images->magick,exception);
2463 if (magick_info == (
const MagickInfo *) NULL)
2465 (void) ThrowMagickException(exception,GetMagickModule(),
2466 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2468 blob_info=DestroyImageInfo(blob_info);
2471 if (GetMagickAdjoin(magick_info) == MagickFalse)
2473 blob_info=DestroyImageInfo(blob_info);
2474 return(ImageToBlob(image_info,images,length,exception));
2476 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2477 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2482 blob_info->length=0;
2483 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2484 sizeof(
unsigned char));
2485 if (blob_info->blob == (
void *) NULL)
2486 (void) ThrowMagickException(exception,GetMagickModule(),
2487 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2490 (void) CloseBlob(images);
2491 images->blob->exempt=MagickTrue;
2492 *images->filename=
'\0';
2493 status=WriteImages(blob_info,images,images->filename,exception);
2494 *length=images->blob->length;
2495 blob=DetachBlob(images->blob);
2496 if (blob != (
void *) NULL)
2498 if (status == MagickFalse)
2499 blob=RelinquishMagickMemory(blob);
2501 blob=ResizeQuantumMemory(blob,*length+1,
sizeof(
unsigned char));
2503 else if (status == MagickFalse)
2504 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2510 filename[MagickPathExtent],
2511 unique[MagickPathExtent];
2519 file=AcquireUniqueFileResource(unique);
2522 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2523 image_info->filename);
2527 blob_info->file=fdopen(file,
"wb");
2528 if (blob_info->file != (FILE *) NULL)
2530 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2531 images->magick,unique);
2532 status=WriteImages(blob_info,images,filename,exception);
2533 (void) fclose(blob_info->file);
2534 if (status != MagickFalse)
2535 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2537 (void) RelinquishUniqueFileResource(unique);
2540 blob_info=DestroyImageInfo(blob_info);
2572MagickExport
void ImagesToCustomStream(
const ImageInfo *image_info,
2585 assert(image_info != (
const ImageInfo *) NULL);
2586 assert(image_info->signature == MagickCoreSignature);
2587 assert(images != (
Image *) NULL);
2588 assert(images->signature == MagickCoreSignature);
2590 assert(image_info->custom_stream->signature == MagickCoreSignature);
2591 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2593 if (IsEventLogging() != MagickFalse)
2594 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2595 image_info->filename);
2596 clone_info=CloneImageInfo(image_info);
2597 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
2599 if (*clone_info->magick !=
'\0')
2600 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2601 magick_info=GetMagickInfo(images->magick,exception);
2602 if (magick_info == (
const MagickInfo *) NULL)
2604 (void) ThrowMagickException(exception,GetMagickModule(),
2605 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2607 clone_info=DestroyImageInfo(clone_info);
2610 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2611 blob_support=GetMagickBlobSupport(magick_info);
2612 if ((blob_support != MagickFalse) &&
2613 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2615 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2616 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2617 blob_support=MagickFalse;
2619 if (blob_support != MagickFalse)
2624 (void) CloseBlob(images);
2625 *images->filename=
'\0';
2626 (void) WriteImages(clone_info,images,images->filename,exception);
2631 filename[MagickPathExtent],
2632 unique[MagickPathExtent];
2644 blob=(
unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2646 if (blob == (
unsigned char *) NULL)
2648 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2649 image_info->filename);
2650 clone_info=DestroyImageInfo(clone_info);
2653 file=AcquireUniqueFileResource(unique);
2656 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
2657 image_info->filename);
2658 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2659 clone_info=DestroyImageInfo(clone_info);
2662 clone_info->file=fdopen(file,
"wb+");
2663 if (clone_info->file != (FILE *) NULL)
2668 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2669 images->magick,unique);
2670 status=WriteImages(clone_info,images,filename,exception);
2671 if (status != MagickFalse)
2673 (void) fseek(clone_info->file,0,SEEK_SET);
2674 count=(ssize_t) MagickMaxBufferExtent;
2675 while (count == (ssize_t) MagickMaxBufferExtent)
2677 count=(ssize_t) fread(blob,
sizeof(*blob),MagickMaxBufferExtent,
2679 (void) image_info->custom_stream->writer(blob,(
size_t) count,
2680 image_info->custom_stream->data);
2683 (void) fclose(clone_info->file);
2685 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2686 (void) RelinquishUniqueFileResource(unique);
2688 clone_info=DestroyImageInfo(clone_info);
2724MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2728 filename[MagickPathExtent];
2757 assert(image_info != (
ImageInfo *) NULL);
2758 assert(image_info->signature == MagickCoreSignature);
2759 assert(image != (
Image *) NULL);
2760 assert(image->signature == MagickCoreSignature);
2761 assert(inject_image != (
Image *) NULL);
2762 assert(inject_image->signature == MagickCoreSignature);
2764 if (IsEventLogging() != MagickFalse)
2765 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2766 unique_file=(FILE *) NULL;
2767 file=AcquireUniqueFileResource(filename);
2769 unique_file=fdopen(file,
"wb");
2770 if ((file == -1) || (unique_file == (FILE *) NULL))
2772 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2773 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2775 return(MagickFalse);
2777 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2778 if (byte_image == (
Image *) NULL)
2780 (void) fclose(unique_file);
2781 (void) RelinquishUniqueFileResource(filename);
2782 return(MagickFalse);
2784 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2786 DestroyBlob(byte_image);
2787 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2788 write_info=CloneImageInfo(image_info);
2789 SetImageInfoFile(write_info,unique_file);
2790 status=WriteImage(write_info,byte_image,exception);
2791 write_info=DestroyImageInfo(write_info);
2792 byte_image=DestroyImage(byte_image);
2793 (void) fclose(unique_file);
2794 if (status == MagickFalse)
2796 (void) RelinquishUniqueFileResource(filename);
2797 return(MagickFalse);
2802 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2805 (void) RelinquishUniqueFileResource(filename);
2806 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2807 image_info->filename);
2808 return(MagickFalse);
2810 quantum=(size_t) MagickMaxBufferExtent;
2811 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2812 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2813 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2814 if (buffer == (
unsigned char *) NULL)
2816 (void) RelinquishUniqueFileResource(filename);
2818 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2823 ssize_t count = read(file,buffer,quantum);
2830 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2835 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2836 (void) RelinquishUniqueFileResource(filename);
2837 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2863MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2865 assert(image != (
const Image *) NULL);
2866 assert(image->signature == MagickCoreSignature);
2867 if (IsEventLogging() != MagickFalse)
2868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2869 return(image->blob->exempt);
2894MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2897 *magick_restrict blob_info;
2899 assert(image != (
const Image *) NULL);
2900 assert(image->signature == MagickCoreSignature);
2901 if (IsEventLogging() != MagickFalse)
2902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2903 blob_info=image->blob;
2904 switch (blob_info->type)
2913 if (blob_info->file_info.file == (FILE *) NULL)
2914 return(MagickFalse);
2915 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2916 return(status == -1 ? MagickFalse : MagickTrue);
2920#if defined(MAGICKCORE_ZLIB_DELEGATE)
2924 if (blob_info->file_info.gzfile == (gzFile) NULL)
2925 return(MagickFalse);
2926 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2927 return(offset < 0 ? MagickFalse : MagickTrue);
2932 case UndefinedStream:
2936 case StandardStream:
2940 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
2941 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
2948 return(MagickFalse);
2973MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2975 assert(image != (
const Image *) NULL);
2976 assert(image->signature == MagickCoreSignature);
2977 if (IsEventLogging() != MagickFalse)
2978 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2979 return(image->blob->temporary);
3011MagickExport
void *MapBlob(
int file,
const MapMode mode,
3012 const MagickOffsetType offset,
const size_t length)
3014#if defined(MAGICKCORE_HAVE_MMAP)
3027#if defined(MAP_ANONYMOUS)
3028 flags|=MAP_ANONYMOUS;
3037 protection=PROT_READ;
3043 protection=PROT_WRITE;
3049 protection=PROT_READ | PROT_WRITE;
3054#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3055 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3057 map=mmap((
char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3058 if (map == MAP_FAILED)
3059 map=mmap((
char *) NULL,length,protection,flags,file,offset);
3061 if (map == MAP_FAILED)
3098MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
3107 assert(buffer != (
unsigned char *) NULL);
3114 *buffer++=(
unsigned char) c;
3118 *buffer++=(
unsigned char) c;
3148MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
3156 assert(p != (
unsigned char *) NULL);
3163 *p++=(
unsigned char) c;
3199static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
3211 size=MagickMinBufferExtent;
3212 option=GetImageOption(image_info,
"stream:buffer-size");
3213 if (option != (
const char *) NULL)
3214 size=StringToUnsignedLong(option);
3215 status=setvbuf(blob_info->file_info.file,(
char *) NULL,size == 0 ?
3216 _IONBF : _IOFBF,size);
3217 return(status == 0 ? MagickTrue : MagickFalse);
3220#if defined(MAGICKCORE_ZLIB_DELEGATE)
3221static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
3223#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3224 return(gzopen(path,mode));
3232 path_wide=create_wchar_path(path);
3233 if (path_wide == (
wchar_t *) NULL)
3234 return((gzFile) NULL);
3235 file=gzopen_w(path_wide,mode);
3236 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
3242MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
3246 *magick_restrict blob_info;
3249 extension[MagickPathExtent],
3250 filename[MagickPathExtent];
3261 assert(image_info != (
ImageInfo *) NULL);
3262 assert(image_info->signature == MagickCoreSignature);
3263 assert(image != (
Image *) NULL);
3264 assert(image->signature == MagickCoreSignature);
3265 if (IsEventLogging() != MagickFalse)
3266 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3267 image_info->filename);
3268 blob_info=image->blob;
3269 if (image_info->blob != (
void *) NULL)
3271 if (image_info->stream != (StreamHandler) NULL)
3272 blob_info->stream=(StreamHandler) image_info->stream;
3273 AttachBlob(blob_info,image_info->blob,image_info->length);
3277 (*image->filename ==
'\0'))
3279 blob_info->type=CustomStream;
3280 blob_info->custom_stream=image_info->custom_stream;
3283 (void) DetachBlob(blob_info);
3284 blob_info->mode=mode;
3287 default: type=
"r";
break;
3288 case ReadBlobMode: type=
"r";
break;
3289 case ReadBinaryBlobMode: type=
"rb";
break;
3290 case WriteBlobMode: type=
"w";
break;
3291 case WriteBinaryBlobMode: type=
"w+b";
break;
3292 case AppendBlobMode: type=
"a";
break;
3293 case AppendBinaryBlobMode: type=
"a+b";
break;
3296 blob_info->synchronize=image_info->synchronize;
3297 if (image_info->stream != (StreamHandler) NULL)
3299 blob_info->stream=image_info->stream;
3302 blob_info->type=FifoStream;
3310 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3311 rights=ReadPolicyRights;
3313 rights=WritePolicyRights;
3314 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3317 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3318 "NotAuthorized",
"`%s'",filename);
3319 return(MagickFalse);
3321 if ((LocaleCompare(filename,
"-") == 0) ||
3322 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
3324 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
3325#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3326 if (strchr(type,
'b') != (
char *) NULL)
3327 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3329 blob_info->type=StandardStream;
3330 blob_info->exempt=MagickTrue;
3331 return(SetStreamBuffering(image_info,blob_info));
3333 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
3334 (IsGeometry(filename+3) != MagickFalse))
3341 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
3342 if (blob_info->file_info.file == (FILE *) NULL)
3344 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3345 return(MagickFalse);
3347#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3348 if (strchr(type,
'b') != (
char *) NULL)
3349 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3351 blob_info->type=FileStream;
3352 blob_info->exempt=MagickTrue;
3353 return(SetStreamBuffering(image_info,blob_info));
3355#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3356 if (*filename ==
'|')
3359 fileMode[MagickPathExtent],
3367 (void) signal(SIGPIPE,SIG_IGN);
3371 sanitize_command=SanitizeString(filename+1);
3372 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3373 sanitize_command=DestroyString(sanitize_command);
3374 if (blob_info->file_info.file == (FILE *) NULL)
3376 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3377 return(MagickFalse);
3379 blob_info->type=PipeStream;
3380 blob_info->exempt=MagickTrue;
3381 return(SetStreamBuffering(image_info,blob_info));
3384 status=GetPathAttributes(filename,&blob_info->properties);
3385#if defined(S_ISFIFO)
3386 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3388 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3389 if (blob_info->file_info.file == (FILE *) NULL)
3391 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3392 return(MagickFalse);
3394 blob_info->type=FileStream;
3395 blob_info->exempt=MagickTrue;
3396 return(SetStreamBuffering(image_info,blob_info));
3399 GetPathComponent(image->filename,ExtensionPath,extension);
3402 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3403 if ((image_info->adjoin == MagickFalse) ||
3404 (strchr(filename,
'%') != (
char *) NULL))
3409 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
3410 image->scene,filename,exception);
3411 if ((LocaleCompare(filename,image->filename) == 0) &&
3412 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
3413 (GetNextImageInList(image) != (
Image *) NULL)))
3416 path[MagickPathExtent];
3418 GetPathComponent(image->filename,RootPath,path);
3419 if (*extension ==
'\0')
3420 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
3421 path,(
double) image->scene);
3423 (
void) FormatLocaleString(filename,MagickPathExtent,
3424 "%s-%.20g.%s",path,(
double) image->scene,extension);
3426 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3429 if (image_info->file != (FILE *) NULL)
3431 blob_info->file_info.file=image_info->file;
3432 blob_info->type=FileStream;
3433 blob_info->exempt=MagickTrue;
3438 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3439 if (blob_info->file_info.file != (FILE *) NULL)
3447 blob_info->type=FileStream;
3448 (void) SetStreamBuffering(image_info,blob_info);
3449 (void) memset(magick,0,
sizeof(magick));
3450 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
3451 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3452#if defined(MAGICKCORE_POSIX_SUPPORT)
3453 (void) fflush(blob_info->file_info.file);
3455 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3456 " read %.20g magic header bytes",(double) count);
3457#if defined(MAGICKCORE_ZLIB_DELEGATE)
3458 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
3459 ((
int) magick[2] == 0x08))
3462 gzfile = gzopen_utf8(filename,
"rb");
3464 if (gzfile != (gzFile) NULL)
3466 if (blob_info->file_info.file != (FILE *) NULL)
3467 (void) fclose(blob_info->file_info.file);
3468 blob_info->file_info.file=(FILE *) NULL;
3469 blob_info->file_info.gzfile=gzfile;
3470 blob_info->type=ZipStream;
3474#if defined(MAGICKCORE_BZLIB_DELEGATE)
3475 if (strncmp((
char *) magick,
"BZh",3) == 0)
3478 *bzfile = BZ2_bzopen(filename,
"r");
3480 if (bzfile != (BZFILE *) NULL)
3482 if (blob_info->file_info.file != (FILE *) NULL)
3483 (void) fclose(blob_info->file_info.file);
3484 blob_info->file_info.file=(FILE *) NULL;
3485 blob_info->file_info.bzfile=bzfile;
3486 blob_info->type=BZipStream;
3490 if (blob_info->type == FileStream)
3501 sans_exception=AcquireExceptionInfo();
3502 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3503 sans_exception=DestroyExceptionInfo(sans_exception);
3504 length=(size_t) blob_info->properties.st_size;
3505 if ((magick_info != (
const MagickInfo *) NULL) &&
3506 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3507 (length > MagickMaxBufferExtent) &&
3508 (AcquireMagickResource(MapResource,length) != MagickFalse))
3513 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3515 if (blob == (
void *) NULL)
3516 RelinquishMagickResource(MapResource,length);
3522 if (image_info->file != (FILE *) NULL)
3523 blob_info->exempt=MagickFalse;
3526 (void) fclose(blob_info->file_info.file);
3527 blob_info->file_info.file=(FILE *) NULL;
3529 AttachBlob(blob_info,blob,length);
3530 blob_info->mapped=MagickTrue;
3537#if defined(MAGICKCORE_ZLIB_DELEGATE)
3538 if ((LocaleCompare(extension,
"gz") == 0) ||
3539 (LocaleCompare(extension,
"wmz") == 0) ||
3540 (LocaleCompare(extension,
"svgz") == 0))
3542 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3543 if (blob_info->file_info.gzfile != (gzFile) NULL)
3544 blob_info->type=ZipStream;
3548#if defined(MAGICKCORE_BZLIB_DELEGATE)
3549 if (LocaleCompare(extension,
"bz2") == 0)
3551 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3552 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3553 blob_info->type=BZipStream;
3558 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3559 if (blob_info->file_info.file != (FILE *) NULL)
3561 blob_info->type=FileStream;
3562 (void) SetStreamBuffering(image_info,blob_info);
3565 blob_info->status=0;
3566 blob_info->error_number=0;
3567 if (blob_info->type != UndefinedStream)
3568 blob_info->size=GetBlobSize(image);
3571 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3572 return(MagickFalse);
3611#if defined(__cplusplus) || defined(c_plusplus)
3615static size_t PingStream(
const Image *magick_unused(image),
3616 const void *magick_unused(pixels),
const size_t columns)
3618 magick_unreferenced(image);
3619 magick_unreferenced(pixels);
3623#if defined(__cplusplus) || defined(c_plusplus)
3627MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3643 assert(image_info != (
ImageInfo *) NULL);
3644 assert(image_info->signature == MagickCoreSignature);
3646 if (IsEventLogging() != MagickFalse)
3647 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3648 image_info->filename);
3649 if ((blob == (
const void *) NULL) || (length == 0))
3651 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3652 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3653 return((
Image *) NULL);
3655 ping_info=CloneImageInfo(image_info);
3656 ping_info->blob=(
void *) blob;
3657 ping_info->length=length;
3658 ping_info->ping=MagickTrue;
3659 if (*ping_info->magick ==
'\0')
3660 (void) SetImageInfo(ping_info,0,exception);
3661 magick_info=GetMagickInfo(ping_info->magick,exception);
3662 if (magick_info == (
const MagickInfo *) NULL)
3664 (void) ThrowMagickException(exception,GetMagickModule(),
3665 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3667 ping_info=DestroyImageInfo(ping_info);
3668 return((
Image *) NULL);
3670 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3673 filename[MagickPathExtent];
3678 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3679 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3680 ping_info->magick,filename);
3681 image=ReadStream(ping_info,&PingStream,exception);
3682 if (image != (
Image *) NULL)
3683 (void) DetachBlob(image->blob);
3684 ping_info=DestroyImageInfo(ping_info);
3690 ping_info->blob=(
void *) NULL;
3691 ping_info->length=0;
3692 *ping_info->filename=
'\0';
3693 status=BlobToFile(ping_info->filename,blob,length,exception);
3694 if (status == MagickFalse)
3696 (void) RelinquishUniqueFileResource(ping_info->filename);
3697 ping_info=DestroyImageInfo(ping_info);
3698 return((
Image *) NULL);
3700 clone_info=CloneImageInfo(ping_info);
3701 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3702 ping_info->magick,ping_info->filename);
3703 image=ReadStream(clone_info,&PingStream,exception);
3704 if (image != (
Image *) NULL)
3712 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3714 (void) CopyMagickString(images->filename,image_info->filename,
3716 (void) CopyMagickString(images->magick_filename,image_info->filename,
3718 (void) CopyMagickString(images->magick,magick_info->name,
3720 images=GetNextImageInList(images);
3723 clone_info=DestroyImageInfo(clone_info);
3724 (void) RelinquishUniqueFileResource(ping_info->filename);
3725 ping_info=DestroyImageInfo(ping_info);
3760MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
void *data)
3763 *magick_restrict blob_info;
3774 assert(image != (
Image *) NULL);
3775 assert(image->signature == MagickCoreSignature);
3776 assert(image->blob != (
BlobInfo *) NULL);
3777 assert(image->blob->type != UndefinedStream);
3780 assert(data != (
void *) NULL);
3781 blob_info=image->blob;
3783 q=(
unsigned char *) data;
3784 switch (blob_info->type)
3786 case UndefinedStream:
3788 case StandardStream:
3796 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3801 c=getc(blob_info->file_info.file);
3804 *q++=(
unsigned char) c;
3810 c=getc(blob_info->file_info.file);
3813 *q++=(
unsigned char) c;
3819 c=getc(blob_info->file_info.file);
3822 *q++=(
unsigned char) c;
3828 c=getc(blob_info->file_info.file);
3831 *q++=(
unsigned char) c;
3838 if ((count != (ssize_t) length) &&
3839 (ferror(blob_info->file_info.file) != 0))
3840 ThrowBlobException(blob_info);
3845#if defined(MAGICKCORE_ZLIB_DELEGATE)
3856 for (i=0; i < length; i+=(size_t) count)
3858 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3859 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3872 c=gzgetc(blob_info->file_info.gzfile);
3875 *q++=(
unsigned char) c;
3881 c=gzgetc(blob_info->file_info.gzfile);
3884 *q++=(
unsigned char) c;
3890 c=gzgetc(blob_info->file_info.gzfile);
3893 *q++=(
unsigned char) c;
3899 c=gzgetc(blob_info->file_info.gzfile);
3902 *q++=(
unsigned char) c;
3909 (void) gzerror(blob_info->file_info.gzfile,&status);
3910 if ((count != (ssize_t) length) && (status != Z_OK))
3911 ThrowBlobException(blob_info);
3912 if (blob_info->eof == MagickFalse)
3913 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3920#if defined(MAGICKCORE_BZLIB_DELEGATE)
3927 for (i=0; i < length; i+=(size_t) count)
3929 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(
int)
3930 MagickMin(length-i,MagickMaxBufferExtent));
3940 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3941 if ((count != (ssize_t) length) && (status != BZ_OK))
3942 ThrowBlobException(blob_info);
3953 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3955 blob_info->eof=MagickTrue;
3958 p=blob_info->data+blob_info->offset;
3959 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3960 blob_info->length-blob_info->offset);
3961 blob_info->offset+=count;
3962 if (count != (ssize_t) length)
3963 blob_info->eof=MagickTrue;
3964 (void) memcpy(q,p,(
size_t) count);
3969 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
3970 count=blob_info->custom_stream->reader(q,length,
3971 blob_info->custom_stream->data);
4000MagickExport
int ReadBlobByte(
Image *image)
4003 *magick_restrict blob_info;
4008 assert(image != (
Image *) NULL);
4009 assert(image->signature == MagickCoreSignature);
4010 assert(image->blob != (
BlobInfo *) NULL);
4011 assert(image->blob->type != UndefinedStream);
4012 blob_info=image->blob;
4013 switch (blob_info->type)
4015 case StandardStream:
4019 c=getc(blob_info->file_info.file);
4022 if (ferror(blob_info->file_info.file) != 0)
4023 ThrowBlobException(blob_info);
4030 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4032 blob_info->eof=MagickTrue;
4035 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
4036 blob_info->offset++;
4047 count=ReadBlob(image,1,buffer);
4080MagickExport
double ReadBlobDouble(
Image *image)
4091 quantum.double_value=0.0;
4092 quantum.unsigned_value=ReadBlobLongLong(image);
4093 return(quantum.double_value);
4119MagickExport
float ReadBlobFloat(
Image *image)
4130 quantum.float_value=0.0;
4131 quantum.unsigned_value=ReadBlobLong(image);
4132 return(quantum.float_value);
4158MagickExport
unsigned int ReadBlobLong(
Image *image)
4172 assert(image != (
Image *) NULL);
4173 assert(image->signature == MagickCoreSignature);
4175 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4178 if (image->endian == LSBEndian)
4180 value=(
unsigned int) (*p++);
4181 value|=(
unsigned int) (*p++) << 8;
4182 value|=(
unsigned int) (*p++) << 16;
4183 value|=(
unsigned int) (*p++) << 24;
4186 value=(
unsigned int) (*p++) << 24;
4187 value|=(
unsigned int) (*p++) << 16;
4188 value|=(
unsigned int) (*p++) << 8;
4189 value|=(
unsigned int) (*p++);
4216MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
4230 assert(image != (
Image *) NULL);
4231 assert(image->signature == MagickCoreSignature);
4233 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4235 return(MagickULLConstant(0));
4236 if (image->endian == LSBEndian)
4238 value=(MagickSizeType) (*p++);
4239 value|=(MagickSizeType) (*p++) << 8;
4240 value|=(MagickSizeType) (*p++) << 16;
4241 value|=(MagickSizeType) (*p++) << 24;
4242 value|=(MagickSizeType) (*p++) << 32;
4243 value|=(MagickSizeType) (*p++) << 40;
4244 value|=(MagickSizeType) (*p++) << 48;
4245 value|=(MagickSizeType) (*p++) << 56;
4248 value=(MagickSizeType) (*p++) << 56;
4249 value|=(MagickSizeType) (*p++) << 48;
4250 value|=(MagickSizeType) (*p++) << 40;
4251 value|=(MagickSizeType) (*p++) << 32;
4252 value|=(MagickSizeType) (*p++) << 24;
4253 value|=(MagickSizeType) (*p++) << 16;
4254 value|=(MagickSizeType) (*p++) << 8;
4255 value|=(MagickSizeType) (*p++);
4282MagickExport
unsigned short ReadBlobShort(
Image *image)
4296 assert(image != (
Image *) NULL);
4297 assert(image->signature == MagickCoreSignature);
4299 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4301 return((
unsigned short) 0U);
4302 if (image->endian == LSBEndian)
4304 value=(
unsigned short) (*p++);
4305 value|=(
unsigned short) (*p++) << 8;
4308 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
4309 value|=(
unsigned short) (*p++);
4336MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
4350 assert(image != (
Image *) NULL);
4351 assert(image->signature == MagickCoreSignature);
4353 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4356 value=(
unsigned int) (*p++);
4357 value|=(
unsigned int) (*p++) << 8;
4358 value|=(
unsigned int) (*p++) << 16;
4359 value|=(
unsigned int) (*p++) << 24;
4386MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
4397 quantum.unsigned_value=ReadBlobLSBLong(image);
4398 return(quantum.signed_value);
4424MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
4438 assert(image != (
Image *) NULL);
4439 assert(image->signature == MagickCoreSignature);
4441 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4443 return((
unsigned short) 0U);
4444 value=(
unsigned short) (*p++);
4445 value|=(
unsigned short) (*p++) << 8;
4472MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
4483 quantum.unsigned_value=ReadBlobLSBShort(image);
4484 return(quantum.signed_value);
4510MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
4524 assert(image != (
Image *) NULL);
4525 assert(image->signature == MagickCoreSignature);
4527 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4530 value=(
unsigned int) (*p++) << 24;
4531 value|=(
unsigned int) (*p++) << 16;
4532 value|=(
unsigned int) (*p++) << 8;
4533 value|=(
unsigned int) (*p++);
4560MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
4574 assert(image != (
Image *) NULL);
4575 assert(image->signature == MagickCoreSignature);
4577 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4579 return(MagickULLConstant(0));
4580 value=(MagickSizeType) (*p++) << 56;
4581 value|=(MagickSizeType) (*p++) << 48;
4582 value|=(MagickSizeType) (*p++) << 40;
4583 value|=(MagickSizeType) (*p++) << 32;
4584 value|=(MagickSizeType) (*p++) << 24;
4585 value|=(MagickSizeType) (*p++) << 16;
4586 value|=(MagickSizeType) (*p++) << 8;
4587 value|=(MagickSizeType) (*p++);
4614MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
4628 assert(image != (
Image *) NULL);
4629 assert(image->signature == MagickCoreSignature);
4631 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4633 return((
unsigned short) 0U);
4634 value=(
unsigned short) ((*p++) << 8);
4635 value|=(
unsigned short) (*p++);
4636 return((
unsigned short) (value & 0xffff));
4662MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4673 quantum.unsigned_value=ReadBlobMSBLong(image);
4674 return(quantum.signed_value);
4700MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4711 quantum.unsigned_value=ReadBlobMSBShort(image);
4712 return(quantum.signed_value);
4738MagickExport
signed int ReadBlobSignedLong(
Image *image)
4749 quantum.unsigned_value=ReadBlobLong(image);
4750 return(quantum.signed_value);
4776MagickExport
signed short ReadBlobSignedShort(
Image *image)
4787 quantum.unsigned_value=ReadBlobShort(image);
4788 return(quantum.signed_value);
4826MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4827 const size_t length,
void *magick_restrict data,ssize_t *count)
4830 *magick_restrict blob_info;
4832 assert(image != (
Image *) NULL);
4833 assert(image->signature == MagickCoreSignature);
4834 assert(image->blob != (
BlobInfo *) NULL);
4835 assert(image->blob->type != UndefinedStream);
4836 assert(count != (ssize_t *) NULL);
4837 blob_info=image->blob;
4838 if (blob_info->type != BlobStream)
4840 assert(data != NULL);
4841 *count=ReadBlob(image,length,(
unsigned char *) data);
4844 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4847 blob_info->eof=MagickTrue;
4850 data=blob_info->data+blob_info->offset;
4851 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4852 blob_info->length-blob_info->offset);
4853 blob_info->offset+=(*count);
4854 if (*count != (ssize_t) length)
4855 blob_info->eof=MagickTrue;
4884MagickExport
char *ReadBlobString(
Image *image,
char *
string)
4887 *magick_restrict blob_info;
4895 assert(image != (
Image *) NULL);
4896 assert(image->signature == MagickCoreSignature);
4897 assert(image->blob != (
BlobInfo *) NULL);
4898 assert(image->blob->type != UndefinedStream);
4899 if (IsEventLogging() != MagickFalse)
4900 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4902 blob_info=image->blob;
4903 switch (blob_info->type)
4905 case UndefinedStream:
4907 case StandardStream:
4910 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4911 if (p == (
char *) NULL)
4913 if (ferror(blob_info->file_info.file) != 0)
4914 ThrowBlobException(blob_info);
4915 return((
char *) NULL);
4922#if defined(MAGICKCORE_ZLIB_DELEGATE)
4923 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4924 if (p == (
char *) NULL)
4927 (void) gzerror(blob_info->file_info.gzfile,&status);
4929 ThrowBlobException(blob_info);
4930 return((
char *) NULL);
4940 c=ReadBlobByte(image);
4943 blob_info->eof=MagickTrue;
4949 }
while (i < (MaxTextExtent-2));
4957 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4960 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4962 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4963 return((
char *) NULL);
4993 assert(blob->signature == MagickCoreSignature);
4994 if (IsEventLogging() != MagickFalse)
4995 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4996 LockSemaphoreInfo(blob->semaphore);
4997 blob->reference_count++;
4998 UnlockSemaphoreInfo(blob->semaphore);
5035MagickExport MagickOffsetType SeekBlob(
Image *image,
5036 const MagickOffsetType offset,
const int whence)
5039 *magick_restrict blob_info;
5041 assert(image != (
Image *) NULL);
5042 assert(image->signature == MagickCoreSignature);
5043 assert(image->blob != (
BlobInfo *) NULL);
5044 assert(image->blob->type != UndefinedStream);
5045 if (IsEventLogging() != MagickFalse)
5046 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5047 blob_info=image->blob;
5048 switch (blob_info->type)
5050 case UndefinedStream:
5052 case StandardStream:
5057 if ((offset < 0) && (whence == SEEK_SET))
5059 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5061 blob_info->offset=TellBlob(image);
5066#if defined(MAGICKCORE_ZLIB_DELEGATE)
5067 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
5070 blob_info->offset=TellBlob(image);
5086 blob_info->offset=offset;
5091 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5092 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5097 if ((blob_info->offset+offset) < 0)
5099 blob_info->offset+=offset;
5104 if (((MagickOffsetType) blob_info->length+offset) < 0)
5106 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5110 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5112 blob_info->eof=MagickFalse;
5115 if (blob_info->offset >= (MagickOffsetType) ((off_t) blob_info->extent))
5121 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5123 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5124 blob_info->custom_stream->data);
5128 return(blob_info->offset);
5156MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
5158 assert(image != (
const Image *) NULL);
5159 assert(image->signature == MagickCoreSignature);
5160 if (IsEventLogging() != MagickFalse)
5161 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5162 image->blob->exempt=exempt;
5191MagickExport MagickBooleanType SetBlobExtent(
Image *image,
5192 const MagickSizeType extent)
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:
5209 return(MagickFalse);
5218 if (extent != (MagickSizeType) ((off_t) extent))
5219 return(MagickFalse);
5220 offset=SeekBlob(image,0,SEEK_END);
5222 return(MagickFalse);
5223 if ((MagickSizeType) offset >= extent)
5225 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5228 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5229 blob_info->file_info.file);
5230#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5231 if (blob_info->synchronize != MagickFalse)
5236 file=fileno(blob_info->file_info.file);
5237 if ((file == -1) || (offset < 0))
5238 return(MagickFalse);
5239 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5242 offset=SeekBlob(image,offset,SEEK_SET);
5244 return(MagickFalse);
5249 return(MagickFalse);
5251 return(MagickFalse);
5253 return(MagickFalse);
5256 if (extent != (MagickSizeType) ((
size_t) extent))
5257 return(MagickFalse);
5258 if (blob_info->mapped != MagickFalse)
5266 (void) UnmapBlob(blob_info->data,blob_info->length);
5267 RelinquishMagickResource(MapResource,blob_info->length);
5268 if (extent != (MagickSizeType) ((off_t) extent))
5269 return(MagickFalse);
5270 offset=SeekBlob(image,0,SEEK_END);
5272 return(MagickFalse);
5273 if ((MagickSizeType) offset >= extent)
5275 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5276 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
5277 blob_info->file_info.file);
5278#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5279 if (blob_info->synchronize != MagickFalse)
5284 file=fileno(blob_info->file_info.file);
5285 if ((file == -1) || (offset < 0))
5286 return(MagickFalse);
5287 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5291 offset=SeekBlob(image,offset,SEEK_SET);
5293 return(MagickFalse);
5294 (void) AcquireMagickResource(MapResource,extent);
5295 blob_info->data=(
unsigned char*) MapBlob(fileno(
5296 blob_info->file_info.file),WriteMode,0,(size_t) extent);
5297 blob_info->extent=(size_t) extent;
5298 blob_info->length=(size_t) extent;
5299 (void) SyncBlob(image);
5302 blob_info->extent=(size_t) extent;
5303 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
5304 blob_info->extent+1,
sizeof(*blob_info->data));
5305 (void) SyncBlob(image);
5306 if (blob_info->data == (
unsigned char *) NULL)
5308 (void) DetachBlob(blob_info);
5309 return(MagickFalse);
5347 assert(custom_stream->signature == MagickCoreSignature);
5348 custom_stream->data=data;
5377 CustomStreamHandler reader)
5380 assert(custom_stream->signature == MagickCoreSignature);
5381 custom_stream->reader=reader;
5410 CustomStreamSeeker seeker)
5413 assert(custom_stream->signature == MagickCoreSignature);
5414 custom_stream->seeker=seeker;
5443 CustomStreamTeller teller)
5446 assert(custom_stream->signature == MagickCoreSignature);
5447 custom_stream->teller=teller;
5476 CustomStreamHandler writer)
5479 assert(custom_stream->signature == MagickCoreSignature);
5480 custom_stream->writer=writer;
5507static int SyncBlob(
const Image *image)
5510 *magick_restrict blob_info;
5515 assert(image != (
Image *) NULL);
5516 assert(image->signature == MagickCoreSignature);
5517 assert(image->blob != (
BlobInfo *) NULL);
5518 if (IsEventLogging() != MagickFalse)
5519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5520 if (EOFBlob(image) != 0)
5522 blob_info=image->blob;
5524 switch (blob_info->type)
5526 case UndefinedStream:
5527 case StandardStream:
5532 status=fflush(blob_info->file_info.file);
5537#if defined(MAGICKCORE_ZLIB_DELEGATE)
5538 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5544#if defined(MAGICKCORE_BZLIB_DELEGATE)
5545 status=BZ2_bzflush(blob_info->file_info.bzfile);
5581MagickExport MagickOffsetType TellBlob(
const Image *image)
5584 *magick_restrict blob_info;
5589 assert(image != (
Image *) NULL);
5590 assert(image->signature == MagickCoreSignature);
5591 assert(image->blob != (
BlobInfo *) NULL);
5592 assert(image->blob->type != UndefinedStream);
5593 if (IsEventLogging() != MagickFalse)
5594 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5595 blob_info=image->blob;
5597 switch (blob_info->type)
5599 case UndefinedStream:
5600 case StandardStream:
5604 offset=ftell(blob_info->file_info.file);
5611#if defined(MAGICKCORE_ZLIB_DELEGATE)
5612 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5622 offset=blob_info->offset;
5627 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5628 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5660MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5662#if defined(MAGICKCORE_HAVE_MMAP)
5666 status=munmap(map,length);
5667 return(status == -1 ? MagickFalse : MagickTrue);
5671 return(MagickFalse);
5703MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
5707 *magick_restrict blob_info;
5721 assert(image != (
Image *) NULL);
5722 assert(image->signature == MagickCoreSignature);
5723 assert(image->blob != (
BlobInfo *) NULL);
5724 assert(image->blob->type != UndefinedStream);
5727 assert(data != (
const void *) NULL);
5728 blob_info=image->blob;
5730 p=(
const unsigned char *) data;
5731 q=(
unsigned char *) data;
5732 switch (blob_info->type)
5734 case UndefinedStream:
5736 case StandardStream:
5744 count=(ssize_t) fwrite((
const char *) data,1,length,
5745 blob_info->file_info.file);
5750 c=putc((
int) *p++,blob_info->file_info.file);
5758 c=putc((
int) *p++,blob_info->file_info.file);
5766 c=putc((
int) *p++,blob_info->file_info.file);
5774 c=putc((
int) *p++,blob_info->file_info.file);
5783 if ((count != (ssize_t) length) &&
5784 (ferror(blob_info->file_info.file) != 0))
5785 ThrowBlobException(blob_info);
5790#if defined(MAGICKCORE_ZLIB_DELEGATE)
5801 for (i=0; i < length; i+=(size_t) count)
5803 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5804 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5817 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5825 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5833 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5841 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5851 (void) gzerror(blob_info->file_info.gzfile,&status);
5852 if ((count != (ssize_t) length) && (status != Z_OK))
5853 ThrowBlobException(blob_info);
5859#if defined(MAGICKCORE_BZLIB_DELEGATE)
5866 for (i=0; i < length; i+=(size_t) count)
5868 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5869 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5879 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5880 if ((count != (ssize_t) length) && (status != BZ_OK))
5881 ThrowBlobException(blob_info);
5887 count=(ssize_t) blob_info->stream(image,data,length);
5892 if ((blob_info->offset+(MagickOffsetType) length) >=
5893 (MagickOffsetType) blob_info->extent)
5895 if (blob_info->mapped != MagickFalse)
5897 blob_info->extent+=length+blob_info->quantum;
5898 blob_info->quantum<<=1;
5899 blob_info->data=(
unsigned char *) ResizeQuantumMemory(
5900 blob_info->data,blob_info->extent+1,
sizeof(*blob_info->data));
5901 (void) SyncBlob(image);
5902 if (blob_info->data == (
unsigned char *) NULL)
5904 (void) DetachBlob(blob_info);
5908 q=blob_info->data+blob_info->offset;
5909 (void) memcpy(q,p,length);
5910 blob_info->offset+=(MagickOffsetType) length;
5911 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5912 blob_info->length=(
size_t) blob_info->offset;
5913 count=(ssize_t) length;
5918 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
5919 count=blob_info->custom_stream->writer((
unsigned char *) data,
5920 length,blob_info->custom_stream->data);
5952MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
5955 *magick_restrict blob_info;
5960 assert(image != (
Image *) NULL);
5961 assert(image->signature == MagickCoreSignature);
5962 assert(image->blob != (
BlobInfo *) NULL);
5963 assert(image->blob->type != UndefinedStream);
5964 blob_info=image->blob;
5966 switch (blob_info->type)
5968 case StandardStream:
5975 c=putc((
int) value,blob_info->file_info.file);
5978 if (ferror(blob_info->file_info.file) != 0)
5979 ThrowBlobException(blob_info);
5987 count=WriteBlobStream(image,1,&value);
6019MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
6030 quantum.unsigned_value=0U;
6031 quantum.float_value=value;
6032 return(WriteBlobLong(image,quantum.unsigned_value));
6060MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
6065 assert(image != (
Image *) NULL);
6066 assert(image->signature == MagickCoreSignature);
6067 if (image->endian == LSBEndian)
6069 buffer[0]=(
unsigned char) value;
6070 buffer[1]=(
unsigned char) (value >> 8);
6071 buffer[2]=(
unsigned char) (value >> 16);
6072 buffer[3]=(
unsigned char) (value >> 24);
6073 return(WriteBlobStream(image,4,buffer));
6075 buffer[0]=(
unsigned char) (value >> 24);
6076 buffer[1]=(
unsigned char) (value >> 16);
6077 buffer[2]=(
unsigned char) (value >> 8);
6078 buffer[3]=(
unsigned char) value;
6079 return(WriteBlobStream(image,4,buffer));
6107MagickExport ssize_t WriteBlobLongLong(
Image *image,
const MagickSizeType value)
6112 assert(image != (
Image *) NULL);
6113 assert(image->signature == MagickCoreSignature);
6114 if (image->endian == LSBEndian)
6116 buffer[0]=(
unsigned char) value;
6117 buffer[1]=(
unsigned char) (value >> 8);
6118 buffer[2]=(
unsigned char) (value >> 16);
6119 buffer[3]=(
unsigned char) (value >> 24);
6120 buffer[4]=(
unsigned char) (value >> 32);
6121 buffer[5]=(
unsigned char) (value >> 40);
6122 buffer[6]=(
unsigned char) (value >> 48);
6123 buffer[7]=(
unsigned char) (value >> 56);
6124 return(WriteBlobStream(image,8,buffer));
6126 buffer[0]=(
unsigned char) (value >> 56);
6127 buffer[1]=(
unsigned char) (value >> 48);
6128 buffer[2]=(
unsigned char) (value >> 40);
6129 buffer[3]=(
unsigned char) (value >> 32);
6130 buffer[4]=(
unsigned char) (value >> 24);
6131 buffer[5]=(
unsigned char) (value >> 16);
6132 buffer[6]=(
unsigned char) (value >> 8);
6133 buffer[7]=(
unsigned char) value;
6134 return(WriteBlobStream(image,8,buffer));
6162MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
6167 assert(image != (
Image *) NULL);
6168 assert(image->signature == MagickCoreSignature);
6169 if (image->endian == LSBEndian)
6171 buffer[0]=(
unsigned char) value;
6172 buffer[1]=(
unsigned char) (value >> 8);
6173 return(WriteBlobStream(image,2,buffer));
6175 buffer[0]=(
unsigned char) (value >> 8);
6176 buffer[1]=(
unsigned char) value;
6177 return(WriteBlobStream(image,2,buffer));
6205MagickExport ssize_t WriteBlobSignedLong(
Image *image,
const signed int value)
6219 assert(image != (
Image *) NULL);
6220 assert(image->signature == MagickCoreSignature);
6221 quantum.signed_value=value;
6222 if (image->endian == LSBEndian)
6224 buffer[0]=(
unsigned char) quantum.unsigned_value;
6225 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6226 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6227 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6228 return(WriteBlobStream(image,4,buffer));
6230 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
6231 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
6232 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
6233 buffer[3]=(
unsigned char) quantum.unsigned_value;
6234 return(WriteBlobStream(image,4,buffer));
6262MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
6267 assert(image != (
Image *) NULL);
6268 assert(image->signature == MagickCoreSignature);
6269 buffer[0]=(
unsigned char) value;
6270 buffer[1]=(
unsigned char) (value >> 8);
6271 buffer[2]=(
unsigned char) (value >> 16);
6272 buffer[3]=(
unsigned char) (value >> 24);
6273 return(WriteBlobStream(image,4,buffer));
6301MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
6306 assert(image != (
Image *) NULL);
6307 assert(image->signature == MagickCoreSignature);
6308 buffer[0]=(
unsigned char) value;
6309 buffer[1]=(
unsigned char) (value >> 8);
6310 return(WriteBlobStream(image,2,buffer));
6338MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
6352 assert(image != (
Image *) NULL);
6353 assert(image->signature == MagickCoreSignature);
6354 quantum.signed_value=value;
6355 buffer[0]=(
unsigned char) quantum.unsigned_value;
6356 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6357 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
6358 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
6359 return(WriteBlobStream(image,4,buffer));
6387MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
6388 const signed short value)
6402 assert(image != (
Image *) NULL);
6403 assert(image->signature == MagickCoreSignature);
6404 quantum.signed_value=value;
6405 buffer[0]=(
unsigned char) quantum.unsigned_value;
6406 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
6407 return(WriteBlobStream(image,2,buffer));
6435MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
6440 assert(image != (
Image *) NULL);
6441 assert(image->signature == MagickCoreSignature);
6442 buffer[0]=(
unsigned char) (value >> 24);
6443 buffer[1]=(
unsigned char) (value >> 16);
6444 buffer[2]=(
unsigned char) (value >> 8);
6445 buffer[3]=(
unsigned char) value;
6446 return(WriteBlobStream(image,4,buffer));
6474MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
6475 const signed short value)
6489 assert(image != (
Image *) NULL);
6490 assert(image->signature == MagickCoreSignature);
6491 quantum.signed_value=value;
6492 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
6493 buffer[1]=(
unsigned char) quantum.unsigned_value;
6494 return(WriteBlobStream(image,2,buffer));
6522MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
6527 assert(image != (
Image *) NULL);
6528 assert(image->signature == MagickCoreSignature);
6529 buffer[0]=(
unsigned char) (value >> 8);
6530 buffer[1]=(
unsigned char) value;
6531 return(WriteBlobStream(image,2,buffer));
6559MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
6561 assert(image != (
Image *) NULL);
6562 assert(image->signature == MagickCoreSignature);
6563 assert(
string != (
const char *) NULL);
6564 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));