blob: d9ba97e777e1c5505c1b9db94ab487c8be70bd57 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% IIIII N N L IIIII N N EEEEE %
7% I NN N L I NN N E %
8% I N N N L I N N N EEE %
9% I N NN L I N NN E %
10% IIIII N N LLLLL IIIII N N EEEEE %
11% %
12% %
13% Read Inline Images %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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% http://www.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 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/client.h"
cristy3f858f42012-01-07 00:03:25 +000046#include "MagickCore/constitute.h"
cristy4c08aed2011-07-01 19:47:50 +000047#include "MagickCore/display.h"
48#include "MagickCore/exception.h"
49#include "MagickCore/exception-private.h"
50#include "MagickCore/image.h"
51#include "MagickCore/image-private.h"
52#include "MagickCore/list.h"
53#include "MagickCore/magick.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/option.h"
56#include "MagickCore/quantum-private.h"
57#include "MagickCore/static.h"
58#include "MagickCore/string_.h"
59#include "MagickCore/module.h"
60#include "MagickCore/utility.h"
61#include "MagickCore/xwindow.h"
62#include "MagickCore/xwindow-private.h"
cristy3ed852e2009-09-05 21:47:34 +000063
64/*
65%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66% %
67% %
68% %
69% R e a d I N L I N E I m a g e %
70% %
71% %
72% %
73%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74%
75% ReadINLINEImage() reads base64-encoded inlines images.
76%
77% The format of the ReadINLINEImage method is:
78%
79% Image *ReadINLINEImage(const ImageInfo *image_info,
80% ExceptionInfo *exception)
81%
82% A description of each parameter follows:
83%
84% o image_info: the image info.
85%
86% o exception: return any errors or warnings in this structure.
87%
88*/
89
90static inline size_t MagickMin(const size_t x,const size_t y)
91{
92 if (x < y)
93 return(x);
94 return(y);
95}
96
97static Image *ReadINLINEImage(const ImageInfo *image_info,
98 ExceptionInfo *exception)
99{
100 Image
101 *image;
102
103 MagickBooleanType
104 status;
105
106 register size_t
107 i;
108
cristy3ed852e2009-09-05 21:47:34 +0000109 size_t
110 quantum;
111
cristyebc891a2011-04-24 23:04:16 +0000112 ssize_t
113 count;
114
cristy3ed852e2009-09-05 21:47:34 +0000115 unsigned char
116 *inline_image;
117
118 /*
119 Open image file.
120 */
121 assert(image_info != (const ImageInfo *) NULL);
122 assert(image_info->signature == MagickSignature);
123 if (image_info->debug != MagickFalse)
124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
125 image_info->filename);
126 assert(exception != (ExceptionInfo *) NULL);
127 assert(exception->signature == MagickSignature);
128 if (LocaleNCompare(image_info->filename,"data:",5) == 0)
129 return(ReadInlineImage(image_info,image_info->filename,exception));
cristy9950d572011-10-01 18:22:35 +0000130 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000131 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
132 if (status == MagickFalse)
133 {
134 image=DestroyImageList(image);
135 return((Image *) NULL);
136 }
137 quantum=MagickMin((size_t) GetBlobSize(image),MagickMaxBufferExtent);
138 inline_image=(unsigned char *) AcquireQuantumMemory(quantum,
139 sizeof(*inline_image));
cristy564a5692012-01-20 23:56:26 +0000140 count=0;
cristy3ed852e2009-09-05 21:47:34 +0000141 for (i=0; inline_image != (unsigned char *) NULL; i+=count)
142 {
143 count=(ssize_t) ReadBlob(image,quantum,inline_image+i);
144 if (count <= 0)
145 {
146 count=0;
147 if (errno != EINTR)
148 break;
149 }
150 if (~(1UL*i) < (quantum+1))
151 {
152 inline_image=(unsigned char *) RelinquishMagickMemory(inline_image);
153 break;
154 }
155 inline_image=(unsigned char *) ResizeQuantumMemory(inline_image,i+quantum+1,
156 sizeof(*inline_image));
157 }
158 if (inline_image == (unsigned char *) NULL)
159 {
160 (void) ThrowMagickException(exception,GetMagickModule(),
161 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
162 return((Image *) NULL);
163 }
164 inline_image[i+count]='\0';
165 image=DestroyImageList(image);
166 image=ReadInlineImage(image_info,(char *) inline_image,exception);
167 inline_image=(unsigned char *) RelinquishMagickMemory(inline_image);
168 return(image);
169}
170
171/*
172%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173% %
174% %
175% %
176% R e g i s t e r I N L I N E I m a g e %
177% %
178% %
179% %
180%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181%
182% RegisterINLINEImage() adds attributes for the INLINE image format to
183% the list of supported formats. The attributes include the image format
184% tag, a method to read and/or write the format, whether the format
185% supports the saving of more than one frame to the same file or blob,
186% whether the format supports native in-memory I/O, and a brief
187% description of the format.
188%
189% The format of the RegisterINLINEImage method is:
190%
cristybb503372010-05-27 20:51:26 +0000191% size_t RegisterINLINEImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000192%
193*/
cristybb503372010-05-27 20:51:26 +0000194ModuleExport size_t RegisterINLINEImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000195{
196 MagickInfo
197 *entry;
198
199 entry=SetMagickInfo("INLINE");
200 entry->decoder=(DecodeImageHandler *) ReadINLINEImage;
cristy009d7392010-07-25 22:08:41 +0000201 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000202 entry->description=ConstantString("Base64-encoded inline images");
203 entry->module=ConstantString("INLINE");
204 (void) RegisterMagickInfo(entry);
205 return(MagickImageCoderSignature);
206}
207
208/*
209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210% %
211% %
212% %
213% U n r e g i s t e r I N L I N E I m a g e %
214% %
215% %
216% %
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218%
219% UnregisterINLINEImage() removes format registrations made by the
220% INLINE module from the list of supported formats.
221%
222% The format of the UnregisterINLINEImage method is:
223%
224% UnregisterINLINEImage(void)
225%
226*/
227ModuleExport void UnregisterINLINEImage(void)
228{
229 (void) UnregisterMagickInfo("INLINE");
230}