47#include "magick/studio.h"
48#include "magick/blob.h"
49#include "magick/blob-private.h"
50#include "magick/cache.h"
51#include "magick/client.h"
52#include "magick/constitute.h"
53#include "magick/delegate.h"
54#include "magick/exception.h"
55#include "magick/exception-private.h"
56#include "magick/geometry.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/locale_.h"
60#include "magick/log.h"
61#include "magick/magick.h"
62#include "magick/memory_.h"
63#include "magick/nt-base-private.h"
64#include "magick/option.h"
65#include "magick/policy.h"
66#include "magick/resource_.h"
67#include "magick/semaphore.h"
68#include "magick/string_.h"
69#include "magick/string-private.h"
70#include "magick/timer-private.h"
71#include "magick/token.h"
72#include "magick/utility.h"
73#include "magick/utility-private.h"
74#if defined(MAGICKCORE_ZLIB_DELEGATE)
77#if defined(MAGICKCORE_BZLIB_DELEGATE)
84#define MagickMaxBlobExtent (8*8192)
85#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
86# define MAP_ANONYMOUS MAP_ANON
88#if !defined(MAP_FAILED)
89#define MAP_FAILED ((void *) -1)
93#define _O_BINARY O_BINARY
95#if defined(MAGICKCORE_WINDOWS_SUPPORT)
100# define MAGICKCORE_HAVE_MMAP 1
101# define mmap(address,length,protection,access,file,offset) \
102 NTMapMemory(address,length,protection,access,file,offset)
105# define munmap(address,length) NTUnmapMemory(address,length)
108# define pclose _pclose
123#if defined(MAGICKCORE_ZLIB_DELEGATE)
128#if defined(MAGICKCORE_BZLIB_DELEGATE)
198 SyncBlob(
const Image *);
227MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
230 assert(blob_info != (BlobInfo *) NULL);
231 if (IsEventLogging() != MagickFalse)
232 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
233 blob_info->length=length;
234 blob_info->extent=length;
235 blob_info->quantum=(size_t) MagickMaxBlobExtent;
237 blob_info->type=BlobStream;
238 blob_info->file_info.file=(FILE *) NULL;
239 blob_info->data=(
unsigned char *) blob;
240 blob_info->mapped=MagickFalse;
273MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
274 const size_t length,ExceptionInfo *exception)
285 assert(filename != (
const char *) NULL);
286 assert(blob != (
const void *) NULL);
287 if (IsEventLogging() != MagickFalse)
288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
289 if (*filename ==
'\0')
290 file=AcquireUniqueFileResource(filename);
292 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
295 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
298 for (i=0; i < length; i+=(size_t) count)
300 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
301 MagickMaxBufferExtent));
310 if ((file == -1) || (i < length))
312 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
349MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
350 const size_t length,ExceptionInfo *exception)
365 assert(image_info != (ImageInfo *) NULL);
366 assert(image_info->signature == MagickCoreSignature);
367 assert(exception != (ExceptionInfo *) NULL);
368 if (IsEventLogging() != MagickFalse)
369 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
370 image_info->filename);
371 if ((blob == (
const void *) NULL) || (length == 0))
373 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
374 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
375 return((Image *) NULL);
377 blob_info=CloneImageInfo(image_info);
378 blob_info->blob=(
void *) blob;
379 blob_info->length=length;
380 if (*blob_info->magick ==
'\0')
381 (void) SetImageInfo(blob_info,0,exception);
382 magick_info=GetMagickInfo(blob_info->magick,exception);
383 if (magick_info == (
const MagickInfo *) NULL)
385 (void) ThrowMagickException(exception,GetMagickModule(),
386 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
388 blob_info=DestroyImageInfo(blob_info);
389 return((Image *) NULL);
391 if (GetMagickBlobSupport(magick_info) != MagickFalse)
394 filename[MagickPathExtent];
399 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
400 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
401 blob_info->magick,filename);
402 image=ReadImage(blob_info,exception);
403 if (image != (Image *) NULL)
404 (void) DetachBlob(image->blob);
405 blob_info=DestroyImageInfo(blob_info);
411 blob_info->blob=(
void *) NULL;
413 *blob_info->filename=
'\0';
414 status=BlobToFile(blob_info->filename,blob,length,exception);
415 if (status == MagickFalse)
417 (void) RelinquishUniqueFileResource(blob_info->filename);
418 blob_info=DestroyImageInfo(blob_info);
419 return((Image *) NULL);
421 clone_info=CloneImageInfo(blob_info);
422 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
423 blob_info->magick,blob_info->filename);
424 image=ReadImage(clone_info,exception);
425 if (image != (Image *) NULL)
433 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
435 (void) CopyMagickString(images->filename,image_info->filename,
437 (void) CopyMagickString(images->magick_filename,image_info->filename,
439 (void) CopyMagickString(images->magick,magick_info->name,
441 images=GetNextImageInList(images);
444 clone_info=DestroyImageInfo(clone_info);
445 (void) RelinquishUniqueFileResource(blob_info->filename);
446 blob_info=DestroyImageInfo(blob_info);
473MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
481 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
482 GetBlobInfo(clone_info);
483 if (blob_info == (BlobInfo *) NULL)
485 semaphore=clone_info->semaphore;
486 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
487 if (blob_info->mapped != MagickFalse)
488 (void) AcquireMagickResource(MapResource,blob_info->length);
489 clone_info->semaphore=semaphore;
490 LockSemaphoreInfo(clone_info->semaphore);
491 clone_info->reference_count=1;
492 UnlockSemaphoreInfo(clone_info->semaphore);
519static inline void ThrowBlobException(BlobInfo *blob_info)
521 if ((blob_info->status == 0) && (errno != 0))
522 blob_info->error_number=errno;
523 blob_info->status=(-1);
526MagickExport MagickBooleanType CloseBlob(Image *image)
529 *magick_restrict blob_info;
537 assert(image != (Image *) NULL);
538 assert(image->signature == MagickCoreSignature);
539 if (IsEventLogging() != MagickFalse)
540 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
541 blob_info=image->blob;
542 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
544 (void) SyncBlob(image);
545 status=blob_info->status;
546 switch (blob_info->type)
548 case UndefinedStream:
554 if (blob_info->synchronize != MagickFalse)
556 status=fflush(blob_info->file_info.file);
558 ThrowBlobException(blob_info);
559 status=fsync(fileno(blob_info->file_info.file));
561 ThrowBlobException(blob_info);
563 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
564 ThrowBlobException(blob_info);
569#if defined(MAGICKCORE_ZLIB_DELEGATE)
571 (void) gzerror(blob_info->file_info.gzfile,&status);
573 ThrowBlobException(blob_info);
579#if defined(MAGICKCORE_BZLIB_DELEGATE)
581 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
583 ThrowBlobException(blob_info);
591 if (blob_info->file_info.file != (FILE *) NULL)
593 if (blob_info->synchronize != MagickFalse)
595 status=fflush(blob_info->file_info.file);
597 ThrowBlobException(blob_info);
598 status=fsync(fileno(blob_info->file_info.file));
600 ThrowBlobException(blob_info);
602 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
603 ThrowBlobException(blob_info);
608 blob_info->size=GetBlobSize(image);
609 image->extent=blob_info->size;
610 blob_info->eof=MagickFalse;
612 blob_info->mode=UndefinedBlobMode;
613 if (blob_info->exempt != MagickFalse)
615 blob_info->type=UndefinedStream;
616 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
618 switch (blob_info->type)
620 case UndefinedStream:
625 if (blob_info->file_info.file != (FILE *) NULL)
627 status=fclose(blob_info->file_info.file);
629 ThrowBlobException(blob_info);
635#if defined(MAGICKCORE_HAVE_PCLOSE)
636 status=pclose(blob_info->file_info.file);
638 ThrowBlobException(blob_info);
644#if defined(MAGICKCORE_ZLIB_DELEGATE)
645 status=gzclose(blob_info->file_info.gzfile);
647 ThrowBlobException(blob_info);
653#if defined(MAGICKCORE_BZLIB_DELEGATE)
654 BZ2_bzclose(blob_info->file_info.bzfile);
662 if (blob_info->file_info.file != (FILE *) NULL)
664 status=fclose(blob_info->file_info.file);
666 ThrowBlobException(blob_info);
671 (void) DetachBlob(blob_info);
672 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
697MagickExport
void DestroyBlob(Image *image)
700 *magick_restrict blob_info;
705 assert(image != (Image *) NULL);
706 assert(image->signature == MagickCoreSignature);
707 assert(image->blob != (BlobInfo *) NULL);
708 assert(image->blob->signature == MagickCoreSignature);
709 if (IsEventLogging() != MagickFalse)
710 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
711 blob_info=image->blob;
713 LockSemaphoreInfo(blob_info->semaphore);
714 blob_info->reference_count--;
715 assert(blob_info->reference_count >= 0);
716 if (blob_info->reference_count == 0)
718 UnlockSemaphoreInfo(blob_info->semaphore);
719 if (destroy == MagickFalse)
721 image->blob=(BlobInfo *) NULL;
724 (void) CloseBlob(image);
725 if (blob_info->mapped != MagickFalse)
727 (void) UnmapBlob(blob_info->data,blob_info->length);
728 RelinquishMagickResource(MapResource,blob_info->length);
731 DestroySemaphoreInfo(&blob_info->semaphore);
732 blob_info->signature=(~MagickCoreSignature);
733 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
758MagickExport
unsigned char *DetachBlob(BlobInfo *blob_info)
763 assert(blob_info != (BlobInfo *) NULL);
764 if (IsEventLogging() != MagickFalse)
765 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
766 if (blob_info->mapped != MagickFalse)
768 (void) UnmapBlob(blob_info->data,blob_info->length);
769 blob_info->data=NULL;
770 RelinquishMagickResource(MapResource,blob_info->length);
772 blob_info->mapped=MagickFalse;
775 blob_info->mode=UndefinedBlobMode;
776 blob_info->eof=MagickFalse;
778 blob_info->exempt=MagickFalse;
779 blob_info->type=UndefinedStream;
780 blob_info->file_info.file=(FILE *) NULL;
781 data=blob_info->data;
782 blob_info->data=(
unsigned char *) NULL;
783 blob_info->stream=(StreamHandler) NULL;
811MagickPrivate
void DisassociateBlob(Image *image)
814 *magick_restrict blob_info,
820 assert(image != (Image *) NULL);
821 assert(image->signature == MagickCoreSignature);
822 assert(image->blob != (BlobInfo *) NULL);
823 assert(image->blob->signature == MagickCoreSignature);
824 if (IsEventLogging() != MagickFalse)
825 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
826 blob_info=image->blob;
828 LockSemaphoreInfo(blob_info->semaphore);
829 assert(blob_info->reference_count >= 0);
830 if (blob_info->reference_count > 1)
832 UnlockSemaphoreInfo(blob_info->semaphore);
833 if (clone == MagickFalse)
835 clone_info=CloneBlobInfo(blob_info);
837 image->blob=clone_info;
865MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
866 const MagickSizeType length)
878 buffer[MagickMinBufferExtent >> 1];
880 assert(image != (Image *) NULL);
881 assert(image->signature == MagickCoreSignature);
882 if (length != (MagickSizeType) ((MagickOffsetType) length))
885 for (i=0; i < length; i+=(MagickSizeType) count)
887 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
888 (void) ReadBlobStream(image,quantum,buffer,&count);
896 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
923MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
925 assert(image != (Image *) NULL);
926 assert(image->signature == MagickCoreSignature);
927 assert(duplicate != (Image *) NULL);
928 assert(duplicate->signature == MagickCoreSignature);
929 if (IsEventLogging() != MagickFalse)
930 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
932 image->blob=ReferenceBlob(duplicate->blob);
958MagickExport
int EOFBlob(
const Image *image)
961 *magick_restrict blob_info;
963 assert(image != (Image *) NULL);
964 assert(image->signature == MagickCoreSignature);
965 assert(image->blob != (BlobInfo *) NULL);
966 assert(image->blob->type != UndefinedStream);
967 if (IsEventLogging() != MagickFalse)
968 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
969 blob_info=image->blob;
970 switch (blob_info->type)
972 case UndefinedStream:
978 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
984#if defined(MAGICKCORE_ZLIB_DELEGATE)
985 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
992#if defined(MAGICKCORE_BZLIB_DELEGATE)
997 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
998 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1004 blob_info->eof=MagickFalse;
1010 return((
int) blob_info->eof);
1036MagickExport
int ErrorBlob(
const Image *image)
1039 *magick_restrict blob_info;
1041 assert(image != (Image *) NULL);
1042 assert(image->signature == MagickCoreSignature);
1043 assert(image->blob != (BlobInfo *) NULL);
1044 assert(image->blob->type != UndefinedStream);
1045 if (IsEventLogging() != MagickFalse)
1046 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1047 blob_info=image->blob;
1048 switch (blob_info->type)
1050 case UndefinedStream:
1051 case StandardStream:
1056 blob_info->error=ferror(blob_info->file_info.file);
1061#if defined(MAGICKCORE_ZLIB_DELEGATE)
1062 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1068#if defined(MAGICKCORE_BZLIB_DELEGATE)
1069 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1081 return(blob_info->error);
1119MagickExport
unsigned char *FileToBlob(
const char *filename,
const size_t extent,
1120 size_t *length,ExceptionInfo *exception)
1146 assert(filename != (
const char *) NULL);
1147 assert(exception != (ExceptionInfo *) NULL);
1148 assert(exception->signature == MagickCoreSignature);
1149 if (IsEventLogging() != MagickFalse)
1150 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1152 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1153 if (status == MagickFalse)
1156 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1157 "NotAuthorized",
"`%s'",filename);
1161 if (LocaleCompare(filename,
"-") != 0)
1163 status=GetPathAttributes(filename,&attributes);
1164 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1166 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1169 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1173 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1174 return((
unsigned char *) NULL);
1176 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1178 if ((file == fileno(stdin)) || (offset < 0) ||
1179 (offset != (MagickOffsetType) ((ssize_t) offset)))
1190 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1191 quantum=(size_t) MagickMaxBufferExtent;
1192 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1193 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1194 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1195 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1197 count=read(file,blob+i,quantum);
1204 if (~((
size_t) i) < (count+quantum+1))
1206 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1209 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+count+quantum+1,
1211 if ((
size_t) (i+count) >= extent)
1214 if (LocaleCompare(filename,
"-") != 0)
1216 if (blob == (
unsigned char *) NULL)
1218 (void) ThrowMagickException(exception,GetMagickModule(),
1219 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1220 return((
unsigned char *) NULL);
1224 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1225 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1226 return((
unsigned char *) NULL);
1228 *length=(size_t) MagickMin(i+count,extent);
1232 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1233 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1234 blob=(
unsigned char *) NULL;
1235 if (~(*length) >= (MagickPathExtent-1))
1236 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1238 if (blob == (
unsigned char *) NULL)
1241 (void) ThrowMagickException(exception,GetMagickModule(),
1242 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1243 return((
unsigned char *) NULL);
1245 map=MapBlob(file,ReadMode,0,*length);
1246 if (map != (
unsigned char *) NULL)
1248 (void) memcpy(blob,map,*length);
1249 (void) UnmapBlob(map,*length);
1253 (void) lseek(file,0,SEEK_SET);
1254 for (i=0; i < *length; i+=count)
1256 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1257 MagickMaxBufferExtent));
1268 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1269 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1270 return((
unsigned char *) NULL);
1274 if (LocaleCompare(filename,
"-") != 0)
1278 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1279 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1309static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1310 const unsigned char *magick_restrict data)
1313 *magick_restrict blob_info;
1321 assert(image->blob != (BlobInfo *) NULL);
1322 assert(image->blob->type != UndefinedStream);
1323 assert(data != (
void *) NULL);
1324 blob_info=image->blob;
1325 if (blob_info->type != BlobStream)
1326 return(WriteBlob(image,length,data));
1327 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1328 if (extent >= blob_info->extent)
1330 extent+=blob_info->quantum+length;
1331 blob_info->quantum<<=1;
1332 if (SetBlobExtent(image,extent) == MagickFalse)
1335 q=blob_info->data+blob_info->offset;
1336 (void) memcpy(q,data,length);
1337 blob_info->offset+=length;
1338 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1339 blob_info->length=(size_t) blob_info->offset;
1340 return((ssize_t) length);
1343MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename)
1364 assert(image != (
const Image *) NULL);
1365 assert(image->signature == MagickCoreSignature);
1366 assert(filename != (
const char *) NULL);
1367 if (IsEventLogging() != MagickFalse)
1368 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1369 status=IsRightsAuthorized(PathPolicyDomain,WritePolicyRights,filename);
1370 if (status == MagickFalse)
1373 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1374 PolicyError,
"NotAuthorized",
"`%s'",filename);
1375 return(MagickFalse);
1378 if (LocaleCompare(filename,
"-") != 0)
1379 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1382 ThrowFileException(&image->exception,BlobError,
"UnableToOpenBlob",
1384 return(MagickFalse);
1386 quantum=(size_t) MagickMaxBufferExtent;
1387 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1388 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1389 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1390 if (blob == (
unsigned char *) NULL)
1393 ThrowFileException(&image->exception,ResourceLimitError,
1394 "MemoryAllocationFailed",filename);
1395 return(MagickFalse);
1399 count=read(file,blob,quantum);
1406 length=(size_t) count;
1407 count=WriteBlobStream(image,length,blob);
1408 if (count != (ssize_t) length)
1410 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1417 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1419 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1446MagickExport MagickBooleanType GetBlobError(
const Image *image)
1448 assert(image != (
const Image *) NULL);
1449 assert(image->signature == MagickCoreSignature);
1450 if (IsEventLogging() != MagickFalse)
1451 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1452 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1453 errno=image->blob->error_number;
1454 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1479MagickExport FILE *GetBlobFileHandle(
const Image *image)
1481 assert(image != (
const Image *) NULL);
1482 assert(image->signature == MagickCoreSignature);
1483 return(image->blob->file_info.file);
1508MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1510 assert(blob_info != (BlobInfo *) NULL);
1511 (void) memset(blob_info,0,
sizeof(*blob_info));
1512 blob_info->type=UndefinedStream;
1513 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1514 blob_info->properties.st_mtime=GetMagickTime();
1515 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1516 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1517 blob_info->reference_count=1;
1518 blob_info->semaphore=AllocateSemaphoreInfo();
1519 blob_info->signature=MagickCoreSignature;
1544MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1546 assert(image != (Image *) NULL);
1547 assert(image->signature == MagickCoreSignature);
1548 if (IsEventLogging() != MagickFalse)
1549 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1550 return(&image->blob->properties);
1576MagickExport MagickSizeType GetBlobSize(
const Image *image)
1579 *magick_restrict blob_info;
1584 assert(image != (Image *) NULL);
1585 assert(image->signature == MagickCoreSignature);
1586 assert(image->blob != (BlobInfo *) NULL);
1587 if (IsEventLogging() != MagickFalse)
1588 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1589 blob_info=image->blob;
1591 switch (blob_info->type)
1593 case UndefinedStream:
1594 case StandardStream:
1596 extent=blob_info->size;
1604 extent=(MagickSizeType) blob_info->properties.st_size;
1606 extent=blob_info->size;
1607 file_descriptor=fileno(blob_info->file_info.file);
1608 if (file_descriptor == -1)
1610 if (fstat(file_descriptor,&blob_info->properties) == 0)
1611 extent=(MagickSizeType) blob_info->properties.st_size;
1616 extent=blob_info->size;
1625 status=GetPathAttributes(image->filename,&blob_info->properties);
1626 if (status != MagickFalse)
1627 extent=(MagickSizeType) blob_info->properties.st_size;
1634 extent=(MagickSizeType) blob_info->length;
1663MagickExport
unsigned char *GetBlobStreamData(
const Image *image)
1665 assert(image != (
const Image *) NULL);
1666 assert(image->signature == MagickCoreSignature);
1667 return(image->blob->data);
1692MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1694 assert(image != (
const Image *) NULL);
1695 assert(image->signature == MagickCoreSignature);
1696 if (IsEventLogging() != MagickFalse)
1697 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1698 return(image->blob->stream);
1734MagickExport
unsigned char *ImageToBlob(
const ImageInfo *image_info,
1735 Image *image,
size_t *length,ExceptionInfo *exception)
1749 assert(image_info != (
const ImageInfo *) NULL);
1750 assert(image_info->signature == MagickCoreSignature);
1751 assert(image != (Image *) NULL);
1752 assert(image->signature == MagickCoreSignature);
1753 assert(exception != (ExceptionInfo *) NULL);
1754 assert(exception->signature == MagickCoreSignature);
1755 if (IsEventLogging() != MagickFalse)
1756 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1757 image_info->filename);
1759 blob=(
unsigned char *) NULL;
1760 blob_info=CloneImageInfo(image_info);
1761 blob_info->adjoin=MagickFalse;
1762 (void) SetImageInfo(blob_info,1,exception);
1763 if (*blob_info->magick !=
'\0')
1764 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
1765 magick_info=GetMagickInfo(image->magick,exception);
1766 if (magick_info == (
const MagickInfo *) NULL)
1768 (void) ThrowMagickException(exception,GetMagickModule(),
1769 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
1771 blob_info=DestroyImageInfo(blob_info);
1774 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
1775 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1780 blob_info->length=0;
1781 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1782 sizeof(
unsigned char));
1783 if (blob_info->blob == NULL)
1784 (void) ThrowMagickException(exception,GetMagickModule(),
1785 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1788 (void) CloseBlob(image);
1789 image->blob->exempt=MagickTrue;
1790 *image->filename=
'\0';
1791 status=WriteImage(blob_info,image);
1792 InheritException(exception,&image->exception);
1793 *length=image->blob->length;
1794 blob=DetachBlob(image->blob);
1795 if (blob != (
void *) NULL)
1797 if (status == MagickFalse)
1798 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1800 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
1801 sizeof(
unsigned char));
1803 else if (status == MagickFalse)
1804 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
1810 unique[MagickPathExtent];
1818 file=AcquireUniqueFileResource(unique);
1821 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
1822 image_info->filename);
1826 blob_info->file=fdopen(file,
"wb");
1827 if (blob_info->file != (FILE *) NULL)
1829 (void) FormatLocaleString(image->filename,MagickPathExtent,
1830 "%s:%s",image->magick,unique);
1831 status=WriteImage(blob_info,image);
1832 (void) fclose(blob_info->file);
1833 if (status == MagickFalse)
1834 InheritException(exception,&image->exception);
1836 blob=FileToBlob(unique,SIZE_MAX,length,exception);
1838 (void) RelinquishUniqueFileResource(unique);
1841 blob_info=DestroyImageInfo(blob_info);
1873MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
1874 ExceptionInfo *exception)
1898 assert(image != (Image *) NULL);
1899 assert(image->signature == MagickCoreSignature);
1900 assert(image->blob != (BlobInfo *) NULL);
1901 assert(image->blob->type != UndefinedStream);
1902 assert(filename != (
const char *) NULL);
1903 if (IsEventLogging() != MagickFalse)
1904 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1905 if (*filename ==
'\0')
1906 file=AcquireUniqueFileResource(filename);
1908 if (LocaleCompare(filename,
"-") == 0)
1909 file=fileno(stdout);
1911 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1914 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1915 return(MagickFalse);
1917 quantum=(size_t) MagickMaxBufferExtent;
1918 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1919 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1920 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
1921 if (buffer == (
unsigned char *) NULL)
1924 (void) ThrowMagickException(exception,GetMagickModule(),
1925 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
1926 return(MagickFalse);
1929 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1930 for (i=0; count > 0; )
1932 length=(size_t) count;
1933 for (i=0; i < length; i+=count)
1935 count=write(file,p+i,(
size_t) (length-i));
1945 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1947 if (LocaleCompare(filename,
"-") != 0)
1949 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
1950 if ((file == -1) || (i < length))
1954 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1955 return(MagickFalse);
1995MagickExport
unsigned char *ImagesToBlob(
const ImageInfo *image_info,
1996 Image *images,
size_t *length,ExceptionInfo *exception)
2010 assert(image_info != (
const ImageInfo *) NULL);
2011 assert(image_info->signature == MagickCoreSignature);
2012 assert(images != (Image *) NULL);
2013 assert(images->signature == MagickCoreSignature);
2014 assert(exception != (ExceptionInfo *) NULL);
2015 if (IsEventLogging() != MagickFalse)
2016 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2017 image_info->filename);
2019 blob=(
unsigned char *) NULL;
2020 blob_info=CloneImageInfo(image_info);
2021 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2023 if (*blob_info->magick !=
'\0')
2024 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2025 magick_info=GetMagickInfo(images->magick,exception);
2026 if (magick_info == (
const MagickInfo *) NULL)
2028 (void) ThrowMagickException(exception,GetMagickModule(),
2029 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2031 blob_info=DestroyImageInfo(blob_info);
2034 if (GetMagickAdjoin(magick_info) == MagickFalse)
2036 blob_info=DestroyImageInfo(blob_info);
2037 return(ImageToBlob(image_info,images,length,exception));
2039 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2040 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2045 blob_info->length=0;
2046 blob_info->blob=(
void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2047 sizeof(
unsigned char));
2048 if (blob_info->blob == (
void *) NULL)
2049 (void) ThrowMagickException(exception,GetMagickModule(),
2050 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2053 (void) CloseBlob(images);
2054 images->blob->exempt=MagickTrue;
2055 *images->filename=
'\0';
2056 status=WriteImages(blob_info,images,images->filename,exception);
2057 *length=images->blob->length;
2058 blob=DetachBlob(images->blob);
2059 if (blob != (
void *) NULL)
2061 if (status == MagickFalse)
2062 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2064 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
2065 sizeof(
unsigned char));
2068 if (status == MagickFalse)
2069 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2075 filename[MagickPathExtent],
2076 unique[MagickPathExtent];
2084 file=AcquireUniqueFileResource(unique);
2087 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2088 image_info->filename);
2092 blob_info->file=fdopen(file,
"wb");
2093 if (blob_info->file != (FILE *) NULL)
2095 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2096 images->magick,unique);
2097 status=WriteImages(blob_info,images,filename,exception);
2098 (void) fclose(blob_info->file);
2099 if (status == MagickFalse)
2100 InheritException(exception,&images->exception);
2102 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2104 (void) RelinquishUniqueFileResource(unique);
2107 blob_info=DestroyImageInfo(blob_info);
2143MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2144 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2147 filename[MagickPathExtent];
2176 assert(image_info != (ImageInfo *) NULL);
2177 assert(image_info->signature == MagickCoreSignature);
2178 assert(image != (Image *) NULL);
2179 assert(image->signature == MagickCoreSignature);
2180 assert(inject_image != (Image *) NULL);
2181 assert(inject_image->signature == MagickCoreSignature);
2182 assert(exception != (ExceptionInfo *) NULL);
2183 if (IsEventLogging() != MagickFalse)
2184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2185 unique_file=(FILE *) NULL;
2186 file=AcquireUniqueFileResource(filename);
2188 unique_file=fdopen(file,
"wb");
2189 if ((file == -1) || (unique_file == (FILE *) NULL))
2191 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2192 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2194 return(MagickFalse);
2196 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2197 if (byte_image == (Image *) NULL)
2199 (void) fclose(unique_file);
2200 (void) RelinquishUniqueFileResource(filename);
2201 return(MagickFalse);
2203 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2205 DestroyBlob(byte_image);
2206 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2207 write_info=CloneImageInfo(image_info);
2208 SetImageInfoFile(write_info,unique_file);
2209 status=WriteImage(write_info,byte_image);
2210 write_info=DestroyImageInfo(write_info);
2211 byte_image=DestroyImage(byte_image);
2212 (void) fclose(unique_file);
2213 if (status == MagickFalse)
2215 (void) RelinquishUniqueFileResource(filename);
2216 return(MagickFalse);
2221 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2224 (void) RelinquishUniqueFileResource(filename);
2225 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2226 image_info->filename);
2227 return(MagickFalse);
2229 quantum=(size_t) MagickMaxBufferExtent;
2230 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2231 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2232 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2233 if (buffer == (
unsigned char *) NULL)
2235 (void) RelinquishUniqueFileResource(filename);
2237 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2242 ssize_t count = read(file,buffer,quantum);
2249 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2254 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2255 (void) RelinquishUniqueFileResource(filename);
2256 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2282MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2284 assert(image != (
const Image *) NULL);
2285 assert(image->signature == MagickCoreSignature);
2286 if (IsEventLogging() != MagickFalse)
2287 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2288 return(image->blob->exempt);
2313MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2316 *magick_restrict blob_info;
2318 assert(image != (
const Image *) NULL);
2319 assert(image->signature == MagickCoreSignature);
2320 if (IsEventLogging() != MagickFalse)
2321 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2322 blob_info=image->blob;
2323 switch (blob_info->type)
2332 if (blob_info->file_info.file == (FILE *) NULL)
2333 return(MagickFalse);
2334 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2335 return(status == -1 ? MagickFalse : MagickTrue);
2339#if defined(MAGICKCORE_ZLIB_DELEGATE)
2343 if (blob_info->file_info.gzfile == (gzFile) NULL)
2344 return(MagickFalse);
2345 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2346 return(offset < 0 ? MagickFalse : MagickTrue);
2351 case UndefinedStream:
2355 case StandardStream:
2360 return(MagickFalse);
2385MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2387 assert(image != (
const Image *) NULL);
2388 assert(image->signature == MagickCoreSignature);
2389 if (IsEventLogging() != MagickFalse)
2390 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2391 return(image->blob->temporary);
2423MagickExport
unsigned char *MapBlob(
int file,
const MapMode mode,
2424 const MagickOffsetType offset,
const size_t length)
2426#if defined(MAGICKCORE_HAVE_MMAP)
2439#if defined(MAP_ANONYMOUS)
2440 flags|=MAP_ANONYMOUS;
2442 return((
unsigned char *) NULL);
2449 protection=PROT_READ;
2455 protection=PROT_WRITE;
2461 protection=PROT_READ | PROT_WRITE;
2466#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2467 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,offset);
2469 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags |
2470 MAP_HUGETLB,file,offset);
2471 if (map == (
unsigned char *) MAP_FAILED)
2472 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,
2475 if (map == (
unsigned char *) MAP_FAILED)
2476 return((
unsigned char *) NULL);
2483 return((
unsigned char *) NULL);
2512MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
2521 assert(buffer != (
unsigned char *) NULL);
2528 *buffer++=(
unsigned char) c;
2532 *buffer++=(
unsigned char) c;
2562MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
2570 assert(p != (
unsigned char *) NULL);
2577 *p++=(
unsigned char) c;
2613static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
2625 size=MagickMinBufferExtent;
2626 option=GetImageOption(image_info,
"stream:buffer-size");
2627 if (option != (
const char *) NULL)
2628 size=StringToUnsignedLong(option);
2629 status=setvbuf(image->blob->file_info.file,(
char *) NULL,size == 0 ?
2630 _IONBF : _IOFBF,size);
2631 return(status == 0 ? MagickTrue : MagickFalse);
2634#if defined(MAGICKCORE_ZLIB_DELEGATE)
2635static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
2637#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2638 return(gzopen(path,mode));
2646 path_wide=NTCreateWidePath(path);
2647 if (path_wide == (
wchar_t *) NULL)
2648 return((gzFile) NULL);
2649 file=gzopen_w(path_wide,mode);
2650 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
2656MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
2657 Image *image,
const BlobMode mode,ExceptionInfo *exception)
2660 *magick_restrict blob_info;
2663 extension[MagickPathExtent],
2664 filename[MagickPathExtent];
2675 assert(image_info != (ImageInfo *) NULL);
2676 assert(image_info->signature == MagickCoreSignature);
2677 assert(image != (Image *) NULL);
2678 assert(image->signature == MagickCoreSignature);
2679 if (IsEventLogging() != MagickFalse)
2680 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2681 image_info->filename);
2682 blob_info=image->blob;
2683 if (image_info->blob != (
void *) NULL)
2685 if (image_info->stream != (StreamHandler) NULL)
2686 blob_info->stream=(StreamHandler) image_info->stream;
2687 AttachBlob(blob_info,image_info->blob,image_info->length);
2690 (void) DetachBlob(blob_info);
2691 blob_info->mode=mode;
2694 default: type=
"r";
break;
2695 case ReadBlobMode: type=
"r";
break;
2696 case ReadBinaryBlobMode: type=
"rb";
break;
2697 case WriteBlobMode: type=
"w";
break;
2698 case WriteBinaryBlobMode: type=
"w+b";
break;
2699 case AppendBlobMode: type=
"a";
break;
2700 case AppendBinaryBlobMode: type=
"a+b";
break;
2703 blob_info->synchronize=image_info->synchronize;
2704 if (image_info->stream != (StreamHandler) NULL)
2706 blob_info->stream=(StreamHandler) image_info->stream;
2709 blob_info->type=FifoStream;
2717 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2718 rights=ReadPolicyRights;
2720 rights=WritePolicyRights;
2721 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2724 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2725 "NotAuthorized",
"`%s'",filename);
2726 return(MagickFalse);
2728 if ((LocaleCompare(filename,
"-") == 0) ||
2729 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
2731 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
2732#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2733 if (strchr(type,
'b') != (
char *) NULL)
2734 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2736 blob_info->type=StandardStream;
2737 blob_info->exempt=MagickTrue;
2738 return(SetStreamBuffering(image_info,image));
2740 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
2741 (IsGeometry(filename+3) != MagickFalse))
2744 fileMode[MagickPathExtent];
2748 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2749 if (blob_info->file_info.file == (FILE *) NULL)
2751 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2752 return(MagickFalse);
2754#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2755 if (strchr(type,
'b') != (
char *) NULL)
2756 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2758 blob_info->type=FileStream;
2759 blob_info->exempt=MagickTrue;
2760 return(SetStreamBuffering(image_info,image));
2762#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2763 if (*filename ==
'|')
2766 fileMode[MagickPathExtent],
2774 (void) signal(SIGPIPE,SIG_IGN);
2778 sanitize_command=SanitizeString(filename+1);
2779 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2781 sanitize_command=DestroyString(sanitize_command);
2782 if (blob_info->file_info.file == (FILE *) NULL)
2784 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2785 return(MagickFalse);
2787 blob_info->type=PipeStream;
2788 blob_info->exempt=MagickTrue;
2789 return(SetStreamBuffering(image_info,image));
2792 status=GetPathAttributes(filename,&blob_info->properties);
2793#if defined(S_ISFIFO)
2794 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2796 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2797 if (blob_info->file_info.file == (FILE *) NULL)
2799 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2800 return(MagickFalse);
2802 blob_info->type=FileStream;
2803 blob_info->exempt=MagickTrue;
2804 return(SetStreamBuffering(image_info,image));
2807 GetPathComponent(image->filename,ExtensionPath,extension);
2810 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2811 if ((image_info->adjoin == MagickFalse) ||
2812 (strchr(filename,
'%') != (
char *) NULL))
2817 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
2818 image->scene,filename);
2819 if ((LocaleCompare(filename,image->filename) == 0) &&
2820 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2821 (GetNextImageInList(image) != (Image *) NULL)))
2824 path[MagickPathExtent];
2826 GetPathComponent(image->filename,RootPath,path);
2827 if (*extension ==
'\0')
2828 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
2829 path,(
double) image->scene);
2831 (
void) FormatLocaleString(filename,MagickPathExtent,
2832 "%s-%.20g.%s",path,(
double) image->scene,extension);
2834 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2835#if defined(macintosh)
2836 SetApplicationType(filename,image_info->magick,
'8BIM');
2840 if (image_info->file != (FILE *) NULL)
2842 blob_info->file_info.file=image_info->file;
2843 blob_info->type=FileStream;
2844 blob_info->exempt=MagickTrue;
2849 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2850 if (blob_info->file_info.file != (FILE *) NULL)
2858 blob_info->type=FileStream;
2859 (void) fstat(fileno(blob_info->file_info.file),
2860 &blob_info->properties);
2861 (void) SetStreamBuffering(image_info,image);
2862 (void) memset(magick,0,
sizeof(magick));
2863 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2864 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2865#if defined(MAGICKCORE_POSIX_SUPPORT)
2866 (void) fflush(blob_info->file_info.file);
2868 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2869 " read %.20g magic header bytes",(
double) count);
2870#if defined(MAGICKCORE_ZLIB_DELEGATE)
2871 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2872 ((
int) magick[2] == 0x08))
2875 gzfile = gzopen_utf8(filename,
"rb");
2877 if (gzfile != (gzFile) NULL)
2879 if (blob_info->file_info.file != (FILE *) NULL)
2880 (void) fclose(blob_info->file_info.file);
2881 blob_info->file_info.file=(FILE *) NULL;
2882 blob_info->file_info.gzfile=gzfile;
2883 blob_info->type=ZipStream;
2887#if defined(MAGICKCORE_BZLIB_DELEGATE)
2888 if (strncmp((
char *) magick,
"BZh",3) == 0)
2891 *bzfile = BZ2_bzopen(filename,
"r");
2893 if (bzfile != (BZFILE *) NULL)
2895 if (blob_info->file_info.file != (FILE *) NULL)
2896 (void) fclose(blob_info->file_info.file);
2897 blob_info->file_info.file=(FILE *) NULL;
2898 blob_info->file_info.bzfile=bzfile;
2899 blob_info->type=BZipStream;
2903 if (blob_info->type == FileStream)
2914 sans_exception=AcquireExceptionInfo();
2915 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2916 sans_exception=DestroyExceptionInfo(sans_exception);
2917 length=(size_t) blob_info->properties.st_size;
2918 if ((magick_info != (
const MagickInfo *) NULL) &&
2919 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2920 (length > MagickMaxBufferExtent) &&
2921 (AcquireMagickResource(MapResource,length) != MagickFalse))
2926 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
2928 if (blob == (
void *) NULL)
2929 RelinquishMagickResource(MapResource,length);
2935 if (image_info->file != (FILE *) NULL)
2936 blob_info->exempt=MagickFalse;
2939 (void) fclose(blob_info->file_info.file);
2940 blob_info->file_info.file=(FILE *) NULL;
2942 AttachBlob(blob_info,blob,length);
2943 blob_info->mapped=MagickTrue;
2950#if defined(MAGICKCORE_ZLIB_DELEGATE)
2951 if ((LocaleCompare(extension,
"gz") == 0) ||
2952 (LocaleCompare(extension,
"wmz") == 0) ||
2953 (LocaleCompare(extension,
"svgz") == 0))
2955 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
2956 if (blob_info->file_info.gzfile != (gzFile) NULL)
2957 blob_info->type=ZipStream;
2961#if defined(MAGICKCORE_BZLIB_DELEGATE)
2962 if (LocaleCompare(extension,
"bz2") == 0)
2964 if (mode == WriteBinaryBlobMode)
2966 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
2967 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
2968 blob_info->type=BZipStream;
2973 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2974 if (blob_info->file_info.file != (FILE *) NULL)
2976 blob_info->type=FileStream;
2977 (void) SetStreamBuffering(image_info,image);
2980 blob_info->status=0;
2981 blob_info->error_number=0;
2982 if (blob_info->type != UndefinedStream)
2983 blob_info->size=GetBlobSize(image);
2986 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2987 return(MagickFalse);
3026#if defined(__cplusplus) || defined(c_plusplus)
3030static size_t PingStream(
const Image *magick_unused(image),
3031 const void *magick_unused(pixels),
const size_t columns)
3033 magick_unreferenced(image);
3034 magick_unreferenced(pixels);
3039#if defined(__cplusplus) || defined(c_plusplus)
3043MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3044 const size_t length,ExceptionInfo *exception)
3059 assert(image_info != (ImageInfo *) NULL);
3060 assert(image_info->signature == MagickCoreSignature);
3061 assert(exception != (ExceptionInfo *) NULL);
3062 if (IsEventLogging() != MagickFalse)
3063 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3064 image_info->filename);
3065 if ((blob == (
const void *) NULL) || (length == 0))
3067 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3068 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3069 return((Image *) NULL);
3071 ping_info=CloneImageInfo(image_info);
3072 ping_info->blob=(
void *) blob;
3073 ping_info->length=length;
3074 ping_info->ping=MagickTrue;
3075 if (*ping_info->magick ==
'\0')
3076 (void) SetImageInfo(ping_info,0,exception);
3077 magick_info=GetMagickInfo(ping_info->magick,exception);
3078 if (magick_info == (
const MagickInfo *) NULL)
3080 (void) ThrowMagickException(exception,GetMagickModule(),
3081 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3083 ping_info=DestroyImageInfo(ping_info);
3084 return((Image *) NULL);
3086 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3089 filename[MagickPathExtent];
3094 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3095 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3096 ping_info->magick,filename);
3097 image=ReadStream(ping_info,&PingStream,exception);
3098 if (image != (Image *) NULL)
3099 (void) DetachBlob(image->blob);
3100 ping_info=DestroyImageInfo(ping_info);
3106 ping_info->blob=(
void *) NULL;
3107 ping_info->length=0;
3108 *ping_info->filename=
'\0';
3109 status=BlobToFile(ping_info->filename,blob,length,exception);
3110 if (status == MagickFalse)
3112 (void) RelinquishUniqueFileResource(ping_info->filename);
3113 ping_info=DestroyImageInfo(ping_info);
3114 return((Image *) NULL);
3116 clone_info=CloneImageInfo(ping_info);
3117 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3118 ping_info->magick,ping_info->filename);
3119 image=ReadStream(clone_info,&PingStream,exception);
3120 if (image != (Image *) NULL)
3128 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3130 (void) CopyMagickString(images->filename,image_info->filename,
3132 (void) CopyMagickString(images->magick_filename,image_info->filename,
3134 (void) CopyMagickString(images->magick,magick_info->name,
3136 images=GetNextImageInList(images);
3139 clone_info=DestroyImageInfo(clone_info);
3140 (void) RelinquishUniqueFileResource(ping_info->filename);
3141 ping_info=DestroyImageInfo(ping_info);
3176MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
3177 unsigned char *data)
3180 *magick_restrict blob_info;
3191 assert(image != (Image *) NULL);
3192 assert(image->signature == MagickCoreSignature);
3193 assert(image->blob != (BlobInfo *) NULL);
3194 assert(image->blob->type != UndefinedStream);
3197 assert(data != (
void *) NULL);
3198 blob_info=image->blob;
3201 switch (blob_info->type)
3203 case UndefinedStream:
3205 case StandardStream:
3213 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3218 c=getc(blob_info->file_info.file);
3221 *q++=(
unsigned char) c;
3227 c=getc(blob_info->file_info.file);
3230 *q++=(
unsigned char) c;
3236 c=getc(blob_info->file_info.file);
3239 *q++=(
unsigned char) c;
3245 c=getc(blob_info->file_info.file);
3248 *q++=(
unsigned char) c;
3255 if ((count != (ssize_t) length) &&
3256 (ferror(blob_info->file_info.file) != 0))
3257 ThrowBlobException(blob_info);
3262#if defined(MAGICKCORE_ZLIB_DELEGATE)
3273 for (i=0; i < (ssize_t) length; i+=count)
3275 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3276 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3289 c=gzgetc(blob_info->file_info.gzfile);
3292 *q++=(
unsigned char) c;
3298 c=gzgetc(blob_info->file_info.gzfile);
3301 *q++=(
unsigned char) c;
3307 c=gzgetc(blob_info->file_info.gzfile);
3310 *q++=(
unsigned char) c;
3316 c=gzgetc(blob_info->file_info.gzfile);
3319 *q++=(
unsigned char) c;
3327 (void) gzerror(blob_info->file_info.gzfile,&status);
3328 if ((count != (ssize_t) length) && (status != Z_OK))
3329 ThrowBlobException(blob_info);
3330 if (blob_info->eof == MagickFalse)
3331 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3338#if defined(MAGICKCORE_BZLIB_DELEGATE)
3345 for (i=0; i < (ssize_t) length; i+=count)
3347 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3348 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3358 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3359 if ((count != (ssize_t) length) && (status != BZ_OK))
3360 ThrowBlobException(blob_info);
3371 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3373 blob_info->eof=MagickTrue;
3376 p=blob_info->data+blob_info->offset;
3377 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3378 blob_info->length-blob_info->offset);
3379 blob_info->offset+=count;
3380 if (count != (ssize_t) length)
3381 blob_info->eof=MagickTrue;
3382 (void) memcpy(q,p,(
size_t) count);
3411MagickExport
int ReadBlobByte(Image *image)
3414 *magick_restrict blob_info;
3419 assert(image != (Image *) NULL);
3420 assert(image->signature == MagickCoreSignature);
3421 assert(image->blob != (BlobInfo *) NULL);
3422 assert(image->blob->type != UndefinedStream);
3423 blob_info=image->blob;
3424 switch (blob_info->type)
3426 case StandardStream:
3430 c=getc(blob_info->file_info.file);
3433 if (ferror(blob_info->file_info.file) != 0)
3434 ThrowBlobException(blob_info);
3441 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3443 blob_info->eof=MagickTrue;
3446 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
3447 blob_info->offset++;
3458 count=ReadBlob(image,1,buffer);
3491MagickExport
double ReadBlobDouble(Image *image)
3502 quantum.double_value=0.0;
3503 quantum.unsigned_value=ReadBlobLongLong(image);
3504 return(quantum.double_value);
3530MagickExport
float ReadBlobFloat(Image *image)
3541 quantum.float_value=0.0;
3542 quantum.unsigned_value=ReadBlobLong(image);
3543 return(quantum.float_value);
3569MagickExport
unsigned int ReadBlobLong(Image *image)
3583 assert(image != (Image *) NULL);
3584 assert(image->signature == MagickCoreSignature);
3586 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3589 if (image->endian == LSBEndian)
3591 value=(
unsigned int) (*p++);
3592 value|=(
unsigned int) (*p++) << 8;
3593 value|=(
unsigned int) (*p++) << 16;
3594 value|=(
unsigned int) (*p++) << 24;
3597 value=(
unsigned int) (*p++) << 24;
3598 value|=(
unsigned int) (*p++) << 16;
3599 value|=(
unsigned int) (*p++) << 8;
3600 value|=(
unsigned int) (*p++);
3627MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3641 assert(image != (Image *) NULL);
3642 assert(image->signature == MagickCoreSignature);
3644 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3646 return(MagickULLConstant(0));
3647 if (image->endian == LSBEndian)
3649 value=(MagickSizeType) (*p++);
3650 value|=(MagickSizeType) (*p++) << 8;
3651 value|=(MagickSizeType) (*p++) << 16;
3652 value|=(MagickSizeType) (*p++) << 24;
3653 value|=(MagickSizeType) (*p++) << 32;
3654 value|=(MagickSizeType) (*p++) << 40;
3655 value|=(MagickSizeType) (*p++) << 48;
3656 value|=(MagickSizeType) (*p++) << 56;
3659 value=(MagickSizeType) (*p++) << 56;
3660 value|=(MagickSizeType) (*p++) << 48;
3661 value|=(MagickSizeType) (*p++) << 40;
3662 value|=(MagickSizeType) (*p++) << 32;
3663 value|=(MagickSizeType) (*p++) << 24;
3664 value|=(MagickSizeType) (*p++) << 16;
3665 value|=(MagickSizeType) (*p++) << 8;
3666 value|=(MagickSizeType) (*p++);
3693MagickExport
unsigned short ReadBlobShort(Image *image)
3707 assert(image != (Image *) NULL);
3708 assert(image->signature == MagickCoreSignature);
3710 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3712 return((
unsigned short) 0U);
3713 if (image->endian == LSBEndian)
3715 value=(
unsigned short) (*p++);
3716 value|=(
unsigned short) (*p++) << 8;
3719 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3720 value|=(
unsigned short) (*p++);
3747MagickExport
unsigned int ReadBlobLSBLong(Image *image)
3761 assert(image != (Image *) NULL);
3762 assert(image->signature == MagickCoreSignature);
3764 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3767 value=(
unsigned int) (*p++);
3768 value|=(
unsigned int) (*p++) << 8;
3769 value|=(
unsigned int) (*p++) << 16;
3770 value|=(
unsigned int) (*p++) << 24;
3797MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
3808 quantum.unsigned_value=ReadBlobLSBLong(image);
3809 return(quantum.signed_value);
3835MagickExport
unsigned short ReadBlobLSBShort(Image *image)
3849 assert(image != (Image *) NULL);
3850 assert(image->signature == MagickCoreSignature);
3852 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3854 return((
unsigned short) 0U);
3855 value=(
unsigned short) (*p++);
3856 value|=(
unsigned short) (*p++) << 8;
3883MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
3894 quantum.unsigned_value=ReadBlobLSBShort(image);
3895 return(quantum.signed_value);
3921MagickExport
unsigned int ReadBlobMSBLong(Image *image)
3935 assert(image != (Image *) NULL);
3936 assert(image->signature == MagickCoreSignature);
3938 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3941 value=(
unsigned int) (*p++) << 24;
3942 value|=(
unsigned int) (*p++) << 16;
3943 value|=(
unsigned int) (*p++) << 8;
3944 value|=(
unsigned int) (*p++);
3971MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
3985 assert(image != (Image *) NULL);
3986 assert(image->signature == MagickCoreSignature);
3988 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3990 return(MagickULLConstant(0));
3991 value=(MagickSizeType) (*p++) << 56;
3992 value|=(MagickSizeType) (*p++) << 48;
3993 value|=(MagickSizeType) (*p++) << 40;
3994 value|=(MagickSizeType) (*p++) << 32;
3995 value|=(MagickSizeType) (*p++) << 24;
3996 value|=(MagickSizeType) (*p++) << 16;
3997 value|=(MagickSizeType) (*p++) << 8;
3998 value|=(MagickSizeType) (*p++);
4025MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4039 assert(image != (Image *) NULL);
4040 assert(image->signature == MagickCoreSignature);
4042 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4044 return((
unsigned short) 0U);
4045 value=(
unsigned short) ((*p++) << 8);
4046 value|=(
unsigned short) (*p++);
4073MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4084 quantum.unsigned_value=ReadBlobMSBLong(image);
4085 return(quantum.signed_value);
4111MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4122 quantum.unsigned_value=ReadBlobMSBShort(image);
4123 return(quantum.signed_value);
4149MagickExport
signed int ReadBlobSignedLong(Image *image)
4160 quantum.unsigned_value=ReadBlobLong(image);
4161 return(quantum.signed_value);
4187MagickExport
signed short ReadBlobSignedShort(Image *image)
4198 quantum.unsigned_value=ReadBlobShort(image);
4199 return(quantum.signed_value);
4237MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4238 const size_t length,
void *magick_restrict data,ssize_t *count)
4241 *magick_restrict blob_info;
4243 assert(image != (Image *) NULL);
4244 assert(image->signature == MagickCoreSignature);
4245 assert(image->blob != (BlobInfo *) NULL);
4246 assert(image->blob->type != UndefinedStream);
4247 assert(count != (ssize_t *) NULL);
4248 blob_info=image->blob;
4249 if (blob_info->type != BlobStream)
4251 assert(data != NULL);
4252 *count=ReadBlob(image,length,(
unsigned char *) data);
4255 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4258 blob_info->eof=MagickTrue;
4261 data=blob_info->data+blob_info->offset;
4262 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4263 blob_info->length-blob_info->offset);
4264 blob_info->offset+=(*count);
4265 if (*count != (ssize_t) length)
4266 blob_info->eof=MagickTrue;
4295MagickExport
char *ReadBlobString(Image *image,
char *
string)
4298 *magick_restrict blob_info;
4306 assert(image != (Image *) NULL);
4307 assert(image->signature == MagickCoreSignature);
4308 assert(image->blob != (BlobInfo *) NULL);
4309 assert(image->blob->type != UndefinedStream);
4310 if (IsEventLogging() != MagickFalse)
4311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4313 blob_info=image->blob;
4314 switch (blob_info->type)
4316 case UndefinedStream:
4318 case StandardStream:
4321 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4322 if (p == (
char *) NULL)
4324 if (ferror(blob_info->file_info.file) != 0)
4325 ThrowBlobException(blob_info);
4326 return((
char *) NULL);
4333#if defined(MAGICKCORE_ZLIB_DELEGATE)
4334 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4335 if (p == (
char *) NULL)
4338 (void) gzerror(blob_info->file_info.gzfile,&status);
4340 ThrowBlobException(blob_info);
4341 return((
char *) NULL);
4351 c=ReadBlobByte(image);
4354 blob_info->eof=MagickTrue;
4360 }
while (i < (MaxTextExtent-2));
4368 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4371 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4373 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4374 return((
char *) NULL);
4401MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4403 assert(blob != (BlobInfo *) NULL);
4404 assert(blob->signature == MagickCoreSignature);
4405 if (IsEventLogging() != MagickFalse)
4406 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4407 LockSemaphoreInfo(blob->semaphore);
4408 blob->reference_count++;
4409 UnlockSemaphoreInfo(blob->semaphore);
4446MagickExport MagickOffsetType SeekBlob(Image *image,
4447 const MagickOffsetType offset,
const int whence)
4450 *magick_restrict blob_info;
4452 assert(image != (Image *) NULL);
4453 assert(image->signature == MagickCoreSignature);
4454 assert(image->blob != (BlobInfo *) NULL);
4455 assert(image->blob->type != UndefinedStream);
4456 if (IsEventLogging() != MagickFalse)
4457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4458 blob_info=image->blob;
4459 switch (blob_info->type)
4461 case UndefinedStream:
4463 case StandardStream:
4468 if ((offset < 0) && (whence == SEEK_SET))
4470 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4472 blob_info->offset=TellBlob(image);
4477#if defined(MAGICKCORE_ZLIB_DELEGATE)
4478 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4481 blob_info->offset=TellBlob(image);
4497 blob_info->offset=offset;
4502 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4503 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4508 if ((blob_info->offset+offset) < 0)
4510 blob_info->offset+=offset;
4515 if (((MagickOffsetType) blob_info->length+offset) < 0)
4517 blob_info->offset=blob_info->length+offset;
4521 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4523 blob_info->eof=MagickFalse;
4529 return(blob_info->offset);
4557MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
4559 assert(image != (
const Image *) NULL);
4560 assert(image->signature == MagickCoreSignature);
4561 if (IsEventLogging() != MagickFalse)
4562 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4563 image->blob->exempt=exempt;
4592MagickExport MagickBooleanType SetBlobExtent(Image *image,
4593 const MagickSizeType extent)
4596 *magick_restrict blob_info;
4598 assert(image != (Image *) NULL);
4599 assert(image->signature == MagickCoreSignature);
4600 assert(image->blob != (BlobInfo *) NULL);
4601 assert(image->blob->type != UndefinedStream);
4602 if (IsEventLogging() != MagickFalse)
4603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4604 blob_info=image->blob;
4605 switch (blob_info->type)
4607 case UndefinedStream:
4609 case StandardStream:
4610 return(MagickFalse);
4619 if (extent != (MagickSizeType) ((off_t) extent))
4620 return(MagickFalse);
4621 offset=SeekBlob(image,0,SEEK_END);
4623 return(MagickFalse);
4624 if ((MagickSizeType) offset >= extent)
4626 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4629 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4630 blob_info->file_info.file);
4631#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4632 if (blob_info->synchronize != MagickFalse)
4637 file=fileno(blob_info->file_info.file);
4638 if ((file == -1) || (offset < 0))
4639 return(MagickFalse);
4640 (void) posix_fallocate(file,offset,extent-offset);
4643 offset=SeekBlob(image,offset,SEEK_SET);
4645 return(MagickFalse);
4650 return(MagickFalse);
4652 return(MagickFalse);
4654 return(MagickFalse);
4657 if (extent != (MagickSizeType) ((
size_t) extent))
4658 return(MagickFalse);
4659 if (blob_info->mapped != MagickFalse)
4667 (void) UnmapBlob(blob_info->data,blob_info->length);
4668 RelinquishMagickResource(MapResource,blob_info->length);
4669 if (extent != (MagickSizeType) ((off_t) extent))
4670 return(MagickFalse);
4671 offset=SeekBlob(image,0,SEEK_END);
4673 return(MagickFalse);
4674 if ((MagickSizeType) offset >= extent)
4676 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4677 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4678 blob_info->file_info.file);
4679#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4680 if (blob_info->synchronize != MagickFalse)
4685 file=fileno(blob_info->file_info.file);
4686 if ((file == -1) || (offset < 0))
4687 return(MagickFalse);
4688 (void) posix_fallocate(file,offset,extent-offset);
4691 offset=SeekBlob(image,offset,SEEK_SET);
4693 return(MagickFalse);
4694 (void) AcquireMagickResource(MapResource,extent);
4695 blob_info->data=(
unsigned char*) MapBlob(fileno(
4696 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
4697 blob_info->extent=(size_t) extent;
4698 blob_info->length=(size_t) extent;
4699 (void) SyncBlob(image);
4702 blob_info->extent=(size_t) extent;
4703 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4704 blob_info->extent+1,
sizeof(*blob_info->data));
4705 (void) SyncBlob(image);
4706 if (blob_info->data == (
unsigned char *) NULL)
4708 (void) DetachBlob(blob_info);
4709 return(MagickFalse);
4741static int SyncBlob(
const Image *image)
4744 *magick_restrict blob_info;
4749 assert(image != (Image *) NULL);
4750 assert(image->signature == MagickCoreSignature);
4751 assert(image->blob != (BlobInfo *) NULL);
4752 if (IsEventLogging() != MagickFalse)
4753 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4754 if (EOFBlob(image) != 0)
4756 blob_info=image->blob;
4758 switch (blob_info->type)
4760 case UndefinedStream:
4761 case StandardStream:
4766 status=fflush(blob_info->file_info.file);
4771#if defined(MAGICKCORE_ZLIB_DELEGATE)
4772 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4778#if defined(MAGICKCORE_BZLIB_DELEGATE)
4779 status=BZ2_bzflush(blob_info->file_info.bzfile);
4813MagickExport MagickOffsetType TellBlob(
const Image *image)
4816 *magick_restrict blob_info;
4821 assert(image != (Image *) NULL);
4822 assert(image->signature == MagickCoreSignature);
4823 assert(image->blob != (BlobInfo *) NULL);
4824 assert(image->blob->type != UndefinedStream);
4825 if (IsEventLogging() != MagickFalse)
4826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4827 blob_info=image->blob;
4829 switch (blob_info->type)
4831 case UndefinedStream:
4832 case StandardStream:
4836 offset=ftell(blob_info->file_info.file);
4843#if defined(MAGICKCORE_ZLIB_DELEGATE)
4844 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4854 offset=blob_info->offset;
4886MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
4888#if defined(MAGICKCORE_HAVE_MMAP)
4892 status=munmap(map,length);
4893 return(status == -1 ? MagickFalse : MagickTrue);
4897 return(MagickFalse);
4930MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
4931 const unsigned char *data)
4934 *magick_restrict blob_info;
4948 assert(image != (Image *) NULL);
4949 assert(image->signature == MagickCoreSignature);
4950 assert(image->blob != (BlobInfo *) NULL);
4951 assert(image->blob->type != UndefinedStream);
4954 assert(data != (
const unsigned char *) NULL);
4955 blob_info=image->blob;
4957 p=(
const unsigned char *) data;
4958 q=(
unsigned char *) data;
4959 switch (blob_info->type)
4961 case UndefinedStream:
4963 case StandardStream:
4971 count=(ssize_t) fwrite((
const char *) data,1,length,
4972 blob_info->file_info.file);
4977 c=putc((
int) *p++,blob_info->file_info.file);
4985 c=putc((
int) *p++,blob_info->file_info.file);
4993 c=putc((
int) *p++,blob_info->file_info.file);
5001 c=putc((
int) *p++,blob_info->file_info.file);
5010 if ((count != (ssize_t) length) &&
5011 (ferror(blob_info->file_info.file) != 0))
5012 ThrowBlobException(blob_info);
5017#if defined(MAGICKCORE_ZLIB_DELEGATE)
5028 for (i=0; i < (ssize_t) length; i+=count)
5030 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5031 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5044 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5052 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5060 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5068 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5078 (void) gzerror(blob_info->file_info.gzfile,&status);
5079 if ((count != (ssize_t) length) && (status != Z_OK))
5080 ThrowBlobException(blob_info);
5086#if defined(MAGICKCORE_BZLIB_DELEGATE)
5093 for (i=0; i < (ssize_t) length; i+=count)
5095 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5096 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5106 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5107 if ((count != (ssize_t) length) && (status != BZ_OK))
5108 ThrowBlobException(blob_info);
5114 count=(ssize_t) blob_info->stream(image,data,length);
5122 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5123 if (extent >= blob_info->extent)
5125 extent+=blob_info->quantum+length;
5126 blob_info->quantum<<=1;
5127 if (SetBlobExtent(image,extent) == MagickFalse)
5130 q=blob_info->data+blob_info->offset;
5131 (void) memcpy(q,p,length);
5132 blob_info->offset+=length;
5133 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5134 blob_info->length=(size_t) blob_info->offset;
5135 count=(ssize_t) length;
5138 if (count != (ssize_t) length)
5139 ThrowBlobException(blob_info);
5168MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
5171 *magick_restrict blob_info;
5176 assert(image != (Image *) NULL);
5177 assert(image->signature == MagickCoreSignature);
5178 assert(image->blob != (BlobInfo *) NULL);
5179 assert(image->blob->type != UndefinedStream);
5180 blob_info=image->blob;
5182 switch (blob_info->type)
5184 case StandardStream:
5191 c=putc((
int) value,blob_info->file_info.file);
5194 if (ferror(blob_info->file_info.file) != 0)
5195 ThrowBlobException(blob_info);
5203 count=WriteBlobStream(image,1,&value);
5235MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
5246 quantum.unsigned_value=0U;
5247 quantum.float_value=value;
5248 return(WriteBlobLong(image,quantum.unsigned_value));
5276MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
5281 assert(image != (Image *) NULL);
5282 assert(image->signature == MagickCoreSignature);
5283 if (image->endian == LSBEndian)
5285 buffer[0]=(
unsigned char) value;
5286 buffer[1]=(
unsigned char) (value >> 8);
5287 buffer[2]=(
unsigned char) (value >> 16);
5288 buffer[3]=(
unsigned char) (value >> 24);
5289 return(WriteBlobStream(image,4,buffer));
5291 buffer[0]=(
unsigned char) (value >> 24);
5292 buffer[1]=(
unsigned char) (value >> 16);
5293 buffer[2]=(
unsigned char) (value >> 8);
5294 buffer[3]=(
unsigned char) value;
5295 return(WriteBlobStream(image,4,buffer));
5323MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
5328 assert(image != (Image *) NULL);
5329 assert(image->signature == MagickCoreSignature);
5330 if (image->endian == LSBEndian)
5332 buffer[0]=(
unsigned char) value;
5333 buffer[1]=(
unsigned char) (value >> 8);
5334 return(WriteBlobStream(image,2,buffer));
5336 buffer[0]=(
unsigned char) (value >> 8);
5337 buffer[1]=(
unsigned char) value;
5338 return(WriteBlobStream(image,2,buffer));
5366MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
5371 assert(image != (Image *) NULL);
5372 assert(image->signature == MagickCoreSignature);
5373 buffer[0]=(
unsigned char) value;
5374 buffer[1]=(
unsigned char) (value >> 8);
5375 buffer[2]=(
unsigned char) (value >> 16);
5376 buffer[3]=(
unsigned char) (value >> 24);
5377 return(WriteBlobStream(image,4,buffer));
5405MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
5410 assert(image != (Image *) NULL);
5411 assert(image->signature == MagickCoreSignature);
5412 buffer[0]=(
unsigned char) value;
5413 buffer[1]=(
unsigned char) (value >> 8);
5414 return(WriteBlobStream(image,2,buffer));
5442MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
5456 assert(image != (Image *) NULL);
5457 assert(image->signature == MagickCoreSignature);
5458 quantum.signed_value=value;
5459 buffer[0]=(
unsigned char) quantum.unsigned_value;
5460 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5461 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5462 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5463 return(WriteBlobStream(image,4,buffer));
5491MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5492 const signed short value)
5506 assert(image != (Image *) NULL);
5507 assert(image->signature == MagickCoreSignature);
5508 quantum.signed_value=value;
5509 buffer[0]=(
unsigned char) quantum.unsigned_value;
5510 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5511 return(WriteBlobStream(image,2,buffer));
5539MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
5544 assert(image != (Image *) NULL);
5545 assert(image->signature == MagickCoreSignature);
5546 buffer[0]=(
unsigned char) (value >> 24);
5547 buffer[1]=(
unsigned char) (value >> 16);
5548 buffer[2]=(
unsigned char) (value >> 8);
5549 buffer[3]=(
unsigned char) value;
5550 return(WriteBlobStream(image,4,buffer));
5578MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5579 const MagickSizeType value)
5584 assert(image != (Image *) NULL);
5585 assert(image->signature == MagickCoreSignature);
5586 buffer[0]=(
unsigned char) (value >> 56);
5587 buffer[1]=(
unsigned char) (value >> 48);
5588 buffer[2]=(
unsigned char) (value >> 40);
5589 buffer[3]=(
unsigned char) (value >> 32);
5590 buffer[4]=(
unsigned char) (value >> 24);
5591 buffer[5]=(
unsigned char) (value >> 16);
5592 buffer[6]=(
unsigned char) (value >> 8);
5593 buffer[7]=(
unsigned char) value;
5594 return(WriteBlobStream(image,8,buffer));
5622MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
5627 assert(image != (Image *) NULL);
5628 assert(image->signature == MagickCoreSignature);
5629 buffer[0]=(
unsigned char) (value >> 8);
5630 buffer[1]=(
unsigned char) value;
5631 return(WriteBlobStream(image,2,buffer));
5659MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,
const signed int value)
5673 assert(image != (Image *) NULL);
5674 assert(image->signature == MagickCoreSignature);
5675 quantum.signed_value=value;
5676 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5677 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5678 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5679 buffer[3]=(
unsigned char) quantum.unsigned_value;
5680 return(WriteBlobStream(image,4,buffer));
5708MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5709 const signed short value)
5723 assert(image != (Image *) NULL);
5724 assert(image->signature == MagickCoreSignature);
5725 quantum.signed_value=value;
5726 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5727 buffer[1]=(
unsigned char) quantum.unsigned_value;
5728 return(WriteBlobStream(image,2,buffer));
5756MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
5758 assert(image != (Image *) NULL);
5759 assert(image->signature == MagickCoreSignature);
5760 assert(
string != (
const char *) NULL);
5761 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));