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;
273MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
274 const size_t length,ExceptionInfo *exception)
285 assert(filename != (
const char *) NULL);
286 assert(blob != (
const void *) NULL);
287 if (IsEventLogging() != MagickFalse)
288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
289 if (*filename ==
'\0')
290 file=AcquireUniqueFileResource(filename);
292 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
295 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
298 for (i=0; i < length; i+=(size_t) count)
300 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
301 MagickMaxBufferExtent));
309 file=close_utf8(file);
310 if ((file == -1) || (i < length))
312 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
349MagickExport Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
350 const size_t length,ExceptionInfo *exception)
365 assert(image_info != (ImageInfo *) NULL);
366 assert(image_info->signature == MagickCoreSignature);
367 assert(exception != (ExceptionInfo *) NULL);
368 if (IsEventLogging() != MagickFalse)
369 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
370 image_info->filename);
371 if ((blob == (
const void *) NULL) || (length == 0))
373 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
374 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
375 return((Image *) NULL);
377 blob_info=CloneImageInfo(image_info);
378 blob_info->blob=(
void *) blob;
379 blob_info->length=length;
380 if (*blob_info->magick ==
'\0')
381 (void) SetImageInfo(blob_info,0,exception);
382 magick_info=GetMagickInfo(blob_info->magick,exception);
383 if (magick_info == (
const MagickInfo *) NULL)
385 (void) ThrowMagickException(exception,GetMagickModule(),
386 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
388 blob_info=DestroyImageInfo(blob_info);
389 return((Image *) NULL);
391 if (GetMagickBlobSupport(magick_info) != MagickFalse)
394 filename[MagickPathExtent];
399 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
400 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
401 blob_info->magick,filename);
402 image=ReadImage(blob_info,exception);
403 if (image != (Image *) NULL)
404 (void) DetachBlob(image->blob);
405 blob_info=DestroyImageInfo(blob_info);
411 blob_info->blob=(
void *) NULL;
413 *blob_info->filename=
'\0';
414 status=BlobToFile(blob_info->filename,blob,length,exception);
415 if (status == MagickFalse)
417 (void) RelinquishUniqueFileResource(blob_info->filename);
418 blob_info=DestroyImageInfo(blob_info);
419 return((Image *) NULL);
421 clone_info=CloneImageInfo(blob_info);
422 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
423 blob_info->magick,blob_info->filename);
424 image=ReadImage(clone_info,exception);
425 if (image != (Image *) NULL)
433 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
435 (void) CopyMagickString(images->filename,image_info->filename,
437 (void) CopyMagickString(images->magick_filename,image_info->filename,
439 (void) CopyMagickString(images->magick,magick_info->name,
441 images=GetNextImageInList(images);
444 clone_info=DestroyImageInfo(clone_info);
445 (void) RelinquishUniqueFileResource(blob_info->filename);
446 blob_info=DestroyImageInfo(blob_info);
473MagickExport BlobInfo *CloneBlobInfo(
const BlobInfo *blob_info)
481 clone_info=(BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
482 GetBlobInfo(clone_info);
483 if (blob_info == (BlobInfo *) NULL)
485 semaphore=clone_info->semaphore;
486 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
487 if (blob_info->mapped != MagickFalse)
488 (void) AcquireMagickResource(MapResource,blob_info->length);
489 clone_info->semaphore=semaphore;
490 LockSemaphoreInfo(clone_info->semaphore);
491 clone_info->reference_count=1;
492 UnlockSemaphoreInfo(clone_info->semaphore);
519static inline void ThrowBlobException(BlobInfo *blob_info)
521 if ((blob_info->status == 0) && (errno != 0))
522 blob_info->error_number=errno;
523 blob_info->status=(-1);
526MagickExport MagickBooleanType CloseBlob(Image *image)
529 *magick_restrict blob_info;
537 assert(image != (Image *) NULL);
538 assert(image->signature == MagickCoreSignature);
539 if (IsEventLogging() != MagickFalse)
540 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
541 blob_info=image->blob;
542 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
544 (void) SyncBlob(image);
545 status=blob_info->status;
546 switch (blob_info->type)
548 case UndefinedStream:
554 if (blob_info->synchronize != MagickFalse)
556 status=fflush(blob_info->file_info.file);
558 ThrowBlobException(blob_info);
559 status=fsync(fileno(blob_info->file_info.file));
561 ThrowBlobException(blob_info);
563 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
564 ThrowBlobException(blob_info);
569#if defined(MAGICKCORE_ZLIB_DELEGATE)
571 (void) gzerror(blob_info->file_info.gzfile,&status);
573 ThrowBlobException(blob_info);
579#if defined(MAGICKCORE_BZLIB_DELEGATE)
581 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
583 ThrowBlobException(blob_info);
591 if (blob_info->file_info.file != (FILE *) NULL)
593 if (blob_info->synchronize != MagickFalse)
595 status=fflush(blob_info->file_info.file);
597 ThrowBlobException(blob_info);
598 status=fsync(fileno(blob_info->file_info.file));
600 ThrowBlobException(blob_info);
602 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
603 ThrowBlobException(blob_info);
608 blob_info->size=GetBlobSize(image);
609 image->extent=blob_info->size;
610 blob_info->eof=MagickFalse;
612 blob_info->mode=UndefinedBlobMode;
613 if (blob_info->exempt != MagickFalse)
615 blob_info->type=UndefinedStream;
616 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
618 switch (blob_info->type)
620 case UndefinedStream:
625 if (blob_info->file_info.file != (FILE *) NULL)
627 status=fclose(blob_info->file_info.file);
629 ThrowBlobException(blob_info);
635#if defined(MAGICKCORE_HAVE_PCLOSE)
636 status=pclose(blob_info->file_info.file);
638 ThrowBlobException(blob_info);
644#if defined(MAGICKCORE_ZLIB_DELEGATE)
645 status=gzclose(blob_info->file_info.gzfile);
647 ThrowBlobException(blob_info);
653#if defined(MAGICKCORE_BZLIB_DELEGATE)
654 BZ2_bzclose(blob_info->file_info.bzfile);
662 if (blob_info->file_info.file != (FILE *) NULL)
664 status=fclose(blob_info->file_info.file);
666 ThrowBlobException(blob_info);
671 (void) DetachBlob(blob_info);
672 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
697MagickExport
void DestroyBlob(Image *image)
700 *magick_restrict blob_info;
705 assert(image != (Image *) NULL);
706 assert(image->signature == MagickCoreSignature);
707 assert(image->blob != (BlobInfo *) NULL);
708 assert(image->blob->signature == MagickCoreSignature);
709 if (IsEventLogging() != MagickFalse)
710 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
711 blob_info=image->blob;
713 LockSemaphoreInfo(blob_info->semaphore);
714 blob_info->reference_count--;
715 assert(blob_info->reference_count >= 0);
716 if (blob_info->reference_count == 0)
718 UnlockSemaphoreInfo(blob_info->semaphore);
719 if (destroy == MagickFalse)
721 image->blob=(BlobInfo *) NULL;
724 (void) CloseBlob(image);
725 if (blob_info->mapped != MagickFalse)
727 (void) UnmapBlob(blob_info->data,blob_info->length);
728 RelinquishMagickResource(MapResource,blob_info->length);
731 DestroySemaphoreInfo(&blob_info->semaphore);
732 blob_info->signature=(~MagickCoreSignature);
733 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
758MagickExport
unsigned char *DetachBlob(BlobInfo *blob_info)
763 assert(blob_info != (BlobInfo *) NULL);
764 if (IsEventLogging() != MagickFalse)
765 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
766 if (blob_info->mapped != MagickFalse)
768 (void) UnmapBlob(blob_info->data,blob_info->length);
769 blob_info->data=NULL;
770 RelinquishMagickResource(MapResource,blob_info->length);
772 blob_info->mapped=MagickFalse;
775 blob_info->mode=UndefinedBlobMode;
776 blob_info->eof=MagickFalse;
778 blob_info->exempt=MagickFalse;
779 blob_info->type=UndefinedStream;
780 blob_info->file_info.file=(FILE *) NULL;
781 data=blob_info->data;
782 blob_info->data=(
unsigned char *) NULL;
783 blob_info->stream=(StreamHandler) NULL;
811MagickPrivate
void DisassociateBlob(Image *image)
814 *magick_restrict blob_info,
820 assert(image != (Image *) NULL);
821 assert(image->signature == MagickCoreSignature);
822 assert(image->blob != (BlobInfo *) NULL);
823 assert(image->blob->signature == MagickCoreSignature);
824 if (IsEventLogging() != MagickFalse)
825 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
826 blob_info=image->blob;
828 LockSemaphoreInfo(blob_info->semaphore);
829 assert(blob_info->reference_count >= 0);
830 if (blob_info->reference_count > 1)
832 UnlockSemaphoreInfo(blob_info->semaphore);
833 if (clone == MagickFalse)
835 clone_info=CloneBlobInfo(blob_info);
837 image->blob=clone_info;
865MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
866 const MagickSizeType length)
878 buffer[MagickMinBufferExtent >> 1];
880 assert(image != (Image *) NULL);
881 assert(image->signature == MagickCoreSignature);
882 if (length != (MagickSizeType) ((MagickOffsetType) length))
885 for (i=0; i < length; i+=(MagickSizeType) count)
887 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
888 (void) ReadBlobStream(image,quantum,buffer,&count);
896 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
923MagickExport
void DuplicateBlob(Image *image,
const Image *duplicate)
925 assert(image != (Image *) NULL);
926 assert(image->signature == MagickCoreSignature);
927 assert(duplicate != (Image *) NULL);
928 assert(duplicate->signature == MagickCoreSignature);
929 if (IsEventLogging() != MagickFalse)
930 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
932 image->blob=ReferenceBlob(duplicate->blob);
958MagickExport
int EOFBlob(
const Image *image)
961 *magick_restrict blob_info;
963 assert(image != (Image *) NULL);
964 assert(image->signature == MagickCoreSignature);
965 assert(image->blob != (BlobInfo *) NULL);
966 assert(image->blob->type != UndefinedStream);
967 if (IsEventLogging() != MagickFalse)
968 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
969 blob_info=image->blob;
970 switch (blob_info->type)
972 case UndefinedStream:
978 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
984#if defined(MAGICKCORE_ZLIB_DELEGATE)
985 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
992#if defined(MAGICKCORE_BZLIB_DELEGATE)
997 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
998 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1004 blob_info->eof=MagickFalse;
1010 return((
int) blob_info->eof);
1036MagickExport
int ErrorBlob(
const Image *image)
1039 *magick_restrict blob_info;
1041 assert(image != (Image *) NULL);
1042 assert(image->signature == MagickCoreSignature);
1043 assert(image->blob != (BlobInfo *) NULL);
1044 assert(image->blob->type != UndefinedStream);
1045 if (IsEventLogging() != MagickFalse)
1046 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1047 blob_info=image->blob;
1048 switch (blob_info->type)
1050 case UndefinedStream:
1051 case StandardStream:
1056 blob_info->error=ferror(blob_info->file_info.file);
1061#if defined(MAGICKCORE_ZLIB_DELEGATE)
1062 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1068#if defined(MAGICKCORE_BZLIB_DELEGATE)
1069 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1081 return(blob_info->error);
1119MagickExport
unsigned char *FileToBlob(
const char *filename,
const size_t extent,
1120 size_t *length,ExceptionInfo *exception)
1146 assert(filename != (
const char *) NULL);
1147 assert(exception != (ExceptionInfo *) NULL);
1148 assert(exception->signature == MagickCoreSignature);
1149 if (IsEventLogging() != MagickFalse)
1150 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1152 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1153 if (status == MagickFalse)
1156 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1157 "NotAuthorized",
"`%s'",filename);
1161 if (LocaleCompare(filename,
"-") != 0)
1164 flags = O_RDONLY | O_BINARY;
1166 status=GetPathAttributes(filename,&attributes);
1167 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1169 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1172#if defined(O_NOFOLLOW)
1173 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1174 if (status == MagickFalse)
1177 file=open_utf8(filename,flags,0);
1181 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1182 return((
unsigned char *) NULL);
1184 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1185 if (status == MagickFalse)
1187 file=close_utf8(file)-1;
1189 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1190 "NotAuthorized",
"`%s'",filename);
1193 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1195 if ((file == fileno(stdin)) || (offset < 0) ||
1196 (offset != (MagickOffsetType) ((ssize_t) offset)))
1207 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1208 quantum=(size_t) MagickMaxBufferExtent;
1209 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1210 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1211 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1212 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1214 count=read(file,blob+i,quantum);
1221 if (~((
size_t) i) < (count+quantum+1))
1223 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1226 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+count+quantum+1,
1228 if ((
size_t) (i+count) >= extent)
1231 if (LocaleCompare(filename,
"-") != 0)
1232 file=close_utf8(file);
1233 if (blob == (
unsigned char *) NULL)
1235 (void) ThrowMagickException(exception,GetMagickModule(),
1236 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1237 return((
unsigned char *) NULL);
1241 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1242 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1243 return((
unsigned char *) NULL);
1245 *length=(size_t) MagickMin(i+count,extent);
1249 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1250 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1251 blob=(
unsigned char *) NULL;
1252 if (~(*length) >= (MagickPathExtent-1))
1253 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1255 if (blob == (
unsigned char *) NULL)
1257 file=close_utf8(file);
1258 (void) ThrowMagickException(exception,GetMagickModule(),
1259 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1260 return((
unsigned char *) NULL);
1262 map=MapBlob(file,ReadMode,0,*length);
1263 if (map != (
unsigned char *) NULL)
1265 (void) memcpy(blob,map,*length);
1266 (void) UnmapBlob(map,*length);
1270 (void) lseek(file,0,SEEK_SET);
1271 for (i=0; i < *length; i+=count)
1273 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1274 MagickMaxBufferExtent));
1284 file=close_utf8(file)-1;
1285 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1286 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1287 return((
unsigned char *) NULL);
1291 if (LocaleCompare(filename,
"-") != 0)
1292 file=close_utf8(file);
1295 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1296 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1326static inline ssize_t WriteBlobStream(Image *image,
const size_t length,
1327 const unsigned char *magick_restrict data)
1330 *magick_restrict blob_info;
1338 assert(image->blob != (BlobInfo *) NULL);
1339 assert(image->blob->type != UndefinedStream);
1340 assert(data != (
void *) NULL);
1341 blob_info=image->blob;
1342 if (blob_info->type != BlobStream)
1343 return(WriteBlob(image,length,data));
1344 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1349 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1350 if (extent >= blob_info->extent)
1352 extent+=blob_info->quantum+length;
1353 blob_info->quantum<<=1;
1354 if (SetBlobExtent(image,extent) == MagickFalse)
1357 q=blob_info->data+blob_info->offset;
1358 (void) memcpy(q,data,length);
1359 blob_info->offset+=length;
1360 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1361 blob_info->length=(size_t) blob_info->offset;
1362 return((ssize_t) length);
1365MagickExport MagickBooleanType FileToImage(Image *image,
const char *filename)
1386 assert(image != (
const Image *) NULL);
1387 assert(image->signature == MagickCoreSignature);
1388 assert(filename != (
const char *) NULL);
1389 if (IsEventLogging() != MagickFalse)
1390 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1391 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1392 if (status == MagickFalse)
1395 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1396 PolicyError,
"NotAuthorized",
"`%s'",filename);
1397 return(MagickFalse);
1400 if (LocaleCompare(filename,
"-") != 0)
1403 flags = O_RDONLY | O_BINARY;
1405#if defined(O_NOFOLLOW)
1406 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
1407 if (status == MagickFalse)
1410 file=open_utf8(filename,flags,0);
1414 ThrowFileException(&image->exception,BlobError,
"UnableToOpenBlob",
1416 return(MagickFalse);
1418 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1419 if (status == MagickFalse)
1422 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1423 PolicyError,
"NotAuthorized",
"`%s'",filename);
1424 return(MagickFalse);
1426 quantum=(size_t) MagickMaxBufferExtent;
1427 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1428 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1429 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1430 if (blob == (
unsigned char *) NULL)
1432 file=close_utf8(file);
1433 ThrowFileException(&image->exception,ResourceLimitError,
1434 "MemoryAllocationFailed",filename);
1435 return(MagickFalse);
1439 count=read(file,blob,quantum);
1446 length=(size_t) count;
1447 count=WriteBlobStream(image,length,blob);
1448 if (count != (ssize_t) length)
1450 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1455 file=close_utf8(file);
1457 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1459 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1486MagickExport MagickBooleanType GetBlobError(
const Image *image)
1488 assert(image != (
const Image *) NULL);
1489 assert(image->signature == MagickCoreSignature);
1490 if (IsEventLogging() != MagickFalse)
1491 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1492 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1493 errno=image->blob->error_number;
1494 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1519MagickExport FILE *GetBlobFileHandle(
const Image *image)
1521 assert(image != (
const Image *) NULL);
1522 assert(image->signature == MagickCoreSignature);
1523 return(image->blob->file_info.file);
1548MagickExport
void GetBlobInfo(BlobInfo *blob_info)
1550 assert(blob_info != (BlobInfo *) NULL);
1551 (void) memset(blob_info,0,
sizeof(*blob_info));
1552 blob_info->type=UndefinedStream;
1553 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1554 blob_info->properties.st_mtime=GetMagickTime();
1555 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1556 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1557 blob_info->reference_count=1;
1558 blob_info->semaphore=AllocateSemaphoreInfo();
1559 blob_info->signature=MagickCoreSignature;
1584MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1586 assert(image != (Image *) NULL);
1587 assert(image->signature == MagickCoreSignature);
1588 if (IsEventLogging() != MagickFalse)
1589 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1590 return(&image->blob->properties);
1616MagickExport MagickSizeType GetBlobSize(
const Image *image)
1619 *magick_restrict blob_info;
1624 assert(image != (Image *) NULL);
1625 assert(image->signature == MagickCoreSignature);
1626 assert(image->blob != (BlobInfo *) NULL);
1627 if (IsEventLogging() != MagickFalse)
1628 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1629 blob_info=image->blob;
1631 switch (blob_info->type)
1633 case UndefinedStream:
1634 case StandardStream:
1636 extent=blob_info->size;
1644 extent=(MagickSizeType) blob_info->properties.st_size;
1646 extent=blob_info->size;
1647 file_descriptor=fileno(blob_info->file_info.file);
1648 if (file_descriptor == -1)
1650 if (fstat(file_descriptor,&blob_info->properties) == 0)
1651 extent=(MagickSizeType) blob_info->properties.st_size;
1656 extent=blob_info->size;
1665 status=GetPathAttributes(image->filename,&blob_info->properties);
1666 if (status != MagickFalse)
1667 extent=(MagickSizeType) blob_info->properties.st_size;
1674 extent=(MagickSizeType) blob_info->length;
1703MagickExport
unsigned char *GetBlobStreamData(
const Image *image)
1705 assert(image != (
const Image *) NULL);
1706 assert(image->signature == MagickCoreSignature);
1707 return(image->blob->data);
1732MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1734 assert(image != (
const Image *) NULL);
1735 assert(image->signature == MagickCoreSignature);
1736 if (IsEventLogging() != MagickFalse)
1737 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1738 return(image->blob->stream);
1774MagickExport
unsigned char *ImageToBlob(
const ImageInfo *image_info,
1775 Image *image,
size_t *length,ExceptionInfo *exception)
1789 assert(image_info != (
const ImageInfo *) NULL);
1790 assert(image_info->signature == MagickCoreSignature);
1791 assert(image != (Image *) NULL);
1792 assert(image->signature == MagickCoreSignature);
1793 assert(exception != (ExceptionInfo *) NULL);
1794 assert(exception->signature == MagickCoreSignature);
1795 if (IsEventLogging() != MagickFalse)
1796 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1797 image_info->filename);
1799 blob=(
unsigned char *) NULL;
1800 blob_info=CloneImageInfo(image_info);
1801 blob_info->adjoin=MagickFalse;
1802 (void) SetImageInfo(blob_info,1,exception);
1803 if (*blob_info->magick !=
'\0')
1804 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
1805 magick_info=GetMagickInfo(image->magick,exception);
1806 if (magick_info == (
const MagickInfo *) NULL)
1808 (void) ThrowMagickException(exception,GetMagickModule(),
1809 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
1811 blob_info=DestroyImageInfo(blob_info);
1814 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
1815 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1820 blob_info->length=0;
1821 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1822 sizeof(
unsigned char));
1823 if (blob_info->blob == NULL)
1824 (void) ThrowMagickException(exception,GetMagickModule(),
1825 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1828 (void) CloseBlob(image);
1829 image->blob->exempt=MagickTrue;
1830 *image->filename=
'\0';
1831 status=WriteImage(blob_info,image);
1832 InheritException(exception,&image->exception);
1833 *length=image->blob->length;
1834 blob=DetachBlob(image->blob);
1835 if (blob != (
void *) NULL)
1837 if (status == MagickFalse)
1838 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1840 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
1841 sizeof(
unsigned char));
1843 else if (status == MagickFalse)
1844 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
1850 unique[MagickPathExtent];
1858 file=AcquireUniqueFileResource(unique);
1861 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
1862 image_info->filename);
1866 blob_info->file=fdopen(file,
"wb");
1867 if (blob_info->file != (FILE *) NULL)
1869 (void) FormatLocaleString(image->filename,MagickPathExtent,
1870 "%s:%s",image->magick,unique);
1871 status=WriteImage(blob_info,image);
1872 (void) fclose(blob_info->file);
1873 if (status == MagickFalse)
1874 InheritException(exception,&image->exception);
1876 blob=FileToBlob(unique,SIZE_MAX,length,exception);
1878 (void) RelinquishUniqueFileResource(unique);
1881 blob_info=DestroyImageInfo(blob_info);
1913MagickExport MagickBooleanType ImageToFile(Image *image,
char *filename,
1914 ExceptionInfo *exception)
1938 assert(image != (Image *) NULL);
1939 assert(image->signature == MagickCoreSignature);
1940 assert(image->blob != (BlobInfo *) NULL);
1941 assert(image->blob->type != UndefinedStream);
1942 assert(filename != (
const char *) NULL);
1943 if (IsEventLogging() != MagickFalse)
1944 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1945 if (*filename ==
'\0')
1946 file=AcquireUniqueFileResource(filename);
1948 if (LocaleCompare(filename,
"-") == 0)
1949 file=fileno(stdout);
1951 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
1954 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1955 return(MagickFalse);
1957 quantum=(size_t) MagickMaxBufferExtent;
1958 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1959 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1960 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
1961 if (buffer == (
unsigned char *) NULL)
1963 file=close_utf8(file)-1;
1964 (void) ThrowMagickException(exception,GetMagickModule(),
1965 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
1966 return(MagickFalse);
1969 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1970 for (i=0; count > 0; )
1972 length=(size_t) count;
1973 for (i=0; i < length; i+=count)
1975 count=write(file,p+i,(
size_t) (length-i));
1985 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1987 if (LocaleCompare(filename,
"-") != 0)
1988 file=close_utf8(file);
1989 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
1990 if ((file == -1) || (i < length))
1993 file=close_utf8(file);
1994 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1995 return(MagickFalse);
2035MagickExport
unsigned char *ImagesToBlob(
const ImageInfo *image_info,
2036 Image *images,
size_t *length,ExceptionInfo *exception)
2050 assert(image_info != (
const ImageInfo *) NULL);
2051 assert(image_info->signature == MagickCoreSignature);
2052 assert(images != (Image *) NULL);
2053 assert(images->signature == MagickCoreSignature);
2054 assert(exception != (ExceptionInfo *) NULL);
2055 if (IsEventLogging() != MagickFalse)
2056 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2057 image_info->filename);
2059 blob=(
unsigned char *) NULL;
2060 blob_info=CloneImageInfo(image_info);
2061 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2063 if (*blob_info->magick !=
'\0')
2064 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2065 magick_info=GetMagickInfo(images->magick,exception);
2066 if (magick_info == (
const MagickInfo *) NULL)
2068 (void) ThrowMagickException(exception,GetMagickModule(),
2069 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2071 blob_info=DestroyImageInfo(blob_info);
2074 if (GetMagickAdjoin(magick_info) == MagickFalse)
2076 blob_info=DestroyImageInfo(blob_info);
2077 return(ImageToBlob(image_info,images,length,exception));
2079 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2080 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2085 blob_info->length=0;
2086 blob_info->blob=(
void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2087 sizeof(
unsigned char));
2088 if (blob_info->blob == (
void *) NULL)
2089 (void) ThrowMagickException(exception,GetMagickModule(),
2090 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2093 (void) CloseBlob(images);
2094 images->blob->exempt=MagickTrue;
2095 *images->filename=
'\0';
2096 status=WriteImages(blob_info,images,images->filename,exception);
2097 *length=images->blob->length;
2098 blob=DetachBlob(images->blob);
2099 if (blob != (
void *) NULL)
2101 if (status == MagickFalse)
2102 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2104 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
2105 sizeof(
unsigned char));
2108 if (status == MagickFalse)
2109 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2115 filename[MagickPathExtent],
2116 unique[MagickPathExtent];
2124 file=AcquireUniqueFileResource(unique);
2127 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2128 image_info->filename);
2132 blob_info->file=fdopen(file,
"wb");
2133 if (blob_info->file != (FILE *) NULL)
2135 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2136 images->magick,unique);
2137 status=WriteImages(blob_info,images,filename,exception);
2138 (void) fclose(blob_info->file);
2139 if (status == MagickFalse)
2140 InheritException(exception,&images->exception);
2142 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2144 (void) RelinquishUniqueFileResource(unique);
2147 blob_info=DestroyImageInfo(blob_info);
2183MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2184 Image *image,Image *inject_image,
const char *format,ExceptionInfo *exception)
2187 filename[MagickPathExtent];
2216 assert(image_info != (ImageInfo *) NULL);
2217 assert(image_info->signature == MagickCoreSignature);
2218 assert(image != (Image *) NULL);
2219 assert(image->signature == MagickCoreSignature);
2220 assert(inject_image != (Image *) NULL);
2221 assert(inject_image->signature == MagickCoreSignature);
2222 assert(exception != (ExceptionInfo *) NULL);
2223 if (IsEventLogging() != MagickFalse)
2224 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2225 unique_file=(FILE *) NULL;
2226 file=AcquireUniqueFileResource(filename);
2228 unique_file=fdopen(file,
"wb");
2229 if ((file == -1) || (unique_file == (FILE *) NULL))
2231 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2232 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2234 return(MagickFalse);
2236 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2237 if (byte_image == (Image *) NULL)
2239 (void) fclose(unique_file);
2240 (void) RelinquishUniqueFileResource(filename);
2241 return(MagickFalse);
2243 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2245 DestroyBlob(byte_image);
2246 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2247 write_info=CloneImageInfo(image_info);
2248 SetImageInfoFile(write_info,unique_file);
2249 status=WriteImage(write_info,byte_image);
2250 write_info=DestroyImageInfo(write_info);
2251 byte_image=DestroyImage(byte_image);
2252 (void) fclose(unique_file);
2253 if (status == MagickFalse)
2255 (void) RelinquishUniqueFileResource(filename);
2256 return(MagickFalse);
2261 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2264 (void) RelinquishUniqueFileResource(filename);
2265 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2266 image_info->filename);
2267 return(MagickFalse);
2269 quantum=(size_t) MagickMaxBufferExtent;
2270 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2271 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2272 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2273 if (buffer == (
unsigned char *) NULL)
2275 (void) RelinquishUniqueFileResource(filename);
2276 file=close_utf8(file);
2277 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2282 ssize_t count = read(file,buffer,quantum);
2289 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2292 file=close_utf8(file);
2294 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2295 (void) RelinquishUniqueFileResource(filename);
2296 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2322MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2324 assert(image != (
const Image *) NULL);
2325 assert(image->signature == MagickCoreSignature);
2326 if (IsEventLogging() != MagickFalse)
2327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2328 return(image->blob->exempt);
2353MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2356 *magick_restrict blob_info;
2358 assert(image != (
const Image *) NULL);
2359 assert(image->signature == MagickCoreSignature);
2360 if (IsEventLogging() != MagickFalse)
2361 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2362 blob_info=image->blob;
2363 switch (blob_info->type)
2372 if (blob_info->file_info.file == (FILE *) NULL)
2373 return(MagickFalse);
2374 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2375 return(status == -1 ? MagickFalse : MagickTrue);
2379#if defined(MAGICKCORE_ZLIB_DELEGATE)
2383 if (blob_info->file_info.gzfile == (gzFile) NULL)
2384 return(MagickFalse);
2385 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2386 return(offset < 0 ? MagickFalse : MagickTrue);
2391 case UndefinedStream:
2395 case StandardStream:
2400 return(MagickFalse);
2425MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2427 assert(image != (
const Image *) NULL);
2428 assert(image->signature == MagickCoreSignature);
2429 if (IsEventLogging() != MagickFalse)
2430 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2431 return(image->blob->temporary);
2463MagickExport
unsigned char *MapBlob(
int file,
const MapMode mode,
2464 const MagickOffsetType offset,
const size_t length)
2466#if defined(MAGICKCORE_HAVE_MMAP)
2479#if defined(MAP_ANONYMOUS)
2480 flags|=MAP_ANONYMOUS;
2482 return((
unsigned char *) NULL);
2489 protection=PROT_READ;
2495 protection=PROT_WRITE;
2501 protection=PROT_READ | PROT_WRITE;
2506#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2507 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,offset);
2509 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags |
2510 MAP_HUGETLB,file,offset);
2511 if (map == (
unsigned char *) MAP_FAILED)
2512 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,
2515 if (map == (
unsigned char *) MAP_FAILED)
2516 return((
unsigned char *) NULL);
2523 return((
unsigned char *) NULL);
2552MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
2561 assert(buffer != (
unsigned char *) NULL);
2568 *buffer++=(
unsigned char) c;
2572 *buffer++=(
unsigned char) c;
2602MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
2610 assert(p != (
unsigned char *) NULL);
2617 *p++=(
unsigned char) c;
2653static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
2665 size=MagickMinBufferExtent;
2666 option=GetImageOption(image_info,
"stream:buffer-size");
2667 if (option != (
const char *) NULL)
2668 size=StringToUnsignedLong(option);
2669 status=setvbuf(image->blob->file_info.file,(
char *) NULL,size == 0 ?
2670 _IONBF : _IOFBF,size);
2671 return(status == 0 ? MagickTrue : MagickFalse);
2674#if defined(MAGICKCORE_ZLIB_DELEGATE)
2675static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
2677#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2678 return(gzopen(path,mode));
2686 path_wide=NTCreateWidePath(path);
2687 if (path_wide == (
wchar_t *) NULL)
2688 return((gzFile) NULL);
2689 file=gzopen_w(path_wide,mode);
2690 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
2696MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
2697 Image *image,
const BlobMode mode,ExceptionInfo *exception)
2700 *magick_restrict blob_info;
2703 extension[MagickPathExtent],
2704 filename[MagickPathExtent];
2718 assert(image_info != (ImageInfo *) NULL);
2719 assert(image_info->signature == MagickCoreSignature);
2720 assert(image != (Image *) NULL);
2721 assert(image->signature == MagickCoreSignature);
2722 if (IsEventLogging() != MagickFalse)
2723 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2724 image_info->filename);
2725 blob_info=image->blob;
2726 if (image_info->blob != (
void *) NULL)
2728 if (image_info->stream != (StreamHandler) NULL)
2729 blob_info->stream=(StreamHandler) image_info->stream;
2730 AttachBlob(blob_info,image_info->blob,image_info->length);
2733 (void) DetachBlob(blob_info);
2734 blob_info->mode=mode;
2740 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
2744 case ReadBinaryBlobMode:
2746 flags=O_RDONLY | O_BINARY;
2748 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
2753 flags=O_WRONLY | O_CREAT | O_TRUNC;
2755 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
2758 case WriteBinaryBlobMode:
2760 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
2762 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
2763 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
2764 MagickTrue : MagickFalse;
2767 case AppendBlobMode:
2769 flags=O_WRONLY | O_CREAT | O_APPEND;
2771 status=IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow");
2774 case AppendBinaryBlobMode:
2776 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
2778 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow") &&
2779 IsRightsAuthorized(SystemPolicyDomain,WritePolicyRights,
"follow") ?
2780 MagickTrue : MagickFalse;
2787 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,
"follow");
2791#if defined(O_NOFOLLOW)
2792 if (status == MagickFalse)
2796 blob_info->synchronize=image_info->synchronize;
2797 if (image_info->stream != (StreamHandler) NULL)
2799 blob_info->stream=(StreamHandler) image_info->stream;
2802 blob_info->type=FifoStream;
2810 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2811 rights=ReadPolicyRights;
2813 rights=WritePolicyRights;
2814 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2817 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2818 "NotAuthorized",
"`%s'",filename);
2819 return(MagickFalse);
2821 if ((LocaleCompare(filename,
"-") == 0) ||
2822 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
2824 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
2825#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2826 if (strchr(type,
'b') != (
char *) NULL)
2827 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2829 blob_info->type=StandardStream;
2830 blob_info->exempt=MagickTrue;
2831 return(SetStreamBuffering(image_info,image));
2833 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
2834 (IsGeometry(filename+3) != MagickFalse))
2837 fileMode[MagickPathExtent];
2841 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2842 if (blob_info->file_info.file == (FILE *) NULL)
2844 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2845 return(MagickFalse);
2847#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2848 if (strchr(type,
'b') != (
char *) NULL)
2849 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2851 blob_info->type=FileStream;
2852 blob_info->exempt=MagickTrue;
2853 return(SetStreamBuffering(image_info,image));
2855#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2856 if (*filename ==
'|')
2859 fileMode[MagickPathExtent],
2867 (void) signal(SIGPIPE,SIG_IGN);
2871 sanitize_command=SanitizeString(filename+1);
2872 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2874 sanitize_command=DestroyString(sanitize_command);
2875 if (blob_info->file_info.file == (FILE *) NULL)
2877 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2878 return(MagickFalse);
2880 blob_info->type=PipeStream;
2881 blob_info->exempt=MagickTrue;
2882 return(SetStreamBuffering(image_info,image));
2885 status=GetPathAttributes(filename,&blob_info->properties);
2886#if defined(S_ISFIFO)
2887 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2889 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2890 if (blob_info->file_info.file == (FILE *) NULL)
2892 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2893 return(MagickFalse);
2895 blob_info->type=FileStream;
2896 blob_info->exempt=MagickTrue;
2897 return(SetStreamBuffering(image_info,image));
2900 GetPathComponent(image->filename,ExtensionPath,extension);
2903 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2904 if ((image_info->adjoin == MagickFalse) ||
2905 (strchr(filename,
'%') != (
char *) NULL))
2910 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
2911 image->scene,filename);
2912 if ((LocaleCompare(filename,image->filename) == 0) &&
2913 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2914 (GetNextImageInList(image) != (Image *) NULL)))
2917 path[MagickPathExtent];
2919 GetPathComponent(image->filename,RootPath,path);
2920 if (*extension ==
'\0')
2921 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
2922 path,(
double) image->scene);
2924 (
void) FormatLocaleString(filename,MagickPathExtent,
2925 "%s-%.20g.%s",path,(
double) image->scene,extension);
2927 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2928#if defined(macintosh)
2929 SetApplicationType(filename,image_info->magick,
'8BIM');
2933 if (image_info->file != (FILE *) NULL)
2935 blob_info->file_info.file=image_info->file;
2936 blob_info->type=FileStream;
2937 blob_info->exempt=MagickTrue;
2945 blob_info->file_info.file=(FILE *) NULL;
2946 file=open_utf8(filename,flags,0);
2948 blob_info->file_info.file=fdopen(file,type);
2949 if (blob_info->file_info.file != (FILE *) NULL)
2957 blob_info->type=FileStream;
2958 (void) fstat(fileno(blob_info->file_info.file),
2959 &blob_info->properties);
2960 (void) SetStreamBuffering(image_info,image);
2961 (void) memset(magick,0,
sizeof(magick));
2962 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2963 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2964#if defined(MAGICKCORE_POSIX_SUPPORT)
2965 (void) fflush(blob_info->file_info.file);
2967 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2968 " read %.20g magic header bytes",(
double) count);
2969#if defined(MAGICKCORE_ZLIB_DELEGATE)
2970 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2971 ((
int) magick[2] == 0x08))
2974 gzfile = gzopen_utf8(filename,
"rb");
2976 if (gzfile != (gzFile) NULL)
2978 if (blob_info->file_info.file != (FILE *) NULL)
2979 (void) fclose(blob_info->file_info.file);
2980 blob_info->file_info.file=(FILE *) NULL;
2981 blob_info->file_info.gzfile=gzfile;
2982 blob_info->type=ZipStream;
2986#if defined(MAGICKCORE_BZLIB_DELEGATE)
2987 if (strncmp((
char *) magick,
"BZh",3) == 0)
2990 *bzfile = BZ2_bzopen(filename,
"r");
2992 if (bzfile != (BZFILE *) NULL)
2994 if (blob_info->file_info.file != (FILE *) NULL)
2995 (void) fclose(blob_info->file_info.file);
2996 blob_info->file_info.file=(FILE *) NULL;
2997 blob_info->file_info.bzfile=bzfile;
2998 blob_info->type=BZipStream;
3002 if (blob_info->type == FileStream)
3013 sans_exception=AcquireExceptionInfo();
3014 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3015 sans_exception=DestroyExceptionInfo(sans_exception);
3016 length=(size_t) blob_info->properties.st_size;
3017 if ((magick_info != (
const MagickInfo *) NULL) &&
3018 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3019 (length > MagickMaxBufferExtent) &&
3020 (AcquireMagickResource(MapResource,length) != MagickFalse))
3025 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3027 if (blob == (
void *) NULL)
3028 RelinquishMagickResource(MapResource,length);
3034 if (image_info->file != (FILE *) NULL)
3035 blob_info->exempt=MagickFalse;
3038 (void) fclose(blob_info->file_info.file);
3039 blob_info->file_info.file=(FILE *) NULL;
3041 AttachBlob(blob_info,blob,length);
3042 blob_info->mapped=MagickTrue;
3049#if defined(MAGICKCORE_ZLIB_DELEGATE)
3050 if ((LocaleCompare(extension,
"gz") == 0) ||
3051 (LocaleCompare(extension,
"wmz") == 0) ||
3052 (LocaleCompare(extension,
"svgz") == 0))
3054 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3055 if (blob_info->file_info.gzfile != (gzFile) NULL)
3056 blob_info->type=ZipStream;
3060#if defined(MAGICKCORE_BZLIB_DELEGATE)
3061 if (LocaleCompare(extension,
"bz2") == 0)
3063 if (mode == WriteBinaryBlobMode)
3065 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3066 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3067 blob_info->type=BZipStream;
3075 blob_info->file_info.file=(FILE *) NULL;
3076 file=open_utf8(filename,flags,P_MODE);
3078 blob_info->file_info.file=fdopen(file,type);
3079 if (blob_info->file_info.file != (FILE *) NULL)
3081 blob_info->type=FileStream;
3082 (void) SetStreamBuffering(image_info,image);
3085 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3088 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3089 "NotAuthorized",
"`%s'",filename);
3090 return(MagickFalse);
3092 blob_info->status=0;
3093 blob_info->error_number=0;
3094 if (blob_info->type != UndefinedStream)
3095 blob_info->size=GetBlobSize(image);
3098 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3099 return(MagickFalse);
3138#if defined(__cplusplus) || defined(c_plusplus)
3142static size_t PingStream(
const Image *magick_unused(image),
3143 const void *magick_unused(pixels),
const size_t columns)
3145 magick_unreferenced(image);
3146 magick_unreferenced(pixels);
3151#if defined(__cplusplus) || defined(c_plusplus)
3155MagickExport Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3156 const size_t length,ExceptionInfo *exception)
3171 assert(image_info != (ImageInfo *) NULL);
3172 assert(image_info->signature == MagickCoreSignature);
3173 assert(exception != (ExceptionInfo *) NULL);
3174 if (IsEventLogging() != MagickFalse)
3175 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3176 image_info->filename);
3177 if ((blob == (
const void *) NULL) || (length == 0))
3179 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3180 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3181 return((Image *) NULL);
3183 ping_info=CloneImageInfo(image_info);
3184 ping_info->blob=(
void *) blob;
3185 ping_info->length=length;
3186 ping_info->ping=MagickTrue;
3187 if (*ping_info->magick ==
'\0')
3188 (void) SetImageInfo(ping_info,0,exception);
3189 magick_info=GetMagickInfo(ping_info->magick,exception);
3190 if (magick_info == (
const MagickInfo *) NULL)
3192 (void) ThrowMagickException(exception,GetMagickModule(),
3193 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3195 ping_info=DestroyImageInfo(ping_info);
3196 return((Image *) NULL);
3198 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3201 filename[MagickPathExtent];
3206 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3207 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3208 ping_info->magick,filename);
3209 image=ReadStream(ping_info,&PingStream,exception);
3210 if (image != (Image *) NULL)
3211 (void) DetachBlob(image->blob);
3212 ping_info=DestroyImageInfo(ping_info);
3218 ping_info->blob=(
void *) NULL;
3219 ping_info->length=0;
3220 *ping_info->filename=
'\0';
3221 status=BlobToFile(ping_info->filename,blob,length,exception);
3222 if (status == MagickFalse)
3224 (void) RelinquishUniqueFileResource(ping_info->filename);
3225 ping_info=DestroyImageInfo(ping_info);
3226 return((Image *) NULL);
3228 clone_info=CloneImageInfo(ping_info);
3229 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3230 ping_info->magick,ping_info->filename);
3231 image=ReadStream(clone_info,&PingStream,exception);
3232 if (image != (Image *) NULL)
3240 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3242 (void) CopyMagickString(images->filename,image_info->filename,
3244 (void) CopyMagickString(images->magick_filename,image_info->filename,
3246 (void) CopyMagickString(images->magick,magick_info->name,
3248 images=GetNextImageInList(images);
3251 clone_info=DestroyImageInfo(clone_info);
3252 (void) RelinquishUniqueFileResource(ping_info->filename);
3253 ping_info=DestroyImageInfo(ping_info);
3288MagickExport ssize_t ReadBlob(Image *image,
const size_t length,
3289 unsigned char *data)
3292 *magick_restrict blob_info;
3303 assert(image != (Image *) NULL);
3304 assert(image->signature == MagickCoreSignature);
3305 assert(image->blob != (BlobInfo *) NULL);
3306 assert(image->blob->type != UndefinedStream);
3309 assert(data != (
void *) NULL);
3310 blob_info=image->blob;
3313 switch (blob_info->type)
3315 case UndefinedStream:
3317 case StandardStream:
3325 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3330 c=getc(blob_info->file_info.file);
3333 *q++=(
unsigned char) c;
3339 c=getc(blob_info->file_info.file);
3342 *q++=(
unsigned char) c;
3348 c=getc(blob_info->file_info.file);
3351 *q++=(
unsigned char) c;
3357 c=getc(blob_info->file_info.file);
3360 *q++=(
unsigned char) c;
3367 if ((count != (ssize_t) length) &&
3368 (ferror(blob_info->file_info.file) != 0))
3369 ThrowBlobException(blob_info);
3374#if defined(MAGICKCORE_ZLIB_DELEGATE)
3385 for (i=0; i < (ssize_t) length; i+=count)
3387 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3388 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3401 c=gzgetc(blob_info->file_info.gzfile);
3404 *q++=(
unsigned char) c;
3410 c=gzgetc(blob_info->file_info.gzfile);
3413 *q++=(
unsigned char) c;
3419 c=gzgetc(blob_info->file_info.gzfile);
3422 *q++=(
unsigned char) c;
3428 c=gzgetc(blob_info->file_info.gzfile);
3431 *q++=(
unsigned char) c;
3439 (void) gzerror(blob_info->file_info.gzfile,&status);
3440 if ((count != (ssize_t) length) && (status != Z_OK))
3441 ThrowBlobException(blob_info);
3442 if (blob_info->eof == MagickFalse)
3443 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3450#if defined(MAGICKCORE_BZLIB_DELEGATE)
3457 for (i=0; i < (ssize_t) length; i+=count)
3459 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3460 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3470 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3471 if ((count != (ssize_t) length) && (status != BZ_OK))
3472 ThrowBlobException(blob_info);
3483 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3485 blob_info->eof=MagickTrue;
3488 p=blob_info->data+blob_info->offset;
3489 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3490 blob_info->length-blob_info->offset);
3491 blob_info->offset+=count;
3492 if (count != (ssize_t) length)
3493 blob_info->eof=MagickTrue;
3494 (void) memcpy(q,p,(
size_t) count);
3523MagickExport
int ReadBlobByte(Image *image)
3526 *magick_restrict blob_info;
3531 assert(image != (Image *) NULL);
3532 assert(image->signature == MagickCoreSignature);
3533 assert(image->blob != (BlobInfo *) NULL);
3534 assert(image->blob->type != UndefinedStream);
3535 blob_info=image->blob;
3536 switch (blob_info->type)
3538 case StandardStream:
3542 c=getc(blob_info->file_info.file);
3545 if (ferror(blob_info->file_info.file) != 0)
3546 ThrowBlobException(blob_info);
3553 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3555 blob_info->eof=MagickTrue;
3558 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
3559 blob_info->offset++;
3570 count=ReadBlob(image,1,buffer);
3603MagickExport
double ReadBlobDouble(Image *image)
3614 quantum.double_value=0.0;
3615 quantum.unsigned_value=ReadBlobLongLong(image);
3616 return(quantum.double_value);
3642MagickExport
float ReadBlobFloat(Image *image)
3653 quantum.float_value=0.0;
3654 quantum.unsigned_value=ReadBlobLong(image);
3655 return(quantum.float_value);
3681MagickExport
unsigned int ReadBlobLong(Image *image)
3695 assert(image != (Image *) NULL);
3696 assert(image->signature == MagickCoreSignature);
3698 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3701 if (image->endian == LSBEndian)
3703 value=(
unsigned int) (*p++);
3704 value|=(
unsigned int) (*p++) << 8;
3705 value|=(
unsigned int) (*p++) << 16;
3706 value|=(
unsigned int) (*p++) << 24;
3709 value=(
unsigned int) (*p++) << 24;
3710 value|=(
unsigned int) (*p++) << 16;
3711 value|=(
unsigned int) (*p++) << 8;
3712 value|=(
unsigned int) (*p++);
3739MagickExport MagickSizeType ReadBlobLongLong(Image *image)
3753 assert(image != (Image *) NULL);
3754 assert(image->signature == MagickCoreSignature);
3756 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3758 return(MagickULLConstant(0));
3759 if (image->endian == LSBEndian)
3761 value=(MagickSizeType) (*p++);
3762 value|=(MagickSizeType) (*p++) << 8;
3763 value|=(MagickSizeType) (*p++) << 16;
3764 value|=(MagickSizeType) (*p++) << 24;
3765 value|=(MagickSizeType) (*p++) << 32;
3766 value|=(MagickSizeType) (*p++) << 40;
3767 value|=(MagickSizeType) (*p++) << 48;
3768 value|=(MagickSizeType) (*p++) << 56;
3771 value=(MagickSizeType) (*p++) << 56;
3772 value|=(MagickSizeType) (*p++) << 48;
3773 value|=(MagickSizeType) (*p++) << 40;
3774 value|=(MagickSizeType) (*p++) << 32;
3775 value|=(MagickSizeType) (*p++) << 24;
3776 value|=(MagickSizeType) (*p++) << 16;
3777 value|=(MagickSizeType) (*p++) << 8;
3778 value|=(MagickSizeType) (*p++);
3805MagickExport
unsigned short ReadBlobShort(Image *image)
3819 assert(image != (Image *) NULL);
3820 assert(image->signature == MagickCoreSignature);
3822 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3824 return((
unsigned short) 0U);
3825 if (image->endian == LSBEndian)
3827 value=(
unsigned short) (*p++);
3828 value|=(
unsigned short) (*p++) << 8;
3831 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3832 value|=(
unsigned short) (*p++);
3859MagickExport
unsigned int ReadBlobLSBLong(Image *image)
3873 assert(image != (Image *) NULL);
3874 assert(image->signature == MagickCoreSignature);
3876 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3879 value=(
unsigned int) (*p++);
3880 value|=(
unsigned int) (*p++) << 8;
3881 value|=(
unsigned int) (*p++) << 16;
3882 value|=(
unsigned int) (*p++) << 24;
3909MagickExport
signed int ReadBlobLSBSignedLong(Image *image)
3920 quantum.unsigned_value=ReadBlobLSBLong(image);
3921 return(quantum.signed_value);
3947MagickExport
unsigned short ReadBlobLSBShort(Image *image)
3961 assert(image != (Image *) NULL);
3962 assert(image->signature == MagickCoreSignature);
3964 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3966 return((
unsigned short) 0U);
3967 value=(
unsigned short) (*p++);
3968 value|=(
unsigned short) (*p++) << 8;
3995MagickExport
signed short ReadBlobLSBSignedShort(Image *image)
4006 quantum.unsigned_value=ReadBlobLSBShort(image);
4007 return(quantum.signed_value);
4033MagickExport
unsigned int ReadBlobMSBLong(Image *image)
4047 assert(image != (Image *) NULL);
4048 assert(image->signature == MagickCoreSignature);
4050 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4053 value=(
unsigned int) (*p++) << 24;
4054 value|=(
unsigned int) (*p++) << 16;
4055 value|=(
unsigned int) (*p++) << 8;
4056 value|=(
unsigned int) (*p++);
4083MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4097 assert(image != (Image *) NULL);
4098 assert(image->signature == MagickCoreSignature);
4100 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4102 return(MagickULLConstant(0));
4103 value=(MagickSizeType) (*p++) << 56;
4104 value|=(MagickSizeType) (*p++) << 48;
4105 value|=(MagickSizeType) (*p++) << 40;
4106 value|=(MagickSizeType) (*p++) << 32;
4107 value|=(MagickSizeType) (*p++) << 24;
4108 value|=(MagickSizeType) (*p++) << 16;
4109 value|=(MagickSizeType) (*p++) << 8;
4110 value|=(MagickSizeType) (*p++);
4137MagickExport
unsigned short ReadBlobMSBShort(Image *image)
4151 assert(image != (Image *) NULL);
4152 assert(image->signature == MagickCoreSignature);
4154 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4156 return((
unsigned short) 0U);
4157 value=(
unsigned short) ((*p++) << 8);
4158 value|=(
unsigned short) (*p++);
4185MagickExport
signed int ReadBlobMSBSignedLong(Image *image)
4196 quantum.unsigned_value=ReadBlobMSBLong(image);
4197 return(quantum.signed_value);
4223MagickExport
signed short ReadBlobMSBSignedShort(Image *image)
4234 quantum.unsigned_value=ReadBlobMSBShort(image);
4235 return(quantum.signed_value);
4261MagickExport
signed int ReadBlobSignedLong(Image *image)
4272 quantum.unsigned_value=ReadBlobLong(image);
4273 return(quantum.signed_value);
4299MagickExport
signed short ReadBlobSignedShort(Image *image)
4310 quantum.unsigned_value=ReadBlobShort(image);
4311 return(quantum.signed_value);
4349MagickExport magick_hot_spot
const void *ReadBlobStream(Image *image,
4350 const size_t length,
void *magick_restrict data,ssize_t *count)
4353 *magick_restrict blob_info;
4355 assert(image != (Image *) NULL);
4356 assert(image->signature == MagickCoreSignature);
4357 assert(image->blob != (BlobInfo *) NULL);
4358 assert(image->blob->type != UndefinedStream);
4359 assert(count != (ssize_t *) NULL);
4360 blob_info=image->blob;
4361 if (blob_info->type != BlobStream)
4363 assert(data != NULL);
4364 *count=ReadBlob(image,length,(
unsigned char *) data);
4367 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4370 blob_info->eof=MagickTrue;
4373 data=blob_info->data+blob_info->offset;
4374 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4375 blob_info->length-blob_info->offset);
4376 blob_info->offset+=(*count);
4377 if (*count != (ssize_t) length)
4378 blob_info->eof=MagickTrue;
4407MagickExport
char *ReadBlobString(Image *image,
char *
string)
4410 *magick_restrict blob_info;
4418 assert(image != (Image *) NULL);
4419 assert(image->signature == MagickCoreSignature);
4420 assert(image->blob != (BlobInfo *) NULL);
4421 assert(image->blob->type != UndefinedStream);
4422 if (IsEventLogging() != MagickFalse)
4423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4425 blob_info=image->blob;
4426 switch (blob_info->type)
4428 case UndefinedStream:
4430 case StandardStream:
4433 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4434 if (p == (
char *) NULL)
4436 if (ferror(blob_info->file_info.file) != 0)
4437 ThrowBlobException(blob_info);
4438 return((
char *) NULL);
4445#if defined(MAGICKCORE_ZLIB_DELEGATE)
4446 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4447 if (p == (
char *) NULL)
4450 (void) gzerror(blob_info->file_info.gzfile,&status);
4452 ThrowBlobException(blob_info);
4453 return((
char *) NULL);
4463 c=ReadBlobByte(image);
4466 blob_info->eof=MagickTrue;
4472 }
while (i < (MaxTextExtent-2));
4480 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4483 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4485 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4486 return((
char *) NULL);
4513MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
4515 assert(blob != (BlobInfo *) NULL);
4516 assert(blob->signature == MagickCoreSignature);
4517 if (IsEventLogging() != MagickFalse)
4518 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4519 LockSemaphoreInfo(blob->semaphore);
4520 blob->reference_count++;
4521 UnlockSemaphoreInfo(blob->semaphore);
4558MagickExport MagickOffsetType SeekBlob(Image *image,
4559 const MagickOffsetType offset,
const int whence)
4562 *magick_restrict blob_info;
4564 assert(image != (Image *) NULL);
4565 assert(image->signature == MagickCoreSignature);
4566 assert(image->blob != (BlobInfo *) NULL);
4567 assert(image->blob->type != UndefinedStream);
4568 if (IsEventLogging() != MagickFalse)
4569 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4570 blob_info=image->blob;
4571 switch (blob_info->type)
4573 case UndefinedStream:
4575 case StandardStream:
4580 if ((offset < 0) && (whence == SEEK_SET))
4582 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4584 blob_info->offset=TellBlob(image);
4589#if defined(MAGICKCORE_ZLIB_DELEGATE)
4590 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4593 blob_info->offset=TellBlob(image);
4609 blob_info->offset=offset;
4614 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4615 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4620 if ((blob_info->offset+offset) < 0)
4622 blob_info->offset+=offset;
4627 if (((MagickOffsetType) blob_info->length+offset) < 0)
4629 blob_info->offset=blob_info->length+offset;
4633 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4635 blob_info->eof=MagickFalse;
4641 return(blob_info->offset);
4669MagickExport
void SetBlobExempt(Image *image,
const MagickBooleanType exempt)
4671 assert(image != (
const Image *) NULL);
4672 assert(image->signature == MagickCoreSignature);
4673 if (IsEventLogging() != MagickFalse)
4674 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4675 image->blob->exempt=exempt;
4704MagickExport MagickBooleanType SetBlobExtent(Image *image,
4705 const MagickSizeType extent)
4708 *magick_restrict blob_info;
4710 assert(image != (Image *) NULL);
4711 assert(image->signature == MagickCoreSignature);
4712 assert(image->blob != (BlobInfo *) NULL);
4713 assert(image->blob->type != UndefinedStream);
4714 if (IsEventLogging() != MagickFalse)
4715 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4716 blob_info=image->blob;
4717 switch (blob_info->type)
4719 case UndefinedStream:
4721 case StandardStream:
4722 return(MagickFalse);
4731 if (extent != (MagickSizeType) ((off_t) extent))
4732 return(MagickFalse);
4733 offset=SeekBlob(image,0,SEEK_END);
4735 return(MagickFalse);
4736 if ((MagickSizeType) offset >= extent)
4738 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4741 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4742 blob_info->file_info.file);
4743#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4744 if (blob_info->synchronize != MagickFalse)
4749 file=fileno(blob_info->file_info.file);
4750 if ((file == -1) || (offset < 0))
4751 return(MagickFalse);
4752 (void) posix_fallocate(file,offset,extent-offset);
4755 offset=SeekBlob(image,offset,SEEK_SET);
4757 return(MagickFalse);
4762 return(MagickFalse);
4764 return(MagickFalse);
4766 return(MagickFalse);
4769 if (extent != (MagickSizeType) ((
size_t) extent))
4770 return(MagickFalse);
4771 if (blob_info->mapped != MagickFalse)
4779 (void) UnmapBlob(blob_info->data,blob_info->length);
4780 RelinquishMagickResource(MapResource,blob_info->length);
4781 if (extent != (MagickSizeType) ((off_t) extent))
4782 return(MagickFalse);
4783 offset=SeekBlob(image,0,SEEK_END);
4785 return(MagickFalse);
4786 if ((MagickSizeType) offset >= extent)
4788 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4789 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4790 blob_info->file_info.file);
4791#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4792 if (blob_info->synchronize != MagickFalse)
4797 file=fileno(blob_info->file_info.file);
4798 if ((file == -1) || (offset < 0))
4799 return(MagickFalse);
4800 (void) posix_fallocate(file,offset,extent-offset);
4803 offset=SeekBlob(image,offset,SEEK_SET);
4805 return(MagickFalse);
4806 (void) AcquireMagickResource(MapResource,extent);
4807 blob_info->data=(
unsigned char*) MapBlob(fileno(
4808 blob_info->file_info.file),WriteMode,0,(
size_t) extent);
4809 blob_info->extent=(size_t) extent;
4810 blob_info->length=(size_t) extent;
4811 (void) SyncBlob(image);
4814 blob_info->extent=(size_t) extent;
4815 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4816 blob_info->extent+1,
sizeof(*blob_info->data));
4817 (void) SyncBlob(image);
4818 if (blob_info->data == (
unsigned char *) NULL)
4820 (void) DetachBlob(blob_info);
4821 return(MagickFalse);
4853static int SyncBlob(
const Image *image)
4856 *magick_restrict blob_info;
4861 assert(image != (Image *) NULL);
4862 assert(image->signature == MagickCoreSignature);
4863 assert(image->blob != (BlobInfo *) NULL);
4864 if (IsEventLogging() != MagickFalse)
4865 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4866 if (EOFBlob(image) != 0)
4868 blob_info=image->blob;
4870 switch (blob_info->type)
4872 case UndefinedStream:
4873 case StandardStream:
4878 status=fflush(blob_info->file_info.file);
4883#if defined(MAGICKCORE_ZLIB_DELEGATE)
4884 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4890#if defined(MAGICKCORE_BZLIB_DELEGATE)
4891 status=BZ2_bzflush(blob_info->file_info.bzfile);
4925MagickExport MagickOffsetType TellBlob(
const Image *image)
4928 *magick_restrict blob_info;
4933 assert(image != (Image *) NULL);
4934 assert(image->signature == MagickCoreSignature);
4935 assert(image->blob != (BlobInfo *) NULL);
4936 assert(image->blob->type != UndefinedStream);
4937 if (IsEventLogging() != MagickFalse)
4938 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4939 blob_info=image->blob;
4941 switch (blob_info->type)
4943 case UndefinedStream:
4944 case StandardStream:
4948 offset=ftell(blob_info->file_info.file);
4955#if defined(MAGICKCORE_ZLIB_DELEGATE)
4956 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4966 offset=blob_info->offset;
4998MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
5000#if defined(MAGICKCORE_HAVE_MMAP)
5004 status=munmap(map,length);
5005 return(status == -1 ? MagickFalse : MagickTrue);
5009 return(MagickFalse);
5042MagickExport ssize_t WriteBlob(Image *image,
const size_t length,
5043 const unsigned char *data)
5046 *magick_restrict blob_info;
5060 assert(image != (Image *) NULL);
5061 assert(image->signature == MagickCoreSignature);
5062 assert(image->blob != (BlobInfo *) NULL);
5063 assert(image->blob->type != UndefinedStream);
5066 assert(data != (
const unsigned char *) NULL);
5067 blob_info=image->blob;
5069 p=(
const unsigned char *) data;
5070 q=(
unsigned char *) data;
5071 switch (blob_info->type)
5073 case UndefinedStream:
5075 case StandardStream:
5083 count=(ssize_t) fwrite((
const char *) data,1,length,
5084 blob_info->file_info.file);
5089 c=putc((
int) *p++,blob_info->file_info.file);
5097 c=putc((
int) *p++,blob_info->file_info.file);
5105 c=putc((
int) *p++,blob_info->file_info.file);
5113 c=putc((
int) *p++,blob_info->file_info.file);
5122 if ((count != (ssize_t) length) &&
5123 (ferror(blob_info->file_info.file) != 0))
5124 ThrowBlobException(blob_info);
5129#if defined(MAGICKCORE_ZLIB_DELEGATE)
5140 for (i=0; i < (ssize_t) length; i+=count)
5142 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5143 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5156 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5164 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5172 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5180 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5190 (void) gzerror(blob_info->file_info.gzfile,&status);
5191 if ((count != (ssize_t) length) && (status != Z_OK))
5192 ThrowBlobException(blob_info);
5198#if defined(MAGICKCORE_BZLIB_DELEGATE)
5205 for (i=0; i < (ssize_t) length; i+=count)
5207 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5208 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5218 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5219 if ((count != (ssize_t) length) && (status != BZ_OK))
5220 ThrowBlobException(blob_info);
5226 count=(ssize_t) blob_info->stream(image,data,length);
5234 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5239 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5240 if (extent >= blob_info->extent)
5242 extent+=blob_info->quantum+length;
5243 blob_info->quantum<<=1;
5244 if (SetBlobExtent(image,extent) == MagickFalse)
5247 q=blob_info->data+blob_info->offset;
5248 (void) memcpy(q,p,length);
5249 blob_info->offset+=length;
5250 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5251 blob_info->length=(size_t) blob_info->offset;
5252 count=(ssize_t) length;
5255 if (count != (ssize_t) length)
5256 ThrowBlobException(blob_info);
5285MagickExport ssize_t WriteBlobByte(Image *image,
const unsigned char value)
5288 *magick_restrict blob_info;
5293 assert(image != (Image *) NULL);
5294 assert(image->signature == MagickCoreSignature);
5295 assert(image->blob != (BlobInfo *) NULL);
5296 assert(image->blob->type != UndefinedStream);
5297 blob_info=image->blob;
5299 switch (blob_info->type)
5301 case StandardStream:
5308 c=putc((
int) value,blob_info->file_info.file);
5311 if (ferror(blob_info->file_info.file) != 0)
5312 ThrowBlobException(blob_info);
5320 count=WriteBlobStream(image,1,&value);
5352MagickExport ssize_t WriteBlobFloat(Image *image,
const float value)
5363 quantum.unsigned_value=0U;
5364 quantum.float_value=value;
5365 return(WriteBlobLong(image,quantum.unsigned_value));
5393MagickExport ssize_t WriteBlobLong(Image *image,
const unsigned int value)
5398 assert(image != (Image *) NULL);
5399 assert(image->signature == MagickCoreSignature);
5400 if (image->endian == LSBEndian)
5402 buffer[0]=(
unsigned char) value;
5403 buffer[1]=(
unsigned char) (value >> 8);
5404 buffer[2]=(
unsigned char) (value >> 16);
5405 buffer[3]=(
unsigned char) (value >> 24);
5406 return(WriteBlobStream(image,4,buffer));
5408 buffer[0]=(
unsigned char) (value >> 24);
5409 buffer[1]=(
unsigned char) (value >> 16);
5410 buffer[2]=(
unsigned char) (value >> 8);
5411 buffer[3]=(
unsigned char) value;
5412 return(WriteBlobStream(image,4,buffer));
5440MagickExport ssize_t WriteBlobShort(Image *image,
const unsigned short value)
5445 assert(image != (Image *) NULL);
5446 assert(image->signature == MagickCoreSignature);
5447 if (image->endian == LSBEndian)
5449 buffer[0]=(
unsigned char) value;
5450 buffer[1]=(
unsigned char) (value >> 8);
5451 return(WriteBlobStream(image,2,buffer));
5453 buffer[0]=(
unsigned char) (value >> 8);
5454 buffer[1]=(
unsigned char) value;
5455 return(WriteBlobStream(image,2,buffer));
5483MagickExport ssize_t WriteBlobLSBLong(Image *image,
const unsigned int value)
5488 assert(image != (Image *) NULL);
5489 assert(image->signature == MagickCoreSignature);
5490 buffer[0]=(
unsigned char) value;
5491 buffer[1]=(
unsigned char) (value >> 8);
5492 buffer[2]=(
unsigned char) (value >> 16);
5493 buffer[3]=(
unsigned char) (value >> 24);
5494 return(WriteBlobStream(image,4,buffer));
5522MagickExport ssize_t WriteBlobLSBShort(Image *image,
const unsigned short value)
5527 assert(image != (Image *) NULL);
5528 assert(image->signature == MagickCoreSignature);
5529 buffer[0]=(
unsigned char) value;
5530 buffer[1]=(
unsigned char) (value >> 8);
5531 return(WriteBlobStream(image,2,buffer));
5559MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,
const signed int value)
5573 assert(image != (Image *) NULL);
5574 assert(image->signature == MagickCoreSignature);
5575 quantum.signed_value=value;
5576 buffer[0]=(
unsigned char) quantum.unsigned_value;
5577 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5578 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5579 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5580 return(WriteBlobStream(image,4,buffer));
5608MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
5609 const signed short value)
5623 assert(image != (Image *) NULL);
5624 assert(image->signature == MagickCoreSignature);
5625 quantum.signed_value=value;
5626 buffer[0]=(
unsigned char) quantum.unsigned_value;
5627 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5628 return(WriteBlobStream(image,2,buffer));
5656MagickExport ssize_t WriteBlobMSBLong(Image *image,
const unsigned int value)
5661 assert(image != (Image *) NULL);
5662 assert(image->signature == MagickCoreSignature);
5663 buffer[0]=(
unsigned char) (value >> 24);
5664 buffer[1]=(
unsigned char) (value >> 16);
5665 buffer[2]=(
unsigned char) (value >> 8);
5666 buffer[3]=(
unsigned char) value;
5667 return(WriteBlobStream(image,4,buffer));
5695MagickExport ssize_t WriteBlobMSBLongLong(Image *image,
5696 const MagickSizeType value)
5701 assert(image != (Image *) NULL);
5702 assert(image->signature == MagickCoreSignature);
5703 buffer[0]=(
unsigned char) (value >> 56);
5704 buffer[1]=(
unsigned char) (value >> 48);
5705 buffer[2]=(
unsigned char) (value >> 40);
5706 buffer[3]=(
unsigned char) (value >> 32);
5707 buffer[4]=(
unsigned char) (value >> 24);
5708 buffer[5]=(
unsigned char) (value >> 16);
5709 buffer[6]=(
unsigned char) (value >> 8);
5710 buffer[7]=(
unsigned char) value;
5711 return(WriteBlobStream(image,8,buffer));
5739MagickExport ssize_t WriteBlobMSBShort(Image *image,
const unsigned short value)
5744 assert(image != (Image *) NULL);
5745 assert(image->signature == MagickCoreSignature);
5746 buffer[0]=(
unsigned char) (value >> 8);
5747 buffer[1]=(
unsigned char) value;
5748 return(WriteBlobStream(image,2,buffer));
5776MagickExport ssize_t WriteBlobMSBSignedLong(Image *image,
const signed int value)
5790 assert(image != (Image *) NULL);
5791 assert(image->signature == MagickCoreSignature);
5792 quantum.signed_value=value;
5793 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5794 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5795 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5796 buffer[3]=(
unsigned char) quantum.unsigned_value;
5797 return(WriteBlobStream(image,4,buffer));
5825MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
5826 const signed short value)
5840 assert(image != (Image *) NULL);
5841 assert(image->signature == MagickCoreSignature);
5842 quantum.signed_value=value;
5843 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5844 buffer[1]=(
unsigned char) quantum.unsigned_value;
5845 return(WriteBlobStream(image,2,buffer));
5873MagickExport ssize_t WriteBlobString(Image *image,
const char *
string)
5875 assert(image != (Image *) NULL);
5876 assert(image->signature == MagickCoreSignature);
5877 assert(
string != (
const char *) NULL);
5878 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));