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 65541
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
104 #if defined(MAGICKCORE_ZLIB_DELEGATE)
109 #if defined(MAGICKCORE_BZLIB_DELEGATE)
206 MagickExport
void AttachBlob(
BlobInfo *blob_info,
const void *blob,
209 assert(blob_info != (
BlobInfo *) NULL);
210 if (IsEventLogging() != MagickFalse)
211 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
212 blob_info->length=length;
213 blob_info->extent=length;
214 blob_info->quantum=(size_t) MagickMaxBlobExtent;
216 blob_info->type=BlobStream;
217 blob_info->file_info.file=(FILE *) NULL;
218 blob_info->data=(
unsigned char *) blob;
219 blob_info->mapped=MagickFalse;
252 MagickExport MagickBooleanType BlobToFile(
char *filename,
const void *blob,
264 assert(filename != (
const char *) NULL);
265 if (IsEventLogging() != MagickFalse)
266 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
267 assert(blob != (
const void *) NULL);
268 if (*filename ==
'\0')
269 file=AcquireUniqueFileResource(filename);
271 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
274 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
277 for (i=0; i < length; i+=count)
279 count=write(file,(
const char *) blob+i,MagickMin(length-i,(
size_t)
289 if ((file == -1) || (i < length))
291 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
328 MagickExport
Image *BlobToImage(
const ImageInfo *image_info,
const void *blob,
344 assert(image_info != (
ImageInfo *) NULL);
345 assert(image_info->signature == MagickCoreSignature);
346 if (IsEventLogging() != MagickFalse)
347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
348 image_info->filename);
350 if ((blob == (
const void *) NULL) || (length == 0))
352 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
353 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
354 return((
Image *) NULL);
356 blob_info=CloneImageInfo(image_info);
357 blob_info->blob=(
void *) blob;
358 blob_info->length=length;
359 if (*blob_info->magick ==
'\0')
360 (void) SetImageInfo(blob_info,0,exception);
361 magick_info=GetMagickInfo(blob_info->magick,exception);
364 (void) ThrowMagickException(exception,GetMagickModule(),
365 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
367 blob_info=DestroyImageInfo(blob_info);
368 return((
Image *) NULL);
370 if (GetMagickBlobSupport(magick_info) != MagickFalse)
373 filename[MagickPathExtent];
378 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
379 (void) FormatLocaleString(blob_info->filename,MaxTextExtent,
"%s:%s",
380 blob_info->magick,filename);
381 image=ReadImage(blob_info,exception);
382 if (image != (
Image *) NULL)
383 (void) DetachBlob(image->blob);
384 blob_info=DestroyImageInfo(blob_info);
390 blob_info->blob=(
void *) NULL;
392 *blob_info->filename=
'\0';
393 status=BlobToFile(blob_info->filename,blob,length,exception);
394 if (status == MagickFalse)
396 (void) RelinquishUniqueFileResource(blob_info->filename);
397 blob_info=DestroyImageInfo(blob_info);
398 return((
Image *) NULL);
400 clone_info=CloneImageInfo(blob_info);
401 (void) FormatLocaleString(clone_info->filename,MaxTextExtent,
"%s:%s",
402 blob_info->magick,blob_info->filename);
403 image=ReadImage(clone_info,exception);
404 if (image != (
Image *) NULL)
412 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
414 (void) CopyMagickString(images->filename,image_info->filename,
416 (void) CopyMagickString(images->magick_filename,image_info->filename,
418 (void) CopyMagickString(images->magick,magick_info->name,MaxTextExtent);
419 images=GetNextImageInList(images);
422 clone_info=DestroyImageInfo(clone_info);
423 (void) RelinquishUniqueFileResource(blob_info->filename);
424 blob_info=DestroyImageInfo(blob_info);
459 clone_info=(
BlobInfo *) AcquireMagickMemory(
sizeof(*clone_info));
460 GetBlobInfo(clone_info);
463 semaphore=clone_info->semaphore;
464 (void) memcpy(clone_info,blob_info,
sizeof(*clone_info));
465 if (blob_info->mapped != MagickFalse)
466 (void) AcquireMagickResource(MapResource,blob_info->length);
467 clone_info->semaphore=semaphore;
468 LockSemaphoreInfo(clone_info->semaphore);
469 clone_info->reference_count=1;
470 UnlockSemaphoreInfo(clone_info->semaphore);
497 static inline void ThrowBlobException(
BlobInfo *blob_info)
499 if ((blob_info->status == MagickFalse) && (errno != 0))
500 blob_info->error_number=errno;
501 blob_info->status=MagickTrue;
504 MagickExport MagickBooleanType CloseBlob(
Image *image)
507 *magick_restrict blob_info;
515 assert(image != (
Image *) NULL);
516 assert(image->signature == MagickCoreSignature);
517 if (IsEventLogging() != MagickFalse)
518 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
519 blob_info=image->blob;
520 if ((blob_info == (
BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
522 status=SyncBlob(image);
523 switch (blob_info->type)
525 case UndefinedStream:
531 if (blob_info->synchronize != MagickFalse)
533 status=fflush(blob_info->file_info.file);
535 ThrowBlobException(blob_info);
536 status=fsync(fileno(blob_info->file_info.file));
538 ThrowBlobException(blob_info);
540 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
541 ThrowBlobException(blob_info);
546 #if defined(MAGICKCORE_ZLIB_DELEGATE)
548 (void) gzerror(blob_info->file_info.gzfile,&status);
550 ThrowBlobException(blob_info);
556 #if defined(MAGICKCORE_BZLIB_DELEGATE)
558 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
560 ThrowBlobException(blob_info);
568 if (blob_info->file_info.file != (FILE *) NULL)
570 if (blob_info->synchronize != MagickFalse)
572 status=fflush(blob_info->file_info.file);
574 ThrowBlobException(blob_info);
575 status=fsync(fileno(blob_info->file_info.file));
577 ThrowBlobException(blob_info);
579 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
580 ThrowBlobException(blob_info);
585 blob_info->size=GetBlobSize(image);
586 image->extent=blob_info->size;
587 blob_info->eof=MagickFalse;
589 if (blob_info->exempt != MagickFalse)
591 blob_info->type=UndefinedStream;
592 return(blob_info->status);
594 switch (blob_info->type)
596 case UndefinedStream:
601 if (blob_info->file_info.file != (FILE *) NULL)
603 status=fclose(blob_info->file_info.file);
605 ThrowBlobException(blob_info);
611 #if defined(MAGICKCORE_HAVE_PCLOSE)
612 status=pclose(blob_info->file_info.file);
614 ThrowBlobException(blob_info);
620 #if defined(MAGICKCORE_ZLIB_DELEGATE)
621 status=gzclose(blob_info->file_info.gzfile);
623 ThrowBlobException(blob_info);
629 #if defined(MAGICKCORE_BZLIB_DELEGATE)
630 BZ2_bzclose(blob_info->file_info.bzfile);
638 if (blob_info->file_info.file != (FILE *) NULL)
640 status=fclose(blob_info->file_info.file);
642 ThrowBlobException(blob_info);
647 (void) DetachBlob(blob_info);
648 return(blob_info->status);
673 MagickExport
void DestroyBlob(
Image *image)
676 *magick_restrict blob_info;
681 assert(image != (
Image *) NULL);
682 assert(image->signature == MagickCoreSignature);
683 if (IsEventLogging() != MagickFalse)
684 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
685 assert(image->blob != (
BlobInfo *) NULL);
686 assert(image->blob->signature == MagickCoreSignature);
687 blob_info=image->blob;
689 LockSemaphoreInfo(blob_info->semaphore);
690 blob_info->reference_count--;
691 assert(blob_info->reference_count >= 0);
692 if (blob_info->reference_count == 0)
694 UnlockSemaphoreInfo(blob_info->semaphore);
695 if (destroy == MagickFalse)
700 (void) CloseBlob(image);
701 if (blob_info->mapped != MagickFalse)
703 (void) UnmapBlob(blob_info->data,blob_info->length);
704 RelinquishMagickResource(MapResource,blob_info->length);
707 DestroySemaphoreInfo(&blob_info->semaphore);
708 blob_info->signature=(~MagickCoreSignature);
709 image->blob=(
BlobInfo *) RelinquishMagickMemory(blob_info);
734 MagickExport
unsigned char *DetachBlob(
BlobInfo *blob_info)
739 assert(blob_info != (
BlobInfo *) NULL);
740 if (IsEventLogging() != MagickFalse)
741 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
742 if (blob_info->mapped != MagickFalse)
744 (void) UnmapBlob(blob_info->data,blob_info->length);
745 blob_info->data=NULL;
746 RelinquishMagickResource(MapResource,blob_info->length);
748 blob_info->mapped=MagickFalse;
751 blob_info->eof=MagickFalse;
753 blob_info->exempt=MagickFalse;
754 blob_info->type=UndefinedStream;
755 blob_info->file_info.file=(FILE *) NULL;
756 data=blob_info->data;
757 blob_info->data=(
unsigned char *) NULL;
758 blob_info->stream=(StreamHandler) NULL;
786 MagickPrivate
void DisassociateBlob(
Image *image)
789 *magick_restrict blob_info,
795 assert(image != (
Image *) NULL);
796 assert(image->signature == MagickCoreSignature);
797 if (IsEventLogging() != MagickFalse)
798 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
799 assert(image->blob != (
BlobInfo *) NULL);
800 assert(image->blob->signature == MagickCoreSignature);
801 blob_info=image->blob;
803 LockSemaphoreInfo(blob_info->semaphore);
804 assert(blob_info->reference_count >= 0);
805 if (blob_info->reference_count > 1)
807 UnlockSemaphoreInfo(blob_info->semaphore);
808 if (clone == MagickFalse)
810 clone_info=CloneBlobInfo(blob_info);
812 image->blob=clone_info;
840 MagickExport MagickBooleanType DiscardBlobBytes(
Image *image,
841 const MagickSizeType length)
853 buffer[MagickMinBufferExtent >> 1];
855 assert(image != (
Image *) NULL);
856 assert(image->signature == MagickCoreSignature);
857 if (length != (MagickSizeType) ((MagickOffsetType) length))
860 for (i=0; i < (MagickOffsetType) length; i+=count)
862 quantum=(size_t) MagickMin(length-i,
sizeof(buffer));
863 (void) ReadBlobStream(image,quantum,buffer,&count);
871 return(i < (MagickOffsetType) length ? MagickFalse : MagickTrue);
898 MagickExport
void DuplicateBlob(
Image *image,
const Image *duplicate)
900 assert(image != (
Image *) NULL);
901 assert(image->signature == MagickCoreSignature);
902 if (IsEventLogging() != MagickFalse)
903 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
904 assert(duplicate != (
Image *) NULL);
905 assert(duplicate->signature == MagickCoreSignature);
907 image->blob=ReferenceBlob(duplicate->blob);
933 MagickExport
int EOFBlob(
const Image *image)
936 *magick_restrict blob_info;
938 assert(image != (
Image *) NULL);
939 assert(image->signature == MagickCoreSignature);
940 if (IsEventLogging() != MagickFalse)
941 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
942 assert(image->blob != (
BlobInfo *) NULL);
943 assert(image->blob->type != UndefinedStream);
944 blob_info=image->blob;
945 switch (blob_info->type)
947 case UndefinedStream:
953 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
959 #if defined(MAGICKCORE_ZLIB_DELEGATE)
960 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
967 #if defined(MAGICKCORE_BZLIB_DELEGATE)
972 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
973 blob_info->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
979 blob_info->eof=MagickFalse;
985 return((
int) blob_info->eof);
1011 MagickExport
int ErrorBlob(
const Image *image)
1014 *magick_restrict blob_info;
1016 assert(image != (
Image *) NULL);
1017 assert(image->signature == MagickCoreSignature);
1018 if (IsEventLogging() != MagickFalse)
1019 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1020 assert(image->blob != (
BlobInfo *) NULL);
1021 assert(image->blob->type != UndefinedStream);
1022 blob_info=image->blob;
1023 switch (blob_info->type)
1025 case UndefinedStream:
1026 case StandardStream:
1031 blob_info->error=ferror(blob_info->file_info.file);
1036 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1037 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1043 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1044 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1056 return(blob_info->error);
1094 MagickExport
unsigned char *FileToBlob(
const char *filename,
const size_t extent,
1121 assert(filename != (
const char *) NULL);
1122 if (IsEventLogging() != MagickFalse)
1123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1126 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1127 if (status == MagickFalse)
1130 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1131 "NotAuthorized",
"`%s'",filename);
1135 if (LocaleCompare(filename,
"-") != 0)
1137 status=GetPathAttributes(filename,&attributes);
1138 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1140 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1143 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1147 ThrowFileException(exception,BlobError,
"UnableToOpenFile",filename);
1148 return((
unsigned char *) NULL);
1150 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1152 if ((file == fileno(stdin)) || (offset < 0) ||
1153 (offset != (MagickOffsetType) ((ssize_t) offset)))
1164 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1165 quantum=(size_t) MagickMaxBufferExtent;
1166 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1167 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1168 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1169 for (i=0; blob != (
unsigned char *) NULL; i+=count)
1171 count=read(file,blob+i,quantum);
1178 if (~((
size_t) i) < (quantum+1))
1180 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1183 blob=(
unsigned char *) ResizeQuantumMemory(blob,i+quantum+1,
1185 if ((
size_t) (i+count) >= extent)
1188 if (LocaleCompare(filename,
"-") != 0)
1190 if (blob == (
unsigned char *) NULL)
1192 (void) ThrowMagickException(exception,GetMagickModule(),
1193 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1194 return((
unsigned char *) NULL);
1198 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1199 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1200 return((
unsigned char *) NULL);
1202 *length=(size_t) MagickMin(i+count,extent);
1206 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1207 MagickMin(extent,MAGICK_SSIZE_MAX));
1208 blob=(
unsigned char *) NULL;
1209 if (~(*length) >= (MaxTextExtent-1))
1210 blob=(
unsigned char *) AcquireQuantumMemory(*length+MaxTextExtent,
1212 if (blob == (
unsigned char *) NULL)
1215 (void) ThrowMagickException(exception,GetMagickModule(),
1216 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1217 return((
unsigned char *) NULL);
1219 map=MapBlob(file,ReadMode,0,*length);
1220 if (map != (
unsigned char *) NULL)
1222 (void) memcpy(blob,map,*length);
1223 (void) UnmapBlob(map,*length);
1227 (void) lseek(file,0,SEEK_SET);
1228 for (i=0; i < *length; i+=count)
1230 count=read(file,blob+i,MagickMin(*length-i,(
size_t) MAGICK_SSIZE_MAX));
1241 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1242 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1243 return((
unsigned char *) NULL);
1247 if (LocaleCompare(filename,
"-") != 0)
1251 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1252 ThrowFileException(exception,BlobError,
"UnableToReadBlob",filename);
1282 static inline ssize_t WriteBlobStream(
Image *image,
const size_t length,
1283 const unsigned char *magick_restrict data)
1286 *magick_restrict blob_info;
1294 assert(image->blob != (
BlobInfo *) NULL);
1295 assert(image->blob->type != UndefinedStream);
1296 assert(data != (
void *) NULL);
1297 blob_info=image->blob;
1298 if (blob_info->type != BlobStream)
1299 return(WriteBlob(image,length,data));
1300 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1301 if (extent >= blob_info->extent)
1303 extent=blob_info->extent+blob_info->quantum+length;
1304 blob_info->quantum<<=1;
1305 if (SetBlobExtent(image,extent) == MagickFalse)
1308 q=blob_info->data+blob_info->offset;
1309 (void) memcpy(q,data,length);
1310 blob_info->offset+=length;
1311 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1312 blob_info->length=(
size_t) blob_info->offset;
1313 return((ssize_t) length);
1316 MagickExport MagickBooleanType FileToImage(
Image *image,
const char *filename)
1337 assert(image != (
const Image *) NULL);
1338 assert(image->signature == MagickCoreSignature);
1339 assert(filename != (
const char *) NULL);
1340 if (IsEventLogging() != MagickFalse)
1341 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1342 status=IsRightsAuthorized(PathPolicyDomain,WritePolicyRights,filename);
1343 if (status == MagickFalse)
1346 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1347 PolicyError,
"NotAuthorized",
"`%s'",filename);
1348 return(MagickFalse);
1351 if (LocaleCompare(filename,
"-") != 0)
1352 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1355 ThrowFileException(&image->exception,BlobError,
"UnableToOpenBlob",
1357 return(MagickFalse);
1359 quantum=(size_t) MagickMaxBufferExtent;
1360 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1361 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1362 blob=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*blob));
1363 if (blob == (
unsigned char *) NULL)
1366 ThrowFileException(&image->exception,ResourceLimitError,
1367 "MemoryAllocationFailed",filename);
1368 return(MagickFalse);
1372 count=read(file,blob,quantum);
1379 length=(size_t) count;
1380 count=WriteBlobStream(image,length,blob);
1381 if (count != (ssize_t) length)
1383 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1390 ThrowFileException(&image->exception,BlobError,
"UnableToWriteBlob",
1392 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1419 MagickExport MagickBooleanType GetBlobError(
const Image *image)
1421 assert(image != (
const Image *) NULL);
1422 assert(image->signature == MagickCoreSignature);
1423 if (IsEventLogging() != MagickFalse)
1424 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1425 if ((image->blob->status != MagickFalse) && (image->blob->error_number != 0))
1426 errno=image->blob->error_number;
1427 return(image->blob->status);
1452 MagickExport FILE *GetBlobFileHandle(
const Image *image)
1454 assert(image != (
const Image *) NULL);
1455 assert(image->signature == MagickCoreSignature);
1456 return(image->blob->file_info.file);
1481 MagickExport
void GetBlobInfo(
BlobInfo *blob_info)
1483 assert(blob_info != (
BlobInfo *) NULL);
1484 (void) memset(blob_info,0,
sizeof(*blob_info));
1485 blob_info->type=UndefinedStream;
1486 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1487 blob_info->properties.st_mtime=GetMagickTime();
1488 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1489 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1490 blob_info->reference_count=1;
1491 blob_info->semaphore=AllocateSemaphoreInfo();
1492 blob_info->signature=MagickCoreSignature;
1517 MagickExport
const struct stat *GetBlobProperties(
const Image *image)
1519 assert(image != (
Image *) NULL);
1520 assert(image->signature == MagickCoreSignature);
1521 if (IsEventLogging() != MagickFalse)
1522 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1523 return(&image->blob->properties);
1549 MagickExport MagickSizeType GetBlobSize(
const Image *image)
1552 *magick_restrict blob_info;
1557 assert(image != (
Image *) NULL);
1558 assert(image->signature == MagickCoreSignature);
1559 if (IsEventLogging() != MagickFalse)
1560 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1561 assert(image->blob != (
BlobInfo *) NULL);
1562 blob_info=image->blob;
1564 switch (blob_info->type)
1566 case UndefinedStream:
1567 case StandardStream:
1569 extent=blob_info->size;
1577 extent=(MagickSizeType) blob_info->properties.st_size;
1579 extent=blob_info->size;
1580 file_descriptor=fileno(blob_info->file_info.file);
1581 if (file_descriptor == -1)
1583 if (fstat(file_descriptor,&blob_info->properties) == 0)
1584 extent=(MagickSizeType) blob_info->properties.st_size;
1589 extent=blob_info->size;
1598 status=GetPathAttributes(image->filename,&blob_info->properties);
1599 if (status != MagickFalse)
1600 extent=(MagickSizeType) blob_info->properties.st_size;
1607 extent=(MagickSizeType) blob_info->length;
1636 MagickExport
unsigned char *GetBlobStreamData(
const Image *image)
1638 assert(image != (
const Image *) NULL);
1639 assert(image->signature == MagickCoreSignature);
1640 return(image->blob->data);
1665 MagickExport StreamHandler GetBlobStreamHandler(
const Image *image)
1667 assert(image != (
const Image *) NULL);
1668 assert(image->signature == MagickCoreSignature);
1669 if (IsEventLogging() != MagickFalse)
1670 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1671 return(image->blob->stream);
1707 MagickExport
unsigned char *ImageToBlob(
const ImageInfo *image_info,
1722 assert(image_info != (
const ImageInfo *) NULL);
1723 assert(image_info->signature == MagickCoreSignature);
1724 if (IsEventLogging() != MagickFalse)
1725 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1726 image_info->filename);
1727 assert(image != (
Image *) NULL);
1728 assert(image->signature == MagickCoreSignature);
1731 blob=(
unsigned char *) NULL;
1732 blob_info=CloneImageInfo(image_info);
1733 blob_info->adjoin=MagickFalse;
1734 (void) SetImageInfo(blob_info,1,exception);
1735 if (*blob_info->magick !=
'\0')
1736 (void) CopyMagickString(image->magick,blob_info->magick,MaxTextExtent);
1737 magick_info=GetMagickInfo(image->magick,exception);
1738 if (magick_info == (
const MagickInfo *) NULL)
1740 (void) ThrowMagickException(exception,GetMagickModule(),
1741 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
1743 blob_info=DestroyImageInfo(blob_info);
1746 (void) CopyMagickString(blob_info->magick,image->magick,MaxTextExtent);
1747 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1752 blob_info->length=0;
1753 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
1754 sizeof(
unsigned char));
1755 if (blob_info->blob == (
void *) NULL)
1756 (void) ThrowMagickException(exception,GetMagickModule(),
1757 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1760 (void) CloseBlob(image);
1761 image->blob->exempt=MagickTrue;
1762 *image->filename=
'\0';
1763 status=WriteImage(blob_info,image);
1764 InheritException(exception,&image->exception);
1765 *length=image->blob->length;
1766 blob=DetachBlob(image->blob);
1767 if (blob == (
unsigned char *) NULL)
1768 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
1769 else if (status == MagickFalse)
1770 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1772 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
1779 unique[MaxTextExtent];
1787 file=AcquireUniqueFileResource(unique);
1790 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",
1791 image_info->filename);
1795 blob_info->file=fdopen(file,
"wb");
1796 if (blob_info->file != (FILE *) NULL)
1798 (void) FormatLocaleString(image->filename,MaxTextExtent,
"%s:%s",
1799 image->magick,unique);
1800 status=WriteImage(blob_info,image);
1801 (void) CloseBlob(image);
1802 (void) fclose(blob_info->file);
1803 if (status == MagickFalse)
1804 InheritException(exception,&image->exception);
1806 blob=FileToBlob(unique,~0UL,length,exception);
1808 (void) RelinquishUniqueFileResource(unique);
1811 blob_info=DestroyImageInfo(blob_info);
1843 MagickExport MagickBooleanType ImageToFile(
Image *image,
char *filename,
1868 assert(image != (
Image *) NULL);
1869 assert(image->signature == MagickCoreSignature);
1870 assert(image->blob != (
BlobInfo *) NULL);
1871 assert(image->blob->type != UndefinedStream);
1872 if (IsEventLogging() != MagickFalse)
1873 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1874 assert(filename != (
const char *) NULL);
1875 if (*filename ==
'\0')
1876 file=AcquireUniqueFileResource(filename);
1878 if (LocaleCompare(filename,
"-") == 0)
1879 file=fileno(stdout);
1881 file=open_utf8(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1884 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1885 return(MagickFalse);
1887 quantum=(size_t) MagickMaxBufferExtent;
1888 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1889 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1890 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
1891 if (buffer == (
unsigned char *) NULL)
1894 (void) ThrowMagickException(exception,GetMagickModule(),
1895 ResourceLimitError,
"MemoryAllocationError",
"`%s'",filename);
1896 return(MagickFalse);
1899 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1900 for (i=0; count > 0; )
1902 length=(size_t) count;
1903 for (i=0; i < length; i+=count)
1905 count=write(file,p+i,(
size_t) (length-i));
1915 p=(
const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
1917 if (LocaleCompare(filename,
"-") != 0)
1919 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
1920 if ((file == -1) || (i < length))
1924 ThrowFileException(exception,BlobError,
"UnableToWriteBlob",filename);
1925 return(MagickFalse);
1965 MagickExport
unsigned char *ImagesToBlob(
const ImageInfo *image_info,
1980 assert(image_info != (
const ImageInfo *) NULL);
1981 assert(image_info->signature == MagickCoreSignature);
1982 if (IsEventLogging() != MagickFalse)
1983 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1984 image_info->filename);
1985 assert(images != (
Image *) NULL);
1986 assert(images->signature == MagickCoreSignature);
1989 blob=(
unsigned char *) NULL;
1990 clone_info=CloneImageInfo(image_info);
1991 (void) SetImageInfo(clone_info,(
unsigned int) GetImageListLength(images),
1993 if (*clone_info->magick !=
'\0')
1994 (void) CopyMagickString(images->magick,clone_info->magick,MaxTextExtent);
1995 magick_info=GetMagickInfo(images->magick,exception);
1996 if (magick_info == (
const MagickInfo *) NULL)
1998 (void) ThrowMagickException(exception,GetMagickModule(),
1999 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
2001 clone_info=DestroyImageInfo(clone_info);
2004 if (GetMagickAdjoin(magick_info) == MagickFalse)
2006 clone_info=DestroyImageInfo(clone_info);
2007 return(ImageToBlob(image_info,images,length,exception));
2009 (void) CopyMagickString(clone_info->magick,images->magick,MaxTextExtent);
2010 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2015 clone_info->length=0;
2016 clone_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2017 sizeof(
unsigned char));
2018 if (clone_info->blob == (
void *) NULL)
2019 (void) ThrowMagickException(exception,GetMagickModule(),
2020 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",images->filename);
2023 (void) CloseBlob(images);
2024 images->blob->exempt=MagickTrue;
2025 *images->filename=
'\0';
2026 status=WriteImages(clone_info,images,images->filename,exception);
2027 *length=images->blob->length;
2028 blob=DetachBlob(images->blob);
2029 if (blob == (
unsigned char *) NULL)
2030 clone_info->blob=RelinquishMagickMemory(clone_info->blob);
2031 else if (status == MagickFalse)
2032 blob=(
unsigned char *) RelinquishMagickMemory(blob);
2034 blob=(
unsigned char *) ResizeQuantumMemory(blob,*length+1,
2041 filename[MaxTextExtent],
2042 unique[MaxTextExtent];
2050 file=AcquireUniqueFileResource(unique);
2053 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",
2054 image_info->filename);
2058 clone_info->file=fdopen(file,
"wb");
2059 if (clone_info->file != (FILE *) NULL)
2061 (void) FormatLocaleString(filename,MaxTextExtent,
"%s:%s",
2062 images->magick,unique);
2063 status=WriteImages(clone_info,images,filename,exception);
2064 (void) CloseBlob(images);
2065 (void) fclose(clone_info->file);
2066 if (status == MagickFalse)
2067 InheritException(exception,&images->exception);
2069 blob=FileToBlob(unique,~0UL,length,exception);
2071 (void) RelinquishUniqueFileResource(unique);
2074 clone_info=DestroyImageInfo(clone_info);
2110 MagickExport MagickBooleanType InjectImageBlob(
const ImageInfo *image_info,
2114 filename[MaxTextExtent];
2143 assert(image_info != (
ImageInfo *) NULL);
2144 assert(image_info->signature == MagickCoreSignature);
2145 assert(image != (
Image *) NULL);
2146 assert(image->signature == MagickCoreSignature);
2147 if (IsEventLogging() != MagickFalse)
2148 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2149 assert(inject_image != (
Image *) NULL);
2150 assert(inject_image->signature == MagickCoreSignature);
2152 unique_file=(FILE *) NULL;
2153 file=AcquireUniqueFileResource(filename);
2155 unique_file=fdopen(file,
"wb");
2156 if ((file == -1) || (unique_file == (FILE *) NULL))
2158 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
2159 ThrowFileException(exception,FileOpenError,
"UnableToCreateTemporaryFile",
2161 return(MagickFalse);
2163 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2164 if (byte_image == (
Image *) NULL)
2166 (void) fclose(unique_file);
2167 (void) RelinquishUniqueFileResource(filename);
2168 return(MagickFalse);
2170 (void) FormatLocaleString(byte_image->filename,MaxTextExtent,
"%s:%s",format,
2172 DestroyBlob(byte_image);
2173 byte_image->blob=CloneBlobInfo((
BlobInfo *) NULL);
2174 write_info=CloneImageInfo(image_info);
2175 *write_info->magick=
'\0';
2176 SetImageInfoFile(write_info,unique_file);
2177 status=WriteImage(write_info,byte_image);
2178 write_info=DestroyImageInfo(write_info);
2179 byte_image=DestroyImage(byte_image);
2180 (void) fclose(unique_file);
2181 if (status == MagickFalse)
2183 (void) RelinquishUniqueFileResource(filename);
2184 return(MagickFalse);
2189 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
2192 (void) RelinquishUniqueFileResource(filename);
2193 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
2194 image_info->filename);
2195 return(MagickFalse);
2197 quantum=(size_t) MagickMaxBufferExtent;
2198 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2199 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2200 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
2201 if (buffer == (
unsigned char *) NULL)
2203 (void) RelinquishUniqueFileResource(filename);
2205 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2210 ssize_t count = read(file,buffer,quantum);
2217 status=WriteBlobStream(image,(
size_t) count,buffer) == count ? MagickTrue :
2222 ThrowFileException(exception,FileOpenError,
"UnableToWriteBlob",filename);
2223 (void) RelinquishUniqueFileResource(filename);
2224 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
2250 MagickExport MagickBooleanType IsBlobExempt(
const Image *image)
2252 assert(image != (
const Image *) NULL);
2253 assert(image->signature == MagickCoreSignature);
2254 if (IsEventLogging() != MagickFalse)
2255 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2256 return(image->blob->exempt);
2281 MagickExport MagickBooleanType IsBlobSeekable(
const Image *image)
2284 *magick_restrict blob_info;
2286 assert(image != (
const Image *) NULL);
2287 assert(image->signature == MagickCoreSignature);
2288 if (IsEventLogging() != MagickFalse)
2289 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2290 blob_info=image->blob;
2291 switch (blob_info->type)
2300 if (blob_info->file_info.file == (FILE *) NULL)
2301 return(MagickFalse);
2302 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
2303 return(status == -1 ? MagickFalse : MagickTrue);
2307 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2311 if (blob_info->file_info.gzfile == (gzFile) NULL)
2312 return(MagickFalse);
2313 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
2314 return(offset < 0 ? MagickFalse : MagickTrue);
2319 case UndefinedStream:
2323 case StandardStream:
2328 return(MagickFalse);
2353 MagickExport MagickBooleanType IsBlobTemporary(
const Image *image)
2355 assert(image != (
const Image *) NULL);
2356 assert(image->signature == MagickCoreSignature);
2357 if (IsEventLogging() != MagickFalse)
2358 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2359 return(image->blob->temporary);
2391 MagickExport
unsigned char *MapBlob(
int file,
const MapMode mode,
2392 const MagickOffsetType offset,
const size_t length)
2394 #if defined(MAGICKCORE_HAVE_MMAP)
2407 #if defined(MAP_ANONYMOUS)
2408 flags|=MAP_ANONYMOUS;
2410 return((
unsigned char *) NULL);
2417 protection=PROT_READ;
2423 protection=PROT_WRITE;
2429 protection=PROT_READ | PROT_WRITE;
2434 #if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
2435 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,offset);
2437 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags |
2438 MAP_HUGETLB,file,offset);
2439 if (map == (
unsigned char *) MAP_FAILED)
2440 map=(
unsigned char *) mmap((
char *) NULL,length,protection,flags,file,
2443 if (map == (
unsigned char *) MAP_FAILED)
2444 return((
unsigned char *) NULL);
2451 return((
unsigned char *) NULL);
2480 MagickExport
void MSBOrderLong(
unsigned char *buffer,
const size_t length)
2489 assert(buffer != (
unsigned char *) NULL);
2496 *buffer++=(
unsigned char) c;
2500 *buffer++=(
unsigned char) c;
2530 MagickExport
void MSBOrderShort(
unsigned char *p,
const size_t length)
2538 assert(p != (
unsigned char *) NULL);
2545 *p++=(
unsigned char) c;
2581 static inline MagickBooleanType SetStreamBuffering(
const ImageInfo *image_info,
2593 size=MagickMinBufferExtent;
2594 option=GetImageOption(image_info,
"stream:buffer-size");
2595 if (option != (
const char *) NULL)
2596 size=StringToUnsignedLong(option);
2597 status=setvbuf(image->blob->file_info.file,(
char *) NULL,size == 0 ?
2598 _IONBF : _IOFBF,size);
2599 return(status == 0 ? MagickTrue : MagickFalse);
2602 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2603 static inline gzFile gzopen_utf8(
const char *path,
const char *mode)
2605 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
2606 return(gzopen(path,mode));
2614 path_wide=create_wchar_path(path);
2615 if (path_wide == (
wchar_t *) NULL)
2616 return((gzFile) NULL);
2617 file=gzopen_w(path_wide,mode);
2618 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
2624 MagickExport MagickBooleanType OpenBlob(
const ImageInfo *image_info,
2628 *magick_restrict blob_info;
2631 extension[MaxTextExtent],
2632 filename[MaxTextExtent];
2643 assert(image_info != (
ImageInfo *) NULL);
2644 assert(image_info->signature == MagickCoreSignature);
2645 if (IsEventLogging() != MagickFalse)
2646 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
2647 image_info->filename);
2648 assert(image != (
Image *) NULL);
2649 assert(image->signature == MagickCoreSignature);
2650 blob_info=image->blob;
2651 if (image_info->blob != (
void *) NULL)
2653 if (image_info->stream != (StreamHandler) NULL)
2654 blob_info->stream=(StreamHandler) image_info->stream;
2655 AttachBlob(blob_info,image_info->blob,image_info->length);
2658 (void) DetachBlob(blob_info);
2659 blob_info->mode=mode;
2662 default: type=
"r";
break;
2663 case ReadBlobMode: type=
"r";
break;
2664 case ReadBinaryBlobMode: type=
"rb";
break;
2665 case WriteBlobMode: type=
"w";
break;
2666 case WriteBinaryBlobMode: type=
"w+b";
break;
2667 case AppendBlobMode: type=
"a";
break;
2668 case AppendBinaryBlobMode: type=
"a+b";
break;
2671 blob_info->synchronize=image_info->synchronize;
2672 if (image_info->stream != (StreamHandler) NULL)
2674 blob_info->stream=(StreamHandler) image_info->stream;
2677 blob_info->type=FifoStream;
2685 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2686 rights=ReadPolicyRights;
2688 rights=WritePolicyRights;
2689 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2692 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2693 "NotAuthorized",
"`%s'",filename);
2694 return(MagickFalse);
2696 if ((LocaleCompare(filename,
"-") == 0) ||
2697 ((*filename ==
'\0') && (image_info->file == (FILE *) NULL)))
2699 blob_info->file_info.file=(*type ==
'r') ? stdin : stdout;
2700 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2701 if (strchr(type,
'b') != (
char *) NULL)
2702 setmode(fileno(blob_info->file_info.file),_O_BINARY);
2704 blob_info->type=StandardStream;
2705 blob_info->exempt=MagickTrue;
2706 return(SetStreamBuffering(image_info,image));
2708 if ((LocaleNCompare(filename,
"fd:",3) == 0) &&
2709 (IsGeometry(filename+3) != MagickFalse))
2712 mode[MaxTextExtent];
2716 blob_info->file_info.file=fdopen(StringToLong(filename+3),mode);
2717 if (blob_info->file_info.file == (FILE *) NULL)
2719 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2720 return(MagickFalse);
2722 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
2723 if (strchr(type,
'b') != (
char *) NULL)
2724 setmode(fileno(blob_info->file_info.file),_O_BINARY);
2726 blob_info->type=FileStream;
2727 blob_info->exempt=MagickTrue;
2728 return(SetStreamBuffering(image_info,image));
2730 #if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
2731 if (*filename ==
'|')
2734 fileMode[MaxTextExtent],
2740 #if defined(SIGPIPE)
2742 (void) signal(SIGPIPE,SIG_IGN);
2746 sanitize_command=SanitizeString(filename+1);
2747 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,
2749 sanitize_command=DestroyString(sanitize_command);
2750 if (blob_info->file_info.file == (FILE *) NULL)
2752 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2753 return(MagickFalse);
2755 blob_info->type=PipeStream;
2756 blob_info->exempt=MagickTrue;
2757 return(SetStreamBuffering(image_info,image));
2760 status=GetPathAttributes(filename,&blob_info->properties);
2761 #if defined(S_ISFIFO)
2762 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
2764 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2765 if (blob_info->file_info.file == (FILE *) NULL)
2767 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2768 return(MagickFalse);
2770 blob_info->type=FileStream;
2771 blob_info->exempt=MagickTrue;
2772 return(SetStreamBuffering(image_info,image));
2775 GetPathComponent(image->filename,ExtensionPath,extension);
2778 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2779 if ((image_info->adjoin == MagickFalse) ||
2780 (strchr(filename,
'%') != (
char *) NULL))
2785 (void) InterpretImageFilename(image_info,image,image->filename,(
int)
2786 image->scene,filename);
2787 if ((LocaleCompare(filename,image->filename) == 0) &&
2788 ((GetPreviousImageInList(image) != (
Image *) NULL) ||
2789 (GetNextImageInList(image) != (
Image *) NULL)))
2792 path[MaxTextExtent];
2794 GetPathComponent(image->filename,RootPath,path);
2795 if (*extension ==
'\0')
2796 (void) FormatLocaleString(filename,MaxTextExtent,
"%s-%.20g",
2797 path,(
double) image->scene);
2799 (
void) FormatLocaleString(filename,MaxTextExtent,
"%s-%.20g.%s",
2800 path,(
double) image->scene,extension);
2802 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
2803 #if defined(macintosh)
2804 SetApplicationType(filename,image_info->magick,
'8BIM');
2808 if (image_info->file != (FILE *) NULL)
2810 blob_info->file_info.file=image_info->file;
2811 blob_info->type=FileStream;
2812 blob_info->exempt=MagickTrue;
2817 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2818 if (blob_info->file_info.file != (FILE *) NULL)
2826 blob_info->type=FileStream;
2827 (void) fstat(fileno(blob_info->file_info.file),
2828 &blob_info->properties);
2829 (void) SetStreamBuffering(image_info,image);
2830 (void) memset(magick,0,
sizeof(magick));
2831 count=fread(magick,1,
sizeof(magick),blob_info->file_info.file);
2832 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
2833 #if defined(MAGICKCORE_POSIX_SUPPORT)
2834 (void) fflush(blob_info->file_info.file);
2836 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2837 " read %.20g magic header bytes",(double) count);
2838 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2839 if (((
int) magick[0] == 0x1F) && ((
int) magick[1] == 0x8B) &&
2840 ((
int) magick[2] == 0x08))
2843 gzfile = gzopen_utf8(filename,
"rb");
2845 if (gzfile != (gzFile) NULL)
2847 if (blob_info->file_info.file != (FILE *) NULL)
2848 (void) fclose(blob_info->file_info.file);
2849 blob_info->file_info.file=(FILE *) NULL;
2850 blob_info->file_info.gzfile=gzfile;
2851 blob_info->type=ZipStream;
2855 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2856 if (strncmp((
char *) magick,
"BZh",3) == 0)
2859 *bzfile = BZ2_bzopen(filename,
"r");
2861 if (bzfile != (BZFILE *) NULL)
2863 if (blob_info->file_info.file != (FILE *) NULL)
2864 (void) fclose(blob_info->file_info.file);
2865 blob_info->file_info.file=(FILE *) NULL;
2866 blob_info->file_info.bzfile=bzfile;
2867 blob_info->type=BZipStream;
2871 if (blob_info->type == FileStream)
2882 sans_exception=AcquireExceptionInfo();
2883 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2884 sans_exception=DestroyExceptionInfo(sans_exception);
2885 length=(size_t) blob_info->properties.st_size;
2886 if ((magick_info != (
const MagickInfo *) NULL) &&
2887 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2888 (length > MagickMaxBufferExtent) &&
2889 (AcquireMagickResource(MapResource,length) != MagickFalse))
2894 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
2896 if (blob == (
void *) NULL)
2897 RelinquishMagickResource(MapResource,length);
2903 if (image_info->file != (FILE *) NULL)
2904 blob_info->exempt=MagickFalse;
2907 (void) fclose(blob_info->file_info.file);
2908 blob_info->file_info.file=(FILE *) NULL;
2910 AttachBlob(blob_info,blob,length);
2911 blob_info->mapped=MagickTrue;
2918 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2919 if ((LocaleCompare(extension,
"Z") == 0) ||
2920 (LocaleCompare(extension,
"gz") == 0) ||
2921 (LocaleCompare(extension,
"wmz") == 0) ||
2922 (LocaleCompare(extension,
"svgz") == 0))
2924 blob_info->file_info.gzfile=gzopen_utf8(filename,
"wb");
2925 if (blob_info->file_info.gzfile != (gzFile) NULL)
2926 blob_info->type=ZipStream;
2930 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2931 if (LocaleCompare(extension,
"bz2") == 0)
2933 if (mode == WriteBinaryBlobMode)
2935 blob_info->file_info.bzfile=BZ2_bzopen(filename,
"w");
2936 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
2937 blob_info->type=BZipStream;
2942 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
2943 if (blob_info->file_info.file != (FILE *) NULL)
2945 blob_info->type=FileStream;
2946 (void) SetStreamBuffering(image_info,image);
2949 blob_info->status=MagickFalse;
2950 blob_info->error_number=0;
2951 if (blob_info->type != UndefinedStream)
2952 blob_info->size=GetBlobSize(image);
2955 ThrowFileException(exception,BlobError,
"UnableToOpenBlob",filename);
2956 return(MagickFalse);
2995 #if defined(__cplusplus) || defined(c_plusplus)
2999 static size_t PingStream(
const Image *magick_unused(image),
3000 const void *magick_unused(pixels),
const size_t columns)
3002 magick_unreferenced(image);
3003 magick_unreferenced(pixels);
3008 #if defined(__cplusplus) || defined(c_plusplus)
3012 MagickExport
Image *PingBlob(
const ImageInfo *image_info,
const void *blob,
3028 assert(image_info != (
ImageInfo *) NULL);
3029 assert(image_info->signature == MagickCoreSignature);
3030 if (IsEventLogging() != MagickFalse)
3031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
3032 image_info->filename);
3034 if ((blob == (
const void *) NULL) || (length == 0))
3036 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3037 "ZeroLengthBlobNotPermitted",
"`%s'",image_info->filename);
3038 return((
Image *) NULL);
3040 ping_info=CloneImageInfo(image_info);
3041 ping_info->blob=(
void *) blob;
3042 ping_info->length=length;
3043 ping_info->ping=MagickTrue;
3044 if (*ping_info->magick ==
'\0')
3045 (void) SetImageInfo(ping_info,0,exception);
3046 magick_info=GetMagickInfo(ping_info->magick,exception);
3047 if (magick_info == (
const MagickInfo *) NULL)
3049 (void) ThrowMagickException(exception,GetMagickModule(),
3050 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
3052 ping_info=DestroyImageInfo(ping_info);
3053 return((
Image *) NULL);
3055 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3058 filename[MagickPathExtent];
3063 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3064 (void) FormatLocaleString(ping_info->filename,MaxTextExtent,
"%s:%s",
3065 ping_info->magick,filename);
3066 image=ReadStream(ping_info,&PingStream,exception);
3067 if (image != (
Image *) NULL)
3068 (void) DetachBlob(image->blob);
3069 ping_info=DestroyImageInfo(ping_info);
3075 ping_info->blob=(
void *) NULL;
3076 ping_info->length=0;
3077 *ping_info->filename=
'\0';
3078 status=BlobToFile(ping_info->filename,blob,length,exception);
3079 if (status == MagickFalse)
3081 (void) RelinquishUniqueFileResource(ping_info->filename);
3082 ping_info=DestroyImageInfo(ping_info);
3083 return((
Image *) NULL);
3085 clone_info=CloneImageInfo(ping_info);
3086 (void) FormatLocaleString(clone_info->filename,MaxTextExtent,
"%s:%s",
3087 ping_info->magick,ping_info->filename);
3088 image=ReadStream(clone_info,&PingStream,exception);
3089 if (image != (
Image *) NULL)
3097 for (images=GetFirstImageInList(image); images != (
Image *) NULL; )
3099 (void) CopyMagickString(images->filename,image_info->filename,
3101 (void) CopyMagickString(images->magick_filename,image_info->filename,
3103 (void) CopyMagickString(images->magick,magick_info->name,MaxTextExtent);
3104 images=GetNextImageInList(images);
3107 clone_info=DestroyImageInfo(clone_info);
3108 (void) RelinquishUniqueFileResource(ping_info->filename);
3109 ping_info=DestroyImageInfo(ping_info);
3144 MagickExport ssize_t ReadBlob(
Image *image,
const size_t length,
3145 unsigned char *data)
3148 *magick_restrict blob_info;
3159 assert(image != (
Image *) NULL);
3160 assert(image->signature == MagickCoreSignature);
3161 assert(image->blob != (
BlobInfo *) NULL);
3162 assert(image->blob->type != UndefinedStream);
3165 assert(data != (
void *) NULL);
3166 blob_info=image->blob;
3169 switch (blob_info->type)
3171 case UndefinedStream:
3173 case StandardStream:
3181 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3186 c=getc(blob_info->file_info.file);
3189 *q++=(
unsigned char) c;
3194 c=getc(blob_info->file_info.file);
3197 *q++=(
unsigned char) c;
3202 c=getc(blob_info->file_info.file);
3205 *q++=(
unsigned char) c;
3210 c=getc(blob_info->file_info.file);
3213 *q++=(
unsigned char) c;
3219 if ((count != (ssize_t) length) &&
3220 (ferror(blob_info->file_info.file) != 0))
3221 ThrowBlobException(blob_info);
3226 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3237 for (i=0; i < (ssize_t) length; i+=count)
3239 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
3240 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3253 c=gzgetc(blob_info->file_info.gzfile);
3256 *q++=(
unsigned char) c;
3261 c=gzgetc(blob_info->file_info.gzfile);
3264 *q++=(
unsigned char) c;
3269 c=gzgetc(blob_info->file_info.gzfile);
3272 *q++=(
unsigned char) c;
3277 c=gzgetc(blob_info->file_info.gzfile);
3280 *q++=(
unsigned char) c;
3287 (void) gzerror(blob_info->file_info.gzfile,&status);
3288 if ((count != (ssize_t) length) && (status != Z_OK))
3289 ThrowBlobException(blob_info);
3290 if (blob_info->eof == MagickFalse)
3291 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
3298 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3305 for (i=0; i < (ssize_t) length; i+=count)
3307 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,
3308 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
3318 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
3319 if ((count != (ssize_t) length) && (status != BZ_OK))
3320 ThrowBlobException(blob_info);
3331 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
3333 blob_info->eof=MagickTrue;
3336 p=blob_info->data+blob_info->offset;
3337 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
3338 blob_info->length-blob_info->offset);
3339 blob_info->offset+=count;
3340 if (count != (ssize_t) length)
3341 blob_info->eof=MagickTrue;
3342 (
void) memcpy(q,p,(
size_t) count);
3371 MagickExport
int ReadBlobByte(
Image *image)
3374 *magick_restrict blob_info;
3382 assert(image != (
Image *) NULL);
3383 assert(image->signature == MagickCoreSignature);
3384 assert(image->blob != (
BlobInfo *) NULL);
3385 assert(image->blob->type != UndefinedStream);
3386 blob_info=image->blob;
3387 switch (blob_info->type)
3389 case StandardStream:
3396 p=(
const unsigned char *) buffer;
3397 c=getc(blob_info->file_info.file);
3400 *buffer=(
unsigned char) c;
3408 p=(
const unsigned char *) ReadBlobStream(image,1,buffer,&count);
3440 MagickExport
double ReadBlobDouble(
Image *image)
3451 quantum.double_value=0.0;
3452 quantum.unsigned_value=ReadBlobLongLong(image);
3453 return(quantum.double_value);
3479 MagickExport
float ReadBlobFloat(
Image *image)
3490 quantum.float_value=0.0;
3491 quantum.unsigned_value=ReadBlobLong(image);
3492 return(quantum.float_value);
3518 MagickExport
unsigned int ReadBlobLong(
Image *image)
3532 assert(image != (
Image *) NULL);
3533 assert(image->signature == MagickCoreSignature);
3535 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3538 if (image->endian == LSBEndian)
3540 value=(
unsigned int) (*p++);
3541 value|=(
unsigned int) (*p++) << 8;
3542 value|=(
unsigned int) (*p++) << 16;
3543 value|=(
unsigned int) (*p++) << 24;
3546 value=(
unsigned int) (*p++) << 24;
3547 value|=(
unsigned int) (*p++) << 16;
3548 value|=(
unsigned int) (*p++) << 8;
3549 value|=(
unsigned int) (*p++);
3576 MagickExport MagickSizeType ReadBlobLongLong(
Image *image)
3590 assert(image != (
Image *) NULL);
3591 assert(image->signature == MagickCoreSignature);
3593 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3595 return(MagickULLConstant(0));
3596 if (image->endian == LSBEndian)
3598 value=(MagickSizeType) (*p++);
3599 value|=(MagickSizeType) (*p++) << 8;
3600 value|=(MagickSizeType) (*p++) << 16;
3601 value|=(MagickSizeType) (*p++) << 24;
3602 value|=(MagickSizeType) (*p++) << 32;
3603 value|=(MagickSizeType) (*p++) << 40;
3604 value|=(MagickSizeType) (*p++) << 48;
3605 value|=(MagickSizeType) (*p++) << 56;
3608 value=(MagickSizeType) (*p++) << 56;
3609 value|=(MagickSizeType) (*p++) << 48;
3610 value|=(MagickSizeType) (*p++) << 40;
3611 value|=(MagickSizeType) (*p++) << 32;
3612 value|=(MagickSizeType) (*p++) << 24;
3613 value|=(MagickSizeType) (*p++) << 16;
3614 value|=(MagickSizeType) (*p++) << 8;
3615 value|=(MagickSizeType) (*p++);
3642 MagickExport
unsigned short ReadBlobShort(
Image *image)
3656 assert(image != (
Image *) NULL);
3657 assert(image->signature == MagickCoreSignature);
3659 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3661 return((
unsigned short) 0U);
3662 if (image->endian == LSBEndian)
3664 value=(
unsigned short) (*p++);
3665 value|=(
unsigned short) (*p++) << 8;
3668 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3669 value|=(
unsigned short) (*p++);
3696 MagickExport
unsigned int ReadBlobLSBLong(
Image *image)
3710 assert(image != (
Image *) NULL);
3711 assert(image->signature == MagickCoreSignature);
3713 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3716 value=(
unsigned int) (*p++);
3717 value|=(
unsigned int) (*p++) << 8;
3718 value|=(
unsigned int) (*p++) << 16;
3719 value|=(
unsigned int) (*p++) << 24;
3746 MagickExport
signed int ReadBlobLSBSignedLong(
Image *image)
3757 quantum.unsigned_value=ReadBlobLSBLong(image);
3758 return(quantum.signed_value);
3784 MagickExport
unsigned short ReadBlobLSBShort(
Image *image)
3798 assert(image != (
Image *) NULL);
3799 assert(image->signature == MagickCoreSignature);
3801 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3803 return((
unsigned short) 0U);
3804 value=(
unsigned short) (*p++);
3805 value|=(
unsigned short) (*p++) << 8;
3832 MagickExport
signed short ReadBlobLSBSignedShort(
Image *image)
3843 quantum.unsigned_value=ReadBlobLSBShort(image);
3844 return(quantum.signed_value);
3870 MagickExport
unsigned int ReadBlobMSBLong(
Image *image)
3884 assert(image != (
Image *) NULL);
3885 assert(image->signature == MagickCoreSignature);
3887 p=(
const unsigned char *) ReadBlobStream(image,4,buffer,&count);
3890 value=(
unsigned int) (*p++) << 24;
3891 value|=(
unsigned int) (*p++) << 16;
3892 value|=(
unsigned int) (*p++) << 8;
3893 value|=(
unsigned int) (*p++);
3920 MagickExport MagickSizeType ReadBlobMSBLongLong(
Image *image)
3934 assert(image != (
Image *) NULL);
3935 assert(image->signature == MagickCoreSignature);
3937 p=(
const unsigned char *) ReadBlobStream(image,8,buffer,&count);
3939 return(MagickULLConstant(0));
3940 value=(MagickSizeType) (*p++) << 56;
3941 value|=(MagickSizeType) (*p++) << 48;
3942 value|=(MagickSizeType) (*p++) << 40;
3943 value|=(MagickSizeType) (*p++) << 32;
3944 value|=(MagickSizeType) (*p++) << 24;
3945 value|=(MagickSizeType) (*p++) << 16;
3946 value|=(MagickSizeType) (*p++) << 8;
3947 value|=(MagickSizeType) (*p++);
3974 MagickExport
unsigned short ReadBlobMSBShort(
Image *image)
3988 assert(image != (
Image *) NULL);
3989 assert(image->signature == MagickCoreSignature);
3991 p=(
const unsigned char *) ReadBlobStream(image,2,buffer,&count);
3993 return((
unsigned short) 0U);
3994 value=(
unsigned short) ((
unsigned short) (*p++) << 8);
3995 value|=(
unsigned short) (*p++);
4022 MagickExport
signed int ReadBlobMSBSignedLong(
Image *image)
4033 quantum.unsigned_value=ReadBlobMSBLong(image);
4034 return(quantum.signed_value);
4060 MagickExport
signed short ReadBlobMSBSignedShort(
Image *image)
4071 quantum.unsigned_value=ReadBlobMSBShort(image);
4072 return(quantum.signed_value);
4098 MagickExport
signed int ReadBlobSignedLong(
Image *image)
4109 quantum.unsigned_value=ReadBlobLong(image);
4110 return(quantum.signed_value);
4136 MagickExport
signed short ReadBlobSignedShort(
Image *image)
4147 quantum.unsigned_value=ReadBlobShort(image);
4148 return(quantum.signed_value);
4186 MagickExport magick_hot_spot
const void *ReadBlobStream(
Image *image,
4187 const size_t length,
void *magick_restrict data,ssize_t *count)
4190 *magick_restrict blob_info;
4192 assert(image != (
Image *) NULL);
4193 assert(image->signature == MagickCoreSignature);
4194 assert(image->blob != (
BlobInfo *) NULL);
4195 assert(image->blob->type != UndefinedStream);
4196 assert(count != (ssize_t *) NULL);
4197 blob_info=image->blob;
4198 if (blob_info->type != BlobStream)
4200 assert(data != NULL);
4201 *count=ReadBlob(image,length,(
unsigned char *) data);
4204 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4207 blob_info->eof=MagickTrue;
4210 data=blob_info->data+blob_info->offset;
4211 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4212 blob_info->length-blob_info->offset);
4213 blob_info->offset+=(*count);
4214 if (*count != (ssize_t) length)
4215 blob_info->eof=MagickTrue;
4244 MagickExport
char *ReadBlobString(
Image *image,
char *
string)
4252 assert(image != (
Image *) NULL);
4253 assert(image->signature == MagickCoreSignature);
4254 for (i=0; i < (MaxTextExtent-1L); i++)
4256 c=ReadBlobByte(image);
4260 return((
char *) NULL);
4266 if ((i > 0) && (
string[i-1] ==
'\r'))
4301 assert(blob->signature == MagickCoreSignature);
4302 if (IsEventLogging() != MagickFalse)
4303 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
4304 LockSemaphoreInfo(blob->semaphore);
4305 blob->reference_count++;
4306 UnlockSemaphoreInfo(blob->semaphore);
4343 MagickExport MagickOffsetType SeekBlob(
Image *image,
4344 const MagickOffsetType offset,
const int whence)
4347 *magick_restrict blob_info;
4349 assert(image != (
Image *) NULL);
4350 assert(image->signature == MagickCoreSignature);
4351 if (IsEventLogging() != MagickFalse)
4352 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4353 assert(image->blob != (
BlobInfo *) NULL);
4354 assert(image->blob->type != UndefinedStream);
4355 blob_info=image->blob;
4356 switch (blob_info->type)
4358 case UndefinedStream:
4360 case StandardStream:
4365 if ((offset < 0) && (whence == SEEK_SET))
4367 if (fseek(blob_info->file_info.file,offset,whence) < 0)
4369 blob_info->offset=TellBlob(image);
4374 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4375 if (gzseek(blob_info->file_info.gzfile,(off_t) offset,whence) < 0)
4378 blob_info->offset=TellBlob(image);
4394 blob_info->offset=offset;
4399 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
4400 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
4405 if ((blob_info->offset+offset) < 0)
4407 blob_info->offset+=offset;
4412 if (((MagickOffsetType) blob_info->length+offset) < 0)
4414 blob_info->offset=blob_info->length+offset;
4418 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
4420 blob_info->eof=MagickFalse;
4423 if (blob_info->offset >= (MagickOffsetType) ((off_t) blob_info->extent))
4428 return(blob_info->offset);
4456 MagickExport
void SetBlobExempt(
Image *image,
const MagickBooleanType exempt)
4458 assert(image != (
const Image *) NULL);
4459 assert(image->signature == MagickCoreSignature);
4460 if (IsEventLogging() != MagickFalse)
4461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4462 image->blob->exempt=exempt;
4491 MagickExport MagickBooleanType SetBlobExtent(
Image *image,
4492 const MagickSizeType extent)
4495 *magick_restrict blob_info;
4497 assert(image != (
Image *) NULL);
4498 assert(image->signature == MagickCoreSignature);
4499 if (IsEventLogging() != MagickFalse)
4500 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4501 assert(image->blob != (
BlobInfo *) NULL);
4502 assert(image->blob->type != UndefinedStream);
4503 blob_info=image->blob;
4504 switch (blob_info->type)
4506 case UndefinedStream:
4508 case StandardStream:
4509 return(MagickFalse);
4518 if (extent != (MagickSizeType) ((off_t) extent))
4519 return(MagickFalse);
4520 offset=SeekBlob(image,0,SEEK_END);
4522 return(MagickFalse);
4523 if ((MagickSizeType) offset >= extent)
4525 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4528 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4529 blob_info->file_info.file);
4530 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4531 if (blob_info->synchronize != MagickFalse)
4536 file=fileno(blob_info->file_info.file);
4537 if ((file == -1) || (offset < 0))
4538 return(MagickFalse);
4539 (void) posix_fallocate(file,offset,extent-offset);
4542 offset=SeekBlob(image,offset,SEEK_SET);
4544 return(MagickFalse);
4549 return(MagickFalse);
4551 return(MagickFalse);
4553 return(MagickFalse);
4556 if (extent != (MagickSizeType) ((
size_t) extent))
4557 return(MagickFalse);
4558 if (blob_info->mapped != MagickFalse)
4566 (void) UnmapBlob(blob_info->data,blob_info->length);
4567 RelinquishMagickResource(MapResource,blob_info->length);
4568 if (extent != (MagickSizeType) ((off_t) extent))
4569 return(MagickFalse);
4570 offset=SeekBlob(image,0,SEEK_END);
4572 return(MagickFalse);
4573 if ((MagickSizeType) offset >= extent)
4575 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
4576 count=(ssize_t) fwrite((
const unsigned char *)
"",1,1,
4577 blob_info->file_info.file);
4578 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
4579 if (blob_info->synchronize != MagickFalse)
4584 file=fileno(blob_info->file_info.file);
4585 if ((file == -1) || (offset < 0))
4586 return(MagickFalse);
4587 (void) posix_fallocate(file,offset,extent-offset);
4590 offset=SeekBlob(image,offset,SEEK_SET);
4592 return(MagickFalse);
4593 (void) AcquireMagickResource(MapResource,extent);
4594 blob_info->data=(
unsigned char*) MapBlob(fileno(
4595 blob_info->file_info.file),WriteMode,0,(size_t) extent);
4596 blob_info->extent=(size_t) extent;
4597 blob_info->length=(size_t) extent;
4598 (void) SyncBlob(image);
4601 blob_info->extent=(size_t) extent;
4602 blob_info->data=(
unsigned char *) ResizeQuantumMemory(blob_info->data,
4603 blob_info->extent+1,
sizeof(*blob_info->data));
4604 (void) SyncBlob(image);
4605 if (blob_info->data == (
unsigned char *) NULL)
4607 (void) DetachBlob(blob_info);
4608 return(MagickFalse);
4639 static int SyncBlob(
Image *image)
4642 *magick_restrict blob_info;
4647 assert(image != (
Image *) NULL);
4648 assert(image->signature == MagickCoreSignature);
4649 if (IsEventLogging() != MagickFalse)
4650 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4651 assert(image->blob != (
BlobInfo *) NULL);
4652 assert(image->blob->type != UndefinedStream);
4653 blob_info=image->blob;
4655 switch (blob_info->type)
4657 case UndefinedStream:
4658 case StandardStream:
4663 status=fflush(blob_info->file_info.file);
4668 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4669 status=gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
4675 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4676 status=BZ2_bzflush(blob_info->file_info.bzfile);
4710 MagickExport MagickOffsetType TellBlob(
const Image *image)
4713 *magick_restrict blob_info;
4718 assert(image != (
Image *) NULL);
4719 assert(image->signature == MagickCoreSignature);
4720 if (IsEventLogging() != MagickFalse)
4721 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4722 assert(image->blob != (
BlobInfo *) NULL);
4723 assert(image->blob->type != UndefinedStream);
4724 blob_info=image->blob;
4726 switch (blob_info->type)
4728 case UndefinedStream:
4729 case StandardStream:
4733 offset=ftell(blob_info->file_info.file);
4740 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4741 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
4751 offset=blob_info->offset;
4783 MagickExport MagickBooleanType UnmapBlob(
void *map,
const size_t length)
4785 #if defined(MAGICKCORE_HAVE_MMAP)
4789 status=munmap(map,length);
4790 return(status == -1 ? MagickFalse : MagickTrue);
4794 return(MagickFalse);
4827 MagickExport ssize_t WriteBlob(
Image *image,
const size_t length,
4828 const unsigned char *data)
4831 *magick_restrict blob_info;
4845 assert(image != (
Image *) NULL);
4846 assert(image->signature == MagickCoreSignature);
4847 assert(image->blob != (
BlobInfo *) NULL);
4848 assert(image->blob->type != UndefinedStream);
4851 assert(data != (
const unsigned char *) NULL);
4852 blob_info=image->blob;
4854 p=(
const unsigned char *) data;
4855 q=(
unsigned char *) data;
4856 switch (blob_info->type)
4858 case UndefinedStream:
4860 case StandardStream:
4868 count=(ssize_t) fwrite((
const char *) data,1,length,
4869 blob_info->file_info.file);
4874 c=putc((
int) *p++,blob_info->file_info.file);
4881 c=putc((
int) *p++,blob_info->file_info.file);
4888 c=putc((
int) *p++,blob_info->file_info.file);
4895 c=putc((
int) *p++,blob_info->file_info.file);
4903 if ((count != (ssize_t) length) &&
4904 (ferror(blob_info->file_info.file) != 0))
4905 ThrowBlobException(blob_info);
4910 #if defined(MAGICKCORE_ZLIB_DELEGATE)
4921 for (i=0; i < (ssize_t) length; i+=count)
4923 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
4924 (
unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4937 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
4944 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
4951 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
4958 c=gzputc(blob_info->file_info.gzfile,(
int) *p++);
4967 (void) gzerror(blob_info->file_info.gzfile,&status);
4968 if ((count != (ssize_t) length) && (status != Z_OK))
4969 ThrowBlobException(blob_info);
4975 #if defined(MAGICKCORE_BZLIB_DELEGATE)
4982 for (i=0; i < (ssize_t) length; i+=count)
4984 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
4985 (
int) MagickMin(length-i,MagickMaxBufferExtent));
4995 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4996 if ((count != (ssize_t) length) && (status != BZ_OK))
4997 ThrowBlobException(blob_info);
5003 count=(ssize_t) blob_info->stream(image,data,length);
5008 if ((blob_info->offset+(MagickOffsetType) length) >=
5009 (MagickOffsetType) blob_info->extent)
5011 if (blob_info->mapped != MagickFalse)
5013 blob_info->extent+=length+blob_info->quantum;
5014 blob_info->quantum<<=1;
5015 blob_info->data=(
unsigned char *) ResizeQuantumMemory(
5016 blob_info->data,blob_info->extent+1,
sizeof(*blob_info->data));
5017 (void) SyncBlob(image);
5018 if (blob_info->data == (
unsigned char *) NULL)
5020 (void) DetachBlob(blob_info);
5024 q=blob_info->data+blob_info->offset;
5025 (void) memcpy(q,p,length);
5026 blob_info->offset+=length;
5027 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5028 blob_info->length=(
size_t) blob_info->offset;
5029 count=(ssize_t) length;
5032 if (count != (ssize_t) length)
5033 ThrowBlobException(blob_info);
5062 MagickExport ssize_t WriteBlobByte(
Image *image,
const unsigned char value)
5065 *magick_restrict blob_info;
5070 assert(image != (
Image *) NULL);
5071 assert(image->signature == MagickCoreSignature);
5072 assert(image->blob != (
BlobInfo *) NULL);
5073 assert(image->blob->type != UndefinedStream);
5074 blob_info=image->blob;
5076 switch (blob_info->type)
5078 case StandardStream:
5085 c=putc((
int) value,blob_info->file_info.file);
5088 if (ferror(blob_info->file_info.file) != 0)
5089 ThrowBlobException(blob_info);
5097 count=WriteBlobStream(image,1,&value);
5129 MagickExport ssize_t WriteBlobFloat(
Image *image,
const float value)
5140 quantum.unsigned_value=0U;
5141 quantum.float_value=value;
5142 return(WriteBlobLong(image,quantum.unsigned_value));
5170 MagickExport ssize_t WriteBlobLong(
Image *image,
const unsigned int value)
5175 assert(image != (
Image *) NULL);
5176 assert(image->signature == MagickCoreSignature);
5177 if (image->endian == LSBEndian)
5179 buffer[0]=(
unsigned char) value;
5180 buffer[1]=(
unsigned char) (value >> 8);
5181 buffer[2]=(
unsigned char) (value >> 16);
5182 buffer[3]=(
unsigned char) (value >> 24);
5183 return(WriteBlobStream(image,4,buffer));
5185 buffer[0]=(
unsigned char) (value >> 24);
5186 buffer[1]=(
unsigned char) (value >> 16);
5187 buffer[2]=(
unsigned char) (value >> 8);
5188 buffer[3]=(
unsigned char) value;
5189 return(WriteBlobStream(image,4,buffer));
5217 MagickExport ssize_t WriteBlobShort(
Image *image,
const unsigned short value)
5222 assert(image != (
Image *) NULL);
5223 assert(image->signature == MagickCoreSignature);
5224 if (image->endian == LSBEndian)
5226 buffer[0]=(
unsigned char) value;
5227 buffer[1]=(
unsigned char) (value >> 8);
5228 return(WriteBlobStream(image,2,buffer));
5230 buffer[0]=(
unsigned char) (value >> 8);
5231 buffer[1]=(
unsigned char) value;
5232 return(WriteBlobStream(image,2,buffer));
5260 MagickExport ssize_t WriteBlobLSBLong(
Image *image,
const unsigned int value)
5265 assert(image != (
Image *) NULL);
5266 assert(image->signature == MagickCoreSignature);
5267 buffer[0]=(
unsigned char) value;
5268 buffer[1]=(
unsigned char) (value >> 8);
5269 buffer[2]=(
unsigned char) (value >> 16);
5270 buffer[3]=(
unsigned char) (value >> 24);
5271 return(WriteBlobStream(image,4,buffer));
5299 MagickExport ssize_t WriteBlobLSBShort(
Image *image,
const unsigned short value)
5304 assert(image != (
Image *) NULL);
5305 assert(image->signature == MagickCoreSignature);
5306 buffer[0]=(
unsigned char) value;
5307 buffer[1]=(
unsigned char) (value >> 8);
5308 return(WriteBlobStream(image,2,buffer));
5336 MagickExport ssize_t WriteBlobLSBSignedLong(
Image *image,
const signed int value)
5350 assert(image != (
Image *) NULL);
5351 assert(image->signature == MagickCoreSignature);
5352 quantum.signed_value=value;
5353 buffer[0]=(
unsigned char) quantum.unsigned_value;
5354 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5355 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 16);
5356 buffer[3]=(
unsigned char) (quantum.unsigned_value >> 24);
5357 return(WriteBlobStream(image,4,buffer));
5385 MagickExport ssize_t WriteBlobLSBSignedShort(
Image *image,
5386 const signed short value)
5400 assert(image != (
Image *) NULL);
5401 assert(image->signature == MagickCoreSignature);
5402 quantum.signed_value=value;
5403 buffer[0]=(
unsigned char) quantum.unsigned_value;
5404 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 8);
5405 return(WriteBlobStream(image,2,buffer));
5433 MagickExport ssize_t WriteBlobMSBLong(
Image *image,
const unsigned int value)
5438 assert(image != (
Image *) NULL);
5439 assert(image->signature == MagickCoreSignature);
5440 buffer[0]=(
unsigned char) (value >> 24);
5441 buffer[1]=(
unsigned char) (value >> 16);
5442 buffer[2]=(
unsigned char) (value >> 8);
5443 buffer[3]=(
unsigned char) value;
5444 return(WriteBlobStream(image,4,buffer));
5472 MagickExport ssize_t WriteBlobMSBLongLong(
Image *image,
5473 const MagickSizeType value)
5478 assert(image != (
Image *) NULL);
5479 assert(image->signature == MagickCoreSignature);
5480 buffer[0]=(
unsigned char) (value >> 56);
5481 buffer[1]=(
unsigned char) (value >> 48);
5482 buffer[2]=(
unsigned char) (value >> 40);
5483 buffer[3]=(
unsigned char) (value >> 32);
5484 buffer[4]=(
unsigned char) (value >> 24);
5485 buffer[5]=(
unsigned char) (value >> 16);
5486 buffer[6]=(
unsigned char) (value >> 8);
5487 buffer[7]=(
unsigned char) value;
5488 return(WriteBlobStream(image,8,buffer));
5516 MagickExport ssize_t WriteBlobMSBShort(
Image *image,
const unsigned short value)
5521 assert(image != (
Image *) NULL);
5522 assert(image->signature == MagickCoreSignature);
5523 buffer[0]=(
unsigned char) (value >> 8);
5524 buffer[1]=(
unsigned char) value;
5525 return(WriteBlobStream(image,2,buffer));
5553 MagickExport ssize_t WriteBlobMSBSignedLong(
Image *image,
const signed int value)
5567 assert(image != (
Image *) NULL);
5568 assert(image->signature == MagickCoreSignature);
5569 quantum.signed_value=value;
5570 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 24);
5571 buffer[1]=(
unsigned char) (quantum.unsigned_value >> 16);
5572 buffer[2]=(
unsigned char) (quantum.unsigned_value >> 8);
5573 buffer[3]=(
unsigned char) quantum.unsigned_value;
5574 return(WriteBlobStream(image,4,buffer));
5602 MagickExport ssize_t WriteBlobMSBSignedShort(
Image *image,
5603 const signed short value)
5617 assert(image != (
Image *) NULL);
5618 assert(image->signature == MagickCoreSignature);
5619 quantum.signed_value=value;
5620 buffer[0]=(
unsigned char) (quantum.unsigned_value >> 8);
5621 buffer[1]=(
unsigned char) quantum.unsigned_value;
5622 return(WriteBlobStream(image,2,buffer));
5650 MagickExport ssize_t WriteBlobString(
Image *image,
const char *
string)
5652 assert(image != (
Image *) NULL);
5653 assert(image->signature == MagickCoreSignature);
5654 assert(
string != (
const char *) NULL);
5655 return(WriteBlobStream(image,strlen(
string),(
const unsigned char *)
string));