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');
2906 if (image_info->file != (FILE *) NULL)
2908 blob_info->file_info.file=image_info->file;
2909 blob_info->type=FileStream;
2910 blob_info->exempt=MagickTrue;
2918 blob_info->file_info.file=(FILE *) NULL;
2919 file=open_utf8(filename,flags,0);
2921 blob_info->file_info.file=fdopen(file,type);
2922 if (blob_info->file_info.file != (FILE *) NULL)
2930 blob_info->type=FileStream;
2931 (void) fstat(fileno(blob_info->file_info.file),
2932 &blob_info->properties);
2933 (void) SetStreamBuffering(image_info,image);
2934 (void) memset(magick,0,
sizeof(magick));
2935 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2936 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2937#if defined(MAGICKCORE_POSIX_SUPPORT)
2938 (void) fflush(blob_info->file_info.file);
2940 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2941 " read %.20g magic header bytes",(
double) count);
2942#if defined(MAGICKCORE_ZLIB_DELEGATE)
2943 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2944 ((
int) magick[2] == 0x08))
2947 gzfile = gzopen_utf8(filename,
"rb");
2949 if (gzfile != (gzFile) NULL)
2951 if (blob_info->file_info.file != (FILE *) NULL)
2952 (void) fclose(blob_info->file_info.file);
2953 blob_info->file_info.file=(FILE *) NULL;
2954 blob_info->file_info.gzfile=gzfile;
2955 blob_info->type=ZipStream;
2959#if defined(MAGICKCORE_BZLIB_DELEGATE)
2960 if (strncmp((
char *) magick,
"BZh",3) == 0)
2963 *bzfile = BZ2_bzopen(filename,
"r");
2965 if (bzfile != (BZFILE *) NULL)
2967 if (blob_info->file_info.file != (FILE *) NULL)
2968 (void) fclose(blob_info->file_info.file);
2969 blob_info->file_info.file=(FILE *) NULL;
2970 blob_info->file_info.bzfile=bzfile;
2971 blob_info->type=BZipStream;
2975 if (blob_info->type == FileStream)
2986 sans_exception=AcquireExceptionInfo();
2987 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2988 sans_exception=DestroyExceptionInfo(sans_exception);
2989 length=(size_t) blob_info->properties.st_size;
2990 if ((magick_info != (
const MagickInfo *) NULL) &&
2991 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2992 (length > MagickMaxBufferExtent) &&
2993 (AcquireMagickResource(MapResource,length) != MagickFalse))
2998 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3000 if (blob == (
void *) NULL)
3001 RelinquishMagickResource(MapResource,length);
3007 if (image_info->file != (FILE *) NULL)
3008 blob_info->exempt=MagickFalse;
3011 (void) fclose(blob_info->file_info.file);
3012 blob_info->file_info.file=(FILE *) NULL;
3014 AttachBlob(blob_info,blob,length);
3015 blob_info->mapped=MagickTrue;
3022#if defined(MAGICKCORE_ZLIB_DELEGATE)
3023 if ((LocaleCompare(extension,
"gz") == 0) ||
3024 (LocaleCompare(extension,
"wmz") == 0) ||
3025 (LocaleCompare(extension,
"svgz") == 0))
3027 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3028 if (blob_info->file_info.gzfile != (gzFile) NULL)
3029 blob_info->type=ZipStream;
3033#if defined(MAGICKCORE_BZLIB_DELEGATE)
3034 if (LocaleCompare(extension,
"bz2") == 0)
3036 if (mode == WriteBinaryBlobMode)
3038 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3039 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3040 blob_info->type=BZipStream;
3048 blob_info->file_info.file=(FILE *) NULL;
3049 file=open_utf8(filename,flags,P_MODE);
3051 blob_info->file_info.file=fdopen(file,type);
3052 if (blob_info->file_info.file != (FILE *) NULL)
3054 blob_info->type=FileStream;
3055 (void) SetStreamBuffering(image_info,image);
3058 if (IsPathAuthorized(rights,filename) == MagickFalse)
3061 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3062 "NotAuthorized",
"`%s'",filename);
3063 return(MagickFalse);
3065 blob_info->status=0;
3066 blob_info->error_number=0;
3067 if (blob_info->type != UndefinedStream)
3068 blob_info->size=GetBlobSize(image);
3071 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3072 return(MagickFalse);
3111#if defined(__cplusplus) || defined(c_plusplus)
3115static size_t PingStream(
const Image *magick_unused(image),
3116 const void *magick_unused(pixels),
const size_t columns)
3118 magick_unreferenced(image);
3119 magick_unreferenced(pixels);
3124#if defined(__cplusplus) || defined(c_plusplus)
3128MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3129 const size_t length,ExceptionInfo *exception)
3144 assert(image_info != (ImageInfo *) NULL);
3145 assert(image_info->signature == MagickCoreSignature);
3146 assert(exception != (ExceptionInfo *) NULL);
3147 if (IsEventLogging() != MagickFalse)
3148 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3149 image_info->filename);
3150 if ((blob == (
const void *) NULL) || (length == 0))
3152 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3153 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3154 return((Image *) NULL);
3156 ping_info=CloneImageInfo(image_info);
3157 ping_info->blob=(
void *) blob;
3158 ping_info->length=length;
3159 ping_info->ping=MagickTrue;
3160 if (*ping_info->magick ==
'\0')
3161 (void) SetImageInfo(ping_info,0,exception);
3162 magick_info=GetMagickInfo(ping_info->magick,exception);
3163 if (magick_info == (
const MagickInfo *) NULL)
3165 (void) ThrowMagickException(exception,GetMagickModule(),
3166 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3168 ping_info=DestroyImageInfo(ping_info);
3169 return((Image *) NULL);
3171 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3174 filename[MagickPathExtent];
3179 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3180 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3181 ping_info->magick,filename);
3182 image=ReadStream(ping_info,&PingStream,exception);
3183 if (image != (Image *) NULL)
3184 (void) DetachBlob(image->blob);
3185 ping_info=DestroyImageInfo(ping_info);
3191 ping_info->blob=(
void *) NULL;
3192 ping_info->length=0;
3193 *ping_info->filename=
'\0';
3194 status=BlobToFile(ping_info->filename,blob,length,exception);
3195 if (status == MagickFalse)
3197 (void) RelinquishUniqueFileResource(ping_info->filename);
3198 ping_info=DestroyImageInfo(ping_info);
3199 return((Image *) NULL);
3201 clone_info=CloneImageInfo(ping_info);
3202 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3203 ping_info->magick,ping_info->filename);
3204 image=ReadStream(clone_info,&PingStream,exception);
3205 if (image != (Image *) NULL)
3213 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3215 (void) CopyMagickString(images->filename,image_info->filename,
3217 (void) CopyMagickString(images->magick_filename,image_info->filename,
3219 (void) CopyMagickString(images->magick,magick_info->name,
3221 images=GetNextImageInList(images);
3224 clone_info=DestroyImageInfo(clone_info);
3225 (void) RelinquishUniqueFileResource(ping_info->filename);
3226 ping_info=DestroyImageInfo(ping_info);
3261MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
3262 unsigned char *data)
3265 *magick_restrict blob_info;
3276 assert(image != (Image *) NULL);
3277 assert(image->signature == MagickCoreSignature);
3278 assert(image->blob != (BlobInfo *) NULL);
3279 assert(image->blob->type != UndefinedStream);
3282 assert(data != (
void *) NULL);
3283 blob_info=image->blob;
3286 switch (blob_info->type)
3288 case UndefinedStream:
3290 case StandardStream:
3298 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3303 c=getc(blob_info->file_info.file);
3306 *q++=(
unsigned char) c;
3312 c=getc(blob_info->file_info.file);
3315 *q++=(
unsigned char) c;
3321 c=getc(blob_info->file_info.file);
3324 *q++=(
unsigned char) c;
3330 c=getc(blob_info->file_info.file);
3333 *q++=(
unsigned char) c;
3340 if ((count != (ssize_t) length) &&
3341 (ferror(blob_info->file_info.file) != 0))
3342 ThrowBlobException(blob_info);
3347#if defined(MAGICKCORE_ZLIB_DELEGATE)
3358 for (i=0; i < (ssize_t) length; i+=count)
3360 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3361 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3374 c=gzgetc(blob_info->file_info.gzfile);
3377 *q++=(
unsigned char) c;
3383 c=gzgetc(blob_info->file_info.gzfile);
3386 *q++=(
unsigned char) c;
3392 c=gzgetc(blob_info->file_info.gzfile);
3395 *q++=(
unsigned char) c;
3401 c=gzgetc(blob_info->file_info.gzfile);
3404 *q++=(
unsigned char) c;
3412 (void) gzerror(blob_info->file_info.gzfile,&status);
3413 if ((count != (ssize_t) length) && (status != Z_OK))
3414 ThrowBlobException(blob_info);
3415 if (blob_info->eof == MagickFalse)
3416 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3423#if defined(MAGICKCORE_BZLIB_DELEGATE)
3430 for (i=0; i < (ssize_t) length; i+=count)
3432 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3433 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3443 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3444 if ((count != (ssize_t) length) && (status != BZ_OK))
3445 ThrowBlobException(blob_info);
3456 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3458 blob_info->eof=MagickTrue;
3461 p=blob_info->data+blob_info->offset;
3462 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3463 blob_info->length-blob_info->offset);
3464 blob_info->offset+=count;
3465 if (count != (ssize_t) length)
3466 blob_info->eof=MagickTrue;
3467 (void) memcpy(q,p,(
size_t) count);
3496MagickExport
int ReadBlobByte(Image *image)
3499 *magick_restrict blob_info;
3504 assert(image != (Image *) NULL);
3505 assert(image->signature == MagickCoreSignature);
3506 assert(image->blob != (BlobInfo *) NULL);
3507 assert(image->blob->type != UndefinedStream);
3508 blob_info=image->blob;
3509 switch (blob_info->type)
3511 case StandardStream:
3515 c=getc(blob_info->file_info.file);
3518 if (ferror(blob_info->file_info.file) != 0)
3519 ThrowBlobException(blob_info);
3526 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3528 blob_info->eof=MagickTrue;
3531 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
3532 blob_info->offset++;
3543 count=ReadBlob(image,1,buffer);
3576MagickExport
double ReadBlobDouble(Image *image)
3587 quantum.double_value=0.0;
3588 quantum.unsigned_value=ReadBlobLongLong(image);
3589 return(quantum.double_value);
3615MagickExport
float ReadBlobFloat(Image *image)
3626 quantum.float_value=0.0;
3627 quantum.unsigned_value=ReadBlobLong(image);
3628 return(quantum.float_value);
3654MagickExport
unsigned int ReadBlobLong(Image *image)
3668 assert(image != (Image *) NULL);
3669 assert(image->signature == MagickCoreSignature);
3671 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3674 if (image->endian == LSBEndian)
3676 value=(
unsigned int) (*p++);
3677 value|=(
unsigned int) (*p++) << 8;
3678 value|=(
unsigned int) (*p++) << 16;
3679 value|=(
unsigned int) (*p++) << 24;
3682 value=(
unsigned int) (*p++) << 24;
3683 value|=(
unsigned int) (*p++) << 16;
3684 value|=(
unsigned int) (*p++) << 8;
3685 value|=(
unsigned int) (*p++);
3712MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3726 assert(image != (Image *) NULL);
3727 assert(image->signature == MagickCoreSignature);
3729 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3731 return(MagickULLConstant(0));
3732 if (image->endian == LSBEndian)
3734 value=(MagickSizeType) (*p++);
3735 value|=(MagickSizeType) (*p++) << 8;
3736 value|=(MagickSizeType) (*p++) << 16;
3737 value|=(MagickSizeType) (*p++) << 24;
3738 value|=(MagickSizeType) (*p++) << 32;
3739 value|=(MagickSizeType) (*p++) << 40;
3740 value|=(MagickSizeType) (*p++) << 48;
3741 value|=(MagickSizeType) (*p++) << 56;
3744 value=(MagickSizeType) (*p++) << 56;
3745 value|=(MagickSizeType) (*p++) << 48;
3746 value|=(MagickSizeType) (*p++) << 40;
3747 value|=(MagickSizeType) (*p++) << 32;
3748 value|=(MagickSizeType) (*p++) << 24;
3749 value|=(MagickSizeType) (*p++) << 16;
3750 value|=(MagickSizeType) (*p++) << 8;
3751 value|=(MagickSizeType) (*p++);
3778MagickExport
unsigned short ReadBlobShort(Image *image)
3792 assert(image != (Image *) NULL);
3793 assert(image->signature == MagickCoreSignature);
3795 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3797 return((
unsigned short) 0U);
3798 if (image->endian == LSBEndian)
3800 value=(
unsigned short) (*p++);
3801 value|=(
unsigned short) (*p++) << 8;
3804 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3805 value|=(
unsigned short) (*p++);
3832MagickExport
unsigned int ReadBlobLSBLong(Image *image)
3846 assert(image != (Image *) NULL);
3847 assert(image->signature == MagickCoreSignature);
3849 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3852 value=(
unsigned int) (*p++);
3853 value|=(
unsigned int) (*p++) << 8;
3854 value|=(
unsigned int) (*p++) << 16;
3855 value|=(
unsigned int) (*p++) << 24;
3882MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
3893 quantum.unsigned_value=ReadBlobLSBLong(image);
3894 return(quantum.signed_value);
3920MagickExport
unsigned short ReadBlobLSBShort(Image *image)
3934 assert(image != (Image *) NULL);
3935 assert(image->signature == MagickCoreSignature);
3937 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3939 return((
unsigned short) 0U);
3940 value=(
unsigned short) (*p++);
3941 value|=(
unsigned short) (*p++) << 8;
3968MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
3979 quantum.unsigned_value=ReadBlobLSBShort(image);
3980 return(quantum.signed_value);
4006MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4020 assert(image != (Image *) NULL);
4021 assert(image->signature == MagickCoreSignature);
4023 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4026 value=(
unsigned int) (*p++) << 24;
4027 value|=(
unsigned int) (*p++) << 16;
4028 value|=(
unsigned int) (*p++) << 8;
4029 value|=(
unsigned int) (*p++);
4056MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4070 assert(image != (Image *) NULL);
4071 assert(image->signature == MagickCoreSignature);
4073 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4075 return(MagickULLConstant(0));
4076 value=(MagickSizeType) (*p++) << 56;
4077 value|=(MagickSizeType) (*p++) << 48;
4078 value|=(MagickSizeType) (*p++) << 40;
4079 value|=(MagickSizeType) (*p++) << 32;
4080 value|=(MagickSizeType) (*p++) << 24;
4081 value|=(MagickSizeType) (*p++) << 16;
4082 value|=(MagickSizeType) (*p++) << 8;
4083 value|=(MagickSizeType) (*p++);
4110MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4124 assert(image != (Image *) NULL);
4125 assert(image->signature == MagickCoreSignature);
4127 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4129 return((
unsigned short) 0U);
4130 value=(
unsigned short) ((*p++) << 8);
4131 value|=(
unsigned short) (*p++);
4158MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4169 quantum.unsigned_value=ReadBlobMSBLong(image);
4170 return(quantum.signed_value);
4196MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4207 quantum.unsigned_value=ReadBlobMSBShort(image);
4208 return(quantum.signed_value);
4234MagickExport
signed int ReadBlobSignedLong(Image *image)
4245 quantum.unsigned_value=ReadBlobLong(image);
4246 return(quantum.signed_value);
4272MagickExport
signed short ReadBlobSignedShort(Image *image)
4283 quantum.unsigned_value=ReadBlobShort(image);
4284 return(quantum.signed_value);
4322MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4323 const size_t length,
void *magick_restrict data,ssize_t *count)
4326 *magick_restrict blob_info;
4328 assert(image != (Image *) NULL);
4329 assert(image->signature == MagickCoreSignature);
4330 assert(image->blob != (BlobInfo *) NULL);
4331 assert(image->blob->type != UndefinedStream);
4332 assert(count != (ssize_t *) NULL);
4333 blob_info=image->blob;
4334 if (blob_info->type != BlobStream)
4336 assert(data != NULL);
4337 *count=ReadBlob(image,length,(
unsigned char *) data);
4340 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4343 blob_info->eof=MagickTrue;
4346 data=blob_info->data+blob_info->offset;
4347 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4348 blob_info->length-blob_info->offset);
4349 blob_info->offset+=(*count);
4350 if (*count != (ssize_t) length)
4351 blob_info->eof=MagickTrue;
4380MagickExport
char *ReadBlobString(Image *image,
char *
string)
4383 *magick_restrict blob_info;
4391 assert(image != (Image *) NULL);
4392 assert(image->signature == MagickCoreSignature);
4393 assert(image->blob != (BlobInfo *) NULL);
4394 assert(image->blob->type != UndefinedStream);
4395 if (IsEventLogging() != MagickFalse)
4396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4398 blob_info=image->blob;
4399 switch (blob_info->type)
4401 case UndefinedStream:
4403 case StandardStream:
4406 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4407 if (p == (
char *) NULL)
4409 if (ferror(blob_info->file_info.file) != 0)
4410 ThrowBlobException(blob_info);
4411 return((
char *) NULL);
4418#if defined(MAGICKCORE_ZLIB_DELEGATE)
4419 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4420 if (p == (
char *) NULL)
4423 (void) gzerror(blob_info->file_info.gzfile,&status);
4425 ThrowBlobException(blob_info);
4426 return((
char *) NULL);
4436 c=ReadBlobByte(image);
4439 blob_info->eof=MagickTrue;
4445 }
while (i < (MaxTextExtent-2));
4453 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4456 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4458 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4459 return((
char *) NULL);
4486MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4488 assert(blob != (BlobInfo *) NULL);
4489 assert(blob->signature == MagickCoreSignature);
4490 if (IsEventLogging() != MagickFalse)
4491 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4492 LockSemaphoreInfo(blob->semaphore);
4493 blob->reference_count++;
4494 UnlockSemaphoreInfo(blob->semaphore);
4531MagickExport MagickOffsetType SeekBlob(Image *image,
4532 const MagickOffsetType offset,
const int whence)
4535 *magick_restrict blob_info;
4537 assert(image != (Image *) NULL);
4538 assert(image->signature == MagickCoreSignature);
4539 assert(image->blob != (BlobInfo *) NULL);
4540 assert(image->blob->type != UndefinedStream);
4541 if (IsEventLogging() != MagickFalse)
4542 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4543 blob_info=image->blob;
4544 switch (blob_info->type)
4546 case UndefinedStream:
4548 case StandardStream:
4553 if ((offset < 0) && (whence == SEEK_SET))
4555 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4557 blob_info->offset=TellBlob(image);
4562#if defined(MAGICKCORE_ZLIB_DELEGATE)
4563 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4566 blob_info->offset=TellBlob(image);
4582 blob_info->offset=offset;
4587 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4588 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4593 if ((blob_info->offset+offset) < 0)
4595 blob_info->offset+=offset;
4600 if (((MagickOffsetType) blob_info->length+offset) < 0)
4602 blob_info->offset=blob_info->length+offset;
4606 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4608 blob_info->eof=MagickFalse;
4614 return(blob_info->offset);
4642MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
4644 assert(image != (
const Image *) NULL);
4645 assert(image->signature == MagickCoreSignature);
4646 if (IsEventLogging() != MagickFalse)
4647 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4648 image->blob->exempt=exempt;
4677MagickExport MagickBooleanType SetBlobExtent(Image *image,
4678 const MagickSizeType extent)
4681 *magick_restrict blob_info;
4683 assert(image != (Image *) NULL);
4684 assert(image->signature == MagickCoreSignature);
4685 assert(image->blob != (BlobInfo *) NULL);
4686 assert(image->blob->type != UndefinedStream);
4687 if (IsEventLogging() != MagickFalse)
4688 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4689 blob_info=image->blob;
4690 switch (blob_info->type)
4692 case UndefinedStream:
4694 case StandardStream:
4695 return(MagickFalse);
4704 if (extent != (MagickSizeType) ((off_t) extent))
4705 return(MagickFalse);
4706 offset=SeekBlob(image,0,SEEK_END);
4708 return(MagickFalse);
4709 if ((MagickSizeType) offset >= extent)
4711 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4714 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4715 blob_info->file_info.file);
4716#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4717 if (blob_info->synchronize != MagickFalse)
4722 file=fileno(blob_info->file_info.file);
4723 if ((file == -1) || (offset < 0))
4724 return(MagickFalse);
4725 (void) posix_fallocate(file,offset,extent-offset);
4728 offset=SeekBlob(image,offset,SEEK_SET);
4730 return(MagickFalse);
4735 return(MagickFalse);
4737 return(MagickFalse);
4739 return(MagickFalse);
4742 if (extent != (MagickSizeType) ((
size_t) extent))
4743 return(MagickFalse);
4744 if (blob_info->mapped != MagickFalse)
4752 (void) UnmapBlob(blob_info->data,blob_info->length);
4753 RelinquishMagickResource(MapResource,blob_info->length);
4754 if (extent != (MagickSizeType) ((off_t) extent))
4755 return(MagickFalse);
4756 offset=SeekBlob(image,0,SEEK_END);
4758 return(MagickFalse);
4759 if ((MagickSizeType) offset >= extent)
4761 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4762 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4763 blob_info->file_info.file);
4764#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4765 if (blob_info->synchronize != MagickFalse)
4770 file=fileno(blob_info->file_info.file);
4771 if ((file == -1) || (offset < 0))
4772 return(MagickFalse);
4773 (void) posix_fallocate(file,offset,extent-offset);
4776 offset=SeekBlob(image,offset,SEEK_SET);
4778 return(MagickFalse);
4779 (void) AcquireMagickResource(MapResource,extent);
4780 blob_info->data=(
unsigned char*) MapBlob(fileno(
4781 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
4782 blob_info->extent=(size_t) extent;
4783 blob_info->length=(size_t) extent;
4784 (void) SyncBlob(image);
4787 blob_info->extent=(size_t) extent;
4788 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4789 blob_info->extent+1,
sizeof(*blob_info->data));
4790 (void) SyncBlob(image);
4791 if (blob_info->data == (
unsigned char *) NULL)
4793 (void) DetachBlob(blob_info);
4794 return(MagickFalse);
4826static int SyncBlob(
const Image *image)
4829 *magick_restrict blob_info;
4834 assert(image != (Image *) NULL);
4835 assert(image->signature == MagickCoreSignature);
4836 assert(image->blob != (BlobInfo *) NULL);
4837 if (IsEventLogging() != MagickFalse)
4838 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4839 if (EOFBlob(image) != 0)
4841 blob_info=image->blob;
4843 switch (blob_info->type)
4845 case UndefinedStream:
4846 case StandardStream:
4851 status=fflush(blob_info->file_info.file);
4856#if defined(MAGICKCORE_ZLIB_DELEGATE)
4857 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4863#if defined(MAGICKCORE_BZLIB_DELEGATE)
4864 status=BZ2_bzflush(blob_info->file_info.bzfile);
4898MagickExport MagickOffsetType TellBlob(
const Image *image)
4901 *magick_restrict blob_info;
4906 assert(image != (Image *) NULL);
4907 assert(image->signature == MagickCoreSignature);
4908 assert(image->blob != (BlobInfo *) NULL);
4909 assert(image->blob->type != UndefinedStream);
4910 if (IsEventLogging() != MagickFalse)
4911 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4912 blob_info=image->blob;
4914 switch (blob_info->type)
4916 case UndefinedStream:
4917 case StandardStream:
4921 offset=ftell(blob_info->file_info.file);
4928#if defined(MAGICKCORE_ZLIB_DELEGATE)
4929 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4939 offset=blob_info->offset;
4971MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
4973#if defined(MAGICKCORE_HAVE_MMAP)
4977 status=munmap(map,length);
4978 return(status == -1 ? MagickFalse : MagickTrue);
4982 return(MagickFalse);
5015MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5016 const unsigned char *data)
5019 *magick_restrict blob_info;
5033 assert(image != (Image *) NULL);
5034 assert(image->signature == MagickCoreSignature);
5035 assert(image->blob != (BlobInfo *) NULL);
5036 assert(image->blob->type != UndefinedStream);
5039 assert(data != (
const unsigned char *) NULL);
5040 blob_info=image->blob;
5042 p=(
const unsigned char *) data;
5043 q=(
unsigned char *) data;
5044 switch (blob_info->type)
5046 case UndefinedStream:
5048 case StandardStream:
5056 count=(ssize_t) fwrite((
const char *) data,1,length,
5057 blob_info->file_info.file);
5062 c=putc((
int) *p++,blob_info->file_info.file);
5070 c=putc((
int) *p++,blob_info->file_info.file);
5078 c=putc((
int) *p++,blob_info->file_info.file);
5086 c=putc((
int) *p++,blob_info->file_info.file);
5095 if ((count != (ssize_t) length) &&
5096 (ferror(blob_info->file_info.file) != 0))
5097 ThrowBlobException(blob_info);
5102#if defined(MAGICKCORE_ZLIB_DELEGATE)
5113 for (i=0; i < (ssize_t) length; i+=count)
5115 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5116 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5129 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5137 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5145 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5153 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5163 (void) gzerror(blob_info->file_info.gzfile,&status);
5164 if ((count != (ssize_t) length) && (status != Z_OK))
5165 ThrowBlobException(blob_info);
5171#if defined(MAGICKCORE_BZLIB_DELEGATE)
5178 for (i=0; i < (ssize_t) length; i+=count)
5180 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5181 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5191 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5192 if ((count != (ssize_t) length) && (status != BZ_OK))
5193 ThrowBlobException(blob_info);
5199 count=(ssize_t) blob_info->stream(image,data,length);
5207 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5212 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5213 if (extent >= blob_info->extent)
5215 extent+=blob_info->quantum+length;
5216 blob_info->quantum<<=1;
5217 if (SetBlobExtent(image,extent) == MagickFalse)
5220 q=blob_info->data+blob_info->offset;
5221 (void) memcpy(q,p,length);
5222 blob_info->offset+=length;
5223 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5224 blob_info->length=(size_t) blob_info->offset;
5225 count=(ssize_t) length;
5228 if (count != (ssize_t) length)
5229 ThrowBlobException(blob_info);
5258MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
5261 *magick_restrict blob_info;
5266 assert(image != (Image *) NULL);
5267 assert(image->signature == MagickCoreSignature);
5268 assert(image->blob != (BlobInfo *) NULL);
5269 assert(image->blob->type != UndefinedStream);
5270 blob_info=image->blob;
5272 switch (blob_info->type)
5274 case StandardStream:
5281 c=putc((
int) value,blob_info->file_info.file);
5284 if (ferror(blob_info->file_info.file) != 0)
5285 ThrowBlobException(blob_info);
5293 count=WriteBlobStream(image,1,&value);
5325MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
5336 quantum.unsigned_value=0U;
5337 quantum.float_value=value;
5338 return(WriteBlobLong(image,quantum.unsigned_value));
5366MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
5371 assert(image != (Image *) NULL);
5372 assert(image->signature == MagickCoreSignature);
5373 if (image->endian == LSBEndian)
5375 buffer[0]=(
unsigned char) value;
5376 buffer[1]=(
unsigned char) (value >> 8);
5377 buffer[2]=(
unsigned char) (value >> 16);
5378 buffer[3]=(
unsigned char) (value >> 24);
5379 return(WriteBlobStream(image,4,buffer));
5381 buffer[0]=(
unsigned char) (value >> 24);
5382 buffer[1]=(
unsigned char) (value >> 16);
5383 buffer[2]=(
unsigned char) (value >> 8);
5384 buffer[3]=(
unsigned char) value;
5385 return(WriteBlobStream(image,4,buffer));
5413MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
5418 assert(image != (Image *) NULL);
5419 assert(image->signature == MagickCoreSignature);
5420 if (image->endian == LSBEndian)
5422 buffer[0]=(
unsigned char) value;
5423 buffer[1]=(
unsigned char) (value >> 8);
5424 return(WriteBlobStream(image,2,buffer));
5426 buffer[0]=(
unsigned char) (value >> 8);
5427 buffer[1]=(
unsigned char) value;
5428 return(WriteBlobStream(image,2,buffer));
5456MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
5461 assert(image != (Image *) NULL);
5462 assert(image->signature == MagickCoreSignature);
5463 buffer[0]=(
unsigned char) value;
5464 buffer[1]=(
unsigned char) (value >> 8);
5465 buffer[2]=(
unsigned char) (value >> 16);
5466 buffer[3]=(
unsigned char) (value >> 24);
5467 return(WriteBlobStream(image,4,buffer));
5495MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
5500 assert(image != (Image *) NULL);
5501 assert(image->signature == MagickCoreSignature);
5502 buffer[0]=(
unsigned char) value;
5503 buffer[1]=(
unsigned char) (value >> 8);
5504 return(WriteBlobStream(image,2,buffer));
5532MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
5546 assert(image != (Image *) NULL);
5547 assert(image->signature == MagickCoreSignature);
5548 quantum.signed_value=value;
5549 buffer[0]=(
unsigned char) quantum.unsigned_value;
5550 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5551 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5552 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5553 return(WriteBlobStream(image,4,buffer));
5581MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5582 const signed short value)
5596 assert(image != (Image *) NULL);
5597 assert(image->signature == MagickCoreSignature);
5598 quantum.signed_value=value;
5599 buffer[0]=(
unsigned char) quantum.unsigned_value;
5600 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5601 return(WriteBlobStream(image,2,buffer));
5629MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
5634 assert(image != (Image *) NULL);
5635 assert(image->signature == MagickCoreSignature);
5636 buffer[0]=(
unsigned char) (value >> 24);
5637 buffer[1]=(
unsigned char) (value >> 16);
5638 buffer[2]=(
unsigned char) (value >> 8);
5639 buffer[3]=(
unsigned char) value;
5640 return(WriteBlobStream(image,4,buffer));
5668MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5669 const MagickSizeType value)
5674 assert(image != (Image *) NULL);
5675 assert(image->signature == MagickCoreSignature);
5676 buffer[0]=(
unsigned char) (value >> 56);
5677 buffer[1]=(
unsigned char) (value >> 48);
5678 buffer[2]=(
unsigned char) (value >> 40);
5679 buffer[3]=(
unsigned char) (value >> 32);
5680 buffer[4]=(
unsigned char) (value >> 24);
5681 buffer[5]=(
unsigned char) (value >> 16);
5682 buffer[6]=(
unsigned char) (value >> 8);
5683 buffer[7]=(
unsigned char) value;
5684 return(WriteBlobStream(image,8,buffer));
5712MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
5717 assert(image != (Image *) NULL);
5718 assert(image->signature == MagickCoreSignature);
5719 buffer[0]=(
unsigned char) (value >> 8);
5720 buffer[1]=(
unsigned char) value;
5721 return(WriteBlobStream(image,2,buffer));
5749MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,
const signed int value)
5763 assert(image != (Image *) NULL);
5764 assert(image->signature == MagickCoreSignature);
5765 quantum.signed_value=value;
5766 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5767 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5768 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5769 buffer[3]=(
unsigned char) quantum.unsigned_value;
5770 return(WriteBlobStream(image,4,buffer));
5798MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5799 const signed short value)
5813 assert(image != (Image *) NULL);
5814 assert(image->signature == MagickCoreSignature);
5815 quantum.signed_value=value;
5816 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5817 buffer[1]=(
unsigned char) quantum.unsigned_value;
5818 return(WriteBlobStream(image,2,buffer));
5846MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
5848 assert(image != (Image *) NULL);
5849 assert(image->signature == MagickCoreSignature);
5850 assert(
string != (
const char *) NULL);
5851 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));