MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
blob.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% BBBB L OOO BBBB %
7% B B L O O B B %
8% BBBB L O O BBBB %
9% B B L O O B B %
10% BBBB LLLLL OOO BBBB %
11% %
12% %
13% MagickCore Binary Large OBjectS Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1999 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/license/ %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39␌
40/*
41 Include declarations.
42*/
43#ifdef __VMS
44#include <types.h>
45#include <mman.h>
46#endif
47#include "MagickCore/studio.h"
48#include "MagickCore/blob.h"
49#include "MagickCore/blob-private.h"
50#include "MagickCore/cache.h"
51#include "MagickCore/client.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/delegate.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/locale_.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/memory-private.h"
64#include "MagickCore/nt-base-private.h"
65#include "MagickCore/option.h"
66#include "MagickCore/policy.h"
67#include "MagickCore/policy-private.h"
68#include "MagickCore/resource_.h"
69#include "MagickCore/semaphore.h"
70#include "MagickCore/string_.h"
71#include "MagickCore/string-private.h"
72#include "MagickCore/timer-private.h"
73#include "MagickCore/token.h"
74#include "MagickCore/utility.h"
75#include "MagickCore/utility-private.h"
76#if defined(MAGICKCORE_ZLIB_DELEGATE)
77#include "zlib.h"
78#endif
79#if defined(MAGICKCORE_BZLIB_DELEGATE)
80#include "bzlib.h"
81#endif
82␌
83/*
84 Define declarations.
85*/
86#define MagickMaxBlobExtent (8*8192)
87#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
88# define MAP_ANONYMOUS MAP_ANON
89#endif
90#if !defined(MAP_FAILED)
91#define MAP_FAILED ((void *) -1)
92#endif
93#if defined(__OS2__)
94#include <io.h>
95#define _O_BINARY O_BINARY
96#endif
97#if defined(MAGICKCORE_WINDOWS_SUPPORT)
98# if !defined(fsync)
99# define fsync _commit
100# endif
101# if !defined(mmap)
102# define MAGICKCORE_HAVE_MMAP 1
103# define mmap(address,length,protection,access,file,offset) \
104 NTMapMemory(address,length,protection,access,file,offset)
105# endif
106# if !defined(munmap)
107# define munmap(address,length) NTUnmapMemory(address,length)
108# endif
109# if !defined(pclose)
110# define pclose _pclose
111# endif
112# if !defined(popen)
113# define popen _popen
114# endif
115#endif
116␌
117/*
118 Typedef declarations.
119*/
120typedef union FileInfo
121{
122 FILE
123 *file;
124
125#if defined(MAGICKCORE_ZLIB_DELEGATE)
126 gzFile
127 gzfile;
128#endif
129
130#if defined(MAGICKCORE_BZLIB_DELEGATE)
131 BZFILE
132 *bzfile;
133#endif
134} FileInfo;
135
137{
138 size_t
139 length,
140 extent,
141 quantum;
142
143 BlobMode
144 mode;
145
146 MagickBooleanType
147 mapped,
148 eof;
149
150 int
151 error,
152 error_number;
153
154 MagickOffsetType
155 offset;
156
157 MagickSizeType
158 size;
159
160 MagickBooleanType
161 exempt,
162 synchronize,
163 temporary;
164
165 int
166 status;
167
168 StreamType
169 type;
170
172 file_info;
173
174 struct stat
175 properties;
176
177 StreamHandler
178 stream;
179
180 CustomStreamInfo
181 *custom_stream;
182
183 unsigned char
184 *data;
185
186 MagickBooleanType
187 debug;
188
190 *semaphore;
191
192 ssize_t
193 reference_count;
194
195 size_t
196 signature;
197};
198
200{
201 CustomStreamHandler
202 reader,
203 writer;
204
205 CustomStreamSeeker
206 seeker;
207
208 CustomStreamTeller
209 teller;
210
211 void
212 *data;
213
214 size_t
215 signature;
216};
217␌
218/*
219 Forward declarations.
220*/
221static int
222 SyncBlob(const Image *);
223␌
224/*
225%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226% %
227% %
228% %
229+ A c q u i r e C u s t o m S t r e a m I n f o %
230% %
231% %
232% %
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234%
235% AcquireCustomStreamInfo() allocates the CustomStreamInfo structure.
236%
237% The format of the AcquireCustomStreamInfo method is:
238%
239% CustomStreamInfo *AcquireCustomStreamInfo(ExceptionInfo *exception)
240%
241% A description of each parameter follows:
242%
243% o exception: return any errors or warnings in this structure.
244%
245*/
246MagickExport CustomStreamInfo *AcquireCustomStreamInfo(
247 ExceptionInfo *magick_unused(exception))
248{
249 CustomStreamInfo
250 *custom_stream;
251
252 magick_unreferenced(exception);
253 custom_stream=(CustomStreamInfo *) AcquireCriticalMemory(
254 sizeof(*custom_stream));
255 (void) memset(custom_stream,0,sizeof(*custom_stream));
256 custom_stream->signature=MagickCoreSignature;
257 return(custom_stream);
258}
259␌
260/*
261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262% %
263% %
264% %
265+ A t t a c h B l o b %
266% %
267% %
268% %
269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270%
271% AttachBlob() attaches a blob to the BlobInfo structure.
272%
273% The format of the AttachBlob method is:
274%
275% void AttachBlob(BlobInfo *blob_info,const void *blob,const size_t length)
276%
277% A description of each parameter follows:
278%
279% o blob_info: Specifies a pointer to a BlobInfo structure.
280%
281% o blob: the address of a character stream in one of the image formats
282% understood by ImageMagick.
283%
284% o length: This size_t integer reflects the length in bytes of the blob.
285%
286*/
287MagickExport void AttachBlob(BlobInfo *blob_info,const void *blob,
288 const size_t length)
289{
290 assert(blob_info != (BlobInfo *) NULL);
291 if (IsEventLogging() != MagickFalse)
292 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
293 blob_info->length=length;
294 blob_info->extent=length;
295 blob_info->quantum=(size_t) MagickMaxBlobExtent;
296 blob_info->offset=0;
297 blob_info->type=BlobStream;
298 blob_info->file_info.file=(FILE *) NULL;
299 blob_info->data=(unsigned char *) blob;
300 blob_info->mapped=MagickFalse;
301}
302␌
303/*
304%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305% %
306% %
307% %
308+ A t t a c h C u s t o m S t r e a m %
309% %
310% %
311% %
312%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313%
314% AttachCustomStream() attaches a CustomStreamInfo to the BlobInfo structure.
315%
316% The format of the AttachCustomStream method is:
317%
318% void AttachCustomStream(BlobInfo *blob_info,
319% CustomStreamInfo *custom_stream)
320%
321% A description of each parameter follows:
322%
323% o blob_info: specifies a pointer to a BlobInfo structure.
324%
325% o custom_stream: the custom stream info.
326%
327*/
328MagickExport void AttachCustomStream(BlobInfo *blob_info,
329 CustomStreamInfo *custom_stream)
330{
331 assert(blob_info != (BlobInfo *) NULL);
332 assert(custom_stream != (CustomStreamInfo *) NULL);
333 assert(custom_stream->signature == MagickCoreSignature);
334 if (IsEventLogging() != MagickFalse)
335 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
336 blob_info->type=CustomStream;
337 blob_info->custom_stream=custom_stream;
338}
339␌
340/*
341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342% %
343% %
344% %
345+ B l o b T o F i l e %
346% %
347% %
348% %
349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350%
351% BlobToFile() writes a blob to a file. It returns MagickFalse if an error
352% occurs otherwise MagickTrue.
353%
354% The format of the BlobToFile method is:
355%
356% MagickBooleanType BlobToFile(char *filename,const void *blob,
357% const size_t length,ExceptionInfo *exception)
358%
359% A description of each parameter follows:
360%
361% o filename: Write the blob to this file. The filename buffer length must
362% be a minimum of MagickPathExtent characters.
363%
364% o blob: the address of a blob.
365%
366% o length: This length in bytes of the blob.
367%
368% o exception: return any errors or warnings in this structure.
369%
370*/
371MagickExport MagickBooleanType BlobToFile(char *filename,const void *blob,
372 const size_t length,ExceptionInfo *exception)
373{
374 int
375 file;
376
377 size_t
378 i;
379
380 ssize_t
381 count;
382
383 assert(filename != (const char *) NULL);
384 assert(blob != (const void *) NULL);
385 if (IsEventLogging() != MagickFalse)
386 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
387 if (*filename == '\0')
388 file=AcquireUniqueFileResource(filename);
389 else
390 file=open_utf8(filename,O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL | O_BINARY,P_MODE);
391 if (file == -1)
392 {
393 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
394 return(MagickFalse);
395 }
396 for (i=0; i < length; i+=(size_t) count)
397 {
398 count=MagickWrite(file,(const char *) blob+i,MagickMin(length-i,(size_t)
399 MagickMaxBufferExtent));
400 if (count <= 0)
401 break;
402 }
403 file=close_utf8(file);
404 if ((file == -1) || (i < length))
405 {
406 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
407 return(MagickFalse);
408 }
409 return(MagickTrue);
410}
411␌
412/*
413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414% %
415% %
416% %
417% B l o b T o I m a g e %
418% %
419% %
420% %
421%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422%
423% BlobToImage() implements direct to memory image formats. It returns the
424% blob as an image.
425%
426% The format of the BlobToImage method is:
427%
428% Image *BlobToImage(const ImageInfo *image_info,const void *blob,
429% const size_t length,ExceptionInfo *exception)
430%
431% A description of each parameter follows:
432%
433% o image_info: the image info.
434%
435% o blob: the address of a character stream in one of the image formats
436% understood by ImageMagick.
437%
438% o length: This size_t integer reflects the length in bytes of the blob.
439%
440% o exception: return any errors or warnings in this structure.
441%
442*/
443MagickExport Image *BlobToImage(const ImageInfo *image_info,const void *blob,
444 const size_t length,ExceptionInfo *exception)
445{
446 const MagickInfo
447 *magick_info;
448
449 Image
450 *image;
451
452 ImageInfo
453 *blob_info,
454 *clone_info;
455
456 MagickBooleanType
457 status;
458
459 assert(image_info != (ImageInfo *) NULL);
460 assert(image_info->signature == MagickCoreSignature);
461 assert(exception != (ExceptionInfo *) NULL);
462 if (IsEventLogging() != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
464 image_info->filename);
465 if ((blob == (const void *) NULL) || (length == 0))
466 {
467 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
468 "ZeroLengthBlobNotPermitted","`%s'",image_info->filename);
469 return((Image *) NULL);
470 }
471 blob_info=CloneImageInfo(image_info);
472 blob_info->blob=(void *) blob;
473 blob_info->length=length;
474 if (*blob_info->magick == '\0')
475 (void) SetImageInfo(blob_info,0,exception);
476 magick_info=GetMagickInfo(blob_info->magick,exception);
477 if (magick_info == (const MagickInfo *) NULL)
478 {
479 (void) ThrowMagickException(exception,GetMagickModule(),
480 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
481 blob_info->magick);
482 blob_info=DestroyImageInfo(blob_info);
483 return((Image *) NULL);
484 }
485 if (GetMagickBlobSupport(magick_info) != MagickFalse)
486 {
487 char
488 filename[MagickPathExtent];
489
490 /*
491 Native blob support for this image format.
492 */
493 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
494 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,"%s:%s",
495 blob_info->magick,filename);
496 image=ReadImage(blob_info,exception);
497 if (image != (Image *) NULL)
498 (void) DetachBlob(image->blob);
499 blob_info=DestroyImageInfo(blob_info);
500 return(image);
501 }
502 /*
503 Write blob to a temporary file on disk.
504 */
505 blob_info->blob=(void *) NULL;
506 blob_info->length=0;
507 *blob_info->filename='\0';
508 status=BlobToFile(blob_info->filename,blob,length,exception);
509 if (status == MagickFalse)
510 {
511 (void) RelinquishUniqueFileResource(blob_info->filename);
512 blob_info=DestroyImageInfo(blob_info);
513 return((Image *) NULL);
514 }
515 clone_info=CloneImageInfo(blob_info);
516 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:%s",
517 blob_info->magick,blob_info->filename);
518 image=ReadImage(clone_info,exception);
519 if (image != (Image *) NULL)
520 {
521 Image
522 *images;
523
524 /*
525 Restore original filenames and image format.
526 */
527 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
528 {
529 (void) CopyMagickString(images->filename,image_info->filename,
530 MagickPathExtent);
531 (void) CopyMagickString(images->magick_filename,image_info->filename,
532 MagickPathExtent);
533 (void) CopyMagickString(images->magick,magick_info->name,
534 MagickPathExtent);
535 images=GetNextImageInList(images);
536 }
537 }
538 clone_info=DestroyImageInfo(clone_info);
539 (void) RelinquishUniqueFileResource(blob_info->filename);
540 blob_info=DestroyImageInfo(blob_info);
541 return(image);
542}
543␌
544/*
545%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
546% %
547% %
548% %
549+ C l o n e B l o b I n f o %
550% %
551% %
552% %
553%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554%
555% CloneBlobInfo() makes a duplicate of the given blob info structure, or if
556% blob info is NULL, a new one.
557%
558% The format of the CloneBlobInfo method is:
559%
560% BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
561%
562% A description of each parameter follows:
563%
564% o blob_info: the blob info.
565%
566*/
567MagickExport BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
568{
569 BlobInfo
570 *clone_info;
571
573 *semaphore;
574
575 clone_info=(BlobInfo *) AcquireCriticalMemory(sizeof(*clone_info));
576 GetBlobInfo(clone_info);
577 if (blob_info == (BlobInfo *) NULL)
578 return(clone_info);
579 semaphore=clone_info->semaphore;
580 (void) memcpy(clone_info,blob_info,sizeof(*clone_info));
581 if (blob_info->mapped != MagickFalse)
582 (void) AcquireMagickResource(MapResource,blob_info->length);
583 clone_info->semaphore=semaphore;
584 LockSemaphoreInfo(clone_info->semaphore);
585 clone_info->reference_count=1;
586 UnlockSemaphoreInfo(clone_info->semaphore);
587 return(clone_info);
588}
589␌
590/*
591%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
592% %
593% %
594% %
595+ C l o s e B l o b %
596% %
597% %
598% %
599%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
600%
601% CloseBlob() closes a stream associated with the image.
602%
603% The format of the CloseBlob method is:
604%
605% MagickBooleanType CloseBlob(Image *image)
606%
607% A description of each parameter follows:
608%
609% o image: the image.
610%
611*/
612
613static inline void ThrowBlobException(BlobInfo *blob_info)
614{
615 if ((blob_info->status == 0) && (errno != 0))
616 blob_info->error_number=errno;
617 blob_info->status=(-1);
618}
619
620MagickExport MagickBooleanType CloseBlob(Image *image)
621{
622 BlobInfo
623 *magick_restrict blob_info;
624
625 int
626 status;
627
628 /*
629 Close image file.
630 */
631 assert(image != (Image *) NULL);
632 assert(image->signature == MagickCoreSignature);
633 if (IsEventLogging() != MagickFalse)
634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
635 blob_info=image->blob;
636 if ((blob_info == (BlobInfo *) NULL) || (blob_info->type == UndefinedStream))
637 return(MagickTrue);
638 status=SyncBlob(image);
639 /* Some platforms report an error when a read-only stream is flushed. */
640 if ((status != 0) &&
641 ((blob_info->mode == WriteBlobMode) ||
642 (blob_info->mode == WriteBinaryBlobMode) ||
643 (blob_info->mode == AppendBlobMode) ||
644 (blob_info->mode == AppendBinaryBlobMode)))
645 ThrowBlobException(blob_info);
646 status=blob_info->status;
647 switch (blob_info->type)
648 {
649 case UndefinedStream:
650 break;
651 case StandardStream:
652 case FileStream:
653 case PipeStream:
654 {
655 if (blob_info->synchronize != MagickFalse)
656 {
657 status=fflush(blob_info->file_info.file);
658 if (status != 0)
659 ThrowBlobException(blob_info);
660 status=fsync(fileno(blob_info->file_info.file));
661 if (status != 0)
662 ThrowBlobException(blob_info);
663 }
664 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
665 ThrowBlobException(blob_info);
666 break;
667 }
668 case ZipStream:
669 {
670#if defined(MAGICKCORE_ZLIB_DELEGATE)
671 status=Z_OK;
672 (void) gzerror(blob_info->file_info.gzfile,&status);
673 if (status != Z_OK)
674 ThrowBlobException(blob_info);
675#endif
676 break;
677 }
678 case BZipStream:
679 {
680#if defined(MAGICKCORE_BZLIB_DELEGATE)
681 status=BZ_OK;
682 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
683 if (status != BZ_OK)
684 ThrowBlobException(blob_info);
685#endif
686 break;
687 }
688 case FifoStream:
689 break;
690 case BlobStream:
691 {
692 if (blob_info->file_info.file != (FILE *) NULL)
693 {
694 if (blob_info->synchronize != MagickFalse)
695 {
696 status=fflush(blob_info->file_info.file);
697 if (status != 0)
698 ThrowBlobException(blob_info);
699 status=fsync(fileno(blob_info->file_info.file));
700 if (status != 0)
701 ThrowBlobException(blob_info);
702 }
703 if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
704 ThrowBlobException(blob_info);
705 }
706 break;
707 }
708 case CustomStream:
709 break;
710 }
711 blob_info->size=GetBlobSize(image);
712 image->extent=blob_info->size;
713 blob_info->eof=MagickFalse;
714 blob_info->error=0;
715 blob_info->mode=UndefinedBlobMode;
716 if (blob_info->exempt != MagickFalse)
717 {
718 blob_info->type=UndefinedStream;
719 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
720 }
721 switch (blob_info->type)
722 {
723 case UndefinedStream:
724 case StandardStream:
725 break;
726 case FileStream:
727 {
728 if (blob_info->file_info.file != (FILE *) NULL)
729 {
730 status=fclose(blob_info->file_info.file);
731 if (status != 0)
732 ThrowBlobException(blob_info);
733 }
734 break;
735 }
736 case PipeStream:
737 {
738#if defined(MAGICKCORE_HAVE_PCLOSE)
739 status=pclose(blob_info->file_info.file);
740 if (status != 0)
741 ThrowBlobException(blob_info);
742#endif
743 break;
744 }
745 case ZipStream:
746 {
747#if defined(MAGICKCORE_ZLIB_DELEGATE)
748 status=gzclose(blob_info->file_info.gzfile);
749 if (status != Z_OK)
750 ThrowBlobException(blob_info);
751#endif
752 break;
753 }
754 case BZipStream:
755 {
756#if defined(MAGICKCORE_BZLIB_DELEGATE)
757 BZ2_bzclose(blob_info->file_info.bzfile);
758#endif
759 break;
760 }
761 case FifoStream:
762 break;
763 case BlobStream:
764 {
765 if (blob_info->file_info.file != (FILE *) NULL)
766 {
767 status=fclose(blob_info->file_info.file);
768 if (status != 0)
769 ThrowBlobException(blob_info);
770 }
771 break;
772 }
773 case CustomStream:
774 break;
775 }
776 (void) DetachBlob(blob_info);
777 return(blob_info->status != 0 ? MagickFalse : MagickTrue);
778}
779␌
780/*
781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782% %
783% %
784% %
785% C u s t o m S t r e a m T o I m a g e %
786% %
787% %
788% %
789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790%
791% CustomStreamToImage() is the equivalent of ReadImage(), but reads the
792% formatted "file" from the supplied method rather than to an actual file.
793%
794% The format of the CustomStreamToImage method is:
795%
796% Image *CustomStreamToImage(const ImageInfo *image_info,
797% ExceptionInfo *exception)
798%
799% A description of each parameter follows:
800%
801% o image_info: the image info.
802%
803% o exception: return any errors or warnings in this structure.
804%
805*/
806MagickExport Image *CustomStreamToImage(const ImageInfo *image_info,
807 ExceptionInfo *exception)
808{
809 const MagickInfo
810 *magick_info;
811
812 Image
813 *image;
814
815 ImageInfo
816 *blob_info;
817
818 assert(image_info != (ImageInfo *) NULL);
819 assert(image_info->signature == MagickCoreSignature);
820 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
821 assert(image_info->custom_stream->signature == MagickCoreSignature);
822 assert(image_info->custom_stream->reader != (CustomStreamHandler) NULL);
823 assert(exception != (ExceptionInfo *) NULL);
824 if (IsEventLogging() != MagickFalse)
825 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
826 image_info->filename);
827 blob_info=CloneImageInfo(image_info);
828 if (*blob_info->magick == '\0')
829 (void) SetImageInfo(blob_info,0,exception);
830 magick_info=GetMagickInfo(blob_info->magick,exception);
831 if (magick_info == (const MagickInfo *) NULL)
832 {
833 (void) ThrowMagickException(exception,GetMagickModule(),
834 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
835 blob_info->magick);
836 blob_info=DestroyImageInfo(blob_info);
837 return((Image *) NULL);
838 }
839 image=(Image *) NULL;
840 if ((GetMagickBlobSupport(magick_info) != MagickFalse) ||
841 (*blob_info->filename != '\0'))
842 {
843 char
844 filename[MagickPathExtent];
845
846 /*
847 Native blob support for this image format or SetImageInfo changed the
848 blob to a file.
849 */
850 (void) CopyMagickString(filename,blob_info->filename,MagickPathExtent);
851 (void) FormatLocaleString(blob_info->filename,MagickPathExtent,"%s:%s",
852 blob_info->magick,filename);
853 image=ReadImage(blob_info,exception);
854 }
855 else
856 {
857 char
858 unique[MagickPathExtent];
859
860 int
861 file;
862
863 ImageInfo
864 *clone_info;
865
866 unsigned char
867 *blob;
868
869 /*
870 Write data to file on disk.
871 */
872 blob_info->custom_stream=(CustomStreamInfo *) NULL;
873 blob=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
874 sizeof(*blob));
875 if (blob == (unsigned char *) NULL)
876 {
877 ThrowFileException(exception,BlobError,"UnableToReadBlob",
878 image_info->filename);
879 blob_info=DestroyImageInfo(blob_info);
880 return((Image *) NULL);
881 }
882 file=AcquireUniqueFileResource(unique);
883 if (file == -1)
884 {
885 ThrowFileException(exception,BlobError,"UnableToReadBlob",
886 image_info->filename);
887 blob=(unsigned char *) RelinquishMagickMemory(blob);
888 blob_info=DestroyImageInfo(blob_info);
889 return((Image *) NULL);
890 }
891 clone_info=CloneImageInfo(blob_info);
892 blob_info->file=fdopen(file,"wb+");
893 if (blob_info->file != (FILE *) NULL)
894 {
895 ssize_t
896 count;
897
898 count=(ssize_t) MagickMaxBufferExtent;
899 while (count == (ssize_t) MagickMaxBufferExtent)
900 {
901 count=image_info->custom_stream->reader(blob,MagickMaxBufferExtent,
902 image_info->custom_stream->data);
903 if (count > 0)
904 count=(ssize_t) write(file,(const char *) blob,(size_t) count);
905 }
906 (void) fclose(blob_info->file);
907 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,
908 "%s:%s",blob_info->magick,unique);
909 image=ReadImage(clone_info,exception);
910 if (image != (Image *) NULL)
911 {
912 Image
913 *images;
914
915 /*
916 Restore original filenames and image format.
917 */
918 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
919 {
920 (void) CopyMagickString(images->filename,image_info->filename,
921 MagickPathExtent);
922 (void) CopyMagickString(images->magick_filename,
923 image_info->filename,MagickPathExtent);
924 (void) CopyMagickString(images->magick,magick_info->name,
925 MagickPathExtent);
926 images=GetNextImageInList(images);
927 }
928 }
929 }
930 clone_info=DestroyImageInfo(clone_info);
931 blob=(unsigned char *) RelinquishMagickMemory(blob);
932 (void) RelinquishUniqueFileResource(unique);
933 }
934 blob_info=DestroyImageInfo(blob_info);
935 if (image != (Image *) NULL)
936 if (CloseBlob(image) == MagickFalse)
937 image=DestroyImageList(image);
938 return(image);
939}
940␌
941/*
942%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943% %
944% %
945% %
946+ D e s t r o y B l o b %
947% %
948% %
949% %
950%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
951%
952% DestroyBlob() deallocates memory associated with a blob.
953%
954% The format of the DestroyBlob method is:
955%
956% void DestroyBlob(Image *image)
957%
958% A description of each parameter follows:
959%
960% o image: the image.
961%
962*/
963MagickExport void DestroyBlob(Image *image)
964{
965 BlobInfo
966 *magick_restrict blob_info;
967
968 MagickBooleanType
969 destroy;
970
971 assert(image != (Image *) NULL);
972 assert(image->signature == MagickCoreSignature);
973 assert(image->blob != (BlobInfo *) NULL);
974 assert(image->blob->signature == MagickCoreSignature);
975 if (IsEventLogging() != MagickFalse)
976 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
977 blob_info=image->blob;
978 destroy=MagickFalse;
979 LockSemaphoreInfo(blob_info->semaphore);
980 blob_info->reference_count--;
981 assert(blob_info->reference_count >= 0);
982 if (blob_info->reference_count == 0)
983 destroy=MagickTrue;
984 UnlockSemaphoreInfo(blob_info->semaphore);
985 if (destroy == MagickFalse)
986 {
987 image->blob=(BlobInfo *) NULL;
988 return;
989 }
990 (void) CloseBlob(image);
991 if (blob_info->mapped != MagickFalse)
992 {
993 (void) UnmapBlob(blob_info->data,blob_info->length);
994 RelinquishMagickResource(MapResource,blob_info->length);
995 }
996 if (blob_info->semaphore != (SemaphoreInfo *) NULL)
997 RelinquishSemaphoreInfo(&blob_info->semaphore);
998 blob_info->signature=(~MagickCoreSignature);
999 image->blob=(BlobInfo *) RelinquishMagickMemory(blob_info);
1000}
1001␌
1002/*
1003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1004% %
1005% %
1006% %
1007+ D e s t r o y C u s t o m S t r e a m I n f o %
1008% %
1009% %
1010% %
1011%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1012%
1013% DestroyCustomStreamInfo() destroys memory associated with the
1014% CustomStreamInfo structure.
1015%
1016% The format of the DestroyCustomStreamInfo method is:
1017%
1018% CustomStreamInfo *DestroyCustomStreamInfo(CustomStreamInfo *stream_info)
1019%
1020% A description of each parameter follows:
1021%
1022% o custom_stream: the custom stream info.
1023%
1024*/
1025MagickExport CustomStreamInfo *DestroyCustomStreamInfo(
1026 CustomStreamInfo *custom_stream)
1027{
1028 assert(custom_stream != (CustomStreamInfo *) NULL);
1029 assert(custom_stream->signature == MagickCoreSignature);
1030 if (IsEventLogging() != MagickFalse)
1031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1032 custom_stream->signature=(~MagickCoreSignature);
1033 custom_stream=(CustomStreamInfo *) RelinquishMagickMemory(custom_stream);
1034 return(custom_stream);
1035}
1036␌
1037/*
1038%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1039% %
1040% %
1041% %
1042+ D e t a c h B l o b %
1043% %
1044% %
1045% %
1046%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047%
1048% DetachBlob() detaches a blob from the BlobInfo structure.
1049%
1050% The format of the DetachBlob method is:
1051%
1052% void *DetachBlob(BlobInfo *blob_info)
1053%
1054% A description of each parameter follows:
1055%
1056% o blob_info: Specifies a pointer to a BlobInfo structure.
1057%
1058*/
1059MagickExport void *DetachBlob(BlobInfo *blob_info)
1060{
1061 void
1062 *data;
1063
1064 assert(blob_info != (BlobInfo *) NULL);
1065 if (IsEventLogging() != MagickFalse)
1066 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1067 if (blob_info->mapped != MagickFalse)
1068 {
1069 (void) UnmapBlob(blob_info->data,blob_info->length);
1070 blob_info->data=NULL;
1071 RelinquishMagickResource(MapResource,blob_info->length);
1072 }
1073 blob_info->mapped=MagickFalse;
1074 blob_info->length=0;
1075 /*
1076 We should not reset blob_info->extent because we use it to check if the
1077 blob was opened inside ImagesToBlob and ImagesToBlob.
1078 */
1079 blob_info->offset=0;
1080 blob_info->mode=UndefinedBlobMode;
1081 blob_info->eof=MagickFalse;
1082 blob_info->error=0;
1083 blob_info->exempt=MagickFalse;
1084 blob_info->type=UndefinedStream;
1085 blob_info->file_info.file=(FILE *) NULL;
1086 data=blob_info->data;
1087 blob_info->data=(unsigned char *) NULL;
1088 blob_info->stream=(StreamHandler) NULL;
1089 blob_info->custom_stream=(CustomStreamInfo *) NULL;
1090 return(data);
1091}
1092␌
1093/*
1094%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1095% %
1096% %
1097% %
1098+ D i s a s s o c i a t e B l o b %
1099% %
1100% %
1101% %
1102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1103%
1104% DisassociateBlob() disassociates the image stream. It checks if the
1105% blob of the specified image is referenced by other images. If the reference
1106% count is higher then 1 a new blob is assigned to the specified image.
1107%
1108% The format of the DisassociateBlob method is:
1109%
1110% void DisassociateBlob(const Image *image)
1111%
1112% A description of each parameter follows:
1113%
1114% o image: the image.
1115%
1116*/
1117
1118static inline void ResetDisassociatedBlobStorage(BlobInfo *blob_info)
1119{
1120 assert(blob_info != (BlobInfo *) NULL);
1121 assert(blob_info->signature == MagickCoreSignature);
1122 if (blob_info->mapped != MagickFalse)
1123 {
1124 RelinquishMagickResource(MapResource,blob_info->length);
1125 blob_info->mapped=MagickFalse;
1126 }
1127 blob_info->data=(unsigned char *) NULL;
1128 blob_info->length=0;
1129 blob_info->offset=0;
1130 blob_info->mode=UndefinedBlobMode;
1131 blob_info->type=UndefinedStream;
1132}
1133
1134MagickExport void DisassociateBlob(Image *image)
1135{
1136 BlobInfo
1137 *magick_restrict blob_info,
1138 *clone_info;
1139
1140 MagickBooleanType
1141 clone;
1142
1143 assert(image != (Image *) NULL);
1144 assert(image->signature == MagickCoreSignature);
1145 assert(image->blob != (BlobInfo *) NULL);
1146 assert(image->blob->signature == MagickCoreSignature);
1147 if (IsEventLogging() != MagickFalse)
1148 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1149 blob_info=image->blob;
1150 clone=MagickFalse;
1151 LockSemaphoreInfo(blob_info->semaphore);
1152 assert(blob_info->reference_count >= 0);
1153 if (blob_info->reference_count > 1)
1154 clone=MagickTrue;
1155 UnlockSemaphoreInfo(blob_info->semaphore);
1156 if (clone == MagickFalse)
1157 return;
1158 clone_info=CloneBlobInfo(blob_info);
1159 ResetDisassociatedBlobStorage(clone_info);
1160 DestroyBlob(image);
1161 image->blob=clone_info;
1162}
1163␌
1164/*
1165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1166% %
1167% %
1168% %
1169+ D i s c a r d B l o b B y t e s %
1170% %
1171% %
1172% %
1173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1174%
1175% DiscardBlobBytes() discards bytes in a blob.
1176%
1177% The format of the DiscardBlobBytes method is:
1178%
1179% MagickBooleanType DiscardBlobBytes(Image *image,
1180% const MagickSizeType length)
1181%
1182% A description of each parameter follows.
1183%
1184% o image: the image.
1185%
1186% o length: the number of bytes to skip.
1187%
1188*/
1189MagickExport MagickBooleanType DiscardBlobBytes(Image *image,
1190 const MagickSizeType length)
1191{
1192 MagickSizeType
1193 i;
1194
1195 size_t
1196 quantum;
1197
1198 ssize_t
1199 count;
1200
1201 unsigned char
1202 buffer[MagickMinBufferExtent >> 1];
1203
1204 assert(image != (Image *) NULL);
1205 assert(image->signature == MagickCoreSignature);
1206 if (length != (MagickSizeType) ((MagickOffsetType) length))
1207 return(MagickFalse);
1208 count=0;
1209 for (i=0; i < length; i+=(MagickSizeType) count)
1210 {
1211 quantum=(size_t) MagickMin(length-i,sizeof(buffer));
1212 (void) ReadBlobStream(image,quantum,buffer,&count);
1213 if (count <= 0)
1214 {
1215 if (count == 0)
1216 break; /* EOF is terminal, errno may hold a stale EINTR */
1217 count=0;
1218 if (errno != EINTR)
1219 break;
1220 }
1221 }
1222 return(i < (MagickSizeType) length ? MagickFalse : MagickTrue);
1223}
1224␌
1225/*
1226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1227% %
1228% %
1229% %
1230+ D u p l i c a t e s B l o b %
1231% %
1232% %
1233% %
1234%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1235%
1236% DuplicateBlob() duplicates a blob descriptor.
1237%
1238% The format of the DuplicateBlob method is:
1239%
1240% void DuplicateBlob(Image *image,const Image *duplicate)
1241%
1242% A description of each parameter follows:
1243%
1244% o image: the image.
1245%
1246% o duplicate: the duplicate image.
1247%
1248*/
1249MagickExport void DuplicateBlob(Image *image,const Image *duplicate)
1250{
1251 assert(image != (Image *) NULL);
1252 assert(image->signature == MagickCoreSignature);
1253 assert(duplicate != (Image *) NULL);
1254 assert(duplicate->signature == MagickCoreSignature);
1255 if (IsEventLogging() != MagickFalse)
1256 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1257 DestroyBlob(image);
1258 image->blob=ReferenceBlob(duplicate->blob);
1259}
1260␌
1261/*
1262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1263% %
1264% %
1265% %
1266+ E O F B l o b %
1267% %
1268% %
1269% %
1270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1271%
1272% EOFBlob() returns a non-zero value when EOF has been detected reading from
1273% a blob or file.
1274%
1275% The format of the EOFBlob method is:
1276%
1277% int EOFBlob(const Image *image)
1278%
1279% A description of each parameter follows:
1280%
1281% o image: the image.
1282%
1283*/
1284MagickExport int EOFBlob(const Image *image)
1285{
1286 BlobInfo
1287 *magick_restrict blob_info;
1288
1289 assert(image != (Image *) NULL);
1290 assert(image->signature == MagickCoreSignature);
1291 assert(image->blob != (BlobInfo *) NULL);
1292 assert(image->blob->type != UndefinedStream);
1293 if (IsEventLogging() != MagickFalse)
1294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1295 blob_info=image->blob;
1296 switch (blob_info->type)
1297 {
1298 case UndefinedStream:
1299 case StandardStream:
1300 break;
1301 case FileStream:
1302 case PipeStream:
1303 {
1304 blob_info->eof=feof(blob_info->file_info.file) != 0 ? MagickTrue :
1305 MagickFalse;
1306 break;
1307 }
1308 case ZipStream:
1309 {
1310#if defined(MAGICKCORE_ZLIB_DELEGATE)
1311 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
1312 MagickFalse;
1313#endif
1314 break;
1315 }
1316 case BZipStream:
1317 {
1318#if defined(MAGICKCORE_BZLIB_DELEGATE)
1319 int
1320 status;
1321
1322 status=0;
1323 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
1324 if (status == BZ_UNEXPECTED_EOF)
1325 blob_info->eof=MagickTrue;
1326#endif
1327 break;
1328 }
1329 case FifoStream:
1330 {
1331 blob_info->eof=MagickFalse;
1332 break;
1333 }
1334 case BlobStream:
1335 break;
1336 case CustomStream:
1337 break;
1338 }
1339 return((int) blob_info->eof);
1340}
1341␌
1342/*
1343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1344% %
1345% %
1346% %
1347+ E r r o r B l o b %
1348% %
1349% %
1350% %
1351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1352%
1353% ErrorBlob() returns a non-zero value when an error has been detected reading
1354% from a blob or file.
1355%
1356% The format of the ErrorBlob method is:
1357%
1358% int ErrorBlob(const Image *image)
1359%
1360% A description of each parameter follows:
1361%
1362% o image: the image.
1363%
1364*/
1365MagickExport int ErrorBlob(const Image *image)
1366{
1367 BlobInfo
1368 *magick_restrict blob_info;
1369
1370 assert(image != (Image *) NULL);
1371 assert(image->signature == MagickCoreSignature);
1372 assert(image->blob != (BlobInfo *) NULL);
1373 assert(image->blob->type != UndefinedStream);
1374 if (IsEventLogging() != MagickFalse)
1375 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1376 blob_info=image->blob;
1377 switch (blob_info->type)
1378 {
1379 case UndefinedStream:
1380 case StandardStream:
1381 break;
1382 case FileStream:
1383 case PipeStream:
1384 {
1385 blob_info->error=ferror(blob_info->file_info.file);
1386 break;
1387 }
1388 case ZipStream:
1389 {
1390#if defined(MAGICKCORE_ZLIB_DELEGATE)
1391 (void) gzerror(blob_info->file_info.gzfile,&blob_info->error);
1392#endif
1393 break;
1394 }
1395 case BZipStream:
1396 {
1397#if defined(MAGICKCORE_BZLIB_DELEGATE)
1398 (void) BZ2_bzerror(blob_info->file_info.bzfile,&blob_info->error);
1399#endif
1400 break;
1401 }
1402 case FifoStream:
1403 {
1404 blob_info->error=0;
1405 break;
1406 }
1407 case BlobStream:
1408 break;
1409 case CustomStream:
1410 break;
1411 }
1412 return(blob_info->error);
1413}
1414␌
1415/*
1416%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417% %
1418% %
1419% %
1420% F i l e T o B l o b %
1421% %
1422% %
1423% %
1424%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1425%
1426% FileToBlob() returns the contents of a file as a buffer terminated with
1427% the '\0' character. The length of the buffer (not including the extra
1428% terminating '\0' character) is returned via the 'length' parameter. Free
1429% the buffer with RelinquishMagickMemory().
1430%
1431% The format of the FileToBlob method is:
1432%
1433% void *FileToBlob(const char *filename,const size_t extent,
1434% size_t *length,ExceptionInfo *exception)
1435%
1436% A description of each parameter follows:
1437%
1438% o blob: FileToBlob() returns the contents of a file as a blob. If
1439% an error occurs NULL is returned.
1440%
1441% o filename: the filename.
1442%
1443% o extent: The maximum length of the blob.
1444%
1445% o length: On return, this reflects the actual length of the blob.
1446%
1447% o exception: return any errors or warnings in this structure.
1448%
1449*/
1450MagickExport void *FileToBlob(const char *filename,const size_t extent,
1451 size_t *length,ExceptionInfo *exception)
1452{
1453 int
1454 file;
1455
1456 MagickBooleanType
1457 status;
1458
1459 MagickOffsetType
1460 offset;
1461
1462 size_t
1463 i;
1464
1465 ssize_t
1466 count;
1467
1468 struct stat
1469 attributes;
1470
1471 unsigned char
1472 *blob;
1473
1474 void
1475 *map;
1476
1477 assert(filename != (const char *) NULL);
1478 assert(exception != (ExceptionInfo *) NULL);
1479 assert(exception->signature == MagickCoreSignature);
1480 if (IsEventLogging() != MagickFalse)
1481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1482 *length=0;
1483 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1484 ThrowPolicyException(filename,NULL);
1485 file=fileno(stdin);
1486 if (LocaleCompare(filename,"-") != 0)
1487 {
1488 int
1489 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1490
1491 status=GetPathAttributes(filename,&attributes);
1492 if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
1493 {
1494 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1495 return(NULL);
1496 }
1497 file=open_utf8(filename,flags,0);
1498 }
1499 if (file == -1)
1500 {
1501 ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
1502 return(NULL);
1503 }
1504 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1505 {
1506 file=close_utf8(file)-1;
1507 ThrowPolicyException(filename,NULL);
1508 }
1509 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
1510 count=0;
1511 if ((file == fileno(stdin)) || (offset < 0) ||
1512 (offset != (MagickOffsetType) ((ssize_t) offset)))
1513 {
1514 size_t
1515 quantum;
1516
1517 struct stat
1518 file_stats;
1519
1520 /*
1521 Stream is not seekable.
1522 */
1523 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
1524 quantum=(size_t) MagickMaxBufferExtent;
1525 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1526 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1527 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1528 for (i=0; blob != (unsigned char *) NULL; i+=(size_t) count)
1529 {
1530 count=MagickRead(file,blob+i,quantum);
1531 if (count <= 0)
1532 break;
1533 if (~i < ((size_t) count+quantum+1))
1534 {
1535 blob=(unsigned char *) RelinquishMagickMemory(blob);
1536 break;
1537 }
1538 blob=(unsigned char *) ResizeQuantumMemory(blob,i+(size_t) count+
1539 quantum+1,sizeof(*blob));
1540 if ((i+(size_t) count) >= extent)
1541 break;
1542 }
1543 if (LocaleCompare(filename,"-") != 0)
1544 file=close_utf8(file);
1545 if (blob == (unsigned char *) NULL)
1546 {
1547 (void) ThrowMagickException(exception,GetMagickModule(),
1548 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1549 return(NULL);
1550 }
1551 if ((count < 0) || (file == -1))
1552 {
1553 blob=(unsigned char *) RelinquishMagickMemory(blob);
1554 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1555 return(NULL);
1556 }
1557 *length=(size_t) MagickMin(i+(size_t) count,extent);
1558 blob[*length]='\0';
1559 return(blob);
1560 }
1561 *length=(size_t) MagickMin(offset,(MagickOffsetType)
1562 MagickMin(extent,(size_t) MAGICK_SSIZE_MAX));
1563 blob=(unsigned char *) NULL;
1564 if (~(*length) >= (MagickPathExtent-1))
1565 blob=(unsigned char *) AcquireQuantumMemory(*length+MagickPathExtent,
1566 sizeof(*blob));
1567 if (blob == (unsigned char *) NULL)
1568 {
1569 file=close_utf8(file);
1570 (void) ThrowMagickException(exception,GetMagickModule(),
1571 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1572 return(NULL);
1573 }
1574 map=MapBlob(file,ReadMode,0,*length);
1575 if (map != (unsigned char *) NULL)
1576 {
1577 (void) memcpy(blob,map,*length);
1578 (void) UnmapBlob(map,*length);
1579 }
1580 else
1581 {
1582 (void) lseek(file,0,SEEK_SET);
1583 for (i=0; i < *length; i+=(size_t) count)
1584 {
1585 count=MagickRead(file,blob+i,(size_t) MagickMin(*length-i,(size_t)
1586 MagickMaxBufferExtent));
1587 if (count <= 0)
1588 break;
1589 }
1590 if (i < *length)
1591 {
1592 file=close_utf8(file)-1;
1593 blob=(unsigned char *) RelinquishMagickMemory(blob);
1594 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1595 return(NULL);
1596 }
1597 }
1598 blob[*length]='\0';
1599 if (LocaleCompare(filename,"-") != 0)
1600 file=close_utf8(file);
1601 if (file == -1)
1602 {
1603 blob=(unsigned char *) RelinquishMagickMemory(blob);
1604 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1605 }
1606 return(blob);
1607}
1608␌
1609/*
1610%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611% %
1612% %
1613% %
1614% F i l e T o I m a g e %
1615% %
1616% %
1617% %
1618%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619%
1620% FileToImage() write the contents of a file to an image.
1621%
1622% The format of the FileToImage method is:
1623%
1624% MagickBooleanType FileToImage(Image *,const char *filename)
1625%
1626% A description of each parameter follows:
1627%
1628% o image: the image.
1629%
1630% o filename: the filename.
1631%
1632*/
1633
1634static inline ssize_t WriteBlobStream(Image *image,const size_t length,
1635 const void *magick_restrict data)
1636{
1637 BlobInfo
1638 *magick_restrict blob_info;
1639
1640 MagickSizeType
1641 extent;
1642
1643 unsigned char
1644 *magick_restrict q;
1645
1646 assert(image->blob != (BlobInfo *) NULL);
1647 assert(image->blob->type != UndefinedStream);
1648 assert(data != NULL);
1649 blob_info=image->blob;
1650 if (blob_info->type != BlobStream)
1651 return(WriteBlob(image,length,(const unsigned char *) data));
1652 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
1653 {
1654 errno=EOVERFLOW;
1655 return(0);
1656 }
1657 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
1658 if (extent >= blob_info->extent)
1659 {
1660 extent+=blob_info->quantum+length;
1661 if (blob_info->quantum <= (MAGICK_SIZE_MAX >> 1))
1662 blob_info->quantum<<=1;
1663 if (SetBlobExtent(image,extent) == MagickFalse)
1664 return(0);
1665 }
1666 q=blob_info->data+blob_info->offset;
1667 (void) memcpy(q,data,length);
1668 blob_info->offset+=(MagickOffsetType) length;
1669 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
1670 blob_info->length=(size_t) blob_info->offset;
1671 return((ssize_t) length);
1672}
1673
1674MagickExport MagickBooleanType FileToImage(Image *image,const char *filename,
1675 ExceptionInfo *exception)
1676{
1677 int
1678 file;
1679
1680 MagickBooleanType
1681 status;
1682
1683 size_t
1684 length,
1685 quantum;
1686
1687 ssize_t
1688 count;
1689
1690 struct stat
1691 file_stats;
1692
1693 unsigned char
1694 *blob;
1695
1696 assert(image != (const Image *) NULL);
1697 assert(image->signature == MagickCoreSignature);
1698 assert(filename != (const char *) NULL);
1699 if (IsEventLogging() != MagickFalse)
1700 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1701 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1702 ThrowPolicyException(filename,MagickFalse);
1703 file=fileno(stdin);
1704 if (LocaleCompare(filename,"-") != 0)
1705 {
1706 int
1707 flags = O_RDONLY | O_CLOEXEC | O_BINARY;
1708
1709 file=open_utf8(filename,flags,0);
1710 }
1711 if (file == -1)
1712 {
1713 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
1714 return(MagickFalse);
1715 }
1716 if (IsPathAuthorized(ReadPolicyRights,filename) == MagickFalse)
1717 {
1718 if (LocaleCompare(filename,"-") != 0)
1719 (void) close_utf8(file);
1720 ThrowPolicyException(filename,MagickFalse);
1721 }
1722 quantum=(size_t) MagickMaxBufferExtent;
1723 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1724 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1725 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1726 if (blob == (unsigned char *) NULL)
1727 {
1728 if (LocaleCompare(filename,"-") != 0)
1729 (void) close_utf8(file);
1730 ThrowFileException(exception,ResourceLimitError,"MemoryAllocationFailed",
1731 filename);
1732 return(MagickFalse);
1733 }
1734 status=MagickTrue;
1735 for ( ; ; )
1736 {
1737 count=MagickRead(file,blob,quantum);
1738 if (count <= 0)
1739 {
1740 if (count < 0)
1741 {
1742 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
1743 status=MagickFalse;
1744 }
1745 break;
1746 }
1747 length=(size_t) count;
1748 count=WriteBlobStream(image,length,blob);
1749 if (count != (ssize_t) length)
1750 {
1751 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1752 status=MagickFalse;
1753 break;
1754 }
1755 }
1756 if (LocaleCompare(filename,"-") != 0)
1757 {
1758 if (close_utf8(file) == -1)
1759 {
1760 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1761 status=MagickFalse;
1762 }
1763 }
1764 blob=(unsigned char *) RelinquishMagickMemory(blob);
1765 return(status);
1766}
1767␌
1768/*
1769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1770% %
1771% %
1772% %
1773+ G e t B l o b E r r o r %
1774% %
1775% %
1776% %
1777%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1778%
1779% GetBlobError() returns MagickTrue if the blob associated with the specified
1780% image encountered an error.
1781%
1782% The format of the GetBlobError method is:
1783%
1784% MagickBooleanType GetBlobError(const Image *image)
1785%
1786% A description of each parameter follows:
1787%
1788% o image: the image.
1789%
1790*/
1791MagickExport MagickBooleanType GetBlobError(const Image *image)
1792{
1793 assert(image != (const Image *) NULL);
1794 assert(image->signature == MagickCoreSignature);
1795 if (IsEventLogging() != MagickFalse)
1796 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1797 if ((image->blob->status != 0) && (image->blob->error_number != 0))
1798 errno=image->blob->error_number;
1799 return(image->blob->status == 0 ? MagickFalse : MagickTrue);
1800}
1801␌
1802/*
1803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1804% %
1805% %
1806% %
1807+ G e t B l o b F i l e H a n d l e %
1808% %
1809% %
1810% %
1811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1812%
1813% GetBlobFileHandle() returns the stdio file handle associated with the image
1814% blob. If the blob is not backed by a FILE object, NULL is returned.
1815%
1816% The format of the GetBlobFile method is:
1817%
1818% FILE *GetBlobFileHandle(const Image *image)
1819%
1820% A description of each parameter follows:
1821%
1822% o image: the image.
1823%
1824*/
1825MagickExport FILE *GetBlobFileHandle(const Image *image)
1826{
1827 assert(image != (const Image *) NULL);
1828 assert(image->signature == MagickCoreSignature);
1829 assert(image->blob != (BlobInfo *) NULL);
1830 assert(image->blob->signature == MagickCoreSignature);
1831 switch (image->blob->type)
1832 {
1833 case StandardStream:
1834 case FileStream:
1835 case PipeStream:
1836 return(image->blob->file_info.file);
1837 case UndefinedStream:
1838 case ZipStream:
1839 case BZipStream:
1840 case FifoStream:
1841 case BlobStream:
1842 case CustomStream:
1843 break;
1844 }
1845 return((FILE *) NULL);
1846}
1847␌
1848/*
1849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1850% %
1851% %
1852% %
1853+ G e t B l o b I n f o %
1854% %
1855% %
1856% %
1857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1858%
1859% GetBlobInfo() initializes the BlobInfo structure.
1860%
1861% The format of the GetBlobInfo method is:
1862%
1863% void GetBlobInfo(BlobInfo *blob_info)
1864%
1865% A description of each parameter follows:
1866%
1867% o blob_info: Specifies a pointer to a BlobInfo structure.
1868%
1869*/
1870MagickExport void GetBlobInfo(BlobInfo *blob_info)
1871{
1872 assert(blob_info != (BlobInfo *) NULL);
1873 (void) memset(blob_info,0,sizeof(*blob_info));
1874 blob_info->type=UndefinedStream;
1875 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1876 blob_info->properties.st_mtime=GetMagickTime();
1877 blob_info->properties.st_ctime=blob_info->properties.st_mtime;
1878 blob_info->debug=GetLogEventMask() & BlobEvent ? MagickTrue : MagickFalse;
1879 blob_info->reference_count=1;
1880 blob_info->semaphore=AcquireSemaphoreInfo();
1881 blob_info->signature=MagickCoreSignature;
1882}
1883␌
1884/*
1885%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1886% %
1887% %
1888% %
1889% G e t B l o b P r o p e r t i e s %
1890% %
1891% %
1892% %
1893%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1894%
1895% GetBlobProperties() returns information about an image blob.
1896%
1897% The format of the GetBlobProperties method is:
1898%
1899% const struct stat *GetBlobProperties(const Image *image)
1900%
1901% A description of each parameter follows:
1902%
1903% o image: the image.
1904%
1905*/
1906MagickExport const struct stat *GetBlobProperties(const Image *image)
1907{
1908 assert(image != (Image *) NULL);
1909 assert(image->signature == MagickCoreSignature);
1910 if (IsEventLogging() != MagickFalse)
1911 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1912 return(&image->blob->properties);
1913}
1914␌
1915/*
1916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1917% %
1918% %
1919% %
1920+ G e t B l o b S i z e %
1921% %
1922% %
1923% %
1924%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1925%
1926% GetBlobSize() returns the current length of the image file or blob; zero is
1927% returned if the size cannot be determined.
1928%
1929% The format of the GetBlobSize method is:
1930%
1931% MagickSizeType GetBlobSize(const Image *image)
1932%
1933% A description of each parameter follows:
1934%
1935% o image: the image.
1936%
1937*/
1938MagickExport MagickSizeType GetBlobSize(const Image *image)
1939{
1940 BlobInfo
1941 *magick_restrict blob_info;
1942
1943 MagickSizeType
1944 extent;
1945
1946 assert(image != (Image *) NULL);
1947 assert(image->signature == MagickCoreSignature);
1948 assert(image->blob != (BlobInfo *) NULL);
1949 if (IsEventLogging() != MagickFalse)
1950 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1951 blob_info=image->blob;
1952 extent=0;
1953 switch (blob_info->type)
1954 {
1955 case UndefinedStream:
1956 case StandardStream:
1957 {
1958 extent=blob_info->size;
1959 break;
1960 }
1961 case FileStream:
1962 {
1963 int
1964 file_descriptor;
1965
1966 extent=(MagickSizeType) blob_info->properties.st_size;
1967 if (extent == 0)
1968 extent=blob_info->size;
1969 file_descriptor=fileno(blob_info->file_info.file);
1970 if (file_descriptor == -1)
1971 break;
1972 if (fstat(file_descriptor,&blob_info->properties) == 0)
1973 extent=(MagickSizeType) blob_info->properties.st_size;
1974 break;
1975 }
1976 case PipeStream:
1977 {
1978 extent=blob_info->size;
1979 break;
1980 }
1981 case ZipStream:
1982 case BZipStream:
1983 {
1984 MagickBooleanType
1985 status;
1986
1987 status=GetPathAttributes(image->filename,&blob_info->properties);
1988 if (status != MagickFalse)
1989 extent=(MagickSizeType) blob_info->properties.st_size;
1990 break;
1991 }
1992 case FifoStream:
1993 break;
1994 case BlobStream:
1995 {
1996 extent=(MagickSizeType) blob_info->length;
1997 break;
1998 }
1999 case CustomStream:
2000 {
2001 if ((blob_info->custom_stream->teller != (CustomStreamTeller) NULL) &&
2002 (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL))
2003 {
2004 MagickOffsetType
2005 offset;
2006
2007 offset=blob_info->custom_stream->teller(
2008 blob_info->custom_stream->data);
2009 extent=(MagickSizeType) blob_info->custom_stream->seeker(0,SEEK_END,
2010 blob_info->custom_stream->data);
2011 (void) blob_info->custom_stream->seeker(offset,SEEK_SET,
2012 blob_info->custom_stream->data);
2013 }
2014 break;
2015 }
2016 }
2017 return(extent);
2018}
2019␌
2020/*
2021%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022% %
2023% %
2024% %
2025+ G e t B l o b S t r e a m D a t a %
2026% %
2027% %
2028% %
2029%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2030%
2031% GetBlobStreamData() returns the stream data for the image.
2032%
2033% The format of the GetBlobStreamData method is:
2034%
2035% void *GetBlobStreamData(const Image *image)
2036%
2037% A description of each parameter follows:
2038%
2039% o image: the image.
2040%
2041*/
2042MagickExport void *GetBlobStreamData(const Image *image)
2043{
2044 assert(image != (const Image *) NULL);
2045 assert(image->signature == MagickCoreSignature);
2046 return(image->blob->data);
2047}
2048␌
2049/*
2050%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2051% %
2052% %
2053% %
2054+ G e t B l o b S t r e a m H a n d l e r %
2055% %
2056% %
2057% %
2058%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2059%
2060% GetBlobStreamHandler() returns the stream handler for the image.
2061%
2062% The format of the GetBlobStreamHandler method is:
2063%
2064% StreamHandler GetBlobStreamHandler(const Image *image)
2065%
2066% A description of each parameter follows:
2067%
2068% o image: the image.
2069%
2070*/
2071MagickExport StreamHandler GetBlobStreamHandler(const Image *image)
2072{
2073 assert(image != (const Image *) NULL);
2074 assert(image->signature == MagickCoreSignature);
2075 if (IsEventLogging() != MagickFalse)
2076 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2077 return(image->blob->stream);
2078}
2079␌
2080/*
2081%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2082% %
2083% %
2084% %
2085% I m a g e T o B l o b %
2086% %
2087% %
2088% %
2089%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2090%
2091% ImageToBlob() implements direct to memory image formats. It returns the
2092% image as a formatted blob and its length. The magick member of the Image
2093% structure determines the format of the returned blob (GIF, JPEG, PNG,
2094% etc.). This method is the equivalent of WriteImage(), but writes the
2095% formatted "file" to a memory buffer rather than to an actual file.
2096%
2097% The format of the ImageToBlob method is:
2098%
2099% void *ImageToBlob(const ImageInfo *image_info,Image *image,
2100% size_t *length,ExceptionInfo *exception)
2101%
2102% A description of each parameter follows:
2103%
2104% o image_info: the image info.
2105%
2106% o image: the image.
2107%
2108% o length: return the actual length of the blob.
2109%
2110% o exception: return any errors or warnings in this structure.
2111%
2112*/
2113MagickExport void *ImageToBlob(const ImageInfo *image_info,
2114 Image *image,size_t *length,ExceptionInfo *exception)
2115{
2116 const MagickInfo
2117 *magick_info;
2118
2119 ImageInfo
2120 *blob_info;
2121
2122 MagickBooleanType
2123 status;
2124
2125 void
2126 *blob;
2127
2128 assert(image_info != (const ImageInfo *) NULL);
2129 assert(image_info->signature == MagickCoreSignature);
2130 assert(image != (Image *) NULL);
2131 assert(image->signature == MagickCoreSignature);
2132 assert(exception != (ExceptionInfo *) NULL);
2133 assert(exception->signature == MagickCoreSignature);
2134 if (IsEventLogging() != MagickFalse)
2135 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2136 image_info->filename);
2137 *length=0;
2138 blob=(unsigned char *) NULL;
2139 blob_info=CloneImageInfo(image_info);
2140 blob_info->adjoin=MagickFalse;
2141 (void) SetImageInfo(blob_info,1,exception);
2142 if (*blob_info->magick != '\0')
2143 (void) CopyMagickString(image->magick,blob_info->magick,MagickPathExtent);
2144 magick_info=GetMagickInfo(image->magick,exception);
2145 if (magick_info == (const MagickInfo *) NULL)
2146 {
2147 (void) ThrowMagickException(exception,GetMagickModule(),
2148 MissingDelegateError,"NoEncodeDelegateForThisImageFormat","`%s'",
2149 image->magick);
2150 blob_info=DestroyImageInfo(blob_info);
2151 return(blob);
2152 }
2153 (void) CopyMagickString(blob_info->magick,image->magick,MagickPathExtent);
2154 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2155 {
2156 /*
2157 Native blob support for this image format.
2158 */
2159 blob_info->length=0;
2160 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2161 sizeof(unsigned char));
2162 if (blob_info->blob == NULL)
2163 (void) ThrowMagickException(exception,GetMagickModule(),
2164 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2165 else
2166 {
2167 (void) CloseBlob(image);
2168 image->blob->exempt=MagickTrue;
2169 image->blob->extent=0;
2170 *image->filename='\0';
2171 status=WriteImage(blob_info,image,exception);
2172 SyncBlobStream(blob_info,image,image);
2173 *length=image->blob->length;
2174 blob=DetachBlob(image->blob);
2175 if (blob != (void *) NULL)
2176 {
2177 if (status == MagickFalse)
2178 blob=RelinquishMagickMemory(blob);
2179 else
2180 blob=ResizeQuantumMemory(blob,*length+1,sizeof(unsigned char));
2181 }
2182 else
2183 if ((status == MagickFalse) && (image->blob->extent == 0))
2184 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2185 }
2186 }
2187 else
2188 {
2189 char
2190 unique[MagickPathExtent];
2191
2192 int
2193 file;
2194
2195 /*
2196 Write file to disk in blob image format.
2197 */
2198 file=AcquireUniqueFileResource(unique);
2199 if (file == -1)
2200 {
2201 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
2202 image_info->filename);
2203 }
2204 else
2205 {
2206 blob_info->file=fdopen(file,"wb");
2207 if (blob_info->file != (FILE *) NULL)
2208 {
2209 (void) FormatLocaleString(image->filename,MagickPathExtent,
2210 "%s:%s",image->magick,unique);
2211 status=WriteImage(blob_info,image,exception);
2212 (void) fclose(blob_info->file);
2213 if (status != MagickFalse)
2214 blob=FileToBlob(unique,MAGICK_SIZE_MAX,length,exception);
2215 }
2216 (void) RelinquishUniqueFileResource(unique);
2217 }
2218 }
2219 blob_info=DestroyImageInfo(blob_info);
2220 return(blob);
2221}
2222␌
2223/*
2224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2225% %
2226% %
2227% %
2228+ I m a g e T o C u s t o m S t r e a m %
2229% %
2230% %
2231% %
2232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2233%
2234% ImageToCustomStream() is the equivalent of WriteImage(), but writes the
2235% formatted "file" to the custom stream rather than to an actual file.
2236%
2237% The format of the ImageToCustomStream method is:
2238%
2239% void ImageToCustomStream(const ImageInfo *image_info,Image *image,
2240% ExceptionInfo *exception)
2241%
2242% A description of each parameter follows:
2243%
2244% o image_info: the image info.
2245%
2246% o image: the image.
2247%
2248% o exception: return any errors or warnings in this structure.
2249%
2250*/
2251MagickExport void ImageToCustomStream(const ImageInfo *image_info,Image *image,
2252 ExceptionInfo *exception)
2253{
2254 const MagickInfo
2255 *magick_info;
2256
2257 ImageInfo
2258 *clone_info;
2259
2260 MagickBooleanType
2261 blob_support,
2262 status;
2263
2264 assert(image_info != (const ImageInfo *) NULL);
2265 assert(image_info->signature == MagickCoreSignature);
2266 assert(image != (Image *) NULL);
2267 assert(image->signature == MagickCoreSignature);
2268 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2269 assert(image_info->custom_stream->signature == MagickCoreSignature);
2270 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2271 assert(exception != (ExceptionInfo *) NULL);
2272 if (IsEventLogging() != MagickFalse)
2273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2274 image_info->filename);
2275 clone_info=CloneImageInfo(image_info);
2276 clone_info->adjoin=MagickFalse;
2277 (void) SetImageInfo(clone_info,1,exception);
2278 if (*clone_info->magick != '\0')
2279 (void) CopyMagickString(image->magick,clone_info->magick,MagickPathExtent);
2280 magick_info=GetMagickInfo(image->magick,exception);
2281 if (magick_info == (const MagickInfo *) NULL)
2282 {
2283 (void) ThrowMagickException(exception,GetMagickModule(),
2284 MissingDelegateError,"NoEncodeDelegateForThisImageFormat","`%s'",
2285 image->magick);
2286 clone_info=DestroyImageInfo(clone_info);
2287 return;
2288 }
2289 (void) CopyMagickString(clone_info->magick,image->magick,MagickPathExtent);
2290 blob_support=GetMagickBlobSupport(magick_info);
2291 if ((blob_support != MagickFalse) &&
2292 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2293 {
2294 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2295 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2296 blob_support=MagickFalse;
2297 }
2298 if (blob_support != MagickFalse)
2299 {
2300 /*
2301 Native blob support for this image format.
2302 */
2303 (void) CloseBlob(image);
2304 *image->filename='\0';
2305 if (WriteImage(clone_info,image,exception) == MagickFalse)
2306 {
2307 (void) ThrowMagickException(exception,GetMagickModule(),
2308 BlobError,"UnableToWriteBlob","`%s'",image_info->filename);
2309 }
2310 }
2311 else
2312 {
2313 char
2314 unique[MagickPathExtent];
2315
2316 int
2317 file;
2318
2319 unsigned char
2320 *blob;
2321
2322 /*
2323 Write file to disk in blob image format.
2324 */
2325 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2326 blob=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2327 sizeof(*blob));
2328 if (blob == (unsigned char *) NULL)
2329 {
2330 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
2331 image_info->filename);
2332 clone_info=DestroyImageInfo(clone_info);
2333 return;
2334 }
2335 file=AcquireUniqueFileResource(unique);
2336 if (file == -1)
2337 {
2338 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
2339 image_info->filename);
2340 blob=(unsigned char *) RelinquishMagickMemory(blob);
2341 clone_info=DestroyImageInfo(clone_info);
2342 return;
2343 }
2344 clone_info->file=fdopen(file,"wb+");
2345 if (clone_info->file != (FILE *) NULL)
2346 {
2347 ssize_t
2348 count;
2349
2350 (void) FormatLocaleString(image->filename,MagickPathExtent,"%s:%s",
2351 image->magick,unique);
2352 status=WriteImage(clone_info,image,exception);
2353 if (status != MagickFalse)
2354 {
2355 (void) fseek(clone_info->file,0,SEEK_SET);
2356 count=(ssize_t) MagickMaxBufferExtent;
2357 while (count == (ssize_t) MagickMaxBufferExtent)
2358 {
2359 count=(ssize_t) fread(blob,sizeof(*blob),MagickMaxBufferExtent,
2360 clone_info->file);
2361 if (image_info->custom_stream->writer(blob,(size_t) count,
2362 image_info->custom_stream->data) != count)
2363 {
2364 (void) ThrowMagickException(exception,GetMagickModule(),
2365 BlobError,"UnableToWriteBlob","`%s'",image_info->filename);
2366 break;
2367 }
2368 }
2369 }
2370 (void) fclose(clone_info->file);
2371 }
2372 blob=(unsigned char *) RelinquishMagickMemory(blob);
2373 (void) RelinquishUniqueFileResource(unique);
2374 }
2375 clone_info=DestroyImageInfo(clone_info);
2376}
2377␌
2378/*
2379%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2380% %
2381% %
2382% %
2383% I m a g e T o F i l e %
2384% %
2385% %
2386% %
2387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2388%
2389% ImageToFile() writes an image to a file. It returns MagickFalse if an error
2390% occurs otherwise MagickTrue.
2391%
2392% The format of the ImageToFile method is:
2393%
2394% MagickBooleanType ImageToFile(Image *image,char *filename,
2395% ExceptionInfo *exception)
2396%
2397% A description of each parameter follows:
2398%
2399% o image: the image.
2400%
2401% o filename: Write the image to this file.
2402%
2403% o exception: return any errors or warnings in this structure.
2404%
2405*/
2406MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
2407 ExceptionInfo *exception)
2408{
2409 int
2410 file;
2411
2412 const unsigned char
2413 *p;
2414
2415 size_t
2416 i;
2417
2418 size_t
2419 length,
2420 quantum;
2421
2422 ssize_t
2423 count;
2424
2425 struct stat
2426 file_stats;
2427
2428 unsigned char
2429 *buffer;
2430
2431 assert(image != (Image *) NULL);
2432 assert(image->signature == MagickCoreSignature);
2433 assert(image->blob != (BlobInfo *) NULL);
2434 assert(image->blob->type != UndefinedStream);
2435 assert(filename != (const char *) NULL);
2436 if (IsEventLogging() != MagickFalse)
2437 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
2438 if (*filename == '\0')
2439 file=AcquireUniqueFileResource(filename);
2440 else
2441 if (LocaleCompare(filename,"-") == 0)
2442 file=fileno(stdout);
2443 else
2444 file=open_utf8(filename,O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL | O_BINARY,P_MODE);
2445 if (file == -1)
2446 {
2447 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
2448 return(MagickFalse);
2449 }
2450 quantum=(size_t) MagickMaxBufferExtent;
2451 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2452 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2453 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
2454 if (buffer == (unsigned char *) NULL)
2455 {
2456 file=close_utf8(file)-1;
2457 (void) ThrowMagickException(exception,GetMagickModule(),
2458 ResourceLimitError,"MemoryAllocationError","`%s'",filename);
2459 return(MagickFalse);
2460 }
2461 length=0;
2462 p=(const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2463 for (i=0; count > 0; )
2464 {
2465 length=(size_t) count;
2466 for (i=0; i < length; i+=(size_t) count)
2467 {
2468 count=MagickWrite(file,p+i,(size_t) (length-i));
2469 if (count <= 0)
2470 break;
2471 }
2472 if (i < length)
2473 break;
2474 p=(const unsigned char *) ReadBlobStream(image,quantum,buffer,&count);
2475 }
2476 if (LocaleCompare(filename,"-") != 0)
2477 file=close_utf8(file);
2478 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2479 if ((file == -1) || (i < length))
2480 {
2481 if (file != -1)
2482 file=close_utf8(file);
2483 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
2484 return(MagickFalse);
2485 }
2486 return(MagickTrue);
2487}
2488␌
2489/*
2490%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2491% %
2492% %
2493% %
2494% I m a g e s T o B l o b %
2495% %
2496% %
2497% %
2498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2499%
2500% ImagesToBlob() implements direct to memory image formats. It returns the
2501% image sequence as a blob and its length. The magick member of the ImageInfo
2502% structure determines the format of the returned blob (GIF, JPEG, PNG, etc.)
2503%
2504% Note, some image formats do not permit multiple images to the same image
2505% stream (e.g. JPEG). in this instance, just the first image of the
2506% sequence is returned as a blob.
2507%
2508% The format of the ImagesToBlob method is:
2509%
2510% void *ImagesToBlob(const ImageInfo *image_info,Image *images,
2511% size_t *length,ExceptionInfo *exception)
2512%
2513% A description of each parameter follows:
2514%
2515% o image_info: the image info.
2516%
2517% o images: the image list.
2518%
2519% o length: return the actual length of the blob.
2520%
2521% o exception: return any errors or warnings in this structure.
2522%
2523*/
2524MagickExport void *ImagesToBlob(const ImageInfo *image_info,Image *images,
2525 size_t *length,ExceptionInfo *exception)
2526{
2527 const MagickInfo
2528 *magick_info;
2529
2530 ImageInfo
2531 *blob_info;
2532
2533 MagickBooleanType
2534 status;
2535
2536 void
2537 *blob;
2538
2539 assert(image_info != (const ImageInfo *) NULL);
2540 assert(image_info->signature == MagickCoreSignature);
2541 assert(images != (Image *) NULL);
2542 assert(images->signature == MagickCoreSignature);
2543 assert(exception != (ExceptionInfo *) NULL);
2544 if (IsEventLogging() != MagickFalse)
2545 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2546 image_info->filename);
2547 *length=0;
2548 blob=(unsigned char *) NULL;
2549 blob_info=CloneImageInfo(image_info);
2550 (void) SetImageInfo(blob_info,(unsigned int) GetImageListLength(images),
2551 exception);
2552 if (*blob_info->magick != '\0')
2553 (void) CopyMagickString(images->magick,blob_info->magick,MagickPathExtent);
2554 magick_info=GetMagickInfo(images->magick,exception);
2555 if (magick_info == (const MagickInfo *) NULL)
2556 {
2557 (void) ThrowMagickException(exception,GetMagickModule(),
2558 MissingDelegateError,"NoEncodeDelegateForThisImageFormat","`%s'",
2559 images->magick);
2560 blob_info=DestroyImageInfo(blob_info);
2561 return(blob);
2562 }
2563 if (GetMagickAdjoin(magick_info) == MagickFalse)
2564 {
2565 blob_info=DestroyImageInfo(blob_info);
2566 return(ImageToBlob(image_info,images,length,exception));
2567 }
2568 (void) CopyMagickString(blob_info->magick,images->magick,MagickPathExtent);
2569 if (GetMagickBlobSupport(magick_info) != MagickFalse)
2570 {
2571 /*
2572 Native blob support for this images format.
2573 */
2574 blob_info->length=0;
2575 blob_info->blob=AcquireQuantumMemory(MagickMaxBlobExtent,
2576 sizeof(unsigned char));
2577 if (blob_info->blob == (void *) NULL)
2578 (void) ThrowMagickException(exception,GetMagickModule(),
2579 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
2580 else
2581 {
2582 (void) CloseBlob(images);
2583 images->blob->exempt=MagickTrue;
2584 images->blob->extent=0;
2585 *images->filename='\0';
2586 status=WriteImages(blob_info,images,images->filename,exception);
2587 SyncBlobStream(blob_info,images,images);
2588 *length=images->blob->length;
2589 blob=DetachBlob(images->blob);
2590 if (blob != (void *) NULL)
2591 {
2592 if (status == MagickFalse)
2593 blob=RelinquishMagickMemory(blob);
2594 else
2595 blob=ResizeQuantumMemory(blob,*length+1,sizeof(unsigned char));
2596 }
2597 else if ((status == MagickFalse) && (images->blob->extent == 0))
2598 blob_info->blob=RelinquishMagickMemory(blob_info->blob);
2599 }
2600 }
2601 else
2602 {
2603 char
2604 filename[MagickPathExtent],
2605 unique[MagickPathExtent];
2606
2607 int
2608 file;
2609
2610 /*
2611 Write file to disk in blob images format.
2612 */
2613 file=AcquireUniqueFileResource(unique);
2614 if (file == -1)
2615 {
2616 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
2617 image_info->filename);
2618 }
2619 else
2620 {
2621 blob_info->file=fdopen(file,"wb");
2622 if (blob_info->file != (FILE *) NULL)
2623 {
2624 (void) FormatLocaleString(filename,MagickPathExtent,"%s:%s",
2625 images->magick,unique);
2626 status=WriteImages(blob_info,images,filename,exception);
2627 (void) fclose(blob_info->file);
2628 if (status != MagickFalse)
2629 blob=FileToBlob(unique,MAGICK_SIZE_MAX,length,exception);
2630 }
2631 (void) RelinquishUniqueFileResource(unique);
2632 }
2633 }
2634 blob_info=DestroyImageInfo(blob_info);
2635 return(blob);
2636}
2637␌
2638/*
2639%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2640% %
2641% %
2642% %
2643+ I m a g e s T o C u s t o m B l o b %
2644% %
2645% %
2646% %
2647%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2648%
2649% ImagesToCustomStream() is the equivalent of WriteImages(), but writes the
2650% formatted "file" to the custom stream rather than to an actual file.
2651%
2652% The format of the ImageToCustomStream method is:
2653%
2654% void ImagesToCustomStream(const ImageInfo *image_info,Image *images,
2655% ExceptionInfo *exception)
2656%
2657% A description of each parameter follows:
2658%
2659% o image_info: the image info.
2660%
2661% o images: the image list.
2662%
2663% o exception: return any errors or warnings in this structure.
2664%
2665*/
2666MagickExport void ImagesToCustomStream(const ImageInfo *image_info,
2667 Image *images,ExceptionInfo *exception)
2668{
2669 const MagickInfo
2670 *magick_info;
2671
2672 ImageInfo
2673 *clone_info;
2674
2675 MagickBooleanType
2676 blob_support,
2677 status;
2678
2679 assert(image_info != (const ImageInfo *) NULL);
2680 assert(image_info->signature == MagickCoreSignature);
2681 assert(images != (Image *) NULL);
2682 assert(images->signature == MagickCoreSignature);
2683 assert(image_info->custom_stream != (CustomStreamInfo *) NULL);
2684 assert(image_info->custom_stream->signature == MagickCoreSignature);
2685 assert(image_info->custom_stream->writer != (CustomStreamHandler) NULL);
2686 assert(exception != (ExceptionInfo *) NULL);
2687 if (IsEventLogging() != MagickFalse)
2688 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2689 image_info->filename);
2690 clone_info=CloneImageInfo(image_info);
2691 (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(images),
2692 exception);
2693 if (*clone_info->magick != '\0')
2694 (void) CopyMagickString(images->magick,clone_info->magick,MagickPathExtent);
2695 magick_info=GetMagickInfo(images->magick,exception);
2696 if (magick_info == (const MagickInfo *) NULL)
2697 {
2698 (void) ThrowMagickException(exception,GetMagickModule(),
2699 MissingDelegateError,"NoEncodeDelegateForThisImageFormat","`%s'",
2700 images->magick);
2701 clone_info=DestroyImageInfo(clone_info);
2702 return;
2703 }
2704 (void) CopyMagickString(clone_info->magick,images->magick,MagickPathExtent);
2705 blob_support=GetMagickBlobSupport(magick_info);
2706 if ((blob_support != MagickFalse) &&
2707 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
2708 {
2709 if ((clone_info->custom_stream->seeker == (CustomStreamSeeker) NULL) ||
2710 (clone_info->custom_stream->teller == (CustomStreamTeller) NULL))
2711 blob_support=MagickFalse;
2712 }
2713 if (blob_support != MagickFalse)
2714 {
2715 /*
2716 Native blob support for this image format.
2717 */
2718 (void) CloseBlob(images);
2719 *images->filename='\0';
2720 (void) WriteImages(clone_info,images,images->filename,exception);
2721 }
2722 else
2723 {
2724 char
2725 filename[MagickPathExtent],
2726 unique[MagickPathExtent];
2727
2728 int
2729 file;
2730
2731 unsigned char
2732 *blob;
2733
2734 /*
2735 Write file to disk in blob image format.
2736 */
2737 clone_info->custom_stream=(CustomStreamInfo *) NULL;
2738 blob=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
2739 sizeof(*blob));
2740 if (blob == (unsigned char *) NULL)
2741 {
2742 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
2743 image_info->filename);
2744 clone_info=DestroyImageInfo(clone_info);
2745 return;
2746 }
2747 file=AcquireUniqueFileResource(unique);
2748 if (file == -1)
2749 {
2750 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
2751 image_info->filename);
2752 blob=(unsigned char *) RelinquishMagickMemory(blob);
2753 clone_info=DestroyImageInfo(clone_info);
2754 return;
2755 }
2756 clone_info->file=fdopen(file,"wb+");
2757 if (clone_info->file != (FILE *) NULL)
2758 {
2759 ssize_t
2760 count;
2761
2762 (void) FormatLocaleString(filename,MagickPathExtent,"%s:%s",
2763 images->magick,unique);
2764 status=WriteImages(clone_info,images,filename,exception);
2765 if (status != MagickFalse)
2766 {
2767 (void) fseek(clone_info->file,0,SEEK_SET);
2768 count=(ssize_t) MagickMaxBufferExtent;
2769 while (count == (ssize_t) MagickMaxBufferExtent)
2770 {
2771 count=(ssize_t) fread(blob,sizeof(*blob),MagickMaxBufferExtent,
2772 clone_info->file);
2773 (void) image_info->custom_stream->writer(blob,(size_t) count,
2774 image_info->custom_stream->data);
2775 }
2776 }
2777 (void) fclose(clone_info->file);
2778 }
2779 blob=(unsigned char *) RelinquishMagickMemory(blob);
2780 (void) RelinquishUniqueFileResource(unique);
2781 }
2782 clone_info=DestroyImageInfo(clone_info);
2783}
2784␌
2785/*
2786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2787% %
2788% %
2789% %
2790% I n j e c t I m a g e B l o b %
2791% %
2792% %
2793% %
2794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2795%
2796% InjectImageBlob() injects the image with a copy of itself in the specified
2797% format (e.g. inject JPEG into a PDF image).
2798%
2799% The format of the InjectImageBlob method is:
2800%
2801% MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
2802% Image *image,Image *inject_image,const char *format,
2803% ExceptionInfo *exception)
2804%
2805% A description of each parameter follows:
2806%
2807% o image_info: the image info..
2808%
2809% o image: the image.
2810%
2811% o inject_image: inject into the image stream.
2812%
2813% o format: the image format.
2814%
2815% o exception: return any errors or warnings in this structure.
2816%
2817*/
2818MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
2819 Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
2820{
2821 char
2822 filename[MagickPathExtent];
2823
2824 FILE
2825 *unique_file;
2826
2827 Image
2828 *byte_image;
2829
2830 ImageInfo
2831 *write_info;
2832
2833 int
2834 file;
2835
2836 MagickBooleanType
2837 status;
2838
2839 size_t
2840 quantum;
2841
2842 struct stat
2843 file_stats;
2844
2845 unsigned char
2846 *buffer;
2847
2848 assert(image_info != (ImageInfo *) NULL);
2849 assert(image_info->signature == MagickCoreSignature);
2850 assert(image != (Image *) NULL);
2851 assert(image->signature == MagickCoreSignature);
2852 assert(inject_image != (Image *) NULL);
2853 assert(inject_image->signature == MagickCoreSignature);
2854 assert(exception != (ExceptionInfo *) NULL);
2855 if (IsEventLogging() != MagickFalse)
2856 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2857 unique_file=(FILE *) NULL;
2858 file=AcquireUniqueFileResource(filename);
2859 if (file != -1)
2860 unique_file=fdopen(file,"wb");
2861 if ((file == -1) || (unique_file == (FILE *) NULL))
2862 {
2863 if (file != -1)
2864 {
2865 (void) close_utf8(file);
2866 (void) RelinquishUniqueFileResource(filename);
2867 }
2868 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
2869 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2870 image->filename);
2871 return(MagickFalse);
2872 }
2873 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
2874 if (byte_image == (Image *) NULL)
2875 {
2876 (void) fclose(unique_file);
2877 (void) RelinquishUniqueFileResource(filename);
2878 return(MagickFalse);
2879 }
2880 (void) FormatLocaleString(byte_image->filename,MagickPathExtent,"%s:%s",
2881 format,filename);
2882 DestroyBlob(byte_image);
2883 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
2884 write_info=CloneImageInfo(image_info);
2885 SetImageInfoFile(write_info,unique_file);
2886 status=WriteImage(write_info,byte_image,exception);
2887 write_info=DestroyImageInfo(write_info);
2888 byte_image=DestroyImage(byte_image);
2889 (void) fclose(unique_file);
2890 if (status == MagickFalse)
2891 {
2892 (void) RelinquishUniqueFileResource(filename);
2893 return(MagickFalse);
2894 }
2895 file=open_utf8(filename,O_RDONLY | O_CLOEXEC | O_BINARY,0);
2896 if (file == -1)
2897 {
2898 (void) RelinquishUniqueFileResource(filename);
2899 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2900 image_info->filename);
2901 return(MagickFalse);
2902 }
2903 quantum=(size_t) MagickMaxBufferExtent;
2904 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2905 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
2906 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
2907 if (buffer == (unsigned char *) NULL)
2908 {
2909 (void) close_utf8(file);
2910 (void) RelinquishUniqueFileResource(filename);
2911 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2912 image->filename);
2913 }
2914 status=MagickTrue;
2915 for ( ; ; )
2916 {
2917 ssize_t
2918 count;
2919
2920 count=read(file,buffer,quantum);
2921 if (count < 0)
2922 {
2923 if (errno == EINTR)
2924 continue;
2925 status=MagickFalse;
2926 break;
2927 }
2928 if (count == 0)
2929 break;
2930 if (WriteBlobStream(image,(size_t) count,buffer) != count)
2931 {
2932 status=MagickFalse;
2933 break;
2934 }
2935 }
2936 if (close_utf8(file) == -1)
2937 {
2938 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",filename);
2939 status=MagickFalse;
2940 }
2941 (void) RelinquishUniqueFileResource(filename);
2942 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2943 return(status);
2944}
2945␌
2946/*
2947%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2948% %
2949% %
2950% %
2951% I s B l o b E x e m p t %
2952% %
2953% %
2954% %
2955%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2956%
2957% IsBlobExempt() returns true if the blob is exempt.
2958%
2959% The format of the IsBlobExempt method is:
2960%
2961% MagickBooleanType IsBlobExempt(const Image *image)
2962%
2963% A description of each parameter follows:
2964%
2965% o image: the image.
2966%
2967*/
2968MagickExport MagickBooleanType IsBlobExempt(const Image *image)
2969{
2970 assert(image != (const Image *) NULL);
2971 assert(image->signature == MagickCoreSignature);
2972 if (IsEventLogging() != MagickFalse)
2973 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2974 return(image->blob->exempt);
2975}
2976␌
2977/*
2978%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2979% %
2980% %
2981% %
2982% I s B l o b S e e k a b l e %
2983% %
2984% %
2985% %
2986%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2987%
2988% IsBlobSeekable() returns true if the blob is seekable.
2989%
2990% The format of the IsBlobSeekable method is:
2991%
2992% MagickBooleanType IsBlobSeekable(const Image *image)
2993%
2994% A description of each parameter follows:
2995%
2996% o image: the image.
2997%
2998*/
2999MagickExport MagickBooleanType IsBlobSeekable(const Image *image)
3000{
3001 BlobInfo
3002 *magick_restrict blob_info;
3003
3004 assert(image != (const Image *) NULL);
3005 assert(image->signature == MagickCoreSignature);
3006 if (IsEventLogging() != MagickFalse)
3007 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3008 blob_info=image->blob;
3009 switch (blob_info->type)
3010 {
3011 case BlobStream:
3012 return(MagickTrue);
3013 case FileStream:
3014 {
3015 int
3016 status;
3017
3018 if (blob_info->file_info.file == (FILE *) NULL)
3019 return(MagickFalse);
3020 status=fseek(blob_info->file_info.file,0,SEEK_CUR);
3021 return(status == -1 ? MagickFalse : MagickTrue);
3022 }
3023 case ZipStream:
3024 {
3025#if defined(MAGICKCORE_ZLIB_DELEGATE)
3026 MagickOffsetType
3027 offset;
3028
3029 if (blob_info->file_info.gzfile == (gzFile) NULL)
3030 return(MagickFalse);
3031 offset=gzseek(blob_info->file_info.gzfile,0,SEEK_CUR);
3032 return(offset < 0 ? MagickFalse : MagickTrue);
3033#else
3034 break;
3035#endif
3036 }
3037 case UndefinedStream:
3038 case BZipStream:
3039 case FifoStream:
3040 case PipeStream:
3041 case StandardStream:
3042 break;
3043 case CustomStream:
3044 {
3045 if ((blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL) &&
3046 (blob_info->custom_stream->teller != (CustomStreamTeller) NULL))
3047 return(MagickTrue);
3048 break;
3049 }
3050 default:
3051 break;
3052 }
3053 return(MagickFalse);
3054}
3055␌
3056/*
3057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3058% %
3059% %
3060% %
3061% I s B l o b T e m p o r a r y %
3062% %
3063% %
3064% %
3065%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3066%
3067% IsBlobTemporary() returns true if the blob is temporary.
3068%
3069% The format of the IsBlobTemporary method is:
3070%
3071% MagickBooleanType IsBlobTemporary(const Image *image)
3072%
3073% A description of each parameter follows:
3074%
3075% o image: the image.
3076%
3077*/
3078MagickExport MagickBooleanType IsBlobTemporary(const Image *image)
3079{
3080 assert(image != (const Image *) NULL);
3081 assert(image->signature == MagickCoreSignature);
3082 if (IsEventLogging() != MagickFalse)
3083 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3084 return(image->blob->temporary);
3085}
3086␌
3087/*
3088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3089% %
3090% %
3091% %
3092+ M a p B l o b %
3093% %
3094% %
3095% %
3096%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3097%
3098% MapBlob() creates a mapping from a file to a binary large object.
3099%
3100% The format of the MapBlob method is:
3101%
3102% void *MapBlob(int file,const MapMode mode,const MagickOffsetType offset,
3103% const size_t length)
3104%
3105% A description of each parameter follows:
3106%
3107% o file: map this file descriptor.
3108%
3109% o mode: ReadMode, WriteMode, or IOMode.
3110%
3111% o offset: starting at this offset within the file.
3112%
3113% o length: the length of the mapping is returned in this pointer.
3114%
3115*/
3116MagickExport void *MapBlob(int file,const MapMode mode,
3117 const MagickOffsetType offset,const size_t length)
3118{
3119#if defined(MAGICKCORE_HAVE_MMAP)
3120 int
3121 flags,
3122 protection;
3123
3124 void
3125 *map;
3126
3127 /*
3128 Map file.
3129 */
3130 flags=0;
3131 if (file == -1)
3132#if defined(MAP_ANONYMOUS)
3133 flags|=MAP_ANONYMOUS;
3134#else
3135 return(NULL);
3136#endif
3137 switch (mode)
3138 {
3139 case ReadMode:
3140 default:
3141 {
3142 protection=PROT_READ;
3143 flags|=MAP_PRIVATE;
3144 break;
3145 }
3146 case WriteMode:
3147 {
3148 protection=PROT_WRITE;
3149 flags|=MAP_SHARED;
3150 break;
3151 }
3152 case IOMode:
3153 {
3154 protection=PROT_READ | PROT_WRITE;
3155 flags|=MAP_SHARED;
3156 break;
3157 }
3158 }
3159#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
3160 map=mmap((char *) NULL,length,protection,flags,file,offset);
3161#else
3162 map=mmap((char *) NULL,length,protection,flags | MAP_HUGETLB,file,offset);
3163 if (map == MAP_FAILED)
3164 map=mmap((char *) NULL,length,protection,flags,file,offset);
3165#endif
3166 if (map == MAP_FAILED)
3167 return(NULL);
3168 return(map);
3169#else
3170 (void) file;
3171 (void) mode;
3172 (void) offset;
3173 (void) length;
3174 return(NULL);
3175#endif
3176}
3177␌
3178/*
3179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3180% %
3181% %
3182% %
3183+ M S B O r d e r L o n g %
3184% %
3185% %
3186% %
3187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3188%
3189% MSBOrderLong() converts a least-significant byte first buffer of integers to
3190% most-significant byte first.
3191%
3192% The format of the MSBOrderLong method is:
3193%
3194% void MSBOrderLong(unsigned char *buffer,const size_t length)
3195%
3196% A description of each parameter follows.
3197%
3198% o buffer: Specifies a pointer to a buffer of integers.
3199%
3200% o length: Specifies the length of the buffer.
3201%
3202*/
3203MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
3204{
3205 int
3206 c;
3207
3208 unsigned char
3209 *p,
3210 *q;
3211
3212 assert(buffer != (unsigned char *) NULL);
3213 q=buffer+length;
3214 while (buffer < q)
3215 {
3216 p=buffer+3;
3217 c=(int) (*p);
3218 *p=(*buffer);
3219 *buffer++=(unsigned char) c;
3220 p=buffer+1;
3221 c=(int) (*p);
3222 *p=(*buffer);
3223 *buffer++=(unsigned char) c;
3224 buffer+=2;
3225 }
3226}
3227␌
3228/*
3229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3230% %
3231% %
3232% %
3233+ M S B O r d e r S h o r t %
3234% %
3235% %
3236% %
3237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3238%
3239% MSBOrderShort() converts a least-significant byte first buffer of integers
3240% to most-significant byte first.
3241%
3242% The format of the MSBOrderShort method is:
3243%
3244% void MSBOrderShort(unsigned char *p,const size_t length)
3245%
3246% A description of each parameter follows.
3247%
3248% o p: Specifies a pointer to a buffer of integers.
3249%
3250% o length: Specifies the length of the buffer.
3251%
3252*/
3253MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
3254{
3255 int
3256 c;
3257
3258 unsigned char
3259 *q;
3260
3261 assert(p != (unsigned char *) NULL);
3262 q=p+length;
3263 while (p < q)
3264 {
3265 c=(int) (*p);
3266 *p=(*(p+1));
3267 p++;
3268 *p++=(unsigned char) c;
3269 }
3270}
3271␌
3272/*
3273%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3274% %
3275% %
3276% %
3277+ O p e n B l o b %
3278% %
3279% %
3280% %
3281%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3282%
3283% OpenBlob() opens a file associated with the image. A file name of '-' sets
3284% the file to stdin for type 'r' and stdout for type 'w'. If the filename
3285% suffix is '.gz', the image is decompressed for type 'r' and compressed for
3286% type 'w'. If the filename prefix is '|', it is piped to or from a system
3287% command.
3288%
3289% The format of the OpenBlob method is:
3290%
3291% MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
3292% const BlobMode mode,ExceptionInfo *exception)
3293%
3294% A description of each parameter follows:
3295%
3296% o image_info: the image info.
3297%
3298% o image: the image.
3299%
3300% o mode: the mode for opening the file.
3301%
3302*/
3303
3304static inline MagickBooleanType SetStreamBuffering(const ImageInfo *image_info,
3305 const BlobInfo *blob_info)
3306{
3307 const char
3308 *option;
3309
3310 int
3311 status;
3312
3313 size_t
3314 size;
3315
3316 size=MagickMinBufferExtent;
3317 option=GetImageOption(image_info,"stream:buffer-size");
3318 if (option != (const char *) NULL)
3319 size=StringToUnsignedLong(option);
3320 status=setvbuf(blob_info->file_info.file,(char *) NULL,size == 0 ?
3321 _IONBF : _IOFBF,size);
3322 return(status == 0 ? MagickTrue : MagickFalse);
3323}
3324
3325#if defined(MAGICKCORE_ZLIB_DELEGATE)
3326static inline gzFile gzopen_utf8(const char *path,const char *mode)
3327{
3328#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
3329 return(gzopen(path,mode));
3330#else
3331 gzFile
3332 file;
3333
3334 wchar_t
3335 *path_wide;
3336
3337 path_wide=NTCreateWidePath(path);
3338 if (path_wide == (wchar_t *) NULL)
3339 return((gzFile) NULL);
3340 file=gzopen_w(path_wide,mode);
3341 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
3342 return(file);
3343#endif
3344}
3345#endif
3346
3347MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
3348 Image *image,const BlobMode mode,ExceptionInfo *exception)
3349{
3350 BlobInfo
3351 *magick_restrict blob_info;
3352
3353 char
3354 extension[MagickPathExtent],
3355 filename[MagickPathExtent];
3356
3357 const char
3358 *type;
3359
3360 int
3361 flags = O_RDONLY | O_CLOEXEC;
3362
3363 MagickBooleanType
3364 status;
3365
3366 PolicyRights
3367 rights;
3368
3369 assert(image_info != (ImageInfo *) NULL);
3370 assert(image_info->signature == MagickCoreSignature);
3371 assert(image != (Image *) NULL);
3372 assert(image->signature == MagickCoreSignature);
3373 if (IsEventLogging() != MagickFalse)
3374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3375 image_info->filename);
3376 blob_info=image->blob;
3377 if (image_info->blob != (void *) NULL)
3378 {
3379 if (image_info->stream != (StreamHandler) NULL)
3380 blob_info->stream=(StreamHandler) image_info->stream;
3381 AttachBlob(blob_info,image_info->blob,image_info->length);
3382 return(MagickTrue);
3383 }
3384 if ((image_info->custom_stream != (CustomStreamInfo *) NULL) &&
3385 (*image->filename == '\0'))
3386 {
3387 blob_info->type=CustomStream;
3388 blob_info->custom_stream=image_info->custom_stream;
3389 if (blob_info->custom_stream->seeker != (CustomStreamSeeker) NULL)
3390 blob_info->custom_stream->seeker(0,SEEK_SET,
3391 blob_info->custom_stream->data);
3392 return(MagickTrue);
3393 }
3394 (void) DetachBlob(blob_info);
3395 blob_info->mode=mode;
3396 switch (mode)
3397 {
3398 case ReadBlobMode:
3399 {
3400 flags=O_RDONLY | O_CLOEXEC;
3401 type="r";
3402 break;
3403 }
3404 case ReadBinaryBlobMode:
3405 {
3406 flags=O_RDONLY | O_CLOEXEC | O_BINARY;
3407 type="rb";
3408 break;
3409 }
3410 case WriteBlobMode:
3411 {
3412 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC;
3413 type="w";
3414 break;
3415 }
3416 case WriteBinaryBlobMode:
3417 {
3418 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_TRUNC | O_BINARY;
3419 type="w+b";
3420 break;
3421 }
3422 case AppendBlobMode:
3423 {
3424 flags=O_WRONLY | O_CLOEXEC | O_CREAT | O_APPEND;
3425 type="a";
3426 break;
3427 }
3428 case AppendBinaryBlobMode:
3429 {
3430 flags=O_RDWR | O_CLOEXEC | O_CREAT | O_APPEND | O_BINARY;
3431 type="a+b";
3432 break;
3433 }
3434 default:
3435 {
3436 flags=O_RDONLY | O_CLOEXEC;
3437 type="r";
3438 break;
3439 }
3440 }
3441 if (*type != 'r')
3442 blob_info->synchronize=image_info->synchronize;
3443 if (image_info->stream != (StreamHandler) NULL)
3444 {
3445 blob_info->stream=image_info->stream;
3446 if (*type == 'w')
3447 {
3448 blob_info->type=FifoStream;
3449 return(MagickTrue);
3450 }
3451 }
3452 /*
3453 Open image file.
3454 */
3455 *filename='\0';
3456 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3457 rights=ReadPolicyRights;
3458 if (*type == 'w')
3459 rights=WritePolicyRights;
3460 if (IsPathAuthorized(rights,filename) == MagickFalse)
3461 ThrowPolicyException(filename,MagickFalse);
3462 if ((LocaleCompare(filename,"-") == 0) ||
3463 ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
3464 {
3465 blob_info->file_info.file=(*type == 'r') ? stdin : stdout;
3466#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3467 if (strchr(type,'b') != (char *) NULL)
3468 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3469#endif
3470 blob_info->type=StandardStream;
3471 blob_info->exempt=MagickTrue;
3472 return(SetStreamBuffering(image_info,blob_info));
3473 }
3474 if ((LocaleNCompare(filename,"fd:",3) == 0) &&
3475 (IsGeometry(filename+3) != MagickFalse))
3476 {
3477 char
3478 fileMode[2];
3479
3480 *fileMode=(*type);
3481 fileMode[1]='\0';
3482 blob_info->file_info.file=fdopen(StringToInteger(filename+3),fileMode);
3483 if (blob_info->file_info.file == (FILE *) NULL)
3484 {
3485 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
3486 return(MagickFalse);
3487 }
3488#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__)
3489 if (strchr(type,'b') != (char *) NULL)
3490 (void) setmode(fileno(blob_info->file_info.file),_O_BINARY);
3491#endif
3492 blob_info->type=FileStream;
3493 blob_info->exempt=MagickTrue;
3494 return(SetStreamBuffering(image_info,blob_info));
3495 }
3496#if defined(MAGICKCORE_HAVE_POPEN) && defined(MAGICKCORE_PIPES_SUPPORT)
3497 if (*filename == '|')
3498 {
3499 char
3500 fileMode[MagickPathExtent],
3501 *sanitize_command;
3502
3503 /*
3504 Pipe image to or from a system command.
3505 */
3506#if defined(SIGPIPE)
3507 if (*type == 'w')
3508 (void) signal(SIGPIPE,SIG_IGN);
3509#endif
3510 *fileMode=(*type);
3511 fileMode[1]='\0';
3512 sanitize_command=SanitizeString(filename+1);
3513 blob_info->file_info.file=(FILE *) popen_utf8(sanitize_command,fileMode);
3514 sanitize_command=DestroyString(sanitize_command);
3515 if (blob_info->file_info.file == (FILE *) NULL)
3516 {
3517 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
3518 return(MagickFalse);
3519 }
3520 blob_info->type=PipeStream;
3521 blob_info->exempt=MagickTrue;
3522 return(SetStreamBuffering(image_info,blob_info));
3523 }
3524#endif
3525 status=GetPathAttributes(filename,&blob_info->properties);
3526#if defined(S_ISFIFO)
3527 if ((status != MagickFalse) && S_ISFIFO(blob_info->properties.st_mode))
3528 {
3529 blob_info->file_info.file=(FILE *) fopen_utf8(filename,type);
3530 if (blob_info->file_info.file == (FILE *) NULL)
3531 {
3532 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
3533 return(MagickFalse);
3534 }
3535 blob_info->type=FileStream;
3536 blob_info->exempt=MagickTrue;
3537 return(SetStreamBuffering(image_info,blob_info));
3538 }
3539#endif
3540 GetPathComponent(image->filename,ExtensionPath,extension);
3541 if (*type == 'w')
3542 {
3543 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
3544 if ((image_info->adjoin == MagickFalse) ||
3545 (strchr(filename,'%') != (char *) NULL))
3546 {
3547 /*
3548 Form filename for multi-part images.
3549 */
3550 (void) InterpretImageFilename(image_info,image,image->filename,(int)
3551 image->scene,filename,exception);
3552 if ((LocaleCompare(filename,image->filename) == 0) &&
3553 ((GetPreviousImageInList(image) != (Image *) NULL) ||
3554 (GetNextImageInList(image) != (Image *) NULL)))
3555 {
3556 char
3557 path[MagickPathExtent];
3558
3559 GetPathComponent(image->filename,RootPath,path);
3560 if (*extension == '\0')
3561 (void) FormatLocaleString(filename,MagickPathExtent,"%s-%.17g",
3562 path,(double) image->scene);
3563 else
3564 (void) FormatLocaleString(filename,MagickPathExtent,
3565 "%s-%.17g.%s",path,(double) image->scene,extension);
3566 }
3567 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
3568 }
3569 if (IsPathAuthorized(rights,filename) == MagickFalse)
3570 ThrowPolicyException(filename,MagickFalse);
3571 }
3572 if (image_info->file != (FILE *) NULL)
3573 {
3574 blob_info->file_info.file=image_info->file;
3575 blob_info->type=FileStream;
3576 blob_info->exempt=MagickTrue;
3577 }
3578 else
3579 if (*type == 'r')
3580 {
3581 int
3582 file;
3583
3584 blob_info->file_info.file=(FILE *) NULL;
3585 file=open_utf8(filename,flags,0);
3586 if (file >= 0)
3587 blob_info->file_info.file=fdopen(file,type);
3588 if (blob_info->file_info.file != (FILE *) NULL)
3589 {
3590 size_t
3591 count;
3592
3593 unsigned char
3594 magick[3];
3595
3596 blob_info->type=FileStream;
3597 (void) SetStreamBuffering(image_info,blob_info);
3598 (void) memset(magick,0,sizeof(magick));
3599 count=fread(magick,1,sizeof(magick),blob_info->file_info.file);
3600 (void) fseek(blob_info->file_info.file,-((off_t) count),SEEK_CUR);
3601#if defined(MAGICKCORE_POSIX_SUPPORT)
3602 (void) fflush(blob_info->file_info.file);
3603#endif
3604 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
3605 " read %.17g magic header bytes",(double) count);
3606#if defined(MAGICKCORE_ZLIB_DELEGATE)
3607 if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
3608 ((int) magick[2] == 0x08))
3609 {
3610 gzFile
3611 gzfile = gzopen_utf8(filename,"rb");
3612
3613 if (gzfile != (gzFile) NULL)
3614 {
3615 if (blob_info->file_info.file != (FILE *) NULL)
3616 (void) fclose(blob_info->file_info.file);
3617 blob_info->file_info.file=(FILE *) NULL;
3618 blob_info->file_info.gzfile=gzfile;
3619 blob_info->type=ZipStream;
3620 }
3621 }
3622#endif
3623#if defined(MAGICKCORE_BZLIB_DELEGATE)
3624 if (strncmp((char *) magick,"BZh",3) == 0)
3625 {
3626 BZFILE
3627 *bzfile = BZ2_bzopen(filename,"r");
3628
3629 if (bzfile != (BZFILE *) NULL)
3630 {
3631 if (blob_info->file_info.file != (FILE *) NULL)
3632 (void) fclose(blob_info->file_info.file);
3633 blob_info->file_info.file=(FILE *) NULL;
3634 blob_info->file_info.bzfile=bzfile;
3635 blob_info->type=BZipStream;
3636 }
3637 }
3638#endif
3639 if (blob_info->type == FileStream)
3640 {
3641 const MagickInfo
3642 *magick_info;
3643
3644 ExceptionInfo
3645 *sans_exception;
3646
3647 size_t
3648 length;
3649
3650 sans_exception=AcquireExceptionInfo();
3651 magick_info=GetMagickInfo(image_info->magick,sans_exception);
3652 sans_exception=DestroyExceptionInfo(sans_exception);
3653 length=(size_t) blob_info->properties.st_size;
3654 if ((magick_info != (const MagickInfo *) NULL) &&
3655 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
3656 (AcquireMagickResource(MapResource,length) != MagickFalse))
3657 {
3658 void
3659 *blob;
3660
3661 blob=MapBlob(fileno(blob_info->file_info.file),ReadMode,0,
3662 length);
3663 if (blob == (void *) NULL)
3664 RelinquishMagickResource(MapResource,length);
3665 else
3666 {
3667 /*
3668 Format supports blobs-- use memory-mapped I/O.
3669 */
3670 if (image_info->file != (FILE *) NULL)
3671 blob_info->exempt=MagickFalse;
3672 else
3673 {
3674 (void) fclose(blob_info->file_info.file);
3675 blob_info->file_info.file=(FILE *) NULL;
3676 }
3677 AttachBlob(blob_info,blob,length);
3678 blob_info->mapped=MagickTrue;
3679 }
3680 }
3681 }
3682 }
3683 }
3684 else
3685#if defined(MAGICKCORE_ZLIB_DELEGATE)
3686 if ((LocaleCompare(extension,"gz") == 0) ||
3687 (LocaleCompare(extension,"wmz") == 0) ||
3688 (LocaleCompare(extension,"svgz") == 0))
3689 {
3690 blob_info->file_info.gzfile=gzopen_utf8(filename,"wb");
3691 if (blob_info->file_info.gzfile != (gzFile) NULL)
3692 blob_info->type=ZipStream;
3693 }
3694 else
3695#endif
3696#if defined(MAGICKCORE_BZLIB_DELEGATE)
3697 if (LocaleCompare(extension,"bz2") == 0)
3698 {
3699 blob_info->file_info.bzfile=BZ2_bzopen(filename,"w");
3700 if (blob_info->file_info.bzfile != (BZFILE *) NULL)
3701 blob_info->type=BZipStream;
3702 }
3703 else
3704#endif
3705 {
3706 int
3707 file;
3708
3709 blob_info->file_info.file=(FILE *) NULL;
3710 file=open_utf8(filename,flags,P_MODE);
3711 if (file >= 0)
3712 blob_info->file_info.file=fdopen(file,type);
3713 if (blob_info->file_info.file != (FILE *) NULL)
3714 {
3715 blob_info->type=FileStream;
3716 (void) SetStreamBuffering(image_info,blob_info);
3717 }
3718 }
3719 if (IsPathAuthorized(rights,filename) == MagickFalse)
3720 ThrowPolicyException(filename,MagickFalse);
3721 blob_info->status=0;
3722 blob_info->error_number=0;
3723 if (blob_info->type != UndefinedStream)
3724 blob_info->size=GetBlobSize(image);
3725 else
3726 {
3727 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
3728 return(MagickFalse);
3729 }
3730 return(MagickTrue);
3731}
3732␌
3733/*
3734%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3735% %
3736% %
3737% %
3738+ P i n g B l o b %
3739% %
3740% %
3741% %
3742%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3743%
3744% PingBlob() returns all the attributes of an image or image sequence except
3745% for the pixels. It is much faster and consumes far less memory than
3746% BlobToImage(). On failure, a NULL image is returned and exception
3747% describes the reason for the failure.
3748%
3749% The format of the PingBlob method is:
3750%
3751% Image *PingBlob(const ImageInfo *image_info,const void *blob,
3752% const size_t length,ExceptionInfo *exception)
3753%
3754% A description of each parameter follows:
3755%
3756% o image_info: the image info.
3757%
3758% o blob: the address of a character stream in one of the image formats
3759% understood by ImageMagick.
3760%
3761% o length: This size_t integer reflects the length in bytes of the blob.
3762%
3763% o exception: return any errors or warnings in this structure.
3764%
3765*/
3766
3767#if defined(__cplusplus) || defined(c_plusplus)
3768extern "C" {
3769#endif
3770
3771static size_t PingStream(const Image *magick_unused(image),
3772 const void *magick_unused(pixels),const size_t columns)
3773{
3774 magick_unreferenced(image);
3775 magick_unreferenced(pixels);
3776 return(columns);
3777}
3778
3779#if defined(__cplusplus) || defined(c_plusplus)
3780}
3781#endif
3782
3783MagickExport Image *PingBlob(const ImageInfo *image_info,const void *blob,
3784 const size_t length,ExceptionInfo *exception)
3785{
3786 const MagickInfo
3787 *magick_info;
3788
3789 Image
3790 *image;
3791
3792 ImageInfo
3793 *clone_info,
3794 *ping_info;
3795
3796 MagickBooleanType
3797 status;
3798
3799 assert(image_info != (ImageInfo *) NULL);
3800 assert(image_info->signature == MagickCoreSignature);
3801 assert(exception != (ExceptionInfo *) NULL);
3802 if (IsEventLogging() != MagickFalse)
3803 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3804 image_info->filename);
3805 if ((blob == (const void *) NULL) || (length == 0))
3806 {
3807 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
3808 "ZeroLengthBlobNotPermitted","`%s'",image_info->filename);
3809 return((Image *) NULL);
3810 }
3811 ping_info=CloneImageInfo(image_info);
3812 ping_info->blob=(void *) blob;
3813 ping_info->length=length;
3814 ping_info->ping=MagickTrue;
3815 if (*ping_info->magick == '\0')
3816 (void) SetImageInfo(ping_info,0,exception);
3817 magick_info=GetMagickInfo(ping_info->magick,exception);
3818 if (magick_info == (const MagickInfo *) NULL)
3819 {
3820 (void) ThrowMagickException(exception,GetMagickModule(),
3821 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
3822 ping_info->magick);
3823 ping_info=DestroyImageInfo(ping_info);
3824 return((Image *) NULL);
3825 }
3826 if (GetMagickBlobSupport(magick_info) != MagickFalse)
3827 {
3828 char
3829 filename[MagickPathExtent];
3830
3831 /*
3832 Native blob support for this image format.
3833 */
3834 (void) CopyMagickString(filename,ping_info->filename,MagickPathExtent);
3835 (void) FormatLocaleString(ping_info->filename,MagickPathExtent,"%s:%s",
3836 ping_info->magick,filename);
3837 image=ReadStream(ping_info,&PingStream,exception);
3838 if (image != (Image *) NULL)
3839 (void) DetachBlob(image->blob);
3840 ping_info=DestroyImageInfo(ping_info);
3841 return(image);
3842 }
3843 /*
3844 Write blob to a temporary file on disk.
3845 */
3846 ping_info->blob=(void *) NULL;
3847 ping_info->length=0;
3848 *ping_info->filename='\0';
3849 status=BlobToFile(ping_info->filename,blob,length,exception);
3850 if (status == MagickFalse)
3851 {
3852 (void) RelinquishUniqueFileResource(ping_info->filename);
3853 ping_info=DestroyImageInfo(ping_info);
3854 return((Image *) NULL);
3855 }
3856 clone_info=CloneImageInfo(ping_info);
3857 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:%s",
3858 ping_info->magick,ping_info->filename);
3859 image=ReadStream(clone_info,&PingStream,exception);
3860 if (image != (Image *) NULL)
3861 {
3862 Image
3863 *images;
3864
3865 /*
3866 Restore original filenames and image format.
3867 */
3868 for (images=GetFirstImageInList(image); images != (Image *) NULL; )
3869 {
3870 (void) CopyMagickString(images->filename,image_info->filename,
3871 MagickPathExtent);
3872 (void) CopyMagickString(images->magick_filename,image_info->filename,
3873 MagickPathExtent);
3874 (void) CopyMagickString(images->magick,magick_info->name,
3875 MagickPathExtent);
3876 images=GetNextImageInList(images);
3877 }
3878 }
3879 clone_info=DestroyImageInfo(clone_info);
3880 (void) RelinquishUniqueFileResource(ping_info->filename);
3881 ping_info=DestroyImageInfo(ping_info);
3882 return(image);
3883}
3884␌
3885/*
3886%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3887% %
3888% %
3889% %
3890+ R e a d B l o b %
3891% %
3892% %
3893% %
3894%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3895%
3896% ReadBlob() reads data from the blob or image file and returns it. It
3897% returns the number of bytes read. If length is zero, ReadBlob() returns
3898% zero and has no other results. If length is greater than MAGICK_SSIZE_MAX,
3899% the result is unspecified.
3900%
3901% The format of the ReadBlob method is:
3902%
3903% ssize_t ReadBlob(Image *image,const size_t length,void *data)
3904%
3905% A description of each parameter follows:
3906%
3907% o image: the image.
3908%
3909% o length: Specifies an integer representing the number of bytes to read
3910% from the file.
3911%
3912% o data: Specifies an area to place the information requested from the
3913% file.
3914%
3915*/
3916MagickExport ssize_t ReadBlob(Image *image,const size_t length,void *data)
3917{
3918 BlobInfo
3919 *magick_restrict blob_info;
3920
3921 int
3922 c;
3923
3924 ssize_t
3925 count;
3926
3927 unsigned char
3928 *q;
3929
3930 assert(image != (Image *) NULL);
3931 assert(image->signature == MagickCoreSignature);
3932 assert(image->blob != (BlobInfo *) NULL);
3933 assert(image->blob->type != UndefinedStream);
3934 if (length == 0)
3935 return(0);
3936 assert(data != (void *) NULL);
3937 blob_info=image->blob;
3938 count=0;
3939 q=(unsigned char *) data;
3940 switch (blob_info->type)
3941 {
3942 case UndefinedStream:
3943 break;
3944 case StandardStream:
3945 case FileStream:
3946 case PipeStream:
3947 {
3948 switch (length)
3949 {
3950 default:
3951 {
3952 count=(ssize_t) fread(q,1,length,blob_info->file_info.file);
3953 break;
3954 }
3955 case 4:
3956 {
3957 c=getc(blob_info->file_info.file);
3958 if (c == EOF)
3959 break;
3960 *q++=(unsigned char) c;
3961 count++;
3962 magick_fallthrough;
3963 }
3964 case 3:
3965 {
3966 c=getc(blob_info->file_info.file);
3967 if (c == EOF)
3968 break;
3969 *q++=(unsigned char) c;
3970 count++;
3971 magick_fallthrough;
3972 }
3973 case 2:
3974 {
3975 c=getc(blob_info->file_info.file);
3976 if (c == EOF)
3977 break;
3978 *q++=(unsigned char) c;
3979 count++;
3980 magick_fallthrough;
3981 }
3982 case 1:
3983 {
3984 c=getc(blob_info->file_info.file);
3985 if (c == EOF)
3986 break;
3987 *q++=(unsigned char) c;
3988 count++;
3989 magick_fallthrough;
3990 }
3991 case 0:
3992 break;
3993 }
3994 if ((count != (ssize_t) length) &&
3995 (ferror(blob_info->file_info.file) != 0))
3996 ThrowBlobException(blob_info);
3997 break;
3998 }
3999 case ZipStream:
4000 {
4001#if defined(MAGICKCORE_ZLIB_DELEGATE)
4002 int
4003 status;
4004
4005 switch (length)
4006 {
4007 default:
4008 {
4009 size_t
4010 i;
4011
4012 for (i=0; i < length; i+=(size_t) count)
4013 {
4014 count=(ssize_t) gzread(blob_info->file_info.gzfile,q+i,
4015 (unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
4016 if (count <= 0)
4017 {
4018 count=0;
4019 if (errno != EINTR)
4020 break;
4021 }
4022 }
4023 count=(ssize_t) i;
4024 break;
4025 }
4026 case 4:
4027 {
4028 c=gzgetc(blob_info->file_info.gzfile);
4029 if (c == EOF)
4030 break;
4031 *q++=(unsigned char) c;
4032 count++;
4033 magick_fallthrough;
4034 }
4035 case 3:
4036 {
4037 c=gzgetc(blob_info->file_info.gzfile);
4038 if (c == EOF)
4039 break;
4040 *q++=(unsigned char) c;
4041 count++;
4042 magick_fallthrough;
4043 }
4044 case 2:
4045 {
4046 c=gzgetc(blob_info->file_info.gzfile);
4047 if (c == EOF)
4048 break;
4049 *q++=(unsigned char) c;
4050 count++;
4051 magick_fallthrough;
4052 }
4053 case 1:
4054 {
4055 c=gzgetc(blob_info->file_info.gzfile);
4056 if (c == EOF)
4057 break;
4058 *q++=(unsigned char) c;
4059 count++;
4060 magick_fallthrough;
4061 }
4062 case 0:
4063 break;
4064 }
4065 status=Z_OK;
4066 (void) gzerror(blob_info->file_info.gzfile,&status);
4067 if ((count != (ssize_t) length) && (status != Z_OK))
4068 ThrowBlobException(blob_info);
4069 if (blob_info->eof == MagickFalse)
4070 blob_info->eof=gzeof(blob_info->file_info.gzfile) != 0 ? MagickTrue :
4071 MagickFalse;
4072#endif
4073 break;
4074 }
4075 case BZipStream:
4076 {
4077#if defined(MAGICKCORE_BZLIB_DELEGATE)
4078 int
4079 status;
4080
4081 size_t
4082 i;
4083
4084 if (blob_info->status != 0)
4085 break;
4086 for (i=0; i < length; i+=(size_t) count)
4087 {
4088 count=(ssize_t) BZ2_bzread(blob_info->file_info.bzfile,q+i,(int)
4089 MagickMin(length-i,MagickMaxBufferExtent));
4090 if (count <= 0)
4091 {
4092 count=0;
4093 if (errno != EINTR)
4094 break;
4095 }
4096 }
4097 count=(ssize_t) i;
4098 status=BZ_OK;
4099 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
4100 if ((count != (ssize_t) length) && (status != BZ_OK))
4101 ThrowBlobException(blob_info);
4102 if ((count != (ssize_t) length) && (status == BZ_OK))
4103 blob_info->eof=MagickTrue;
4104#endif
4105 break;
4106 }
4107 case FifoStream:
4108 break;
4109 case BlobStream:
4110 {
4111 const unsigned char
4112 *p;
4113
4114 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4115 {
4116 blob_info->eof=MagickTrue;
4117 break;
4118 }
4119 p=blob_info->data+blob_info->offset;
4120 count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
4121 blob_info->length-blob_info->offset);
4122 blob_info->offset+=count;
4123 if (count != (ssize_t) length)
4124 blob_info->eof=MagickTrue;
4125 (void) memcpy(q,p,(size_t) count);
4126 break;
4127 }
4128 case CustomStream:
4129 {
4130 if (blob_info->custom_stream->reader != (CustomStreamHandler) NULL)
4131 {
4132 count=blob_info->custom_stream->reader(q,length,
4133 blob_info->custom_stream->data);
4134 if (count <= 0)
4135 {
4136 blob_info->eof=MagickTrue;
4137 count=0;
4138 }
4139 }
4140 break;
4141 }
4142 }
4143 return(count);
4144}
4145␌
4146/*
4147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4148% %
4149% %
4150% %
4151+ R e a d B l o b B y t e %
4152% %
4153% %
4154% %
4155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4156%
4157% ReadBlobByte() reads a single byte from the image file and returns it.
4158%
4159% The format of the ReadBlobByte method is:
4160%
4161% int ReadBlobByte(Image *image)
4162%
4163% A description of each parameter follows.
4164%
4165% o image: the image.
4166%
4167*/
4168MagickExport int ReadBlobByte(Image *image)
4169{
4170 BlobInfo
4171 *magick_restrict blob_info;
4172
4173 int
4174 c;
4175
4176 assert(image != (Image *) NULL);
4177 assert(image->signature == MagickCoreSignature);
4178 assert(image->blob != (BlobInfo *) NULL);
4179 assert(image->blob->type != UndefinedStream);
4180 blob_info=image->blob;
4181 switch (blob_info->type)
4182 {
4183 case StandardStream:
4184 case FileStream:
4185 case PipeStream:
4186 {
4187 c=getc(blob_info->file_info.file);
4188 if (c == EOF)
4189 {
4190 if (ferror(blob_info->file_info.file) != 0)
4191 ThrowBlobException(blob_info);
4192 return(EOF);
4193 }
4194 break;
4195 }
4196 case BlobStream:
4197 {
4198 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
4199 {
4200 blob_info->eof=MagickTrue;
4201 return(EOF);
4202 }
4203 c=(int) (*((unsigned char *) blob_info->data+blob_info->offset));
4204 blob_info->offset++;
4205 break;
4206 }
4207 default:
4208 {
4209 ssize_t
4210 count;
4211
4212 unsigned char
4213 buffer[1];
4214
4215 count=ReadBlob(image,1,buffer);
4216 if (count != 1)
4217 return(EOF);
4218 c=(int) *buffer;
4219 break;
4220 }
4221 }
4222 return(c);
4223}
4224␌
4225/*
4226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4227% %
4228% %
4229% %
4230+ R e a d B l o b D o u b l e %
4231% %
4232% %
4233% %
4234%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4235%
4236% ReadBlobDouble() reads a double value as a 64-bit quantity in the byte-order
4237% specified by the endian member of the image structure.
4238%
4239% The format of the ReadBlobDouble method is:
4240%
4241% double ReadBlobDouble(Image *image)
4242%
4243% A description of each parameter follows.
4244%
4245% o image: the image.
4246%
4247*/
4248MagickExport double ReadBlobDouble(Image *image)
4249{
4250 union
4251 {
4252 MagickSizeType
4253 unsigned_value;
4254
4255 double
4256 double_value;
4257 } quantum;
4258
4259 quantum.double_value=0.0;
4260 quantum.unsigned_value=ReadBlobLongLong(image);
4261 return(quantum.double_value);
4262}
4263␌
4264/*
4265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4266% %
4267% %
4268% %
4269+ R e a d B l o b F l o a t %
4270% %
4271% %
4272% %
4273%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4274%
4275% ReadBlobFloat() reads a float value as a 32-bit quantity in the byte-order
4276% specified by the endian member of the image structure.
4277%
4278% The format of the ReadBlobFloat method is:
4279%
4280% float ReadBlobFloat(Image *image)
4281%
4282% A description of each parameter follows.
4283%
4284% o image: the image.
4285%
4286*/
4287MagickExport float ReadBlobFloat(Image *image)
4288{
4289 union
4290 {
4291 unsigned int
4292 unsigned_value;
4293
4294 float
4295 float_value;
4296 } quantum;
4297
4298 quantum.float_value=0.0;
4299 quantum.unsigned_value=ReadBlobLong(image);
4300 return(quantum.float_value);
4301}
4302␌
4303/*
4304%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4305% %
4306% %
4307% %
4308+ R e a d B l o b L o n g %
4309% %
4310% %
4311% %
4312%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4313%
4314% ReadBlobLong() reads a unsigned int value as a 32-bit quantity in the
4315% byte-order specified by the endian member of the image structure.
4316%
4317% The format of the ReadBlobLong method is:
4318%
4319% unsigned int ReadBlobLong(Image *image)
4320%
4321% A description of each parameter follows.
4322%
4323% o image: the image.
4324%
4325*/
4326MagickExport unsigned int ReadBlobLong(Image *image)
4327{
4328 const unsigned char
4329 *p;
4330
4331 ssize_t
4332 count;
4333
4334 unsigned char
4335 buffer[4];
4336
4337 unsigned int
4338 value;
4339
4340 assert(image != (Image *) NULL);
4341 assert(image->signature == MagickCoreSignature);
4342 *buffer='\0';
4343 p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4344 if (count != 4)
4345 return(0UL);
4346 if (image->endian == LSBEndian)
4347 {
4348 value=(unsigned int) (*p++);
4349 value|=(unsigned int) (*p++) << 8;
4350 value|=(unsigned int) (*p++) << 16;
4351 value|=(unsigned int) (*p++) << 24;
4352 return(value);
4353 }
4354 value=(unsigned int) (*p++) << 24;
4355 value|=(unsigned int) (*p++) << 16;
4356 value|=(unsigned int) (*p++) << 8;
4357 value|=(unsigned int) (*p++);
4358 return(value);
4359}
4360␌
4361/*
4362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4363% %
4364% %
4365% %
4366+ R e a d B l o b L o n g L o n g %
4367% %
4368% %
4369% %
4370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4371%
4372% ReadBlobLongLong() reads a long long value as a 64-bit quantity in the
4373% byte-order specified by the endian member of the image structure.
4374%
4375% The format of the ReadBlobLongLong method is:
4376%
4377% MagickSizeType ReadBlobLongLong(Image *image)
4378%
4379% A description of each parameter follows.
4380%
4381% o image: the image.
4382%
4383*/
4384MagickExport MagickSizeType ReadBlobLongLong(Image *image)
4385{
4386 MagickSizeType
4387 value;
4388
4389 const unsigned char
4390 *p;
4391
4392 ssize_t
4393 count;
4394
4395 unsigned char
4396 buffer[8];
4397
4398 assert(image != (Image *) NULL);
4399 assert(image->signature == MagickCoreSignature);
4400 *buffer='\0';
4401 p=(const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4402 if (count != 8)
4403 return(MagickULLConstant(0));
4404 if (image->endian == LSBEndian)
4405 {
4406 value=(MagickSizeType) (*p++);
4407 value|=(MagickSizeType) (*p++) << 8;
4408 value|=(MagickSizeType) (*p++) << 16;
4409 value|=(MagickSizeType) (*p++) << 24;
4410 value|=(MagickSizeType) (*p++) << 32;
4411 value|=(MagickSizeType) (*p++) << 40;
4412 value|=(MagickSizeType) (*p++) << 48;
4413 value|=(MagickSizeType) (*p++) << 56;
4414 return(value);
4415 }
4416 value=(MagickSizeType) (*p++) << 56;
4417 value|=(MagickSizeType) (*p++) << 48;
4418 value|=(MagickSizeType) (*p++) << 40;
4419 value|=(MagickSizeType) (*p++) << 32;
4420 value|=(MagickSizeType) (*p++) << 24;
4421 value|=(MagickSizeType) (*p++) << 16;
4422 value|=(MagickSizeType) (*p++) << 8;
4423 value|=(MagickSizeType) (*p++);
4424 return(value);
4425}
4426␌
4427/*
4428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4429% %
4430% %
4431% %
4432+ R e a d B l o b S h o r t %
4433% %
4434% %
4435% %
4436%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4437%
4438% ReadBlobShort() reads a short value as a 16-bit quantity in the byte-order
4439% specified by the endian member of the image structure.
4440%
4441% The format of the ReadBlobShort method is:
4442%
4443% unsigned short ReadBlobShort(Image *image)
4444%
4445% A description of each parameter follows.
4446%
4447% o image: the image.
4448%
4449*/
4450MagickExport unsigned short ReadBlobShort(Image *image)
4451{
4452 const unsigned char
4453 *p;
4454
4455 unsigned short
4456 value;
4457
4458 ssize_t
4459 count;
4460
4461 unsigned char
4462 buffer[2];
4463
4464 assert(image != (Image *) NULL);
4465 assert(image->signature == MagickCoreSignature);
4466 *buffer='\0';
4467 p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4468 if (count != 2)
4469 return((unsigned short) 0U);
4470 if (image->endian == LSBEndian)
4471 {
4472 value=(unsigned short) (*p++);
4473 value|=(unsigned short) (*p++) << 8;
4474 return(value);
4475 }
4476 value=(unsigned short) ((unsigned short) (*p++) << 8);
4477 value|=(unsigned short) (*p++);
4478 return(value);
4479}
4480␌
4481/*
4482%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4483% %
4484% %
4485% %
4486+ R e a d B l o b L S B L o n g %
4487% %
4488% %
4489% %
4490%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4491%
4492% ReadBlobLSBLong() reads a unsigned int value as a 32-bit quantity in
4493% least-significant byte first order.
4494%
4495% The format of the ReadBlobLSBLong method is:
4496%
4497% unsigned int ReadBlobLSBLong(Image *image)
4498%
4499% A description of each parameter follows.
4500%
4501% o image: the image.
4502%
4503*/
4504MagickExport unsigned int ReadBlobLSBLong(Image *image)
4505{
4506 const unsigned char
4507 *p;
4508
4509 unsigned int
4510 value;
4511
4512 ssize_t
4513 count;
4514
4515 unsigned char
4516 buffer[4];
4517
4518 assert(image != (Image *) NULL);
4519 assert(image->signature == MagickCoreSignature);
4520 *buffer='\0';
4521 p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4522 if (count != 4)
4523 return(0U);
4524 value=(unsigned int) (*p++);
4525 value|=(unsigned int) (*p++) << 8;
4526 value|=(unsigned int) (*p++) << 16;
4527 value|=(unsigned int) (*p++) << 24;
4528 return(value);
4529}
4530␌
4531/*
4532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4533% %
4534% %
4535% %
4536+ R e a d B l o b L S B S i g n e d L o n g %
4537% %
4538% %
4539% %
4540%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4541%
4542% ReadBlobLSBSignedLong() reads a signed int value as a 32-bit quantity in
4543% least-significant byte first order.
4544%
4545% The format of the ReadBlobLSBSignedLong method is:
4546%
4547% signed int ReadBlobLSBSignedLong(Image *image)
4548%
4549% A description of each parameter follows.
4550%
4551% o image: the image.
4552%
4553*/
4554MagickExport signed int ReadBlobLSBSignedLong(Image *image)
4555{
4556 union
4557 {
4558 unsigned int
4559 unsigned_value;
4560
4561 signed int
4562 signed_value;
4563 } quantum;
4564
4565 quantum.unsigned_value=ReadBlobLSBLong(image);
4566 return(quantum.signed_value);
4567}
4568␌
4569/*
4570%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4571% %
4572% %
4573% %
4574+ R e a d B l o b L S B S h o r t %
4575% %
4576% %
4577% %
4578%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4579%
4580% ReadBlobLSBShort() reads a short value as a 16-bit quantity in
4581% least-significant byte first order.
4582%
4583% The format of the ReadBlobLSBShort method is:
4584%
4585% unsigned short ReadBlobLSBShort(Image *image)
4586%
4587% A description of each parameter follows.
4588%
4589% o image: the image.
4590%
4591*/
4592MagickExport unsigned short ReadBlobLSBShort(Image *image)
4593{
4594 const unsigned char
4595 *p;
4596
4597 unsigned short
4598 value;
4599
4600 ssize_t
4601 count;
4602
4603 unsigned char
4604 buffer[2];
4605
4606 assert(image != (Image *) NULL);
4607 assert(image->signature == MagickCoreSignature);
4608 *buffer='\0';
4609 p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4610 if (count != 2)
4611 return((unsigned short) 0U);
4612 value=(unsigned short) (*p++);
4613 value|=(unsigned short) (*p++) << 8;
4614 return(value);
4615}
4616␌
4617/*
4618%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4619% %
4620% %
4621% %
4622+ R e a d B l o b L S B S i g n e d S h o r t %
4623% %
4624% %
4625% %
4626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4627%
4628% ReadBlobLSBSignedShort() reads a signed short value as a 16-bit quantity in
4629% least-significant byte-order.
4630%
4631% The format of the ReadBlobLSBSignedShort method is:
4632%
4633% signed short ReadBlobLSBSignedShort(Image *image)
4634%
4635% A description of each parameter follows.
4636%
4637% o image: the image.
4638%
4639*/
4640MagickExport signed short ReadBlobLSBSignedShort(Image *image)
4641{
4642 union
4643 {
4644 unsigned short
4645 unsigned_value;
4646
4647 signed short
4648 signed_value;
4649 } quantum;
4650
4651 quantum.unsigned_value=ReadBlobLSBShort(image);
4652 return(quantum.signed_value);
4653}
4654␌
4655/*
4656%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4657% %
4658% %
4659% %
4660+ R e a d B l o b M S B L o n g %
4661% %
4662% %
4663% %
4664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4665%
4666% ReadBlobMSBLong() reads a unsigned int value as a 32-bit quantity in
4667% most-significant byte first order.
4668%
4669% The format of the ReadBlobMSBLong method is:
4670%
4671% unsigned int ReadBlobMSBLong(Image *image)
4672%
4673% A description of each parameter follows.
4674%
4675% o image: the image.
4676%
4677*/
4678MagickExport unsigned int ReadBlobMSBLong(Image *image)
4679{
4680 const unsigned char
4681 *p;
4682
4683 unsigned int
4684 value;
4685
4686 ssize_t
4687 count;
4688
4689 unsigned char
4690 buffer[4];
4691
4692 assert(image != (Image *) NULL);
4693 assert(image->signature == MagickCoreSignature);
4694 *buffer='\0';
4695 p=(const unsigned char *) ReadBlobStream(image,4,buffer,&count);
4696 if (count != 4)
4697 return(0UL);
4698 value=(unsigned int) (*p++) << 24;
4699 value|=(unsigned int) (*p++) << 16;
4700 value|=(unsigned int) (*p++) << 8;
4701 value|=(unsigned int) (*p++);
4702 return(value);
4703}
4704␌
4705/*
4706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4707% %
4708% %
4709% %
4710+ R e a d B l o b M S B L o n g L o n g %
4711% %
4712% %
4713% %
4714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4715%
4716% ReadBlobMSBLongLong() reads a unsigned long long value as a 64-bit quantity
4717% in most-significant byte first order.
4718%
4719% The format of the ReadBlobMSBLongLong method is:
4720%
4721% unsigned int ReadBlobMSBLongLong(Image *image)
4722%
4723% A description of each parameter follows.
4724%
4725% o image: the image.
4726%
4727*/
4728MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
4729{
4730 const unsigned char
4731 *p;
4732
4733 MagickSizeType
4734 value;
4735
4736 ssize_t
4737 count;
4738
4739 unsigned char
4740 buffer[8];
4741
4742 assert(image != (Image *) NULL);
4743 assert(image->signature == MagickCoreSignature);
4744 *buffer='\0';
4745 p=(const unsigned char *) ReadBlobStream(image,8,buffer,&count);
4746 if (count != 8)
4747 return(MagickULLConstant(0));
4748 value=(MagickSizeType) (*p++) << 56;
4749 value|=(MagickSizeType) (*p++) << 48;
4750 value|=(MagickSizeType) (*p++) << 40;
4751 value|=(MagickSizeType) (*p++) << 32;
4752 value|=(MagickSizeType) (*p++) << 24;
4753 value|=(MagickSizeType) (*p++) << 16;
4754 value|=(MagickSizeType) (*p++) << 8;
4755 value|=(MagickSizeType) (*p++);
4756 return(value);
4757}
4758␌
4759/*
4760%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4761% %
4762% %
4763% %
4764+ R e a d B l o b M S B S h o r t %
4765% %
4766% %
4767% %
4768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4769%
4770% ReadBlobMSBShort() reads a short value as a 16-bit quantity in
4771% most-significant byte first order.
4772%
4773% The format of the ReadBlobMSBShort method is:
4774%
4775% unsigned short ReadBlobMSBShort(Image *image)
4776%
4777% A description of each parameter follows.
4778%
4779% o image: the image.
4780%
4781*/
4782MagickExport unsigned short ReadBlobMSBShort(Image *image)
4783{
4784 const unsigned char
4785 *p;
4786
4787 unsigned short
4788 value;
4789
4790 ssize_t
4791 count;
4792
4793 unsigned char
4794 buffer[2];
4795
4796 assert(image != (Image *) NULL);
4797 assert(image->signature == MagickCoreSignature);
4798 *buffer='\0';
4799 p=(const unsigned char *) ReadBlobStream(image,2,buffer,&count);
4800 if (count != 2)
4801 return((unsigned short) 0U);
4802 value=(unsigned short) ((*p++) << 8);
4803 value|=(unsigned short) (*p++);
4804 return((unsigned short) (value & 0xffff));
4805}
4806␌
4807/*
4808%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4809% %
4810% %
4811% %
4812+ R e a d B l o b M S B S i g n e d L o n g %
4813% %
4814% %
4815% %
4816%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4817%
4818% ReadBlobMSBSignedLong() reads a signed int value as a 32-bit quantity in
4819% most-significant byte-order.
4820%
4821% The format of the ReadBlobMSBSignedLong method is:
4822%
4823% signed int ReadBlobMSBSignedLong(Image *image)
4824%
4825% A description of each parameter follows.
4826%
4827% o image: the image.
4828%
4829*/
4830MagickExport signed int ReadBlobMSBSignedLong(Image *image)
4831{
4832 union
4833 {
4834 unsigned int
4835 unsigned_value;
4836
4837 signed int
4838 signed_value;
4839 } quantum;
4840
4841 quantum.unsigned_value=ReadBlobMSBLong(image);
4842 return(quantum.signed_value);
4843}
4844␌
4845/*
4846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4847% %
4848% %
4849% %
4850+ R e a d B l o b M S B S i g n e d S h o r t %
4851% %
4852% %
4853% %
4854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4855%
4856% ReadBlobMSBSignedShort() reads a signed short value as a 16-bit quantity in
4857% most-significant byte-order.
4858%
4859% The format of the ReadBlobMSBSignedShort method is:
4860%
4861% signed short ReadBlobMSBSignedShort(Image *image)
4862%
4863% A description of each parameter follows.
4864%
4865% o image: the image.
4866%
4867*/
4868MagickExport signed short ReadBlobMSBSignedShort(Image *image)
4869{
4870 union
4871 {
4872 unsigned short
4873 unsigned_value;
4874
4875 signed short
4876 signed_value;
4877 } quantum;
4878
4879 quantum.unsigned_value=ReadBlobMSBShort(image);
4880 return(quantum.signed_value);
4881}
4882␌
4883/*
4884%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4885% %
4886% %
4887% %
4888+ R e a d B l o b S i g n e d L o n g %
4889% %
4890% %
4891% %
4892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4893%
4894% ReadBlobSignedLong() reads a signed int value as a 32-bit quantity in the
4895% byte-order specified by the endian member of the image structure.
4896%
4897% The format of the ReadBlobSignedLong method is:
4898%
4899% signed int ReadBlobSignedLong(Image *image)
4900%
4901% A description of each parameter follows.
4902%
4903% o image: the image.
4904%
4905*/
4906MagickExport signed int ReadBlobSignedLong(Image *image)
4907{
4908 union
4909 {
4910 unsigned int
4911 unsigned_value;
4912
4913 signed int
4914 signed_value;
4915 } quantum;
4916
4917 quantum.unsigned_value=ReadBlobLong(image);
4918 return(quantum.signed_value);
4919}
4920␌
4921/*
4922%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4923% %
4924% %
4925% %
4926+ R e a d B l o b S i g n e d S h o r t %
4927% %
4928% %
4929% %
4930%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4931%
4932% ReadBlobSignedShort() reads a signed short value as a 16-bit quantity in the
4933% byte-order specified by the endian member of the image structure.
4934%
4935% The format of the ReadBlobSignedShort method is:
4936%
4937% signed short ReadBlobSignedShort(Image *image)
4938%
4939% A description of each parameter follows.
4940%
4941% o image: the image.
4942%
4943*/
4944MagickExport signed short ReadBlobSignedShort(Image *image)
4945{
4946 union
4947 {
4948 unsigned short
4949 unsigned_value;
4950
4951 signed short
4952 signed_value;
4953 } quantum;
4954
4955 quantum.unsigned_value=ReadBlobShort(image);
4956 return(quantum.signed_value);
4957}
4958␌
4959/*
4960%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4961% %
4962% %
4963% %
4964+ R e a d B l o b S t r e a m %
4965% %
4966% %
4967% %
4968%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4969%
4970% ReadBlobStream() reads data from the blob or image file and returns it. It
4971% returns a pointer to the data buffer you supply or to the image memory
4972% buffer if its supported (zero-copy). If length is zero, ReadBlobStream()
4973% returns a count of zero and has no other results. If length is greater than
4974% MAGICK_SSIZE_MAX, the result is unspecified.
4975%
4976% The format of the ReadBlobStream method is:
4977%
4978% const void *ReadBlobStream(Image *image,const size_t length,
4979% void *magick_restrict data,ssize_t *count)
4980%
4981% A description of each parameter follows:
4982%
4983% o image: the image.
4984%
4985% o length: Specifies an integer representing the number of bytes to read
4986% from the file.
4987%
4988% o count: returns the number of bytes read.
4989%
4990% o data: Specifies an area to place the information requested from the
4991% file.
4992%
4993*/
4994MagickExport magick_hot_spot const void *ReadBlobStream(Image *image,
4995 const size_t length,void *magick_restrict data,ssize_t *count)
4996{
4997 BlobInfo
4998 *magick_restrict blob_info;
4999
5000 assert(image != (Image *) NULL);
5001 assert(image->signature == MagickCoreSignature);
5002 assert(image->blob != (BlobInfo *) NULL);
5003 assert(image->blob->type != UndefinedStream);
5004 assert(count != (ssize_t *) NULL);
5005 blob_info=image->blob;
5006 if (blob_info->type != BlobStream)
5007 {
5008 assert(data != NULL);
5009 *count=ReadBlob(image,length,(unsigned char *) data);
5010 return(data);
5011 }
5012 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
5013 {
5014 *count=0;
5015 blob_info->eof=MagickTrue;
5016 return(data);
5017 }
5018 data=blob_info->data+blob_info->offset;
5019 *count=(ssize_t) MagickMin((MagickOffsetType) length,(MagickOffsetType)
5020 blob_info->length-blob_info->offset);
5021 blob_info->offset+=(*count);
5022 if (*count != (ssize_t) length)
5023 blob_info->eof=MagickTrue;
5024 return(data);
5025}
5026␌
5027/*
5028%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5029% %
5030% %
5031% %
5032+ R e a d B l o b S t r i n g %
5033% %
5034% %
5035% %
5036%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5037%
5038% ReadBlobString() reads characters from a blob or file until a newline
5039% character is read or an end-of-file condition is encountered.
5040%
5041% The format of the ReadBlobString method is:
5042%
5043% char *ReadBlobString(Image *image,char *string)
5044%
5045% A description of each parameter follows:
5046%
5047% o image: the image.
5048%
5049% o string: the address of a character buffer.
5050%
5051*/
5052MagickExport char *ReadBlobString(Image *image,char *string)
5053{
5054 BlobInfo
5055 *magick_restrict blob_info;
5056
5057 int
5058 c = -1;
5059
5060 size_t
5061 i = 0;
5062
5063 assert(image != (Image *) NULL);
5064 assert(image->signature == MagickCoreSignature);
5065 assert(image->blob != (BlobInfo *) NULL);
5066 assert(image->blob->type != UndefinedStream);
5067 if (IsEventLogging() != MagickFalse)
5068 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5069 *string='\0';
5070 blob_info=image->blob;
5071 switch (blob_info->type)
5072 {
5073 case UndefinedStream:
5074 break;
5075 case StandardStream:
5076 case FileStream:
5077 {
5078 char *p = fgets(string,MagickPathExtent,blob_info->file_info.file);
5079 if (p == (char *) NULL)
5080 {
5081 if (ferror(blob_info->file_info.file) != 0)
5082 ThrowBlobException(blob_info);
5083 return((char *) NULL);
5084 }
5085 i=strlen(string);
5086 break;
5087 }
5088 case ZipStream:
5089 {
5090#if defined(MAGICKCORE_ZLIB_DELEGATE)
5091 char *p = gzgets(blob_info->file_info.gzfile,string,MagickPathExtent);
5092 if (p == (char *) NULL)
5093 {
5094 int status = Z_OK;
5095 (void) gzerror(blob_info->file_info.gzfile,&status);
5096 if (status != Z_OK)
5097 ThrowBlobException(blob_info);
5098 return((char *) NULL);
5099 }
5100 i=strlen(string);
5101 break;
5102#endif
5103 }
5104 default:
5105 {
5106 do
5107 {
5108 c=ReadBlobByte(image);
5109 if (c == EOF)
5110 {
5111 blob_info->eof=MagickTrue;
5112 break;
5113 }
5114 string[i++]=(char) c;
5115 if (c == '\n')
5116 break;
5117 } while (i < (MaxTextExtent-2));
5118 string[i]='\0';
5119 break;
5120 }
5121 }
5122 /*
5123 Strip trailing newline.
5124 */
5125 if ((string[i] == '\r') || (string[i] == '\n'))
5126 string[i]='\0';
5127 while ((i > 0) && ((string[i-1] == '\r') || (string[i-1] == '\n')))
5128 string[--i]='\0';
5129 if ((*string == '\0') && (blob_info->eof != MagickFalse))
5130 return((char *) NULL);
5131 return(string);
5132}
5133␌
5134/*
5135%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5136% %
5137% %
5138% %
5139+ R e f e r e n c e B l o b %
5140% %
5141% %
5142% %
5143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5144%
5145% ReferenceBlob() increments the reference count associated with the pixel
5146% blob returning a pointer to the blob.
5147%
5148% The format of the ReferenceBlob method is:
5149%
5150% BlobInfo ReferenceBlob(BlobInfo *blob_info)
5151%
5152% A description of each parameter follows:
5153%
5154% o blob_info: the blob_info.
5155%
5156*/
5157MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
5158{
5159 assert(blob != (BlobInfo *) NULL);
5160 assert(blob->signature == MagickCoreSignature);
5161 if (IsEventLogging() != MagickFalse)
5162 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5163 LockSemaphoreInfo(blob->semaphore);
5164 blob->reference_count++;
5165 UnlockSemaphoreInfo(blob->semaphore);
5166 return(blob);
5167}
5168␌
5169/*
5170%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5171% %
5172% %
5173% %
5174+ S e e k B l o b %
5175% %
5176% %
5177% %
5178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5179%
5180% SeekBlob() sets the offset in bytes from the beginning of a blob or file
5181% and returns the resulting offset.
5182%
5183% The format of the SeekBlob method is:
5184%
5185% MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
5186% const int whence)
5187%
5188% A description of each parameter follows:
5189%
5190% o image: the image.
5191%
5192% o offset: Specifies an integer representing the offset in bytes.
5193%
5194% o whence: Specifies an integer representing how the offset is
5195% treated relative to the beginning of the blob as follows:
5196%
5197% SEEK_SET Set position equal to offset bytes.
5198% SEEK_CUR Set position to current location plus offset.
5199% SEEK_END Set position to EOF plus offset.
5200%
5201*/
5202MagickExport MagickOffsetType SeekBlob(Image *image,
5203 const MagickOffsetType offset,const int whence)
5204{
5205 BlobInfo
5206 *magick_restrict blob_info;
5207
5208 assert(image != (Image *) NULL);
5209 assert(image->signature == MagickCoreSignature);
5210 assert(image->blob != (BlobInfo *) NULL);
5211 assert(image->blob->type != UndefinedStream);
5212 if (IsEventLogging() != MagickFalse)
5213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5214 blob_info=image->blob;
5215 switch (blob_info->type)
5216 {
5217 case UndefinedStream:
5218 break;
5219 case StandardStream:
5220 case PipeStream:
5221 return(-1);
5222 case FileStream:
5223 {
5224 if ((offset < 0) && (whence == SEEK_SET))
5225 return(-1);
5226 if (fseek(blob_info->file_info.file,offset,whence) < 0)
5227 return(-1);
5228 blob_info->offset=TellBlob(image);
5229 break;
5230 }
5231 case ZipStream:
5232 {
5233#if defined(MAGICKCORE_ZLIB_DELEGATE)
5234 if (gzseek(blob_info->file_info.gzfile,(long) offset,whence) < 0)
5235 return(-1);
5236#endif
5237 blob_info->offset=TellBlob(image);
5238 break;
5239 }
5240 case BZipStream:
5241 return(-1);
5242 case FifoStream:
5243 return(-1);
5244 case BlobStream:
5245 {
5246 switch (whence)
5247 {
5248 case SEEK_SET:
5249 default:
5250 {
5251 if (offset < 0)
5252 return(-1);
5253 blob_info->offset=offset;
5254 break;
5255 }
5256 case SEEK_CUR:
5257 {
5258 if (((offset > 0) && (blob_info->offset > (MAGICK_SSIZE_MAX-offset))) ||
5259 ((offset < 0) && (blob_info->offset < (MAGICK_SSIZE_MIN-offset))))
5260 {
5261 errno=EOVERFLOW;
5262 return(-1);
5263 }
5264 if ((blob_info->offset+offset) < 0)
5265 return(-1);
5266 blob_info->offset+=offset;
5267 break;
5268 }
5269 case SEEK_END:
5270 {
5271 if (((MagickOffsetType) blob_info->length+offset) < 0)
5272 return(-1);
5273 blob_info->offset=(MagickOffsetType) blob_info->length+offset;
5274 break;
5275 }
5276 }
5277 if (blob_info->offset < (MagickOffsetType) ((off_t) blob_info->length))
5278 {
5279 blob_info->eof=MagickFalse;
5280 break;
5281 }
5282 break;
5283 }
5284 case CustomStream:
5285 {
5286 if (blob_info->custom_stream->seeker == (CustomStreamSeeker) NULL)
5287 return(-1);
5288 blob_info->offset=blob_info->custom_stream->seeker(offset,whence,
5289 blob_info->custom_stream->data);
5290 break;
5291 }
5292 }
5293 return(blob_info->offset);
5294}
5295␌
5296/*
5297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5298% %
5299% %
5300% %
5301+ S e t B l o b E x e m p t %
5302% %
5303% %
5304% %
5305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5306%
5307% SetBlobExempt() sets the blob exempt status.
5308%
5309% The format of the SetBlobExempt method is:
5310%
5311% MagickBooleanType SetBlobExempt(const Image *image,
5312% const MagickBooleanType exempt)
5313%
5314% A description of each parameter follows:
5315%
5316% o image: the image.
5317%
5318% o exempt: Set to true if this blob is exempt from being closed.
5319%
5320*/
5321MagickExport void SetBlobExempt(Image *image,const MagickBooleanType exempt)
5322{
5323 assert(image != (const Image *) NULL);
5324 assert(image->signature == MagickCoreSignature);
5325 if (IsEventLogging() != MagickFalse)
5326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5327 image->blob->exempt=exempt;
5328}
5329␌
5330/*
5331%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5332% %
5333% %
5334% %
5335+ S e t B l o b E x t e n t %
5336% %
5337% %
5338% %
5339%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5340%
5341% SetBlobExtent() ensures enough space is allocated for the blob. If the
5342% method is successful, subsequent writes to bytes in the specified range are
5343% guaranteed not to fail.
5344%
5345% The format of the SetBlobExtent method is:
5346%
5347% MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
5348%
5349% A description of each parameter follows:
5350%
5351% o image: the image.
5352%
5353% o extent: the blob maximum extent.
5354%
5355*/
5356MagickExport MagickBooleanType SetBlobExtent(Image *image,
5357 const MagickSizeType extent)
5358{
5359 BlobInfo
5360 *magick_restrict blob_info;
5361
5362 assert(image != (Image *) NULL);
5363 assert(image->signature == MagickCoreSignature);
5364 assert(image->blob != (BlobInfo *) NULL);
5365 assert(image->blob->type != UndefinedStream);
5366 if (IsEventLogging() != MagickFalse)
5367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5368 blob_info=image->blob;
5369 switch (blob_info->type)
5370 {
5371 case UndefinedStream:
5372 break;
5373 case StandardStream:
5374 return(MagickFalse);
5375 case FileStream:
5376 {
5377 MagickOffsetType
5378 offset;
5379
5380 ssize_t
5381 count;
5382
5383 if (extent != (MagickSizeType) ((off_t) extent))
5384 return(MagickFalse);
5385 offset=SeekBlob(image,0,SEEK_END);
5386 if (offset < 0)
5387 return(MagickFalse);
5388 if ((MagickSizeType) offset >= extent)
5389 break;
5390 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5391 if (offset < 0)
5392 break;
5393 count=(ssize_t) fwrite((const unsigned char *) "",1,1,
5394 blob_info->file_info.file);
5395#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5396 if (blob_info->synchronize != MagickFalse)
5397 {
5398 int
5399 file;
5400
5401 file=fileno(blob_info->file_info.file);
5402 if ((file == -1) || (offset < 0))
5403 return(MagickFalse);
5404 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-offset);
5405 }
5406#endif
5407 offset=SeekBlob(image,offset,SEEK_SET);
5408 if (count != 1)
5409 return(MagickFalse);
5410 break;
5411 }
5412 case PipeStream:
5413 case ZipStream:
5414 return(MagickFalse);
5415 case BZipStream:
5416 return(MagickFalse);
5417 case FifoStream:
5418 return(MagickFalse);
5419 case BlobStream:
5420 {
5421 if (extent != (MagickSizeType) ((size_t) extent))
5422 return(MagickFalse);
5423 if (blob_info->mapped != MagickFalse)
5424 {
5425 MagickOffsetType
5426 offset;
5427
5428 ssize_t
5429 count;
5430
5431 (void) UnmapBlob(blob_info->data,blob_info->length);
5432 RelinquishMagickResource(MapResource,blob_info->length);
5433 if (extent != (MagickSizeType) ((off_t) extent))
5434 return(MagickFalse);
5435 offset=SeekBlob(image,0,SEEK_END);
5436 if (offset < 0)
5437 return(MagickFalse);
5438 if ((MagickSizeType) offset >= extent)
5439 break;
5440 offset=SeekBlob(image,(MagickOffsetType) extent-1,SEEK_SET);
5441 count=(ssize_t) fwrite((const unsigned char *) "",1,1,
5442 blob_info->file_info.file);
5443#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
5444 if (blob_info->synchronize != MagickFalse)
5445 {
5446 int
5447 file;
5448
5449 file=fileno(blob_info->file_info.file);
5450 if ((file == -1) || (offset < 0))
5451 return(MagickFalse);
5452 (void) posix_fallocate(file,offset,(MagickOffsetType) extent-
5453 offset);
5454 }
5455#endif
5456 offset=SeekBlob(image,offset,SEEK_SET);
5457 if (count != 1)
5458 return(MagickFalse);
5459 (void) AcquireMagickResource(MapResource,extent);
5460 blob_info->data=(unsigned char*) MapBlob(fileno(
5461 blob_info->file_info.file),WriteMode,0,(size_t) extent);
5462 blob_info->extent=(size_t) extent;
5463 blob_info->length=(size_t) extent;
5464 (void) SyncBlob(image);
5465 break;
5466 }
5467 blob_info->extent=(size_t) extent;
5468 blob_info->data=(unsigned char *) ResizeQuantumMemory(blob_info->data,
5469 blob_info->extent+1,sizeof(*blob_info->data));
5470 (void) SyncBlob(image);
5471 if (blob_info->data == (unsigned char *) NULL)
5472 {
5473 (void) DetachBlob(blob_info);
5474 return(MagickFalse);
5475 }
5476 break;
5477 }
5478 case CustomStream:
5479 break;
5480 }
5481 return(MagickTrue);
5482}
5483␌
5484/*
5485%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5486% %
5487% %
5488% %
5489+ S e t C u s t o m S t r e a m D a t a %
5490% %
5491% %
5492% %
5493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5494%
5495% SetCustomStreamData() sets the stream info data member.
5496%
5497% The format of the SetCustomStreamData method is:
5498%
5499% void SetCustomStreamData(CustomStreamInfo *custom_stream,void *)
5500%
5501% A description of each parameter follows:
5502%
5503% o custom_stream: the custom stream info.
5504%
5505% o data: an object containing information about the custom stream.
5506%
5507*/
5508MagickExport void SetCustomStreamData(CustomStreamInfo *custom_stream,
5509 void *data)
5510{
5511 assert(custom_stream != (CustomStreamInfo *) NULL);
5512 assert(custom_stream->signature == MagickCoreSignature);
5513 custom_stream->data=data;
5514}
5515␌
5516/*
5517%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5518% %
5519% %
5520% %
5521+ S e t C u s t o m S t r e a m R e a d e r %
5522% %
5523% %
5524% %
5525%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5526%
5527% SetCustomStreamReader() sets the stream info reader member.
5528%
5529% The format of the SetCustomStreamReader method is:
5530%
5531% void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5532% CustomStreamHandler reader)
5533%
5534% A description of each parameter follows:
5535%
5536% o custom_stream: the custom stream info.
5537%
5538% o reader: a function to read from the stream.
5539%
5540*/
5541MagickExport void SetCustomStreamReader(CustomStreamInfo *custom_stream,
5542 CustomStreamHandler reader)
5543{
5544 assert(custom_stream != (CustomStreamInfo *) NULL);
5545 assert(custom_stream->signature == MagickCoreSignature);
5546 custom_stream->reader=reader;
5547}
5548␌
5549/*
5550%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5551% %
5552% %
5553% %
5554+ S e t C u s t o m S t r e a m S e e k e r %
5555% %
5556% %
5557% %
5558%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5559%
5560% SetCustomStreamSeeker() sets the stream info seeker member.
5561%
5562% The format of the SetCustomStreamReader method is:
5563%
5564% void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5565% CustomStreamSeeker seeker)
5566%
5567% A description of each parameter follows:
5568%
5569% o custom_stream: the custom stream info.
5570%
5571% o seeker: a function to seek in the custom stream.
5572%
5573*/
5574MagickExport void SetCustomStreamSeeker(CustomStreamInfo *custom_stream,
5575 CustomStreamSeeker seeker)
5576{
5577 assert(custom_stream != (CustomStreamInfo *) NULL);
5578 assert(custom_stream->signature == MagickCoreSignature);
5579 custom_stream->seeker=seeker;
5580}
5581␌
5582/*
5583%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5584% %
5585% %
5586% %
5587+ S e t C u s t o m S t r e a m T e l l e r %
5588% %
5589% %
5590% %
5591%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5592%
5593% SetCustomStreamTeller() sets the stream info teller member.
5594%
5595% The format of the SetCustomStreamTeller method is:
5596%
5597% void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5598% CustomStreamTeller *teller)
5599%
5600% A description of each parameter follows:
5601%
5602% o custom_stream: the custom stream info.
5603%
5604% o teller: a function to set the position in the stream.
5605%
5606*/
5607MagickExport void SetCustomStreamTeller(CustomStreamInfo *custom_stream,
5608 CustomStreamTeller teller)
5609{
5610 assert(custom_stream != (CustomStreamInfo *) NULL);
5611 assert(custom_stream->signature == MagickCoreSignature);
5612 custom_stream->teller=teller;
5613}
5614␌
5615/*
5616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5617% %
5618% %
5619% %
5620+ S e t C u s t o m S t r e a m W r i t e r %
5621% %
5622% %
5623% %
5624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5625%
5626% SetCustomStreamWriter() sets the stream info writer member.
5627%
5628% The format of the SetCustomStreamWriter method is:
5629%
5630% void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5631% CustomStreamHandler *writer)
5632%
5633% A description of each parameter follows:
5634%
5635% o custom_stream: the custom stream info.
5636%
5637% o writer: a function to write to the custom stream.
5638%
5639*/
5640MagickExport void SetCustomStreamWriter(CustomStreamInfo *custom_stream,
5641 CustomStreamHandler writer)
5642{
5643 assert(custom_stream != (CustomStreamInfo *) NULL);
5644 assert(custom_stream->signature == MagickCoreSignature);
5645 custom_stream->writer=writer;
5646}
5647␌
5648/*
5649%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5650% %
5651% %
5652% %
5653+ S y n c B l o b %
5654% %
5655% %
5656% %
5657%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5658%
5659% SyncBlob() flushes the datastream if it is a file or synchronizes the data
5660% attributes if it is an blob. It returns 0 on success; otherwise, it returns
5661% -1 and set errno to indicate the error.
5662%
5663% The format of the SyncBlob method is:
5664%
5665% int SyncBlob(const Image *image)
5666%
5667% A description of each parameter follows:
5668%
5669% o image: the image.
5670%
5671*/
5672static int SyncBlob(const Image *image)
5673{
5674 BlobInfo
5675 *magick_restrict blob_info;
5676
5677 int
5678 status;
5679
5680 assert(image != (Image *) NULL);
5681 assert(image->signature == MagickCoreSignature);
5682 assert(image->blob != (BlobInfo *) NULL);
5683 if (IsEventLogging() != MagickFalse)
5684 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5685 if (EOFBlob(image) != 0)
5686 return(0);
5687 blob_info=image->blob;
5688 status=0;
5689 switch (blob_info->type)
5690 {
5691 case UndefinedStream:
5692 break;
5693 case StandardStream:
5694 {
5695 if ((blob_info->mode == WriteBlobMode) ||
5696 (blob_info->mode == WriteBinaryBlobMode) ||
5697 (blob_info->mode == AppendBlobMode) ||
5698 (blob_info->mode == AppendBinaryBlobMode))
5699 status=fflush(blob_info->file_info.file);
5700 break;
5701 }
5702 case FileStream:
5703 case PipeStream:
5704 {
5705 status=fflush(blob_info->file_info.file);
5706 break;
5707 }
5708 case ZipStream:
5709 {
5710#if defined(MAGICKCORE_ZLIB_DELEGATE)
5711 (void) gzflush(blob_info->file_info.gzfile,Z_SYNC_FLUSH);
5712#endif
5713 break;
5714 }
5715 case BZipStream:
5716 {
5717#if defined(MAGICKCORE_BZLIB_DELEGATE)
5718 status=BZ2_bzflush(blob_info->file_info.bzfile);
5719#endif
5720 break;
5721 }
5722 case FifoStream:
5723 break;
5724 case BlobStream:
5725 break;
5726 case CustomStream:
5727 break;
5728 }
5729 return(status);
5730}
5731␌
5732/*
5733%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5734% %
5735% %
5736% %
5737+ S y n c B l o b S t r e a m %
5738% %
5739% %
5740% %
5741%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5742%
5743% SyncBlobStream() transfers ownership of an in-memory blob stream from the
5744% image that has just been written back to the owner image and to image info.
5745% The method copies the current buffer state onto the owner, refreshes
5746% image info, and detaches the borrower so exactly one object owns the memory.
5747%
5748% The format of the SyncBlobStream method is:
5749%
5750% void SyncBlobStream(ImageInfo *image,const Image *image,Image *owner)
5751%
5752% A description of each parameter follows:
5753%
5754% o image_info: image info whose blob/length members are refreshed; may be
5755% NULL.
5756%
5757% o owner: the image ImagesToBlob()/ImageToBlob() harvests the blob from
5758% (the head of the list).
5759%
5760% o image: the image that was just written; may be the owner.
5761%
5762*/
5763MagickPrivate void SyncBlobStream(ImageInfo *image_info,Image *image,
5764 Image *owner)
5765{
5766 assert(owner != (Image *) NULL);
5767 assert(image != (Image *) NULL);
5768 assert(image->signature == MagickCoreSignature);
5769 if (image->blob == (BlobInfo *) NULL)
5770 return;
5771 if (image->blob->data == (unsigned char *) NULL)
5772 return;
5773 if (image_info != (ImageInfo *) NULL)
5774 {
5775 image_info->blob=(void *) image->blob->data;
5776 image_info->length=image->blob->length;
5777 }
5778 if ((image == owner) || (owner->blob == (BlobInfo *) NULL))
5779 return;
5780 if (owner->blob == (BlobInfo *) NULL)
5781 return;
5782 /*
5783 Promote the live buffer onto the owner before detaching the borrower;
5784 DetachBlob() clears image->blob->data.
5785 */
5786 owner->blob->data=image->blob->data;
5787 owner->blob->extent=image->blob->extent;
5788 owner->blob->length=image->blob->length;
5789 owner->blob->offset=image->blob->offset;
5790 owner->blob->quantum=image->blob->quantum;
5791 owner->blob->type=BlobStream;
5792 owner->blob->exempt=MagickTrue;
5793 (void) DetachBlob(image->blob);
5794}
5795␌
5796/*
5797%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5798% %
5799% %
5800% %
5801+ T e l l B l o b %
5802% %
5803% %
5804% %
5805%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5806%
5807% TellBlob() obtains the current value of the blob or file position.
5808%
5809% The format of the TellBlob method is:
5810%
5811% MagickOffsetType TellBlob(const Image *image)
5812%
5813% A description of each parameter follows:
5814%
5815% o image: the image.
5816%
5817*/
5818MagickExport MagickOffsetType TellBlob(const Image *image)
5819{
5820 BlobInfo
5821 *magick_restrict blob_info;
5822
5823 MagickOffsetType
5824 offset;
5825
5826 assert(image != (Image *) NULL);
5827 assert(image->signature == MagickCoreSignature);
5828 assert(image->blob != (BlobInfo *) NULL);
5829 assert(image->blob->type != UndefinedStream);
5830 if (IsEventLogging() != MagickFalse)
5831 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5832 blob_info=image->blob;
5833 offset=(-1);
5834 switch (blob_info->type)
5835 {
5836 case UndefinedStream:
5837 case StandardStream:
5838 break;
5839 case FileStream:
5840 {
5841 offset=ftell(blob_info->file_info.file);
5842 break;
5843 }
5844 case PipeStream:
5845 break;
5846 case ZipStream:
5847 {
5848#if defined(MAGICKCORE_ZLIB_DELEGATE)
5849 offset=(MagickOffsetType) gztell(blob_info->file_info.gzfile);
5850#endif
5851 break;
5852 }
5853 case BZipStream:
5854 break;
5855 case FifoStream:
5856 break;
5857 case BlobStream:
5858 {
5859 offset=blob_info->offset;
5860 break;
5861 }
5862 case CustomStream:
5863 {
5864 if (blob_info->custom_stream->teller != (CustomStreamTeller) NULL)
5865 offset=blob_info->custom_stream->teller(blob_info->custom_stream->data);
5866 break;
5867 }
5868 }
5869 return(offset);
5870}
5871␌
5872/*
5873%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5874% %
5875% %
5876% %
5877+ U n m a p B l o b %
5878% %
5879% %
5880% %
5881%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5882%
5883% UnmapBlob() deallocates the binary large object previously allocated with
5884% the MapBlob method.
5885%
5886% The format of the UnmapBlob method is:
5887%
5888% MagickBooleanType UnmapBlob(void *map,const size_t length)
5889%
5890% A description of each parameter follows:
5891%
5892% o map: the address of the binary large object.
5893%
5894% o length: the length of the binary large object.
5895%
5896*/
5897MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
5898{
5899#if defined(MAGICKCORE_HAVE_MMAP)
5900 int
5901 status;
5902
5903 status=munmap(map,length);
5904 return(status == -1 ? MagickFalse : MagickTrue);
5905#else
5906 (void) map;
5907 (void) length;
5908 return(MagickFalse);
5909#endif
5910}
5911␌
5912/*
5913%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5914% %
5915% %
5916% %
5917+ W r i t e B l o b %
5918% %
5919% %
5920% %
5921%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5922%
5923% WriteBlob() writes data to a blob or image file. It returns the number of
5924% bytes written.
5925%
5926% The format of the WriteBlob method is:
5927%
5928% ssize_t WriteBlob(Image *image,const size_t length,const void *data)
5929%
5930% A description of each parameter follows:
5931%
5932% o image: the image.
5933%
5934% o length: Specifies an integer representing the number of bytes to
5935% write to the file.
5936%
5937% o data: The address of the data to write to the blob or file.
5938%
5939*/
5940MagickExport ssize_t WriteBlob(Image *image,const size_t length,
5941 const void *data)
5942{
5943 BlobInfo
5944 *magick_restrict blob_info;
5945
5946 int
5947 c;
5948
5949 const unsigned char
5950 *p;
5951
5952 unsigned char
5953 *q;
5954
5955 ssize_t
5956 count;
5957
5958 assert(image != (Image *) NULL);
5959 assert(image->signature == MagickCoreSignature);
5960 assert(image->blob != (BlobInfo *) NULL);
5961 assert(image->blob->type != UndefinedStream);
5962 if (length == 0)
5963 return(0);
5964 assert(data != (const void *) NULL);
5965 blob_info=image->blob;
5966 count=0;
5967 p=(const unsigned char *) data;
5968 q=(unsigned char *) data;
5969 switch (blob_info->type)
5970 {
5971 case UndefinedStream:
5972 break;
5973 case StandardStream:
5974 case FileStream:
5975 case PipeStream:
5976 {
5977 switch (length)
5978 {
5979 default:
5980 {
5981 count=(ssize_t) fwrite((const char *) data,1,length,
5982 blob_info->file_info.file);
5983 break;
5984 }
5985 case 4:
5986 {
5987 c=putc((int) *p++,blob_info->file_info.file);
5988 if (c == EOF)
5989 break;
5990 count++;
5991 magick_fallthrough;
5992 }
5993 case 3:
5994 {
5995 c=putc((int) *p++,blob_info->file_info.file);
5996 if (c == EOF)
5997 break;
5998 count++;
5999 magick_fallthrough;
6000 }
6001 case 2:
6002 {
6003 c=putc((int) *p++,blob_info->file_info.file);
6004 if (c == EOF)
6005 break;
6006 count++;
6007 magick_fallthrough;
6008 }
6009 case 1:
6010 {
6011 c=putc((int) *p++,blob_info->file_info.file);
6012 if (c == EOF)
6013 break;
6014 count++;
6015 magick_fallthrough;
6016 }
6017 case 0:
6018 break;
6019 }
6020 if ((count != (ssize_t) length) &&
6021 (ferror(blob_info->file_info.file) != 0))
6022 ThrowBlobException(blob_info);
6023 break;
6024 }
6025 case ZipStream:
6026 {
6027#if defined(MAGICKCORE_ZLIB_DELEGATE)
6028 int
6029 status;
6030
6031 switch (length)
6032 {
6033 default:
6034 {
6035 size_t
6036 i;
6037
6038 for (i=0; i < length; i+=(size_t) count)
6039 {
6040 count=(ssize_t) gzwrite(blob_info->file_info.gzfile,q+i,
6041 (unsigned int) MagickMin(length-i,MagickMaxBufferExtent));
6042 if (count <= 0)
6043 {
6044 count=0;
6045 if (errno != EINTR)
6046 break;
6047 }
6048 }
6049 count=(ssize_t) i;
6050 break;
6051 }
6052 case 4:
6053 {
6054 c=gzputc(blob_info->file_info.gzfile,(int) *p++);
6055 if (c == EOF)
6056 break;
6057 count++;
6058 magick_fallthrough;
6059 }
6060 case 3:
6061 {
6062 c=gzputc(blob_info->file_info.gzfile,(int) *p++);
6063 if (c == EOF)
6064 break;
6065 count++;
6066 magick_fallthrough;
6067 }
6068 case 2:
6069 {
6070 c=gzputc(blob_info->file_info.gzfile,(int) *p++);
6071 if (c == EOF)
6072 break;
6073 count++;
6074 magick_fallthrough;
6075 }
6076 case 1:
6077 {
6078 c=gzputc(blob_info->file_info.gzfile,(int) *p++);
6079 if (c == EOF)
6080 break;
6081 count++;
6082 magick_fallthrough;
6083 }
6084 case 0:
6085 break;
6086 }
6087 status=Z_OK;
6088 (void) gzerror(blob_info->file_info.gzfile,&status);
6089 if ((count != (ssize_t) length) && (status != Z_OK))
6090 ThrowBlobException(blob_info);
6091#endif
6092 break;
6093 }
6094 case BZipStream:
6095 {
6096#if defined(MAGICKCORE_BZLIB_DELEGATE)
6097 int
6098 status;
6099
6100 size_t
6101 i;
6102
6103 for (i=0; i < length; i+=(size_t) count)
6104 {
6105 count=(ssize_t) BZ2_bzwrite(blob_info->file_info.bzfile,q+i,
6106 (int) MagickMin(length-i,MagickMaxBufferExtent));
6107 if (count <= 0)
6108 {
6109 count=0;
6110 if (errno != EINTR)
6111 break;
6112 }
6113 }
6114 count=(ssize_t) i;
6115 status=BZ_OK;
6116 (void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
6117 if ((count != (ssize_t) length) && (status != BZ_OK))
6118 ThrowBlobException(blob_info);
6119#endif
6120 break;
6121 }
6122 case FifoStream:
6123 {
6124 count=(ssize_t) blob_info->stream(image,data,length);
6125 break;
6126 }
6127 case BlobStream:
6128 {
6129 MagickSizeType
6130 extent;
6131
6132 if (blob_info->offset > (MagickOffsetType) (MAGICK_SSIZE_MAX-length))
6133 {
6134 errno=EOVERFLOW;
6135 return(0);
6136 }
6137 extent=(MagickSizeType) (blob_info->offset+(MagickOffsetType) length);
6138 if (extent >= blob_info->extent)
6139 {
6140 extent+=blob_info->quantum+length;
6141 blob_info->quantum<<=1;
6142 if (SetBlobExtent(image,extent) == MagickFalse)
6143 return(0);
6144 }
6145 q=blob_info->data+blob_info->offset;
6146 (void) memcpy(q,p,length);
6147 blob_info->offset+=(MagickOffsetType) length;
6148 if (blob_info->offset >= (MagickOffsetType) blob_info->length)
6149 blob_info->length=(size_t) blob_info->offset;
6150 count=(ssize_t) length;
6151 break;
6152 }
6153 case CustomStream:
6154 {
6155 if (blob_info->custom_stream->writer != (CustomStreamHandler) NULL)
6156 count=blob_info->custom_stream->writer((unsigned char *) data,
6157 length,blob_info->custom_stream->data);
6158 break;
6159 }
6160 }
6161 return(count);
6162}
6163␌
6164/*
6165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6166% %
6167% %
6168% %
6169+ W r i t e B l o b B y t e %
6170% %
6171% %
6172% %
6173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6174%
6175% WriteBlobByte() write an integer to a blob. It returns the number of bytes
6176% written (either 0 or 1);
6177%
6178% The format of the WriteBlobByte method is:
6179%
6180% ssize_t WriteBlobByte(Image *image,const unsigned char value)
6181%
6182% A description of each parameter follows.
6183%
6184% o image: the image.
6185%
6186% o value: Specifies the value to write.
6187%
6188*/
6189MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
6190{
6191 BlobInfo
6192 *magick_restrict blob_info;
6193
6194 ssize_t
6195 count;
6196
6197 assert(image != (Image *) NULL);
6198 assert(image->signature == MagickCoreSignature);
6199 assert(image->blob != (BlobInfo *) NULL);
6200 assert(image->blob->type != UndefinedStream);
6201 blob_info=image->blob;
6202 count=0;
6203 switch (blob_info->type)
6204 {
6205 case StandardStream:
6206 case FileStream:
6207 case PipeStream:
6208 {
6209 int
6210 c;
6211
6212 c=putc((int) value,blob_info->file_info.file);
6213 if (c == EOF)
6214 {
6215 if (ferror(blob_info->file_info.file) != 0)
6216 ThrowBlobException(blob_info);
6217 break;
6218 }
6219 count++;
6220 break;
6221 }
6222 default:
6223 {
6224 count=WriteBlobStream(image,1,&value);
6225 break;
6226 }
6227 }
6228 return(count);
6229}
6230
6231/*
6232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6233% %
6234% %
6235% %
6236+ W r i t e B l o b P a d d i n g %
6237% %
6238% %
6239% %
6240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6241%
6242% WriteBlobPadding() writes length copies of a byte value to a blob. It
6243% returns the number of bytes written, stopping on a short or failed write.
6244% Lengths greater than MAGICK_SSIZE_MAX return zero and set errno to EOVERFLOW.
6245%
6246% The format of the WriteBlobPadding method is:
6247%
6248% ssize_t WriteBlobPadding(Image *image,const size_t length,
6249% const char value)
6250%
6251% A description of each parameter follows.
6252%
6253% o image: the image.
6254%
6255% o length: the number of bytes to write.
6256%
6257% o value: the byte value to repeat.
6258%
6259*/
6260MagickExport ssize_t WriteBlobPadding(Image *image,const size_t length,
6261 const char value)
6262{
6263 size_t
6264 extent;
6265
6266 ssize_t
6267 count,
6268 total;
6269
6270 unsigned char
6271 padding[1024];
6272
6273 assert(image != (Image *) NULL);
6274 assert(image->signature == MagickCoreSignature);
6275 assert(image->blob != (BlobInfo *) NULL);
6276 assert(image->blob->type != UndefinedStream);
6277 if (length == 0)
6278 return(0);
6279 if (length > (size_t) MAGICK_SSIZE_MAX)
6280 {
6281 errno=EOVERFLOW;
6282 return(0);
6283 }
6284 (void) memset(padding,value,sizeof(padding));
6285 total=0;
6286 while ((size_t) total < length)
6287 {
6288 extent=MagickMin(sizeof(padding),length-(size_t) total);
6289 count=WriteBlob(image,extent,padding);
6290 if (count <= 0)
6291 break;
6292 total+=count;
6293 if (count != (ssize_t) extent)
6294 break;
6295 }
6296 return(total);
6297}
6298␌
6299/*
6300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6301% %
6302% %
6303% %
6304+ W r i t e B l o b F l o a t %
6305% %
6306% %
6307% %
6308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6309%
6310% WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
6311% specified by the endian member of the image structure.
6312%
6313% The format of the WriteBlobFloat method is:
6314%
6315% ssize_t WriteBlobFloat(Image *image,const float value)
6316%
6317% A description of each parameter follows.
6318%
6319% o image: the image.
6320%
6321% o value: Specifies the value to write.
6322%
6323*/
6324MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
6325{
6326 union
6327 {
6328 unsigned int
6329 unsigned_value;
6330
6331 float
6332 float_value;
6333 } quantum;
6334
6335 quantum.unsigned_value=0U;
6336 quantum.float_value=value;
6337 return(WriteBlobLong(image,quantum.unsigned_value));
6338}
6339␌
6340/*
6341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6342% %
6343% %
6344% %
6345+ W r i t e B l o b L o n g %
6346% %
6347% %
6348% %
6349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6350%
6351% WriteBlobLong() writes a unsigned int value as a 32-bit quantity in the
6352% byte-order specified by the endian member of the image structure.
6353%
6354% The format of the WriteBlobLong method is:
6355%
6356% ssize_t WriteBlobLong(Image *image,const unsigned int value)
6357%
6358% A description of each parameter follows.
6359%
6360% o image: the image.
6361%
6362% o value: Specifies the value to write.
6363%
6364*/
6365MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
6366{
6367 unsigned char
6368 buffer[4];
6369
6370 assert(image != (Image *) NULL);
6371 assert(image->signature == MagickCoreSignature);
6372 if (image->endian == LSBEndian)
6373 {
6374 buffer[0]=(unsigned char) value;
6375 buffer[1]=(unsigned char) (value >> 8);
6376 buffer[2]=(unsigned char) (value >> 16);
6377 buffer[3]=(unsigned char) (value >> 24);
6378 return(WriteBlobStream(image,4,buffer));
6379 }
6380 buffer[0]=(unsigned char) (value >> 24);
6381 buffer[1]=(unsigned char) (value >> 16);
6382 buffer[2]=(unsigned char) (value >> 8);
6383 buffer[3]=(unsigned char) value;
6384 return(WriteBlobStream(image,4,buffer));
6385}
6386␌
6387/*
6388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6389% %
6390% %
6391% %
6392+ W r i t e B l o b L o n g L o n g %
6393% %
6394% %
6395% %
6396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6397%
6398% WriteBlobMSBLongLong() writes a long long value as a 64-bit quantity in the
6399% byte-order specified by the endian member of the image structure.
6400%
6401% The format of the WriteBlobLongLong method is:
6402%
6403% ssize_t WriteBlobLongLong(Image *image,const MagickSizeType value)
6404%
6405% A description of each parameter follows.
6406%
6407% o value: Specifies the value to write.
6408%
6409% o image: the image.
6410%
6411*/
6412MagickExport ssize_t WriteBlobLongLong(Image *image,const MagickSizeType value)
6413{
6414 unsigned char
6415 buffer[8];
6416
6417 assert(image != (Image *) NULL);
6418 assert(image->signature == MagickCoreSignature);
6419 if (image->endian == LSBEndian)
6420 {
6421 buffer[0]=(unsigned char) value;
6422 buffer[1]=(unsigned char) (value >> 8);
6423 buffer[2]=(unsigned char) (value >> 16);
6424 buffer[3]=(unsigned char) (value >> 24);
6425 buffer[4]=(unsigned char) (value >> 32);
6426 buffer[5]=(unsigned char) (value >> 40);
6427 buffer[6]=(unsigned char) (value >> 48);
6428 buffer[7]=(unsigned char) (value >> 56);
6429 return(WriteBlobStream(image,8,buffer));
6430 }
6431 buffer[0]=(unsigned char) (value >> 56);
6432 buffer[1]=(unsigned char) (value >> 48);
6433 buffer[2]=(unsigned char) (value >> 40);
6434 buffer[3]=(unsigned char) (value >> 32);
6435 buffer[4]=(unsigned char) (value >> 24);
6436 buffer[5]=(unsigned char) (value >> 16);
6437 buffer[6]=(unsigned char) (value >> 8);
6438 buffer[7]=(unsigned char) value;
6439 return(WriteBlobStream(image,8,buffer));
6440}
6441␌
6442/*
6443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6444% %
6445% %
6446% %
6447+ W r i t e B l o b S h o r t %
6448% %
6449% %
6450% %
6451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6452%
6453% WriteBlobShort() writes a short value as a 16-bit quantity in the
6454% byte-order specified by the endian member of the image structure.
6455%
6456% The format of the WriteBlobShort method is:
6457%
6458% ssize_t WriteBlobShort(Image *image,const unsigned short value)
6459%
6460% A description of each parameter follows.
6461%
6462% o image: the image.
6463%
6464% o value: Specifies the value to write.
6465%
6466*/
6467MagickExport ssize_t WriteBlobShort(Image *image,const unsigned short value)
6468{
6469 unsigned char
6470 buffer[2];
6471
6472 assert(image != (Image *) NULL);
6473 assert(image->signature == MagickCoreSignature);
6474 if (image->endian == LSBEndian)
6475 {
6476 buffer[0]=(unsigned char) value;
6477 buffer[1]=(unsigned char) (value >> 8);
6478 return(WriteBlobStream(image,2,buffer));
6479 }
6480 buffer[0]=(unsigned char) (value >> 8);
6481 buffer[1]=(unsigned char) value;
6482 return(WriteBlobStream(image,2,buffer));
6483}
6484␌
6485/*
6486%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6487% %
6488% %
6489% %
6490+ W r i t e B l o b S i g n e d L o n g %
6491% %
6492% %
6493% %
6494%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6495%
6496% WriteBlobSignedLong() writes a signed value as a 32-bit quantity in the
6497% byte-order specified by the endian member of the image structure.
6498%
6499% The format of the WriteBlobSignedLong method is:
6500%
6501% ssize_t WriteBlobSignedLong(Image *image,const signed int value)
6502%
6503% A description of each parameter follows.
6504%
6505% o image: the image.
6506%
6507% o value: Specifies the value to write.
6508%
6509*/
6510MagickExport ssize_t WriteBlobSignedLong(Image *image,const signed int value)
6511{
6512 union
6513 {
6514 unsigned int
6515 unsigned_value;
6516
6517 signed int
6518 signed_value;
6519 } quantum;
6520
6521 unsigned char
6522 buffer[4];
6523
6524 assert(image != (Image *) NULL);
6525 assert(image->signature == MagickCoreSignature);
6526 quantum.signed_value=value;
6527 if (image->endian == LSBEndian)
6528 {
6529 buffer[0]=(unsigned char) quantum.unsigned_value;
6530 buffer[1]=(unsigned char) (quantum.unsigned_value >> 8);
6531 buffer[2]=(unsigned char) (quantum.unsigned_value >> 16);
6532 buffer[3]=(unsigned char) (quantum.unsigned_value >> 24);
6533 return(WriteBlobStream(image,4,buffer));
6534 }
6535 buffer[0]=(unsigned char) (quantum.unsigned_value >> 24);
6536 buffer[1]=(unsigned char) (quantum.unsigned_value >> 16);
6537 buffer[2]=(unsigned char) (quantum.unsigned_value >> 8);
6538 buffer[3]=(unsigned char) quantum.unsigned_value;
6539 return(WriteBlobStream(image,4,buffer));
6540}
6541␌
6542/*
6543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6544% %
6545% %
6546% %
6547+ W r i t e B l o b L S B L o n g %
6548% %
6549% %
6550% %
6551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6552%
6553% WriteBlobLSBLong() writes a unsigned int value as a 32-bit quantity in
6554% least-significant byte first order.
6555%
6556% The format of the WriteBlobLSBLong method is:
6557%
6558% ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
6559%
6560% A description of each parameter follows.
6561%
6562% o image: the image.
6563%
6564% o value: Specifies the value to write.
6565%
6566*/
6567MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
6568{
6569 unsigned char
6570 buffer[4];
6571
6572 assert(image != (Image *) NULL);
6573 assert(image->signature == MagickCoreSignature);
6574 buffer[0]=(unsigned char) value;
6575 buffer[1]=(unsigned char) (value >> 8);
6576 buffer[2]=(unsigned char) (value >> 16);
6577 buffer[3]=(unsigned char) (value >> 24);
6578 return(WriteBlobStream(image,4,buffer));
6579}
6580␌
6581/*
6582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6583% %
6584% %
6585% %
6586+ W r i t e B l o b L S B S h o r t %
6587% %
6588% %
6589% %
6590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6591%
6592% WriteBlobLSBShort() writes a unsigned short value as a 16-bit quantity in
6593% least-significant byte first order.
6594%
6595% The format of the WriteBlobLSBShort method is:
6596%
6597% ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
6598%
6599% A description of each parameter follows.
6600%
6601% o image: the image.
6602%
6603% o value: Specifies the value to write.
6604%
6605*/
6606MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
6607{
6608 unsigned char
6609 buffer[2];
6610
6611 assert(image != (Image *) NULL);
6612 assert(image->signature == MagickCoreSignature);
6613 buffer[0]=(unsigned char) value;
6614 buffer[1]=(unsigned char) (value >> 8);
6615 return(WriteBlobStream(image,2,buffer));
6616}
6617␌
6618/*
6619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6620% %
6621% %
6622% %
6623+ W r i t e B l o b L S B S i g n e d L o n g %
6624% %
6625% %
6626% %
6627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6628%
6629% WriteBlobLSBSignedLong() writes a signed value as a 32-bit quantity in
6630% least-significant byte first order.
6631%
6632% The format of the WriteBlobLSBSignedLong method is:
6633%
6634% ssize_t WriteBlobLSBSignedLong(Image *image,const signed int value)
6635%
6636% A description of each parameter follows.
6637%
6638% o image: the image.
6639%
6640% o value: Specifies the value to write.
6641%
6642*/
6643MagickExport ssize_t WriteBlobLSBSignedLong(Image *image,const signed int value)
6644{
6645 union
6646 {
6647 unsigned int
6648 unsigned_value;
6649
6650 signed int
6651 signed_value;
6652 } quantum;
6653
6654 unsigned char
6655 buffer[4];
6656
6657 assert(image != (Image *) NULL);
6658 assert(image->signature == MagickCoreSignature);
6659 quantum.signed_value=value;
6660 buffer[0]=(unsigned char) quantum.unsigned_value;
6661 buffer[1]=(unsigned char) (quantum.unsigned_value >> 8);
6662 buffer[2]=(unsigned char) (quantum.unsigned_value >> 16);
6663 buffer[3]=(unsigned char) (quantum.unsigned_value >> 24);
6664 return(WriteBlobStream(image,4,buffer));
6665}
6666␌
6667/*
6668%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6669% %
6670% %
6671% %
6672+ W r i t e B l o b L S B S i g n e d S h o r t %
6673% %
6674% %
6675% %
6676%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6677%
6678% WriteBlobLSBSignedShort() writes a signed short value as a 16-bit quantity
6679% in least-significant byte first order.
6680%
6681% The format of the WriteBlobLSBSignedShort method is:
6682%
6683% ssize_t WriteBlobLSBSignedShort(Image *image,const signed short value)
6684%
6685% A description of each parameter follows.
6686%
6687% o image: the image.
6688%
6689% o value: Specifies the value to write.
6690%
6691*/
6692MagickExport ssize_t WriteBlobLSBSignedShort(Image *image,
6693 const signed short value)
6694{
6695 union
6696 {
6697 unsigned short
6698 unsigned_value;
6699
6700 signed short
6701 signed_value;
6702 } quantum;
6703
6704 unsigned char
6705 buffer[2];
6706
6707 assert(image != (Image *) NULL);
6708 assert(image->signature == MagickCoreSignature);
6709 quantum.signed_value=value;
6710 buffer[0]=(unsigned char) quantum.unsigned_value;
6711 buffer[1]=(unsigned char) (quantum.unsigned_value >> 8);
6712 return(WriteBlobStream(image,2,buffer));
6713}
6714␌
6715/*
6716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6717% %
6718% %
6719% %
6720+ W r i t e B l o b M S B L o n g %
6721% %
6722% %
6723% %
6724%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6725%
6726% WriteBlobMSBLong() writes a unsigned int value as a 32-bit quantity in
6727% most-significant byte first order.
6728%
6729% The format of the WriteBlobMSBLong method is:
6730%
6731% ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
6732%
6733% A description of each parameter follows.
6734%
6735% o value: Specifies the value to write.
6736%
6737% o image: the image.
6738%
6739*/
6740MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
6741{
6742 unsigned char
6743 buffer[4];
6744
6745 assert(image != (Image *) NULL);
6746 assert(image->signature == MagickCoreSignature);
6747 buffer[0]=(unsigned char) (value >> 24);
6748 buffer[1]=(unsigned char) (value >> 16);
6749 buffer[2]=(unsigned char) (value >> 8);
6750 buffer[3]=(unsigned char) value;
6751 return(WriteBlobStream(image,4,buffer));
6752}
6753␌
6754/*
6755%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6756% %
6757% %
6758% %
6759+ W r i t e B l o b M S B S i g n e d S h o r t %
6760% %
6761% %
6762% %
6763%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6764%
6765% WriteBlobMSBSignedShort() writes a signed short value as a 16-bit quantity
6766% in most-significant byte first order.
6767%
6768% The format of the WriteBlobMSBSignedShort method is:
6769%
6770% ssize_t WriteBlobMSBSignedShort(Image *image,const signed short value)
6771%
6772% A description of each parameter follows.
6773%
6774% o image: the image.
6775%
6776% o value: Specifies the value to write.
6777%
6778*/
6779MagickExport ssize_t WriteBlobMSBSignedShort(Image *image,
6780 const signed short value)
6781{
6782 union
6783 {
6784 unsigned short
6785 unsigned_value;
6786
6787 signed short
6788 signed_value;
6789 } quantum;
6790
6791 unsigned char
6792 buffer[2];
6793
6794 assert(image != (Image *) NULL);
6795 assert(image->signature == MagickCoreSignature);
6796 quantum.signed_value=value;
6797 buffer[0]=(unsigned char) (quantum.unsigned_value >> 8);
6798 buffer[1]=(unsigned char) quantum.unsigned_value;
6799 return(WriteBlobStream(image,2,buffer));
6800}
6801␌
6802/*
6803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6804% %
6805% %
6806% %
6807+ W r i t e B l o b M S B S h o r t %
6808% %
6809% %
6810% %
6811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6812%
6813% WriteBlobMSBShort() writes a unsigned short value as a 16-bit quantity in
6814% most-significant byte first order.
6815%
6816% The format of the WriteBlobMSBShort method is:
6817%
6818% ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
6819%
6820% A description of each parameter follows.
6821%
6822% o value: Specifies the value to write.
6823%
6824% o file: Specifies the file to write the data to.
6825%
6826*/
6827MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
6828{
6829 unsigned char
6830 buffer[2];
6831
6832 assert(image != (Image *) NULL);
6833 assert(image->signature == MagickCoreSignature);
6834 buffer[0]=(unsigned char) (value >> 8);
6835 buffer[1]=(unsigned char) value;
6836 return(WriteBlobStream(image,2,buffer));
6837}
6838␌
6839/*
6840%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6841% %
6842% %
6843% %
6844+ W r i t e B l o b S t r i n g %
6845% %
6846% %
6847% %
6848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6849%
6850% WriteBlobString() write a string to a blob. It returns the number of
6851% characters written.
6852%
6853% The format of the WriteBlobString method is:
6854%
6855% ssize_t WriteBlobString(Image *image,const char *string)
6856%
6857% A description of each parameter follows.
6858%
6859% o image: the image.
6860%
6861% o string: Specifies the string to write.
6862%
6863*/
6864MagickExport ssize_t WriteBlobString(Image *image,const char *string)
6865{
6866 assert(image != (Image *) NULL);
6867 assert(image->signature == MagickCoreSignature);
6868 assert(string != (const char *) NULL);
6869 return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));
6870}