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/policy-private.h"
67#include "magick/resource_.h"
68#include "magick/semaphore.h"
69#include "magick/string_.h"
70#include "magick/string-private.h"
71#include "magick/timer-private.h"
72#include "magick/token.h"
73#include "magick/utility.h"
74#include "magick/utility-private.h"
75#if defined(MAGICKCORE_ZLIB_DELEGATE)
78#if defined(MAGICKCORE_BZLIB_DELEGATE)
85#define IsPathAuthorized(rights,filename) \
86 ((IsRightsAuthorized(PathPolicyDomain,rights,filename) != MagickFalse) && \
87 ((IsRightsAuthorizedByName(SystemPolicyDomain,"symlink",rights,"follow") != MagickFalse) || \
88 (is_symlink_utf8(filename) == MagickFalse)))
89#define MagickMaxBlobExtent (8*8192)
90#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
91# define MAP_ANONYMOUS MAP_ANON
93#if !defined(MAP_FAILED)
94#define MAP_FAILED ((void *) -1)
98#define _O_BINARY O_BINARY
100#if defined(MAGICKCORE_WINDOWS_SUPPORT)
102# define fsync _commit
105# define MAGICKCORE_HAVE_MMAP 1
106# define mmap(address,length,protection,access,file,offset) \
107 NTMapMemory(address,length,protection,access,file,offset)
110# define munmap(address,length) NTUnmapMemory(address,length)
113# define pclose _pclose
128#if defined(MAGICKCORE_ZLIB_DELEGATE)
133#if defined(MAGICKCORE_BZLIB_DELEGATE)
203 SyncBlob(
const Image *);
232MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
235 assert(blob_info != (BlobInfo *) NULL);
236 if (IsEventLogging() != MagickFalse)
237 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
238 blob_info->length=length;
239 blob_info->extent=length;
240 blob_info->quantum=(size_t) MagickMaxBlobExtent;
242 blob_info->type=BlobStream;
243 blob_info->file_info.file=(FILE *) NULL;
244 blob_info->data=(
unsigned char *) blob;
245 blob_info->mapped=MagickFalse;
279MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
280 const size_t length,ExceptionInfo *exception)
291 assert(filename != (
const char *) NULL);
292 assert(blob != (
const void *) NULL);
293 if (IsEventLogging() != MagickFalse)
294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
295 if (*filename ==
'\0')
296 file=AcquireUniqueFileResource(filename);
298 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
301 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
304 for (i=0; i < length; i+=(size_t) count)
306 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
307 MagickMaxBufferExtent));
315 file=close_utf8(file);
316 if ((file == -1) || (i < length))
318 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
355MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
356 const size_t length,ExceptionInfo *exception)
371 assert(image_info != (ImageInfo *) NULL);
372 assert(image_info->signature == MagickCoreSignature);
373 assert(exception != (ExceptionInfo *) NULL);
374 if (IsEventLogging() != MagickFalse)
375 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
376 image_info->filename);
377 if ((blob == (
const void *) NULL) || (length == 0))
379 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
380 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
381 return((Image *) NULL);
383 blob_info=CloneImageInfo(image_info);
384 blob_info->blob=(
void *) blob;
385 blob_info->length=length;
386 if (*blob_info->magick ==
'\0')
387 (void) SetImageInfo(blob_info,0,exception);
388 magick_info=GetMagickInfo(blob_info->magick,exception);
389 if (magick_info == (
const MagickInfo *) NULL)
391 (void) ThrowMagickException(exception,GetMagickModule(),
392 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
394 blob_info=DestroyImageInfo(blob_info);
395 return((Image *) NULL);
397 if (GetMagickBlobSupport(magick_info) != MagickFalse)
400 filename[MagickPathExtent];
405 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
406 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
407 blob_info->magick,filename);
408 image=ReadImage(blob_info,exception);
409 if (image != (Image *) NULL)
410 (void) DetachBlob(image->blob);
411 blob_info=DestroyImageInfo(blob_info);
417 blob_info->blob=(
void *) NULL;
419 *blob_info->filename=
'\0';
420 status=BlobToFile(blob_info->filename,blob,length,exception);
421 if (status == MagickFalse)
423 (void) RelinquishUniqueFileResource(blob_info->filename);
424 blob_info=DestroyImageInfo(blob_info);
425 return((Image *) NULL);
427 clone_info=CloneImageInfo(blob_info);
428 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
429 blob_info->magick,blob_info->filename);
430 image=ReadImage(clone_info,exception);
431 if (image != (Image *) NULL)
439 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
441 (void) CopyMagickString(images->filename,image_info->filename,
443 (void) CopyMagickString(images->magick_filename,image_info->filename,
445 (void) CopyMagickString(images->magick,magick_info->name,
447 images=GetNextImageInList(images);
450 clone_info=DestroyImageInfo(clone_info);
451 (void) RelinquishUniqueFileResource(blob_info->filename);
452 blob_info=DestroyImageInfo(blob_info);
479MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
487 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
488 GetBlobInfo(clone_info);
489 if (blob_info == (BlobInfo *) NULL)
491 semaphore=clone_info->semaphore;
492 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
493 if (blob_info->mapped != MagickFalse)
494 (void) AcquireMagickResource(MapResource,blob_info->length);
495 clone_info->semaphore=semaphore;
496 LockSemaphoreInfo(clone_info->semaphore);
497 clone_info->reference_count=1;
498 UnlockSemaphoreInfo(clone_info->semaphore);
525static inline void ThrowBlobException(BlobInfo *blob_info)
527 if ((blob_info->status == 0) && (errno != 0))
528 blob_info->error_number=errno;
529 blob_info->status=(-1);
532MagickExport MagickBooleanType CloseBlob(Image *image)
535 *magick_restrict blob_info;
543 assert(image != (Image *) NULL);
544 assert(image->signature == MagickCoreSignature);
545 if (IsEventLogging() != MagickFalse)
546 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
547 blob_info=image->blob;
548 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
550 (void) SyncBlob(image);
551 status=blob_info->status;
552 switch (blob_info->type)
554 case UndefinedStream:
560 if (blob_info->synchronize != MagickFalse)
562 status=fflush(blob_info->file_info.file);
564 ThrowBlobException(blob_info);
565 status=fsync(fileno(blob_info->file_info.file));
567 ThrowBlobException(blob_info);
569 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
570 ThrowBlobException(blob_info);
575#if defined(MAGICKCORE_ZLIB_DELEGATE)
577 (void) gzerror(blob_info->file_info.gzfile,&status);
579 ThrowBlobException(blob_info);
585#if defined(MAGICKCORE_BZLIB_DELEGATE)
587 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
589 ThrowBlobException(blob_info);
597 if (blob_info->file_info.file != (FILE *) NULL)
599 if (blob_info->synchronize != MagickFalse)
601 status=fflush(blob_info->file_info.file);
603 ThrowBlobException(blob_info);
604 status=fsync(fileno(blob_info->file_info.file));
606 ThrowBlobException(blob_info);
608 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
609 ThrowBlobException(blob_info);
614 blob_info->size=GetBlobSize(image);
615 image->extent=blob_info->size;
616 blob_info->eof=MagickFalse;
618 blob_info->mode=UndefinedBlobMode;
619 if (blob_info->exempt != MagickFalse)
621 blob_info->type=UndefinedStream;
622 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
624 switch (blob_info->type)
626 case UndefinedStream:
631 if (blob_info->file_info.file != (FILE *) NULL)
633 status=fclose(blob_info->file_info.file);
635 ThrowBlobException(blob_info);
641#if defined(MAGICKCORE_HAVE_PCLOSE)
642 status=pclose(blob_info->file_info.file);
644 ThrowBlobException(blob_info);
650#if defined(MAGICKCORE_ZLIB_DELEGATE)
651 status=gzclose(blob_info->file_info.gzfile);
653 ThrowBlobException(blob_info);
659#if defined(MAGICKCORE_BZLIB_DELEGATE)
660 BZ2_bzclose(blob_info->file_info.bzfile);
668 if (blob_info->file_info.file != (FILE *) NULL)
670 status=fclose(blob_info->file_info.file);
672 ThrowBlobException(blob_info);
677 (void) DetachBlob(blob_info);
678 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
703MagickExport
void DestroyBlob(Image *image)
706 *magick_restrict blob_info;
711 assert(image != (Image *) NULL);
712 assert(image->signature == MagickCoreSignature);
713 assert(image->blob != (BlobInfo *) NULL);
714 assert(image->blob->signature == MagickCoreSignature);
715 if (IsEventLogging() != MagickFalse)
716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
717 blob_info=image->blob;
719 LockSemaphoreInfo(blob_info->semaphore);
720 blob_info->reference_count--;
721 assert(blob_info->reference_count >= 0);
722 if (blob_info->reference_count == 0)
724 UnlockSemaphoreInfo(blob_info->semaphore);
725 if (destroy == MagickFalse)
727 image->blob=(BlobInfo *) NULL;
730 (void) CloseBlob(image);
731 if (blob_info->mapped != MagickFalse)
733 (void) UnmapBlob(blob_info->data,blob_info->length);
734 RelinquishMagickResource(MapResource,blob_info->length);
737 DestroySemaphoreInfo(&blob_info->semaphore);
738 blob_info->signature=(~MagickCoreSignature);
739 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
764MagickExport
unsigned char *DetachBlob(BlobInfo *blob_info)
769 assert(blob_info != (BlobInfo *) NULL);
770 if (IsEventLogging() != MagickFalse)
771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
772 if (blob_info->mapped != MagickFalse)
774 (void) UnmapBlob(blob_info->data,blob_info->length);
775 blob_info->data=NULL;
776 RelinquishMagickResource(MapResource,blob_info->length);
778 blob_info->mapped=MagickFalse;
781 blob_info->mode=UndefinedBlobMode;
782 blob_info->eof=MagickFalse;
784 blob_info->exempt=MagickFalse;
785 blob_info->type=UndefinedStream;
786 blob_info->file_info.file=(FILE *) NULL;
787 data=blob_info->data;
788 blob_info->data=(
unsigned char *) NULL;
789 blob_info->stream=(StreamHandler) NULL;
817MagickPrivate
void DisassociateBlob(Image *image)
820 *magick_restrict blob_info,
826 assert(image != (Image *) NULL);
827 assert(image->signature == MagickCoreSignature);
828 assert(image->blob != (BlobInfo *) NULL);
829 assert(image->blob->signature == MagickCoreSignature);
830 if (IsEventLogging() != MagickFalse)
831 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
832 blob_info=image->blob;
834 LockSemaphoreInfo(blob_info->semaphore);
835 assert(blob_info->reference_count >= 0);
836 if (blob_info->reference_count > 1)
838 UnlockSemaphoreInfo(blob_info->semaphore);
839 if (clone == MagickFalse)
841 clone_info=CloneBlobInfo(blob_info);
843 image->blob=clone_info;
871MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
872 const MagickSizeType length)
884 buffer[MagickMinBufferExtent >> 1];
886 assert(image != (Image *) NULL);
887 assert(image->signature == MagickCoreSignature);
888 if (length != (MagickSizeType) ((MagickOffsetType) length))
891 for (i=0; i < length; i+=(MagickSizeType) count)
893 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
894 (void) ReadBlobStream(image,quantum,buffer,&count);
902 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
929MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
931 assert(image != (Image *) NULL);
932 assert(image->signature == MagickCoreSignature);
933 assert(duplicate != (Image *) NULL);
934 assert(duplicate->signature == MagickCoreSignature);
935 if (IsEventLogging() != MagickFalse)
936 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
938 image->blob=ReferenceBlob(duplicate->blob);
964MagickExport
int EOFBlob(
const Image *image)
967 *magick_restrict blob_info;
969 assert(image != (Image *) NULL);
970 assert(image->signature == MagickCoreSignature);
971 assert(image->blob != (BlobInfo *) NULL);
972 assert(image->blob->type != UndefinedStream);
973 if (IsEventLogging() != MagickFalse)
974 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
975 blob_info=image->blob;
976 switch (blob_info->type)
978 case UndefinedStream:
984 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
990#if defined(MAGICKCORE_ZLIB_DELEGATE)
991 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
998#if defined(MAGICKCORE_BZLIB_DELEGATE)
1003 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1004 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1010 blob_info->eof=MagickFalse;
1016 return((
int) blob_info->eof);
1042MagickExport
int ErrorBlob(
const Image *image)
1045 *magick_restrict blob_info;
1047 assert(image != (Image *) NULL);
1048 assert(image->signature == MagickCoreSignature);
1049 assert(image->blob != (BlobInfo *) NULL);
1050 assert(image->blob->type != UndefinedStream);
1051 if (IsEventLogging() != MagickFalse)
1052 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1053 blob_info=image->blob;
1054 switch (blob_info->type)
1056 case UndefinedStream:
1057 case StandardStream:
1062 blob_info->error=ferror(blob_info->file_info.file);
1067#if defined(MAGICKCORE_ZLIB_DELEGATE)
1068 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1074#if defined(MAGICKCORE_BZLIB_DELEGATE)
1075 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1087 return(blob_info->error);
1125MagickExport
unsigned char *FileToBlob(
const char *filename,
const size_t extent,
1126 size_t *length,ExceptionInfo *exception)
1152 assert(filename != (
const char *) NULL);
1153 assert(exception != (ExceptionInfo *) NULL);
1154 assert(exception->signature == MagickCoreSignature);
1155 if (IsEventLogging() != MagickFalse)
1156 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1158 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1159 ThrowPolicyException(filename,(
unsigned char *) NULL);
1161 if (LocaleCompare(filename,
"-") != 0)
1164 flags = O_RDONLY | O_BINARY;
1166 status=GetPathAttributes(filename,&attributes);
1167 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1169 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1172 file=open_utf8(filename,flags,0);
1176 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1177 return((
unsigned char *) NULL);
1179 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1181 file=close_utf8(file)-1;
1182 ThrowPolicyException(filename,(
unsigned char *) NULL);
1184 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1186 if ((file == fileno(stdin)) || (offset < 0) ||
1187 (offset != (MagickOffsetType) ((ssize_t) offset)))
1198 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1199 quantum=(size_t) MagickMaxBufferExtent;
1200 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1201 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1202 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1203 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1205 count=read(file,blob+i,quantum);
1212 if (~((
size_t) i) < (count+quantum+1))
1214 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1217 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+count+quantum+1,
1219 if ((
size_t) (i+count) >= extent)
1222 if (LocaleCompare(filename,
"-") != 0)
1223 file=close_utf8(file);
1224 if (blob == (
unsigned char *) NULL)
1226 (void) ThrowMagickException(exception,GetMagickModule(),
1227 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1228 return((
unsigned char *) NULL);
1232 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1233 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1234 return((
unsigned char *) NULL);
1236 *length=(size_t) MagickMin(i+count,extent);
1240 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1241 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1242 blob=(
unsigned char *) NULL;
1243 if (~(*length) >= (MagickPathExtent-1))
1244 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1246 if (blob == (
unsigned char *) NULL)
1248 file=close_utf8(file);
1249 (void) ThrowMagickException(exception,GetMagickModule(),
1250 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1251 return((
unsigned char *) NULL);
1253 map=MapBlob(file,ReadMode,0,*length);
1254 if (map != (
unsigned char *) NULL)
1256 (void) memcpy(blob,map,*length);
1257 (void) UnmapBlob(map,*length);
1261 (void) lseek(file,0,SEEK_SET);
1262 for (i=0; i < *length; i+=count)
1264 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1265 MagickMaxBufferExtent));
1275 file=close_utf8(file)-1;
1276 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1277 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1278 return((
unsigned char *) NULL);
1282 if (LocaleCompare(filename,
"-") != 0)
1283 file=close_utf8(file);
1286 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1287 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1317static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1318 const unsigned char *magick_restrict data)
1321 *magick_restrict blob_info;
1329 assert(image->blob != (BlobInfo *) NULL);
1330 assert(image->blob->type != UndefinedStream);
1331 assert(data != (
void *) NULL);
1332 blob_info=image->blob;
1333 if (blob_info->type != BlobStream)
1334 return(WriteBlob(image,length,data));
1335 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1340 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1341 if (extent >= blob_info->extent)
1343 extent+=blob_info->quantum+length;
1344 blob_info->quantum<<=1;
1345 if (SetBlobExtent(image,extent) == MagickFalse)
1348 q=blob_info->data+blob_info->offset;
1349 (void) memcpy(q,data,length);
1350 blob_info->offset+=length;
1351 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1352 blob_info->length=(size_t) blob_info->offset;
1353 return((ssize_t) length);
1356MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename)
1377 assert(image != (
const Image *) NULL);
1378 assert(image->signature == MagickCoreSignature);
1379 assert(filename != (
const char *) NULL);
1380 if (IsEventLogging() != MagickFalse)
1381 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1382 exception=(&image->exception);
1383 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1384 ThrowPolicyException(filename,MagickFalse);
1386 if (LocaleCompare(filename,
"-") != 0)
1389 flags = O_RDONLY | O_BINARY;
1391 file=open_utf8(filename,flags,0);
1395 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1396 return(MagickFalse);
1398 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1399 ThrowPolicyException(filename,MagickFalse);
1400 quantum=(size_t) MagickMaxBufferExtent;
1401 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1402 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1403 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1404 if (blob == (
unsigned char *) NULL)
1406 file=close_utf8(file);
1407 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1409 return(MagickFalse);
1413 count=read(file,blob,quantum);
1420 length=(size_t) count;
1421 count=WriteBlobStream(image,length,blob);
1422 if (count != (ssize_t) length)
1424 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1428 file=close_utf8(file);
1430 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1431 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1458MagickExport MagickBooleanType GetBlobError(
const Image *image)
1460 assert(image != (
const Image *) NULL);
1461 assert(image->signature == MagickCoreSignature);
1462 if (IsEventLogging() != MagickFalse)
1463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1464 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1465 errno=image->blob->error_number;
1466 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1491MagickExport FILE *GetBlobFileHandle(
const Image *image)
1493 assert(image != (
const Image *) NULL);
1494 assert(image->signature == MagickCoreSignature);
1495 return(image->blob->file_info.file);
1520MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1522 assert(blob_info != (BlobInfo *) NULL);
1523 (void) memset(blob_info,0,
sizeof(*blob_info));
1524 blob_info->type=UndefinedStream;
1525 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1526 blob_info->properties.st_mtime=GetMagickTime();
1527 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1528 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1529 blob_info->reference_count=1;
1530 blob_info->semaphore=AllocateSemaphoreInfo();
1531 blob_info->signature=MagickCoreSignature;
1556MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1558 assert(image != (Image *) NULL);
1559 assert(image->signature == MagickCoreSignature);
1560 if (IsEventLogging() != MagickFalse)
1561 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1562 return(&image->blob->properties);
1588MagickExport MagickSizeType GetBlobSize(
const Image *image)
1591 *magick_restrict blob_info;
1596 assert(image != (Image *) NULL);
1597 assert(image->signature == MagickCoreSignature);
1598 assert(image->blob != (BlobInfo *) NULL);
1599 if (IsEventLogging() != MagickFalse)
1600 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1601 blob_info=image->blob;
1603 switch (blob_info->type)
1605 case UndefinedStream:
1606 case StandardStream:
1608 extent=blob_info->size;
1616 extent=(MagickSizeType) blob_info->properties.st_size;
1618 extent=blob_info->size;
1619 file_descriptor=fileno(blob_info->file_info.file);
1620 if (file_descriptor == -1)
1622 if (fstat(file_descriptor,&blob_info->properties) == 0)
1623 extent=(MagickSizeType) blob_info->properties.st_size;
1628 extent=blob_info->size;
1637 status=GetPathAttributes(image->filename,&blob_info->properties);
1638 if (status != MagickFalse)
1639 extent=(MagickSizeType) blob_info->properties.st_size;
1646 extent=(MagickSizeType) blob_info->length;
1675MagickExport
unsigned char *GetBlobStreamData(
const Image *image)
1677 assert(image != (
const Image *) NULL);
1678 assert(image->signature == MagickCoreSignature);
1679 return(image->blob->data);
1704MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1706 assert(image != (
const Image *) NULL);
1707 assert(image->signature == MagickCoreSignature);
1708 if (IsEventLogging() != MagickFalse)
1709 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1710 return(image->blob->stream);
1746MagickExport
unsigned char *ImageToBlob(
const ImageInfo *image_info,
1747 Image *image,
size_t *length,ExceptionInfo *exception)
1761 assert(image_info != (
const ImageInfo *) NULL);
1762 assert(image_info->signature == MagickCoreSignature);
1763 assert(image != (Image *) NULL);
1764 assert(image->signature == MagickCoreSignature);
1765 assert(exception != (ExceptionInfo *) NULL);
1766 assert(exception->signature == MagickCoreSignature);
1767 if (IsEventLogging() != MagickFalse)
1768 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1769 image_info->filename);
1771 blob=(
unsigned char *) NULL;
1772 blob_info=CloneImageInfo(image_info);
1773 blob_info->adjoin=MagickFalse;
1774 (void) SetImageInfo(blob_info,1,exception);
1775 if (*blob_info->magick !=
'\0')
1776 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
1777 magick_info=GetMagickInfo(image->magick,exception);
1778 if (magick_info == (
const MagickInfo *) NULL)
1780 (void) ThrowMagickException(exception,GetMagickModule(),
1781 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
1783 blob_info=DestroyImageInfo(blob_info);
1786 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
1787 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1792 blob_info->length=0;
1793 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1794 sizeof(
unsigned char));
1795 if (blob_info->blob == NULL)
1796 (void) ThrowMagickException(exception,GetMagickModule(),
1797 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1800 (void) CloseBlob(image);
1801 image->blob->exempt=MagickTrue;
1802 *image->filename=
'\0';
1803 status=WriteImage(blob_info,image);
1804 InheritException(exception,&image->exception);
1805 *length=image->blob->length;
1806 blob=DetachBlob(image->blob);
1807 if (blob != (
void *) NULL)
1809 if (status == MagickFalse)
1810 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1812 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
1813 sizeof(
unsigned char));
1815 else if (status == MagickFalse)
1816 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
1822 unique[MagickPathExtent];
1830 file=AcquireUniqueFileResource(unique);
1833 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
1834 image_info->filename);
1838 blob_info->file=fdopen(file,
"wb");
1839 if (blob_info->file != (FILE *) NULL)
1841 (void) FormatLocaleString(image->filename,MagickPathExtent,
1842 "%s:%s",image->magick,unique);
1843 status=WriteImage(blob_info,image);
1844 (void) fclose(blob_info->file);
1845 if (status == MagickFalse)
1846 InheritException(exception,&image->exception);
1848 blob=FileToBlob(unique,SIZE_MAX,length,exception);
1850 (void) RelinquishUniqueFileResource(unique);
1853 blob_info=DestroyImageInfo(blob_info);
1885MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
1886 ExceptionInfo *exception)
1910 assert(image != (Image *) NULL);
1911 assert(image->signature == MagickCoreSignature);
1912 assert(image->blob != (BlobInfo *) NULL);
1913 assert(image->blob->type != UndefinedStream);
1914 assert(filename != (
const char *) NULL);
1915 if (IsEventLogging() != MagickFalse)
1916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1917 if (*filename ==
'\0')
1918 file=AcquireUniqueFileResource(filename);
1920 if (LocaleCompare(filename,
"-") == 0)
1921 file=fileno(stdout);
1923 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
1926 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1927 return(MagickFalse);
1929 quantum=(size_t) MagickMaxBufferExtent;
1930 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1931 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1932 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
1933 if (buffer == (
unsigned char *) NULL)
1935 file=close_utf8(file)-1;
1936 (void) ThrowMagickException(exception,GetMagickModule(),
1937 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
1938 return(MagickFalse);
1941 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1942 for (i=0; count > 0; )
1944 length=(size_t) count;
1945 for (i=0; i < length; i+=count)
1947 count=write(file,p+i,(
size_t) (length-i));
1957 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1959 if (LocaleCompare(filename,
"-") != 0)
1960 file=close_utf8(file);
1961 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
1962 if ((file == -1) || (i < length))
1965 file=close_utf8(file);
1966 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1967 return(MagickFalse);
2007MagickExport
unsigned char *ImagesToBlob(
const ImageInfo *image_info,
2008 Image *images,
size_t *length,ExceptionInfo *exception)
2022 assert(image_info != (
const ImageInfo *) NULL);
2023 assert(image_info->signature == MagickCoreSignature);
2024 assert(images != (Image *) NULL);
2025 assert(images->signature == MagickCoreSignature);
2026 assert(exception != (ExceptionInfo *) NULL);
2027 if (IsEventLogging() != MagickFalse)
2028 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2029 image_info->filename);
2031 blob=(
unsigned char *) NULL;
2032 blob_info=CloneImageInfo(image_info);
2033 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2035 if (*blob_info->magick !=
'\0')
2036 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2037 magick_info=GetMagickInfo(images->magick,exception);
2038 if (magick_info == (
const MagickInfo *) NULL)
2040 (void) ThrowMagickException(exception,GetMagickModule(),
2041 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2043 blob_info=DestroyImageInfo(blob_info);
2046 if (GetMagickAdjoin(magick_info) == MagickFalse)
2048 blob_info=DestroyImageInfo(blob_info);
2049 return(ImageToBlob(image_info,images,length,exception));
2051 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2052 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2057 blob_info->length=0;
2058 blob_info->blob=(
void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2059 sizeof(
unsigned char));
2060 if (blob_info->blob == (
void *) NULL)
2061 (void) ThrowMagickException(exception,GetMagickModule(),
2062 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2065 (void) CloseBlob(images);
2066 images->blob->exempt=MagickTrue;
2067 *images->filename=
'\0';
2068 status=WriteImages(blob_info,images,images->filename,exception);
2069 *length=images->blob->length;
2070 blob=DetachBlob(images->blob);
2071 if (blob != (
void *) NULL)
2073 if (status == MagickFalse)
2074 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2076 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
2077 sizeof(
unsigned char));
2080 if (status == MagickFalse)
2081 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2087 filename[MagickPathExtent],
2088 unique[MagickPathExtent];
2096 file=AcquireUniqueFileResource(unique);
2099 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2100 image_info->filename);
2104 blob_info->file=fdopen(file,
"wb");
2105 if (blob_info->file != (FILE *) NULL)
2107 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2108 images->magick,unique);
2109 status=WriteImages(blob_info,images,filename,exception);
2110 (void) fclose(blob_info->file);
2111 if (status == MagickFalse)
2112 InheritException(exception,&images->exception);
2114 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2116 (void) RelinquishUniqueFileResource(unique);
2119 blob_info=DestroyImageInfo(blob_info);
2155MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2156 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2159 filename[MagickPathExtent];
2188 assert(image_info != (ImageInfo *) NULL);
2189 assert(image_info->signature == MagickCoreSignature);
2190 assert(image != (Image *) NULL);
2191 assert(image->signature == MagickCoreSignature);
2192 assert(inject_image != (Image *) NULL);
2193 assert(inject_image->signature == MagickCoreSignature);
2194 assert(exception != (ExceptionInfo *) NULL);
2195 if (IsEventLogging() != MagickFalse)
2196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2197 unique_file=(FILE *) NULL;
2198 file=AcquireUniqueFileResource(filename);
2200 unique_file=fdopen(file,
"wb");
2201 if ((file == -1) || (unique_file == (FILE *) NULL))
2203 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2204 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2206 return(MagickFalse);
2208 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2209 if (byte_image == (Image *) NULL)
2211 (void) fclose(unique_file);
2212 (void) RelinquishUniqueFileResource(filename);
2213 return(MagickFalse);
2215 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2217 DestroyBlob(byte_image);
2218 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2219 write_info=CloneImageInfo(image_info);
2220 SetImageInfoFile(write_info,unique_file);
2221 status=WriteImage(write_info,byte_image);
2222 write_info=DestroyImageInfo(write_info);
2223 byte_image=DestroyImage(byte_image);
2224 (void) fclose(unique_file);
2225 if (status == MagickFalse)
2227 (void) RelinquishUniqueFileResource(filename);
2228 return(MagickFalse);
2233 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2236 (void) RelinquishUniqueFileResource(filename);
2237 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2238 image_info->filename);
2239 return(MagickFalse);
2241 quantum=(size_t) MagickMaxBufferExtent;
2242 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2243 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2244 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2245 if (buffer == (
unsigned char *) NULL)
2247 (void) RelinquishUniqueFileResource(filename);
2248 file=close_utf8(file);
2249 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2254 ssize_t count = read(file,buffer,quantum);
2261 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2264 file=close_utf8(file);
2266 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2267 (void) RelinquishUniqueFileResource(filename);
2268 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2294MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2296 assert(image != (
const Image *) NULL);
2297 assert(image->signature == MagickCoreSignature);
2298 if (IsEventLogging() != MagickFalse)
2299 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2300 return(image->blob->exempt);
2325MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2328 *magick_restrict blob_info;
2330 assert(image != (
const Image *) NULL);
2331 assert(image->signature == MagickCoreSignature);
2332 if (IsEventLogging() != MagickFalse)
2333 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2334 blob_info=image->blob;
2335 switch (blob_info->type)
2344 if (blob_info->file_info.file == (FILE *) NULL)
2345 return(MagickFalse);
2346 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2347 return(status == -1 ? MagickFalse : MagickTrue);
2351#if defined(MAGICKCORE_ZLIB_DELEGATE)
2355 if (blob_info->file_info.gzfile == (gzFile) NULL)
2356 return(MagickFalse);
2357 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2358 return(offset < 0 ? MagickFalse : MagickTrue);
2363 case UndefinedStream:
2367 case StandardStream:
2372 return(MagickFalse);
2397MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2399 assert(image != (
const Image *) NULL);
2400 assert(image->signature == MagickCoreSignature);
2401 if (IsEventLogging() != MagickFalse)
2402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2403 return(image->blob->temporary);
2435MagickExport
unsigned char *MapBlob(
int file,
const MapMode mode,
2436 const MagickOffsetType offset,
const size_t length)
2438#if defined(MAGICKCORE_HAVE_MMAP)
2451#if defined(MAP_ANONYMOUS)
2452 flags|=MAP_ANONYMOUS;
2454 return((
unsigned char *) NULL);
2461 protection=PROT_READ;
2467 protection=PROT_WRITE;
2473 protection=PROT_READ | PROT_WRITE;
2478#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2479 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,offset);
2481 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags |
2482 MAP_HUGETLB,file,offset);
2483 if (map == (
unsigned char *) MAP_FAILED)
2484 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,
2487 if (map == (
unsigned char *) MAP_FAILED)
2488 return((
unsigned char *) NULL);
2495 return((
unsigned char *) NULL);
2524MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
2533 assert(buffer != (
unsigned char *) NULL);
2540 *buffer++=(
unsigned char) c;
2544 *buffer++=(
unsigned char) c;
2574MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
2582 assert(p != (
unsigned char *) NULL);
2589 *p++=(
unsigned char) c;
2625static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
2637 size=MagickMinBufferExtent;
2638 option=GetImageOption(image_info,
"stream:buffer-size");
2639 if (option != (
const char *) NULL)
2640 size=StringToUnsignedLong(option);
2641 status=setvbuf(image->blob->file_info.file,(
char *) NULL,size == 0 ?
2642 _IONBF : _IOFBF,size);
2643 return(status == 0 ? MagickTrue : MagickFalse);
2646#if defined(MAGICKCORE_ZLIB_DELEGATE)
2647static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
2649#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2650 return(gzopen(path,mode));
2658 path_wide=NTCreateWidePath(path);
2659 if (path_wide == (
wchar_t *) NULL)
2660 return((gzFile) NULL);
2661 file=gzopen_w(path_wide,mode);
2662 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
2668MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
2669 Image *image,
const BlobMode mode,ExceptionInfo *exception)
2672 *magick_restrict blob_info;
2675 extension[MagickPathExtent],
2676 filename[MagickPathExtent];
2690 assert(image_info != (ImageInfo *) NULL);
2691 assert(image_info->signature == MagickCoreSignature);
2692 assert(image != (Image *) NULL);
2693 assert(image->signature == MagickCoreSignature);
2694 if (IsEventLogging() != MagickFalse)
2695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2696 image_info->filename);
2697 blob_info=image->blob;
2698 if (image_info->blob != (
void *) NULL)
2700 if (image_info->stream != (StreamHandler) NULL)
2701 blob_info->stream=(StreamHandler) image_info->stream;
2702 AttachBlob(blob_info,image_info->blob,image_info->length);
2705 (void) DetachBlob(blob_info);
2706 blob_info->mode=mode;
2715 case ReadBinaryBlobMode:
2717 flags=O_RDONLY | O_BINARY;
2723 flags=O_WRONLY | O_CREAT | O_TRUNC;
2727 case WriteBinaryBlobMode:
2729 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
2733 case AppendBlobMode:
2735 flags=O_WRONLY | O_CREAT | O_APPEND;
2739 case AppendBinaryBlobMode:
2741 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
2753 blob_info->synchronize=image_info->synchronize;
2754 if (image_info->stream != (StreamHandler) NULL)
2756 blob_info->stream=(StreamHandler) image_info->stream;
2759 blob_info->type=FifoStream;
2767 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2768 rights=ReadPolicyRights;
2770 rights=WritePolicyRights;
2771 if (IsPathAuthorized(rights,filename) == MagickFalse)
2772 ThrowPolicyException(filename,MagickFalse);
2773 if ((LocaleCompare(filename,
"-") == 0) ||
2774 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
2776 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
2777#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2778 if (strchr(type,
'b') != (
char *) NULL)
2779 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2781 blob_info->type=StandardStream;
2782 blob_info->exempt=MagickTrue;
2783 return(SetStreamBuffering(image_info,image));
2785 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
2786 (IsGeometry(filename+3) != MagickFalse))
2789 fileMode[MagickPathExtent];
2793 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2794 if (blob_info->file_info.file == (FILE *) NULL)
2796 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2797 return(MagickFalse);
2799#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2800 if (strchr(type,
'b') != (
char *) NULL)
2801 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2803 blob_info->type=FileStream;
2804 blob_info->exempt=MagickTrue;
2805 return(SetStreamBuffering(image_info,image));
2807#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2808 if (*filename ==
'|')
2811 fileMode[MagickPathExtent],
2819 (void) signal(SIGPIPE,SIG_IGN);
2823 sanitize_command=SanitizeString(filename+1);
2824 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2826 sanitize_command=DestroyString(sanitize_command);
2827 if (blob_info->file_info.file == (FILE *) NULL)
2829 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2830 return(MagickFalse);
2832 blob_info->type=PipeStream;
2833 blob_info->exempt=MagickTrue;
2834 return(SetStreamBuffering(image_info,image));
2837 status=GetPathAttributes(filename,&blob_info->properties);
2838#if defined(S_ISFIFO)
2839 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2841 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2842 if (blob_info->file_info.file == (FILE *) NULL)
2844 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2845 return(MagickFalse);
2847 blob_info->type=FileStream;
2848 blob_info->exempt=MagickTrue;
2849 return(SetStreamBuffering(image_info,image));
2852 GetPathComponent(image->filename,ExtensionPath,extension);
2855 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2856 if ((image_info->adjoin == MagickFalse) ||
2857 (strchr(filename,
'%') != (
char *) NULL))
2862 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
2863 image->scene,filename);
2864 if ((LocaleCompare(filename,image->filename) == 0) &&
2865 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2866 (GetNextImageInList(image) != (Image *) NULL)))
2869 path[MagickPathExtent];
2871 GetPathComponent(image->filename,RootPath,path);
2872 if (*extension ==
'\0')
2873 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
2874 path,(
double) image->scene);
2876 (
void) FormatLocaleString(filename,MagickPathExtent,
2877 "%s-%.20g.%s",path,(
double) image->scene,extension);
2879 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2880#if defined(macintosh)
2881 SetApplicationType(filename,image_info->magick,
'8BIM');
2884 if (IsPathAuthorized(rights,filename) == MagickFalse)
2885 ThrowPolicyException(filename,MagickFalse);
2887 if (image_info->file != (FILE *) NULL)
2889 blob_info->file_info.file=image_info->file;
2890 blob_info->type=FileStream;
2891 blob_info->exempt=MagickTrue;
2899 blob_info->file_info.file=(FILE *) NULL;
2900 file=open_utf8(filename,flags,0);
2902 blob_info->file_info.file=fdopen(file,type);
2903 if (blob_info->file_info.file != (FILE *) NULL)
2911 blob_info->type=FileStream;
2912 (void) fstat(fileno(blob_info->file_info.file),
2913 &blob_info->properties);
2914 (void) SetStreamBuffering(image_info,image);
2915 (void) memset(magick,0,
sizeof(magick));
2916 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2917 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2918#if defined(MAGICKCORE_POSIX_SUPPORT)
2919 (void) fflush(blob_info->file_info.file);
2921 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2922 " read %.20g magic header bytes",(
double) count);
2923#if defined(MAGICKCORE_ZLIB_DELEGATE)
2924 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2925 ((
int) magick[2] == 0x08))
2928 gzfile = gzopen_utf8(filename,
"rb");
2930 if (gzfile != (gzFile) NULL)
2932 if (blob_info->file_info.file != (FILE *) NULL)
2933 (void) fclose(blob_info->file_info.file);
2934 blob_info->file_info.file=(FILE *) NULL;
2935 blob_info->file_info.gzfile=gzfile;
2936 blob_info->type=ZipStream;
2940#if defined(MAGICKCORE_BZLIB_DELEGATE)
2941 if (strncmp((
char *) magick,
"BZh",3) == 0)
2944 *bzfile = BZ2_bzopen(filename,
"r");
2946 if (bzfile != (BZFILE *) NULL)
2948 if (blob_info->file_info.file != (FILE *) NULL)
2949 (void) fclose(blob_info->file_info.file);
2950 blob_info->file_info.file=(FILE *) NULL;
2951 blob_info->file_info.bzfile=bzfile;
2952 blob_info->type=BZipStream;
2956 if (blob_info->type == FileStream)
2967 sans_exception=AcquireExceptionInfo();
2968 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2969 sans_exception=DestroyExceptionInfo(sans_exception);
2970 length=(size_t) blob_info->properties.st_size;
2971 if ((magick_info != (
const MagickInfo *) NULL) &&
2972 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2973 (AcquireMagickResource(MapResource,length) != MagickFalse))
2978 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
2980 if (blob == (
void *) NULL)
2981 RelinquishMagickResource(MapResource,length);
2987 if (image_info->file != (FILE *) NULL)
2988 blob_info->exempt=MagickFalse;
2991 (void) fclose(blob_info->file_info.file);
2992 blob_info->file_info.file=(FILE *) NULL;
2994 AttachBlob(blob_info,blob,length);
2995 blob_info->mapped=MagickTrue;
3002#if defined(MAGICKCORE_ZLIB_DELEGATE)
3003 if ((LocaleCompare(extension,
"gz") == 0) ||
3004 (LocaleCompare(extension,
"wmz") == 0) ||
3005 (LocaleCompare(extension,
"svgz") == 0))
3007 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3008 if (blob_info->file_info.gzfile != (gzFile) NULL)
3009 blob_info->type=ZipStream;
3013#if defined(MAGICKCORE_BZLIB_DELEGATE)
3014 if (LocaleCompare(extension,
"bz2") == 0)
3016 if (mode == WriteBinaryBlobMode)
3018 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3019 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3020 blob_info->type=BZipStream;
3028 blob_info->file_info.file=(FILE *) NULL;
3029 file=open_utf8(filename,flags,P_MODE);
3031 blob_info->file_info.file=fdopen(file,type);
3032 if (blob_info->file_info.file != (FILE *) NULL)
3034 blob_info->type=FileStream;
3035 (void) SetStreamBuffering(image_info,image);
3038 if (IsPathAuthorized(rights,filename) == MagickFalse)
3039 ThrowPolicyException(filename,MagickFalse);
3040 blob_info->status=0;
3041 blob_info->error_number=0;
3042 if (blob_info->type != UndefinedStream)
3043 blob_info->size=GetBlobSize(image);
3046 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3047 return(MagickFalse);
3086#if defined(__cplusplus) || defined(c_plusplus)
3090static size_t PingStream(
const Image *magick_unused(image),
3091 const void *magick_unused(pixels),
const size_t columns)
3093 magick_unreferenced(image);
3094 magick_unreferenced(pixels);
3099#if defined(__cplusplus) || defined(c_plusplus)
3103MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3104 const size_t length,ExceptionInfo *exception)
3119 assert(image_info != (ImageInfo *) NULL);
3120 assert(image_info->signature == MagickCoreSignature);
3121 assert(exception != (ExceptionInfo *) NULL);
3122 if (IsEventLogging() != MagickFalse)
3123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3124 image_info->filename);
3125 if ((blob == (
const void *) NULL) || (length == 0))
3127 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3128 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3129 return((Image *) NULL);
3131 ping_info=CloneImageInfo(image_info);
3132 ping_info->blob=(
void *) blob;
3133 ping_info->length=length;
3134 ping_info->ping=MagickTrue;
3135 if (*ping_info->magick ==
'\0')
3136 (void) SetImageInfo(ping_info,0,exception);
3137 magick_info=GetMagickInfo(ping_info->magick,exception);
3138 if (magick_info == (
const MagickInfo *) NULL)
3140 (void) ThrowMagickException(exception,GetMagickModule(),
3141 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3143 ping_info=DestroyImageInfo(ping_info);
3144 return((Image *) NULL);
3146 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3149 filename[MagickPathExtent];
3154 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3155 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3156 ping_info->magick,filename);
3157 image=ReadStream(ping_info,&PingStream,exception);
3158 if (image != (Image *) NULL)
3159 (void) DetachBlob(image->blob);
3160 ping_info=DestroyImageInfo(ping_info);
3166 ping_info->blob=(
void *) NULL;
3167 ping_info->length=0;
3168 *ping_info->filename=
'\0';
3169 status=BlobToFile(ping_info->filename,blob,length,exception);
3170 if (status == MagickFalse)
3172 (void) RelinquishUniqueFileResource(ping_info->filename);
3173 ping_info=DestroyImageInfo(ping_info);
3174 return((Image *) NULL);
3176 clone_info=CloneImageInfo(ping_info);
3177 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3178 ping_info->magick,ping_info->filename);
3179 image=ReadStream(clone_info,&PingStream,exception);
3180 if (image != (Image *) NULL)
3188 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3190 (void) CopyMagickString(images->filename,image_info->filename,
3192 (void) CopyMagickString(images->magick_filename,image_info->filename,
3194 (void) CopyMagickString(images->magick,magick_info->name,
3196 images=GetNextImageInList(images);
3199 clone_info=DestroyImageInfo(clone_info);
3200 (void) RelinquishUniqueFileResource(ping_info->filename);
3201 ping_info=DestroyImageInfo(ping_info);
3236MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
3237 unsigned char *data)
3240 *magick_restrict blob_info;
3251 assert(image != (Image *) NULL);
3252 assert(image->signature == MagickCoreSignature);
3253 assert(image->blob != (BlobInfo *) NULL);
3254 assert(image->blob->type != UndefinedStream);
3257 assert(data != (
void *) NULL);
3258 blob_info=image->blob;
3261 switch (blob_info->type)
3263 case UndefinedStream:
3265 case StandardStream:
3273 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3278 c=getc(blob_info->file_info.file);
3281 *q++=(
unsigned char) c;
3287 c=getc(blob_info->file_info.file);
3290 *q++=(
unsigned char) c;
3296 c=getc(blob_info->file_info.file);
3299 *q++=(
unsigned char) c;
3305 c=getc(blob_info->file_info.file);
3308 *q++=(
unsigned char) c;
3315 if ((count != (ssize_t) length) &&
3316 (ferror(blob_info->file_info.file) != 0))
3317 ThrowBlobException(blob_info);
3322#if defined(MAGICKCORE_ZLIB_DELEGATE)
3333 for (i=0; i < (ssize_t) length; i+=count)
3335 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3336 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3349 c=gzgetc(blob_info->file_info.gzfile);
3352 *q++=(
unsigned char) c;
3358 c=gzgetc(blob_info->file_info.gzfile);
3361 *q++=(
unsigned char) c;
3367 c=gzgetc(blob_info->file_info.gzfile);
3370 *q++=(
unsigned char) c;
3376 c=gzgetc(blob_info->file_info.gzfile);
3379 *q++=(
unsigned char) c;
3387 (void) gzerror(blob_info->file_info.gzfile,&status);
3388 if ((count != (ssize_t) length) && (status != Z_OK))
3389 ThrowBlobException(blob_info);
3390 if (blob_info->eof == MagickFalse)
3391 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3398#if defined(MAGICKCORE_BZLIB_DELEGATE)
3405 for (i=0; i < (ssize_t) length; i+=count)
3407 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3408 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3418 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3419 if ((count != (ssize_t) length) && (status != BZ_OK))
3420 ThrowBlobException(blob_info);
3431 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3433 blob_info->eof=MagickTrue;
3436 p=blob_info->data+blob_info->offset;
3437 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3438 blob_info->length-blob_info->offset);
3439 blob_info->offset+=count;
3440 if (count != (ssize_t) length)
3441 blob_info->eof=MagickTrue;
3442 (void) memcpy(q,p,(
size_t) count);
3471MagickExport
int ReadBlobByte(Image *image)
3474 *magick_restrict blob_info;
3479 assert(image != (Image *) NULL);
3480 assert(image->signature == MagickCoreSignature);
3481 assert(image->blob != (BlobInfo *) NULL);
3482 assert(image->blob->type != UndefinedStream);
3483 blob_info=image->blob;
3484 switch (blob_info->type)
3486 case StandardStream:
3490 c=getc(blob_info->file_info.file);
3493 if (ferror(blob_info->file_info.file) != 0)
3494 ThrowBlobException(blob_info);
3501 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3503 blob_info->eof=MagickTrue;
3506 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
3507 blob_info->offset++;
3518 count=ReadBlob(image,1,buffer);
3551MagickExport
double ReadBlobDouble(Image *image)
3562 quantum.double_value=0.0;
3563 quantum.unsigned_value=ReadBlobLongLong(image);
3564 return(quantum.double_value);
3590MagickExport
float ReadBlobFloat(Image *image)
3601 quantum.float_value=0.0;
3602 quantum.unsigned_value=ReadBlobLong(image);
3603 return(quantum.float_value);
3629MagickExport
unsigned int ReadBlobLong(Image *image)
3643 assert(image != (Image *) NULL);
3644 assert(image->signature == MagickCoreSignature);
3646 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3649 if (image->endian == LSBEndian)
3651 value=(
unsigned int) (*p++);
3652 value|=(
unsigned int) (*p++) << 8;
3653 value|=(
unsigned int) (*p++) << 16;
3654 value|=(
unsigned int) (*p++) << 24;
3657 value=(
unsigned int) (*p++) << 24;
3658 value|=(
unsigned int) (*p++) << 16;
3659 value|=(
unsigned int) (*p++) << 8;
3660 value|=(
unsigned int) (*p++);
3687MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3701 assert(image != (Image *) NULL);
3702 assert(image->signature == MagickCoreSignature);
3704 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3706 return(MagickULLConstant(0));
3707 if (image->endian == LSBEndian)
3709 value=(MagickSizeType) (*p++);
3710 value|=(MagickSizeType) (*p++) << 8;
3711 value|=(MagickSizeType) (*p++) << 16;
3712 value|=(MagickSizeType) (*p++) << 24;
3713 value|=(MagickSizeType) (*p++) << 32;
3714 value|=(MagickSizeType) (*p++) << 40;
3715 value|=(MagickSizeType) (*p++) << 48;
3716 value|=(MagickSizeType) (*p++) << 56;
3719 value=(MagickSizeType) (*p++) << 56;
3720 value|=(MagickSizeType) (*p++) << 48;
3721 value|=(MagickSizeType) (*p++) << 40;
3722 value|=(MagickSizeType) (*p++) << 32;
3723 value|=(MagickSizeType) (*p++) << 24;
3724 value|=(MagickSizeType) (*p++) << 16;
3725 value|=(MagickSizeType) (*p++) << 8;
3726 value|=(MagickSizeType) (*p++);
3753MagickExport
unsigned short ReadBlobShort(Image *image)
3767 assert(image != (Image *) NULL);
3768 assert(image->signature == MagickCoreSignature);
3770 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3772 return((
unsigned short) 0U);
3773 if (image->endian == LSBEndian)
3775 value=(
unsigned short) (*p++);
3776 value|=(
unsigned short) (*p++) << 8;
3779 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3780 value|=(
unsigned short) (*p++);
3807MagickExport
unsigned int ReadBlobLSBLong(Image *image)
3821 assert(image != (Image *) NULL);
3822 assert(image->signature == MagickCoreSignature);
3824 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3827 value=(
unsigned int) (*p++);
3828 value|=(
unsigned int) (*p++) << 8;
3829 value|=(
unsigned int) (*p++) << 16;
3830 value|=(
unsigned int) (*p++) << 24;
3857MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
3868 quantum.unsigned_value=ReadBlobLSBLong(image);
3869 return(quantum.signed_value);
3895MagickExport
unsigned short ReadBlobLSBShort(Image *image)
3909 assert(image != (Image *) NULL);
3910 assert(image->signature == MagickCoreSignature);
3912 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3914 return((
unsigned short) 0U);
3915 value=(
unsigned short) (*p++);
3916 value|=(
unsigned short) (*p++) << 8;
3943MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
3954 quantum.unsigned_value=ReadBlobLSBShort(image);
3955 return(quantum.signed_value);
3981MagickExport
unsigned int ReadBlobMSBLong(Image *image)
3995 assert(image != (Image *) NULL);
3996 assert(image->signature == MagickCoreSignature);
3998 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4001 value=(
unsigned int) (*p++) << 24;
4002 value|=(
unsigned int) (*p++) << 16;
4003 value|=(
unsigned int) (*p++) << 8;
4004 value|=(
unsigned int) (*p++);
4031MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4045 assert(image != (Image *) NULL);
4046 assert(image->signature == MagickCoreSignature);
4048 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4050 return(MagickULLConstant(0));
4051 value=(MagickSizeType) (*p++) << 56;
4052 value|=(MagickSizeType) (*p++) << 48;
4053 value|=(MagickSizeType) (*p++) << 40;
4054 value|=(MagickSizeType) (*p++) << 32;
4055 value|=(MagickSizeType) (*p++) << 24;
4056 value|=(MagickSizeType) (*p++) << 16;
4057 value|=(MagickSizeType) (*p++) << 8;
4058 value|=(MagickSizeType) (*p++);
4085MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4099 assert(image != (Image *) NULL);
4100 assert(image->signature == MagickCoreSignature);
4102 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4104 return((
unsigned short) 0U);
4105 value=(
unsigned short) ((*p++) << 8);
4106 value|=(
unsigned short) (*p++);
4133MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4144 quantum.unsigned_value=ReadBlobMSBLong(image);
4145 return(quantum.signed_value);
4171MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4182 quantum.unsigned_value=ReadBlobMSBShort(image);
4183 return(quantum.signed_value);
4209MagickExport
signed int ReadBlobSignedLong(Image *image)
4220 quantum.unsigned_value=ReadBlobLong(image);
4221 return(quantum.signed_value);
4247MagickExport
signed short ReadBlobSignedShort(Image *image)
4258 quantum.unsigned_value=ReadBlobShort(image);
4259 return(quantum.signed_value);
4297MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4298 const size_t length,
void *magick_restrict data,ssize_t *count)
4301 *magick_restrict blob_info;
4303 assert(image != (Image *) NULL);
4304 assert(image->signature == MagickCoreSignature);
4305 assert(image->blob != (BlobInfo *) NULL);
4306 assert(image->blob->type != UndefinedStream);
4307 assert(count != (ssize_t *) NULL);
4308 blob_info=image->blob;
4309 if (blob_info->type != BlobStream)
4311 assert(data != NULL);
4312 *count=ReadBlob(image,length,(
unsigned char *) data);
4315 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4318 blob_info->eof=MagickTrue;
4321 data=blob_info->data+blob_info->offset;
4322 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4323 blob_info->length-blob_info->offset);
4324 blob_info->offset+=(*count);
4325 if (*count != (ssize_t) length)
4326 blob_info->eof=MagickTrue;
4355MagickExport
char *ReadBlobString(Image *image,
char *
string)
4358 *magick_restrict blob_info;
4366 assert(image != (Image *) NULL);
4367 assert(image->signature == MagickCoreSignature);
4368 assert(image->blob != (BlobInfo *) NULL);
4369 assert(image->blob->type != UndefinedStream);
4370 if (IsEventLogging() != MagickFalse)
4371 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4373 blob_info=image->blob;
4374 switch (blob_info->type)
4376 case UndefinedStream:
4378 case StandardStream:
4381 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4382 if (p == (
char *) NULL)
4384 if (ferror(blob_info->file_info.file) != 0)
4385 ThrowBlobException(blob_info);
4386 return((
char *) NULL);
4393#if defined(MAGICKCORE_ZLIB_DELEGATE)
4394 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4395 if (p == (
char *) NULL)
4398 (void) gzerror(blob_info->file_info.gzfile,&status);
4400 ThrowBlobException(blob_info);
4401 return((
char *) NULL);
4411 c=ReadBlobByte(image);
4414 blob_info->eof=MagickTrue;
4420 }
while (i < (MaxTextExtent-2));
4428 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4431 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4433 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4434 return((
char *) NULL);
4461MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4463 assert(blob != (BlobInfo *) NULL);
4464 assert(blob->signature == MagickCoreSignature);
4465 if (IsEventLogging() != MagickFalse)
4466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4467 LockSemaphoreInfo(blob->semaphore);
4468 blob->reference_count++;
4469 UnlockSemaphoreInfo(blob->semaphore);
4506MagickExport MagickOffsetType SeekBlob(Image *image,
4507 const MagickOffsetType offset,
const int whence)
4510 *magick_restrict blob_info;
4512 assert(image != (Image *) NULL);
4513 assert(image->signature == MagickCoreSignature);
4514 assert(image->blob != (BlobInfo *) NULL);
4515 assert(image->blob->type != UndefinedStream);
4516 if (IsEventLogging() != MagickFalse)
4517 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4518 blob_info=image->blob;
4519 switch (blob_info->type)
4521 case UndefinedStream:
4523 case StandardStream:
4528 if ((offset < 0) && (whence == SEEK_SET))
4530 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4532 blob_info->offset=TellBlob(image);
4537#if defined(MAGICKCORE_ZLIB_DELEGATE)
4538 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4541 blob_info->offset=TellBlob(image);
4557 blob_info->offset=offset;
4562 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4563 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4568 if ((blob_info->offset+offset) < 0)
4570 blob_info->offset+=offset;
4575 if (((MagickOffsetType) blob_info->length+offset) < 0)
4577 blob_info->offset=blob_info->length+offset;
4581 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4583 blob_info->eof=MagickFalse;
4589 return(blob_info->offset);
4617MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
4619 assert(image != (
const Image *) NULL);
4620 assert(image->signature == MagickCoreSignature);
4621 if (IsEventLogging() != MagickFalse)
4622 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4623 image->blob->exempt=exempt;
4652MagickExport MagickBooleanType SetBlobExtent(Image *image,
4653 const MagickSizeType extent)
4656 *magick_restrict blob_info;
4658 assert(image != (Image *) NULL);
4659 assert(image->signature == MagickCoreSignature);
4660 assert(image->blob != (BlobInfo *) NULL);
4661 assert(image->blob->type != UndefinedStream);
4662 if (IsEventLogging() != MagickFalse)
4663 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4664 blob_info=image->blob;
4665 switch (blob_info->type)
4667 case UndefinedStream:
4669 case StandardStream:
4670 return(MagickFalse);
4679 if (extent != (MagickSizeType) ((off_t) extent))
4680 return(MagickFalse);
4681 offset=SeekBlob(image,0,SEEK_END);
4683 return(MagickFalse);
4684 if ((MagickSizeType) offset >= extent)
4686 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4689 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4690 blob_info->file_info.file);
4691#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4692 if (blob_info->synchronize != MagickFalse)
4697 file=fileno(blob_info->file_info.file);
4698 if ((file == -1) || (offset < 0))
4699 return(MagickFalse);
4700 (void) posix_fallocate(file,offset,extent-offset);
4703 offset=SeekBlob(image,offset,SEEK_SET);
4705 return(MagickFalse);
4710 return(MagickFalse);
4712 return(MagickFalse);
4714 return(MagickFalse);
4717 if (extent != (MagickSizeType) ((
size_t) extent))
4718 return(MagickFalse);
4719 if (blob_info->mapped != MagickFalse)
4727 (void) UnmapBlob(blob_info->data,blob_info->length);
4728 RelinquishMagickResource(MapResource,blob_info->length);
4729 if (extent != (MagickSizeType) ((off_t) extent))
4730 return(MagickFalse);
4731 offset=SeekBlob(image,0,SEEK_END);
4733 return(MagickFalse);
4734 if ((MagickSizeType) offset >= extent)
4736 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4737 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4738 blob_info->file_info.file);
4739#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4740 if (blob_info->synchronize != MagickFalse)
4745 file=fileno(blob_info->file_info.file);
4746 if ((file == -1) || (offset < 0))
4747 return(MagickFalse);
4748 (void) posix_fallocate(file,offset,extent-offset);
4751 offset=SeekBlob(image,offset,SEEK_SET);
4753 return(MagickFalse);
4754 (void) AcquireMagickResource(MapResource,extent);
4755 blob_info->data=(
unsigned char*) MapBlob(fileno(
4756 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
4757 blob_info->extent=(size_t) extent;
4758 blob_info->length=(size_t) extent;
4759 (void) SyncBlob(image);
4762 blob_info->extent=(size_t) extent;
4763 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4764 blob_info->extent+1,
sizeof(*blob_info->data));
4765 (void) SyncBlob(image);
4766 if (blob_info->data == (
unsigned char *) NULL)
4768 (void) DetachBlob(blob_info);
4769 return(MagickFalse);
4801static int SyncBlob(
const Image *image)
4804 *magick_restrict blob_info;
4809 assert(image != (Image *) NULL);
4810 assert(image->signature == MagickCoreSignature);
4811 assert(image->blob != (BlobInfo *) NULL);
4812 if (IsEventLogging() != MagickFalse)
4813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4814 if (EOFBlob(image) != 0)
4816 blob_info=image->blob;
4818 switch (blob_info->type)
4820 case UndefinedStream:
4821 case StandardStream:
4826 status=fflush(blob_info->file_info.file);
4831#if defined(MAGICKCORE_ZLIB_DELEGATE)
4832 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4838#if defined(MAGICKCORE_BZLIB_DELEGATE)
4839 status=BZ2_bzflush(blob_info->file_info.bzfile);
4873MagickExport MagickOffsetType TellBlob(
const Image *image)
4876 *magick_restrict blob_info;
4881 assert(image != (Image *) NULL);
4882 assert(image->signature == MagickCoreSignature);
4883 assert(image->blob != (BlobInfo *) NULL);
4884 assert(image->blob->type != UndefinedStream);
4885 if (IsEventLogging() != MagickFalse)
4886 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4887 blob_info=image->blob;
4889 switch (blob_info->type)
4891 case UndefinedStream:
4892 case StandardStream:
4896 offset=ftell(blob_info->file_info.file);
4903#if defined(MAGICKCORE_ZLIB_DELEGATE)
4904 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4914 offset=blob_info->offset;
4946MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
4948#if defined(MAGICKCORE_HAVE_MMAP)
4952 status=munmap(map,length);
4953 return(status == -1 ? MagickFalse : MagickTrue);
4957 return(MagickFalse);
4990MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
4991 const unsigned char *data)
4994 *magick_restrict blob_info;
5008 assert(image != (Image *) NULL);
5009 assert(image->signature == MagickCoreSignature);
5010 assert(image->blob != (BlobInfo *) NULL);
5011 assert(image->blob->type != UndefinedStream);
5014 assert(data != (
const unsigned char *) NULL);
5015 blob_info=image->blob;
5017 p=(
const unsigned char *) data;
5018 q=(
unsigned char *) data;
5019 switch (blob_info->type)
5021 case UndefinedStream:
5023 case StandardStream:
5031 count=(ssize_t) fwrite((
const char *) data,1,length,
5032 blob_info->file_info.file);
5037 c=putc((
int) *p++,blob_info->file_info.file);
5045 c=putc((
int) *p++,blob_info->file_info.file);
5053 c=putc((
int) *p++,blob_info->file_info.file);
5061 c=putc((
int) *p++,blob_info->file_info.file);
5070 if ((count != (ssize_t) length) &&
5071 (ferror(blob_info->file_info.file) != 0))
5072 ThrowBlobException(blob_info);
5077#if defined(MAGICKCORE_ZLIB_DELEGATE)
5088 for (i=0; i < (ssize_t) length; i+=count)
5090 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5091 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5104 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5112 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5120 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5128 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5138 (void) gzerror(blob_info->file_info.gzfile,&status);
5139 if ((count != (ssize_t) length) && (status != Z_OK))
5140 ThrowBlobException(blob_info);
5146#if defined(MAGICKCORE_BZLIB_DELEGATE)
5153 for (i=0; i < (ssize_t) length; i+=count)
5155 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5156 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5166 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5167 if ((count != (ssize_t) length) && (status != BZ_OK))
5168 ThrowBlobException(blob_info);
5174 count=(ssize_t) blob_info->stream(image,data,length);
5182 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5187 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5188 if (extent >= blob_info->extent)
5190 extent+=blob_info->quantum+length;
5191 blob_info->quantum<<=1;
5192 if (SetBlobExtent(image,extent) == MagickFalse)
5195 q=blob_info->data+blob_info->offset;
5196 (void) memcpy(q,p,length);
5197 blob_info->offset+=length;
5198 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5199 blob_info->length=(size_t) blob_info->offset;
5200 count=(ssize_t) length;
5203 if (count != (ssize_t) length)
5204 ThrowBlobException(blob_info);
5233MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
5236 *magick_restrict blob_info;
5241 assert(image != (Image *) NULL);
5242 assert(image->signature == MagickCoreSignature);
5243 assert(image->blob != (BlobInfo *) NULL);
5244 assert(image->blob->type != UndefinedStream);
5245 blob_info=image->blob;
5247 switch (blob_info->type)
5249 case StandardStream:
5256 c=putc((
int) value,blob_info->file_info.file);
5259 if (ferror(blob_info->file_info.file) != 0)
5260 ThrowBlobException(blob_info);
5268 count=WriteBlobStream(image,1,&value);
5300MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
5311 quantum.unsigned_value=0U;
5312 quantum.float_value=value;
5313 return(WriteBlobLong(image,quantum.unsigned_value));
5341MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
5346 assert(image != (Image *) NULL);
5347 assert(image->signature == MagickCoreSignature);
5348 if (image->endian == LSBEndian)
5350 buffer[0]=(
unsigned char) value;
5351 buffer[1]=(
unsigned char) (value >> 8);
5352 buffer[2]=(
unsigned char) (value >> 16);
5353 buffer[3]=(
unsigned char) (value >> 24);
5354 return(WriteBlobStream(image,4,buffer));
5356 buffer[0]=(
unsigned char) (value >> 24);
5357 buffer[1]=(
unsigned char) (value >> 16);
5358 buffer[2]=(
unsigned char) (value >> 8);
5359 buffer[3]=(
unsigned char) value;
5360 return(WriteBlobStream(image,4,buffer));
5388MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
5393 assert(image != (Image *) NULL);
5394 assert(image->signature == MagickCoreSignature);
5395 if (image->endian == LSBEndian)
5397 buffer[0]=(
unsigned char) value;
5398 buffer[1]=(
unsigned char) (value >> 8);
5399 return(WriteBlobStream(image,2,buffer));
5401 buffer[0]=(
unsigned char) (value >> 8);
5402 buffer[1]=(
unsigned char) value;
5403 return(WriteBlobStream(image,2,buffer));
5431MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
5436 assert(image != (Image *) NULL);
5437 assert(image->signature == MagickCoreSignature);
5438 buffer[0]=(
unsigned char) value;
5439 buffer[1]=(
unsigned char) (value >> 8);
5440 buffer[2]=(
unsigned char) (value >> 16);
5441 buffer[3]=(
unsigned char) (value >> 24);
5442 return(WriteBlobStream(image,4,buffer));
5470MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
5475 assert(image != (Image *) NULL);
5476 assert(image->signature == MagickCoreSignature);
5477 buffer[0]=(
unsigned char) value;
5478 buffer[1]=(
unsigned char) (value >> 8);
5479 return(WriteBlobStream(image,2,buffer));
5507MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
5521 assert(image != (Image *) NULL);
5522 assert(image->signature == MagickCoreSignature);
5523 quantum.signed_value=value;
5524 buffer[0]=(
unsigned char) quantum.unsigned_value;
5525 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5526 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5527 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5528 return(WriteBlobStream(image,4,buffer));
5556MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5557 const signed short value)
5571 assert(image != (Image *) NULL);
5572 assert(image->signature == MagickCoreSignature);
5573 quantum.signed_value=value;
5574 buffer[0]=(
unsigned char) quantum.unsigned_value;
5575 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5576 return(WriteBlobStream(image,2,buffer));
5604MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
5609 assert(image != (Image *) NULL);
5610 assert(image->signature == MagickCoreSignature);
5611 buffer[0]=(
unsigned char) (value >> 24);
5612 buffer[1]=(
unsigned char) (value >> 16);
5613 buffer[2]=(
unsigned char) (value >> 8);
5614 buffer[3]=(
unsigned char) value;
5615 return(WriteBlobStream(image,4,buffer));
5643MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5644 const MagickSizeType value)
5649 assert(image != (Image *) NULL);
5650 assert(image->signature == MagickCoreSignature);
5651 buffer[0]=(
unsigned char) (value >> 56);
5652 buffer[1]=(
unsigned char) (value >> 48);
5653 buffer[2]=(
unsigned char) (value >> 40);
5654 buffer[3]=(
unsigned char) (value >> 32);
5655 buffer[4]=(
unsigned char) (value >> 24);
5656 buffer[5]=(
unsigned char) (value >> 16);
5657 buffer[6]=(
unsigned char) (value >> 8);
5658 buffer[7]=(
unsigned char) value;
5659 return(WriteBlobStream(image,8,buffer));
5687MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
5692 assert(image != (Image *) NULL);
5693 assert(image->signature == MagickCoreSignature);
5694 buffer[0]=(
unsigned char) (value >> 8);
5695 buffer[1]=(
unsigned char) value;
5696 return(WriteBlobStream(image,2,buffer));
5724MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,
const signed int value)
5738 assert(image != (Image *) NULL);
5739 assert(image->signature == MagickCoreSignature);
5740 quantum.signed_value=value;
5741 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5742 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5743 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5744 buffer[3]=(
unsigned char) quantum.unsigned_value;
5745 return(WriteBlobStream(image,4,buffer));
5773MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5774 const signed short value)
5788 assert(image != (Image *) NULL);
5789 assert(image->signature == MagickCoreSignature);
5790 quantum.signed_value=value;
5791 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5792 buffer[1]=(
unsigned char) quantum.unsigned_value;
5793 return(WriteBlobStream(image,2,buffer));
5821MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
5823 assert(image != (Image *) NULL);
5824 assert(image->signature == MagickCoreSignature);
5825 assert(
string != (
const char *) NULL);
5826 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));