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)
1161 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1162 "NotAuthorized",
"`%s'",filename);
1166 if (LocaleCompare(filename,
"-") != 0)
1169 flags = O_RDONLY | O_BINARY;
1171 status=GetPathAttributes(filename,&attributes);
1172 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1174 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1177 file=open_utf8(filename,flags,0);
1181 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1182 return((
unsigned char *) NULL);
1184 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1186 file=close_utf8(file)-1;
1188 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1189 "NotAuthorized",
"`%s'",filename);
1192 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1194 if ((file == fileno(stdin)) || (offset < 0) ||
1195 (offset != (MagickOffsetType) ((ssize_t) offset)))
1206 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1207 quantum=(size_t) MagickMaxBufferExtent;
1208 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1209 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1210 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1211 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1213 count=read(file,blob+i,quantum);
1220 if (~((
size_t) i) < (count+quantum+1))
1222 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1225 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+count+quantum+1,
1227 if ((
size_t) (i+count) >= extent)
1230 if (LocaleCompare(filename,
"-") != 0)
1231 file=close_utf8(file);
1232 if (blob == (
unsigned char *) NULL)
1234 (void) ThrowMagickException(exception,GetMagickModule(),
1235 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1236 return((
unsigned char *) NULL);
1240 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1241 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1242 return((
unsigned char *) NULL);
1244 *length=(size_t) MagickMin(i+count,extent);
1248 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1249 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1250 blob=(
unsigned char *) NULL;
1251 if (~(*length) >= (MagickPathExtent-1))
1252 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1254 if (blob == (
unsigned char *) NULL)
1256 file=close_utf8(file);
1257 (void) ThrowMagickException(exception,GetMagickModule(),
1258 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1259 return((
unsigned char *) NULL);
1261 map=MapBlob(file,ReadMode,0,*length);
1262 if (map != (
unsigned char *) NULL)
1264 (void) memcpy(blob,map,*length);
1265 (void) UnmapBlob(map,*length);
1269 (void) lseek(file,0,SEEK_SET);
1270 for (i=0; i < *length; i+=count)
1272 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1273 MagickMaxBufferExtent));
1283 file=close_utf8(file)-1;
1284 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1285 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1286 return((
unsigned char *) NULL);
1290 if (LocaleCompare(filename,
"-") != 0)
1291 file=close_utf8(file);
1294 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1295 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1325static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1326 const unsigned char *magick_restrict data)
1329 *magick_restrict blob_info;
1337 assert(image->blob != (BlobInfo *) NULL);
1338 assert(image->blob->type != UndefinedStream);
1339 assert(data != (
void *) NULL);
1340 blob_info=image->blob;
1341 if (blob_info->type != BlobStream)
1342 return(WriteBlob(image,length,data));
1343 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1348 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1349 if (extent >= blob_info->extent)
1351 extent+=blob_info->quantum+length;
1352 blob_info->quantum<<=1;
1353 if (SetBlobExtent(image,extent) == MagickFalse)
1356 q=blob_info->data+blob_info->offset;
1357 (void) memcpy(q,data,length);
1358 blob_info->offset+=length;
1359 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1360 blob_info->length=(size_t) blob_info->offset;
1361 return((ssize_t) length);
1364MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename)
1382 assert(image != (
const Image *) NULL);
1383 assert(image->signature == MagickCoreSignature);
1384 assert(filename != (
const char *) NULL);
1385 if (IsEventLogging() != MagickFalse)
1386 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1387 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1390 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1391 PolicyError,
"NotAuthorized",
"`%s'",filename);
1392 return(MagickFalse);
1395 if (LocaleCompare(filename,
"-") != 0)
1398 flags = O_RDONLY | O_BINARY;
1400 file=open_utf8(filename,flags,0);
1404 ThrowFileException(&image->exception,BlobError,
"UnableToOpenBlob",
1406 return(MagickFalse);
1408 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1411 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1412 PolicyError,
"NotAuthorized",
"`%s'",filename);
1413 return(MagickFalse);
1415 quantum=(size_t) MagickMaxBufferExtent;
1416 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1417 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1418 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1419 if (blob == (
unsigned char *) NULL)
1421 file=close_utf8(file);
1422 ThrowFileException(&image->exception,ResourceLimitError,
1423 "MemoryAllocationFailed",filename);
1424 return(MagickFalse);
1428 count=read(file,blob,quantum);
1435 length=(size_t) count;
1436 count=WriteBlobStream(image,length,blob);
1437 if (count != (ssize_t) length)
1439 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1444 file=close_utf8(file);
1446 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1448 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1475MagickExport MagickBooleanType GetBlobError(
const Image *image)
1477 assert(image != (
const Image *) NULL);
1478 assert(image->signature == MagickCoreSignature);
1479 if (IsEventLogging() != MagickFalse)
1480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1481 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1482 errno=image->blob->error_number;
1483 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1508MagickExport FILE *GetBlobFileHandle(
const Image *image)
1510 assert(image != (
const Image *) NULL);
1511 assert(image->signature == MagickCoreSignature);
1512 return(image->blob->file_info.file);
1537MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1539 assert(blob_info != (BlobInfo *) NULL);
1540 (void) memset(blob_info,0,
sizeof(*blob_info));
1541 blob_info->type=UndefinedStream;
1542 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1543 blob_info->properties.st_mtime=GetMagickTime();
1544 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1545 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1546 blob_info->reference_count=1;
1547 blob_info->semaphore=AllocateSemaphoreInfo();
1548 blob_info->signature=MagickCoreSignature;
1573MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1575 assert(image != (Image *) NULL);
1576 assert(image->signature == MagickCoreSignature);
1577 if (IsEventLogging() != MagickFalse)
1578 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1579 return(&image->blob->properties);
1605MagickExport MagickSizeType GetBlobSize(
const Image *image)
1608 *magick_restrict blob_info;
1613 assert(image != (Image *) NULL);
1614 assert(image->signature == MagickCoreSignature);
1615 assert(image->blob != (BlobInfo *) NULL);
1616 if (IsEventLogging() != MagickFalse)
1617 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1618 blob_info=image->blob;
1620 switch (blob_info->type)
1622 case UndefinedStream:
1623 case StandardStream:
1625 extent=blob_info->size;
1633 extent=(MagickSizeType) blob_info->properties.st_size;
1635 extent=blob_info->size;
1636 file_descriptor=fileno(blob_info->file_info.file);
1637 if (file_descriptor == -1)
1639 if (fstat(file_descriptor,&blob_info->properties) == 0)
1640 extent=(MagickSizeType) blob_info->properties.st_size;
1645 extent=blob_info->size;
1654 status=GetPathAttributes(image->filename,&blob_info->properties);
1655 if (status != MagickFalse)
1656 extent=(MagickSizeType) blob_info->properties.st_size;
1663 extent=(MagickSizeType) blob_info->length;
1692MagickExport
unsigned char *GetBlobStreamData(
const Image *image)
1694 assert(image != (
const Image *) NULL);
1695 assert(image->signature == MagickCoreSignature);
1696 return(image->blob->data);
1721MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1723 assert(image != (
const Image *) NULL);
1724 assert(image->signature == MagickCoreSignature);
1725 if (IsEventLogging() != MagickFalse)
1726 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1727 return(image->blob->stream);
1763MagickExport
unsigned char *ImageToBlob(
const ImageInfo *image_info,
1764 Image *image,
size_t *length,ExceptionInfo *exception)
1778 assert(image_info != (
const ImageInfo *) NULL);
1779 assert(image_info->signature == MagickCoreSignature);
1780 assert(image != (Image *) NULL);
1781 assert(image->signature == MagickCoreSignature);
1782 assert(exception != (ExceptionInfo *) NULL);
1783 assert(exception->signature == MagickCoreSignature);
1784 if (IsEventLogging() != MagickFalse)
1785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1786 image_info->filename);
1788 blob=(
unsigned char *) NULL;
1789 blob_info=CloneImageInfo(image_info);
1790 blob_info->adjoin=MagickFalse;
1791 (void) SetImageInfo(blob_info,1,exception);
1792 if (*blob_info->magick !=
'\0')
1793 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
1794 magick_info=GetMagickInfo(image->magick,exception);
1795 if (magick_info == (
const MagickInfo *) NULL)
1797 (void) ThrowMagickException(exception,GetMagickModule(),
1798 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
1800 blob_info=DestroyImageInfo(blob_info);
1803 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
1804 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1809 blob_info->length=0;
1810 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1811 sizeof(
unsigned char));
1812 if (blob_info->blob == NULL)
1813 (void) ThrowMagickException(exception,GetMagickModule(),
1814 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1817 (void) CloseBlob(image);
1818 image->blob->exempt=MagickTrue;
1819 *image->filename=
'\0';
1820 status=WriteImage(blob_info,image);
1821 InheritException(exception,&image->exception);
1822 *length=image->blob->length;
1823 blob=DetachBlob(image->blob);
1824 if (blob != (
void *) NULL)
1826 if (status == MagickFalse)
1827 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1829 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
1830 sizeof(
unsigned char));
1832 else if (status == MagickFalse)
1833 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
1839 unique[MagickPathExtent];
1847 file=AcquireUniqueFileResource(unique);
1850 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
1851 image_info->filename);
1855 blob_info->file=fdopen(file,
"wb");
1856 if (blob_info->file != (FILE *) NULL)
1858 (void) FormatLocaleString(image->filename,MagickPathExtent,
1859 "%s:%s",image->magick,unique);
1860 status=WriteImage(blob_info,image);
1861 (void) fclose(blob_info->file);
1862 if (status == MagickFalse)
1863 InheritException(exception,&image->exception);
1865 blob=FileToBlob(unique,SIZE_MAX,length,exception);
1867 (void) RelinquishUniqueFileResource(unique);
1870 blob_info=DestroyImageInfo(blob_info);
1902MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
1903 ExceptionInfo *exception)
1927 assert(image != (Image *) NULL);
1928 assert(image->signature == MagickCoreSignature);
1929 assert(image->blob != (BlobInfo *) NULL);
1930 assert(image->blob->type != UndefinedStream);
1931 assert(filename != (
const char *) NULL);
1932 if (IsEventLogging() != MagickFalse)
1933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1934 if (*filename ==
'\0')
1935 file=AcquireUniqueFileResource(filename);
1937 if (LocaleCompare(filename,
"-") == 0)
1938 file=fileno(stdout);
1940 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
1943 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1944 return(MagickFalse);
1946 quantum=(size_t) MagickMaxBufferExtent;
1947 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1948 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1949 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
1950 if (buffer == (
unsigned char *) NULL)
1952 file=close_utf8(file)-1;
1953 (void) ThrowMagickException(exception,GetMagickModule(),
1954 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
1955 return(MagickFalse);
1958 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1959 for (i=0; count > 0; )
1961 length=(size_t) count;
1962 for (i=0; i < length; i+=count)
1964 count=write(file,p+i,(
size_t) (length-i));
1974 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1976 if (LocaleCompare(filename,
"-") != 0)
1977 file=close_utf8(file);
1978 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
1979 if ((file == -1) || (i < length))
1982 file=close_utf8(file);
1983 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1984 return(MagickFalse);
2024MagickExport
unsigned char *ImagesToBlob(
const ImageInfo *image_info,
2025 Image *images,
size_t *length,ExceptionInfo *exception)
2039 assert(image_info != (
const ImageInfo *) NULL);
2040 assert(image_info->signature == MagickCoreSignature);
2041 assert(images != (Image *) NULL);
2042 assert(images->signature == MagickCoreSignature);
2043 assert(exception != (ExceptionInfo *) NULL);
2044 if (IsEventLogging() != MagickFalse)
2045 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2046 image_info->filename);
2048 blob=(
unsigned char *) NULL;
2049 blob_info=CloneImageInfo(image_info);
2050 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2052 if (*blob_info->magick !=
'\0')
2053 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2054 magick_info=GetMagickInfo(images->magick,exception);
2055 if (magick_info == (
const MagickInfo *) NULL)
2057 (void) ThrowMagickException(exception,GetMagickModule(),
2058 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2060 blob_info=DestroyImageInfo(blob_info);
2063 if (GetMagickAdjoin(magick_info) == MagickFalse)
2065 blob_info=DestroyImageInfo(blob_info);
2066 return(ImageToBlob(image_info,images,length,exception));
2068 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2069 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2074 blob_info->length=0;
2075 blob_info->blob=(
void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2076 sizeof(
unsigned char));
2077 if (blob_info->blob == (
void *) NULL)
2078 (void) ThrowMagickException(exception,GetMagickModule(),
2079 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2082 (void) CloseBlob(images);
2083 images->blob->exempt=MagickTrue;
2084 *images->filename=
'\0';
2085 status=WriteImages(blob_info,images,images->filename,exception);
2086 *length=images->blob->length;
2087 blob=DetachBlob(images->blob);
2088 if (blob != (
void *) NULL)
2090 if (status == MagickFalse)
2091 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2093 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
2094 sizeof(
unsigned char));
2097 if (status == MagickFalse)
2098 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2104 filename[MagickPathExtent],
2105 unique[MagickPathExtent];
2113 file=AcquireUniqueFileResource(unique);
2116 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2117 image_info->filename);
2121 blob_info->file=fdopen(file,
"wb");
2122 if (blob_info->file != (FILE *) NULL)
2124 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2125 images->magick,unique);
2126 status=WriteImages(blob_info,images,filename,exception);
2127 (void) fclose(blob_info->file);
2128 if (status == MagickFalse)
2129 InheritException(exception,&images->exception);
2131 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2133 (void) RelinquishUniqueFileResource(unique);
2136 blob_info=DestroyImageInfo(blob_info);
2172MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2173 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2176 filename[MagickPathExtent];
2205 assert(image_info != (ImageInfo *) NULL);
2206 assert(image_info->signature == MagickCoreSignature);
2207 assert(image != (Image *) NULL);
2208 assert(image->signature == MagickCoreSignature);
2209 assert(inject_image != (Image *) NULL);
2210 assert(inject_image->signature == MagickCoreSignature);
2211 assert(exception != (ExceptionInfo *) NULL);
2212 if (IsEventLogging() != MagickFalse)
2213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2214 unique_file=(FILE *) NULL;
2215 file=AcquireUniqueFileResource(filename);
2217 unique_file=fdopen(file,
"wb");
2218 if ((file == -1) || (unique_file == (FILE *) NULL))
2220 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2221 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2223 return(MagickFalse);
2225 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2226 if (byte_image == (Image *) NULL)
2228 (void) fclose(unique_file);
2229 (void) RelinquishUniqueFileResource(filename);
2230 return(MagickFalse);
2232 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2234 DestroyBlob(byte_image);
2235 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2236 write_info=CloneImageInfo(image_info);
2237 SetImageInfoFile(write_info,unique_file);
2238 status=WriteImage(write_info,byte_image);
2239 write_info=DestroyImageInfo(write_info);
2240 byte_image=DestroyImage(byte_image);
2241 (void) fclose(unique_file);
2242 if (status == MagickFalse)
2244 (void) RelinquishUniqueFileResource(filename);
2245 return(MagickFalse);
2250 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2253 (void) RelinquishUniqueFileResource(filename);
2254 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2255 image_info->filename);
2256 return(MagickFalse);
2258 quantum=(size_t) MagickMaxBufferExtent;
2259 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2260 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2261 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2262 if (buffer == (
unsigned char *) NULL)
2264 (void) RelinquishUniqueFileResource(filename);
2265 file=close_utf8(file);
2266 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2271 ssize_t count = read(file,buffer,quantum);
2278 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2281 file=close_utf8(file);
2283 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2284 (void) RelinquishUniqueFileResource(filename);
2285 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2311MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2313 assert(image != (
const Image *) NULL);
2314 assert(image->signature == MagickCoreSignature);
2315 if (IsEventLogging() != MagickFalse)
2316 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2317 return(image->blob->exempt);
2342MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2345 *magick_restrict blob_info;
2347 assert(image != (
const Image *) NULL);
2348 assert(image->signature == MagickCoreSignature);
2349 if (IsEventLogging() != MagickFalse)
2350 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2351 blob_info=image->blob;
2352 switch (blob_info->type)
2361 if (blob_info->file_info.file == (FILE *) NULL)
2362 return(MagickFalse);
2363 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2364 return(status == -1 ? MagickFalse : MagickTrue);
2368#if defined(MAGICKCORE_ZLIB_DELEGATE)
2372 if (blob_info->file_info.gzfile == (gzFile) NULL)
2373 return(MagickFalse);
2374 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2375 return(offset < 0 ? MagickFalse : MagickTrue);
2380 case UndefinedStream:
2384 case StandardStream:
2389 return(MagickFalse);
2414MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2416 assert(image != (
const Image *) NULL);
2417 assert(image->signature == MagickCoreSignature);
2418 if (IsEventLogging() != MagickFalse)
2419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2420 return(image->blob->temporary);
2452MagickExport
unsigned char *MapBlob(
int file,
const MapMode mode,
2453 const MagickOffsetType offset,
const size_t length)
2455#if defined(MAGICKCORE_HAVE_MMAP)
2468#if defined(MAP_ANONYMOUS)
2469 flags|=MAP_ANONYMOUS;
2471 return((
unsigned char *) NULL);
2478 protection=PROT_READ;
2484 protection=PROT_WRITE;
2490 protection=PROT_READ | PROT_WRITE;
2495#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2496 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,offset);
2498 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags |
2499 MAP_HUGETLB,file,offset);
2500 if (map == (
unsigned char *) MAP_FAILED)
2501 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,
2504 if (map == (
unsigned char *) MAP_FAILED)
2505 return((
unsigned char *) NULL);
2512 return((
unsigned char *) NULL);
2541MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
2550 assert(buffer != (
unsigned char *) NULL);
2557 *buffer++=(
unsigned char) c;
2561 *buffer++=(
unsigned char) c;
2591MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
2599 assert(p != (
unsigned char *) NULL);
2606 *p++=(
unsigned char) c;
2642static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
2654 size=MagickMinBufferExtent;
2655 option=GetImageOption(image_info,
"stream:buffer-size");
2656 if (option != (
const char *) NULL)
2657 size=StringToUnsignedLong(option);
2658 status=setvbuf(image->blob->file_info.file,(
char *) NULL,size == 0 ?
2659 _IONBF : _IOFBF,size);
2660 return(status == 0 ? MagickTrue : MagickFalse);
2663#if defined(MAGICKCORE_ZLIB_DELEGATE)
2664static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
2666#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2667 return(gzopen(path,mode));
2675 path_wide=NTCreateWidePath(path);
2676 if (path_wide == (
wchar_t *) NULL)
2677 return((gzFile) NULL);
2678 file=gzopen_w(path_wide,mode);
2679 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
2685MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
2686 Image *image,
const BlobMode mode,ExceptionInfo *exception)
2689 *magick_restrict blob_info;
2692 extension[MagickPathExtent],
2693 filename[MagickPathExtent];
2707 assert(image_info != (ImageInfo *) NULL);
2708 assert(image_info->signature == MagickCoreSignature);
2709 assert(image != (Image *) NULL);
2710 assert(image->signature == MagickCoreSignature);
2711 if (IsEventLogging() != MagickFalse)
2712 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2713 image_info->filename);
2714 blob_info=image->blob;
2715 if (image_info->blob != (
void *) NULL)
2717 if (image_info->stream != (StreamHandler) NULL)
2718 blob_info->stream=(StreamHandler) image_info->stream;
2719 AttachBlob(blob_info,image_info->blob,image_info->length);
2722 (void) DetachBlob(blob_info);
2723 blob_info->mode=mode;
2732 case ReadBinaryBlobMode:
2734 flags=O_RDONLY | O_BINARY;
2740 flags=O_WRONLY | O_CREAT | O_TRUNC;
2744 case WriteBinaryBlobMode:
2746 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
2750 case AppendBlobMode:
2752 flags=O_WRONLY | O_CREAT | O_APPEND;
2756 case AppendBinaryBlobMode:
2758 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
2770 blob_info->synchronize=image_info->synchronize;
2771 if (image_info->stream != (StreamHandler) NULL)
2773 blob_info->stream=(StreamHandler) image_info->stream;
2776 blob_info->type=FifoStream;
2784 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2785 rights=ReadPolicyRights;
2787 rights=WritePolicyRights;
2788 if (IsPathAuthorized(rights,filename) == MagickFalse)
2791 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2792 "NotAuthorized",
"`%s'",filename);
2793 return(MagickFalse);
2795 if ((LocaleCompare(filename,
"-") == 0) ||
2796 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
2798 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
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=StandardStream;
2804 blob_info->exempt=MagickTrue;
2805 return(SetStreamBuffering(image_info,image));
2807 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
2808 (IsGeometry(filename+3) != MagickFalse))
2811 fileMode[MagickPathExtent];
2815 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2816 if (blob_info->file_info.file == (FILE *) NULL)
2818 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2819 return(MagickFalse);
2821#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2822 if (strchr(type,
'b') != (
char *) NULL)
2823 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2825 blob_info->type=FileStream;
2826 blob_info->exempt=MagickTrue;
2827 return(SetStreamBuffering(image_info,image));
2829#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2830 if (*filename ==
'|')
2833 fileMode[MagickPathExtent],
2841 (void) signal(SIGPIPE,SIG_IGN);
2845 sanitize_command=SanitizeString(filename+1);
2846 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2848 sanitize_command=DestroyString(sanitize_command);
2849 if (blob_info->file_info.file == (FILE *) NULL)
2851 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2852 return(MagickFalse);
2854 blob_info->type=PipeStream;
2855 blob_info->exempt=MagickTrue;
2856 return(SetStreamBuffering(image_info,image));
2859 status=GetPathAttributes(filename,&blob_info->properties);
2860#if defined(S_ISFIFO)
2861 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2863 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2864 if (blob_info->file_info.file == (FILE *) NULL)
2866 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2867 return(MagickFalse);
2869 blob_info->type=FileStream;
2870 blob_info->exempt=MagickTrue;
2871 return(SetStreamBuffering(image_info,image));
2874 GetPathComponent(image->filename,ExtensionPath,extension);
2877 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2878 if ((image_info->adjoin == MagickFalse) ||
2879 (strchr(filename,
'%') != (
char *) NULL))
2884 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
2885 image->scene,filename);
2886 if ((LocaleCompare(filename,image->filename) == 0) &&
2887 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2888 (GetNextImageInList(image) != (Image *) NULL)))
2891 path[MagickPathExtent];
2893 GetPathComponent(image->filename,RootPath,path);
2894 if (*extension ==
'\0')
2895 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
2896 path,(
double) image->scene);
2898 (
void) FormatLocaleString(filename,MagickPathExtent,
2899 "%s-%.20g.%s",path,(
double) image->scene,extension);
2901 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2902#if defined(macintosh)
2903 SetApplicationType(filename,image_info->magick,
'8BIM');
2906 if (IsPathAuthorized(rights,filename) == MagickFalse)
2909 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2910 "NotAuthorized",
"`%s'",filename);
2911 return(MagickFalse);
2914 if (image_info->file != (FILE *) NULL)
2916 blob_info->file_info.file=image_info->file;
2917 blob_info->type=FileStream;
2918 blob_info->exempt=MagickTrue;
2926 blob_info->file_info.file=(FILE *) NULL;
2927 file=open_utf8(filename,flags,0);
2929 blob_info->file_info.file=fdopen(file,type);
2930 if (blob_info->file_info.file != (FILE *) NULL)
2938 blob_info->type=FileStream;
2939 (void) fstat(fileno(blob_info->file_info.file),
2940 &blob_info->properties);
2941 (void) SetStreamBuffering(image_info,image);
2942 (void) memset(magick,0,
sizeof(magick));
2943 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2944 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2945#if defined(MAGICKCORE_POSIX_SUPPORT)
2946 (void) fflush(blob_info->file_info.file);
2948 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2949 " read %.20g magic header bytes",(
double) count);
2950#if defined(MAGICKCORE_ZLIB_DELEGATE)
2951 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2952 ((
int) magick[2] == 0x08))
2955 gzfile = gzopen_utf8(filename,
"rb");
2957 if (gzfile != (gzFile) NULL)
2959 if (blob_info->file_info.file != (FILE *) NULL)
2960 (void) fclose(blob_info->file_info.file);
2961 blob_info->file_info.file=(FILE *) NULL;
2962 blob_info->file_info.gzfile=gzfile;
2963 blob_info->type=ZipStream;
2967#if defined(MAGICKCORE_BZLIB_DELEGATE)
2968 if (strncmp((
char *) magick,
"BZh",3) == 0)
2971 *bzfile = BZ2_bzopen(filename,
"r");
2973 if (bzfile != (BZFILE *) NULL)
2975 if (blob_info->file_info.file != (FILE *) NULL)
2976 (void) fclose(blob_info->file_info.file);
2977 blob_info->file_info.file=(FILE *) NULL;
2978 blob_info->file_info.bzfile=bzfile;
2979 blob_info->type=BZipStream;
2983 if (blob_info->type == FileStream)
2994 sans_exception=AcquireExceptionInfo();
2995 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2996 sans_exception=DestroyExceptionInfo(sans_exception);
2997 length=(size_t) blob_info->properties.st_size;
2998 if ((magick_info != (
const MagickInfo *) NULL) &&
2999 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3000 (AcquireMagickResource(MapResource,length) != MagickFalse))
3005 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3007 if (blob == (
void *) NULL)
3008 RelinquishMagickResource(MapResource,length);
3014 if (image_info->file != (FILE *) NULL)
3015 blob_info->exempt=MagickFalse;
3018 (void) fclose(blob_info->file_info.file);
3019 blob_info->file_info.file=(FILE *) NULL;
3021 AttachBlob(blob_info,blob,length);
3022 blob_info->mapped=MagickTrue;
3029#if defined(MAGICKCORE_ZLIB_DELEGATE)
3030 if ((LocaleCompare(extension,
"gz") == 0) ||
3031 (LocaleCompare(extension,
"wmz") == 0) ||
3032 (LocaleCompare(extension,
"svgz") == 0))
3034 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3035 if (blob_info->file_info.gzfile != (gzFile) NULL)
3036 blob_info->type=ZipStream;
3040#if defined(MAGICKCORE_BZLIB_DELEGATE)
3041 if (LocaleCompare(extension,
"bz2") == 0)
3043 if (mode == WriteBinaryBlobMode)
3045 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3046 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3047 blob_info->type=BZipStream;
3055 blob_info->file_info.file=(FILE *) NULL;
3056 file=open_utf8(filename,flags,P_MODE);
3058 blob_info->file_info.file=fdopen(file,type);
3059 if (blob_info->file_info.file != (FILE *) NULL)
3061 blob_info->type=FileStream;
3062 (void) SetStreamBuffering(image_info,image);
3065 if (IsPathAuthorized(rights,filename) == MagickFalse)
3068 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3069 "NotAuthorized",
"`%s'",filename);
3070 return(MagickFalse);
3072 blob_info->status=0;
3073 blob_info->error_number=0;
3074 if (blob_info->type != UndefinedStream)
3075 blob_info->size=GetBlobSize(image);
3078 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3079 return(MagickFalse);
3118#if defined(__cplusplus) || defined(c_plusplus)
3122static size_t PingStream(
const Image *magick_unused(image),
3123 const void *magick_unused(pixels),
const size_t columns)
3125 magick_unreferenced(image);
3126 magick_unreferenced(pixels);
3131#if defined(__cplusplus) || defined(c_plusplus)
3135MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3136 const size_t length,ExceptionInfo *exception)
3151 assert(image_info != (ImageInfo *) NULL);
3152 assert(image_info->signature == MagickCoreSignature);
3153 assert(exception != (ExceptionInfo *) NULL);
3154 if (IsEventLogging() != MagickFalse)
3155 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3156 image_info->filename);
3157 if ((blob == (
const void *) NULL) || (length == 0))
3159 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3160 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3161 return((Image *) NULL);
3163 ping_info=CloneImageInfo(image_info);
3164 ping_info->blob=(
void *) blob;
3165 ping_info->length=length;
3166 ping_info->ping=MagickTrue;
3167 if (*ping_info->magick ==
'\0')
3168 (void) SetImageInfo(ping_info,0,exception);
3169 magick_info=GetMagickInfo(ping_info->magick,exception);
3170 if (magick_info == (
const MagickInfo *) NULL)
3172 (void) ThrowMagickException(exception,GetMagickModule(),
3173 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3175 ping_info=DestroyImageInfo(ping_info);
3176 return((Image *) NULL);
3178 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3181 filename[MagickPathExtent];
3186 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3187 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3188 ping_info->magick,filename);
3189 image=ReadStream(ping_info,&PingStream,exception);
3190 if (image != (Image *) NULL)
3191 (void) DetachBlob(image->blob);
3192 ping_info=DestroyImageInfo(ping_info);
3198 ping_info->blob=(
void *) NULL;
3199 ping_info->length=0;
3200 *ping_info->filename=
'\0';
3201 status=BlobToFile(ping_info->filename,blob,length,exception);
3202 if (status == MagickFalse)
3204 (void) RelinquishUniqueFileResource(ping_info->filename);
3205 ping_info=DestroyImageInfo(ping_info);
3206 return((Image *) NULL);
3208 clone_info=CloneImageInfo(ping_info);
3209 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3210 ping_info->magick,ping_info->filename);
3211 image=ReadStream(clone_info,&PingStream,exception);
3212 if (image != (Image *) NULL)
3220 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3222 (void) CopyMagickString(images->filename,image_info->filename,
3224 (void) CopyMagickString(images->magick_filename,image_info->filename,
3226 (void) CopyMagickString(images->magick,magick_info->name,
3228 images=GetNextImageInList(images);
3231 clone_info=DestroyImageInfo(clone_info);
3232 (void) RelinquishUniqueFileResource(ping_info->filename);
3233 ping_info=DestroyImageInfo(ping_info);
3268MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
3269 unsigned char *data)
3272 *magick_restrict blob_info;
3283 assert(image != (Image *) NULL);
3284 assert(image->signature == MagickCoreSignature);
3285 assert(image->blob != (BlobInfo *) NULL);
3286 assert(image->blob->type != UndefinedStream);
3289 assert(data != (
void *) NULL);
3290 blob_info=image->blob;
3293 switch (blob_info->type)
3295 case UndefinedStream:
3297 case StandardStream:
3305 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3310 c=getc(blob_info->file_info.file);
3313 *q++=(
unsigned char) c;
3319 c=getc(blob_info->file_info.file);
3322 *q++=(
unsigned char) c;
3328 c=getc(blob_info->file_info.file);
3331 *q++=(
unsigned char) c;
3337 c=getc(blob_info->file_info.file);
3340 *q++=(
unsigned char) c;
3347 if ((count != (ssize_t) length) &&
3348 (ferror(blob_info->file_info.file) != 0))
3349 ThrowBlobException(blob_info);
3354#if defined(MAGICKCORE_ZLIB_DELEGATE)
3365 for (i=0; i < (ssize_t) length; i+=count)
3367 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3368 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3381 c=gzgetc(blob_info->file_info.gzfile);
3384 *q++=(
unsigned char) c;
3390 c=gzgetc(blob_info->file_info.gzfile);
3393 *q++=(
unsigned char) c;
3399 c=gzgetc(blob_info->file_info.gzfile);
3402 *q++=(
unsigned char) c;
3408 c=gzgetc(blob_info->file_info.gzfile);
3411 *q++=(
unsigned char) c;
3419 (void) gzerror(blob_info->file_info.gzfile,&status);
3420 if ((count != (ssize_t) length) && (status != Z_OK))
3421 ThrowBlobException(blob_info);
3422 if (blob_info->eof == MagickFalse)
3423 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3430#if defined(MAGICKCORE_BZLIB_DELEGATE)
3437 for (i=0; i < (ssize_t) length; i+=count)
3439 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3440 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3450 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3451 if ((count != (ssize_t) length) && (status != BZ_OK))
3452 ThrowBlobException(blob_info);
3463 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3465 blob_info->eof=MagickTrue;
3468 p=blob_info->data+blob_info->offset;
3469 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3470 blob_info->length-blob_info->offset);
3471 blob_info->offset+=count;
3472 if (count != (ssize_t) length)
3473 blob_info->eof=MagickTrue;
3474 (void) memcpy(q,p,(
size_t) count);
3503MagickExport
int ReadBlobByte(Image *image)
3506 *magick_restrict blob_info;
3511 assert(image != (Image *) NULL);
3512 assert(image->signature == MagickCoreSignature);
3513 assert(image->blob != (BlobInfo *) NULL);
3514 assert(image->blob->type != UndefinedStream);
3515 blob_info=image->blob;
3516 switch (blob_info->type)
3518 case StandardStream:
3522 c=getc(blob_info->file_info.file);
3525 if (ferror(blob_info->file_info.file) != 0)
3526 ThrowBlobException(blob_info);
3533 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3535 blob_info->eof=MagickTrue;
3538 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
3539 blob_info->offset++;
3550 count=ReadBlob(image,1,buffer);
3583MagickExport
double ReadBlobDouble(Image *image)
3594 quantum.double_value=0.0;
3595 quantum.unsigned_value=ReadBlobLongLong(image);
3596 return(quantum.double_value);
3622MagickExport
float ReadBlobFloat(Image *image)
3633 quantum.float_value=0.0;
3634 quantum.unsigned_value=ReadBlobLong(image);
3635 return(quantum.float_value);
3661MagickExport
unsigned int ReadBlobLong(Image *image)
3675 assert(image != (Image *) NULL);
3676 assert(image->signature == MagickCoreSignature);
3678 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3681 if (image->endian == LSBEndian)
3683 value=(
unsigned int) (*p++);
3684 value|=(
unsigned int) (*p++) << 8;
3685 value|=(
unsigned int) (*p++) << 16;
3686 value|=(
unsigned int) (*p++) << 24;
3689 value=(
unsigned int) (*p++) << 24;
3690 value|=(
unsigned int) (*p++) << 16;
3691 value|=(
unsigned int) (*p++) << 8;
3692 value|=(
unsigned int) (*p++);
3719MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3733 assert(image != (Image *) NULL);
3734 assert(image->signature == MagickCoreSignature);
3736 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3738 return(MagickULLConstant(0));
3739 if (image->endian == LSBEndian)
3741 value=(MagickSizeType) (*p++);
3742 value|=(MagickSizeType) (*p++) << 8;
3743 value|=(MagickSizeType) (*p++) << 16;
3744 value|=(MagickSizeType) (*p++) << 24;
3745 value|=(MagickSizeType) (*p++) << 32;
3746 value|=(MagickSizeType) (*p++) << 40;
3747 value|=(MagickSizeType) (*p++) << 48;
3748 value|=(MagickSizeType) (*p++) << 56;
3751 value=(MagickSizeType) (*p++) << 56;
3752 value|=(MagickSizeType) (*p++) << 48;
3753 value|=(MagickSizeType) (*p++) << 40;
3754 value|=(MagickSizeType) (*p++) << 32;
3755 value|=(MagickSizeType) (*p++) << 24;
3756 value|=(MagickSizeType) (*p++) << 16;
3757 value|=(MagickSizeType) (*p++) << 8;
3758 value|=(MagickSizeType) (*p++);
3785MagickExport
unsigned short ReadBlobShort(Image *image)
3799 assert(image != (Image *) NULL);
3800 assert(image->signature == MagickCoreSignature);
3802 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3804 return((
unsigned short) 0U);
3805 if (image->endian == LSBEndian)
3807 value=(
unsigned short) (*p++);
3808 value|=(
unsigned short) (*p++) << 8;
3811 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3812 value|=(
unsigned short) (*p++);
3839MagickExport
unsigned int ReadBlobLSBLong(Image *image)
3853 assert(image != (Image *) NULL);
3854 assert(image->signature == MagickCoreSignature);
3856 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3859 value=(
unsigned int) (*p++);
3860 value|=(
unsigned int) (*p++) << 8;
3861 value|=(
unsigned int) (*p++) << 16;
3862 value|=(
unsigned int) (*p++) << 24;
3889MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
3900 quantum.unsigned_value=ReadBlobLSBLong(image);
3901 return(quantum.signed_value);
3927MagickExport
unsigned short ReadBlobLSBShort(Image *image)
3941 assert(image != (Image *) NULL);
3942 assert(image->signature == MagickCoreSignature);
3944 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3946 return((
unsigned short) 0U);
3947 value=(
unsigned short) (*p++);
3948 value|=(
unsigned short) (*p++) << 8;
3975MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
3986 quantum.unsigned_value=ReadBlobLSBShort(image);
3987 return(quantum.signed_value);
4013MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4027 assert(image != (Image *) NULL);
4028 assert(image->signature == MagickCoreSignature);
4030 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4033 value=(
unsigned int) (*p++) << 24;
4034 value|=(
unsigned int) (*p++) << 16;
4035 value|=(
unsigned int) (*p++) << 8;
4036 value|=(
unsigned int) (*p++);
4063MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4077 assert(image != (Image *) NULL);
4078 assert(image->signature == MagickCoreSignature);
4080 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4082 return(MagickULLConstant(0));
4083 value=(MagickSizeType) (*p++) << 56;
4084 value|=(MagickSizeType) (*p++) << 48;
4085 value|=(MagickSizeType) (*p++) << 40;
4086 value|=(MagickSizeType) (*p++) << 32;
4087 value|=(MagickSizeType) (*p++) << 24;
4088 value|=(MagickSizeType) (*p++) << 16;
4089 value|=(MagickSizeType) (*p++) << 8;
4090 value|=(MagickSizeType) (*p++);
4117MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4131 assert(image != (Image *) NULL);
4132 assert(image->signature == MagickCoreSignature);
4134 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4136 return((
unsigned short) 0U);
4137 value=(
unsigned short) ((*p++) << 8);
4138 value|=(
unsigned short) (*p++);
4165MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4176 quantum.unsigned_value=ReadBlobMSBLong(image);
4177 return(quantum.signed_value);
4203MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4214 quantum.unsigned_value=ReadBlobMSBShort(image);
4215 return(quantum.signed_value);
4241MagickExport
signed int ReadBlobSignedLong(Image *image)
4252 quantum.unsigned_value=ReadBlobLong(image);
4253 return(quantum.signed_value);
4279MagickExport
signed short ReadBlobSignedShort(Image *image)
4290 quantum.unsigned_value=ReadBlobShort(image);
4291 return(quantum.signed_value);
4329MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4330 const size_t length,
void *magick_restrict data,ssize_t *count)
4333 *magick_restrict blob_info;
4335 assert(image != (Image *) NULL);
4336 assert(image->signature == MagickCoreSignature);
4337 assert(image->blob != (BlobInfo *) NULL);
4338 assert(image->blob->type != UndefinedStream);
4339 assert(count != (ssize_t *) NULL);
4340 blob_info=image->blob;
4341 if (blob_info->type != BlobStream)
4343 assert(data != NULL);
4344 *count=ReadBlob(image,length,(
unsigned char *) data);
4347 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4350 blob_info->eof=MagickTrue;
4353 data=blob_info->data+blob_info->offset;
4354 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4355 blob_info->length-blob_info->offset);
4356 blob_info->offset+=(*count);
4357 if (*count != (ssize_t) length)
4358 blob_info->eof=MagickTrue;
4387MagickExport
char *ReadBlobString(Image *image,
char *
string)
4390 *magick_restrict blob_info;
4398 assert(image != (Image *) NULL);
4399 assert(image->signature == MagickCoreSignature);
4400 assert(image->blob != (BlobInfo *) NULL);
4401 assert(image->blob->type != UndefinedStream);
4402 if (IsEventLogging() != MagickFalse)
4403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4405 blob_info=image->blob;
4406 switch (blob_info->type)
4408 case UndefinedStream:
4410 case StandardStream:
4413 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4414 if (p == (
char *) NULL)
4416 if (ferror(blob_info->file_info.file) != 0)
4417 ThrowBlobException(blob_info);
4418 return((
char *) NULL);
4425#if defined(MAGICKCORE_ZLIB_DELEGATE)
4426 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4427 if (p == (
char *) NULL)
4430 (void) gzerror(blob_info->file_info.gzfile,&status);
4432 ThrowBlobException(blob_info);
4433 return((
char *) NULL);
4443 c=ReadBlobByte(image);
4446 blob_info->eof=MagickTrue;
4452 }
while (i < (MaxTextExtent-2));
4460 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4463 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4465 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4466 return((
char *) NULL);
4493MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4495 assert(blob != (BlobInfo *) NULL);
4496 assert(blob->signature == MagickCoreSignature);
4497 if (IsEventLogging() != MagickFalse)
4498 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4499 LockSemaphoreInfo(blob->semaphore);
4500 blob->reference_count++;
4501 UnlockSemaphoreInfo(blob->semaphore);
4538MagickExport MagickOffsetType SeekBlob(Image *image,
4539 const MagickOffsetType offset,
const int whence)
4542 *magick_restrict blob_info;
4544 assert(image != (Image *) NULL);
4545 assert(image->signature == MagickCoreSignature);
4546 assert(image->blob != (BlobInfo *) NULL);
4547 assert(image->blob->type != UndefinedStream);
4548 if (IsEventLogging() != MagickFalse)
4549 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4550 blob_info=image->blob;
4551 switch (blob_info->type)
4553 case UndefinedStream:
4555 case StandardStream:
4560 if ((offset < 0) && (whence == SEEK_SET))
4562 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4564 blob_info->offset=TellBlob(image);
4569#if defined(MAGICKCORE_ZLIB_DELEGATE)
4570 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4573 blob_info->offset=TellBlob(image);
4589 blob_info->offset=offset;
4594 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4595 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4600 if ((blob_info->offset+offset) < 0)
4602 blob_info->offset+=offset;
4607 if (((MagickOffsetType) blob_info->length+offset) < 0)
4609 blob_info->offset=blob_info->length+offset;
4613 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4615 blob_info->eof=MagickFalse;
4621 return(blob_info->offset);
4649MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
4651 assert(image != (
const Image *) NULL);
4652 assert(image->signature == MagickCoreSignature);
4653 if (IsEventLogging() != MagickFalse)
4654 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4655 image->blob->exempt=exempt;
4684MagickExport MagickBooleanType SetBlobExtent(Image *image,
4685 const MagickSizeType extent)
4688 *magick_restrict blob_info;
4690 assert(image != (Image *) NULL);
4691 assert(image->signature == MagickCoreSignature);
4692 assert(image->blob != (BlobInfo *) NULL);
4693 assert(image->blob->type != UndefinedStream);
4694 if (IsEventLogging() != MagickFalse)
4695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4696 blob_info=image->blob;
4697 switch (blob_info->type)
4699 case UndefinedStream:
4701 case StandardStream:
4702 return(MagickFalse);
4711 if (extent != (MagickSizeType) ((off_t) extent))
4712 return(MagickFalse);
4713 offset=SeekBlob(image,0,SEEK_END);
4715 return(MagickFalse);
4716 if ((MagickSizeType) offset >= extent)
4718 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4721 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4722 blob_info->file_info.file);
4723#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4724 if (blob_info->synchronize != MagickFalse)
4729 file=fileno(blob_info->file_info.file);
4730 if ((file == -1) || (offset < 0))
4731 return(MagickFalse);
4732 (void) posix_fallocate(file,offset,extent-offset);
4735 offset=SeekBlob(image,offset,SEEK_SET);
4737 return(MagickFalse);
4742 return(MagickFalse);
4744 return(MagickFalse);
4746 return(MagickFalse);
4749 if (extent != (MagickSizeType) ((
size_t) extent))
4750 return(MagickFalse);
4751 if (blob_info->mapped != MagickFalse)
4759 (void) UnmapBlob(blob_info->data,blob_info->length);
4760 RelinquishMagickResource(MapResource,blob_info->length);
4761 if (extent != (MagickSizeType) ((off_t) extent))
4762 return(MagickFalse);
4763 offset=SeekBlob(image,0,SEEK_END);
4765 return(MagickFalse);
4766 if ((MagickSizeType) offset >= extent)
4768 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4769 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4770 blob_info->file_info.file);
4771#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4772 if (blob_info->synchronize != MagickFalse)
4777 file=fileno(blob_info->file_info.file);
4778 if ((file == -1) || (offset < 0))
4779 return(MagickFalse);
4780 (void) posix_fallocate(file,offset,extent-offset);
4783 offset=SeekBlob(image,offset,SEEK_SET);
4785 return(MagickFalse);
4786 (void) AcquireMagickResource(MapResource,extent);
4787 blob_info->data=(
unsigned char*) MapBlob(fileno(
4788 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
4789 blob_info->extent=(size_t) extent;
4790 blob_info->length=(size_t) extent;
4791 (void) SyncBlob(image);
4794 blob_info->extent=(size_t) extent;
4795 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4796 blob_info->extent+1,
sizeof(*blob_info->data));
4797 (void) SyncBlob(image);
4798 if (blob_info->data == (
unsigned char *) NULL)
4800 (void) DetachBlob(blob_info);
4801 return(MagickFalse);
4833static int SyncBlob(
const Image *image)
4836 *magick_restrict blob_info;
4841 assert(image != (Image *) NULL);
4842 assert(image->signature == MagickCoreSignature);
4843 assert(image->blob != (BlobInfo *) NULL);
4844 if (IsEventLogging() != MagickFalse)
4845 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4846 if (EOFBlob(image) != 0)
4848 blob_info=image->blob;
4850 switch (blob_info->type)
4852 case UndefinedStream:
4853 case StandardStream:
4858 status=fflush(blob_info->file_info.file);
4863#if defined(MAGICKCORE_ZLIB_DELEGATE)
4864 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4870#if defined(MAGICKCORE_BZLIB_DELEGATE)
4871 status=BZ2_bzflush(blob_info->file_info.bzfile);
4905MagickExport MagickOffsetType TellBlob(
const Image *image)
4908 *magick_restrict blob_info;
4913 assert(image != (Image *) NULL);
4914 assert(image->signature == MagickCoreSignature);
4915 assert(image->blob != (BlobInfo *) NULL);
4916 assert(image->blob->type != UndefinedStream);
4917 if (IsEventLogging() != MagickFalse)
4918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4919 blob_info=image->blob;
4921 switch (blob_info->type)
4923 case UndefinedStream:
4924 case StandardStream:
4928 offset=ftell(blob_info->file_info.file);
4935#if defined(MAGICKCORE_ZLIB_DELEGATE)
4936 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4946 offset=blob_info->offset;
4978MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
4980#if defined(MAGICKCORE_HAVE_MMAP)
4984 status=munmap(map,length);
4985 return(status == -1 ? MagickFalse : MagickTrue);
4989 return(MagickFalse);
5022MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5023 const unsigned char *data)
5026 *magick_restrict blob_info;
5040 assert(image != (Image *) NULL);
5041 assert(image->signature == MagickCoreSignature);
5042 assert(image->blob != (BlobInfo *) NULL);
5043 assert(image->blob->type != UndefinedStream);
5046 assert(data != (
const unsigned char *) NULL);
5047 blob_info=image->blob;
5049 p=(
const unsigned char *) data;
5050 q=(
unsigned char *) data;
5051 switch (blob_info->type)
5053 case UndefinedStream:
5055 case StandardStream:
5063 count=(ssize_t) fwrite((
const char *) data,1,length,
5064 blob_info->file_info.file);
5069 c=putc((
int) *p++,blob_info->file_info.file);
5077 c=putc((
int) *p++,blob_info->file_info.file);
5085 c=putc((
int) *p++,blob_info->file_info.file);
5093 c=putc((
int) *p++,blob_info->file_info.file);
5102 if ((count != (ssize_t) length) &&
5103 (ferror(blob_info->file_info.file) != 0))
5104 ThrowBlobException(blob_info);
5109#if defined(MAGICKCORE_ZLIB_DELEGATE)
5120 for (i=0; i < (ssize_t) length; i+=count)
5122 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5123 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5136 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5144 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5152 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5160 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5170 (void) gzerror(blob_info->file_info.gzfile,&status);
5171 if ((count != (ssize_t) length) && (status != Z_OK))
5172 ThrowBlobException(blob_info);
5178#if defined(MAGICKCORE_BZLIB_DELEGATE)
5185 for (i=0; i < (ssize_t) length; i+=count)
5187 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5188 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5198 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5199 if ((count != (ssize_t) length) && (status != BZ_OK))
5200 ThrowBlobException(blob_info);
5206 count=(ssize_t) blob_info->stream(image,data,length);
5214 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5219 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5220 if (extent >= blob_info->extent)
5222 extent+=blob_info->quantum+length;
5223 blob_info->quantum<<=1;
5224 if (SetBlobExtent(image,extent) == MagickFalse)
5227 q=blob_info->data+blob_info->offset;
5228 (void) memcpy(q,p,length);
5229 blob_info->offset+=length;
5230 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5231 blob_info->length=(size_t) blob_info->offset;
5232 count=(ssize_t) length;
5235 if (count != (ssize_t) length)
5236 ThrowBlobException(blob_info);
5265MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
5268 *magick_restrict blob_info;
5273 assert(image != (Image *) NULL);
5274 assert(image->signature == MagickCoreSignature);
5275 assert(image->blob != (BlobInfo *) NULL);
5276 assert(image->blob->type != UndefinedStream);
5277 blob_info=image->blob;
5279 switch (blob_info->type)
5281 case StandardStream:
5288 c=putc((
int) value,blob_info->file_info.file);
5291 if (ferror(blob_info->file_info.file) != 0)
5292 ThrowBlobException(blob_info);
5300 count=WriteBlobStream(image,1,&value);
5332MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
5343 quantum.unsigned_value=0U;
5344 quantum.float_value=value;
5345 return(WriteBlobLong(image,quantum.unsigned_value));
5373MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
5378 assert(image != (Image *) NULL);
5379 assert(image->signature == MagickCoreSignature);
5380 if (image->endian == LSBEndian)
5382 buffer[0]=(
unsigned char) value;
5383 buffer[1]=(
unsigned char) (value >> 8);
5384 buffer[2]=(
unsigned char) (value >> 16);
5385 buffer[3]=(
unsigned char) (value >> 24);
5386 return(WriteBlobStream(image,4,buffer));
5388 buffer[0]=(
unsigned char) (value >> 24);
5389 buffer[1]=(
unsigned char) (value >> 16);
5390 buffer[2]=(
unsigned char) (value >> 8);
5391 buffer[3]=(
unsigned char) value;
5392 return(WriteBlobStream(image,4,buffer));
5420MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
5425 assert(image != (Image *) NULL);
5426 assert(image->signature == MagickCoreSignature);
5427 if (image->endian == LSBEndian)
5429 buffer[0]=(
unsigned char) value;
5430 buffer[1]=(
unsigned char) (value >> 8);
5431 return(WriteBlobStream(image,2,buffer));
5433 buffer[0]=(
unsigned char) (value >> 8);
5434 buffer[1]=(
unsigned char) value;
5435 return(WriteBlobStream(image,2,buffer));
5463MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
5468 assert(image != (Image *) NULL);
5469 assert(image->signature == MagickCoreSignature);
5470 buffer[0]=(
unsigned char) value;
5471 buffer[1]=(
unsigned char) (value >> 8);
5472 buffer[2]=(
unsigned char) (value >> 16);
5473 buffer[3]=(
unsigned char) (value >> 24);
5474 return(WriteBlobStream(image,4,buffer));
5502MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
5507 assert(image != (Image *) NULL);
5508 assert(image->signature == MagickCoreSignature);
5509 buffer[0]=(
unsigned char) value;
5510 buffer[1]=(
unsigned char) (value >> 8);
5511 return(WriteBlobStream(image,2,buffer));
5539MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
5553 assert(image != (Image *) NULL);
5554 assert(image->signature == MagickCoreSignature);
5555 quantum.signed_value=value;
5556 buffer[0]=(
unsigned char) quantum.unsigned_value;
5557 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5558 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5559 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5560 return(WriteBlobStream(image,4,buffer));
5588MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5589 const signed short value)
5603 assert(image != (Image *) NULL);
5604 assert(image->signature == MagickCoreSignature);
5605 quantum.signed_value=value;
5606 buffer[0]=(
unsigned char) quantum.unsigned_value;
5607 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5608 return(WriteBlobStream(image,2,buffer));
5636MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
5641 assert(image != (Image *) NULL);
5642 assert(image->signature == MagickCoreSignature);
5643 buffer[0]=(
unsigned char) (value >> 24);
5644 buffer[1]=(
unsigned char) (value >> 16);
5645 buffer[2]=(
unsigned char) (value >> 8);
5646 buffer[3]=(
unsigned char) value;
5647 return(WriteBlobStream(image,4,buffer));
5675MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5676 const MagickSizeType value)
5681 assert(image != (Image *) NULL);
5682 assert(image->signature == MagickCoreSignature);
5683 buffer[0]=(
unsigned char) (value >> 56);
5684 buffer[1]=(
unsigned char) (value >> 48);
5685 buffer[2]=(
unsigned char) (value >> 40);
5686 buffer[3]=(
unsigned char) (value >> 32);
5687 buffer[4]=(
unsigned char) (value >> 24);
5688 buffer[5]=(
unsigned char) (value >> 16);
5689 buffer[6]=(
unsigned char) (value >> 8);
5690 buffer[7]=(
unsigned char) value;
5691 return(WriteBlobStream(image,8,buffer));
5719MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
5724 assert(image != (Image *) NULL);
5725 assert(image->signature == MagickCoreSignature);
5726 buffer[0]=(
unsigned char) (value >> 8);
5727 buffer[1]=(
unsigned char) value;
5728 return(WriteBlobStream(image,2,buffer));
5756MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,
const signed int value)
5770 assert(image != (Image *) NULL);
5771 assert(image->signature == MagickCoreSignature);
5772 quantum.signed_value=value;
5773 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5774 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5775 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5776 buffer[3]=(
unsigned char) quantum.unsigned_value;
5777 return(WriteBlobStream(image,4,buffer));
5805MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5806 const signed short value)
5820 assert(image != (Image *) NULL);
5821 assert(image->signature == MagickCoreSignature);
5822 quantum.signed_value=value;
5823 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5824 buffer[1]=(
unsigned char) quantum.unsigned_value;
5825 return(WriteBlobStream(image,2,buffer));
5853MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
5855 assert(image != (Image *) NULL);
5856 assert(image->signature == MagickCoreSignature);
5857 assert(
string != (
const char *) NULL);
5858 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));