47#include "magick/studio.h"
48#include "magick/blob.h"
49#include "magick/blob-private.h"
50#include "magick/cache.h"
51#include "magick/client.h"
52#include "magick/constitute.h"
53#include "magick/delegate.h"
54#include "magick/exception.h"
55#include "magick/exception-private.h"
56#include "magick/geometry.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/locale_.h"
60#include "magick/log.h"
61#include "magick/magick.h"
62#include "magick/memory_.h"
63#include "magick/nt-base-private.h"
64#include "magick/option.h"
65#include "magick/policy.h"
66#include "magick/resource_.h"
67#include "magick/semaphore.h"
68#include "magick/string_.h"
69#include "magick/string-private.h"
70#include "magick/timer-private.h"
71#include "magick/token.h"
72#include "magick/utility.h"
73#include "magick/utility-private.h"
74#if defined(MAGICKCORE_ZLIB_DELEGATE)
77#if defined(MAGICKCORE_BZLIB_DELEGATE)
84#define IsPathAuthorized(rights,filename) \
85 ((IsRightsAuthorized(PathPolicyDomain,rights,filename) != MagickFalse) && \
86 ((IsRightsAuthorized(SystemPolicyDomain,rights,"symlink::follow") != MagickFalse) || \
87 (is_symlink_utf8(filename) == MagickFalse)))
88#define MagickMaxBlobExtent (8*8192)
89#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
90# define MAP_ANONYMOUS MAP_ANON
92#if !defined(MAP_FAILED)
93#define MAP_FAILED ((void *) -1)
97#define _O_BINARY O_BINARY
99#if defined(MAGICKCORE_WINDOWS_SUPPORT)
101# define fsync _commit
104# define MAGICKCORE_HAVE_MMAP 1
105# define mmap(address,length,protection,access,file,offset) \
106 NTMapMemory(address,length,protection,access,file,offset)
109# define munmap(address,length) NTUnmapMemory(address,length)
112# define pclose _pclose
127#if defined(MAGICKCORE_ZLIB_DELEGATE)
132#if defined(MAGICKCORE_BZLIB_DELEGATE)
202 SyncBlob(
const Image *);
231MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
234 assert(blob_info != (BlobInfo *) NULL);
235 if (IsEventLogging() != MagickFalse)
236 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
237 blob_info->length=length;
238 blob_info->extent=length;
239 blob_info->quantum=(size_t) MagickMaxBlobExtent;
241 blob_info->type=BlobStream;
242 blob_info->file_info.file=(FILE *) NULL;
243 blob_info->data=(
unsigned char *) blob;
244 blob_info->mapped=MagickFalse;
278MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
279 const size_t length,ExceptionInfo *exception)
290 assert(filename != (
const char *) NULL);
291 assert(blob != (
const void *) NULL);
292 if (IsEventLogging() != MagickFalse)
293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
294 if (*filename ==
'\0')
295 file=AcquireUniqueFileResource(filename);
297 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
300 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
303 for (i=0; i < length; i+=(size_t) count)
305 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
306 MagickMaxBufferExtent));
314 file=close_utf8(file);
315 if ((file == -1) || (i < length))
317 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
354MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
355 const size_t length,ExceptionInfo *exception)
370 assert(image_info != (ImageInfo *) NULL);
371 assert(image_info->signature == MagickCoreSignature);
372 assert(exception != (ExceptionInfo *) NULL);
373 if (IsEventLogging() != MagickFalse)
374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
375 image_info->filename);
376 if ((blob == (
const void *) NULL) || (length == 0))
378 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
379 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
380 return((Image *) NULL);
382 blob_info=CloneImageInfo(image_info);
383 blob_info->blob=(
void *) blob;
384 blob_info->length=length;
385 if (*blob_info->magick ==
'\0')
386 (void) SetImageInfo(blob_info,0,exception);
387 magick_info=GetMagickInfo(blob_info->magick,exception);
388 if (magick_info == (
const MagickInfo *) NULL)
390 (void) ThrowMagickException(exception,GetMagickModule(),
391 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
393 blob_info=DestroyImageInfo(blob_info);
394 return((Image *) NULL);
396 if (GetMagickBlobSupport(magick_info) != MagickFalse)
399 filename[MagickPathExtent];
404 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
405 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
406 blob_info->magick,filename);
407 image=ReadImage(blob_info,exception);
408 if (image != (Image *) NULL)
409 (void) DetachBlob(image->blob);
410 blob_info=DestroyImageInfo(blob_info);
416 blob_info->blob=(
void *) NULL;
418 *blob_info->filename=
'\0';
419 status=BlobToFile(blob_info->filename,blob,length,exception);
420 if (status == MagickFalse)
422 (void) RelinquishUniqueFileResource(blob_info->filename);
423 blob_info=DestroyImageInfo(blob_info);
424 return((Image *) NULL);
426 clone_info=CloneImageInfo(blob_info);
427 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
428 blob_info->magick,blob_info->filename);
429 image=ReadImage(clone_info,exception);
430 if (image != (Image *) NULL)
438 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
440 (void) CopyMagickString(images->filename,image_info->filename,
442 (void) CopyMagickString(images->magick_filename,image_info->filename,
444 (void) CopyMagickString(images->magick,magick_info->name,
446 images=GetNextImageInList(images);
449 clone_info=DestroyImageInfo(clone_info);
450 (void) RelinquishUniqueFileResource(blob_info->filename);
451 blob_info=DestroyImageInfo(blob_info);
478MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
486 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
487 GetBlobInfo(clone_info);
488 if (blob_info == (BlobInfo *) NULL)
490 semaphore=clone_info->semaphore;
491 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
492 if (blob_info->mapped != MagickFalse)
493 (void) AcquireMagickResource(MapResource,blob_info->length);
494 clone_info->semaphore=semaphore;
495 LockSemaphoreInfo(clone_info->semaphore);
496 clone_info->reference_count=1;
497 UnlockSemaphoreInfo(clone_info->semaphore);
524static inline void ThrowBlobException(BlobInfo *blob_info)
526 if ((blob_info->status == 0) && (errno != 0))
527 blob_info->error_number=errno;
528 blob_info->status=(-1);
531MagickExport MagickBooleanType CloseBlob(Image *image)
534 *magick_restrict blob_info;
542 assert(image != (Image *) NULL);
543 assert(image->signature == MagickCoreSignature);
544 if (IsEventLogging() != MagickFalse)
545 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
546 blob_info=image->blob;
547 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
549 (void) SyncBlob(image);
550 status=blob_info->status;
551 switch (blob_info->type)
553 case UndefinedStream:
559 if (blob_info->synchronize != MagickFalse)
561 status=fflush(blob_info->file_info.file);
563 ThrowBlobException(blob_info);
564 status=fsync(fileno(blob_info->file_info.file));
566 ThrowBlobException(blob_info);
568 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
569 ThrowBlobException(blob_info);
574#if defined(MAGICKCORE_ZLIB_DELEGATE)
576 (void) gzerror(blob_info->file_info.gzfile,&status);
578 ThrowBlobException(blob_info);
584#if defined(MAGICKCORE_BZLIB_DELEGATE)
586 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
588 ThrowBlobException(blob_info);
596 if (blob_info->file_info.file != (FILE *) NULL)
598 if (blob_info->synchronize != MagickFalse)
600 status=fflush(blob_info->file_info.file);
602 ThrowBlobException(blob_info);
603 status=fsync(fileno(blob_info->file_info.file));
605 ThrowBlobException(blob_info);
607 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
608 ThrowBlobException(blob_info);
613 blob_info->size=GetBlobSize(image);
614 image->extent=blob_info->size;
615 blob_info->eof=MagickFalse;
617 blob_info->mode=UndefinedBlobMode;
618 if (blob_info->exempt != MagickFalse)
620 blob_info->type=UndefinedStream;
621 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
623 switch (blob_info->type)
625 case UndefinedStream:
630 if (blob_info->file_info.file != (FILE *) NULL)
632 status=fclose(blob_info->file_info.file);
634 ThrowBlobException(blob_info);
640#if defined(MAGICKCORE_HAVE_PCLOSE)
641 status=pclose(blob_info->file_info.file);
643 ThrowBlobException(blob_info);
649#if defined(MAGICKCORE_ZLIB_DELEGATE)
650 status=gzclose(blob_info->file_info.gzfile);
652 ThrowBlobException(blob_info);
658#if defined(MAGICKCORE_BZLIB_DELEGATE)
659 BZ2_bzclose(blob_info->file_info.bzfile);
667 if (blob_info->file_info.file != (FILE *) NULL)
669 status=fclose(blob_info->file_info.file);
671 ThrowBlobException(blob_info);
676 (void) DetachBlob(blob_info);
677 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
702MagickExport
void DestroyBlob(Image *image)
705 *magick_restrict blob_info;
710 assert(image != (Image *) NULL);
711 assert(image->signature == MagickCoreSignature);
712 assert(image->blob != (BlobInfo *) NULL);
713 assert(image->blob->signature == MagickCoreSignature);
714 if (IsEventLogging() != MagickFalse)
715 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
716 blob_info=image->blob;
718 LockSemaphoreInfo(blob_info->semaphore);
719 blob_info->reference_count--;
720 assert(blob_info->reference_count >= 0);
721 if (blob_info->reference_count == 0)
723 UnlockSemaphoreInfo(blob_info->semaphore);
724 if (destroy == MagickFalse)
726 image->blob=(BlobInfo *) NULL;
729 (void) CloseBlob(image);
730 if (blob_info->mapped != MagickFalse)
732 (void) UnmapBlob(blob_info->data,blob_info->length);
733 RelinquishMagickResource(MapResource,blob_info->length);
736 DestroySemaphoreInfo(&blob_info->semaphore);
737 blob_info->signature=(~MagickCoreSignature);
738 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
763MagickExport
unsigned char *DetachBlob(BlobInfo *blob_info)
768 assert(blob_info != (BlobInfo *) NULL);
769 if (IsEventLogging() != MagickFalse)
770 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
771 if (blob_info->mapped != MagickFalse)
773 (void) UnmapBlob(blob_info->data,blob_info->length);
774 blob_info->data=NULL;
775 RelinquishMagickResource(MapResource,blob_info->length);
777 blob_info->mapped=MagickFalse;
780 blob_info->mode=UndefinedBlobMode;
781 blob_info->eof=MagickFalse;
783 blob_info->exempt=MagickFalse;
784 blob_info->type=UndefinedStream;
785 blob_info->file_info.file=(FILE *) NULL;
786 data=blob_info->data;
787 blob_info->data=(
unsigned char *) NULL;
788 blob_info->stream=(StreamHandler) NULL;
816MagickPrivate
void DisassociateBlob(Image *image)
819 *magick_restrict blob_info,
825 assert(image != (Image *) NULL);
826 assert(image->signature == MagickCoreSignature);
827 assert(image->blob != (BlobInfo *) NULL);
828 assert(image->blob->signature == MagickCoreSignature);
829 if (IsEventLogging() != MagickFalse)
830 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
831 blob_info=image->blob;
833 LockSemaphoreInfo(blob_info->semaphore);
834 assert(blob_info->reference_count >= 0);
835 if (blob_info->reference_count > 1)
837 UnlockSemaphoreInfo(blob_info->semaphore);
838 if (clone == MagickFalse)
840 clone_info=CloneBlobInfo(blob_info);
842 image->blob=clone_info;
870MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
871 const MagickSizeType length)
883 buffer[MagickMinBufferExtent >> 1];
885 assert(image != (Image *) NULL);
886 assert(image->signature == MagickCoreSignature);
887 if (length != (MagickSizeType) ((MagickOffsetType) length))
890 for (i=0; i < length; i+=(MagickSizeType) count)
892 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
893 (void) ReadBlobStream(image,quantum,buffer,&count);
901 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
928MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
930 assert(image != (Image *) NULL);
931 assert(image->signature == MagickCoreSignature);
932 assert(duplicate != (Image *) NULL);
933 assert(duplicate->signature == MagickCoreSignature);
934 if (IsEventLogging() != MagickFalse)
935 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
937 image->blob=ReferenceBlob(duplicate->blob);
963MagickExport
int EOFBlob(
const Image *image)
966 *magick_restrict blob_info;
968 assert(image != (Image *) NULL);
969 assert(image->signature == MagickCoreSignature);
970 assert(image->blob != (BlobInfo *) NULL);
971 assert(image->blob->type != UndefinedStream);
972 if (IsEventLogging() != MagickFalse)
973 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
974 blob_info=image->blob;
975 switch (blob_info->type)
977 case UndefinedStream:
983 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
989#if defined(MAGICKCORE_ZLIB_DELEGATE)
990 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
997#if defined(MAGICKCORE_BZLIB_DELEGATE)
1002 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1003 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1009 blob_info->eof=MagickFalse;
1015 return((
int) blob_info->eof);
1041MagickExport
int ErrorBlob(
const Image *image)
1044 *magick_restrict blob_info;
1046 assert(image != (Image *) NULL);
1047 assert(image->signature == MagickCoreSignature);
1048 assert(image->blob != (BlobInfo *) NULL);
1049 assert(image->blob->type != UndefinedStream);
1050 if (IsEventLogging() != MagickFalse)
1051 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1052 blob_info=image->blob;
1053 switch (blob_info->type)
1055 case UndefinedStream:
1056 case StandardStream:
1061 blob_info->error=ferror(blob_info->file_info.file);
1066#if defined(MAGICKCORE_ZLIB_DELEGATE)
1067 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1073#if defined(MAGICKCORE_BZLIB_DELEGATE)
1074 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1086 return(blob_info->error);
1124MagickExport
unsigned char *FileToBlob(
const char *filename,
const size_t extent,
1125 size_t *length,ExceptionInfo *exception)
1151 assert(filename != (
const char *) NULL);
1152 assert(exception != (ExceptionInfo *) NULL);
1153 assert(exception->signature == MagickCoreSignature);
1154 if (IsEventLogging() != MagickFalse)
1155 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1157 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1160 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1161 "NotAuthorized",
"`%s'",filename);
1165 if (LocaleCompare(filename,
"-") != 0)
1168 flags = O_RDONLY | O_BINARY;
1170 status=GetPathAttributes(filename,&attributes);
1171 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1173 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1176 file=open_utf8(filename,flags,0);
1180 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1181 return((
unsigned char *) NULL);
1183 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1185 file=close_utf8(file)-1;
1187 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1188 "NotAuthorized",
"`%s'",filename);
1191 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1193 if ((file == fileno(stdin)) || (offset < 0) ||
1194 (offset != (MagickOffsetType) ((ssize_t) offset)))
1205 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1206 quantum=(size_t) MagickMaxBufferExtent;
1207 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1208 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1209 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1210 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1212 count=read(file,blob+i,quantum);
1219 if (~((
size_t) i) < (count+quantum+1))
1221 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1224 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+count+quantum+1,
1226 if ((
size_t) (i+count) >= extent)
1229 if (LocaleCompare(filename,
"-") != 0)
1230 file=close_utf8(file);
1231 if (blob == (
unsigned char *) NULL)
1233 (void) ThrowMagickException(exception,GetMagickModule(),
1234 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1235 return((
unsigned char *) NULL);
1239 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1240 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1241 return((
unsigned char *) NULL);
1243 *length=(size_t) MagickMin(i+count,extent);
1247 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1248 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1249 blob=(
unsigned char *) NULL;
1250 if (~(*length) >= (MagickPathExtent-1))
1251 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1253 if (blob == (
unsigned char *) NULL)
1255 file=close_utf8(file);
1256 (void) ThrowMagickException(exception,GetMagickModule(),
1257 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1258 return((
unsigned char *) NULL);
1260 map=MapBlob(file,ReadMode,0,*length);
1261 if (map != (
unsigned char *) NULL)
1263 (void) memcpy(blob,map,*length);
1264 (void) UnmapBlob(map,*length);
1268 (void) lseek(file,0,SEEK_SET);
1269 for (i=0; i < *length; i+=count)
1271 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1272 MagickMaxBufferExtent));
1282 file=close_utf8(file)-1;
1283 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1284 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1285 return((
unsigned char *) NULL);
1289 if (LocaleCompare(filename,
"-") != 0)
1290 file=close_utf8(file);
1293 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1294 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1324static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1325 const unsigned char *magick_restrict data)
1328 *magick_restrict blob_info;
1336 assert(image->blob != (BlobInfo *) NULL);
1337 assert(image->blob->type != UndefinedStream);
1338 assert(data != (
void *) NULL);
1339 blob_info=image->blob;
1340 if (blob_info->type != BlobStream)
1341 return(WriteBlob(image,length,data));
1342 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1347 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1348 if (extent >= blob_info->extent)
1350 extent+=blob_info->quantum+length;
1351 blob_info->quantum<<=1;
1352 if (SetBlobExtent(image,extent) == MagickFalse)
1355 q=blob_info->data+blob_info->offset;
1356 (void) memcpy(q,data,length);
1357 blob_info->offset+=length;
1358 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1359 blob_info->length=(size_t) blob_info->offset;
1360 return((ssize_t) length);
1363MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename)
1381 assert(image != (
const Image *) NULL);
1382 assert(image->signature == MagickCoreSignature);
1383 assert(filename != (
const char *) NULL);
1384 if (IsEventLogging() != MagickFalse)
1385 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1386 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1389 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1390 PolicyError,
"NotAuthorized",
"`%s'",filename);
1391 return(MagickFalse);
1394 if (LocaleCompare(filename,
"-") != 0)
1397 flags = O_RDONLY | O_BINARY;
1399 file=open_utf8(filename,flags,0);
1403 ThrowFileException(&image->exception,BlobError,
"UnableToOpenBlob",
1405 return(MagickFalse);
1407 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1410 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1411 PolicyError,
"NotAuthorized",
"`%s'",filename);
1412 return(MagickFalse);
1414 quantum=(size_t) MagickMaxBufferExtent;
1415 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1416 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1417 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1418 if (blob == (
unsigned char *) NULL)
1420 file=close_utf8(file);
1421 ThrowFileException(&image->exception,ResourceLimitError,
1422 "MemoryAllocationFailed",filename);
1423 return(MagickFalse);
1427 count=read(file,blob,quantum);
1434 length=(size_t) count;
1435 count=WriteBlobStream(image,length,blob);
1436 if (count != (ssize_t) length)
1438 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1443 file=close_utf8(file);
1445 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1447 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1474MagickExport MagickBooleanType GetBlobError(
const Image *image)
1476 assert(image != (
const Image *) NULL);
1477 assert(image->signature == MagickCoreSignature);
1478 if (IsEventLogging() != MagickFalse)
1479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1480 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1481 errno=image->blob->error_number;
1482 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1507MagickExport FILE *GetBlobFileHandle(
const Image *image)
1509 assert(image != (
const Image *) NULL);
1510 assert(image->signature == MagickCoreSignature);
1511 return(image->blob->file_info.file);
1536MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1538 assert(blob_info != (BlobInfo *) NULL);
1539 (void) memset(blob_info,0,
sizeof(*blob_info));
1540 blob_info->type=UndefinedStream;
1541 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1542 blob_info->properties.st_mtime=GetMagickTime();
1543 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1544 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1545 blob_info->reference_count=1;
1546 blob_info->semaphore=AllocateSemaphoreInfo();
1547 blob_info->signature=MagickCoreSignature;
1572MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1574 assert(image != (Image *) NULL);
1575 assert(image->signature == MagickCoreSignature);
1576 if (IsEventLogging() != MagickFalse)
1577 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1578 return(&image->blob->properties);
1604MagickExport MagickSizeType GetBlobSize(
const Image *image)
1607 *magick_restrict blob_info;
1612 assert(image != (Image *) NULL);
1613 assert(image->signature == MagickCoreSignature);
1614 assert(image->blob != (BlobInfo *) NULL);
1615 if (IsEventLogging() != MagickFalse)
1616 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1617 blob_info=image->blob;
1619 switch (blob_info->type)
1621 case UndefinedStream:
1622 case StandardStream:
1624 extent=blob_info->size;
1632 extent=(MagickSizeType) blob_info->properties.st_size;
1634 extent=blob_info->size;
1635 file_descriptor=fileno(blob_info->file_info.file);
1636 if (file_descriptor == -1)
1638 if (fstat(file_descriptor,&blob_info->properties) == 0)
1639 extent=(MagickSizeType) blob_info->properties.st_size;
1644 extent=blob_info->size;
1653 status=GetPathAttributes(image->filename,&blob_info->properties);
1654 if (status != MagickFalse)
1655 extent=(MagickSizeType) blob_info->properties.st_size;
1662 extent=(MagickSizeType) blob_info->length;
1691MagickExport
unsigned char *GetBlobStreamData(
const Image *image)
1693 assert(image != (
const Image *) NULL);
1694 assert(image->signature == MagickCoreSignature);
1695 return(image->blob->data);
1720MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1722 assert(image != (
const Image *) NULL);
1723 assert(image->signature == MagickCoreSignature);
1724 if (IsEventLogging() != MagickFalse)
1725 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1726 return(image->blob->stream);
1762MagickExport
unsigned char *ImageToBlob(
const ImageInfo *image_info,
1763 Image *image,
size_t *length,ExceptionInfo *exception)
1777 assert(image_info != (
const ImageInfo *) NULL);
1778 assert(image_info->signature == MagickCoreSignature);
1779 assert(image != (Image *) NULL);
1780 assert(image->signature == MagickCoreSignature);
1781 assert(exception != (ExceptionInfo *) NULL);
1782 assert(exception->signature == MagickCoreSignature);
1783 if (IsEventLogging() != MagickFalse)
1784 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1785 image_info->filename);
1787 blob=(
unsigned char *) NULL;
1788 blob_info=CloneImageInfo(image_info);
1789 blob_info->adjoin=MagickFalse;
1790 (void) SetImageInfo(blob_info,1,exception);
1791 if (*blob_info->magick !=
'\0')
1792 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
1793 magick_info=GetMagickInfo(image->magick,exception);
1794 if (magick_info == (
const MagickInfo *) NULL)
1796 (void) ThrowMagickException(exception,GetMagickModule(),
1797 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
1799 blob_info=DestroyImageInfo(blob_info);
1802 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
1803 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1808 blob_info->length=0;
1809 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1810 sizeof(
unsigned char));
1811 if (blob_info->blob == NULL)
1812 (void) ThrowMagickException(exception,GetMagickModule(),
1813 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1816 (void) CloseBlob(image);
1817 image->blob->exempt=MagickTrue;
1818 *image->filename=
'\0';
1819 status=WriteImage(blob_info,image);
1820 InheritException(exception,&image->exception);
1821 *length=image->blob->length;
1822 blob=DetachBlob(image->blob);
1823 if (blob != (
void *) NULL)
1825 if (status == MagickFalse)
1826 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1828 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
1829 sizeof(
unsigned char));
1831 else if (status == MagickFalse)
1832 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
1838 unique[MagickPathExtent];
1846 file=AcquireUniqueFileResource(unique);
1849 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
1850 image_info->filename);
1854 blob_info->file=fdopen(file,
"wb");
1855 if (blob_info->file != (FILE *) NULL)
1857 (void) FormatLocaleString(image->filename,MagickPathExtent,
1858 "%s:%s",image->magick,unique);
1859 status=WriteImage(blob_info,image);
1860 (void) fclose(blob_info->file);
1861 if (status == MagickFalse)
1862 InheritException(exception,&image->exception);
1864 blob=FileToBlob(unique,SIZE_MAX,length,exception);
1866 (void) RelinquishUniqueFileResource(unique);
1869 blob_info=DestroyImageInfo(blob_info);
1901MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
1902 ExceptionInfo *exception)
1926 assert(image != (Image *) NULL);
1927 assert(image->signature == MagickCoreSignature);
1928 assert(image->blob != (BlobInfo *) NULL);
1929 assert(image->blob->type != UndefinedStream);
1930 assert(filename != (
const char *) NULL);
1931 if (IsEventLogging() != MagickFalse)
1932 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1933 if (*filename ==
'\0')
1934 file=AcquireUniqueFileResource(filename);
1936 if (LocaleCompare(filename,
"-") == 0)
1937 file=fileno(stdout);
1939 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
1942 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1943 return(MagickFalse);
1945 quantum=(size_t) MagickMaxBufferExtent;
1946 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1947 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1948 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
1949 if (buffer == (
unsigned char *) NULL)
1951 file=close_utf8(file)-1;
1952 (void) ThrowMagickException(exception,GetMagickModule(),
1953 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
1954 return(MagickFalse);
1957 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1958 for (i=0; count > 0; )
1960 length=(size_t) count;
1961 for (i=0; i < length; i+=count)
1963 count=write(file,p+i,(
size_t) (length-i));
1973 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1975 if (LocaleCompare(filename,
"-") != 0)
1976 file=close_utf8(file);
1977 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
1978 if ((file == -1) || (i < length))
1981 file=close_utf8(file);
1982 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1983 return(MagickFalse);
2023MagickExport
unsigned char *ImagesToBlob(
const ImageInfo *image_info,
2024 Image *images,
size_t *length,ExceptionInfo *exception)
2038 assert(image_info != (
const ImageInfo *) NULL);
2039 assert(image_info->signature == MagickCoreSignature);
2040 assert(images != (Image *) NULL);
2041 assert(images->signature == MagickCoreSignature);
2042 assert(exception != (ExceptionInfo *) NULL);
2043 if (IsEventLogging() != MagickFalse)
2044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2045 image_info->filename);
2047 blob=(
unsigned char *) NULL;
2048 blob_info=CloneImageInfo(image_info);
2049 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2051 if (*blob_info->magick !=
'\0')
2052 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2053 magick_info=GetMagickInfo(images->magick,exception);
2054 if (magick_info == (
const MagickInfo *) NULL)
2056 (void) ThrowMagickException(exception,GetMagickModule(),
2057 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2059 blob_info=DestroyImageInfo(blob_info);
2062 if (GetMagickAdjoin(magick_info) == MagickFalse)
2064 blob_info=DestroyImageInfo(blob_info);
2065 return(ImageToBlob(image_info,images,length,exception));
2067 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2068 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2073 blob_info->length=0;
2074 blob_info->blob=(
void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2075 sizeof(
unsigned char));
2076 if (blob_info->blob == (
void *) NULL)
2077 (void) ThrowMagickException(exception,GetMagickModule(),
2078 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2081 (void) CloseBlob(images);
2082 images->blob->exempt=MagickTrue;
2083 *images->filename=
'\0';
2084 status=WriteImages(blob_info,images,images->filename,exception);
2085 *length=images->blob->length;
2086 blob=DetachBlob(images->blob);
2087 if (blob != (
void *) NULL)
2089 if (status == MagickFalse)
2090 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2092 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
2093 sizeof(
unsigned char));
2096 if (status == MagickFalse)
2097 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2103 filename[MagickPathExtent],
2104 unique[MagickPathExtent];
2112 file=AcquireUniqueFileResource(unique);
2115 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2116 image_info->filename);
2120 blob_info->file=fdopen(file,
"wb");
2121 if (blob_info->file != (FILE *) NULL)
2123 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2124 images->magick,unique);
2125 status=WriteImages(blob_info,images,filename,exception);
2126 (void) fclose(blob_info->file);
2127 if (status == MagickFalse)
2128 InheritException(exception,&images->exception);
2130 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2132 (void) RelinquishUniqueFileResource(unique);
2135 blob_info=DestroyImageInfo(blob_info);
2171MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2172 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2175 filename[MagickPathExtent];
2204 assert(image_info != (ImageInfo *) NULL);
2205 assert(image_info->signature == MagickCoreSignature);
2206 assert(image != (Image *) NULL);
2207 assert(image->signature == MagickCoreSignature);
2208 assert(inject_image != (Image *) NULL);
2209 assert(inject_image->signature == MagickCoreSignature);
2210 assert(exception != (ExceptionInfo *) NULL);
2211 if (IsEventLogging() != MagickFalse)
2212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2213 unique_file=(FILE *) NULL;
2214 file=AcquireUniqueFileResource(filename);
2216 unique_file=fdopen(file,
"wb");
2217 if ((file == -1) || (unique_file == (FILE *) NULL))
2219 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2220 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2222 return(MagickFalse);
2224 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2225 if (byte_image == (Image *) NULL)
2227 (void) fclose(unique_file);
2228 (void) RelinquishUniqueFileResource(filename);
2229 return(MagickFalse);
2231 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2233 DestroyBlob(byte_image);
2234 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2235 write_info=CloneImageInfo(image_info);
2236 SetImageInfoFile(write_info,unique_file);
2237 status=WriteImage(write_info,byte_image);
2238 write_info=DestroyImageInfo(write_info);
2239 byte_image=DestroyImage(byte_image);
2240 (void) fclose(unique_file);
2241 if (status == MagickFalse)
2243 (void) RelinquishUniqueFileResource(filename);
2244 return(MagickFalse);
2249 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2252 (void) RelinquishUniqueFileResource(filename);
2253 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2254 image_info->filename);
2255 return(MagickFalse);
2257 quantum=(size_t) MagickMaxBufferExtent;
2258 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2259 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2260 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2261 if (buffer == (
unsigned char *) NULL)
2263 (void) RelinquishUniqueFileResource(filename);
2264 file=close_utf8(file);
2265 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2270 ssize_t count = read(file,buffer,quantum);
2277 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2280 file=close_utf8(file);
2282 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2283 (void) RelinquishUniqueFileResource(filename);
2284 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2310MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2312 assert(image != (
const Image *) NULL);
2313 assert(image->signature == MagickCoreSignature);
2314 if (IsEventLogging() != MagickFalse)
2315 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2316 return(image->blob->exempt);
2341MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2344 *magick_restrict blob_info;
2346 assert(image != (
const Image *) NULL);
2347 assert(image->signature == MagickCoreSignature);
2348 if (IsEventLogging() != MagickFalse)
2349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2350 blob_info=image->blob;
2351 switch (blob_info->type)
2360 if (blob_info->file_info.file == (FILE *) NULL)
2361 return(MagickFalse);
2362 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2363 return(status == -1 ? MagickFalse : MagickTrue);
2367#if defined(MAGICKCORE_ZLIB_DELEGATE)
2371 if (blob_info->file_info.gzfile == (gzFile) NULL)
2372 return(MagickFalse);
2373 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2374 return(offset < 0 ? MagickFalse : MagickTrue);
2379 case UndefinedStream:
2383 case StandardStream:
2388 return(MagickFalse);
2413MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2415 assert(image != (
const Image *) NULL);
2416 assert(image->signature == MagickCoreSignature);
2417 if (IsEventLogging() != MagickFalse)
2418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2419 return(image->blob->temporary);
2451MagickExport
unsigned char *MapBlob(
int file,
const MapMode mode,
2452 const MagickOffsetType offset,
const size_t length)
2454#if defined(MAGICKCORE_HAVE_MMAP)
2467#if defined(MAP_ANONYMOUS)
2468 flags|=MAP_ANONYMOUS;
2470 return((
unsigned char *) NULL);
2477 protection=PROT_READ;
2483 protection=PROT_WRITE;
2489 protection=PROT_READ | PROT_WRITE;
2494#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2495 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,offset);
2497 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags |
2498 MAP_HUGETLB,file,offset);
2499 if (map == (
unsigned char *) MAP_FAILED)
2500 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,
2503 if (map == (
unsigned char *) MAP_FAILED)
2504 return((
unsigned char *) NULL);
2511 return((
unsigned char *) NULL);
2540MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
2549 assert(buffer != (
unsigned char *) NULL);
2556 *buffer++=(
unsigned char) c;
2560 *buffer++=(
unsigned char) c;
2590MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
2598 assert(p != (
unsigned char *) NULL);
2605 *p++=(
unsigned char) c;
2641static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
2653 size=MagickMinBufferExtent;
2654 option=GetImageOption(image_info,
"stream:buffer-size");
2655 if (option != (
const char *) NULL)
2656 size=StringToUnsignedLong(option);
2657 status=setvbuf(image->blob->file_info.file,(
char *) NULL,size == 0 ?
2658 _IONBF : _IOFBF,size);
2659 return(status == 0 ? MagickTrue : MagickFalse);
2662#if defined(MAGICKCORE_ZLIB_DELEGATE)
2663static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
2665#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2666 return(gzopen(path,mode));
2674 path_wide=NTCreateWidePath(path);
2675 if (path_wide == (
wchar_t *) NULL)
2676 return((gzFile) NULL);
2677 file=gzopen_w(path_wide,mode);
2678 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
2684MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
2685 Image *image,
const BlobMode mode,ExceptionInfo *exception)
2688 *magick_restrict blob_info;
2691 extension[MagickPathExtent],
2692 filename[MagickPathExtent];
2706 assert(image_info != (ImageInfo *) NULL);
2707 assert(image_info->signature == MagickCoreSignature);
2708 assert(image != (Image *) NULL);
2709 assert(image->signature == MagickCoreSignature);
2710 if (IsEventLogging() != MagickFalse)
2711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2712 image_info->filename);
2713 blob_info=image->blob;
2714 if (image_info->blob != (
void *) NULL)
2716 if (image_info->stream != (StreamHandler) NULL)
2717 blob_info->stream=(StreamHandler) image_info->stream;
2718 AttachBlob(blob_info,image_info->blob,image_info->length);
2721 (void) DetachBlob(blob_info);
2722 blob_info->mode=mode;
2731 case ReadBinaryBlobMode:
2733 flags=O_RDONLY | O_BINARY;
2739 flags=O_WRONLY | O_CREAT | O_TRUNC;
2743 case WriteBinaryBlobMode:
2745 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
2749 case AppendBlobMode:
2751 flags=O_WRONLY | O_CREAT | O_APPEND;
2755 case AppendBinaryBlobMode:
2757 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
2769 blob_info->synchronize=image_info->synchronize;
2770 if (image_info->stream != (StreamHandler) NULL)
2772 blob_info->stream=(StreamHandler) image_info->stream;
2775 blob_info->type=FifoStream;
2783 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2784 rights=ReadPolicyRights;
2786 rights=WritePolicyRights;
2787 if (IsPathAuthorized(rights,filename) == MagickFalse)
2790 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2791 "NotAuthorized",
"`%s'",filename);
2792 return(MagickFalse);
2794 if ((LocaleCompare(filename,
"-") == 0) ||
2795 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
2797 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
2798#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2799 if (strchr(type,
'b') != (
char *) NULL)
2800 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2802 blob_info->type=StandardStream;
2803 blob_info->exempt=MagickTrue;
2804 return(SetStreamBuffering(image_info,image));
2806 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
2807 (IsGeometry(filename+3) != MagickFalse))
2810 fileMode[MagickPathExtent];
2814 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2815 if (blob_info->file_info.file == (FILE *) NULL)
2817 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2818 return(MagickFalse);
2820#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2821 if (strchr(type,
'b') != (
char *) NULL)
2822 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2824 blob_info->type=FileStream;
2825 blob_info->exempt=MagickTrue;
2826 return(SetStreamBuffering(image_info,image));
2828#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2829 if (*filename ==
'|')
2832 fileMode[MagickPathExtent],
2840 (void) signal(SIGPIPE,SIG_IGN);
2844 sanitize_command=SanitizeString(filename+1);
2845 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2847 sanitize_command=DestroyString(sanitize_command);
2848 if (blob_info->file_info.file == (FILE *) NULL)
2850 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2851 return(MagickFalse);
2853 blob_info->type=PipeStream;
2854 blob_info->exempt=MagickTrue;
2855 return(SetStreamBuffering(image_info,image));
2858 status=GetPathAttributes(filename,&blob_info->properties);
2859#if defined(S_ISFIFO)
2860 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2862 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2863 if (blob_info->file_info.file == (FILE *) NULL)
2865 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2866 return(MagickFalse);
2868 blob_info->type=FileStream;
2869 blob_info->exempt=MagickTrue;
2870 return(SetStreamBuffering(image_info,image));
2873 GetPathComponent(image->filename,ExtensionPath,extension);
2876 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2877 if ((image_info->adjoin == MagickFalse) ||
2878 (strchr(filename,
'%') != (
char *) NULL))
2883 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
2884 image->scene,filename);
2885 if ((LocaleCompare(filename,image->filename) == 0) &&
2886 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2887 (GetNextImageInList(image) != (Image *) NULL)))
2890 path[MagickPathExtent];
2892 GetPathComponent(image->filename,RootPath,path);
2893 if (*extension ==
'\0')
2894 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
2895 path,(
double) image->scene);
2897 (
void) FormatLocaleString(filename,MagickPathExtent,
2898 "%s-%.20g.%s",path,(
double) image->scene,extension);
2900 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2901#if defined(macintosh)
2902 SetApplicationType(filename,image_info->magick,
'8BIM');
2905 if (IsPathAuthorized(rights,filename) == MagickFalse)
2908 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2909 "NotAuthorized",
"`%s'",filename);
2910 return(MagickFalse);
2913 if (image_info->file != (FILE *) NULL)
2915 blob_info->file_info.file=image_info->file;
2916 blob_info->type=FileStream;
2917 blob_info->exempt=MagickTrue;
2925 blob_info->file_info.file=(FILE *) NULL;
2926 file=open_utf8(filename,flags,0);
2928 blob_info->file_info.file=fdopen(file,type);
2929 if (blob_info->file_info.file != (FILE *) NULL)
2937 blob_info->type=FileStream;
2938 (void) fstat(fileno(blob_info->file_info.file),
2939 &blob_info->properties);
2940 (void) SetStreamBuffering(image_info,image);
2941 (void) memset(magick,0,
sizeof(magick));
2942 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2943 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2944#if defined(MAGICKCORE_POSIX_SUPPORT)
2945 (void) fflush(blob_info->file_info.file);
2947 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2948 " read %.20g magic header bytes",(
double) count);
2949#if defined(MAGICKCORE_ZLIB_DELEGATE)
2950 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2951 ((
int) magick[2] == 0x08))
2954 gzfile = gzopen_utf8(filename,
"rb");
2956 if (gzfile != (gzFile) NULL)
2958 if (blob_info->file_info.file != (FILE *) NULL)
2959 (void) fclose(blob_info->file_info.file);
2960 blob_info->file_info.file=(FILE *) NULL;
2961 blob_info->file_info.gzfile=gzfile;
2962 blob_info->type=ZipStream;
2966#if defined(MAGICKCORE_BZLIB_DELEGATE)
2967 if (strncmp((
char *) magick,
"BZh",3) == 0)
2970 *bzfile = BZ2_bzopen(filename,
"r");
2972 if (bzfile != (BZFILE *) NULL)
2974 if (blob_info->file_info.file != (FILE *) NULL)
2975 (void) fclose(blob_info->file_info.file);
2976 blob_info->file_info.file=(FILE *) NULL;
2977 blob_info->file_info.bzfile=bzfile;
2978 blob_info->type=BZipStream;
2982 if (blob_info->type == FileStream)
2993 sans_exception=AcquireExceptionInfo();
2994 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2995 sans_exception=DestroyExceptionInfo(sans_exception);
2996 length=(size_t) blob_info->properties.st_size;
2997 if ((magick_info != (
const MagickInfo *) NULL) &&
2998 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2999 (AcquireMagickResource(MapResource,length) != MagickFalse))
3004 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3006 if (blob == (
void *) NULL)
3007 RelinquishMagickResource(MapResource,length);
3013 if (image_info->file != (FILE *) NULL)
3014 blob_info->exempt=MagickFalse;
3017 (void) fclose(blob_info->file_info.file);
3018 blob_info->file_info.file=(FILE *) NULL;
3020 AttachBlob(blob_info,blob,length);
3021 blob_info->mapped=MagickTrue;
3028#if defined(MAGICKCORE_ZLIB_DELEGATE)
3029 if ((LocaleCompare(extension,
"gz") == 0) ||
3030 (LocaleCompare(extension,
"wmz") == 0) ||
3031 (LocaleCompare(extension,
"svgz") == 0))
3033 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3034 if (blob_info->file_info.gzfile != (gzFile) NULL)
3035 blob_info->type=ZipStream;
3039#if defined(MAGICKCORE_BZLIB_DELEGATE)
3040 if (LocaleCompare(extension,
"bz2") == 0)
3042 if (mode == WriteBinaryBlobMode)
3044 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3045 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3046 blob_info->type=BZipStream;
3054 blob_info->file_info.file=(FILE *) NULL;
3055 file=open_utf8(filename,flags,P_MODE);
3057 blob_info->file_info.file=fdopen(file,type);
3058 if (blob_info->file_info.file != (FILE *) NULL)
3060 blob_info->type=FileStream;
3061 (void) SetStreamBuffering(image_info,image);
3064 if (IsPathAuthorized(rights,filename) == MagickFalse)
3067 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3068 "NotAuthorized",
"`%s'",filename);
3069 return(MagickFalse);
3071 blob_info->status=0;
3072 blob_info->error_number=0;
3073 if (blob_info->type != UndefinedStream)
3074 blob_info->size=GetBlobSize(image);
3077 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3078 return(MagickFalse);
3117#if defined(__cplusplus) || defined(c_plusplus)
3121static size_t PingStream(
const Image *magick_unused(image),
3122 const void *magick_unused(pixels),
const size_t columns)
3124 magick_unreferenced(image);
3125 magick_unreferenced(pixels);
3130#if defined(__cplusplus) || defined(c_plusplus)
3134MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3135 const size_t length,ExceptionInfo *exception)
3150 assert(image_info != (ImageInfo *) NULL);
3151 assert(image_info->signature == MagickCoreSignature);
3152 assert(exception != (ExceptionInfo *) NULL);
3153 if (IsEventLogging() != MagickFalse)
3154 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3155 image_info->filename);
3156 if ((blob == (
const void *) NULL) || (length == 0))
3158 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3159 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3160 return((Image *) NULL);
3162 ping_info=CloneImageInfo(image_info);
3163 ping_info->blob=(
void *) blob;
3164 ping_info->length=length;
3165 ping_info->ping=MagickTrue;
3166 if (*ping_info->magick ==
'\0')
3167 (void) SetImageInfo(ping_info,0,exception);
3168 magick_info=GetMagickInfo(ping_info->magick,exception);
3169 if (magick_info == (
const MagickInfo *) NULL)
3171 (void) ThrowMagickException(exception,GetMagickModule(),
3172 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3174 ping_info=DestroyImageInfo(ping_info);
3175 return((Image *) NULL);
3177 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3180 filename[MagickPathExtent];
3185 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3186 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3187 ping_info->magick,filename);
3188 image=ReadStream(ping_info,&PingStream,exception);
3189 if (image != (Image *) NULL)
3190 (void) DetachBlob(image->blob);
3191 ping_info=DestroyImageInfo(ping_info);
3197 ping_info->blob=(
void *) NULL;
3198 ping_info->length=0;
3199 *ping_info->filename=
'\0';
3200 status=BlobToFile(ping_info->filename,blob,length,exception);
3201 if (status == MagickFalse)
3203 (void) RelinquishUniqueFileResource(ping_info->filename);
3204 ping_info=DestroyImageInfo(ping_info);
3205 return((Image *) NULL);
3207 clone_info=CloneImageInfo(ping_info);
3208 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3209 ping_info->magick,ping_info->filename);
3210 image=ReadStream(clone_info,&PingStream,exception);
3211 if (image != (Image *) NULL)
3219 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3221 (void) CopyMagickString(images->filename,image_info->filename,
3223 (void) CopyMagickString(images->magick_filename,image_info->filename,
3225 (void) CopyMagickString(images->magick,magick_info->name,
3227 images=GetNextImageInList(images);
3230 clone_info=DestroyImageInfo(clone_info);
3231 (void) RelinquishUniqueFileResource(ping_info->filename);
3232 ping_info=DestroyImageInfo(ping_info);
3267MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
3268 unsigned char *data)
3271 *magick_restrict blob_info;
3282 assert(image != (Image *) NULL);
3283 assert(image->signature == MagickCoreSignature);
3284 assert(image->blob != (BlobInfo *) NULL);
3285 assert(image->blob->type != UndefinedStream);
3288 assert(data != (
void *) NULL);
3289 blob_info=image->blob;
3292 switch (blob_info->type)
3294 case UndefinedStream:
3296 case StandardStream:
3304 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3309 c=getc(blob_info->file_info.file);
3312 *q++=(
unsigned char) c;
3318 c=getc(blob_info->file_info.file);
3321 *q++=(
unsigned char) c;
3327 c=getc(blob_info->file_info.file);
3330 *q++=(
unsigned char) c;
3336 c=getc(blob_info->file_info.file);
3339 *q++=(
unsigned char) c;
3346 if ((count != (ssize_t) length) &&
3347 (ferror(blob_info->file_info.file) != 0))
3348 ThrowBlobException(blob_info);
3353#if defined(MAGICKCORE_ZLIB_DELEGATE)
3364 for (i=0; i < (ssize_t) length; i+=count)
3366 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3367 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3380 c=gzgetc(blob_info->file_info.gzfile);
3383 *q++=(
unsigned char) c;
3389 c=gzgetc(blob_info->file_info.gzfile);
3392 *q++=(
unsigned char) c;
3398 c=gzgetc(blob_info->file_info.gzfile);
3401 *q++=(
unsigned char) c;
3407 c=gzgetc(blob_info->file_info.gzfile);
3410 *q++=(
unsigned char) c;
3418 (void) gzerror(blob_info->file_info.gzfile,&status);
3419 if ((count != (ssize_t) length) && (status != Z_OK))
3420 ThrowBlobException(blob_info);
3421 if (blob_info->eof == MagickFalse)
3422 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3429#if defined(MAGICKCORE_BZLIB_DELEGATE)
3436 for (i=0; i < (ssize_t) length; i+=count)
3438 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3439 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3449 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3450 if ((count != (ssize_t) length) && (status != BZ_OK))
3451 ThrowBlobException(blob_info);
3462 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3464 blob_info->eof=MagickTrue;
3467 p=blob_info->data+blob_info->offset;
3468 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3469 blob_info->length-blob_info->offset);
3470 blob_info->offset+=count;
3471 if (count != (ssize_t) length)
3472 blob_info->eof=MagickTrue;
3473 (void) memcpy(q,p,(
size_t) count);
3502MagickExport
int ReadBlobByte(Image *image)
3505 *magick_restrict blob_info;
3510 assert(image != (Image *) NULL);
3511 assert(image->signature == MagickCoreSignature);
3512 assert(image->blob != (BlobInfo *) NULL);
3513 assert(image->blob->type != UndefinedStream);
3514 blob_info=image->blob;
3515 switch (blob_info->type)
3517 case StandardStream:
3521 c=getc(blob_info->file_info.file);
3524 if (ferror(blob_info->file_info.file) != 0)
3525 ThrowBlobException(blob_info);
3532 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3534 blob_info->eof=MagickTrue;
3537 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
3538 blob_info->offset++;
3549 count=ReadBlob(image,1,buffer);
3582MagickExport
double ReadBlobDouble(Image *image)
3593 quantum.double_value=0.0;
3594 quantum.unsigned_value=ReadBlobLongLong(image);
3595 return(quantum.double_value);
3621MagickExport
float ReadBlobFloat(Image *image)
3632 quantum.float_value=0.0;
3633 quantum.unsigned_value=ReadBlobLong(image);
3634 return(quantum.float_value);
3660MagickExport
unsigned int ReadBlobLong(Image *image)
3674 assert(image != (Image *) NULL);
3675 assert(image->signature == MagickCoreSignature);
3677 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3680 if (image->endian == LSBEndian)
3682 value=(
unsigned int) (*p++);
3683 value|=(
unsigned int) (*p++) << 8;
3684 value|=(
unsigned int) (*p++) << 16;
3685 value|=(
unsigned int) (*p++) << 24;
3688 value=(
unsigned int) (*p++) << 24;
3689 value|=(
unsigned int) (*p++) << 16;
3690 value|=(
unsigned int) (*p++) << 8;
3691 value|=(
unsigned int) (*p++);
3718MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3732 assert(image != (Image *) NULL);
3733 assert(image->signature == MagickCoreSignature);
3735 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3737 return(MagickULLConstant(0));
3738 if (image->endian == LSBEndian)
3740 value=(MagickSizeType) (*p++);
3741 value|=(MagickSizeType) (*p++) << 8;
3742 value|=(MagickSizeType) (*p++) << 16;
3743 value|=(MagickSizeType) (*p++) << 24;
3744 value|=(MagickSizeType) (*p++) << 32;
3745 value|=(MagickSizeType) (*p++) << 40;
3746 value|=(MagickSizeType) (*p++) << 48;
3747 value|=(MagickSizeType) (*p++) << 56;
3750 value=(MagickSizeType) (*p++) << 56;
3751 value|=(MagickSizeType) (*p++) << 48;
3752 value|=(MagickSizeType) (*p++) << 40;
3753 value|=(MagickSizeType) (*p++) << 32;
3754 value|=(MagickSizeType) (*p++) << 24;
3755 value|=(MagickSizeType) (*p++) << 16;
3756 value|=(MagickSizeType) (*p++) << 8;
3757 value|=(MagickSizeType) (*p++);
3784MagickExport
unsigned short ReadBlobShort(Image *image)
3798 assert(image != (Image *) NULL);
3799 assert(image->signature == MagickCoreSignature);
3801 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3803 return((
unsigned short) 0U);
3804 if (image->endian == LSBEndian)
3806 value=(
unsigned short) (*p++);
3807 value|=(
unsigned short) (*p++) << 8;
3810 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3811 value|=(
unsigned short) (*p++);
3838MagickExport
unsigned int ReadBlobLSBLong(Image *image)
3852 assert(image != (Image *) NULL);
3853 assert(image->signature == MagickCoreSignature);
3855 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3858 value=(
unsigned int) (*p++);
3859 value|=(
unsigned int) (*p++) << 8;
3860 value|=(
unsigned int) (*p++) << 16;
3861 value|=(
unsigned int) (*p++) << 24;
3888MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
3899 quantum.unsigned_value=ReadBlobLSBLong(image);
3900 return(quantum.signed_value);
3926MagickExport
unsigned short ReadBlobLSBShort(Image *image)
3940 assert(image != (Image *) NULL);
3941 assert(image->signature == MagickCoreSignature);
3943 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3945 return((
unsigned short) 0U);
3946 value=(
unsigned short) (*p++);
3947 value|=(
unsigned short) (*p++) << 8;
3974MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
3985 quantum.unsigned_value=ReadBlobLSBShort(image);
3986 return(quantum.signed_value);
4012MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4026 assert(image != (Image *) NULL);
4027 assert(image->signature == MagickCoreSignature);
4029 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4032 value=(
unsigned int) (*p++) << 24;
4033 value|=(
unsigned int) (*p++) << 16;
4034 value|=(
unsigned int) (*p++) << 8;
4035 value|=(
unsigned int) (*p++);
4062MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4076 assert(image != (Image *) NULL);
4077 assert(image->signature == MagickCoreSignature);
4079 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4081 return(MagickULLConstant(0));
4082 value=(MagickSizeType) (*p++) << 56;
4083 value|=(MagickSizeType) (*p++) << 48;
4084 value|=(MagickSizeType) (*p++) << 40;
4085 value|=(MagickSizeType) (*p++) << 32;
4086 value|=(MagickSizeType) (*p++) << 24;
4087 value|=(MagickSizeType) (*p++) << 16;
4088 value|=(MagickSizeType) (*p++) << 8;
4089 value|=(MagickSizeType) (*p++);
4116MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4130 assert(image != (Image *) NULL);
4131 assert(image->signature == MagickCoreSignature);
4133 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4135 return((
unsigned short) 0U);
4136 value=(
unsigned short) ((*p++) << 8);
4137 value|=(
unsigned short) (*p++);
4164MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4175 quantum.unsigned_value=ReadBlobMSBLong(image);
4176 return(quantum.signed_value);
4202MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4213 quantum.unsigned_value=ReadBlobMSBShort(image);
4214 return(quantum.signed_value);
4240MagickExport
signed int ReadBlobSignedLong(Image *image)
4251 quantum.unsigned_value=ReadBlobLong(image);
4252 return(quantum.signed_value);
4278MagickExport
signed short ReadBlobSignedShort(Image *image)
4289 quantum.unsigned_value=ReadBlobShort(image);
4290 return(quantum.signed_value);
4328MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4329 const size_t length,
void *magick_restrict data,ssize_t *count)
4332 *magick_restrict blob_info;
4334 assert(image != (Image *) NULL);
4335 assert(image->signature == MagickCoreSignature);
4336 assert(image->blob != (BlobInfo *) NULL);
4337 assert(image->blob->type != UndefinedStream);
4338 assert(count != (ssize_t *) NULL);
4339 blob_info=image->blob;
4340 if (blob_info->type != BlobStream)
4342 assert(data != NULL);
4343 *count=ReadBlob(image,length,(
unsigned char *) data);
4346 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4349 blob_info->eof=MagickTrue;
4352 data=blob_info->data+blob_info->offset;
4353 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4354 blob_info->length-blob_info->offset);
4355 blob_info->offset+=(*count);
4356 if (*count != (ssize_t) length)
4357 blob_info->eof=MagickTrue;
4386MagickExport
char *ReadBlobString(Image *image,
char *
string)
4389 *magick_restrict blob_info;
4397 assert(image != (Image *) NULL);
4398 assert(image->signature == MagickCoreSignature);
4399 assert(image->blob != (BlobInfo *) NULL);
4400 assert(image->blob->type != UndefinedStream);
4401 if (IsEventLogging() != MagickFalse)
4402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4404 blob_info=image->blob;
4405 switch (blob_info->type)
4407 case UndefinedStream:
4409 case StandardStream:
4412 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4413 if (p == (
char *) NULL)
4415 if (ferror(blob_info->file_info.file) != 0)
4416 ThrowBlobException(blob_info);
4417 return((
char *) NULL);
4424#if defined(MAGICKCORE_ZLIB_DELEGATE)
4425 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4426 if (p == (
char *) NULL)
4429 (void) gzerror(blob_info->file_info.gzfile,&status);
4431 ThrowBlobException(blob_info);
4432 return((
char *) NULL);
4442 c=ReadBlobByte(image);
4445 blob_info->eof=MagickTrue;
4451 }
while (i < (MaxTextExtent-2));
4459 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4462 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4464 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4465 return((
char *) NULL);
4492MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4494 assert(blob != (BlobInfo *) NULL);
4495 assert(blob->signature == MagickCoreSignature);
4496 if (IsEventLogging() != MagickFalse)
4497 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4498 LockSemaphoreInfo(blob->semaphore);
4499 blob->reference_count++;
4500 UnlockSemaphoreInfo(blob->semaphore);
4537MagickExport MagickOffsetType SeekBlob(Image *image,
4538 const MagickOffsetType offset,
const int whence)
4541 *magick_restrict blob_info;
4543 assert(image != (Image *) NULL);
4544 assert(image->signature == MagickCoreSignature);
4545 assert(image->blob != (BlobInfo *) NULL);
4546 assert(image->blob->type != UndefinedStream);
4547 if (IsEventLogging() != MagickFalse)
4548 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4549 blob_info=image->blob;
4550 switch (blob_info->type)
4552 case UndefinedStream:
4554 case StandardStream:
4559 if ((offset < 0) && (whence == SEEK_SET))
4561 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4563 blob_info->offset=TellBlob(image);
4568#if defined(MAGICKCORE_ZLIB_DELEGATE)
4569 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4572 blob_info->offset=TellBlob(image);
4588 blob_info->offset=offset;
4593 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4594 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4599 if ((blob_info->offset+offset) < 0)
4601 blob_info->offset+=offset;
4606 if (((MagickOffsetType) blob_info->length+offset) < 0)
4608 blob_info->offset=blob_info->length+offset;
4612 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4614 blob_info->eof=MagickFalse;
4620 return(blob_info->offset);
4648MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
4650 assert(image != (
const Image *) NULL);
4651 assert(image->signature == MagickCoreSignature);
4652 if (IsEventLogging() != MagickFalse)
4653 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4654 image->blob->exempt=exempt;
4683MagickExport MagickBooleanType SetBlobExtent(Image *image,
4684 const MagickSizeType extent)
4687 *magick_restrict blob_info;
4689 assert(image != (Image *) NULL);
4690 assert(image->signature == MagickCoreSignature);
4691 assert(image->blob != (BlobInfo *) NULL);
4692 assert(image->blob->type != UndefinedStream);
4693 if (IsEventLogging() != MagickFalse)
4694 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4695 blob_info=image->blob;
4696 switch (blob_info->type)
4698 case UndefinedStream:
4700 case StandardStream:
4701 return(MagickFalse);
4710 if (extent != (MagickSizeType) ((off_t) extent))
4711 return(MagickFalse);
4712 offset=SeekBlob(image,0,SEEK_END);
4714 return(MagickFalse);
4715 if ((MagickSizeType) offset >= extent)
4717 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4720 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4721 blob_info->file_info.file);
4722#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4723 if (blob_info->synchronize != MagickFalse)
4728 file=fileno(blob_info->file_info.file);
4729 if ((file == -1) || (offset < 0))
4730 return(MagickFalse);
4731 (void) posix_fallocate(file,offset,extent-offset);
4734 offset=SeekBlob(image,offset,SEEK_SET);
4736 return(MagickFalse);
4741 return(MagickFalse);
4743 return(MagickFalse);
4745 return(MagickFalse);
4748 if (extent != (MagickSizeType) ((
size_t) extent))
4749 return(MagickFalse);
4750 if (blob_info->mapped != MagickFalse)
4758 (void) UnmapBlob(blob_info->data,blob_info->length);
4759 RelinquishMagickResource(MapResource,blob_info->length);
4760 if (extent != (MagickSizeType) ((off_t) extent))
4761 return(MagickFalse);
4762 offset=SeekBlob(image,0,SEEK_END);
4764 return(MagickFalse);
4765 if ((MagickSizeType) offset >= extent)
4767 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4768 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4769 blob_info->file_info.file);
4770#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4771 if (blob_info->synchronize != MagickFalse)
4776 file=fileno(blob_info->file_info.file);
4777 if ((file == -1) || (offset < 0))
4778 return(MagickFalse);
4779 (void) posix_fallocate(file,offset,extent-offset);
4782 offset=SeekBlob(image,offset,SEEK_SET);
4784 return(MagickFalse);
4785 (void) AcquireMagickResource(MapResource,extent);
4786 blob_info->data=(
unsigned char*) MapBlob(fileno(
4787 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
4788 blob_info->extent=(size_t) extent;
4789 blob_info->length=(size_t) extent;
4790 (void) SyncBlob(image);
4793 blob_info->extent=(size_t) extent;
4794 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4795 blob_info->extent+1,
sizeof(*blob_info->data));
4796 (void) SyncBlob(image);
4797 if (blob_info->data == (
unsigned char *) NULL)
4799 (void) DetachBlob(blob_info);
4800 return(MagickFalse);
4832static int SyncBlob(
const Image *image)
4835 *magick_restrict blob_info;
4840 assert(image != (Image *) NULL);
4841 assert(image->signature == MagickCoreSignature);
4842 assert(image->blob != (BlobInfo *) NULL);
4843 if (IsEventLogging() != MagickFalse)
4844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4845 if (EOFBlob(image) != 0)
4847 blob_info=image->blob;
4849 switch (blob_info->type)
4851 case UndefinedStream:
4852 case StandardStream:
4857 status=fflush(blob_info->file_info.file);
4862#if defined(MAGICKCORE_ZLIB_DELEGATE)
4863 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4869#if defined(MAGICKCORE_BZLIB_DELEGATE)
4870 status=BZ2_bzflush(blob_info->file_info.bzfile);
4904MagickExport MagickOffsetType TellBlob(
const Image *image)
4907 *magick_restrict blob_info;
4912 assert(image != (Image *) NULL);
4913 assert(image->signature == MagickCoreSignature);
4914 assert(image->blob != (BlobInfo *) NULL);
4915 assert(image->blob->type != UndefinedStream);
4916 if (IsEventLogging() != MagickFalse)
4917 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4918 blob_info=image->blob;
4920 switch (blob_info->type)
4922 case UndefinedStream:
4923 case StandardStream:
4927 offset=ftell(blob_info->file_info.file);
4934#if defined(MAGICKCORE_ZLIB_DELEGATE)
4935 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4945 offset=blob_info->offset;
4977MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
4979#if defined(MAGICKCORE_HAVE_MMAP)
4983 status=munmap(map,length);
4984 return(status == -1 ? MagickFalse : MagickTrue);
4988 return(MagickFalse);
5021MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5022 const unsigned char *data)
5025 *magick_restrict blob_info;
5039 assert(image != (Image *) NULL);
5040 assert(image->signature == MagickCoreSignature);
5041 assert(image->blob != (BlobInfo *) NULL);
5042 assert(image->blob->type != UndefinedStream);
5045 assert(data != (
const unsigned char *) NULL);
5046 blob_info=image->blob;
5048 p=(
const unsigned char *) data;
5049 q=(
unsigned char *) data;
5050 switch (blob_info->type)
5052 case UndefinedStream:
5054 case StandardStream:
5062 count=(ssize_t) fwrite((
const char *) data,1,length,
5063 blob_info->file_info.file);
5068 c=putc((
int) *p++,blob_info->file_info.file);
5076 c=putc((
int) *p++,blob_info->file_info.file);
5084 c=putc((
int) *p++,blob_info->file_info.file);
5092 c=putc((
int) *p++,blob_info->file_info.file);
5101 if ((count != (ssize_t) length) &&
5102 (ferror(blob_info->file_info.file) != 0))
5103 ThrowBlobException(blob_info);
5108#if defined(MAGICKCORE_ZLIB_DELEGATE)
5119 for (i=0; i < (ssize_t) length; i+=count)
5121 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5122 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5135 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5143 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5151 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5159 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5169 (void) gzerror(blob_info->file_info.gzfile,&status);
5170 if ((count != (ssize_t) length) && (status != Z_OK))
5171 ThrowBlobException(blob_info);
5177#if defined(MAGICKCORE_BZLIB_DELEGATE)
5184 for (i=0; i < (ssize_t) length; i+=count)
5186 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5187 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5197 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5198 if ((count != (ssize_t) length) && (status != BZ_OK))
5199 ThrowBlobException(blob_info);
5205 count=(ssize_t) blob_info->stream(image,data,length);
5213 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5218 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5219 if (extent >= blob_info->extent)
5221 extent+=blob_info->quantum+length;
5222 blob_info->quantum<<=1;
5223 if (SetBlobExtent(image,extent) == MagickFalse)
5226 q=blob_info->data+blob_info->offset;
5227 (void) memcpy(q,p,length);
5228 blob_info->offset+=length;
5229 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5230 blob_info->length=(size_t) blob_info->offset;
5231 count=(ssize_t) length;
5234 if (count != (ssize_t) length)
5235 ThrowBlobException(blob_info);
5264MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
5267 *magick_restrict blob_info;
5272 assert(image != (Image *) NULL);
5273 assert(image->signature == MagickCoreSignature);
5274 assert(image->blob != (BlobInfo *) NULL);
5275 assert(image->blob->type != UndefinedStream);
5276 blob_info=image->blob;
5278 switch (blob_info->type)
5280 case StandardStream:
5287 c=putc((
int) value,blob_info->file_info.file);
5290 if (ferror(blob_info->file_info.file) != 0)
5291 ThrowBlobException(blob_info);
5299 count=WriteBlobStream(image,1,&value);
5331MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
5342 quantum.unsigned_value=0U;
5343 quantum.float_value=value;
5344 return(WriteBlobLong(image,quantum.unsigned_value));
5372MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
5377 assert(image != (Image *) NULL);
5378 assert(image->signature == MagickCoreSignature);
5379 if (image->endian == LSBEndian)
5381 buffer[0]=(
unsigned char) value;
5382 buffer[1]=(
unsigned char) (value >> 8);
5383 buffer[2]=(
unsigned char) (value >> 16);
5384 buffer[3]=(
unsigned char) (value >> 24);
5385 return(WriteBlobStream(image,4,buffer));
5387 buffer[0]=(
unsigned char) (value >> 24);
5388 buffer[1]=(
unsigned char) (value >> 16);
5389 buffer[2]=(
unsigned char) (value >> 8);
5390 buffer[3]=(
unsigned char) value;
5391 return(WriteBlobStream(image,4,buffer));
5419MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
5424 assert(image != (Image *) NULL);
5425 assert(image->signature == MagickCoreSignature);
5426 if (image->endian == LSBEndian)
5428 buffer[0]=(
unsigned char) value;
5429 buffer[1]=(
unsigned char) (value >> 8);
5430 return(WriteBlobStream(image,2,buffer));
5432 buffer[0]=(
unsigned char) (value >> 8);
5433 buffer[1]=(
unsigned char) value;
5434 return(WriteBlobStream(image,2,buffer));
5462MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
5467 assert(image != (Image *) NULL);
5468 assert(image->signature == MagickCoreSignature);
5469 buffer[0]=(
unsigned char) value;
5470 buffer[1]=(
unsigned char) (value >> 8);
5471 buffer[2]=(
unsigned char) (value >> 16);
5472 buffer[3]=(
unsigned char) (value >> 24);
5473 return(WriteBlobStream(image,4,buffer));
5501MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
5506 assert(image != (Image *) NULL);
5507 assert(image->signature == MagickCoreSignature);
5508 buffer[0]=(
unsigned char) value;
5509 buffer[1]=(
unsigned char) (value >> 8);
5510 return(WriteBlobStream(image,2,buffer));
5538MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
5552 assert(image != (Image *) NULL);
5553 assert(image->signature == MagickCoreSignature);
5554 quantum.signed_value=value;
5555 buffer[0]=(
unsigned char) quantum.unsigned_value;
5556 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5557 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5558 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5559 return(WriteBlobStream(image,4,buffer));
5587MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5588 const signed short value)
5602 assert(image != (Image *) NULL);
5603 assert(image->signature == MagickCoreSignature);
5604 quantum.signed_value=value;
5605 buffer[0]=(
unsigned char) quantum.unsigned_value;
5606 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5607 return(WriteBlobStream(image,2,buffer));
5635MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
5640 assert(image != (Image *) NULL);
5641 assert(image->signature == MagickCoreSignature);
5642 buffer[0]=(
unsigned char) (value >> 24);
5643 buffer[1]=(
unsigned char) (value >> 16);
5644 buffer[2]=(
unsigned char) (value >> 8);
5645 buffer[3]=(
unsigned char) value;
5646 return(WriteBlobStream(image,4,buffer));
5674MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5675 const MagickSizeType value)
5680 assert(image != (Image *) NULL);
5681 assert(image->signature == MagickCoreSignature);
5682 buffer[0]=(
unsigned char) (value >> 56);
5683 buffer[1]=(
unsigned char) (value >> 48);
5684 buffer[2]=(
unsigned char) (value >> 40);
5685 buffer[3]=(
unsigned char) (value >> 32);
5686 buffer[4]=(
unsigned char) (value >> 24);
5687 buffer[5]=(
unsigned char) (value >> 16);
5688 buffer[6]=(
unsigned char) (value >> 8);
5689 buffer[7]=(
unsigned char) value;
5690 return(WriteBlobStream(image,8,buffer));
5718MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
5723 assert(image != (Image *) NULL);
5724 assert(image->signature == MagickCoreSignature);
5725 buffer[0]=(
unsigned char) (value >> 8);
5726 buffer[1]=(
unsigned char) value;
5727 return(WriteBlobStream(image,2,buffer));
5755MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,
const signed int value)
5769 assert(image != (Image *) NULL);
5770 assert(image->signature == MagickCoreSignature);
5771 quantum.signed_value=value;
5772 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5773 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5774 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5775 buffer[3]=(
unsigned char) quantum.unsigned_value;
5776 return(WriteBlobStream(image,4,buffer));
5804MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5805 const signed short value)
5819 assert(image != (Image *) NULL);
5820 assert(image->signature == MagickCoreSignature);
5821 quantum.signed_value=value;
5822 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5823 buffer[1]=(
unsigned char) quantum.unsigned_value;
5824 return(WriteBlobStream(image,2,buffer));
5852MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
5854 assert(image != (Image *) NULL);
5855 assert(image->signature == MagickCoreSignature);
5856 assert(
string != (
const char *) NULL);
5857 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));