]> granicus.if.org Git - imagemagick/blob - coders/inline.c
58643a86866fc19e1c75123bbaf6671cb42cacdf
[imagemagick] / coders / inline.c
1 /*
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 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 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 %    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 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/client.h"
46 #include "MagickCore/constitute.h"
47 #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"
63 \f
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
90 static 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
97 static 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
109   size_t
110     quantum;
111
112   ssize_t
113     count;
114
115   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));
130   image=AcquireImage(image_info,exception);
131   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   if (quantum == 0)
139     quantum=MagickMaxBufferExtent;
140   inline_image=(unsigned char *) AcquireQuantumMemory(quantum,
141     sizeof(*inline_image));
142   count=0;
143   for (i=0; inline_image != (unsigned char *) NULL; i+=count)
144   {
145     count=(ssize_t) ReadBlob(image,quantum,inline_image+i);
146     if (count <= 0)
147       {
148         count=0;
149         if (errno != EINTR)
150           break;
151       }
152     if (~((size_t) i) < (quantum+1))
153       {
154         inline_image=(unsigned char *) RelinquishMagickMemory(inline_image);
155         break;
156       }
157     inline_image=(unsigned char *) ResizeQuantumMemory(inline_image,i+quantum+1,
158       sizeof(*inline_image));
159   }
160   if (inline_image == (unsigned char *) NULL)
161     {
162       (void) ThrowMagickException(exception,GetMagickModule(),
163         ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
164       return((Image *) NULL);
165     }
166   inline_image[i+count]='\0';
167   image=DestroyImageList(image);
168   image=ReadInlineImage(image_info,(char *) inline_image,exception);
169   inline_image=(unsigned char *) RelinquishMagickMemory(inline_image);
170   return(image);
171 }
172 \f
173 /*
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 %                                                                             %
176 %                                                                             %
177 %                                                                             %
178 %   R e g i s t e r I N L I N E I m a g e                                     %
179 %                                                                             %
180 %                                                                             %
181 %                                                                             %
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 %
184 %  RegisterINLINEImage() adds attributes for the INLINE image format to
185 %  the list of supported formats.  The attributes include the image format
186 %  tag, a method to read and/or write the format, whether the format
187 %  supports the saving of more than one frame to the same file or blob,
188 %  whether the format supports native in-memory I/O, and a brief
189 %  description of the format.
190 %
191 %  The format of the RegisterINLINEImage method is:
192 %
193 %      size_t RegisterINLINEImage(void)
194 %
195 */
196 ModuleExport size_t RegisterINLINEImage(void)
197 {
198   MagickInfo
199     *entry;
200
201   entry=SetMagickInfo("INLINE");
202   entry->decoder=(DecodeImageHandler *) ReadINLINEImage;
203   entry->format_type=ImplicitFormatType;
204   entry->description=ConstantString("Base64-encoded inline images");
205   entry->module=ConstantString("INLINE");
206   (void) RegisterMagickInfo(entry);
207   return(MagickImageCoderSignature);
208 }
209 \f
210 /*
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 %                                                                             %
213 %                                                                             %
214 %                                                                             %
215 %   U n r e g i s t e r I N L I N E I m a g e                                 %
216 %                                                                             %
217 %                                                                             %
218 %                                                                             %
219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 %
221 %  UnregisterINLINEImage() removes format registrations made by the
222 %  INLINE module from the list of supported formats.
223 %
224 %  The format of the UnregisterINLINEImage method is:
225 %
226 %      UnregisterINLINEImage(void)
227 %
228 */
229 ModuleExport void UnregisterINLINEImage(void)
230 {
231   (void) UnregisterMagickInfo("INLINE");
232 }