]> granicus.if.org Git - imagemagick/blob - coders/inline.c
83ed4d08d997940876c4748d025c29c96b3c6f14
[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 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2012 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   inline_image=(unsigned char *) AcquireQuantumMemory(quantum,
139     sizeof(*inline_image));
140   count=0;
141   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 (~((size_t) 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 \f
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 %
191 %      size_t RegisterINLINEImage(void)
192 %
193 */
194 ModuleExport size_t RegisterINLINEImage(void)
195 {
196   MagickInfo
197     *entry;
198
199   entry=SetMagickInfo("INLINE");
200   entry->decoder=(DecodeImageHandler *) ReadINLINEImage;
201   entry->format_type=ImplicitFormatType;
202   entry->description=ConstantString("Base64-encoded inline images");
203   entry->module=ConstantString("INLINE");
204   (void) RegisterMagickInfo(entry);
205   return(MagickImageCoderSignature);
206 }
207 \f
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 */
227 ModuleExport void UnregisterINLINEImage(void)
228 {
229   (void) UnregisterMagickInfo("INLINE");
230 }