47 #include "magick/studio.h" 48 #include "magick/blob.h" 49 #include "magick/blob-private.h" 50 #include "magick/cache.h" 51 #include "magick/client.h" 52 #include "magick/constitute.h" 53 #include "magick/delegate.h" 54 #include "magick/exception.h" 55 #include "magick/exception-private.h" 56 #include "magick/geometry.h" 57 #include "magick/image-private.h" 58 #include "magick/list.h" 59 #include "magick/locale_.h" 60 #include "magick/log.h" 61 #include "magick/magick.h" 62 #include "magick/memory_.h" 63 #include "magick/nt-base-private.h" 64 #include "magick/option.h" 65 #include "magick/policy.h" 66 #include "magick/policy-private.h" 67 #include "magick/resource_.h" 68 #include "magick/semaphore.h" 69 #include "magick/string_.h" 70 #include "magick/string-private.h" 71 #include "magick/timer-private.h" 72 #include "magick/token.h" 73 #include "magick/utility.h" 74 #include "magick/utility-private.h" 75 #if defined(MAGICKCORE_ZLIB_DELEGATE) 78 #if defined(MAGICKCORE_BZLIB_DELEGATE) 85 #define MagickMaxBlobExtent (8*8192) 86 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) 87 # define MAP_ANONYMOUS MAP_ANON 89 #if !defined(MAP_FAILED) 90 #define MAP_FAILED ((void *) -1) 94 #define _O_BINARY O_BINARY 96 #if defined(MAGICKCORE_WINDOWS_SUPPORT) 98 # define fsync _commit 101 # define MAGICKCORE_HAVE_MMAP 1 102 # define mmap(address,length,protection,access,file,offset) \ 103 NTMapMemory(address,length,protection,access,file,offset) 105 # if !defined(munmap) 106 # define munmap(address,length) NTUnmapMemory(address,length) 108 # if !defined(pclose) 109 # define pclose _pclose 112 # define popen _popen 124 #if defined(MAGICKCORE_ZLIB_DELEGATE) 129 #if defined(MAGICKCORE_BZLIB_DELEGATE) 199 SyncBlob(
const Image *);
228 MagickExport
void AttachBlob(
BlobInfo *blob_info,
const void *blob,
231 assert(blob_info != (
BlobInfo *) NULL);
232 if (IsEventLogging() != MagickFalse)
233 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
234 blob_info->length=length;
235 blob_info->extent=length;
236 blob_info->quantum=(size_t) MagickMaxBlobExtent;
238 blob_info->type=BlobStream;
239 blob_info->file_info.file=(FILE *) NULL;
240 blob_info->data=(
unsigned char *) blob;
241 blob_info->mapped=MagickFalse;
275 MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
287 assert(filename != (
const char *) NULL);
288 assert(blob != (
const void *) NULL);
289 if (IsEventLogging() != MagickFalse)
290 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
291 if (*filename ==
'\0')
292 file=AcquireUniqueFileResource(filename);
294 file=open_utf8(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY,P_MODE);
297 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
300 for (i=0; i < length; i+=(size_t) count)
302 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
303 MagickMaxBufferExtent));
311 file=close_utf8(file);
312 if ((file == -1) || (i < length))
314 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
351 MagickExport
Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
367 assert(image_info != (
ImageInfo *) NULL);
368 assert(image_info->signature == MagickCoreSignature);
370 if (IsEventLogging() != MagickFalse)
371 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
372 image_info->filename);
373 if ((blob == (
const void *) NULL) || (length == 0))
375 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
376 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
377 return((
Image *) NULL);
379 blob_info=CloneImageInfo(image_info);
380 blob_info->blob=(
void *) blob;
381 blob_info->length=length;
382 if (*blob_info->magick ==
'\0')
383 (void) SetImageInfo(blob_info,0,exception);
384 magick_info=GetMagickInfo(blob_info->magick,exception);
387 (void) ThrowMagickException(exception,GetMagickModule(),
388 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
390 blob_info=DestroyImageInfo(blob_info);
391 return((
Image *) NULL);
393 if (GetMagickBlobSupport(magick_info) != MagickFalse)
396 filename[MagickPathExtent];
401 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
402 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,
"%s:%s",
403 blob_info->magick,filename);
404 image=ReadImage(blob_info,exception);
405 if (image != (
Image *) NULL)
406 (void) DetachBlob(image->blob);
407 blob_info=DestroyImageInfo(blob_info);
413 blob_info->blob=(
void *) NULL;
415 *blob_info->filename=
'\0';
416 status=BlobToFile(blob_info->filename,blob,length,exception);
417 if (status == MagickFalse)
419 (void) RelinquishUniqueFileResource(blob_info->filename);
420 blob_info=DestroyImageInfo(blob_info);
421 return((
Image *) NULL);
423 clone_info=CloneImageInfo(blob_info);
424 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
425 blob_info->magick,blob_info->filename);
426 image=ReadImage(clone_info,exception);
427 if (image != (
Image *) NULL)
435 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
437 (void) CopyMagickString(images->filename,image_info->filename,
439 (void) CopyMagickString(images->magick_filename,image_info->filename,
441 (void) CopyMagickString(images->magick,magick_info->name,
443 images=GetNextImageInList(images);
446 clone_info=DestroyImageInfo(clone_info);
447 (void) RelinquishUniqueFileResource(blob_info->filename);
448 blob_info=DestroyImageInfo(blob_info);
483 clone_info=(
BlobInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
484 GetBlobInfo(clone_info);
487 semaphore=clone_info->semaphore;
488 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
489 if (blob_info->mapped != MagickFalse)
490 (void) AcquireMagickResource(MapResource,blob_info->length);
491 clone_info->semaphore=semaphore;
492 LockSemaphoreInfo(clone_info->semaphore);
493 clone_info->reference_count=1;
494 UnlockSemaphoreInfo(clone_info->semaphore);
521 static inline void ThrowBlobException(
BlobInfo *blob_info)
523 if ((blob_info->status == 0) && (errno != 0))
524 blob_info->error_number=errno;
525 blob_info->status=(-1);
528 MagickExport MagickBooleanType CloseBlob(
Image *image)
531 *magick_restrict blob_info;
539 assert(image != (
Image *) NULL);
540 assert(image->signature == MagickCoreSignature);
541 if (IsEventLogging() != MagickFalse)
542 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
543 blob_info=image->blob;
544 if ((blob_info == (
BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
546 (void) SyncBlob(image);
547 status=blob_info->status;
548 switch (blob_info->type)
550 case UndefinedStream:
556 if (blob_info->synchronize != MagickFalse)
558 status=fflush(blob_info->file_info.file);
560 ThrowBlobException(blob_info);
561 status=fsync(fileno(blob_info->file_info.file));
563 ThrowBlobException(blob_info);
565 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
566 ThrowBlobException(blob_info);
571 #if defined(MAGICKCORE_ZLIB_DELEGATE) 573 (void) gzerror(blob_info->file_info.gzfile,&status);
575 ThrowBlobException(blob_info);
581 #if defined(MAGICKCORE_BZLIB_DELEGATE) 583 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
585 ThrowBlobException(blob_info);
593 if (blob_info->file_info.file != (FILE *) NULL)
595 if (blob_info->synchronize != MagickFalse)
597 status=fflush(blob_info->file_info.file);
599 ThrowBlobException(blob_info);
600 status=fsync(fileno(blob_info->file_info.file));
602 ThrowBlobException(blob_info);
604 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
605 ThrowBlobException(blob_info);
610 blob_info->size=GetBlobSize(image);
611 image->extent=blob_info->size;
612 blob_info->eof=MagickFalse;
614 blob_info->mode=UndefinedBlobMode;
615 if (blob_info->exempt != MagickFalse)
617 blob_info->type=UndefinedStream;
618 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
620 switch (blob_info->type)
622 case UndefinedStream:
627 if (blob_info->file_info.file != (FILE *) NULL)
629 status=fclose(blob_info->file_info.file);
631 ThrowBlobException(blob_info);
637 #if defined(MAGICKCORE_HAVE_PCLOSE) 638 status=pclose(blob_info->file_info.file);
640 ThrowBlobException(blob_info);
646 #if defined(MAGICKCORE_ZLIB_DELEGATE) 647 status=gzclose(blob_info->file_info.gzfile);
649 ThrowBlobException(blob_info);
655 #if defined(MAGICKCORE_BZLIB_DELEGATE) 656 BZ2_bzclose(blob_info->file_info.bzfile);
664 if (blob_info->file_info.file != (FILE *) NULL)
666 status=fclose(blob_info->file_info.file);
668 ThrowBlobException(blob_info);
673 (void) DetachBlob(blob_info);
674 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
699 MagickExport
void DestroyBlob(
Image *image)
702 *magick_restrict blob_info;
707 assert(image != (
Image *) NULL);
708 assert(image->signature == MagickCoreSignature);
709 assert(image->blob != (
BlobInfo *) NULL);
710 assert(image->blob->signature == MagickCoreSignature);
711 if (IsEventLogging() != MagickFalse)
712 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
713 blob_info=image->blob;
715 LockSemaphoreInfo(blob_info->semaphore);
716 blob_info->reference_count--;
717 assert(blob_info->reference_count >= 0);
718 if (blob_info->reference_count == 0)
720 UnlockSemaphoreInfo(blob_info->semaphore);
721 if (destroy == MagickFalse)
726 (void) CloseBlob(image);
727 if (blob_info->mapped != MagickFalse)
729 (void) UnmapBlob(blob_info->data,blob_info->length);
730 RelinquishMagickResource(MapResource,blob_info->length);
733 DestroySemaphoreInfo(&blob_info->semaphore);
734 blob_info->signature=(~MagickCoreSignature);
735 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
760 MagickExport
unsigned char *DetachBlob(
BlobInfo *blob_info)
765 assert(blob_info != (
BlobInfo *) NULL);
766 if (IsEventLogging() != MagickFalse)
767 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
768 if (blob_info->mapped != MagickFalse)
770 (void) UnmapBlob(blob_info->data,blob_info->length);
771 blob_info->data=NULL;
772 RelinquishMagickResource(MapResource,blob_info->length);
774 blob_info->mapped=MagickFalse;
777 blob_info->mode=UndefinedBlobMode;
778 blob_info->eof=MagickFalse;
780 blob_info->exempt=MagickFalse;
781 blob_info->type=UndefinedStream;
782 blob_info->file_info.file=(FILE *) NULL;
783 data=blob_info->data;
784 blob_info->data=(
unsigned char *) NULL;
785 blob_info->stream=(StreamHandler) NULL;
813 MagickPrivate
void DisassociateBlob(
Image *image)
816 *magick_restrict blob_info,
822 assert(image != (
Image *) NULL);
823 assert(image->signature == MagickCoreSignature);
824 assert(image->blob != (
BlobInfo *) NULL);
825 assert(image->blob->signature == MagickCoreSignature);
826 if (IsEventLogging() != MagickFalse)
827 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
828 blob_info=image->blob;
830 LockSemaphoreInfo(blob_info->semaphore);
831 assert(blob_info->reference_count >= 0);
832 if (blob_info->reference_count > 1)
834 UnlockSemaphoreInfo(blob_info->semaphore);
835 if (clone == MagickFalse)
837 clone_info=CloneBlobInfo(blob_info);
839 image->blob=clone_info;
867 MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
868 const MagickSizeType length)
880 buffer[MagickMinBufferExtent >> 1];
882 assert(image != (
Image *) NULL);
883 assert(image->signature == MagickCoreSignature);
884 if (length != (MagickSizeType) ((MagickOffsetType) length))
887 for (i=0; i < length; i+=(MagickSizeType) count)
889 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
890 (void) ReadBlobStream(image,quantum,buffer,&count);
898 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
925 MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
927 assert(image != (
Image *) NULL);
928 assert(image->signature == MagickCoreSignature);
929 assert(duplicate != (
Image *) NULL);
930 assert(duplicate->signature == MagickCoreSignature);
931 if (IsEventLogging() != MagickFalse)
932 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
934 image->blob=ReferenceBlob(duplicate->blob);
960 MagickExport
int EOFBlob(
const Image *image)
963 *magick_restrict blob_info;
965 assert(image != (
Image *) NULL);
966 assert(image->signature == MagickCoreSignature);
967 assert(image->blob != (
BlobInfo *) NULL);
968 assert(image->blob->type != UndefinedStream);
969 if (IsEventLogging() != MagickFalse)
970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
971 blob_info=image->blob;
972 switch (blob_info->type)
974 case UndefinedStream:
980 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
986 #if defined(MAGICKCORE_ZLIB_DELEGATE) 987 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
994 #if defined(MAGICKCORE_BZLIB_DELEGATE) 999 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1000 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
1006 blob_info->eof=MagickFalse;
1012 return((
int) blob_info->eof);
1038 MagickExport
int ErrorBlob(
const Image *image)
1041 *magick_restrict blob_info;
1043 assert(image != (
Image *) NULL);
1044 assert(image->signature == MagickCoreSignature);
1045 assert(image->blob != (
BlobInfo *) NULL);
1046 assert(image->blob->type != UndefinedStream);
1047 if (IsEventLogging() != MagickFalse)
1048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1049 blob_info=image->blob;
1050 switch (blob_info->type)
1052 case UndefinedStream:
1053 case StandardStream:
1058 blob_info->error=ferror(blob_info->file_info.file);
1063 #if defined(MAGICKCORE_ZLIB_DELEGATE) 1064 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1070 #if defined(MAGICKCORE_BZLIB_DELEGATE) 1071 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1083 return(blob_info->error);
1121 MagickExport
unsigned char *FileToBlob(
const char *filename,
const size_t extent,
1148 assert(filename != (
const char *) NULL);
1150 assert(exception->signature == MagickCoreSignature);
1151 if (IsEventLogging() != MagickFalse)
1152 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1154 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1155 ThrowPolicyException(filename,(
unsigned char *) NULL);
1157 if (LocaleCompare(filename,
"-") != 0)
1160 flags = O_RDONLY | O_BINARY;
1162 status=GetPathAttributes(filename,&attributes);
1163 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1165 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1168 file=open_utf8(filename,flags,0);
1172 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1173 return((
unsigned char *) NULL);
1175 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1177 file=close_utf8(file)-1;
1178 ThrowPolicyException(filename,(
unsigned char *) NULL);
1180 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1182 if ((file == fileno(stdin)) || (offset < 0) ||
1183 (offset != (MagickOffsetType) ((ssize_t) offset)))
1194 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1195 quantum=(size_t) MagickMaxBufferExtent;
1196 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1197 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1198 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1199 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1201 count=read(file,blob+i,quantum);
1208 if (~((
size_t) i) < (count+quantum+1))
1210 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1213 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+count+quantum+1,
1215 if ((
size_t) (i+count) >= extent)
1218 if (LocaleCompare(filename,
"-") != 0)
1219 file=close_utf8(file);
1220 if (blob == (
unsigned char *) NULL)
1222 (void) ThrowMagickException(exception,GetMagickModule(),
1223 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1224 return((
unsigned char *) NULL);
1228 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1229 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1230 return((
unsigned char *) NULL);
1232 *length=(size_t) MagickMin(i+count,extent);
1236 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1237 MagickMin(extent,(
size_t) MAGICK_SSIZE_MAX));
1238 blob=(
unsigned char *) NULL;
1239 if (~(*length) >= (MagickPathExtent-1))
1240 blob=(
unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1242 if (blob == (
unsigned char *) NULL)
1244 file=close_utf8(file);
1245 (void) ThrowMagickException(exception,GetMagickModule(),
1246 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1247 return((
unsigned char *) NULL);
1249 map=MapBlob(file,ReadMode,0,*length);
1250 if (map != (
unsigned char *) NULL)
1252 (void) memcpy(blob,map,*length);
1253 (void) UnmapBlob(map,*length);
1257 (void) lseek(file,0,SEEK_SET);
1258 for (i=0; i < *length; i+=count)
1260 count=read(file,blob+i,(
size_t) MagickMin(*length-i,(
size_t)
1261 MagickMaxBufferExtent));
1271 file=close_utf8(file)-1;
1272 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1273 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1274 return((
unsigned char *) NULL);
1278 if (LocaleCompare(filename,
"-") != 0)
1279 file=close_utf8(file);
1282 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1283 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1313 static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1314 const unsigned char *magick_restrict data)
1317 *magick_restrict blob_info;
1325 assert(image->blob != (
BlobInfo *) NULL);
1326 assert(image->blob->type != UndefinedStream);
1327 assert(data != (
void *) NULL);
1328 blob_info=image->blob;
1329 if (blob_info->type != BlobStream)
1330 return(WriteBlob(image,length,data));
1331 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1336 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1337 if (extent >= blob_info->extent)
1339 extent+=blob_info->quantum+length;
1340 blob_info->quantum<<=1;
1341 if (SetBlobExtent(image,extent) == MagickFalse)
1344 q=blob_info->data+blob_info->offset;
1345 (void) memcpy(q,data,length);
1346 blob_info->offset+=length;
1347 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1348 blob_info->length=(
size_t) blob_info->offset;
1349 return((ssize_t) length);
1352 MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename)
1373 assert(image != (
const Image *) NULL);
1374 assert(image->signature == MagickCoreSignature);
1375 assert(filename != (
const char *) NULL);
1376 if (IsEventLogging() != MagickFalse)
1377 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1378 exception=(&image->exception);
1379 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1380 ThrowPolicyException(filename,MagickFalse);
1382 if (LocaleCompare(filename,
"-") != 0)
1385 flags = O_RDONLY | O_BINARY;
1387 file=open_utf8(filename,flags,0);
1391 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
1392 return(MagickFalse);
1394 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1395 ThrowPolicyException(filename,MagickFalse);
1396 quantum=(size_t) MagickMaxBufferExtent;
1397 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1398 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1399 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1400 if (blob == (
unsigned char *) NULL)
1402 file=close_utf8(file);
1403 ThrowFileException(exception,ResourceLimitError,
"MemoryAllocationFailed",
1405 return(MagickFalse);
1409 count=read(file,blob,quantum);
1416 length=(size_t) count;
1417 count=WriteBlobStream(image,length,blob);
1418 if (count != (ssize_t) length)
1420 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1424 file=close_utf8(file);
1426 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1427 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1454 MagickExport MagickBooleanType GetBlobError(
const Image *image)
1456 assert(image != (
const Image *) NULL);
1457 assert(image->signature == MagickCoreSignature);
1458 if (IsEventLogging() != MagickFalse)
1459 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1460 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1461 errno=image->blob->error_number;
1462 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1487 MagickExport FILE *GetBlobFileHandle(
const Image *image)
1489 assert(image != (
const Image *) NULL);
1490 assert(image->signature == MagickCoreSignature);
1491 return(image->blob->file_info.file);
1516 MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1518 assert(blob_info != (
BlobInfo *) NULL);
1519 (void) memset(blob_info,0,
sizeof(*blob_info));
1520 blob_info->type=UndefinedStream;
1521 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1522 blob_info->properties.st_mtime=GetMagickTime();
1523 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1524 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1525 blob_info->reference_count=1;
1526 blob_info->semaphore=AllocateSemaphoreInfo();
1527 blob_info->signature=MagickCoreSignature;
1552 MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1554 assert(image != (
Image *) NULL);
1555 assert(image->signature == MagickCoreSignature);
1556 if (IsEventLogging() != MagickFalse)
1557 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1558 return(&image->blob->properties);
1584 MagickExport MagickSizeType GetBlobSize(
const Image *image)
1587 *magick_restrict blob_info;
1592 assert(image != (
Image *) NULL);
1593 assert(image->signature == MagickCoreSignature);
1594 assert(image->blob != (
BlobInfo *) NULL);
1595 if (IsEventLogging() != MagickFalse)
1596 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1597 blob_info=image->blob;
1599 switch (blob_info->type)
1601 case UndefinedStream:
1602 case StandardStream:
1604 extent=blob_info->size;
1612 extent=(MagickSizeType) blob_info->properties.st_size;
1614 extent=blob_info->size;
1615 file_descriptor=fileno(blob_info->file_info.file);
1616 if (file_descriptor == -1)
1618 if (fstat(file_descriptor,&blob_info->properties) == 0)
1619 extent=(MagickSizeType) blob_info->properties.st_size;
1624 extent=blob_info->size;
1633 status=GetPathAttributes(image->filename,&blob_info->properties);
1634 if (status != MagickFalse)
1635 extent=(MagickSizeType) blob_info->properties.st_size;
1642 extent=(MagickSizeType) blob_info->length;
1671 MagickExport
unsigned char *GetBlobStreamData(
const Image *image)
1673 assert(image != (
const Image *) NULL);
1674 assert(image->signature == MagickCoreSignature);
1675 return(image->blob->data);
1700 MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1702 assert(image != (
const Image *) NULL);
1703 assert(image->signature == MagickCoreSignature);
1704 if (IsEventLogging() != MagickFalse)
1705 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1706 return(image->blob->stream);
1742 MagickExport
unsigned char *ImageToBlob(
const ImageInfo *image_info,
1757 assert(image_info != (
const ImageInfo *) NULL);
1758 assert(image_info->signature == MagickCoreSignature);
1759 assert(image != (
Image *) NULL);
1760 assert(image->signature == MagickCoreSignature);
1762 assert(exception->signature == MagickCoreSignature);
1763 if (IsEventLogging() != MagickFalse)
1764 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1765 image_info->filename);
1767 blob=(
unsigned char *) NULL;
1768 blob_info=CloneImageInfo(image_info);
1769 blob_info->adjoin=MagickFalse;
1770 (void) SetImageInfo(blob_info,1,exception);
1771 if (*blob_info->magick !=
'\0')
1772 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
1773 magick_info=GetMagickInfo(image->magick,exception);
1774 if (magick_info == (
const MagickInfo *) NULL)
1776 (void) ThrowMagickException(exception,GetMagickModule(),
1777 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
1779 blob_info=DestroyImageInfo(blob_info);
1782 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
1783 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1788 blob_info->length=0;
1789 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1790 sizeof(
unsigned char));
1791 if (blob_info->blob == NULL)
1792 (void) ThrowMagickException(exception,GetMagickModule(),
1793 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1796 (void) CloseBlob(image);
1797 image->blob->exempt=MagickTrue;
1798 *image->filename=
'\0';
1799 status=WriteImage(blob_info,image);
1800 InheritException(exception,&image->exception);
1801 *length=image->blob->length;
1802 blob=DetachBlob(image->blob);
1803 if (blob != (
void *) NULL)
1805 if (status == MagickFalse)
1806 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1808 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
1809 sizeof(
unsigned char));
1811 else if (status == MagickFalse)
1812 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
1818 unique[MagickPathExtent];
1826 file=AcquireUniqueFileResource(unique);
1829 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
1830 image_info->filename);
1834 blob_info->file=fdopen(file,
"wb");
1835 if (blob_info->file != (FILE *) NULL)
1837 (void) FormatLocaleString(image->filename,MagickPathExtent,
1838 "%s:%s",image->magick,unique);
1839 status=WriteImage(blob_info,image);
1840 (void) fclose(blob_info->file);
1841 if (status == MagickFalse)
1842 InheritException(exception,&image->exception);
1844 blob=FileToBlob(unique,SIZE_MAX,length,exception);
1846 (void) RelinquishUniqueFileResource(unique);
1849 blob_info=DestroyImageInfo(blob_info);
1881 MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
1906 assert(image != (
Image *) NULL);
1907 assert(image->signature == MagickCoreSignature);
1908 assert(image->blob != (
BlobInfo *) NULL);
1909 assert(image->blob->type != UndefinedStream);
1910 assert(filename != (
const char *) NULL);
1911 if (IsEventLogging() != MagickFalse)
1912 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1913 if (*filename ==
'\0')
1914 file=AcquireUniqueFileResource(filename);
1916 if (LocaleCompare(filename,
"-") == 0)
1917 file=fileno(stdout);
1919 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,P_MODE);
1922 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1923 return(MagickFalse);
1925 quantum=(size_t) MagickMaxBufferExtent;
1926 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1927 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1928 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
1929 if (buffer == (
unsigned char *) NULL)
1931 file=close_utf8(file)-1;
1932 (void) ThrowMagickException(exception,GetMagickModule(),
1933 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
1934 return(MagickFalse);
1937 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1938 for (i=0; count > 0; )
1940 length=(size_t) count;
1941 for (i=0; i < length; i+=count)
1943 count=write(file,p+i,(
size_t) (length-i));
1953 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1955 if (LocaleCompare(filename,
"-") != 0)
1956 file=close_utf8(file);
1957 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
1958 if ((file == -1) || (i < length))
1961 file=close_utf8(file);
1962 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1963 return(MagickFalse);
2003 MagickExport
unsigned char *ImagesToBlob(
const ImageInfo *image_info,
2018 assert(image_info != (
const ImageInfo *) NULL);
2019 assert(image_info->signature == MagickCoreSignature);
2020 assert(images != (
Image *) NULL);
2021 assert(images->signature == MagickCoreSignature);
2023 if (IsEventLogging() != MagickFalse)
2024 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2025 image_info->filename);
2027 blob=(
unsigned char *) NULL;
2028 blob_info=CloneImageInfo(image_info);
2029 (void) SetImageInfo(blob_info,(
unsigned int) GetImageListLength(images),
2031 if (*blob_info->magick !=
'\0')
2032 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2033 magick_info=GetMagickInfo(images->magick,exception);
2034 if (magick_info == (
const MagickInfo *) NULL)
2036 (void) ThrowMagickException(exception,GetMagickModule(),
2037 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
"`%s'",
2039 blob_info=DestroyImageInfo(blob_info);
2042 if (GetMagickAdjoin(magick_info) == MagickFalse)
2044 blob_info=DestroyImageInfo(blob_info);
2045 return(ImageToBlob(image_info,images,length,exception));
2047 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2048 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2053 blob_info->length=0;
2054 blob_info->blob=(
void *) AcquireQuantumMemory(MagickMaxBlobExtent,
2055 sizeof(
unsigned char));
2056 if (blob_info->blob == (
void *) NULL)
2057 (
void) ThrowMagickException(exception,GetMagickModule(),
2058 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2061 (void) CloseBlob(images);
2062 images->blob->exempt=MagickTrue;
2063 *images->filename=
'\0';
2064 status=WriteImages(blob_info,images,images->filename,exception);
2065 *length=images->blob->length;
2066 blob=DetachBlob(images->blob);
2067 if (blob != (
void *) NULL)
2069 if (status == MagickFalse)
2070 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2072 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
2073 sizeof(
unsigned char));
2076 if (status == MagickFalse)
2077 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2083 filename[MagickPathExtent],
2084 unique[MagickPathExtent];
2092 file=AcquireUniqueFileResource(unique);
2095 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2096 image_info->filename);
2100 blob_info->file=fdopen(file,
"wb");
2101 if (blob_info->file != (FILE *) NULL)
2103 (void) FormatLocaleString(filename,MagickPathExtent,
"%s:%s",
2104 images->magick,unique);
2105 status=WriteImages(blob_info,images,filename,exception);
2106 (void) fclose(blob_info->file);
2107 if (status == MagickFalse)
2108 InheritException(exception,&images->exception);
2110 blob=FileToBlob(unique,SIZE_MAX,length,exception);
2112 (void) RelinquishUniqueFileResource(unique);
2115 blob_info=DestroyImageInfo(blob_info);
2151 MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2155 filename[MagickPathExtent];
2184 assert(image_info != (
ImageInfo *) NULL);
2185 assert(image_info->signature == MagickCoreSignature);
2186 assert(image != (
Image *) NULL);
2187 assert(image->signature == MagickCoreSignature);
2188 assert(inject_image != (
Image *) NULL);
2189 assert(inject_image->signature == MagickCoreSignature);
2191 if (IsEventLogging() != MagickFalse)
2192 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2193 unique_file=(FILE *) NULL;
2194 file=AcquireUniqueFileResource(filename);
2196 unique_file=fdopen(file,
"wb");
2197 if ((file == -1) || (unique_file == (FILE *) NULL))
2199 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2200 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2202 return(MagickFalse);
2204 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2205 if (byte_image == (
Image *) NULL)
2207 (void) fclose(unique_file);
2208 (void) RelinquishUniqueFileResource(filename);
2209 return(MagickFalse);
2211 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,
"%s:%s",
2213 DestroyBlob(byte_image);
2214 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2215 write_info=CloneImageInfo(image_info);
2216 SetImageInfoFile(write_info,unique_file);
2217 status=WriteImage(write_info,byte_image);
2218 write_info=DestroyImageInfo(write_info);
2219 byte_image=DestroyImage(byte_image);
2220 (void) fclose(unique_file);
2221 if (status == MagickFalse)
2223 (void) RelinquishUniqueFileResource(filename);
2224 return(MagickFalse);
2229 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2232 (void) RelinquishUniqueFileResource(filename);
2233 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2234 image_info->filename);
2235 return(MagickFalse);
2237 quantum=(size_t) MagickMaxBufferExtent;
2238 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2239 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2240 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2241 if (buffer == (
unsigned char *) NULL)
2243 (void) RelinquishUniqueFileResource(filename);
2244 file=close_utf8(file);
2245 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2250 ssize_t count = read(file,buffer,quantum);
2257 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2260 file=close_utf8(file);
2262 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2263 (void) RelinquishUniqueFileResource(filename);
2264 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2290 MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2292 assert(image != (
const Image *) NULL);
2293 assert(image->signature == MagickCoreSignature);
2294 if (IsEventLogging() != MagickFalse)
2295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2296 return(image->blob->exempt);
2321 MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2324 *magick_restrict blob_info;
2326 assert(image != (
const Image *) NULL);
2327 assert(image->signature == MagickCoreSignature);
2328 if (IsEventLogging() != MagickFalse)
2329 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2330 blob_info=image->blob;
2331 switch (blob_info->type)
2340 if (blob_info->file_info.file == (FILE *) NULL)
2341 return(MagickFalse);
2342 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2343 return(status == -1 ? MagickFalse : MagickTrue);
2347 #if defined(MAGICKCORE_ZLIB_DELEGATE) 2351 if (blob_info->file_info.gzfile == (gzFile) NULL)
2352 return(MagickFalse);
2353 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2354 return(offset < 0 ? MagickFalse : MagickTrue);
2359 case UndefinedStream:
2363 case StandardStream:
2368 return(MagickFalse);
2393 MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2395 assert(image != (
const Image *) NULL);
2396 assert(image->signature == MagickCoreSignature);
2397 if (IsEventLogging() != MagickFalse)
2398 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2399 return(image->blob->temporary);
2431 MagickExport
unsigned char *MapBlob(
int file,
const MapMode mode,
2432 const MagickOffsetType offset,
const size_t length)
2434 #if defined(MAGICKCORE_HAVE_MMAP) 2447 #if defined(MAP_ANONYMOUS) 2448 flags|=MAP_ANONYMOUS;
2450 return((
unsigned char *) NULL);
2457 protection=PROT_READ;
2463 protection=PROT_WRITE;
2469 protection=PROT_READ | PROT_WRITE;
2474 #if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB) 2475 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,offset);
2477 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags |
2478 MAP_HUGETLB,file,offset);
2479 if (map == (
unsigned char *) MAP_FAILED)
2480 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,
2483 if (map == (
unsigned char *) MAP_FAILED)
2484 return((
unsigned char *) NULL);
2491 return((
unsigned char *) NULL);
2520 MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
2529 assert(buffer != (
unsigned char *) NULL);
2536 *buffer++=(
unsigned char) c;
2540 *buffer++=(
unsigned char) c;
2570 MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
2578 assert(p != (
unsigned char *) NULL);
2585 *p++=(
unsigned char) c;
2621 static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
2633 size=MagickMinBufferExtent;
2634 option=GetImageOption(image_info,
"stream:buffer-size");
2635 if (option != (
const char *) NULL)
2636 size=StringToUnsignedLong(option);
2637 status=setvbuf(image->blob->file_info.file,(
char *) NULL,size == 0 ?
2638 _IONBF : _IOFBF,size);
2639 return(status == 0 ? MagickTrue : MagickFalse);
2642 #if defined(MAGICKCORE_ZLIB_DELEGATE) 2643 static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
2645 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) 2646 return(gzopen(path,mode));
2654 path_wide=NTCreateWidePath(path);
2655 if (path_wide == (
wchar_t *) NULL)
2656 return((gzFile) NULL);
2657 file=gzopen_w(path_wide,mode);
2658 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
2664 MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
2668 *magick_restrict blob_info;
2671 extension[MagickPathExtent],
2672 filename[MagickPathExtent];
2686 assert(image_info != (
ImageInfo *) NULL);
2687 assert(image_info->signature == MagickCoreSignature);
2688 assert(image != (
Image *) NULL);
2689 assert(image->signature == MagickCoreSignature);
2690 if (IsEventLogging() != MagickFalse)
2691 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2692 image_info->filename);
2693 blob_info=image->blob;
2694 if (image_info->blob != (
void *) NULL)
2696 if (image_info->stream != (StreamHandler) NULL)
2697 blob_info->stream=(StreamHandler) image_info->stream;
2698 AttachBlob(blob_info,image_info->blob,image_info->length);
2701 (void) DetachBlob(blob_info);
2702 blob_info->mode=mode;
2711 case ReadBinaryBlobMode:
2713 flags=O_RDONLY | O_BINARY;
2719 flags=O_WRONLY | O_CREAT | O_TRUNC;
2723 case WriteBinaryBlobMode:
2725 flags=O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
2729 case AppendBlobMode:
2731 flags=O_WRONLY | O_CREAT | O_APPEND;
2735 case AppendBinaryBlobMode:
2737 flags=O_RDWR | O_CREAT | O_APPEND | O_BINARY;
2749 blob_info->synchronize=image_info->synchronize;
2750 if (image_info->stream != (StreamHandler) NULL)
2752 blob_info->stream=(StreamHandler) image_info->stream;
2755 blob_info->type=FifoStream;
2763 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2764 rights=ReadPolicyRights;
2766 rights=WritePolicyRights;
2767 if (IsPathAuthorized(rights,filename) == MagickFalse)
2768 ThrowPolicyException(filename,MagickFalse);
2769 if ((LocaleCompare(filename,
"-") == 0) ||
2770 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
2772 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
2773 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__) 2774 if (strchr(type,
'b') != (
char *) NULL)
2775 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2777 blob_info->type=StandardStream;
2778 blob_info->exempt=MagickTrue;
2779 return(SetStreamBuffering(image_info,image));
2781 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
2782 (IsGeometry(filename+3) != MagickFalse))
2785 fileMode[MagickPathExtent];
2789 blob_info->file_info.file=fdopen(StringToLong(filename+3),fileMode);
2790 if (blob_info->file_info.file == (FILE *) NULL)
2792 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2793 return(MagickFalse);
2795 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__) 2796 if (strchr(type,
'b') != (
char *) NULL)
2797 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
2799 blob_info->type=FileStream;
2800 blob_info->exempt=MagickTrue;
2801 return(SetStreamBuffering(image_info,image));
2803 #if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT) 2804 if (*filename ==
'|')
2807 fileMode[MagickPathExtent],
2813 #if defined(SIGPIPE) 2815 (void) signal(SIGPIPE,SIG_IGN);
2819 sanitize_command=SanitizeString(filename+1);
2820 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2822 sanitize_command=DestroyString(sanitize_command);
2823 if (blob_info->file_info.file == (FILE *) NULL)
2825 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2826 return(MagickFalse);
2828 blob_info->type=PipeStream;
2829 blob_info->exempt=MagickTrue;
2830 return(SetStreamBuffering(image_info,image));
2833 status=GetPathAttributes(filename,&blob_info->properties);
2834 #if defined(S_ISFIFO) 2835 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2837 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2838 if (blob_info->file_info.file == (FILE *) NULL)
2840 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2841 return(MagickFalse);
2843 blob_info->type=FileStream;
2844 blob_info->exempt=MagickTrue;
2845 return(SetStreamBuffering(image_info,image));
2848 GetPathComponent(image->filename,ExtensionPath,extension);
2851 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
2852 if ((image_info->adjoin == MagickFalse) ||
2853 (strchr(filename,
'%') != (
char *) NULL))
2858 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
2859 image->scene,filename);
2860 if ((LocaleCompare(filename,image->filename) == 0) &&
2861 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
2862 (GetNextImageInList(image) != (
Image *) NULL)))
2865 path[MagickPathExtent];
2867 GetPathComponent(image->filename,RootPath,path);
2868 if (*extension ==
'\0')
2869 (void) FormatLocaleString(filename,MagickPathExtent,
"%s-%.20g",
2870 path,(
double) image->scene);
2872 (
void) FormatLocaleString(filename,MagickPathExtent,
2873 "%s-%.20g.%s",path,(
double) image->scene,extension);
2875 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2876 #if defined(macintosh) 2877 SetApplicationType(filename,image_info->magick,
'8BIM');
2880 if (IsPathAuthorized(rights,filename) == MagickFalse)
2881 ThrowPolicyException(filename,MagickFalse);
2883 if (image_info->file != (FILE *) NULL)
2885 blob_info->file_info.file=image_info->file;
2886 blob_info->type=FileStream;
2887 blob_info->exempt=MagickTrue;
2895 blob_info->file_info.file=(FILE *) NULL;
2896 file=open_utf8(filename,flags,0);
2898 blob_info->file_info.file=fdopen(file,type);
2899 if (blob_info->file_info.file != (FILE *) NULL)
2907 blob_info->type=FileStream;
2908 (void) fstat(fileno(blob_info->file_info.file),
2909 &blob_info->properties);
2910 (void) SetStreamBuffering(image_info,image);
2911 (void) memset(magick,0,
sizeof(magick));
2912 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2913 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2914 #if defined(MAGICKCORE_POSIX_SUPPORT) 2915 (void) fflush(blob_info->file_info.file);
2917 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2918 " read %.20g magic header bytes",(double) count);
2919 #if defined(MAGICKCORE_ZLIB_DELEGATE) 2920 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2921 ((
int) magick[2] == 0x08))
2924 gzfile = gzopen_utf8(filename,
"rb");
2926 if (gzfile != (gzFile) NULL)
2928 if (blob_info->file_info.file != (FILE *) NULL)
2929 (void) fclose(blob_info->file_info.file);
2930 blob_info->file_info.file=(FILE *) NULL;
2931 blob_info->file_info.gzfile=gzfile;
2932 blob_info->type=ZipStream;
2936 #if defined(MAGICKCORE_BZLIB_DELEGATE) 2937 if (strncmp((
char *) magick,
"BZh",3) == 0)
2940 *bzfile = BZ2_bzopen(filename,
"r");
2942 if (bzfile != (BZFILE *) NULL)
2944 if (blob_info->file_info.file != (FILE *) NULL)
2945 (void) fclose(blob_info->file_info.file);
2946 blob_info->file_info.file=(FILE *) NULL;
2947 blob_info->file_info.bzfile=bzfile;
2948 blob_info->type=BZipStream;
2952 if (blob_info->type == FileStream)
2963 sans_exception=AcquireExceptionInfo();
2964 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2965 sans_exception=DestroyExceptionInfo(sans_exception);
2966 length=(size_t) blob_info->properties.st_size;
2967 if ((magick_info != (
const MagickInfo *) NULL) &&
2968 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2969 (AcquireMagickResource(MapResource,length) != MagickFalse))
2974 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
2976 if (blob == (
void *) NULL)
2977 RelinquishMagickResource(MapResource,length);
2983 if (image_info->file != (FILE *) NULL)
2984 blob_info->exempt=MagickFalse;
2987 (void) fclose(blob_info->file_info.file);
2988 blob_info->file_info.file=(FILE *) NULL;
2990 AttachBlob(blob_info,blob,length);
2991 blob_info->mapped=MagickTrue;
2998 #if defined(MAGICKCORE_ZLIB_DELEGATE) 2999 if ((LocaleCompare(extension,
"gz") == 0) ||
3000 (LocaleCompare(extension,
"wmz") == 0) ||
3001 (LocaleCompare(extension,
"svgz") == 0))
3003 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
3004 if (blob_info->file_info.gzfile != (gzFile) NULL)
3005 blob_info->type=ZipStream;
3009 #if defined(MAGICKCORE_BZLIB_DELEGATE) 3010 if (LocaleCompare(extension,
"bz2") == 0)
3012 if (mode == WriteBinaryBlobMode)
3014 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
3015 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3016 blob_info->type=BZipStream;
3024 blob_info->file_info.file=(FILE *) NULL;
3025 file=open_utf8(filename,flags,P_MODE);
3027 blob_info->file_info.file=fdopen(file,type);
3028 if (blob_info->file_info.file != (FILE *) NULL)
3030 blob_info->type=FileStream;
3031 (void) SetStreamBuffering(image_info,image);
3034 if (IsPathAuthorized(rights,filename) == MagickFalse)
3035 ThrowPolicyException(filename,MagickFalse);
3036 blob_info->status=0;
3037 blob_info->error_number=0;
3038 if (blob_info->type != UndefinedStream)
3039 blob_info->size=GetBlobSize(image);
3042 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
3043 return(MagickFalse);
3082 #if defined(__cplusplus) || defined(c_plusplus) 3086 static size_t PingStream(
const Image *magick_unused(image),
3087 const void *magick_unused(pixels),
const size_t columns)
3089 magick_unreferenced(image);
3090 magick_unreferenced(pixels);
3095 #if defined(__cplusplus) || defined(c_plusplus) 3099 MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3115 assert(image_info != (
ImageInfo *) NULL);
3116 assert(image_info->signature == MagickCoreSignature);
3118 if (IsEventLogging() != MagickFalse)
3119 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3120 image_info->filename);
3121 if ((blob == (
const void *) NULL) || (length == 0))
3123 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3124 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3125 return((
Image *) NULL);
3127 ping_info=CloneImageInfo(image_info);
3128 ping_info->blob=(
void *) blob;
3129 ping_info->length=length;
3130 ping_info->ping=MagickTrue;
3131 if (*ping_info->magick ==
'\0')
3132 (void) SetImageInfo(ping_info,0,exception);
3133 magick_info=GetMagickInfo(ping_info->magick,exception);
3134 if (magick_info == (
const MagickInfo *) NULL)
3136 (void) ThrowMagickException(exception,GetMagickModule(),
3137 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3139 ping_info=DestroyImageInfo(ping_info);
3140 return((
Image *) NULL);
3142 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3145 filename[MagickPathExtent];
3150 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3151 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,
"%s:%s",
3152 ping_info->magick,filename);
3153 image=ReadStream(ping_info,&PingStream,exception);
3154 if (image != (
Image *) NULL)
3155 (void) DetachBlob(image->blob);
3156 ping_info=DestroyImageInfo(ping_info);
3162 ping_info->blob=(
void *) NULL;
3163 ping_info->length=0;
3164 *ping_info->filename=
'\0';
3165 status=BlobToFile(ping_info->filename,blob,length,exception);
3166 if (status == MagickFalse)
3168 (void) RelinquishUniqueFileResource(ping_info->filename);
3169 ping_info=DestroyImageInfo(ping_info);
3170 return((
Image *) NULL);
3172 clone_info=CloneImageInfo(ping_info);
3173 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
"%s:%s",
3174 ping_info->magick,ping_info->filename);
3175 image=ReadStream(clone_info,&PingStream,exception);
3176 if (image != (
Image *) NULL)
3184 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3186 (void) CopyMagickString(images->filename,image_info->filename,
3188 (void) CopyMagickString(images->magick_filename,image_info->filename,
3190 (void) CopyMagickString(images->magick,magick_info->name,
3192 images=GetNextImageInList(images);
3195 clone_info=DestroyImageInfo(clone_info);
3196 (void) RelinquishUniqueFileResource(ping_info->filename);
3197 ping_info=DestroyImageInfo(ping_info);
3232 MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
3233 unsigned char *data)
3236 *magick_restrict blob_info;
3247 assert(image != (
Image *) NULL);
3248 assert(image->signature == MagickCoreSignature);
3249 assert(image->blob != (
BlobInfo *) NULL);
3250 assert(image->blob->type != UndefinedStream);
3253 assert(data != (
void *) NULL);
3254 blob_info=image->blob;
3257 switch (blob_info->type)
3259 case UndefinedStream:
3261 case StandardStream:
3269 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3274 c=getc(blob_info->file_info.file);
3277 *q++=(
unsigned char) c;
3283 c=getc(blob_info->file_info.file);
3286 *q++=(
unsigned char) c;
3292 c=getc(blob_info->file_info.file);
3295 *q++=(
unsigned char) c;
3301 c=getc(blob_info->file_info.file);
3304 *q++=(
unsigned char) c;
3311 if ((count != (ssize_t) length) &&
3312 (ferror(blob_info->file_info.file) != 0))
3313 ThrowBlobException(blob_info);
3318 #if defined(MAGICKCORE_ZLIB_DELEGATE) 3329 for (i=0; i < (ssize_t) length; i+=count)
3331 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3332 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3345 c=gzgetc(blob_info->file_info.gzfile);
3348 *q++=(
unsigned char) c;
3354 c=gzgetc(blob_info->file_info.gzfile);
3357 *q++=(
unsigned char) c;
3363 c=gzgetc(blob_info->file_info.gzfile);
3366 *q++=(
unsigned char) c;
3372 c=gzgetc(blob_info->file_info.gzfile);
3375 *q++=(
unsigned char) c;
3383 (void) gzerror(blob_info->file_info.gzfile,&status);
3384 if ((count != (ssize_t) length) && (status != Z_OK))
3385 ThrowBlobException(blob_info);
3386 if (blob_info->eof == MagickFalse)
3387 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3394 #if defined(MAGICKCORE_BZLIB_DELEGATE) 3401 for (i=0; i < (ssize_t) length; i+=count)
3403 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3404 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3414 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3415 if ((count != (ssize_t) length) && (status != BZ_OK))
3416 ThrowBlobException(blob_info);
3427 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3429 blob_info->eof=MagickTrue;
3432 p=blob_info->data+blob_info->offset;
3433 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3434 blob_info->length-blob_info->offset);
3435 blob_info->offset+=count;
3436 if (count != (ssize_t) length)
3437 blob_info->eof=MagickTrue;
3438 (
void) memcpy(q,p,(
size_t) count);
3467 MagickExport
int ReadBlobByte(
Image *image)
3470 *magick_restrict blob_info;
3475 assert(image != (
Image *) NULL);
3476 assert(image->signature == MagickCoreSignature);
3477 assert(image->blob != (
BlobInfo *) NULL);
3478 assert(image->blob->type != UndefinedStream);
3479 blob_info=image->blob;
3480 switch (blob_info->type)
3482 case StandardStream:
3486 c=getc(blob_info->file_info.file);
3489 if (ferror(blob_info->file_info.file) != 0)
3490 ThrowBlobException(blob_info);
3497 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3499 blob_info->eof=MagickTrue;
3502 c=(int) (*((
unsigned char *) blob_info->data+blob_info->offset));
3503 blob_info->offset++;
3514 count=ReadBlob(image,1,buffer);
3547 MagickExport
double ReadBlobDouble(
Image *image)
3558 quantum.double_value=0.0;
3559 quantum.unsigned_value=ReadBlobLongLong(image);
3560 return(quantum.double_value);
3586 MagickExport
float ReadBlobFloat(
Image *image)
3597 quantum.float_value=0.0;
3598 quantum.unsigned_value=ReadBlobLong(image);
3599 return(quantum.float_value);
3625 MagickExport
unsigned int ReadBlobLong(
Image *image)
3639 assert(image != (
Image *) NULL);
3640 assert(image->signature == MagickCoreSignature);
3642 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3645 if (image->endian == LSBEndian)
3647 value=(
unsigned int) (*p++);
3648 value|=(
unsigned int) (*p++) << 8;
3649 value|=(
unsigned int) (*p++) << 16;
3650 value|=(
unsigned int) (*p++) << 24;
3653 value=(
unsigned int) (*p++) << 24;
3654 value|=(
unsigned int) (*p++) << 16;
3655 value|=(
unsigned int) (*p++) << 8;
3656 value|=(
unsigned int) (*p++);
3683 MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
3697 assert(image != (
Image *) NULL);
3698 assert(image->signature == MagickCoreSignature);
3700 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3702 return(MagickULLConstant(0));
3703 if (image->endian == LSBEndian)
3705 value=(MagickSizeType) (*p++);
3706 value|=(MagickSizeType) (*p++) << 8;
3707 value|=(MagickSizeType) (*p++) << 16;
3708 value|=(MagickSizeType) (*p++) << 24;
3709 value|=(MagickSizeType) (*p++) << 32;
3710 value|=(MagickSizeType) (*p++) << 40;
3711 value|=(MagickSizeType) (*p++) << 48;
3712 value|=(MagickSizeType) (*p++) << 56;
3715 value=(MagickSizeType) (*p++) << 56;
3716 value|=(MagickSizeType) (*p++) << 48;
3717 value|=(MagickSizeType) (*p++) << 40;
3718 value|=(MagickSizeType) (*p++) << 32;
3719 value|=(MagickSizeType) (*p++) << 24;
3720 value|=(MagickSizeType) (*p++) << 16;
3721 value|=(MagickSizeType) (*p++) << 8;
3722 value|=(MagickSizeType) (*p++);
3749 MagickExport
unsigned short ReadBlobShort(
Image *image)
3763 assert(image != (
Image *) NULL);
3764 assert(image->signature == MagickCoreSignature);
3766 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3768 return((
unsigned short) 0U);
3769 if (image->endian == LSBEndian)
3771 value=(
unsigned short) (*p++);
3772 value|=(
unsigned short) (*p++) << 8;
3775 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3776 value|=(
unsigned short) (*p++);
3803 MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
3817 assert(image != (
Image *) NULL);
3818 assert(image->signature == MagickCoreSignature);
3820 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3823 value=(
unsigned int) (*p++);
3824 value|=(
unsigned int) (*p++) << 8;
3825 value|=(
unsigned int) (*p++) << 16;
3826 value|=(
unsigned int) (*p++) << 24;
3853 MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
3864 quantum.unsigned_value=ReadBlobLSBLong(image);
3865 return(quantum.signed_value);
3891 MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
3905 assert(image != (
Image *) NULL);
3906 assert(image->signature == MagickCoreSignature);
3908 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3910 return((
unsigned short) 0U);
3911 value=(
unsigned short) (*p++);
3912 value|=(
unsigned short) (*p++) << 8;
3939 MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
3950 quantum.unsigned_value=ReadBlobLSBShort(image);
3951 return(quantum.signed_value);
3977 MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
3991 assert(image != (
Image *) NULL);
3992 assert(image->signature == MagickCoreSignature);
3994 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3997 value=(
unsigned int) (*p++) << 24;
3998 value|=(
unsigned int) (*p++) << 16;
3999 value|=(
unsigned int) (*p++) << 8;
4000 value|=(
unsigned int) (*p++);
4027 MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
4041 assert(image != (
Image *) NULL);
4042 assert(image->signature == MagickCoreSignature);
4044 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4046 return(MagickULLConstant(0));
4047 value=(MagickSizeType) (*p++) << 56;
4048 value|=(MagickSizeType) (*p++) << 48;
4049 value|=(MagickSizeType) (*p++) << 40;
4050 value|=(MagickSizeType) (*p++) << 32;
4051 value|=(MagickSizeType) (*p++) << 24;
4052 value|=(MagickSizeType) (*p++) << 16;
4053 value|=(MagickSizeType) (*p++) << 8;
4054 value|=(MagickSizeType) (*p++);
4081 MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
4095 assert(image != (
Image *) NULL);
4096 assert(image->signature == MagickCoreSignature);
4098 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4100 return((
unsigned short) 0U);
4101 value=(
unsigned short) ((*p++) << 8);
4102 value|=(
unsigned short) (*p++);
4129 MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4140 quantum.unsigned_value=ReadBlobMSBLong(image);
4141 return(quantum.signed_value);
4167 MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4178 quantum.unsigned_value=ReadBlobMSBShort(image);
4179 return(quantum.signed_value);
4205 MagickExport
signed int ReadBlobSignedLong(
Image *image)
4216 quantum.unsigned_value=ReadBlobLong(image);
4217 return(quantum.signed_value);
4243 MagickExport
signed short ReadBlobSignedShort(
Image *image)
4254 quantum.unsigned_value=ReadBlobShort(image);
4255 return(quantum.signed_value);
4293 MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4294 const size_t length,
void *magick_restrict data,ssize_t *count)
4297 *magick_restrict blob_info;
4299 assert(image != (
Image *) NULL);
4300 assert(image->signature == MagickCoreSignature);
4301 assert(image->blob != (
BlobInfo *) NULL);
4302 assert(image->blob->type != UndefinedStream);
4303 assert(count != (ssize_t *) NULL);
4304 blob_info=image->blob;
4305 if (blob_info->type != BlobStream)
4307 assert(data != NULL);
4308 *count=ReadBlob(image,length,(
unsigned char *) data);
4311 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4314 blob_info->eof=MagickTrue;
4317 data=blob_info->data+blob_info->offset;
4318 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4319 blob_info->length-blob_info->offset);
4320 blob_info->offset+=(*count);
4321 if (*count != (ssize_t) length)
4322 blob_info->eof=MagickTrue;
4351 MagickExport
char *ReadBlobString(
Image *image,
char *
string)
4354 *magick_restrict blob_info;
4362 assert(image != (
Image *) NULL);
4363 assert(image->signature == MagickCoreSignature);
4364 assert(image->blob != (
BlobInfo *) NULL);
4365 assert(image->blob->type != UndefinedStream);
4366 if (IsEventLogging() != MagickFalse)
4367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4369 blob_info=image->blob;
4370 switch (blob_info->type)
4372 case UndefinedStream:
4374 case StandardStream:
4377 char *p = fgets(
string,MagickPathExtent,blob_info->file_info.file);
4378 if (p == (
char *) NULL)
4380 if (ferror(blob_info->file_info.file) != 0)
4381 ThrowBlobException(blob_info);
4382 return((
char *) NULL);
4389 #if defined(MAGICKCORE_ZLIB_DELEGATE) 4390 char *p = gzgets(blob_info->file_info.gzfile,
string,MagickPathExtent);
4391 if (p == (
char *) NULL)
4394 (void) gzerror(blob_info->file_info.gzfile,&status);
4396 ThrowBlobException(blob_info);
4397 return((
char *) NULL);
4407 c=ReadBlobByte(image);
4410 blob_info->eof=MagickTrue;
4416 }
while (i < (MaxTextExtent-2));
4424 if ((
string[i] ==
'\r') || (
string[i] ==
'\n'))
4427 if ((
string[i-1] ==
'\r') || (
string[i-1] ==
'\n'))
4429 if ((*
string ==
'\0') && (blob_info->eof != MagickFalse))
4430 return((
char *) NULL);
4460 assert(blob->signature == MagickCoreSignature);
4461 if (IsEventLogging() != MagickFalse)
4462 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4463 LockSemaphoreInfo(blob->semaphore);
4464 blob->reference_count++;
4465 UnlockSemaphoreInfo(blob->semaphore);
4502 MagickExport MagickOffsetType SeekBlob(
Image *image,
4503 const MagickOffsetType offset,
const int whence)
4506 *magick_restrict blob_info;
4508 assert(image != (
Image *) NULL);
4509 assert(image->signature == MagickCoreSignature);
4510 assert(image->blob != (
BlobInfo *) NULL);
4511 assert(image->blob->type != UndefinedStream);
4512 if (IsEventLogging() != MagickFalse)
4513 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4514 blob_info=image->blob;
4515 switch (blob_info->type)
4517 case UndefinedStream:
4519 case StandardStream:
4524 if ((offset < 0) && (whence == SEEK_SET))
4526 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4528 blob_info->offset=TellBlob(image);
4533 #if defined(MAGICKCORE_ZLIB_DELEGATE) 4534 if (gzseek(blob_info->file_info.gzfile,offset,whence) < 0)
4537 blob_info->offset=TellBlob(image);
4553 blob_info->offset=offset;
4558 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4559 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4564 if ((blob_info->offset+offset) < 0)
4566 blob_info->offset+=offset;
4571 if (((MagickOffsetType) blob_info->length+offset) < 0)
4573 blob_info->offset=blob_info->length+offset;
4577 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4579 blob_info->eof=MagickFalse;
4585 return(blob_info->offset);
4613 MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
4615 assert(image != (
const Image *) NULL);
4616 assert(image->signature == MagickCoreSignature);
4617 if (IsEventLogging() != MagickFalse)
4618 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4619 image->blob->exempt=exempt;
4648 MagickExport MagickBooleanType SetBlobExtent(
Image *image,
4649 const MagickSizeType extent)
4652 *magick_restrict blob_info;
4654 assert(image != (
Image *) NULL);
4655 assert(image->signature == MagickCoreSignature);
4656 assert(image->blob != (
BlobInfo *) NULL);
4657 assert(image->blob->type != UndefinedStream);
4658 if (IsEventLogging() != MagickFalse)
4659 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4660 blob_info=image->blob;
4661 switch (blob_info->type)
4663 case UndefinedStream:
4665 case StandardStream:
4666 return(MagickFalse);
4675 if (extent != (MagickSizeType) ((off_t) extent))
4676 return(MagickFalse);
4677 offset=SeekBlob(image,0,SEEK_END);
4679 return(MagickFalse);
4680 if ((MagickSizeType) offset >= extent)
4682 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4685 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4686 blob_info->file_info.file);
4687 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE) 4688 if (blob_info->synchronize != MagickFalse)
4693 file=fileno(blob_info->file_info.file);
4694 if ((file == -1) || (offset < 0))
4695 return(MagickFalse);
4696 (void) posix_fallocate(file,offset,extent-offset);
4699 offset=SeekBlob(image,offset,SEEK_SET);
4701 return(MagickFalse);
4706 return(MagickFalse);
4708 return(MagickFalse);
4710 return(MagickFalse);
4713 if (extent != (MagickSizeType) ((
size_t) extent))
4714 return(MagickFalse);
4715 if (blob_info->mapped != MagickFalse)
4723 (void) UnmapBlob(blob_info->data,blob_info->length);
4724 RelinquishMagickResource(MapResource,blob_info->length);
4725 if (extent != (MagickSizeType) ((off_t) extent))
4726 return(MagickFalse);
4727 offset=SeekBlob(image,0,SEEK_END);
4729 return(MagickFalse);
4730 if ((MagickSizeType) offset >= extent)
4732 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4733 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4734 blob_info->file_info.file);
4735 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE) 4736 if (blob_info->synchronize != MagickFalse)
4741 file=fileno(blob_info->file_info.file);
4742 if ((file == -1) || (offset < 0))
4743 return(MagickFalse);
4744 (void) posix_fallocate(file,offset,extent-offset);
4747 offset=SeekBlob(image,offset,SEEK_SET);
4749 return(MagickFalse);
4750 (void) AcquireMagickResource(MapResource,extent);
4751 blob_info->data=(
unsigned char*) MapBlob(fileno(
4752 blob_info->file_info.file),WriteMode,0,(size_t) extent);
4753 blob_info->extent=(size_t) extent;
4754 blob_info->length=(size_t) extent;
4755 (void) SyncBlob(image);
4758 blob_info->extent=(size_t) extent;
4759 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4760 blob_info->extent+1,
sizeof(*blob_info->data));
4761 (void) SyncBlob(image);
4762 if (blob_info->data == (
unsigned char *) NULL)
4764 (void) DetachBlob(blob_info);
4765 return(MagickFalse);
4797 static int SyncBlob(
const Image *image)
4800 *magick_restrict blob_info;
4805 assert(image != (
Image *) NULL);
4806 assert(image->signature == MagickCoreSignature);
4807 assert(image->blob != (
BlobInfo *) NULL);
4808 if (IsEventLogging() != MagickFalse)
4809 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4810 if (EOFBlob(image) != 0)
4812 blob_info=image->blob;
4814 switch (blob_info->type)
4816 case UndefinedStream:
4817 case StandardStream:
4822 status=fflush(blob_info->file_info.file);
4827 #if defined(MAGICKCORE_ZLIB_DELEGATE) 4828 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4834 #if defined(MAGICKCORE_BZLIB_DELEGATE) 4835 status=BZ2_bzflush(blob_info->file_info.bzfile);
4869 MagickExport MagickOffsetType TellBlob(
const Image *image)
4872 *magick_restrict blob_info;
4877 assert(image != (
Image *) NULL);
4878 assert(image->signature == MagickCoreSignature);
4879 assert(image->blob != (
BlobInfo *) NULL);
4880 assert(image->blob->type != UndefinedStream);
4881 if (IsEventLogging() != MagickFalse)
4882 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4883 blob_info=image->blob;
4885 switch (blob_info->type)
4887 case UndefinedStream:
4888 case StandardStream:
4892 offset=ftell(blob_info->file_info.file);
4899 #if defined(MAGICKCORE_ZLIB_DELEGATE) 4900 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4910 offset=blob_info->offset;
4942 MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
4944 #if defined(MAGICKCORE_HAVE_MMAP) 4948 status=munmap(map,length);
4949 return(status == -1 ? MagickFalse : MagickTrue);
4953 return(MagickFalse);
4986 MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
4987 const unsigned char *data)
4990 *magick_restrict blob_info;
5004 assert(image != (
Image *) NULL);
5005 assert(image->signature == MagickCoreSignature);
5006 assert(image->blob != (
BlobInfo *) NULL);
5007 assert(image->blob->type != UndefinedStream);
5010 assert(data != (
const unsigned char *) NULL);
5011 blob_info=image->blob;
5013 p=(
const unsigned char *) data;
5014 q=(
unsigned char *) data;
5015 switch (blob_info->type)
5017 case UndefinedStream:
5019 case StandardStream:
5027 count=(ssize_t) fwrite((
const char *) data,1,length,
5028 blob_info->file_info.file);
5033 c=putc((
int) *p++,blob_info->file_info.file);
5041 c=putc((
int) *p++,blob_info->file_info.file);
5049 c=putc((
int) *p++,blob_info->file_info.file);
5057 c=putc((
int) *p++,blob_info->file_info.file);
5066 if ((count != (ssize_t) length) &&
5067 (ferror(blob_info->file_info.file) != 0))
5068 ThrowBlobException(blob_info);
5073 #if defined(MAGICKCORE_ZLIB_DELEGATE) 5084 for (i=0; i < (ssize_t) length; i+=count)
5086 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
5087 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
5100 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5108 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5116 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5124 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
5134 (void) gzerror(blob_info->file_info.gzfile,&status);
5135 if ((count != (ssize_t) length) && (status != Z_OK))
5136 ThrowBlobException(blob_info);
5142 #if defined(MAGICKCORE_BZLIB_DELEGATE) 5149 for (i=0; i < (ssize_t) length; i+=count)
5151 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
5152 (
int) MagickMin(length-i,MagickMaxBufferExtent));
5162 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
5163 if ((count != (ssize_t) length) && (status != BZ_OK))
5164 ThrowBlobException(blob_info);
5170 count=(ssize_t) blob_info->stream(image,data,length);
5178 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
5183 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
5184 if (extent >= blob_info->extent)
5186 extent+=blob_info->quantum+length;
5187 blob_info->quantum<<=1;
5188 if (SetBlobExtent(image,extent) == MagickFalse)
5191 q=blob_info->data+blob_info->offset;
5192 (void) memcpy(q,p,length);
5193 blob_info->offset+=length;
5194 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5195 blob_info->length=(
size_t) blob_info->offset;
5196 count=(ssize_t) length;
5199 if (count != (ssize_t) length)
5200 ThrowBlobException(blob_info);
5229 MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
5232 *magick_restrict blob_info;
5237 assert(image != (
Image *) NULL);
5238 assert(image->signature == MagickCoreSignature);
5239 assert(image->blob != (
BlobInfo *) NULL);
5240 assert(image->blob->type != UndefinedStream);
5241 blob_info=image->blob;
5243 switch (blob_info->type)
5245 case StandardStream:
5252 c=putc((
int) value,blob_info->file_info.file);
5255 if (ferror(blob_info->file_info.file) != 0)
5256 ThrowBlobException(blob_info);
5264 count=WriteBlobStream(image,1,&value);
5296 MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
5307 quantum.unsigned_value=0U;
5308 quantum.float_value=value;
5309 return(WriteBlobLong(image,quantum.unsigned_value));
5337 MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
5342 assert(image != (
Image *) NULL);
5343 assert(image->signature == MagickCoreSignature);
5344 if (image->endian == LSBEndian)
5346 buffer[0]=(
unsigned char) value;
5347 buffer[1]=(
unsigned char) (value >> 8);
5348 buffer[2]=(
unsigned char) (value >> 16);
5349 buffer[3]=(
unsigned char) (value >> 24);
5350 return(WriteBlobStream(image,4,buffer));
5352 buffer[0]=(
unsigned char) (value >> 24);
5353 buffer[1]=(
unsigned char) (value >> 16);
5354 buffer[2]=(
unsigned char) (value >> 8);
5355 buffer[3]=(
unsigned char) value;
5356 return(WriteBlobStream(image,4,buffer));
5384 MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
5389 assert(image != (
Image *) NULL);
5390 assert(image->signature == MagickCoreSignature);
5391 if (image->endian == LSBEndian)
5393 buffer[0]=(
unsigned char) value;
5394 buffer[1]=(
unsigned char) (value >> 8);
5395 return(WriteBlobStream(image,2,buffer));
5397 buffer[0]=(
unsigned char) (value >> 8);
5398 buffer[1]=(
unsigned char) value;
5399 return(WriteBlobStream(image,2,buffer));
5427 MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
5432 assert(image != (
Image *) NULL);
5433 assert(image->signature == MagickCoreSignature);
5434 buffer[0]=(
unsigned char) value;
5435 buffer[1]=(
unsigned char) (value >> 8);
5436 buffer[2]=(
unsigned char) (value >> 16);
5437 buffer[3]=(
unsigned char) (value >> 24);
5438 return(WriteBlobStream(image,4,buffer));
5466 MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
5471 assert(image != (
Image *) NULL);
5472 assert(image->signature == MagickCoreSignature);
5473 buffer[0]=(
unsigned char) value;
5474 buffer[1]=(
unsigned char) (value >> 8);
5475 return(WriteBlobStream(image,2,buffer));
5503 MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
5517 assert(image != (
Image *) NULL);
5518 assert(image->signature == MagickCoreSignature);
5519 quantum.signed_value=value;
5520 buffer[0]=(
unsigned char) quantum.unsigned_value;
5521 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5522 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5523 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5524 return(WriteBlobStream(image,4,buffer));
5552 MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
5553 const signed short value)
5567 assert(image != (
Image *) NULL);
5568 assert(image->signature == MagickCoreSignature);
5569 quantum.signed_value=value;
5570 buffer[0]=(
unsigned char) quantum.unsigned_value;
5571 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5572 return(WriteBlobStream(image,2,buffer));
5600 MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
5605 assert(image != (
Image *) NULL);
5606 assert(image->signature == MagickCoreSignature);
5607 buffer[0]=(
unsigned char) (value >> 24);
5608 buffer[1]=(
unsigned char) (value >> 16);
5609 buffer[2]=(
unsigned char) (value >> 8);
5610 buffer[3]=(
unsigned char) value;
5611 return(WriteBlobStream(image,4,buffer));
5639 MagickExport ssize_t WriteBlobMSBLongLong(
Image *image,
5640 const MagickSizeType value)
5645 assert(image != (
Image *) NULL);
5646 assert(image->signature == MagickCoreSignature);
5647 buffer[0]=(
unsigned char) (value >> 56);
5648 buffer[1]=(
unsigned char) (value >> 48);
5649 buffer[2]=(
unsigned char) (value >> 40);
5650 buffer[3]=(
unsigned char) (value >> 32);
5651 buffer[4]=(
unsigned char) (value >> 24);
5652 buffer[5]=(
unsigned char) (value >> 16);
5653 buffer[6]=(
unsigned char) (value >> 8);
5654 buffer[7]=(
unsigned char) value;
5655 return(WriteBlobStream(image,8,buffer));
5683 MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
5688 assert(image != (
Image *) NULL);
5689 assert(image->signature == MagickCoreSignature);
5690 buffer[0]=(
unsigned char) (value >> 8);
5691 buffer[1]=(
unsigned char) value;
5692 return(WriteBlobStream(image,2,buffer));
5720 MagickExport ssize_t WriteBlobMSBSignedLong(
Image *image,
const signed int value)
5734 assert(image != (
Image *) NULL);
5735 assert(image->signature == MagickCoreSignature);
5736 quantum.signed_value=value;
5737 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5738 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5739 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5740 buffer[3]=(
unsigned char) quantum.unsigned_value;
5741 return(WriteBlobStream(image,4,buffer));
5769 MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
5770 const signed short value)
5784 assert(image != (
Image *) NULL);
5785 assert(image->signature == MagickCoreSignature);
5786 quantum.signed_value=value;
5787 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5788 buffer[1]=(
unsigned char) quantum.unsigned_value;
5789 return(WriteBlobStream(image,2,buffer));
5817 MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
5819 assert(image != (
Image *) NULL);
5820 assert(image->signature == MagickCoreSignature);
5821 assert(
string != (
const char *) NULL);
5822 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));