47#include "magick/studio.h"
48#include "magick/blob.h"
49#include "magick/blob-private.h"
50#include "magick/cache.h"
51#include "magick/client.h"
52#include "magick/constitute.h"
53#include "magick/delegate.h"
54#include "magick/exception.h"
55#include "magick/exception-private.h"
56#include "magick/geometry.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/locale_.h"
60#include "magick/log.h"
61#include "magick/magick.h"
62#include "magick/memory_.h"
63#include "magick/nt-base-private.h"
64#include "magick/option.h"
65#include "magick/policy.h"
66#include "magick/resource_.h"
67#include "magick/semaphore.h"
68#include "magick/string_.h"
69#include "magick/string-private.h"
70#include "magick/timer-private.h"
71#include "magick/token.h"
72#include "magick/utility.h"
73#include "magick/utility-private.h"
74#if defined(MAGICKCORE_ZLIB_DELEGATE)
77#if defined(MAGICKCORE_BZLIB_DELEGATE)
84#define MagickMaxBlobExtent (8*8192)
85#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
86# define MAP_ANONYMOUS MAP_ANON
88#if !defined(MAP_FAILED)
89#define MAP_FAILED ((void *) -1)
93#define _O_BINARY O_BINARY
95#if defined(MAGICKCORE_WINDOWS_SUPPORT)
100# define MAGICKCORE_HAVE_MMAP 1
101# define mmap(address,length,protection,access,file,offset) \
102 NTMapMemory(address,length,protection,access,file,offset)
105# define munmap(address,length) NTUnmapMemory(address,length)
108# define pclose _pclose
123#if defined(MAGICKCORE_ZLIB_DELEGATE)
128#if defined(MAGICKCORE_BZLIB_DELEGATE)
198 SyncBlob(
const Image *);
227MagickExport
void AttachBlob(BlobInfo *blob_info,
const void *blob,
230 assert(blob_info != (BlobInfo *) NULL);
231 if (IsEventLogging() != MagickFalse)
232 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
233 blob_info->length=length;
234 blob_info->extent=length;
235 blob_info->quantum=(size_t) MagickMaxBlobExtent;
237 blob_info->type=BlobStream;
238 blob_info->file_info.file=(FILE *) NULL;
239 blob_info->data=(
unsigned char *) blob;
240 blob_info->mapped=MagickFalse;
274MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
275 const size_t length,ExceptionInfo *exception)
286 assert(filename != (
const char *) NULL);
287 assert(blob != (
const void *) NULL);
288 if (IsEventLogging() != MagickFalse)
289 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
290 if (*filename ==
'\0')
291 file=AcquireUniqueFileResource(filename);
293 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
296 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
299 for (i=0; i < length; i+=(size_t) count)
301 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
302 MagickMaxBufferExtent));
310 file=close_utf8(file);
311 if ((file == -1) || (i < length))
313 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
350MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
351 const size_t length,ExceptionInfo *exception)
366 assert(image_info != (ImageInfo *) NULL);
367 assert(image_info->signature == MagickCoreSignature);
368 assert(exception != (ExceptionInfo *) NULL);
369 if (IsEventLogging() != MagickFalse)
370 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
371 image_info->filename);
372 if ((blob == (
const void *) NULL) || (length == 0))
374 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
375 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
376 return((Image *) NULL);
378 blob_info=CloneImageInfo(image_info);
379 blob_info->blob=(
void *) blob;
380 blob_info->length=length;
381 if (*blob_info->magick ==
'\0')
382 (void) SetImageInfo(blob_info,0,exception);
383 magick_info=GetMagickInfo(blob_info->magick,exception);
384 if (magick_info == (
const MagickInfo *) NULL)
386 (void) ThrowMagickException(exception,GetMagickModule(),
387 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
389 blob_info=DestroyImageInfo(blob_info);
390 return((Image *) NULL);
392 if (GetMagickBlobSupport(magick_info) != MagickFalse)
395 filename[MagickPathExtent];
400 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
401 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
402 blob_info->magick,filename);
403 image=ReadImage(blob_info,exception);
404 if (image != (Image *) NULL)
405 (void) DetachBlob(image->blob);
406 blob_info=DestroyImageInfo(blob_info);
412 blob_info->blob=(
void *) NULL;
414 *blob_info->filename=
'\0';
415 status=BlobToFile(blob_info->filename,blob,length,exception);
416 if (status == MagickFalse)
418 (void) RelinquishUniqueFileResource(blob_info->filename);
419 blob_info=DestroyImageInfo(blob_info);
420 return((Image *) NULL);
422 clone_info=CloneImageInfo(blob_info);
423 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
424 blob_info->magick,blob_info->filename);
425 image=ReadImage(clone_info,exception);
426 if (image != (Image *) NULL)
434 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
436 (void) CopyMagickString(images->filename,image_info->filename,
438 (void) CopyMagickString(images->magick_filename,image_info->filename,
440 (void) CopyMagickString(images->magick,magick_info->name,
442 images=GetNextImageInList(images);
445 clone_info=DestroyImageInfo(clone_info);
446 (void) RelinquishUniqueFileResource(blob_info->filename);
447 blob_info=DestroyImageInfo(blob_info);
474MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
482 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
483 GetBlobInfo(clone_info);
484 if (blob_info == (BlobInfo *) NULL)
486 semaphore=clone_info->semaphore;
487 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
488 if (blob_info->mapped != MagickFalse)
489 (void) AcquireMagickResource(MapResource,blob_info->length);
490 clone_info->semaphore=semaphore;
491 LockSemaphoreInfo(clone_info->semaphore);
492 clone_info->reference_count=1;
493 UnlockSemaphoreInfo(clone_info->semaphore);
520static inline void ThrowBlobException(BlobInfo *blob_info)
522 if ((blob_info->status == 0) && (errno != 0))
523 blob_info->error_number=errno;
524 blob_info->status=(-1);
527MagickExport MagickBooleanType CloseBlob(Image *image)
530 *magick_restrict blob_info;
538 assert(image != (Image *) NULL);
539 assert(image->signature == MagickCoreSignature);
540 if (IsEventLogging() != MagickFalse)
541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
542 blob_info=image->blob;
543 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
545 (void) SyncBlob(image);
546 status=blob_info->status;
547 switch (blob_info->type)
549 case UndefinedStream:
555 if (blob_info->synchronize != MagickFalse)
557 status=fflush(blob_info->file_info.file);
559 ThrowBlobException(blob_info);
560 status=fsync(fileno(blob_info->file_info.file));
562 ThrowBlobException(blob_info);
564 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
565 ThrowBlobException(blob_info);
570#if defined(MAGICKCORE_ZLIB_DELEGATE)
572 (void) gzerror(blob_info->file_info.gzfile,&status);
574 ThrowBlobException(blob_info);
580#if defined(MAGICKCORE_BZLIB_DELEGATE)
582 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
584 ThrowBlobException(blob_info);
592 if (blob_info->file_info.file != (FILE *) NULL)
594 if (blob_info->synchronize != MagickFalse)
596 status=fflush(blob_info->file_info.file);
598 ThrowBlobException(blob_info);
599 status=fsync(fileno(blob_info->file_info.file));
601 ThrowBlobException(blob_info);
603 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
604 ThrowBlobException(blob_info);
609 blob_info->size=GetBlobSize(image);
610 image->extent=blob_info->size;
611 blob_info->eof=MagickFalse;
613 blob_info->mode=UndefinedBlobMode;
614 if (blob_info->exempt != MagickFalse)
616 blob_info->type=UndefinedStream;
617 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
619 switch (blob_info->type)
621 case UndefinedStream:
626 if (blob_info->file_info.file != (FILE *) NULL)
628 status=fclose(blob_info->file_info.file);
630 ThrowBlobException(blob_info);
636#if defined(MAGICKCORE_HAVE_PCLOSE)
637 status=pclose(blob_info->file_info.file);
639 ThrowBlobException(blob_info);
645#if defined(MAGICKCORE_ZLIB_DELEGATE)
646 status=gzclose(blob_info->file_info.gzfile);
648 ThrowBlobException(blob_info);
654#if defined(MAGICKCORE_BZLIB_DELEGATE)
655 BZ2_bzclose(blob_info->file_info.bzfile);
663 if (blob_info->file_info.file != (FILE *) NULL)
665 status=fclose(blob_info->file_info.file);
667 ThrowBlobException(blob_info);
672 (void) DetachBlob(blob_info);
673 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
698MagickExport
void DestroyBlob(Image *image)
701 *magick_restrict blob_info;
706 assert(image != (Image *) NULL);
707 assert(image->signature == MagickCoreSignature);
708 assert(image->blob != (BlobInfo *) NULL);
709 assert(image->blob->signature == MagickCoreSignature);
710 if (IsEventLogging() != MagickFalse)
711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
712 blob_info=image->blob;
714 LockSemaphoreInfo(blob_info->semaphore);
715 blob_info->reference_count--;
716 assert(blob_info->reference_count >= 0);
717 if (blob_info->reference_count == 0)
719 UnlockSemaphoreInfo(blob_info->semaphore);
720 if (destroy == MagickFalse)
722 image->blob=(BlobInfo *) NULL;
725 (void) CloseBlob(image);
726 if (blob_info->mapped != MagickFalse)
728 (void) UnmapBlob(blob_info->data,blob_info->length);
729 RelinquishMagickResource(MapResource,blob_info->length);
732 DestroySemaphoreInfo(&blob_info->semaphore);
733 blob_info->signature=(~MagickCoreSignature);
734 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
759MagickExport
unsigned char *DetachBlob(BlobInfo *blob_info)
764 assert(blob_info != (BlobInfo *) NULL);
765 if (IsEventLogging() != MagickFalse)
766 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
767 if (blob_info->mapped != MagickFalse)
769 (void) UnmapBlob(blob_info->data,blob_info->length);
770 blob_info->data=NULL;
771 RelinquishMagickResource(MapResource,blob_info->length);
773 blob_info->mapped=MagickFalse;
776 blob_info->mode=UndefinedBlobMode;
777 blob_info->eof=MagickFalse;
779 blob_info->exempt=MagickFalse;
780 blob_info->type=UndefinedStream;
781 blob_info->file_info.file=(FILE *) NULL;
782 data=blob_info->data;
783 blob_info->data=(
unsigned char *) NULL;
784 blob_info->stream=(StreamHandler) NULL;
812MagickPrivate
void DisassociateBlob(Image *image)
815 *magick_restrict blob_info,
821 assert(image != (Image *) NULL);
822 assert(image->signature == MagickCoreSignature);
823 assert(image->blob != (BlobInfo *) NULL);
824 assert(image->blob->signature == MagickCoreSignature);
825 if (IsEventLogging() != MagickFalse)
826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
827 blob_info=image->blob;
829 LockSemaphoreInfo(blob_info->semaphore);
830 assert(blob_info->reference_count >= 0);
831 if (blob_info->reference_count > 1)
833 UnlockSemaphoreInfo(blob_info->semaphore);
834 if (clone == MagickFalse)
836 clone_info=CloneBlobInfo(blob_info);
838 image->blob=clone_info;
866MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
867 const MagickSizeType length)
879 buffer[MagickMinBufferExtent >> 1];
881 assert(image != (Image *) NULL);
882 assert(image->signature == MagickCoreSignature);
883 if (length != (MagickSizeType) ((MagickOffsetType) length))
886 for (i=0; i < length; i+=(MagickSizeType) count)
888 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
889 (void) ReadBlobStream(image,quantum,buffer,&count);
897 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
924MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
926 assert(image != (Image *) NULL);
927 assert(image->signature == MagickCoreSignature);
928 assert(duplicate != (Image *) NULL);
929 assert(duplicate->signature == MagickCoreSignature);
930 if (IsEventLogging() != MagickFalse)
931 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
933 image->blob=ReferenceBlob(duplicate->blob);
959MagickExport
int EOFBlob(
const Image *image)
962 *magick_restrict blob_info;
964 assert(image != (Image *) NULL);
965 assert(image->signature == MagickCoreSignature);
966 assert(image->blob != (BlobInfo *) NULL);
967 assert(image->blob->type != UndefinedStream);
968 if (IsEventLogging() != MagickFalse)
969 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
970 blob_info=image->blob;
971 switch (blob_info->type)
973 case UndefinedStream:
979 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
985#if defined(MAGICKCORE_ZLIB_DELEGATE)
986 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
993#if defined(MAGICKCORE_BZLIB_DELEGATE)
998 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
999 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1005 blob_info->eof=MagickFalse;
1011 return((
int) blob_info->eof);
1037MagickExport
int ErrorBlob(
const Image *image)
1040 *magick_restrict blob_info;
1042 assert(image != (Image *) NULL);
1043 assert(image->signature == MagickCoreSignature);
1044 assert(image->blob != (BlobInfo *) NULL);
1045 assert(image->blob->type != UndefinedStream);
1046 if (IsEventLogging() != MagickFalse)
1047 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1048 blob_info=image->blob;
1049 switch (blob_info->type)
1051 case UndefinedStream:
1052 case StandardStream:
1057 blob_info->error=ferror(blob_info->file_info.file);
1062#if defined(MAGICKCORE_ZLIB_DELEGATE)
1063 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1069#if defined(MAGICKCORE_BZLIB_DELEGATE)
1070 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1082 return(blob_info->error);
1120MagickExport
unsigned char *FileToBlob(
const char *filename,
const size_t extent,
1121 size_t *length,ExceptionInfo *exception)
1147 assert(filename != (
const char *) NULL);
1148 assert(exception != (ExceptionInfo *) NULL);
1149 assert(exception->signature == MagickCoreSignature);
1150 if (IsEventLogging() != MagickFalse)
1151 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1153 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1154 if (status == MagickFalse)
1157 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1158 "NotAuthorized",
"`%s'",filename);
1162 if (LocaleCompare(filename,
"-") != 0)
1165 flags = O_RDONLY | O_BINARY;
1167 status=GetPathAttributes(filename,&attributes);
1168 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1170 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1173#if defined(O_NOFOLLOW)
1174 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1175 if (status == MagickFalse)
1178 file=open_utf8(filename,flags,0);
1182 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1183 return((
unsigned char *) NULL);
1185 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1186 if (status == MagickFalse)
1188 file=close_utf8(file)-1;
1190 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1191 "NotAuthorized",
"`%s'",filename);
1194 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1196 if ((file == fileno(stdin)) || (offset < 0) ||
1197 (offset != (MagickOffsetType) ((ssize_t) offset)))
1208 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1209 quantum=(size_t) MagickMaxBufferExtent;
1210 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1211 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1212 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1213 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1215 count=read(file,blob+i,quantum);
1222 if (~((
size_t) i) < (count+quantum+1))
1224 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1227 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+count+quantum+1,
1229 if ((
size_t) (i+count) >= extent)
1232 if (LocaleCompare(filename,
"-") != 0)
1233 file=close_utf8(file);
1234 if (blob == (
unsigned char *) NULL)
1236 (void) ThrowMagickException(exception,GetMagickModule(),
1237 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1238 return((
unsigned char *) NULL);
1242 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1243 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1244 return((
unsigned char *) NULL);
1246 *length=(size_t) MagickMin(i+count,extent);
1250 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1251 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1252 blob=(
unsigned char *) NULL;
1253 if (~(*length) >= (MagickPathExtent-1))
1254 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1256 if (blob == (
unsigned char *) NULL)
1258 file=close_utf8(file);
1259 (void) ThrowMagickException(exception,GetMagickModule(),
1260 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1261 return((
unsigned char *) NULL);
1263 map=MapBlob(file,ReadMode,0,*length);
1264 if (map != (
unsigned char *) NULL)
1266 (void) memcpy(blob,map,*length);
1267 (void) UnmapBlob(map,*length);
1271 (void) lseek(file,0,SEEK_SET);
1272 for (i=0; i < *length; i+=count)
1274 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1275 MagickMaxBufferExtent));
1285 file=close_utf8(file)-1;
1286 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1287 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1288 return((
unsigned char *) NULL);
1292 if (LocaleCompare(filename,
"-") != 0)
1293 file=close_utf8(file);
1296 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1297 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1327static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1328 const unsigned char *magick_restrict data)
1331 *magick_restrict blob_info;
1339 assert(image->blob != (BlobInfo *) NULL);
1340 assert(image->blob->type != UndefinedStream);
1341 assert(data != (
void *) NULL);
1342 blob_info=image->blob;
1343 if (blob_info->type != BlobStream)
1344 return(WriteBlob(image,length,data));
1345 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1350 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1351 if (extent >= blob_info->extent)
1353 extent+=blob_info->quantum+length;
1354 blob_info->quantum<<=1;
1355 if (SetBlobExtent(image,extent) == MagickFalse)
1358 q=blob_info->data+blob_info->offset;
1359 (void) memcpy(q,data,length);
1360 blob_info->offset+=length;
1361 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1362 blob_info->length=(size_t) blob_info->offset;
1363 return((ssize_t) length);
1366MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename)
1387 assert(image != (
const Image *) NULL);
1388 assert(image->signature == MagickCoreSignature);
1389 assert(filename != (
const char *) NULL);
1390 if (IsEventLogging() != MagickFalse)
1391 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1392 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1393 if (status == MagickFalse)
1396 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1397 PolicyError,
"NotAuthorized",
"`%s'",filename);
1398 return(MagickFalse);
1401 if (LocaleCompare(filename,
"-") != 0)
1404 flags = O_RDONLY | O_BINARY;
1406#if defined(O_NOFOLLOW)
1407 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1408 if (status == MagickFalse)
1411 file=open_utf8(filename,flags,0);
1415 ThrowFileException(&image->exception,BlobError,
"UnableToOpenBlob",
1417 return(MagickFalse);
1419 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1420 if (status == MagickFalse)
1423 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1424 PolicyError,
"NotAuthorized",
"`%s'",filename);
1425 return(MagickFalse);
1427 quantum=(size_t) MagickMaxBufferExtent;
1428 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1429 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1430 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1431 if (blob == (
unsigned char *) NULL)
1433 file=close_utf8(file);
1434 ThrowFileException(&image->exception,ResourceLimitError,
1435 "MemoryAllocationFailed",filename);
1436 return(MagickFalse);
1440 count=read(file,blob,quantum);
1447 length=(size_t) count;
1448 count=WriteBlobStream(image,length,blob);
1449 if (count != (ssize_t) length)
1451 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1456 file=close_utf8(file);
1458 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1460 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1487MagickExport MagickBooleanType GetBlobError(
const Image *image)
1489 assert(image != (
const Image *) NULL);
1490 assert(image->signature == MagickCoreSignature);
1491 if (IsEventLogging() != MagickFalse)
1492 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1493 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1494 errno=image->blob->error_number;
1495 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1520MagickExport FILE *GetBlobFileHandle(
const Image *image)
1522 assert(image != (
const Image *) NULL);
1523 assert(image->signature == MagickCoreSignature);
1524 return(image->blob->file_info.file);
1549MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1551 assert(blob_info != (BlobInfo *) NULL);
1552 (void) memset(blob_info,0,
sizeof(*blob_info));
1553 blob_info->type=UndefinedStream;
1554 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1555 blob_info->properties.st_mtime=GetMagickTime();
1556 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1557 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1558 blob_info->reference_count=1;
1559 blob_info->semaphore=AllocateSemaphoreInfo();
1560 blob_info->signature=MagickCoreSignature;
1585MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1587 assert(image != (Image *) NULL);
1588 assert(image->signature == MagickCoreSignature);
1589 if (IsEventLogging() != MagickFalse)
1590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1591 return(&image->blob->properties);
1617MagickExport MagickSizeType GetBlobSize(
const Image *image)
1620 *magick_restrict blob_info;
1625 assert(image != (Image *) NULL);
1626 assert(image->signature == MagickCoreSignature);
1627 assert(image->blob != (BlobInfo *) NULL);
1628 if (IsEventLogging() != MagickFalse)
1629 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1630 blob_info=image->blob;
1632 switch (blob_info->type)
1634 case UndefinedStream:
1635 case StandardStream:
1637 extent=blob_info->size;
1645 extent=(MagickSizeType) blob_info->properties.st_size;
1647 extent=blob_info->size;
1648 file_descriptor=fileno(blob_info->file_info.file);
1649 if (file_descriptor == -1)
1651 if (fstat(file_descriptor,&blob_info->properties) == 0)
1652 extent=(MagickSizeType) blob_info->properties.st_size;
1657 extent=blob_info->size;
1666 status=GetPathAttributes(image->filename,&blob_info->properties);
1667 if (status != MagickFalse)
1668 extent=(MagickSizeType) blob_info->properties.st_size;
1675 extent=(MagickSizeType) blob_info->length;
1704MagickExport
unsigned char *GetBlobStreamData(
const Image *image)
1706 assert(image != (
const Image *) NULL);
1707 assert(image->signature == MagickCoreSignature);
1708 return(image->blob->data);
1733MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1735 assert(image != (
const Image *) NULL);
1736 assert(image->signature == MagickCoreSignature);
1737 if (IsEventLogging() != MagickFalse)
1738 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1739 return(image->blob->stream);
1775MagickExport
unsigned char *ImageToBlob(
const ImageInfo *image_info,
1776 Image *image,
size_t *length,ExceptionInfo *exception)
1790 assert(image_info != (
const ImageInfo *) NULL);
1791 assert(image_info->signature == MagickCoreSignature);
1792 assert(image != (Image *) NULL);
1793 assert(image->signature == MagickCoreSignature);
1794 assert(exception != (ExceptionInfo *) NULL);
1795 assert(exception->signature == MagickCoreSignature);
1796 if (IsEventLogging() != MagickFalse)
1797 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1798 image_info->filename);
1800 blob=(
unsigned char *) NULL;
1801 blob_info=CloneImageInfo(image_info);
1802 blob_info->adjoin=MagickFalse;
1803 (void) SetImageInfo(blob_info,1,exception);
1804 if (*blob_info->magick !=
'\0')
1805 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
1806 magick_info=GetMagickInfo(image->magick,exception);
1807 if (magick_info == (
const MagickInfo *) NULL)
1809 (void) ThrowMagickException(exception,GetMagickModule(),
1810 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
1812 blob_info=DestroyImageInfo(blob_info);
1815 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
1816 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1821 blob_info->length=0;
1822 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1823 sizeof(
unsigned char));
1824 if (blob_info->blob == NULL)
1825 (void) ThrowMagickException(exception,GetMagickModule(),
1826 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1829 (void) CloseBlob(image);
1830 image->blob->exempt=MagickTrue;
1831 *image->filename=
'\0';
1832 status=WriteImage(blob_info,image);
1833 InheritException(exception,&image->exception);
1834 *length=image->blob->length;
1835 blob=DetachBlob(image->blob);
1836 if (blob != (
void *) NULL)
1838 if (status == MagickFalse)
1839 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1841 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
1842 sizeof(
unsigned char));
1844 else if (status == MagickFalse)
1845 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
1851 unique[MagickPathExtent];
1859 file=AcquireUniqueFileResource(unique);
1862 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
1863 image_info->filename);
1867 blob_info->file=fdopen(file,
"wb");
1868 if (blob_info->file != (FILE *) NULL)
1870 (void) FormatLocaleString(image->filename,MagickPathExtent,
1871 "%s:%s",image->magick,unique);
1872 status=WriteImage(blob_info,image);
1873 (void) fclose(blob_info->file);
1874 if (status == MagickFalse)
1875 InheritException(exception,&image->exception);
1877 blob=FileToBlob(unique,SIZE_MAX,length,exception);
1879 (void) RelinquishUniqueFileResource(unique);
1882 blob_info=DestroyImageInfo(blob_info);
1914MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
1915 ExceptionInfo *exception)
1939 assert(image != (Image *) NULL);
1940 assert(image->signature == MagickCoreSignature);
1941 assert(image->blob != (BlobInfo *) NULL);
1942 assert(image->blob->type != UndefinedStream);
1943 assert(filename != (
const char *) NULL);
1944 if (IsEventLogging() != MagickFalse)
1945 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1946 if (*filename ==
'\0')
1947 file=AcquireUniqueFileResource(filename);
1949 if (LocaleCompare(filename,
"-") == 0)
1950 file=fileno(stdout);
1952 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
1955 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1956 return(MagickFalse);
1958 quantum=(size_t) MagickMaxBufferExtent;
1959 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1960 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1961 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
1962 if (buffer == (
unsigned char *) NULL)
1964 file=close_utf8(file)-1;
1965 (void) ThrowMagickException(exception,GetMagickModule(),
1966 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
1967 return(MagickFalse);
1970 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1971 for (i=0; count > 0; )
1973 length=(size_t) count;
1974 for (i=0; i < length; i+=count)
1976 count=write(file,p+i,(
size_t) (length-i));
1986 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1988 if (LocaleCompare(filename,
"-") != 0)
1989 file=close_utf8(file);
1990 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
1991 if ((file == -1) || (i < length))
1994 file=close_utf8(file);
1995 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1996 return(MagickFalse);
2036MagickExport
unsigned char *ImagesToBlob(
const ImageInfo *image_info,
2037 Image *images,
size_t *length,ExceptionInfo *exception)
2051 assert(image_info != (
const ImageInfo *) NULL);
2052 assert(image_info->signature == MagickCoreSignature);
2053 assert(images != (Image *) NULL);
2054 assert(images->signature == MagickCoreSignature);
2055 assert(exception != (ExceptionInfo *) NULL);
2056 if (IsEventLogging() != MagickFalse)
2057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2058 image_info->filename);
2060 blob=(
unsigned char *) NULL;
2061 blob_info=CloneImageInfo(image_info);
2062 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2064 if (*blob_info->magick !=
'\0')
2065 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2066 magick_info=GetMagickInfo(images->magick,exception);
2067 if (magick_info == (
const MagickInfo *) NULL)
2069 (void) ThrowMagickException(exception,GetMagickModule(),
2070 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2072 blob_info=DestroyImageInfo(blob_info);
2075 if (GetMagickAdjoin(magick_info) == MagickFalse)
2077 blob_info=DestroyImageInfo(blob_info);
2078 return(ImageToBlob(image_info,images,length,exception));
2080 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2081 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2086 blob_info->length=0;
2087 blob_info->blob=(
void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2088 sizeof(
unsigned char));
2089 if (blob_info->blob == (
void *) NULL)
2090 (void) ThrowMagickException(exception,GetMagickModule(),
2091 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2094 (void) CloseBlob(images);
2095 images->blob->exempt=MagickTrue;
2096 *images->filename=
'\0';
2097 status=WriteImages(blob_info,images,images->filename,exception);
2098 *length=images->blob->length;
2099 blob=DetachBlob(images->blob);
2100 if (blob != (
void *) NULL)
2102 if (status == MagickFalse)
2103 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2105 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
2106 sizeof(
unsigned char));
2109 if (status == MagickFalse)
2110 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2116 filename[MagickPathExtent],
2117 unique[MagickPathExtent];
2125 file=AcquireUniqueFileResource(unique);
2128 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2129 image_info->filename);
2133 blob_info->file=fdopen(file,
"wb");
2134 if (blob_info->file != (FILE *) NULL)
2136 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2137 images->magick,unique);
2138 status=WriteImages(blob_info,images,filename,exception);
2139 (void) fclose(blob_info->file);
2140 if (status == MagickFalse)
2141 InheritException(exception,&images->exception);
2143 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2145 (void) RelinquishUniqueFileResource(unique);
2148 blob_info=DestroyImageInfo(blob_info);
2184MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2185 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2188 filename[MagickPathExtent];
2217 assert(image_info != (ImageInfo *) NULL);
2218 assert(image_info->signature == MagickCoreSignature);
2219 assert(image != (Image *) NULL);
2220 assert(image->signature == MagickCoreSignature);
2221 assert(inject_image != (Image *) NULL);
2222 assert(inject_image->signature == MagickCoreSignature);
2223 assert(exception != (ExceptionInfo *) NULL);
2224 if (IsEventLogging() != MagickFalse)
2225 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2226 unique_file=(FILE *) NULL;
2227 file=AcquireUniqueFileResource(filename);
2229 unique_file=fdopen(file,
"wb");
2230 if ((file == -1) || (unique_file == (FILE *) NULL))
2232 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2233 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2235 return(MagickFalse);
2237 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2238 if (byte_image == (Image *) NULL)
2240 (void) fclose(unique_file);
2241 (void) RelinquishUniqueFileResource(filename);
2242 return(MagickFalse);
2244 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2246 DestroyBlob(byte_image);
2247 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2248 write_info=CloneImageInfo(image_info);
2249 SetImageInfoFile(write_info,unique_file);
2250 status=WriteImage(write_info,byte_image);
2251 write_info=DestroyImageInfo(write_info);
2252 byte_image=DestroyImage(byte_image);
2253 (void) fclose(unique_file);
2254 if (status == MagickFalse)
2256 (void) RelinquishUniqueFileResource(filename);
2257 return(MagickFalse);
2262 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2265 (void) RelinquishUniqueFileResource(filename);
2266 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2267 image_info->filename);
2268 return(MagickFalse);
2270 quantum=(size_t) MagickMaxBufferExtent;
2271 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2272 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2273 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2274 if (buffer == (
unsigned char *) NULL)
2276 (void) RelinquishUniqueFileResource(filename);
2277 file=close_utf8(file);
2278 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2283 ssize_t count = read(file,buffer,quantum);
2290 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2293 file=close_utf8(file);
2295 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2296 (void) RelinquishUniqueFileResource(filename);
2297 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2323MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2325 assert(image != (
const Image *) NULL);
2326 assert(image->signature == MagickCoreSignature);
2327 if (IsEventLogging() != MagickFalse)
2328 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2329 return(image->blob->exempt);
2354MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2357 *magick_restrict blob_info;
2359 assert(image != (
const Image *) NULL);
2360 assert(image->signature == MagickCoreSignature);
2361 if (IsEventLogging() != MagickFalse)
2362 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2363 blob_info=image->blob;
2364 switch (blob_info->type)
2373 if (blob_info->file_info.file == (FILE *) NULL)
2374 return(MagickFalse);
2375 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2376 return(status == -1 ? MagickFalse : MagickTrue);
2380#if defined(MAGICKCORE_ZLIB_DELEGATE)
2384 if (blob_info->file_info.gzfile == (gzFile) NULL)
2385 return(MagickFalse);
2386 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2387 return(offset < 0 ? MagickFalse : MagickTrue);
2392 case UndefinedStream:
2396 case StandardStream:
2401 return(MagickFalse);
2426MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2428 assert(image != (
const Image *) NULL);
2429 assert(image->signature == MagickCoreSignature);
2430 if (IsEventLogging() != MagickFalse)
2431 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2432 return(image->blob->temporary);
2464MagickExport
unsigned char *MapBlob(
int file,
const MapMode mode,
2465 const MagickOffsetType offset,
const size_t length)
2467#if defined(MAGICKCORE_HAVE_MMAP)
2480#if defined(MAP_ANONYMOUS)
2481 flags|=MAP_ANONYMOUS;
2483 return((
unsigned char *) NULL);
2490 protection=PROT_READ;
2496 protection=PROT_WRITE;
2502 protection=PROT_READ | PROT_WRITE;
2507#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2508 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,offset);
2510 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags |
2511 MAP_HUGETLB,file,offset);
2512 if (map == (
unsigned char *) MAP_FAILED)
2513 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,
2516 if (map == (
unsigned char *) MAP_FAILED)
2517 return((
unsigned char *) NULL);
2524 return((
unsigned char *) NULL);
2553MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
2562 assert(buffer != (
unsigned char *) NULL);
2569 *buffer++=(
unsigned char) c;
2573 *buffer++=(
unsigned char) c;
2603MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
2611 assert(p != (
unsigned char *) NULL);
2618 *p++=(
unsigned char) c;
2654static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
2666 size=MagickMinBufferExtent;
2667 option=GetImageOption(image_info,
"stream:buffer-size");
2668 if (option != (
const char *) NULL)
2669 size=StringToUnsignedLong(option);
2670 status=setvbuf(image->blob->file_info.file,(
char *) NULL,size == 0 ?
2671 _IONBF : _IOFBF,size);
2672 return(status == 0 ? MagickTrue : MagickFalse);
2675#if defined(MAGICKCORE_ZLIB_DELEGATE)
2676static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
2678#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2679 return(gzopen(path,mode));
2687 path_wide=NTCreateWidePath(path);
2688 if (path_wide == (
wchar_t *) NULL)
2689 return((gzFile) NULL);
2690 file=gzopen_w(path_wide,mode);
2691 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
2697MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
2698 Image *image,
const BlobMode mode,ExceptionInfo *exception)
2701 *magick_restrict blob_info;
2704 extension[MagickPathExtent],
2705 filename[MagickPathExtent];
2719 assert(image_info != (ImageInfo *) NULL);
2720 assert(image_info->signature == MagickCoreSignature);
2721 assert(image != (Image *) NULL);
2722 assert(image->signature == MagickCoreSignature);
2723 if (IsEventLogging() != MagickFalse)
2724 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2725 image_info->filename);
2726 blob_info=image->blob;
2727 if (image_info->blob != (
void *) NULL)
2729 if (image_info->stream != (StreamHandler) NULL)
2730 blob_info->stream=(StreamHandler) image_info->stream;
2731 AttachBlob(blob_info,image_info->blob,image_info->length);
2734 (void) DetachBlob(blob_info);
2735 blob_info->mode=mode;
2741 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
2745 case ReadBinaryBlobMode:
2747 flags=O_RDONLY | O_BINARY;
2749 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
2754 flags=O_WRONLY | O_CREAT | O_TRUNC;
2756 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
2759 case WriteBinaryBlobMode:
2761 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
2763 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
2764 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
2765 MagickTrue : MagickFalse;
2768 case AppendBlobMode:
2770 flags=O_WRONLY | O_CREAT | O_APPEND;
2772 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
2775 case AppendBinaryBlobMode:
2777 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
2779 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
2780 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
2781 MagickTrue : MagickFalse;
2788 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
2792#if defined(O_NOFOLLOW)
2793 if (status == MagickFalse)
2797 blob_info->synchronize=image_info->synchronize;
2798 if (image_info->stream != (StreamHandler) NULL)
2800 blob_info->stream=(StreamHandler) image_info->stream;
2803 blob_info->type=FifoStream;
2811 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2812 rights=ReadPolicyRights;
2814 rights=WritePolicyRights;
2815 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2818 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2819 "NotAuthorized",
"`%s'",filename);
2820 return(MagickFalse);
2822 if ((LocaleCompare(filename,
"-") == 0) ||
2823 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
2825 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
2826#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2827 if (strchr(type,
'b') != (
char *) NULL)
2828 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2830 blob_info->type=StandardStream;
2831 blob_info->exempt=MagickTrue;
2832 return(SetStreamBuffering(image_info,image));
2834 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
2835 (IsGeometry(filename+3) != MagickFalse))
2838 fileMode[MagickPathExtent];
2842 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2843 if (blob_info->file_info.file == (FILE *) NULL)
2845 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2846 return(MagickFalse);
2848#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2849 if (strchr(type,
'b') != (
char *) NULL)
2850 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2852 blob_info->type=FileStream;
2853 blob_info->exempt=MagickTrue;
2854 return(SetStreamBuffering(image_info,image));
2856#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2857 if (*filename ==
'|')
2860 fileMode[MagickPathExtent],
2868 (void) signal(SIGPIPE,SIG_IGN);
2872 sanitize_command=SanitizeString(filename+1);
2873 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2875 sanitize_command=DestroyString(sanitize_command);
2876 if (blob_info->file_info.file == (FILE *) NULL)
2878 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2879 return(MagickFalse);
2881 blob_info->type=PipeStream;
2882 blob_info->exempt=MagickTrue;
2883 return(SetStreamBuffering(image_info,image));
2886 status=GetPathAttributes(filename,&blob_info->properties);
2887#if defined(S_ISFIFO)
2888 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2890 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2891 if (blob_info->file_info.file == (FILE *) NULL)
2893 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2894 return(MagickFalse);
2896 blob_info->type=FileStream;
2897 blob_info->exempt=MagickTrue;
2898 return(SetStreamBuffering(image_info,image));
2901 GetPathComponent(image->filename,ExtensionPath,extension);
2904 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2905 if ((image_info->adjoin == MagickFalse) ||
2906 (strchr(filename,
'%') != (
char *) NULL))
2911 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
2912 image->scene,filename);
2913 if ((LocaleCompare(filename,image->filename) == 0) &&
2914 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2915 (GetNextImageInList(image) != (Image *) NULL)))
2918 path[MagickPathExtent];
2920 GetPathComponent(image->filename,RootPath,path);
2921 if (*extension ==
'\0')
2922 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
2923 path,(
double) image->scene);
2925 (
void) FormatLocaleString(filename,MagickPathExtent,
2926 "%s-%.20g.%s",path,(
double) image->scene,extension);
2928 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2929#if defined(macintosh)
2930 SetApplicationType(filename,image_info->magick,
'8BIM');
2934 if (image_info->file != (FILE *) NULL)
2936 blob_info->file_info.file=image_info->file;
2937 blob_info->type=FileStream;
2938 blob_info->exempt=MagickTrue;
2946 blob_info->file_info.file=(FILE *) NULL;
2947 file=open_utf8(filename,flags,0);
2949 blob_info->file_info.file=fdopen(file,type);
2950 if (blob_info->file_info.file != (FILE *) NULL)
2958 blob_info->type=FileStream;
2959 (void) fstat(fileno(blob_info->file_info.file),
2960 &blob_info->properties);
2961 (void) SetStreamBuffering(image_info,image);
2962 (void) memset(magick,0,
sizeof(magick));
2963 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2964 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2965#if defined(MAGICKCORE_POSIX_SUPPORT)
2966 (void) fflush(blob_info->file_info.file);
2968 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2969 " read %.20g magic header bytes",(
double) count);
2970#if defined(MAGICKCORE_ZLIB_DELEGATE)
2971 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2972 ((
int) magick[2] == 0x08))
2975 gzfile = gzopen_utf8(filename,
"rb");
2977 if (gzfile != (gzFile) NULL)
2979 if (blob_info->file_info.file != (FILE *) NULL)
2980 (void) fclose(blob_info->file_info.file);
2981 blob_info->file_info.file=(FILE *) NULL;
2982 blob_info->file_info.gzfile=gzfile;
2983 blob_info->type=ZipStream;
2987#if defined(MAGICKCORE_BZLIB_DELEGATE)
2988 if (strncmp((
char *) magick,
"BZh",3) == 0)
2991 *bzfile = BZ2_bzopen(filename,
"r");
2993 if (bzfile != (BZFILE *) NULL)
2995 if (blob_info->file_info.file != (FILE *) NULL)
2996 (void) fclose(blob_info->file_info.file);
2997 blob_info->file_info.file=(FILE *) NULL;
2998 blob_info->file_info.bzfile=bzfile;
2999 blob_info->type=BZipStream;
3003 if (blob_info->type == FileStream)
3014 sans_exception=AcquireExceptionInfo();
3015 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3016 sans_exception=DestroyExceptionInfo(sans_exception);
3017 length=(size_t) blob_info->properties.st_size;
3018 if ((magick_info != (
const MagickInfo *) NULL) &&
3019 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3020 (length > MagickMaxBufferExtent) &&
3021 (AcquireMagickResource(MapResource,length) != MagickFalse))
3026 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3028 if (blob == (
void *) NULL)
3029 RelinquishMagickResource(MapResource,length);
3035 if (image_info->file != (FILE *) NULL)
3036 blob_info->exempt=MagickFalse;
3039 (void) fclose(blob_info->file_info.file);
3040 blob_info->file_info.file=(FILE *) NULL;
3042 AttachBlob(blob_info,blob,length);
3043 blob_info->mapped=MagickTrue;
3050#if defined(MAGICKCORE_ZLIB_DELEGATE)
3051 if ((LocaleCompare(extension,
"gz") == 0) ||
3052 (LocaleCompare(extension,
"wmz") == 0) ||
3053 (LocaleCompare(extension,
"svgz") == 0))
3055 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3056 if (blob_info->file_info.gzfile != (gzFile) NULL)
3057 blob_info->type=ZipStream;
3061#if defined(MAGICKCORE_BZLIB_DELEGATE)
3062 if (LocaleCompare(extension,
"bz2") == 0)
3064 if (mode == WriteBinaryBlobMode)
3066 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3067 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3068 blob_info->type=BZipStream;
3076 blob_info->file_info.file=(FILE *) NULL;
3077 file=open_utf8(filename,flags,P_MODE);
3079 blob_info->file_info.file=fdopen(file,type);
3080 if (blob_info->file_info.file != (FILE *) NULL)
3082 blob_info->type=FileStream;
3083 (void) SetStreamBuffering(image_info,image);
3086 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3089 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3090 "NotAuthorized",
"`%s'",filename);
3091 return(MagickFalse);
3093 blob_info->status=0;
3094 blob_info->error_number=0;
3095 if (blob_info->type != UndefinedStream)
3096 blob_info->size=GetBlobSize(image);
3099 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3100 return(MagickFalse);
3139#if defined(__cplusplus) || defined(c_plusplus)
3143static size_t PingStream(
const Image *magick_unused(image),
3144 const void *magick_unused(pixels),
const size_t columns)
3146 magick_unreferenced(image);
3147 magick_unreferenced(pixels);
3152#if defined(__cplusplus) || defined(c_plusplus)
3156MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3157 const size_t length,ExceptionInfo *exception)
3172 assert(image_info != (ImageInfo *) NULL);
3173 assert(image_info->signature == MagickCoreSignature);
3174 assert(exception != (ExceptionInfo *) NULL);
3175 if (IsEventLogging() != MagickFalse)
3176 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3177 image_info->filename);
3178 if ((blob == (
const void *) NULL) || (length == 0))
3180 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3181 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3182 return((Image *) NULL);
3184 ping_info=CloneImageInfo(image_info);
3185 ping_info->blob=(
void *) blob;
3186 ping_info->length=length;
3187 ping_info->ping=MagickTrue;
3188 if (*ping_info->magick ==
'\0')
3189 (void) SetImageInfo(ping_info,0,exception);
3190 magick_info=GetMagickInfo(ping_info->magick,exception);
3191 if (magick_info == (
const MagickInfo *) NULL)
3193 (void) ThrowMagickException(exception,GetMagickModule(),
3194 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3196 ping_info=DestroyImageInfo(ping_info);
3197 return((Image *) NULL);
3199 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3202 filename[MagickPathExtent];
3207 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3208 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3209 ping_info->magick,filename);
3210 image=ReadStream(ping_info,&PingStream,exception);
3211 if (image != (Image *) NULL)
3212 (void) DetachBlob(image->blob);
3213 ping_info=DestroyImageInfo(ping_info);
3219 ping_info->blob=(
void *) NULL;
3220 ping_info->length=0;
3221 *ping_info->filename=
'\0';
3222 status=BlobToFile(ping_info->filename,blob,length,exception);
3223 if (status == MagickFalse)
3225 (void) RelinquishUniqueFileResource(ping_info->filename);
3226 ping_info=DestroyImageInfo(ping_info);
3227 return((Image *) NULL);
3229 clone_info=CloneImageInfo(ping_info);
3230 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3231 ping_info->magick,ping_info->filename);
3232 image=ReadStream(clone_info,&PingStream,exception);
3233 if (image != (Image *) NULL)
3241 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3243 (void) CopyMagickString(images->filename,image_info->filename,
3245 (void) CopyMagickString(images->magick_filename,image_info->filename,
3247 (void) CopyMagickString(images->magick,magick_info->name,
3249 images=GetNextImageInList(images);
3252 clone_info=DestroyImageInfo(clone_info);
3253 (void) RelinquishUniqueFileResource(ping_info->filename);
3254 ping_info=DestroyImageInfo(ping_info);
3289MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
3290 unsigned char *data)
3293 *magick_restrict blob_info;
3304 assert(image != (Image *) NULL);
3305 assert(image->signature == MagickCoreSignature);
3306 assert(image->blob != (BlobInfo *) NULL);
3307 assert(image->blob->type != UndefinedStream);
3310 assert(data != (
void *) NULL);
3311 blob_info=image->blob;
3314 switch (blob_info->type)
3316 case UndefinedStream:
3318 case StandardStream:
3326 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3331 c=getc(blob_info->file_info.file);
3334 *q++=(
unsigned char) c;
3340 c=getc(blob_info->file_info.file);
3343 *q++=(
unsigned char) c;
3349 c=getc(blob_info->file_info.file);
3352 *q++=(
unsigned char) c;
3358 c=getc(blob_info->file_info.file);
3361 *q++=(
unsigned char) c;
3368 if ((count != (ssize_t) length) &&
3369 (ferror(blob_info->file_info.file) != 0))
3370 ThrowBlobException(blob_info);
3375#if defined(MAGICKCORE_ZLIB_DELEGATE)
3386 for (i=0; i < (ssize_t) length; i+=count)
3388 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3389 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3402 c=gzgetc(blob_info->file_info.gzfile);
3405 *q++=(
unsigned char) c;
3411 c=gzgetc(blob_info->file_info.gzfile);
3414 *q++=(
unsigned char) c;
3420 c=gzgetc(blob_info->file_info.gzfile);
3423 *q++=(
unsigned char) c;
3429 c=gzgetc(blob_info->file_info.gzfile);
3432 *q++=(
unsigned char) c;
3440 (void) gzerror(blob_info->file_info.gzfile,&status);
3441 if ((count != (ssize_t) length) && (status != Z_OK))
3442 ThrowBlobException(blob_info);
3443 if (blob_info->eof == MagickFalse)
3444 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3451#if defined(MAGICKCORE_BZLIB_DELEGATE)
3458 for (i=0; i < (ssize_t) length; i+=count)
3460 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3461 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3471 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3472 if ((count != (ssize_t) length) && (status != BZ_OK))
3473 ThrowBlobException(blob_info);
3484 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3486 blob_info->eof=MagickTrue;
3489 p=blob_info->data+blob_info->offset;
3490 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3491 blob_info->length-blob_info->offset);
3492 blob_info->offset+=count;
3493 if (count != (ssize_t) length)
3494 blob_info->eof=MagickTrue;
3495 (void) memcpy(q,p,(
size_t) count);
3524MagickExport
int ReadBlobByte(Image *image)
3527 *magick_restrict blob_info;
3532 assert(image != (Image *) NULL);
3533 assert(image->signature == MagickCoreSignature);
3534 assert(image->blob != (BlobInfo *) NULL);
3535 assert(image->blob->type != UndefinedStream);
3536 blob_info=image->blob;
3537 switch (blob_info->type)
3539 case StandardStream:
3543 c=getc(blob_info->file_info.file);
3546 if (ferror(blob_info->file_info.file) != 0)
3547 ThrowBlobException(blob_info);
3554 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3556 blob_info->eof=MagickTrue;
3559 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
3560 blob_info->offset++;
3571 count=ReadBlob(image,1,buffer);
3604MagickExport
double ReadBlobDouble(Image *image)
3615 quantum.double_value=0.0;
3616 quantum.unsigned_value=ReadBlobLongLong(image);
3617 return(quantum.double_value);
3643MagickExport
float ReadBlobFloat(Image *image)
3654 quantum.float_value=0.0;
3655 quantum.unsigned_value=ReadBlobLong(image);
3656 return(quantum.float_value);
3682MagickExport
unsigned int ReadBlobLong(Image *image)
3696 assert(image != (Image *) NULL);
3697 assert(image->signature == MagickCoreSignature);
3699 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3702 if (image->endian == LSBEndian)
3704 value=(
unsigned int) (*p++);
3705 value|=(
unsigned int) (*p++) << 8;
3706 value|=(
unsigned int) (*p++) << 16;
3707 value|=(
unsigned int) (*p++) << 24;
3710 value=(
unsigned int) (*p++) << 24;
3711 value|=(
unsigned int) (*p++) << 16;
3712 value|=(
unsigned int) (*p++) << 8;
3713 value|=(
unsigned int) (*p++);
3740MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3754 assert(image != (Image *) NULL);
3755 assert(image->signature == MagickCoreSignature);
3757 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3759 return(MagickULLConstant(0));
3760 if (image->endian == LSBEndian)
3762 value=(MagickSizeType) (*p++);
3763 value|=(MagickSizeType) (*p++) << 8;
3764 value|=(MagickSizeType) (*p++) << 16;
3765 value|=(MagickSizeType) (*p++) << 24;
3766 value|=(MagickSizeType) (*p++) << 32;
3767 value|=(MagickSizeType) (*p++) << 40;
3768 value|=(MagickSizeType) (*p++) << 48;
3769 value|=(MagickSizeType) (*p++) << 56;
3772 value=(MagickSizeType) (*p++) << 56;
3773 value|=(MagickSizeType) (*p++) << 48;
3774 value|=(MagickSizeType) (*p++) << 40;
3775 value|=(MagickSizeType) (*p++) << 32;
3776 value|=(MagickSizeType) (*p++) << 24;
3777 value|=(MagickSizeType) (*p++) << 16;
3778 value|=(MagickSizeType) (*p++) << 8;
3779 value|=(MagickSizeType) (*p++);
3806MagickExport
unsigned short ReadBlobShort(Image *image)
3820 assert(image != (Image *) NULL);
3821 assert(image->signature == MagickCoreSignature);
3823 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3825 return((
unsigned short) 0U);
3826 if (image->endian == LSBEndian)
3828 value=(
unsigned short) (*p++);
3829 value|=(
unsigned short) (*p++) << 8;
3832 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3833 value|=(
unsigned short) (*p++);
3860MagickExport
unsigned int ReadBlobLSBLong(Image *image)
3874 assert(image != (Image *) NULL);
3875 assert(image->signature == MagickCoreSignature);
3877 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3880 value=(
unsigned int) (*p++);
3881 value|=(
unsigned int) (*p++) << 8;
3882 value|=(
unsigned int) (*p++) << 16;
3883 value|=(
unsigned int) (*p++) << 24;
3910MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
3921 quantum.unsigned_value=ReadBlobLSBLong(image);
3922 return(quantum.signed_value);
3948MagickExport
unsigned short ReadBlobLSBShort(Image *image)
3962 assert(image != (Image *) NULL);
3963 assert(image->signature == MagickCoreSignature);
3965 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3967 return((
unsigned short) 0U);
3968 value=(
unsigned short) (*p++);
3969 value|=(
unsigned short) (*p++) << 8;
3996MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4007 quantum.unsigned_value=ReadBlobLSBShort(image);
4008 return(quantum.signed_value);
4034MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4048 assert(image != (Image *) NULL);
4049 assert(image->signature == MagickCoreSignature);
4051 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4054 value=(
unsigned int) (*p++) << 24;
4055 value|=(
unsigned int) (*p++) << 16;
4056 value|=(
unsigned int) (*p++) << 8;
4057 value|=(
unsigned int) (*p++);
4084MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4098 assert(image != (Image *) NULL);
4099 assert(image->signature == MagickCoreSignature);
4101 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4103 return(MagickULLConstant(0));
4104 value=(MagickSizeType) (*p++) << 56;
4105 value|=(MagickSizeType) (*p++) << 48;
4106 value|=(MagickSizeType) (*p++) << 40;
4107 value|=(MagickSizeType) (*p++) << 32;
4108 value|=(MagickSizeType) (*p++) << 24;
4109 value|=(MagickSizeType) (*p++) << 16;
4110 value|=(MagickSizeType) (*p++) << 8;
4111 value|=(MagickSizeType) (*p++);
4138MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4152 assert(image != (Image *) NULL);
4153 assert(image->signature == MagickCoreSignature);
4155 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4157 return((
unsigned short) 0U);
4158 value=(
unsigned short) ((*p++) << 8);
4159 value|=(
unsigned short) (*p++);
4186MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4197 quantum.unsigned_value=ReadBlobMSBLong(image);
4198 return(quantum.signed_value);
4224MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4235 quantum.unsigned_value=ReadBlobMSBShort(image);
4236 return(quantum.signed_value);
4262MagickExport
signed int ReadBlobSignedLong(Image *image)
4273 quantum.unsigned_value=ReadBlobLong(image);
4274 return(quantum.signed_value);
4300MagickExport
signed short ReadBlobSignedShort(Image *image)
4311 quantum.unsigned_value=ReadBlobShort(image);
4312 return(quantum.signed_value);
4350MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4351 const size_t length,
void *magick_restrict data,ssize_t *count)
4354 *magick_restrict blob_info;
4356 assert(image != (Image *) NULL);
4357 assert(image->signature == MagickCoreSignature);
4358 assert(image->blob != (BlobInfo *) NULL);
4359 assert(image->blob->type != UndefinedStream);
4360 assert(count != (ssize_t *) NULL);
4361 blob_info=image->blob;
4362 if (blob_info->type != BlobStream)
4364 assert(data != NULL);
4365 *count=ReadBlob(image,length,(
unsigned char *) data);
4368 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4371 blob_info->eof=MagickTrue;
4374 data=blob_info->data+blob_info->offset;
4375 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4376 blob_info->length-blob_info->offset);
4377 blob_info->offset+=(*count);
4378 if (*count != (ssize_t) length)
4379 blob_info->eof=MagickTrue;
4408MagickExport
char *ReadBlobString(Image *image,
char *
string)
4411 *magick_restrict blob_info;
4419 assert(image != (Image *) NULL);
4420 assert(image->signature == MagickCoreSignature);
4421 assert(image->blob != (BlobInfo *) NULL);
4422 assert(image->blob->type != UndefinedStream);
4423 if (IsEventLogging() != MagickFalse)
4424 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4426 blob_info=image->blob;
4427 switch (blob_info->type)
4429 case UndefinedStream:
4431 case StandardStream:
4434 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4435 if (p == (
char *) NULL)
4437 if (ferror(blob_info->file_info.file) != 0)
4438 ThrowBlobException(blob_info);
4439 return((
char *) NULL);
4446#if defined(MAGICKCORE_ZLIB_DELEGATE)
4447 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4448 if (p == (
char *) NULL)
4451 (void) gzerror(blob_info->file_info.gzfile,&status);
4453 ThrowBlobException(blob_info);
4454 return((
char *) NULL);
4464 c=ReadBlobByte(image);
4467 blob_info->eof=MagickTrue;
4473 }
while (i < (MaxTextExtent-2));
4481 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4484 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4486 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4487 return((
char *) NULL);
4514MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4516 assert(blob != (BlobInfo *) NULL);
4517 assert(blob->signature == MagickCoreSignature);
4518 if (IsEventLogging() != MagickFalse)
4519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4520 LockSemaphoreInfo(blob->semaphore);
4521 blob->reference_count++;
4522 UnlockSemaphoreInfo(blob->semaphore);
4559MagickExport MagickOffsetType SeekBlob(Image *image,
4560 const MagickOffsetType offset,
const int whence)
4563 *magick_restrict blob_info;
4565 assert(image != (Image *) NULL);
4566 assert(image->signature == MagickCoreSignature);
4567 assert(image->blob != (BlobInfo *) NULL);
4568 assert(image->blob->type != UndefinedStream);
4569 if (IsEventLogging() != MagickFalse)
4570 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4571 blob_info=image->blob;
4572 switch (blob_info->type)
4574 case UndefinedStream:
4576 case StandardStream:
4581 if ((offset < 0) && (whence == SEEK_SET))
4583 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4585 blob_info->offset=TellBlob(image);
4590#if defined(MAGICKCORE_ZLIB_DELEGATE)
4591 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4594 blob_info->offset=TellBlob(image);
4610 blob_info->offset=offset;
4615 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4616 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4621 if ((blob_info->offset+offset) < 0)
4623 blob_info->offset+=offset;
4628 if (((MagickOffsetType) blob_info->length+offset) < 0)
4630 blob_info->offset=blob_info->length+offset;
4634 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4636 blob_info->eof=MagickFalse;
4642 return(blob_info->offset);
4670MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
4672 assert(image != (
const Image *) NULL);
4673 assert(image->signature == MagickCoreSignature);
4674 if (IsEventLogging() != MagickFalse)
4675 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4676 image->blob->exempt=exempt;
4705MagickExport MagickBooleanType SetBlobExtent(Image *image,
4706 const MagickSizeType extent)
4709 *magick_restrict blob_info;
4711 assert(image != (Image *) NULL);
4712 assert(image->signature == MagickCoreSignature);
4713 assert(image->blob != (BlobInfo *) NULL);
4714 assert(image->blob->type != UndefinedStream);
4715 if (IsEventLogging() != MagickFalse)
4716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4717 blob_info=image->blob;
4718 switch (blob_info->type)
4720 case UndefinedStream:
4722 case StandardStream:
4723 return(MagickFalse);
4732 if (extent != (MagickSizeType) ((off_t) extent))
4733 return(MagickFalse);
4734 offset=SeekBlob(image,0,SEEK_END);
4736 return(MagickFalse);
4737 if ((MagickSizeType) offset >= extent)
4739 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4742 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4743 blob_info->file_info.file);
4744#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4745 if (blob_info->synchronize != MagickFalse)
4750 file=fileno(blob_info->file_info.file);
4751 if ((file == -1) || (offset < 0))
4752 return(MagickFalse);
4753 (void) posix_fallocate(file,offset,extent-offset);
4756 offset=SeekBlob(image,offset,SEEK_SET);
4758 return(MagickFalse);
4763 return(MagickFalse);
4765 return(MagickFalse);
4767 return(MagickFalse);
4770 if (extent != (MagickSizeType) ((
size_t) extent))
4771 return(MagickFalse);
4772 if (blob_info->mapped != MagickFalse)
4780 (void) UnmapBlob(blob_info->data,blob_info->length);
4781 RelinquishMagickResource(MapResource,blob_info->length);
4782 if (extent != (MagickSizeType) ((off_t) extent))
4783 return(MagickFalse);
4784 offset=SeekBlob(image,0,SEEK_END);
4786 return(MagickFalse);
4787 if ((MagickSizeType) offset >= extent)
4789 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4790 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4791 blob_info->file_info.file);
4792#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4793 if (blob_info->synchronize != MagickFalse)
4798 file=fileno(blob_info->file_info.file);
4799 if ((file == -1) || (offset < 0))
4800 return(MagickFalse);
4801 (void) posix_fallocate(file,offset,extent-offset);
4804 offset=SeekBlob(image,offset,SEEK_SET);
4806 return(MagickFalse);
4807 (void) AcquireMagickResource(MapResource,extent);
4808 blob_info->data=(
unsigned char*) MapBlob(fileno(
4809 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
4810 blob_info->extent=(size_t) extent;
4811 blob_info->length=(size_t) extent;
4812 (void) SyncBlob(image);
4815 blob_info->extent=(size_t) extent;
4816 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4817 blob_info->extent+1,
sizeof(*blob_info->data));
4818 (void) SyncBlob(image);
4819 if (blob_info->data == (
unsigned char *) NULL)
4821 (void) DetachBlob(blob_info);
4822 return(MagickFalse);
4854static int SyncBlob(
const Image *image)
4857 *magick_restrict blob_info;
4862 assert(image != (Image *) NULL);
4863 assert(image->signature == MagickCoreSignature);
4864 assert(image->blob != (BlobInfo *) NULL);
4865 if (IsEventLogging() != MagickFalse)
4866 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4867 if (EOFBlob(image) != 0)
4869 blob_info=image->blob;
4871 switch (blob_info->type)
4873 case UndefinedStream:
4874 case StandardStream:
4879 status=fflush(blob_info->file_info.file);
4884#if defined(MAGICKCORE_ZLIB_DELEGATE)
4885 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4891#if defined(MAGICKCORE_BZLIB_DELEGATE)
4892 status=BZ2_bzflush(blob_info->file_info.bzfile);
4926MagickExport MagickOffsetType TellBlob(
const Image *image)
4929 *magick_restrict blob_info;
4934 assert(image != (Image *) NULL);
4935 assert(image->signature == MagickCoreSignature);
4936 assert(image->blob != (BlobInfo *) NULL);
4937 assert(image->blob->type != UndefinedStream);
4938 if (IsEventLogging() != MagickFalse)
4939 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4940 blob_info=image->blob;
4942 switch (blob_info->type)
4944 case UndefinedStream:
4945 case StandardStream:
4949 offset=ftell(blob_info->file_info.file);
4956#if defined(MAGICKCORE_ZLIB_DELEGATE)
4957 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4967 offset=blob_info->offset;
4999MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5001#if defined(MAGICKCORE_HAVE_MMAP)
5005 status=munmap(map,length);
5006 return(status == -1 ? MagickFalse : MagickTrue);
5010 return(MagickFalse);
5043MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5044 const unsigned char *data)
5047 *magick_restrict blob_info;
5061 assert(image != (Image *) NULL);
5062 assert(image->signature == MagickCoreSignature);
5063 assert(image->blob != (BlobInfo *) NULL);
5064 assert(image->blob->type != UndefinedStream);
5067 assert(data != (
const unsigned char *) NULL);
5068 blob_info=image->blob;
5070 p=(
const unsigned char *) data;
5071 q=(
unsigned char *) data;
5072 switch (blob_info->type)
5074 case UndefinedStream:
5076 case StandardStream:
5084 count=(ssize_t) fwrite((
const char *) data,1,length,
5085 blob_info->file_info.file);
5090 c=putc((
int) *p++,blob_info->file_info.file);
5098 c=putc((
int) *p++,blob_info->file_info.file);
5106 c=putc((
int) *p++,blob_info->file_info.file);
5114 c=putc((
int) *p++,blob_info->file_info.file);
5123 if ((count != (ssize_t) length) &&
5124 (ferror(blob_info->file_info.file) != 0))
5125 ThrowBlobException(blob_info);
5130#if defined(MAGICKCORE_ZLIB_DELEGATE)
5141 for (i=0; i < (ssize_t) length; i+=count)
5143 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5144 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5157 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5165 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5173 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5181 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5191 (void) gzerror(blob_info->file_info.gzfile,&status);
5192 if ((count != (ssize_t) length) && (status != Z_OK))
5193 ThrowBlobException(blob_info);
5199#if defined(MAGICKCORE_BZLIB_DELEGATE)
5206 for (i=0; i < (ssize_t) length; i+=count)
5208 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5209 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5219 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5220 if ((count != (ssize_t) length) && (status != BZ_OK))
5221 ThrowBlobException(blob_info);
5227 count=(ssize_t) blob_info->stream(image,data,length);
5235 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5240 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5241 if (extent >= blob_info->extent)
5243 extent+=blob_info->quantum+length;
5244 blob_info->quantum<<=1;
5245 if (SetBlobExtent(image,extent) == MagickFalse)
5248 q=blob_info->data+blob_info->offset;
5249 (void) memcpy(q,p,length);
5250 blob_info->offset+=length;
5251 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5252 blob_info->length=(size_t) blob_info->offset;
5253 count=(ssize_t) length;
5256 if (count != (ssize_t) length)
5257 ThrowBlobException(blob_info);
5286MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
5289 *magick_restrict blob_info;
5294 assert(image != (Image *) NULL);
5295 assert(image->signature == MagickCoreSignature);
5296 assert(image->blob != (BlobInfo *) NULL);
5297 assert(image->blob->type != UndefinedStream);
5298 blob_info=image->blob;
5300 switch (blob_info->type)
5302 case StandardStream:
5309 c=putc((
int) value,blob_info->file_info.file);
5312 if (ferror(blob_info->file_info.file) != 0)
5313 ThrowBlobException(blob_info);
5321 count=WriteBlobStream(image,1,&value);
5353MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
5364 quantum.unsigned_value=0U;
5365 quantum.float_value=value;
5366 return(WriteBlobLong(image,quantum.unsigned_value));
5394MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
5399 assert(image != (Image *) NULL);
5400 assert(image->signature == MagickCoreSignature);
5401 if (image->endian == LSBEndian)
5403 buffer[0]=(
unsigned char) value;
5404 buffer[1]=(
unsigned char) (value >> 8);
5405 buffer[2]=(
unsigned char) (value >> 16);
5406 buffer[3]=(
unsigned char) (value >> 24);
5407 return(WriteBlobStream(image,4,buffer));
5409 buffer[0]=(
unsigned char) (value >> 24);
5410 buffer[1]=(
unsigned char) (value >> 16);
5411 buffer[2]=(
unsigned char) (value >> 8);
5412 buffer[3]=(
unsigned char) value;
5413 return(WriteBlobStream(image,4,buffer));
5441MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
5446 assert(image != (Image *) NULL);
5447 assert(image->signature == MagickCoreSignature);
5448 if (image->endian == LSBEndian)
5450 buffer[0]=(
unsigned char) value;
5451 buffer[1]=(
unsigned char) (value >> 8);
5452 return(WriteBlobStream(image,2,buffer));
5454 buffer[0]=(
unsigned char) (value >> 8);
5455 buffer[1]=(
unsigned char) value;
5456 return(WriteBlobStream(image,2,buffer));
5484MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
5489 assert(image != (Image *) NULL);
5490 assert(image->signature == MagickCoreSignature);
5491 buffer[0]=(
unsigned char) value;
5492 buffer[1]=(
unsigned char) (value >> 8);
5493 buffer[2]=(
unsigned char) (value >> 16);
5494 buffer[3]=(
unsigned char) (value >> 24);
5495 return(WriteBlobStream(image,4,buffer));
5523MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
5528 assert(image != (Image *) NULL);
5529 assert(image->signature == MagickCoreSignature);
5530 buffer[0]=(
unsigned char) value;
5531 buffer[1]=(
unsigned char) (value >> 8);
5532 return(WriteBlobStream(image,2,buffer));
5560MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
5574 assert(image != (Image *) NULL);
5575 assert(image->signature == MagickCoreSignature);
5576 quantum.signed_value=value;
5577 buffer[0]=(
unsigned char) quantum.unsigned_value;
5578 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5579 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5580 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5581 return(WriteBlobStream(image,4,buffer));
5609MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5610 const signed short value)
5624 assert(image != (Image *) NULL);
5625 assert(image->signature == MagickCoreSignature);
5626 quantum.signed_value=value;
5627 buffer[0]=(
unsigned char) quantum.unsigned_value;
5628 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5629 return(WriteBlobStream(image,2,buffer));
5657MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
5662 assert(image != (Image *) NULL);
5663 assert(image->signature == MagickCoreSignature);
5664 buffer[0]=(
unsigned char) (value >> 24);
5665 buffer[1]=(
unsigned char) (value >> 16);
5666 buffer[2]=(
unsigned char) (value >> 8);
5667 buffer[3]=(
unsigned char) value;
5668 return(WriteBlobStream(image,4,buffer));
5696MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5697 const MagickSizeType value)
5702 assert(image != (Image *) NULL);
5703 assert(image->signature == MagickCoreSignature);
5704 buffer[0]=(
unsigned char) (value >> 56);
5705 buffer[1]=(
unsigned char) (value >> 48);
5706 buffer[2]=(
unsigned char) (value >> 40);
5707 buffer[3]=(
unsigned char) (value >> 32);
5708 buffer[4]=(
unsigned char) (value >> 24);
5709 buffer[5]=(
unsigned char) (value >> 16);
5710 buffer[6]=(
unsigned char) (value >> 8);
5711 buffer[7]=(
unsigned char) value;
5712 return(WriteBlobStream(image,8,buffer));
5740MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
5745 assert(image != (Image *) NULL);
5746 assert(image->signature == MagickCoreSignature);
5747 buffer[0]=(
unsigned char) (value >> 8);
5748 buffer[1]=(
unsigned char) value;
5749 return(WriteBlobStream(image,2,buffer));
5777MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,
const signed int value)
5791 assert(image != (Image *) NULL);
5792 assert(image->signature == MagickCoreSignature);
5793 quantum.signed_value=value;
5794 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5795 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5796 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5797 buffer[3]=(
unsigned char) quantum.unsigned_value;
5798 return(WriteBlobStream(image,4,buffer));
5826MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5827 const signed short value)
5841 assert(image != (Image *) NULL);
5842 assert(image->signature == MagickCoreSignature);
5843 quantum.signed_value=value;
5844 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5845 buffer[1]=(
unsigned char) quantum.unsigned_value;
5846 return(WriteBlobStream(image,2,buffer));
5874MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
5876 assert(image != (Image *) NULL);
5877 assert(image->signature == MagickCoreSignature);
5878 assert(
string != (
const char *) NULL);
5879 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));