1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % OOO RRRR AAA %
7 % O O R R A A %
8 % O O RRRR AAAAA %
9 % O O R R A A %
10 % OOO R R A A %
11 % %
12 % %
13 % Read OpenRaster (.ora) files %
14 % %
15 % OpenRaster spec: %
16 % https://www.freedesktop.org/wiki/Specifications/OpenRaster/ %
17 % %
18 % Implementer %
19 % Christopher Chianelli %
20 % August 2020 %
21 % %
22 % %
23 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
24 % dedicated to making software imaging solutions freely available. %
25 % %
26 % You may not use this file except in compliance with the License. You may %
27 % obtain a copy of the License at %
28 % %
29 % https://imagemagick.org/script/license.php %
30 % %
31 % Unless required by applicable law or agreed to in writing, software %
32 % distributed under the License is distributed on an "AS IS" BASIS, %
33 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
34 % See the License for the specific language governing permissions and %
35 % limitations under the License. %
36 % %
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %
39 %
40 */
41
42 /*
43 Include declarations.
44 */
45 #include "MagickCore/studio.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/constitute.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/module.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/resource_.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/utility.h"
62
63 #if defined(MAGICKCORE_ZIP_DELEGATE)
64 #include <zip.h>
65 #endif
66
67 /*
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 % %
70 % %
71 % %
72 % R e a d O R A I m a g e %
73 % %
74 % %
75 % %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %
78 % ReadORAImage reads an ORA file in the most basic way possible: by
79 % reading it as a ZIP File and extracting the mergedimage.png file from it,
80 % which is then passed to ReadPNGImage.
81 %
82 % https://www.freedesktop.org/wiki/Specifications/OpenRaster/Draft/FileLayout/
83 %
84 % The format of the ReadORAImage method is:
85 %
86 % Image *ReadORAImage(const ImageInfo *image_info,ExceptionInfo *exception)
87 %
88 % A description of each parameter follows:
89 %
90 % o image_info: the image info.
91 %
92 % o exception: return any errors or warnings in this structure.
93 %
94 */
95
96 #if defined(__cplusplus) || defined(c_plusplus)
97 extern "C" {
98 #endif
99
100 #if defined(__cplusplus) || defined(c_plusplus)
101 }
102 #endif
103
104 #if defined(MAGICKCORE_PNG_DELEGATE) && defined(MAGICKCORE_ZIP_DELEGATE)
ReadORAImage(const ImageInfo * image_info,ExceptionInfo * exception)105 static Image *ReadORAImage(const ImageInfo *image_info,
106 ExceptionInfo *exception)
107 {
108 char
109 image_data_buffer[8192];
110
111 const char
112 *MERGED_IMAGE_PATH = "mergedimage.png";
113
114 FILE
115 *file;
116
117 Image
118 *image_metadata,
119 *out_image;
120
121 ImageInfo
122 *read_info;
123
124 int
125 unique_file,
126 zip_error;
127
128 MagickBooleanType
129 status;
130
131 struct stat
132 stat_info;
133
134 zip_t
135 *zip_archive;
136
137 zip_file_t
138 *merged_image_file;
139
140 zip_uint64_t
141 read_bytes,
142 offset;
143
144 image_metadata=AcquireImage(image_info,exception);
145 read_info=CloneImageInfo(image_info);
146 SetImageInfoBlob(read_info,(void *) NULL,0);
147 stat(image_info->filename,&stat_info);
148 zip_archive=zip_open(image_info->filename,ZIP_RDONLY,&zip_error);
149 if (zip_archive == NULL)
150 {
151 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
152 image_info->filename);
153 read_info=DestroyImageInfo(read_info);
154 image_metadata=DestroyImage(image_metadata);
155 return((Image *) NULL);
156 }
157 merged_image_file=zip_fopen(zip_archive,MERGED_IMAGE_PATH,ZIP_FL_UNCHANGED);
158 if (merged_image_file == NULL)
159 {
160 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
161 image_info->filename);
162 read_info=DestroyImageInfo(read_info);
163 image_metadata=DestroyImage(image_metadata);
164 zip_discard(zip_archive);
165 return((Image *) NULL);
166 }
167 /* Get a temporary file to write the mergedimage.png of the ZIP to */
168 (void) CopyMagickString(read_info->magick,"PNG",MagickPathExtent);
169 unique_file=AcquireUniqueFileResource(read_info->unique);
170 (void) CopyMagickString(read_info->filename,read_info->unique,
171 MagickPathExtent);
172 file=(FILE *) NULL;
173 if (unique_file != -1)
174 file=fdopen(unique_file,"wb");
175 if ((unique_file == -1) || (file == (FILE *) NULL))
176 {
177 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
178 read_info->filename);
179 if (unique_file != -1)
180 (void) RelinquishUniqueFileResource(read_info->filename);
181 read_info=DestroyImageInfo(read_info);
182 image_metadata=DestroyImage(image_metadata);
183 zip_fclose(merged_image_file);
184 zip_discard(zip_archive);
185 return((Image *) NULL);
186 }
187 /* Write the uncompressed mergedimage.png to the temporary file */
188 status=MagickTrue;
189 offset=0;
190 while (status != MagickFalse)
191 {
192 read_bytes=zip_fread(merged_image_file,image_data_buffer+offset,
193 sizeof(image_data_buffer)-offset);
194 if (read_bytes == -1)
195 status=MagickFalse;
196 else if (read_bytes == 0)
197 {
198 /* Write up to offset of image_data_buffer to temp file */
199 if (!fwrite(image_data_buffer,offset,1,file))
200 status=MagickFalse;
201 break;
202 }
203 else if (read_bytes == sizeof(image_data_buffer)-offset)
204 {
205 /* Write the entirely of image_data_buffer to temp file */
206 if (!fwrite(image_data_buffer,sizeof(image_data_buffer),1,file))
207 status=MagickFalse;
208 else
209 offset=0;
210 }
211 else
212 offset+=read_bytes;
213 }
214 (void) fclose(file);
215 (void) zip_fclose(merged_image_file);
216 (void) zip_discard(zip_archive);
217 if (status == MagickFalse)
218 {
219 ThrowFileException(exception,CoderError,"UnableToReadImageData",
220 read_info->filename);
221 (void) RelinquishUniqueFileResource(read_info->filename);
222 read_info=DestroyImageInfo(read_info);
223 image_metadata=DestroyImage(image_metadata);
224 return((Image *) NULL);
225 }
226 /* Delegate to ReadImage to read mergedimage.png */
227 out_image=ReadImage(read_info,exception);
228 (void) RelinquishUniqueFileResource(read_info->filename);
229 read_info=DestroyImageInfo(read_info);
230 /* Update fields of image from fields of png_image */
231 if (out_image != NULL)
232 {
233 (void) CopyMagickString(out_image->filename,image_metadata->filename,
234 MagickPathExtent);
235 (void) CopyMagickString(out_image->magick_filename,
236 image_metadata->magick_filename,MagickPathExtent);
237 out_image->timestamp=time(&stat_info.st_mtime);
238 (void) CopyMagickString(out_image->magick,image_metadata->magick,
239 MagickPathExtent);
240 out_image->extent=stat_info.st_size;
241 }
242 image_metadata=DestroyImage(image_metadata);
243 return(out_image);
244 }
245 #endif /* MAGICKCORE_ZIP_DELEGATE && MAGICKCORE_PNG_DELEGATE */
246
247 /*
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % %
250 % %
251 % %
252 % R e g i s t e r O R A I m a g e %
253 % %
254 % %
255 % %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 %
258 % RegisterORAImage() adds attributes for the ORA image format to
259 % the list of supported formats. The attributes include the image format
260 % tag, a method to read and/or write the format, whether the format
261 % supports the saving of more than one frame to the same file or blob,
262 % whether the format supports native in-memory I/O, and a brief
263 % description of the format.
264 %
265 % The format of the RegisterORAImage method is:
266 %
267 % size_t RegisterORAImage(void)
268 %
269 */
RegisterORAImage(void)270 ModuleExport size_t RegisterORAImage(void)
271 {
272 MagickInfo
273 *entry;
274
275 entry=AcquireMagickInfo("ORA","ORA","OpenRaster format");
276
277 #if defined(MAGICKCORE_ZIP_DELEGATE) && defined(MAGICKCORE_PNG_DELEGATE)
278 entry->decoder=(DecodeImageHandler *) ReadORAImage;
279 #endif
280 entry->flags^=CoderBlobSupportFlag;
281 entry->flags|=CoderDecoderSeekableStreamFlag;
282 entry->format_type=ExplicitFormatType;
283 (void) RegisterMagickInfo(entry);
284 return(MagickImageCoderSignature);
285 }
286
287 /*
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 % %
290 % %
291 % %
292 % U n r e g i s t e r O R A I m a g e %
293 % %
294 % %
295 % %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 %
298 % UnregisterORAImage() removes format registrations made by the
299 % ORA module from the list of supported formats.
300 %
301 % The format of the UnregisterORAImage method is:
302 %
303 % UnregisterORAImage(void)
304 %
305 */
UnregisterORAImage(void)306 ModuleExport void UnregisterORAImage(void)
307 {
308 (void) UnregisterMagickInfo("ORA");
309 }
310