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