1 /*
2  * The copyright in this software is being made available under the 2-clauses
3  * BSD License, included below. This software may be subject to other third
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2010-2011, Kaori Hagihara
15  * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16  * Copyright (c) 2012, CS Systemes d'Information, France
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 #include "opj_includes.h"
41 
42 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
43 /*@{*/
44 
45 #define OPJ_BOX_SIZE    1024
46 
47 #define OPJ_UNUSED(x) (void)x
48 
49 /** @name Local static functions */
50 /*@{*/
51 
52 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
53 
54 /**
55  * Reads a IHDR box - Image Header box
56  *
57  * @param   p_image_header_data         pointer to actual data (already read from file)
58  * @param   jp2                         the jpeg2000 file codec.
59  * @param   p_image_header_size         the size of the image header
60  * @param   p_manager                   the user event manager.
61  *
62  * @return  true if the image header is valid, false else.
63  */
64 static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
65                                   OPJ_BYTE *p_image_header_data,
66                                   OPJ_UINT32 p_image_header_size,
67                                   opj_event_mgr_t * p_manager);
68 
69 /**
70  * Writes the Image Header box - Image Header box.
71  *
72  * @param jp2                   jpeg2000 file codec.
73  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
74  *
75  * @return  the data being copied.
76 */
77 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
78                                      OPJ_UINT32 * p_nb_bytes_written);
79 
80 /**
81  * Writes the Bit per Component box.
82  *
83  * @param   jp2                     jpeg2000 file codec.
84  * @param   p_nb_bytes_written      pointer to store the nb of bytes written by the function.
85  *
86  * @return  the data being copied.
87 */
88 static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
89                                      OPJ_UINT32 * p_nb_bytes_written);
90 
91 /**
92  * Reads a Bit per Component box.
93  *
94  * @param   p_bpc_header_data           pointer to actual data (already read from file)
95  * @param   jp2                         the jpeg2000 file codec.
96  * @param   p_bpc_header_size           the size of the bpc header
97  * @param   p_manager                   the user event manager.
98  *
99  * @return  true if the bpc header is valid, false else.
100  */
101 static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
102                                   OPJ_BYTE * p_bpc_header_data,
103                                   OPJ_UINT32 p_bpc_header_size,
104                                   opj_event_mgr_t * p_manager);
105 
106 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
107                                   OPJ_BYTE * p_cdef_header_data,
108                                   OPJ_UINT32 p_cdef_header_size,
109                                   opj_event_mgr_t * p_manager);
110 
111 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
112                                opj_event_mgr_t *);
113 
114 /**
115  * Writes the Channel Definition box.
116  *
117  * @param jp2                   jpeg2000 file codec.
118  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
119  *
120  * @return  the data being copied.
121  */
122 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
123                                      OPJ_UINT32 * p_nb_bytes_written);
124 
125 /**
126  * Writes the Colour Specification box.
127  *
128  * @param jp2                   jpeg2000 file codec.
129  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
130  *
131  * @return  the data being copied.
132 */
133 static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
134                                      OPJ_UINT32 * p_nb_bytes_written);
135 
136 /**
137  * Writes a FTYP box - File type box
138  *
139  * @param   cio         the stream to write data to.
140  * @param   jp2         the jpeg2000 file codec.
141  * @param   p_manager   the user event manager.
142  *
143  * @return  true if writing was successful.
144  */
145 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
146                                    opj_stream_private_t *cio,
147                                    opj_event_mgr_t * p_manager);
148 
149 /**
150  * Reads a a FTYP box - File type box
151  *
152  * @param   p_header_data   the data contained in the FTYP box.
153  * @param   jp2             the jpeg2000 file codec.
154  * @param   p_header_size   the size of the data contained in the FTYP box.
155  * @param   p_manager       the user event manager.
156  *
157  * @return true if the FTYP box is valid.
158  */
159 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
160                                   OPJ_BYTE * p_header_data,
161                                   OPJ_UINT32 p_header_size,
162                                   opj_event_mgr_t * p_manager);
163 
164 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
165                                   opj_stream_private_t *stream,
166                                   opj_event_mgr_t * p_manager);
167 
168 /**
169  * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
170  *
171  * @param   p_header_data   the data contained in the file header box.
172  * @param   jp2             the jpeg2000 file codec.
173  * @param   p_header_size   the size of the data contained in the file header box.
174  * @param   p_manager       the user event manager.
175  *
176  * @return true if the JP2 Header box was successfully recognized.
177 */
178 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
179                                   OPJ_BYTE *p_header_data,
180                                   OPJ_UINT32 p_header_size,
181                                   opj_event_mgr_t * p_manager);
182 
183 /**
184  * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
185  *
186  * @param  jp2      the jpeg2000 file codec.
187  * @param  stream      the stream to write data to.
188  * @param  p_manager  user event manager.
189  *
190  * @return true if writing was successful.
191  */
192 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
193                                    opj_stream_private_t *stream,
194                                    opj_event_mgr_t * p_manager);
195 
196 /**
197  * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
198  *
199  * @param   cio         the stream to write data to.
200  * @param   jp2         the jpeg2000 file codec.
201  * @param   p_manager   user event manager.
202  *
203  * @return true if writing was successful.
204 */
205 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
206                                    opj_stream_private_t *cio,
207                                    opj_event_mgr_t * p_manager);
208 
209 #ifdef USE_JPIP
210 /**
211  * Write index Finder box
212  * @param cio     the stream to write to.
213  * @param   jp2         the jpeg2000 file codec.
214  * @param   p_manager   user event manager.
215 */
216 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
217                                     opj_stream_private_t *cio,
218                                     opj_event_mgr_t * p_manager);
219 
220 /**
221  * Write index Finder box
222  * @param cio     the stream to write to.
223  * @param   jp2         the jpeg2000 file codec.
224  * @param   p_manager   user event manager.
225  */
226 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
227                                     opj_stream_private_t *cio,
228                                     opj_event_mgr_t * p_manager);
229 
230 /**
231  * Write file Index (superbox)
232  * @param cio     the stream to write to.
233  * @param   jp2         the jpeg2000 file codec.
234  * @param   p_manager   user event manager.
235  */
236 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
237                                     opj_stream_private_t *cio,
238                                     opj_event_mgr_t * p_manager);
239 #endif /* USE_JPIP */
240 
241 /**
242  * Reads a jpeg2000 file signature box.
243  *
244  * @param   p_header_data   the data contained in the signature box.
245  * @param   jp2             the jpeg2000 file codec.
246  * @param   p_header_size   the size of the data contained in the signature box.
247  * @param   p_manager       the user event manager.
248  *
249  * @return true if the file signature box is valid.
250  */
251 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
252                                 OPJ_BYTE * p_header_data,
253                                 OPJ_UINT32 p_header_size,
254                                 opj_event_mgr_t * p_manager);
255 
256 /**
257  * Writes a jpeg2000 file signature box.
258  *
259  * @param cio the stream to write data to.
260  * @param   jp2         the jpeg2000 file codec.
261  * @param p_manager the user event manager.
262  *
263  * @return true if writing was successful.
264  */
265 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
266                                  opj_stream_private_t *cio,
267                                  opj_event_mgr_t * p_manager);
268 
269 /**
270 Apply collected palette data
271 @param image Image.
272 @param color Collector for profile, cdef and pclr data.
273 @param p_manager the user event manager.
274 @return true in case of success
275 */
276 static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
277                                    opj_jp2_color_t *color,
278                                    opj_event_mgr_t * p_manager);
279 
280 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
281 
282 /**
283  * Collect palette data
284  *
285  * @param jp2 JP2 handle
286  * @param p_pclr_header_data    FIXME DOC
287  * @param p_pclr_header_size    FIXME DOC
288  * @param p_manager
289  *
290  * @return Returns true if successful, returns false otherwise
291 */
292 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
293                                   OPJ_BYTE * p_pclr_header_data,
294                                   OPJ_UINT32 p_pclr_header_size,
295                                   opj_event_mgr_t * p_manager);
296 
297 /**
298  * Collect component mapping data
299  *
300  * @param jp2                 JP2 handle
301  * @param p_cmap_header_data  FIXME DOC
302  * @param p_cmap_header_size  FIXME DOC
303  * @param p_manager           FIXME DOC
304  *
305  * @return Returns true if successful, returns false otherwise
306 */
307 
308 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
309                                   OPJ_BYTE * p_cmap_header_data,
310                                   OPJ_UINT32 p_cmap_header_size,
311                                   opj_event_mgr_t * p_manager);
312 
313 /**
314  * Reads the Color Specification box.
315  *
316  * @param   p_colr_header_data          pointer to actual data (already read from file)
317  * @param   jp2                         the jpeg2000 file codec.
318  * @param   p_colr_header_size          the size of the color header
319  * @param   p_manager                   the user event manager.
320  *
321  * @return  true if the bpc header is valid, false else.
322 */
323 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
324                                   OPJ_BYTE * p_colr_header_data,
325                                   OPJ_UINT32 p_colr_header_size,
326                                   opj_event_mgr_t * p_manager);
327 
328 /*@}*/
329 
330 /*@}*/
331 
332 /**
333  * Sets up the procedures to do on writing header after the codestream.
334  * Developpers wanting to extend the library can add their own writing procedures.
335  */
336 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
337         opj_event_mgr_t * p_manager);
338 
339 /**
340  * Sets up the procedures to do on reading header after the codestream.
341  * Developpers wanting to extend the library can add their own writing procedures.
342  */
343 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
344         opj_event_mgr_t * p_manager);
345 
346 /**
347  * Reads a jpeg2000 file header structure.
348  *
349  * @param jp2 the jpeg2000 file header structure.
350  * @param stream the stream to read data from.
351  * @param p_manager the user event manager.
352  *
353  * @return true if the box is valid.
354  */
355 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
356         opj_stream_private_t *stream,
357         opj_event_mgr_t * p_manager);
358 
359 /**
360  * Executes the given procedures on the given codec.
361  *
362  * @param   p_procedure_list    the list of procedures to execute
363  * @param   jp2                 the jpeg2000 file codec to execute the procedures on.
364  * @param   stream                  the stream to execute the procedures on.
365  * @param   p_manager           the user manager.
366  *
367  * @return  true                if all the procedures were successfully executed.
368  */
369 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
370                              opj_procedure_list_t * p_procedure_list,
371                              opj_stream_private_t *stream,
372                              opj_event_mgr_t * p_manager);
373 
374 /**
375  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
376  *
377  * @param   cio                     the input stream to read data from.
378  * @param   box                     the box structure to fill.
379  * @param   p_number_bytes_read     pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
380  * @param   p_manager               user event manager.
381  *
382  * @return  true if the box is recognized, false otherwise
383 */
384 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
385                                     OPJ_UINT32 * p_number_bytes_read,
386                                     opj_stream_private_t *cio,
387                                     opj_event_mgr_t * p_manager);
388 
389 /**
390  * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
391  * are valid. Developpers wanting to extend the library can add their own validation procedures.
392  */
393 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
394         opj_event_mgr_t * p_manager);
395 
396 /**
397  * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
398  */
399 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
400         opj_event_mgr_t * p_manager);
401 
402 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
403         opj_stream_private_t *cio,
404         opj_event_mgr_t * p_manager);
405 
406 /**
407  * Finds the image execution function related to the given box id.
408  *
409  * @param   p_id    the id of the handler to fetch.
410  *
411  * @return  the given handler or NULL if it could not be found.
412  */
413 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
414     OPJ_UINT32 p_id);
415 
416 /**
417  * Finds the execution function related to the given box id.
418  *
419  * @param   p_id    the id of the handler to fetch.
420  *
421  * @return  the given handler or NULL if it could not be found.
422  */
423 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id);
424 
425 static const opj_jp2_header_handler_t jp2_header [] = {
426     {JP2_JP, opj_jp2_read_jp},
427     {JP2_FTYP, opj_jp2_read_ftyp},
428     {JP2_JP2H, opj_jp2_read_jp2h}
429 };
430 
431 static const opj_jp2_header_handler_t jp2_img_header [] = {
432     {JP2_IHDR, opj_jp2_read_ihdr},
433     {JP2_COLR, opj_jp2_read_colr},
434     {JP2_BPCC, opj_jp2_read_bpcc},
435     {JP2_PCLR, opj_jp2_read_pclr},
436     {JP2_CMAP, opj_jp2_read_cmap},
437     {JP2_CDEF, opj_jp2_read_cdef}
438 
439 };
440 
441 /**
442  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
443  *
444  * @param   box                     the box structure to fill.
445  * @param   p_data                  the character string to read data from.
446  * @param   p_number_bytes_read     pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
447  * @param   p_box_max_size          the maximum number of bytes in the box.
448  * @param   p_manager         FIXME DOC
449  *
450  * @return  true if the box is recognized, false otherwise
451 */
452 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
453         OPJ_BYTE * p_data,
454         OPJ_UINT32 * p_number_bytes_read,
455         OPJ_UINT32 p_box_max_size,
456         opj_event_mgr_t * p_manager);
457 
458 /**
459  * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
460  * are valid. Developpers wanting to extend the library can add their own validation procedures.
461  */
462 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
463         opj_event_mgr_t * p_manager);
464 
465 /**
466  * Sets up the procedures to do on reading header.
467  * Developpers wanting to extend the library can add their own writing procedures.
468  */
469 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
470         opj_event_mgr_t * p_manager);
471 
472 /* ----------------------------------------------------------------------- */
opj_jp2_read_boxhdr(opj_jp2_box_t * box,OPJ_UINT32 * p_number_bytes_read,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)473 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
474                                     OPJ_UINT32 * p_number_bytes_read,
475                                     opj_stream_private_t *cio,
476                                     opj_event_mgr_t * p_manager)
477 {
478     /* read header from file */
479     OPJ_BYTE l_data_header [8];
480 
481     /* preconditions */
482     assert(cio != 00);
483     assert(box != 00);
484     assert(p_number_bytes_read != 00);
485     assert(p_manager != 00);
486 
487     *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio, l_data_header, 8,
488                            p_manager);
489     if (*p_number_bytes_read != 8) {
490         return OPJ_FALSE;
491     }
492 
493     /* process read data */
494     opj_read_bytes(l_data_header, &(box->length), 4);
495     opj_read_bytes(l_data_header + 4, &(box->type), 4);
496 
497     if (box->length == 0) { /* last box */
498         const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
499         if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) {
500             opj_event_msg(p_manager, EVT_ERROR,
501                           "Cannot handle box sizes higher than 2^32\n");
502             return OPJ_FALSE;
503         }
504         box->length = (OPJ_UINT32)bleft + 8U;
505         assert((OPJ_OFF_T)box->length == bleft + 8);
506         return OPJ_TRUE;
507     }
508 
509     /* do we have a "special very large box ?" */
510     /* read then the XLBox */
511     if (box->length == 1) {
512         OPJ_UINT32 l_xl_part_size;
513 
514         OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,
515                                      l_data_header, 8, p_manager);
516         if (l_nb_bytes_read != 8) {
517             if (l_nb_bytes_read > 0) {
518                 *p_number_bytes_read += l_nb_bytes_read;
519             }
520 
521             return OPJ_FALSE;
522         }
523 
524         *p_number_bytes_read = 16;
525         opj_read_bytes(l_data_header, &l_xl_part_size, 4);
526         if (l_xl_part_size != 0) {
527             opj_event_msg(p_manager, EVT_ERROR,
528                           "Cannot handle box sizes higher than 2^32\n");
529             return OPJ_FALSE;
530         }
531         opj_read_bytes(l_data_header + 4, &(box->length), 4);
532     }
533     return OPJ_TRUE;
534 }
535 
536 #if 0
537 static void jp2_write_url(opj_cio_t *cio, char *Idx_file)
538 {
539     OPJ_UINT32 i;
540     opj_jp2_box_t box;
541 
542     box.init_pos = cio_tell(cio);
543     cio_skip(cio, 4);
544     cio_write(cio, JP2_URL, 4); /* DBTL */
545     cio_write(cio, 0, 1);       /* VERS */
546     cio_write(cio, 0, 3);       /* FLAG */
547 
548     if (Idx_file) {
549         for (i = 0; i < strlen(Idx_file); i++) {
550             cio_write(cio, Idx_file[i], 1);
551         }
552     }
553 
554     box.length = cio_tell(cio) - box.init_pos;
555     cio_seek(cio, box.init_pos);
556     cio_write(cio, box.length, 4);  /* L */
557     cio_seek(cio, box.init_pos + box.length);
558 }
559 #endif
560 
opj_jp2_read_ihdr(opj_jp2_t * jp2,OPJ_BYTE * p_image_header_data,OPJ_UINT32 p_image_header_size,opj_event_mgr_t * p_manager)561 static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
562                                   OPJ_BYTE *p_image_header_data,
563                                   OPJ_UINT32 p_image_header_size,
564                                   opj_event_mgr_t * p_manager)
565 {
566     /* preconditions */
567     assert(p_image_header_data != 00);
568     assert(jp2 != 00);
569     assert(p_manager != 00);
570 
571     if (jp2->comps != NULL) {
572         opj_event_msg(p_manager, EVT_WARNING,
573                       "Ignoring ihdr box. First ihdr box already read\n");
574         return OPJ_TRUE;
575     }
576 
577     if (p_image_header_size != 14) {
578         opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
579         return OPJ_FALSE;
580     }
581 
582     opj_read_bytes(p_image_header_data, &(jp2->h), 4);          /* HEIGHT */
583     p_image_header_data += 4;
584     opj_read_bytes(p_image_header_data, &(jp2->w), 4);          /* WIDTH */
585     p_image_header_data += 4;
586     opj_read_bytes(p_image_header_data, &(jp2->numcomps), 2);   /* NC */
587     p_image_header_data += 2;
588 
589     if ((jp2->numcomps - 1U) >=
590             16384U) { /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */
591         opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components (ihdr)\n");
592         return OPJ_FALSE;
593     }
594 
595     /* allocate memory for components */
596     opj_free(jp2->comps);
597     jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps,
598                  sizeof(opj_jp2_comps_t));
599     if (jp2->comps == 0) {
600         opj_event_msg(p_manager, EVT_ERROR,
601                       "Not enough memory to handle image header (ihdr)\n");
602         return OPJ_FALSE;
603     }
604 
605     opj_read_bytes(p_image_header_data, &(jp2->bpc), 1);        /* BPC */
606     ++ p_image_header_data;
607 
608     opj_read_bytes(p_image_header_data, &(jp2->C), 1);          /* C */
609     ++ p_image_header_data;
610 
611     /* Should be equal to 7 cf. chapter about image header box of the norm */
612     if (jp2->C != 7) {
613         opj_event_msg(p_manager, EVT_INFO,
614                       "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n",
615                       jp2->C);
616     }
617 
618     opj_read_bytes(p_image_header_data, &(jp2->UnkC), 1);       /* UnkC */
619     ++ p_image_header_data;
620     opj_read_bytes(p_image_header_data, &(jp2->IPR), 1);        /* IPR */
621     ++ p_image_header_data;
622 
623     jp2->j2k->m_cp.allow_different_bit_depth_sign = (jp2->bpc == 255);
624     jp2->j2k->ihdr_w = jp2->w;
625     jp2->j2k->ihdr_h = jp2->h;
626     jp2->has_ihdr = 1;
627 
628     return OPJ_TRUE;
629 }
630 
opj_jp2_write_ihdr(opj_jp2_t * jp2,OPJ_UINT32 * p_nb_bytes_written)631 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
632                                      OPJ_UINT32 * p_nb_bytes_written
633                                     )
634 {
635     OPJ_BYTE * l_ihdr_data, * l_current_ihdr_ptr;
636 
637     /* preconditions */
638     assert(jp2 != 00);
639     assert(p_nb_bytes_written != 00);
640 
641     /* default image header is 22 bytes wide */
642     l_ihdr_data = (OPJ_BYTE *) opj_calloc(1, 22);
643     if (l_ihdr_data == 00) {
644         return 00;
645     }
646 
647     l_current_ihdr_ptr = l_ihdr_data;
648 
649     opj_write_bytes(l_current_ihdr_ptr, 22, 4);             /* write box size */
650     l_current_ihdr_ptr += 4;
651 
652     opj_write_bytes(l_current_ihdr_ptr, JP2_IHDR, 4);       /* IHDR */
653     l_current_ihdr_ptr += 4;
654 
655     opj_write_bytes(l_current_ihdr_ptr, jp2->h, 4);     /* HEIGHT */
656     l_current_ihdr_ptr += 4;
657 
658     opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4);     /* WIDTH */
659     l_current_ihdr_ptr += 4;
660 
661     opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2);      /* NC */
662     l_current_ihdr_ptr += 2;
663 
664     opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1);       /* BPC */
665     ++l_current_ihdr_ptr;
666 
667     opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1);     /* C : Always 7 */
668     ++l_current_ihdr_ptr;
669 
670     opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC,
671                     1);      /* UnkC, colorspace unknown */
672     ++l_current_ihdr_ptr;
673 
674     opj_write_bytes(l_current_ihdr_ptr, jp2->IPR,
675                     1);       /* IPR, no intellectual property */
676     ++l_current_ihdr_ptr;
677 
678     *p_nb_bytes_written = 22;
679 
680     return l_ihdr_data;
681 }
682 
opj_jp2_write_bpcc(opj_jp2_t * jp2,OPJ_UINT32 * p_nb_bytes_written)683 static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
684                                      OPJ_UINT32 * p_nb_bytes_written
685                                     )
686 {
687     OPJ_UINT32 i;
688     /* room for 8 bytes for box and 1 byte for each component */
689     OPJ_UINT32 l_bpcc_size;
690     OPJ_BYTE * l_bpcc_data, * l_current_bpcc_ptr;
691 
692     /* preconditions */
693     assert(jp2 != 00);
694     assert(p_nb_bytes_written != 00);
695     l_bpcc_size = 8 + jp2->numcomps;
696 
697     l_bpcc_data = (OPJ_BYTE *) opj_calloc(1, l_bpcc_size);
698     if (l_bpcc_data == 00) {
699         return 00;
700     }
701 
702     l_current_bpcc_ptr = l_bpcc_data;
703 
704     opj_write_bytes(l_current_bpcc_ptr, l_bpcc_size,
705                     4);            /* write box size */
706     l_current_bpcc_ptr += 4;
707 
708     opj_write_bytes(l_current_bpcc_ptr, JP2_BPCC, 4);               /* BPCC */
709     l_current_bpcc_ptr += 4;
710 
711     for (i = 0; i < jp2->numcomps; ++i)  {
712         opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc,
713                         1); /* write each component information */
714         ++l_current_bpcc_ptr;
715     }
716 
717     *p_nb_bytes_written = l_bpcc_size;
718 
719     return l_bpcc_data;
720 }
721 
opj_jp2_read_bpcc(opj_jp2_t * jp2,OPJ_BYTE * p_bpc_header_data,OPJ_UINT32 p_bpc_header_size,opj_event_mgr_t * p_manager)722 static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
723                                   OPJ_BYTE * p_bpc_header_data,
724                                   OPJ_UINT32 p_bpc_header_size,
725                                   opj_event_mgr_t * p_manager
726                                  )
727 {
728     OPJ_UINT32 i;
729 
730     /* preconditions */
731     assert(p_bpc_header_data != 00);
732     assert(jp2 != 00);
733     assert(p_manager != 00);
734 
735 
736     if (jp2->bpc != 255) {
737         opj_event_msg(p_manager, EVT_WARNING,
738                       "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",
739                       jp2->bpc);
740     }
741 
742     /* and length is relevant */
743     if (p_bpc_header_size != jp2->numcomps) {
744         opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
745         return OPJ_FALSE;
746     }
747 
748     /* read info for each component */
749     for (i = 0; i < jp2->numcomps; ++i) {
750         opj_read_bytes(p_bpc_header_data, &jp2->comps[i].bpcc,
751                        1);  /* read each BPCC component */
752         ++p_bpc_header_data;
753     }
754 
755     return OPJ_TRUE;
756 }
opj_jp2_write_cdef(opj_jp2_t * jp2,OPJ_UINT32 * p_nb_bytes_written)757 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
758                                      OPJ_UINT32 * p_nb_bytes_written)
759 {
760     /* room for 8 bytes for box, 2 for n */
761     OPJ_UINT32 l_cdef_size = 10;
762     OPJ_BYTE * l_cdef_data, * l_current_cdef_ptr;
763     OPJ_UINT32 l_value;
764     OPJ_UINT16 i;
765 
766     /* preconditions */
767     assert(jp2 != 00);
768     assert(p_nb_bytes_written != 00);
769     assert(jp2->color.jp2_cdef != 00);
770     assert(jp2->color.jp2_cdef->info != 00);
771     assert(jp2->color.jp2_cdef->n > 0U);
772 
773     l_cdef_size += 6U * jp2->color.jp2_cdef->n;
774 
775     l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
776     if (l_cdef_data == 00) {
777         return 00;
778     }
779 
780     l_current_cdef_ptr = l_cdef_data;
781 
782     opj_write_bytes(l_current_cdef_ptr, l_cdef_size, 4);        /* write box size */
783     l_current_cdef_ptr += 4;
784 
785     opj_write_bytes(l_current_cdef_ptr, JP2_CDEF, 4);               /* BPCC */
786     l_current_cdef_ptr += 4;
787 
788     l_value = jp2->color.jp2_cdef->n;
789     opj_write_bytes(l_current_cdef_ptr, l_value, 2);                /* N */
790     l_current_cdef_ptr += 2;
791 
792     for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
793         l_value = jp2->color.jp2_cdef->info[i].cn;
794         opj_write_bytes(l_current_cdef_ptr, l_value, 2);                /* Cni */
795         l_current_cdef_ptr += 2;
796         l_value = jp2->color.jp2_cdef->info[i].typ;
797         opj_write_bytes(l_current_cdef_ptr, l_value, 2);                /* Typi */
798         l_current_cdef_ptr += 2;
799         l_value = jp2->color.jp2_cdef->info[i].asoc;
800         opj_write_bytes(l_current_cdef_ptr, l_value, 2);                /* Asoci */
801         l_current_cdef_ptr += 2;
802     }
803     *p_nb_bytes_written = l_cdef_size;
804 
805     return l_cdef_data;
806 }
807 
opj_jp2_write_colr(opj_jp2_t * jp2,OPJ_UINT32 * p_nb_bytes_written)808 static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
809                                      OPJ_UINT32 * p_nb_bytes_written
810                                     )
811 {
812     /* room for 8 bytes for box 3 for common data and variable upon profile*/
813     OPJ_UINT32 l_colr_size = 11;
814     OPJ_BYTE * l_colr_data, * l_current_colr_ptr;
815 
816     /* preconditions */
817     assert(jp2 != 00);
818     assert(p_nb_bytes_written != 00);
819     assert(jp2->meth == 1 || jp2->meth == 2);
820 
821     switch (jp2->meth) {
822     case 1 :
823         l_colr_size += 4; /* EnumCS */
824         break;
825     case 2 :
826         assert(jp2->color.icc_profile_len); /* ICC profile */
827         l_colr_size += jp2->color.icc_profile_len;
828         break;
829     default :
830         return 00;
831     }
832 
833     l_colr_data = (OPJ_BYTE *) opj_calloc(1, l_colr_size);
834     if (l_colr_data == 00) {
835         return 00;
836     }
837 
838     l_current_colr_ptr = l_colr_data;
839 
840     opj_write_bytes(l_current_colr_ptr, l_colr_size,
841                     4);            /* write box size */
842     l_current_colr_ptr += 4;
843 
844     opj_write_bytes(l_current_colr_ptr, JP2_COLR, 4);               /* BPCC */
845     l_current_colr_ptr += 4;
846 
847     opj_write_bytes(l_current_colr_ptr, jp2->meth, 1);              /* METH */
848     ++l_current_colr_ptr;
849 
850     opj_write_bytes(l_current_colr_ptr, jp2->precedence, 1);        /* PRECEDENCE */
851     ++l_current_colr_ptr;
852 
853     opj_write_bytes(l_current_colr_ptr, jp2->approx, 1);            /* APPROX */
854     ++l_current_colr_ptr;
855 
856     if (jp2->meth ==
857             1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
858         opj_write_bytes(l_current_colr_ptr, jp2->enumcs, 4);
859     }       /* EnumCS */
860     else {
861         if (jp2->meth == 2) {                                      /* ICC profile */
862             OPJ_UINT32 i;
863             for (i = 0; i < jp2->color.icc_profile_len; ++i) {
864                 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
865                 ++l_current_colr_ptr;
866             }
867         }
868     }
869 
870     *p_nb_bytes_written = l_colr_size;
871 
872     return l_colr_data;
873 }
874 
opj_jp2_free_pclr(opj_jp2_color_t * color)875 static void opj_jp2_free_pclr(opj_jp2_color_t *color)
876 {
877     opj_free(color->jp2_pclr->channel_sign);
878     opj_free(color->jp2_pclr->channel_size);
879     opj_free(color->jp2_pclr->entries);
880 
881     if (color->jp2_pclr->cmap) {
882         opj_free(color->jp2_pclr->cmap);
883     }
884 
885     opj_free(color->jp2_pclr);
886     color->jp2_pclr = NULL;
887 }
888 
opj_jp2_check_color(opj_image_t * image,opj_jp2_color_t * color,opj_event_mgr_t * p_manager)889 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
890                                     opj_event_mgr_t *p_manager)
891 {
892     OPJ_UINT16 i;
893 
894     /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
895     if (color->jp2_cdef) {
896         opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
897         OPJ_UINT16 n = color->jp2_cdef->n;
898         OPJ_UINT32 nr_channels =
899             image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
900 
901         /* cdef applies to cmap channels if any */
902         if (color->jp2_pclr && color->jp2_pclr->cmap) {
903             nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
904         }
905 
906         for (i = 0; i < n; i++) {
907             if (info[i].cn >= nr_channels) {
908                 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
909                               info[i].cn, nr_channels);
910                 return OPJ_FALSE;
911             }
912             if (info[i].asoc == 65535U) {
913                 continue;
914             }
915 
916             if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
917                 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
918                               info[i].asoc - 1, nr_channels);
919                 return OPJ_FALSE;
920             }
921         }
922 
923         /* issue 397 */
924         /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
925         while (nr_channels > 0) {
926             for (i = 0; i < n; ++i) {
927                 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
928                     break;
929                 }
930             }
931             if (i == n) {
932                 opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
933                 return OPJ_FALSE;
934             }
935             --nr_channels;
936         }
937     }
938 
939     /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
940        66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
941     if (color->jp2_pclr && color->jp2_pclr->cmap) {
942         OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
943         opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
944         OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
945 
946         /* verify that all original components match an existing one */
947         for (i = 0; i < nr_channels; i++) {
948             if (cmap[i].cmp >= image->numcomps) {
949                 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
950                               cmap[i].cmp, image->numcomps);
951                 is_sane = OPJ_FALSE;
952             }
953         }
954 
955         pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL));
956         if (!pcol_usage) {
957             opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
958             return OPJ_FALSE;
959         }
960         /* verify that no component is targeted more than once */
961         for (i = 0; i < nr_channels; i++) {
962             OPJ_BYTE mtyp = cmap[i].mtyp;
963             OPJ_BYTE pcol = cmap[i].pcol;
964             /* See ISO 15444-1 Table I.14 – MTYPi field values */
965             if (mtyp != 0 && mtyp != 1) {
966                 opj_event_msg(p_manager, EVT_ERROR,
967                               "Invalid value for cmap[%d].mtyp = %d.\n", i,
968                               mtyp);
969                 is_sane = OPJ_FALSE;
970             } else if (pcol >= nr_channels) {
971                 opj_event_msg(p_manager, EVT_ERROR,
972                               "Invalid component/palette index for direct mapping %d.\n", pcol);
973                 is_sane = OPJ_FALSE;
974             } else if (pcol_usage[pcol] && mtyp == 1) {
975                 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
976                 is_sane = OPJ_FALSE;
977             } else if (mtyp == 0 && pcol != 0) {
978                 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
979                  * the value of this field shall be 0. */
980                 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i,
981                               pcol);
982                 is_sane = OPJ_FALSE;
983             } else if (mtyp == 1 && pcol != i) {
984                 /* OpenJPEG implementation limitation. See assert(i == pcol); */
985                 /* in opj_jp2_apply_pclr() */
986                 opj_event_msg(p_manager, EVT_ERROR,
987                               "Implementation limitation: for palette mapping, "
988                               "pcol[%d] should be equal to %d, but is equal "
989                               "to %d.\n", i, i, pcol);
990                 is_sane = OPJ_FALSE;
991             } else {
992                 pcol_usage[pcol] = OPJ_TRUE;
993             }
994         }
995         /* verify that all components are targeted at least once */
996         for (i = 0; i < nr_channels; i++) {
997             if (!pcol_usage[i] && cmap[i].mtyp != 0) {
998                 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n",
999                               i);
1000                 is_sane = OPJ_FALSE;
1001             }
1002         }
1003         /* Issue 235/447 weird cmap */
1004         if (1 && is_sane && (image->numcomps == 1U)) {
1005             for (i = 0; i < nr_channels; i++) {
1006                 if (!pcol_usage[i]) {
1007                     is_sane = 0U;
1008                     opj_event_msg(p_manager, EVT_WARNING,
1009                                   "Component mapping seems wrong. Trying to correct.\n");
1010                     break;
1011                 }
1012             }
1013             if (!is_sane) {
1014                 is_sane = OPJ_TRUE;
1015                 for (i = 0; i < nr_channels; i++) {
1016                     cmap[i].mtyp = 1U;
1017                     cmap[i].pcol = (OPJ_BYTE) i;
1018                 }
1019             }
1020         }
1021         opj_free(pcol_usage);
1022         if (!is_sane) {
1023             return OPJ_FALSE;
1024         }
1025     }
1026 
1027     return OPJ_TRUE;
1028 }
1029 
1030 /* file9.jp2 */
opj_jp2_apply_pclr(opj_image_t * image,opj_jp2_color_t * color,opj_event_mgr_t * p_manager)1031 static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
1032                                    opj_jp2_color_t *color,
1033                                    opj_event_mgr_t * p_manager)
1034 {
1035     opj_image_comp_t *old_comps, *new_comps;
1036     OPJ_BYTE *channel_size, *channel_sign;
1037     OPJ_UINT32 *entries;
1038     opj_jp2_cmap_comp_t *cmap;
1039     OPJ_INT32 *src, *dst;
1040     OPJ_UINT32 j, max;
1041     OPJ_UINT16 i, nr_channels, cmp, pcol;
1042     OPJ_INT32 k, top_k;
1043 
1044     channel_size = color->jp2_pclr->channel_size;
1045     channel_sign = color->jp2_pclr->channel_sign;
1046     entries = color->jp2_pclr->entries;
1047     cmap = color->jp2_pclr->cmap;
1048     nr_channels = color->jp2_pclr->nr_channels;
1049 
1050     for (i = 0; i < nr_channels; ++i) {
1051         /* Palette mapping: */
1052         cmp = cmap[i].cmp;
1053         if (image->comps[cmp].data == NULL) {
1054             opj_event_msg(p_manager, EVT_ERROR,
1055                           "image->comps[%d].data == NULL in opj_jp2_apply_pclr().\n", i);
1056             return OPJ_FALSE;
1057         }
1058     }
1059 
1060     old_comps = image->comps;
1061     /* Overflow check: prevent integer overflow */
1062     for (i = 0; i < nr_channels; ++i) {
1063       cmp = cmap[i].cmp;
1064       if (old_comps[cmp].h == 0 || old_comps[cmp].w > ((OPJ_UINT32)-1) / sizeof(OPJ_INT32) / old_comps[cmp].h) {
1065         return OPJ_FALSE;
1066       }
1067     }
1068 
1069     new_comps = (opj_image_comp_t*)
1070                 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
1071     if (!new_comps) {
1072         opj_event_msg(p_manager, EVT_ERROR,
1073                       "Memory allocation failure in opj_jp2_apply_pclr().\n");
1074         return OPJ_FALSE;
1075     }
1076     for (i = 0; i < nr_channels; ++i) {
1077         pcol = cmap[i].pcol;
1078         cmp = cmap[i].cmp;
1079 
1080         /* Direct use */
1081         if (cmap[i].mtyp == 0) {
1082             assert(pcol == 0);
1083             new_comps[i] = old_comps[cmp];
1084         } else {
1085             assert( i == pcol ); // probably wrong?
1086             new_comps[i] = old_comps[cmp];
1087         }
1088 
1089         /* Palette mapping: */
1090         new_comps[i].data = (OPJ_INT32*)
1091                             opj_image_data_alloc(sizeof(OPJ_INT32) * old_comps[cmp].w * old_comps[cmp].h);
1092         if (!new_comps[i].data) {
1093             while (i > 0) {
1094                 -- i;
1095                 opj_image_data_free(new_comps[i].data);
1096             }
1097             opj_free(new_comps);
1098             opj_event_msg(p_manager, EVT_ERROR,
1099                           "Memory allocation failure in opj_jp2_apply_pclr().\n");
1100             return OPJ_FALSE;
1101         }
1102         new_comps[i].prec = channel_size[i];
1103         new_comps[i].sgnd = channel_sign[i];
1104     }
1105 
1106     top_k = color->jp2_pclr->nr_entries - 1;
1107 
1108     for (i = 0; i < nr_channels; ++i) {
1109         /* Palette mapping: */
1110         cmp = cmap[i].cmp;
1111         pcol = cmap[i].pcol;
1112         src = old_comps[cmp].data;
1113         dst = new_comps[i].data;
1114         max = new_comps[i].w * new_comps[i].h;
1115 
1116         /* Prevent null pointer access */
1117         if (!src || !dst) {
1118           for (j = 0; j < nr_channels; ++j) {
1119             opj_image_data_free(new_comps[j].data);
1120           }
1121           opj_free(new_comps);
1122           new_comps = NULL;
1123           return OPJ_FALSE;
1124         }
1125 
1126         /* Direct use: */
1127         if (cmap[i].mtyp == 0) {
1128             for (j = 0; j < max; ++j) {
1129                 dst[j] = src[j];
1130             }
1131         } else {
1132             assert( i == pcol ); // probably wrong?
1133             for (j = 0; j < max; ++j) {
1134                 /* The index */
1135                 if ((k = src[j]) < 0) {
1136                     k = 0;
1137                 } else if (k > top_k) {
1138                     k = top_k;
1139                 }
1140 
1141                 /* The colour */
1142                 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
1143             }
1144         }
1145     }
1146 
1147     max = image->numcomps;
1148     for (i = 0; i < max; ++i) {
1149         if (old_comps[i].data) {
1150             opj_image_data_free(old_comps[i].data);
1151         }
1152     }
1153 
1154     opj_free(old_comps);
1155     image->comps = new_comps;
1156     image->numcomps = nr_channels;
1157 
1158     return OPJ_TRUE;
1159 }/* apply_pclr() */
1160 
opj_jp2_read_pclr(opj_jp2_t * jp2,OPJ_BYTE * p_pclr_header_data,OPJ_UINT32 p_pclr_header_size,opj_event_mgr_t * p_manager)1161 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
1162                                   OPJ_BYTE * p_pclr_header_data,
1163                                   OPJ_UINT32 p_pclr_header_size,
1164                                   opj_event_mgr_t * p_manager
1165                                  )
1166 {
1167     opj_jp2_pclr_t *jp2_pclr;
1168     OPJ_BYTE *channel_size, *channel_sign;
1169     OPJ_UINT32 *entries;
1170     OPJ_UINT16 nr_entries, nr_channels;
1171     OPJ_UINT16 i, j;
1172     OPJ_UINT32 l_value;
1173     OPJ_BYTE *orig_header_data = p_pclr_header_data;
1174 
1175     /* preconditions */
1176     assert(p_pclr_header_data != 00);
1177     assert(jp2 != 00);
1178     assert(p_manager != 00);
1179     (void)p_pclr_header_size;
1180 
1181     if (jp2->color.jp2_pclr) {
1182         return OPJ_FALSE;
1183     }
1184 
1185     if (p_pclr_header_size < 3) {
1186         return OPJ_FALSE;
1187     }
1188 
1189     opj_read_bytes(p_pclr_header_data, &l_value, 2);    /* NE */
1190     p_pclr_header_data += 2;
1191     nr_entries = (OPJ_UINT16) l_value;
1192     if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1193         opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n",
1194                       (int)nr_entries);
1195         return OPJ_FALSE;
1196     }
1197 
1198     opj_read_bytes(p_pclr_header_data, &l_value, 1);    /* NPC */
1199     ++p_pclr_header_data;
1200     nr_channels = (OPJ_UINT16) l_value;
1201     if (nr_channels == 0U) {
1202         opj_event_msg(p_manager, EVT_ERROR,
1203                       "Invalid PCLR box. Reports 0 palette columns\n");
1204         return OPJ_FALSE;
1205     }
1206 
1207     if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels) {
1208         return OPJ_FALSE;
1209     }
1210 
1211     entries = (OPJ_UINT32*) opj_malloc(sizeof(OPJ_UINT32) * nr_channels *
1212                                        nr_entries);
1213     if (!entries) {
1214         return OPJ_FALSE;
1215     }
1216     channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1217     if (!channel_size) {
1218         opj_free(entries);
1219         return OPJ_FALSE;
1220     }
1221     channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1222     if (!channel_sign) {
1223         opj_free(entries);
1224         opj_free(channel_size);
1225         return OPJ_FALSE;
1226     }
1227 
1228     jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1229     if (!jp2_pclr) {
1230         opj_free(entries);
1231         opj_free(channel_size);
1232         opj_free(channel_sign);
1233         return OPJ_FALSE;
1234     }
1235 
1236     jp2_pclr->channel_sign = channel_sign;
1237     jp2_pclr->channel_size = channel_size;
1238     jp2_pclr->entries = entries;
1239     jp2_pclr->nr_entries = nr_entries;
1240     jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
1241     jp2_pclr->cmap = NULL;
1242 
1243     jp2->color.jp2_pclr = jp2_pclr;
1244 
1245     for (i = 0; i < nr_channels; ++i) {
1246         opj_read_bytes(p_pclr_header_data, &l_value, 1);    /* Bi */
1247         ++p_pclr_header_data;
1248 
1249         channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1250         channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1251     }
1252 
1253     for (j = 0; j < nr_entries; ++j) {
1254         for (i = 0; i < nr_channels; ++i) {
1255             OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i] + 7) >> 3);
1256 
1257             if (bytes_to_read > sizeof(OPJ_UINT32)) {
1258                 bytes_to_read = sizeof(OPJ_UINT32);
1259             }
1260             if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data -
1261                     orig_header_data) + (ptrdiff_t)bytes_to_read) {
1262                 return OPJ_FALSE;
1263             }
1264 
1265             opj_read_bytes(p_pclr_header_data, &l_value, bytes_to_read);    /* Cji */
1266             p_pclr_header_data += bytes_to_read;
1267             *entries = (OPJ_UINT32) l_value;
1268             entries++;
1269         }
1270     }
1271 
1272     return OPJ_TRUE;
1273 }
1274 
opj_jp2_read_cmap(opj_jp2_t * jp2,OPJ_BYTE * p_cmap_header_data,OPJ_UINT32 p_cmap_header_size,opj_event_mgr_t * p_manager)1275 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
1276                                   OPJ_BYTE * p_cmap_header_data,
1277                                   OPJ_UINT32 p_cmap_header_size,
1278                                   opj_event_mgr_t * p_manager
1279                                  )
1280 {
1281     opj_jp2_cmap_comp_t *cmap;
1282     OPJ_BYTE i, nr_channels;
1283     OPJ_UINT32 l_value;
1284 
1285     /* preconditions */
1286     assert(jp2 != 00);
1287     assert(p_cmap_header_data != 00);
1288     assert(p_manager != 00);
1289     (void)p_cmap_header_size;
1290 
1291     /* Need nr_channels: */
1292     if (jp2->color.jp2_pclr == NULL) {
1293         opj_event_msg(p_manager, EVT_ERROR,
1294                       "Need to read a PCLR box before the CMAP box.\n");
1295         return OPJ_FALSE;
1296     }
1297 
1298     /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1299      * inside a JP2 Header box' :
1300     */
1301     if (jp2->color.jp2_pclr->cmap) {
1302         opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1303         return OPJ_FALSE;
1304     }
1305 
1306     nr_channels = jp2->color.jp2_pclr->nr_channels;
1307     if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1308         opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1309         return OPJ_FALSE;
1310     }
1311 
1312     cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(
1313                 opj_jp2_cmap_comp_t));
1314     if (!cmap) {
1315         return OPJ_FALSE;
1316     }
1317 
1318 
1319     for (i = 0; i < nr_channels; ++i) {
1320         opj_read_bytes_BE(p_cmap_header_data, &l_value, 2);     /* CMP^i */
1321         p_cmap_header_data += 2;
1322         cmap[i].cmp = (OPJ_UINT16) l_value;
1323 
1324         opj_read_bytes(p_cmap_header_data, &l_value, 1);            /* MTYP^i */
1325         ++p_cmap_header_data;
1326         cmap[i].mtyp = (OPJ_BYTE) l_value;
1327 
1328         opj_read_bytes(p_cmap_header_data, &l_value, 1);            /* PCOL^i */
1329         ++p_cmap_header_data;
1330         cmap[i].pcol = (OPJ_BYTE) l_value;
1331     }
1332 
1333     jp2->color.jp2_pclr->cmap = cmap;
1334 
1335     return OPJ_TRUE;
1336 }
1337 
opj_jp2_apply_cdef(opj_image_t * image,opj_jp2_color_t * color,opj_event_mgr_t * manager)1338 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
1339                                opj_event_mgr_t *manager)
1340 {
1341     opj_jp2_cdef_info_t *info;
1342     OPJ_UINT16 i, n, cn, asoc, acn;
1343 
1344     info = color->jp2_cdef->info;
1345     n = color->jp2_cdef->n;
1346 
1347     for (i = 0; i < n; ++i) {
1348         /* WATCH: acn = asoc - 1 ! */
1349         asoc = info[i].asoc;
1350         cn = info[i].cn;
1351 
1352         if (cn >= image->numcomps) {
1353             opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n",
1354                           cn, image->numcomps);
1355             continue;
1356         }
1357         if (asoc == 0 || asoc == 65535) {
1358             image->comps[cn].alpha = info[i].typ;
1359             continue;
1360         }
1361 
1362         acn = (OPJ_UINT16)(asoc - 1);
1363         if (acn >= image->numcomps) {
1364             opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n",
1365                           acn, image->numcomps);
1366             continue;
1367         }
1368 
1369         /* Swap only if color channel */
1370         if ((cn != acn) && (info[i].typ == 0)) {
1371             opj_image_comp_t saved;
1372             OPJ_UINT16 j;
1373 
1374             memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1375             memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1376             memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1377 
1378             /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1379             for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j) {
1380                 if (info[j].cn == cn) {
1381                     info[j].cn = acn;
1382                 } else if (info[j].cn == acn) {
1383                     info[j].cn = cn;
1384                 }
1385                 /* asoc is related to color index. Do not update. */
1386             }
1387         }
1388 
1389         image->comps[cn].alpha = info[i].typ;
1390     }
1391 
1392     if (color->jp2_cdef->info) {
1393         opj_free(color->jp2_cdef->info);
1394     }
1395 
1396     opj_free(color->jp2_cdef);
1397     color->jp2_cdef = NULL;
1398 
1399 }/* jp2_apply_cdef() */
1400 
opj_jp2_read_cdef(opj_jp2_t * jp2,OPJ_BYTE * p_cdef_header_data,OPJ_UINT32 p_cdef_header_size,opj_event_mgr_t * p_manager)1401 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
1402                                   OPJ_BYTE * p_cdef_header_data,
1403                                   OPJ_UINT32 p_cdef_header_size,
1404                                   opj_event_mgr_t * p_manager
1405                                  )
1406 {
1407     opj_jp2_cdef_info_t *cdef_info;
1408     OPJ_UINT16 i;
1409     OPJ_UINT32 l_value;
1410 
1411     /* preconditions */
1412     assert(jp2 != 00);
1413     assert(p_cdef_header_data != 00);
1414     assert(p_manager != 00);
1415     (void)p_cdef_header_size;
1416 
1417     /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1418      * inside a JP2 Header box.'*/
1419     if (jp2->color.jp2_cdef) {
1420         return OPJ_FALSE;
1421     }
1422 
1423     if (p_cdef_header_size < 2) {
1424         opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1425         return OPJ_FALSE;
1426     }
1427 
1428     opj_read_bytes(p_cdef_header_data, &l_value, 2);        /* N */
1429     p_cdef_header_data += 2;
1430 
1431     if ((OPJ_UINT16)l_value == 0) { /* szukw000: FIXME */
1432         opj_event_msg(p_manager, EVT_ERROR,
1433                       "Number of channel description is equal to zero in CDEF box.\n");
1434         return OPJ_FALSE;
1435     }
1436 
1437     if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1438         opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1439         return OPJ_FALSE;
1440     }
1441 
1442     cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(
1443                     opj_jp2_cdef_info_t));
1444     if (!cdef_info) {
1445         return OPJ_FALSE;
1446     }
1447 
1448     jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1449     if (!jp2->color.jp2_cdef) {
1450         opj_free(cdef_info);
1451         return OPJ_FALSE;
1452     }
1453     jp2->color.jp2_cdef->info = cdef_info;
1454     jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1455 
1456     for (i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1457         opj_read_bytes(p_cdef_header_data, &l_value, 2);            /* Cn^i */
1458         p_cdef_header_data += 2;
1459         cdef_info[i].cn = (OPJ_UINT16) l_value;
1460 
1461         opj_read_bytes(p_cdef_header_data, &l_value, 2);            /* Typ^i */
1462         p_cdef_header_data += 2;
1463         cdef_info[i].typ = (OPJ_UINT16) l_value;
1464 
1465         opj_read_bytes(p_cdef_header_data, &l_value, 2);            /* Asoc^i */
1466         p_cdef_header_data += 2;
1467         cdef_info[i].asoc = (OPJ_UINT16) l_value;
1468     }
1469 
1470     return OPJ_TRUE;
1471 }
1472 
opj_jp2_read_colr(opj_jp2_t * jp2,OPJ_BYTE * p_colr_header_data,OPJ_UINT32 p_colr_header_size,opj_event_mgr_t * p_manager)1473 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
1474                                   OPJ_BYTE * p_colr_header_data,
1475                                   OPJ_UINT32 p_colr_header_size,
1476                                   opj_event_mgr_t * p_manager
1477                                  )
1478 {
1479     OPJ_UINT32 l_value;
1480 
1481     /* preconditions */
1482     assert(jp2 != 00);
1483     assert(p_colr_header_data != 00);
1484     assert(p_manager != 00);
1485 
1486     if (p_colr_header_size < 3) {
1487         opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1488         return OPJ_FALSE;
1489     }
1490 
1491     /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1492      * Specification boxes after the first.'
1493     */
1494     if (jp2->color.jp2_has_colr) {
1495         opj_event_msg(p_manager, EVT_INFO,
1496                       "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1497         p_colr_header_data += p_colr_header_size;
1498         return OPJ_TRUE;
1499     }
1500 
1501     opj_read_bytes(p_colr_header_data, &jp2->meth, 1);          /* METH */
1502     ++p_colr_header_data;
1503 
1504     opj_read_bytes(p_colr_header_data, &jp2->precedence, 1);    /* PRECEDENCE */
1505     ++p_colr_header_data;
1506 
1507     opj_read_bytes(p_colr_header_data, &jp2->approx, 1);        /* APPROX */
1508     ++p_colr_header_data;
1509 
1510     if (jp2->meth == 1) {
1511         if (p_colr_header_size < 7) {
1512             opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n",
1513                           p_colr_header_size);
1514             return OPJ_FALSE;
1515         }
1516         if ((p_colr_header_size > 7) &&
1517                 (jp2->enumcs != 14)) { /* handled below for CIELab) */
1518             /* testcase Altona_Technical_v20_x4.pdf */
1519             opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n",
1520                           p_colr_header_size);
1521         }
1522 
1523         opj_read_bytes(p_colr_header_data, &jp2->enumcs, 4);        /* EnumCS */
1524 
1525         p_colr_header_data += 4;
1526 
1527         if (jp2->enumcs == 14) { /* CIELab */
1528             OPJ_UINT32 *cielab;
1529             OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
1530 
1531             cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
1532             if (cielab == NULL) {
1533                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
1534                 return OPJ_FALSE;
1535             }
1536             cielab[0] = 14; /* enumcs */
1537 
1538             /* default values */
1539             rl = ra = rb = ol = oa = ob = 0;
1540             il = 0x00443530; /* D50 */
1541             cielab[1] = 0x44454600;/* DEF */
1542 
1543             if (p_colr_header_size == 35) {
1544                 opj_read_bytes(p_colr_header_data, &rl, 4);
1545                 p_colr_header_data += 4;
1546                 opj_read_bytes(p_colr_header_data, &ol, 4);
1547                 p_colr_header_data += 4;
1548                 opj_read_bytes(p_colr_header_data, &ra, 4);
1549                 p_colr_header_data += 4;
1550                 opj_read_bytes(p_colr_header_data, &oa, 4);
1551                 p_colr_header_data += 4;
1552                 opj_read_bytes(p_colr_header_data, &rb, 4);
1553                 p_colr_header_data += 4;
1554                 opj_read_bytes(p_colr_header_data, &ob, 4);
1555                 p_colr_header_data += 4;
1556                 opj_read_bytes(p_colr_header_data, &il, 4);
1557                 p_colr_header_data += 4;
1558 
1559                 cielab[1] = 0;
1560             } else if (p_colr_header_size != 7) {
1561                 opj_event_msg(p_manager, EVT_WARNING,
1562                               "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size);
1563             }
1564             cielab[2] = rl;
1565             cielab[4] = ra;
1566             cielab[6] = rb;
1567             cielab[3] = ol;
1568             cielab[5] = oa;
1569             cielab[7] = ob;
1570             cielab[8] = il;
1571 
1572             jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
1573             jp2->color.icc_profile_len = 0;
1574         }
1575         jp2->color.jp2_has_colr = 1;
1576     } else if (jp2->meth == 2) {
1577         /* ICC profile */
1578         OPJ_INT32 it_icc_value = 0;
1579         OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1580 
1581         jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1582         jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1, (size_t)icc_len);
1583         if (!jp2->color.icc_profile_buf) {
1584             jp2->color.icc_profile_len = 0;
1585             return OPJ_FALSE;
1586         }
1587 
1588         for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) {
1589             opj_read_bytes(p_colr_header_data, &l_value, 1);    /* icc values */
1590             ++p_colr_header_data;
1591             jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1592         }
1593 
1594         jp2->color.jp2_has_colr = 1;
1595     } else if (jp2->meth > 2) {
1596         /*  ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
1597         conforming JP2 reader shall ignore the entire Colour Specification box.*/
1598         opj_event_msg(p_manager, EVT_INFO,
1599                       "COLR BOX meth value is not a regular value (%d), "
1600                       "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1601     }
1602     if (jp2->color.jp2_has_colr) {
1603         jp2->j2k->enumcs = jp2->enumcs;
1604     }
1605     return OPJ_TRUE;
1606 }
1607 
opj_jp2_decode(opj_jp2_t * jp2,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)1608 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1609                         opj_stream_private_t *p_stream,
1610                         opj_image_t* p_image,
1611                         opj_event_mgr_t * p_manager)
1612 {
1613     if (!p_image) {
1614         return OPJ_FALSE;
1615     }
1616 
1617     /* J2K decoding */
1618     if (! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager)) {
1619         opj_event_msg(p_manager, EVT_ERROR,
1620                       "Failed to decode the codestream in the JP2 file\n");
1621         return OPJ_FALSE;
1622     }
1623 
1624     if (jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
1625         /* Bypass all JP2 component transforms */
1626         return OPJ_TRUE;
1627     }
1628 
1629     if (!jp2->ignore_pclr_cmap_cdef) {
1630         if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1631             return OPJ_FALSE;
1632         }
1633 
1634         /* Set Image Color Space */
1635         if (jp2->enumcs == 16) {
1636             p_image->color_space = OPJ_CLRSPC_SRGB;
1637         } else if (jp2->enumcs == 17) {
1638             p_image->color_space = OPJ_CLRSPC_GRAY;
1639         } else if (jp2->enumcs == 18) {
1640             p_image->color_space = OPJ_CLRSPC_SYCC;
1641         } else if (jp2->enumcs == 24) {
1642             p_image->color_space = OPJ_CLRSPC_EYCC;
1643         } else if (jp2->enumcs == 12) {
1644             p_image->color_space = OPJ_CLRSPC_CMYK;
1645         } else {
1646             p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1647         }
1648 
1649         if (jp2->color.jp2_pclr) {
1650             /* Part 1, I.5.3.4: Either both or none : */
1651             if (!jp2->color.jp2_pclr->cmap) {
1652                 opj_jp2_free_pclr(&(jp2->color));
1653             } else {
1654                 if (!opj_jp2_apply_pclr(p_image, &(jp2->color), p_manager)) {
1655                     return OPJ_FALSE;
1656                 }
1657             }
1658         }
1659 
1660         /* Apply the color space if needed */
1661         if (jp2->color.jp2_cdef) {
1662             opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1663         }
1664 
1665         if (jp2->color.icc_profile_buf) {
1666             p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1667             p_image->icc_profile_len = jp2->color.icc_profile_len;
1668             jp2->color.icc_profile_buf = NULL;
1669         }
1670     }
1671 
1672     return OPJ_TRUE;
1673 }
1674 
opj_jp2_write_jp2h(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)1675 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1676                                    opj_stream_private_t *stream,
1677                                    opj_event_mgr_t * p_manager
1678                                   )
1679 {
1680     opj_jp2_img_header_writer_handler_t l_writers [4];
1681     opj_jp2_img_header_writer_handler_t * l_current_writer;
1682 
1683     OPJ_INT32 i, l_nb_pass;
1684     /* size of data for super box*/
1685     OPJ_UINT32 l_jp2h_size = 8;
1686     OPJ_BOOL l_result = OPJ_TRUE;
1687 
1688     /* to store the data of the super box */
1689     OPJ_BYTE l_jp2h_data [8];
1690 
1691     /* preconditions */
1692     assert(stream != 00);
1693     assert(jp2 != 00);
1694     assert(p_manager != 00);
1695 
1696     memset(l_writers, 0, sizeof(l_writers));
1697 
1698     if (jp2->bpc == 255) {
1699         l_nb_pass = 3;
1700         l_writers[0].handler = opj_jp2_write_ihdr;
1701         l_writers[1].handler = opj_jp2_write_bpcc;
1702         l_writers[2].handler = opj_jp2_write_colr;
1703     } else {
1704         l_nb_pass = 2;
1705         l_writers[0].handler = opj_jp2_write_ihdr;
1706         l_writers[1].handler = opj_jp2_write_colr;
1707     }
1708 
1709     if (jp2->color.jp2_cdef != NULL) {
1710         l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1711         l_nb_pass++;
1712     }
1713 
1714     /* write box header */
1715     /* write JP2H type */
1716     opj_write_bytes(l_jp2h_data + 4, JP2_JP2H, 4);
1717 
1718     l_current_writer = l_writers;
1719     for (i = 0; i < l_nb_pass; ++i) {
1720         l_current_writer->m_data = l_current_writer->handler(jp2,
1721                                    &(l_current_writer->m_size));
1722         if (l_current_writer->m_data == 00) {
1723             opj_event_msg(p_manager, EVT_ERROR,
1724                           "Not enough memory to hold JP2 Header data\n");
1725             l_result = OPJ_FALSE;
1726             break;
1727         }
1728 
1729         l_jp2h_size += l_current_writer->m_size;
1730         ++l_current_writer;
1731     }
1732 
1733     if (! l_result) {
1734         l_current_writer = l_writers;
1735         for (i = 0; i < l_nb_pass; ++i) {
1736             if (l_current_writer->m_data != 00) {
1737                 opj_free(l_current_writer->m_data);
1738             }
1739             ++l_current_writer;
1740         }
1741 
1742         return OPJ_FALSE;
1743     }
1744 
1745     /* write super box size */
1746     opj_write_bytes(l_jp2h_data, l_jp2h_size, 4);
1747 
1748     /* write super box data on stream */
1749     if (opj_stream_write_data(stream, l_jp2h_data, 8, p_manager) != 8) {
1750         opj_event_msg(p_manager, EVT_ERROR,
1751                       "Stream error while writing JP2 Header box\n");
1752         l_result = OPJ_FALSE;
1753     }
1754 
1755     if (l_result) {
1756         l_current_writer = l_writers;
1757         for (i = 0; i < l_nb_pass; ++i) {
1758             if (opj_stream_write_data(stream, l_current_writer->m_data,
1759                                       l_current_writer->m_size, p_manager) != l_current_writer->m_size) {
1760                 opj_event_msg(p_manager, EVT_ERROR,
1761                               "Stream error while writing JP2 Header box\n");
1762                 l_result = OPJ_FALSE;
1763                 break;
1764             }
1765             ++l_current_writer;
1766         }
1767     }
1768 
1769     l_current_writer = l_writers;
1770 
1771     /* cleanup */
1772     for (i = 0; i < l_nb_pass; ++i) {
1773         if (l_current_writer->m_data != 00) {
1774             opj_free(l_current_writer->m_data);
1775         }
1776         ++l_current_writer;
1777     }
1778 
1779     return l_result;
1780 }
1781 
opj_jp2_write_ftyp(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1782 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1783                                    opj_stream_private_t *cio,
1784                                    opj_event_mgr_t * p_manager)
1785 {
1786     OPJ_UINT32 i;
1787     OPJ_UINT32 l_ftyp_size;
1788     OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1789     OPJ_BOOL l_result;
1790 
1791     /* preconditions */
1792     assert(cio != 00);
1793     assert(jp2 != 00);
1794     assert(p_manager != 00);
1795     l_ftyp_size = 16 + 4 * jp2->numcl;
1796 
1797     l_ftyp_data = (OPJ_BYTE *) opj_calloc(1, l_ftyp_size);
1798 
1799     if (l_ftyp_data == 00) {
1800         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1801         return OPJ_FALSE;
1802     }
1803 
1804     l_current_data_ptr = l_ftyp_data;
1805 
1806     opj_write_bytes(l_current_data_ptr, l_ftyp_size, 4); /* box size */
1807     l_current_data_ptr += 4;
1808 
1809     opj_write_bytes(l_current_data_ptr, JP2_FTYP, 4); /* FTYP */
1810     l_current_data_ptr += 4;
1811 
1812     opj_write_bytes(l_current_data_ptr, jp2->brand, 4); /* BR */
1813     l_current_data_ptr += 4;
1814 
1815     opj_write_bytes(l_current_data_ptr, jp2->minversion, 4); /* MinV */
1816     l_current_data_ptr += 4;
1817 
1818     for (i = 0; i < jp2->numcl; i++)  {
1819         opj_write_bytes(l_current_data_ptr, jp2->cl[i], 4); /* CL */
1820     }
1821 
1822     l_result = (opj_stream_write_data(cio, l_ftyp_data, l_ftyp_size,
1823                                       p_manager) == l_ftyp_size);
1824     if (! l_result) {
1825         opj_event_msg(p_manager, EVT_ERROR,
1826                       "Error while writing ftyp data to stream\n");
1827     }
1828 
1829     opj_free(l_ftyp_data);
1830 
1831     return l_result;
1832 }
1833 
opj_jp2_write_jp2c(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1834 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1835                                    opj_stream_private_t *cio,
1836                                    opj_event_mgr_t * p_manager)
1837 {
1838     OPJ_OFF_T j2k_codestream_exit;
1839     OPJ_BYTE l_data_header [8];
1840 
1841     /* preconditions */
1842     assert(jp2 != 00);
1843     assert(cio != 00);
1844     assert(p_manager != 00);
1845     assert(opj_stream_has_seek(cio));
1846 
1847     j2k_codestream_exit = opj_stream_tell(cio);
1848     opj_write_bytes(l_data_header,
1849                     (OPJ_UINT32)(j2k_codestream_exit - jp2->j2k_codestream_offset),
1850                     4); /* size of codestream */
1851     opj_write_bytes(l_data_header + 4, JP2_JP2C,
1852                     4);                                     /* JP2C */
1853 
1854     if (! opj_stream_seek(cio, jp2->j2k_codestream_offset, p_manager)) {
1855         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1856         return OPJ_FALSE;
1857     }
1858 
1859     if (opj_stream_write_data(cio, l_data_header, 8, p_manager) != 8) {
1860         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1861         return OPJ_FALSE;
1862     }
1863 
1864     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
1865         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1866         return OPJ_FALSE;
1867     }
1868 
1869     return OPJ_TRUE;
1870 }
1871 
opj_jp2_write_jp(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1872 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
1873                                  opj_stream_private_t *cio,
1874                                  opj_event_mgr_t * p_manager)
1875 {
1876     /* 12 bytes will be read */
1877     OPJ_BYTE l_signature_data [12];
1878 
1879     /* preconditions */
1880     assert(cio != 00);
1881     assert(jp2 != 00);
1882     assert(p_manager != 00);
1883 
1884     OPJ_UNUSED(jp2);
1885 
1886     /* write box length */
1887     opj_write_bytes(l_signature_data, 12, 4);
1888     /* writes box type */
1889     opj_write_bytes(l_signature_data + 4, JP2_JP, 4);
1890     /* writes magic number*/
1891     opj_write_bytes(l_signature_data + 8, 0x0d0a870a, 4);
1892 
1893     if (opj_stream_write_data(cio, l_signature_data, 12, p_manager) != 12) {
1894         return OPJ_FALSE;
1895     }
1896 
1897     return OPJ_TRUE;
1898 }
1899 
1900 /* ----------------------------------------------------------------------- */
1901 /* JP2 decoder interface                                             */
1902 /* ----------------------------------------------------------------------- */
1903 
opj_jp2_setup_decoder(opj_jp2_t * jp2,opj_dparameters_t * parameters)1904 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1905 {
1906     /* setup the J2K codec */
1907     opj_j2k_setup_decoder(jp2->j2k, parameters);
1908 
1909     /* further JP2 initializations go here */
1910     jp2->color.jp2_has_colr = 0;
1911     jp2->comps = NULL;
1912     jp2->ignore_pclr_cmap_cdef = parameters->flags &
1913                                  OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1914 }
1915 
opj_jp2_set_threads(opj_jp2_t * jp2,OPJ_UINT32 num_threads)1916 OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads)
1917 {
1918     return opj_j2k_set_threads(jp2->j2k, num_threads);
1919 }
1920 
1921 /* ----------------------------------------------------------------------- */
1922 /* JP2 encoder interface                                             */
1923 /* ----------------------------------------------------------------------- */
1924 
opj_jp2_setup_encoder(opj_jp2_t * jp2,opj_cparameters_t * parameters,opj_image_t * image,opj_event_mgr_t * p_manager)1925 OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2,
1926                                opj_cparameters_t *parameters,
1927                                opj_image_t *image,
1928                                opj_event_mgr_t * p_manager)
1929 {
1930     OPJ_UINT32 i;
1931     OPJ_UINT32 depth_0;
1932     OPJ_UINT32 sign;
1933     OPJ_UINT32 alpha_count;
1934     OPJ_UINT32 color_channels = 0U;
1935     OPJ_UINT32 alpha_channel = 0U;
1936 
1937 
1938     if (!jp2 || !parameters || !image) {
1939         return OPJ_FALSE;
1940     }
1941 
1942     /* setup the J2K codec */
1943     /* ------------------- */
1944 
1945     /* Check if number of components respects standard */
1946     if (image->numcomps < 1 || image->numcomps > 16384) {
1947         opj_event_msg(p_manager, EVT_ERROR,
1948                       "Invalid number of components specified while setting up JP2 encoder\n");
1949         return OPJ_FALSE;
1950     }
1951 
1952     if (opj_j2k_setup_encoder(jp2->j2k, parameters, image,
1953                               p_manager) == OPJ_FALSE) {
1954         return OPJ_FALSE;
1955     }
1956 
1957     /* setup the JP2 codec */
1958     /* ------------------- */
1959 
1960     /* Profile box */
1961 
1962     jp2->brand = JP2_JP2;   /* BR */
1963     jp2->minversion = 0;    /* MinV */
1964     jp2->numcl = 1;
1965     jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1966     if (!jp2->cl) {
1967         opj_event_msg(p_manager, EVT_ERROR,
1968                       "Not enough memory when setup the JP2 encoder\n");
1969         return OPJ_FALSE;
1970     }
1971     jp2->cl[0] = JP2_JP2;   /* CL0 : JP2 */
1972 
1973     /* Image Header box */
1974 
1975     jp2->numcomps = image->numcomps;    /* NC */
1976     jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(
1977                      opj_jp2_comps_t));
1978     if (!jp2->comps) {
1979         opj_event_msg(p_manager, EVT_ERROR,
1980                       "Not enough memory when setup the JP2 encoder\n");
1981         /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1982         return OPJ_FALSE;
1983     }
1984 
1985     jp2->h = image->y1 - image->y0;     /* HEIGHT */
1986     jp2->w = image->x1 - image->x0;     /* WIDTH */
1987     /* BPC */
1988     depth_0 = image->comps[0].prec - 1;
1989     sign = image->comps[0].sgnd;
1990     jp2->bpc = depth_0 + (sign << 7);
1991     for (i = 1; i < image->numcomps; i++) {
1992         OPJ_UINT32 depth = image->comps[i].prec - 1;
1993         sign = image->comps[i].sgnd;
1994         if (depth_0 != depth) {
1995             jp2->bpc = 255;
1996         }
1997     }
1998     jp2->C = 7;         /* C : Always 7 */
1999     jp2->UnkC = 0;      /* UnkC, colorspace specified in colr box */
2000     jp2->IPR = 0;       /* IPR, no intellectual property */
2001 
2002     /* BitsPerComponent box */
2003     for (i = 0; i < image->numcomps; i++) {
2004         jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2005     }
2006 
2007     /* Colour Specification box */
2008     if (image->icc_profile_len) {
2009         jp2->meth = 2;
2010         jp2->enumcs = 0;
2011     } else {
2012         jp2->meth = 1;
2013         if (image->color_space == 1) {
2014             jp2->enumcs = 16;    /* sRGB as defined by IEC 61966-2-1 */
2015         } else if (image->color_space == 2) {
2016             jp2->enumcs = 17;    /* greyscale */
2017         } else if (image->color_space == 3) {
2018             jp2->enumcs = 18;    /* YUV */
2019         }
2020     }
2021 
2022     /* Channel Definition box */
2023     /* FIXME not provided by parameters */
2024     /* We try to do what we can... */
2025     alpha_count = 0U;
2026     for (i = 0; i < image->numcomps; i++) {
2027         if (image->comps[i].alpha != 0) {
2028             alpha_count++;
2029             alpha_channel = i;
2030         }
2031     }
2032     if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
2033         switch (jp2->enumcs) {
2034         case 16:
2035         case 18:
2036             color_channels = 3;
2037             break;
2038         case 17:
2039             color_channels = 1;
2040             break;
2041         default:
2042             alpha_count = 0U;
2043             break;
2044         }
2045         if (alpha_count == 0U) {
2046             opj_event_msg(p_manager, EVT_WARNING,
2047                           "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
2048         } else if (image->numcomps < (color_channels + 1)) {
2049             opj_event_msg(p_manager, EVT_WARNING,
2050                           "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
2051             alpha_count = 0U;
2052         } else if ((OPJ_UINT32)alpha_channel < color_channels) {
2053             opj_event_msg(p_manager, EVT_WARNING,
2054                           "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
2055             alpha_count = 0U;
2056         }
2057     } else if (alpha_count > 1) {
2058         opj_event_msg(p_manager, EVT_WARNING,
2059                       "Multiple alpha channels specified. No cdef box will be created.\n");
2060     }
2061     if (alpha_count == 1U) { /* if here, we know what we can do */
2062         jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
2063         if (!jp2->color.jp2_cdef) {
2064             opj_event_msg(p_manager, EVT_ERROR,
2065                           "Not enough memory to setup the JP2 encoder\n");
2066             return OPJ_FALSE;
2067         }
2068         /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
2069         /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
2070         jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(
2071                                         image->numcomps * sizeof(opj_jp2_cdef_info_t));
2072         if (!jp2->color.jp2_cdef->info) {
2073             /* memory will be freed by opj_jp2_destroy */
2074             opj_event_msg(p_manager, EVT_ERROR,
2075                           "Not enough memory to setup the JP2 encoder\n");
2076             return OPJ_FALSE;
2077         }
2078         jp2->color.jp2_cdef->n = (OPJ_UINT16)
2079                                  image->numcomps; /* cast is valid : image->numcomps [1,16384] */
2080         for (i = 0U; i < color_channels; i++) {
2081             jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2082                                               i; /* cast is valid : image->numcomps [1,16384] */
2083             jp2->color.jp2_cdef->info[i].typ = 0U;
2084             jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i +
2085                                                 1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
2086         }
2087         for (; i < image->numcomps; i++) {
2088             if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
2089                 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2090                                                   i; /* cast is valid : image->numcomps [1,16384] */
2091                 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
2092                 jp2->color.jp2_cdef->info[i].asoc =
2093                     0U; /* Apply alpha channel to the whole image */
2094             } else {
2095                 /* Unknown channel */
2096                 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2097                                                   i; /* cast is valid : image->numcomps [1,16384] */
2098                 jp2->color.jp2_cdef->info[i].typ = 65535U;
2099                 jp2->color.jp2_cdef->info[i].asoc = 65535U;
2100             }
2101         }
2102     }
2103 
2104     jp2->precedence = 0;    /* PRECEDENCE */
2105     jp2->approx = 0;        /* APPROX */
2106 
2107     jp2->jpip_on = parameters->jpip_on;
2108 
2109     return OPJ_TRUE;
2110 }
2111 
opj_jp2_encode(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2112 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
2113                         opj_stream_private_t *stream,
2114                         opj_event_mgr_t * p_manager)
2115 {
2116     return opj_j2k_encode(jp2->j2k, stream, p_manager);
2117 }
2118 
opj_jp2_end_decompress(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2119 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
2120                                 opj_stream_private_t *cio,
2121                                 opj_event_mgr_t * p_manager
2122                                )
2123 {
2124     /* preconditions */
2125     assert(jp2 != 00);
2126     assert(cio != 00);
2127     assert(p_manager != 00);
2128 
2129     /* customization of the end encoding */
2130     if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
2131         return OPJ_FALSE;
2132     }
2133 
2134     /* write header */
2135     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager)) {
2136         return OPJ_FALSE;
2137     }
2138 
2139     return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
2140 }
2141 
opj_jp2_end_compress(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2142 OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2,
2143                               opj_stream_private_t *cio,
2144                               opj_event_mgr_t * p_manager
2145                              )
2146 {
2147     /* preconditions */
2148     assert(jp2 != 00);
2149     assert(cio != 00);
2150     assert(p_manager != 00);
2151 
2152     /* customization of the end encoding */
2153     if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
2154         return OPJ_FALSE;
2155     }
2156 
2157     if (! opj_j2k_end_compress(jp2->j2k, cio, p_manager)) {
2158         return OPJ_FALSE;
2159     }
2160 
2161     /* write header */
2162     return opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager);
2163 }
2164 
opj_jp2_setup_end_header_writing(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2165 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
2166         opj_event_mgr_t * p_manager)
2167 {
2168     /* preconditions */
2169     assert(jp2 != 00);
2170     assert(p_manager != 00);
2171 
2172 #ifdef USE_JPIP
2173     if (jp2->jpip_on) {
2174         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2175                                                (opj_procedure)opj_jpip_write_iptr, p_manager)) {
2176             return OPJ_FALSE;
2177         }
2178     }
2179 #endif
2180     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2181                                            (opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2182         return OPJ_FALSE;
2183     }
2184     /* DEVELOPER CORNER, add your custom procedures */
2185 #ifdef USE_JPIP
2186     if (jp2->jpip_on) {
2187         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2188                                                (opj_procedure)opj_jpip_write_cidx, p_manager)) {
2189             return OPJ_FALSE;
2190         }
2191         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2192                                                (opj_procedure)opj_jpip_write_fidx, p_manager)) {
2193             return OPJ_FALSE;
2194         }
2195     }
2196 #endif
2197     return OPJ_TRUE;
2198 }
2199 
opj_jp2_setup_end_header_reading(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2200 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
2201         opj_event_mgr_t * p_manager)
2202 {
2203     /* preconditions */
2204     assert(jp2 != 00);
2205     assert(p_manager != 00);
2206 
2207     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2208                                            (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2209         return OPJ_FALSE;
2210     }
2211     /* DEVELOPER CORNER, add your custom procedures */
2212 
2213     return OPJ_TRUE;
2214 }
2215 
opj_jp2_default_validation(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2216 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
2217         opj_stream_private_t *cio,
2218         opj_event_mgr_t * p_manager
2219                                           )
2220 {
2221     OPJ_BOOL l_is_valid = OPJ_TRUE;
2222     OPJ_UINT32 i;
2223 
2224     /* preconditions */
2225     assert(jp2 != 00);
2226     assert(cio != 00);
2227     assert(p_manager != 00);
2228 
2229     OPJ_UNUSED(p_manager);
2230 
2231     /* JPEG2000 codec validation */
2232 
2233     /* STATE checking */
2234     /* make sure the state is at 0 */
2235     l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2236 
2237     /* make sure not reading a jp2h ???? WEIRD */
2238     l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2239 
2240     /* POINTER validation */
2241     /* make sure a j2k codec is present */
2242     l_is_valid &= (jp2->j2k != 00);
2243 
2244     /* make sure a procedure list is present */
2245     l_is_valid &= (jp2->m_procedure_list != 00);
2246 
2247     /* make sure a validation list is present */
2248     l_is_valid &= (jp2->m_validation_list != 00);
2249 
2250     /* PARAMETER VALIDATION */
2251     /* number of components */
2252     l_is_valid &= (jp2->numcl > 0);
2253     /* width */
2254     l_is_valid &= (jp2->h > 0);
2255     /* height */
2256     l_is_valid &= (jp2->w > 0);
2257     /* precision */
2258     for (i = 0; i < jp2->numcomps; ++i) {
2259         l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) <
2260                        38U); /* 0 is valid, ignore sign for check */
2261     }
2262 
2263     /* METH */
2264     l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2265 
2266     /* stream validation */
2267     /* back and forth is needed */
2268     l_is_valid &= opj_stream_has_seek(cio);
2269 
2270     return l_is_valid;
2271 }
2272 
opj_jp2_read_header_procedure(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2273 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
2274         opj_stream_private_t *stream,
2275         opj_event_mgr_t * p_manager
2276                                              )
2277 {
2278     opj_jp2_box_t box;
2279     OPJ_UINT32 l_nb_bytes_read;
2280     const opj_jp2_header_handler_t * l_current_handler;
2281     const opj_jp2_header_handler_t * l_current_handler_misplaced;
2282     OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
2283     OPJ_UINT32 l_current_data_size;
2284     OPJ_BYTE * l_current_data = 00;
2285 
2286     /* preconditions */
2287     assert(stream != 00);
2288     assert(jp2 != 00);
2289     assert(p_manager != 00);
2290 
2291     l_current_data = (OPJ_BYTE*)opj_calloc(1, l_last_data_size);
2292 
2293     if (l_current_data == 00) {
2294         opj_event_msg(p_manager, EVT_ERROR,
2295                       "Not enough memory to handle jpeg2000 file header\n");
2296         return OPJ_FALSE;
2297     }
2298 
2299     while (opj_jp2_read_boxhdr(&box, &l_nb_bytes_read, stream, p_manager)) {
2300         /* is it the codestream box ? */
2301         if (box.type == JP2_JP2C) {
2302             if (jp2->jp2_state & JP2_STATE_HEADER) {
2303                 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2304                 opj_free(l_current_data);
2305                 return OPJ_TRUE;
2306             } else {
2307                 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2308                 opj_free(l_current_data);
2309                 return OPJ_FALSE;
2310             }
2311         } else if (box.length == 0) {
2312             opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2313             opj_free(l_current_data);
2314             return OPJ_FALSE;
2315         }
2316         /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2317         else if (box.length < l_nb_bytes_read) {
2318             opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length,
2319                           box.type);
2320             opj_free(l_current_data);
2321             return OPJ_FALSE;
2322         }
2323 
2324         l_current_handler = opj_jp2_find_handler(box.type);
2325         l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
2326         l_current_data_size = box.length - l_nb_bytes_read;
2327 
2328         if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
2329             if (l_current_handler == 00) {
2330                 opj_event_msg(p_manager, EVT_WARNING,
2331                               "Found a misplaced '%c%c%c%c' box outside jp2h box\n",
2332                               (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2333                               (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2334                 if (jp2->jp2_state & JP2_STATE_HEADER) {
2335                     /* read anyway, we already have jp2h */
2336                     l_current_handler = l_current_handler_misplaced;
2337                 } else {
2338                     opj_event_msg(p_manager, EVT_WARNING,
2339                                   "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n",
2340                                   (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2341                                   (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2342                     jp2->jp2_state |= JP2_STATE_UNKNOWN;
2343                     if (opj_stream_skip(stream, l_current_data_size,
2344                                         p_manager) != l_current_data_size) {
2345                         opj_event_msg(p_manager, EVT_ERROR,
2346                                       "Problem with skipping JPEG2000 box, stream error\n");
2347                         opj_free(l_current_data);
2348                         return OPJ_FALSE;
2349                     }
2350                     continue;
2351                 }
2352             }
2353             if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2354                 /* do not even try to malloc if we can't read */
2355                 opj_event_msg(p_manager, EVT_ERROR,
2356                               "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n",
2357                               box.length, (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2358                               (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0), l_current_data_size,
2359                               (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
2360                 opj_free(l_current_data);
2361                 return OPJ_FALSE;
2362             }
2363             if (l_current_data_size > l_last_data_size) {
2364                 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,
2365                                              l_current_data_size);
2366                 if (!new_current_data) {
2367                     opj_free(l_current_data);
2368                     opj_event_msg(p_manager, EVT_ERROR,
2369                                   "Not enough memory to handle jpeg2000 box\n");
2370                     return OPJ_FALSE;
2371                 }
2372                 l_current_data = new_current_data;
2373                 l_last_data_size = l_current_data_size;
2374             }
2375 
2376             l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream, l_current_data,
2377                               l_current_data_size, p_manager);
2378             if (l_nb_bytes_read != l_current_data_size) {
2379                 opj_event_msg(p_manager, EVT_ERROR,
2380                               "Problem with reading JPEG2000 box, stream error\n");
2381                 opj_free(l_current_data);
2382                 return OPJ_FALSE;
2383             }
2384 
2385             if (! l_current_handler->handler(jp2, l_current_data, l_current_data_size,
2386                                              p_manager)) {
2387                 opj_free(l_current_data);
2388                 return OPJ_FALSE;
2389             }
2390         } else {
2391             if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2392                 opj_event_msg(p_manager, EVT_ERROR,
2393                               "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2394                 opj_free(l_current_data);
2395                 return OPJ_FALSE;
2396             }
2397             if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2398                 opj_event_msg(p_manager, EVT_ERROR,
2399                               "Malformed JP2 file format: second box must be file type box\n");
2400                 opj_free(l_current_data);
2401                 return OPJ_FALSE;
2402             }
2403             jp2->jp2_state |= JP2_STATE_UNKNOWN;
2404             if (opj_stream_skip(stream, l_current_data_size,
2405                                 p_manager) != l_current_data_size) {
2406                 if (jp2->jp2_state & JP2_STATE_CODESTREAM) {
2407                     /* If we already read the codestream, do not error out */
2408                     /* Needed for data/input/nonregression/issue254.jp2 */
2409                     opj_event_msg(p_manager, EVT_WARNING,
2410                                   "Problem with skipping JPEG2000 box, stream error\n");
2411                     opj_free(l_current_data);
2412                     return OPJ_TRUE;
2413                 } else {
2414                     opj_event_msg(p_manager, EVT_ERROR,
2415                                   "Problem with skipping JPEG2000 box, stream error\n");
2416                     opj_free(l_current_data);
2417                     return OPJ_FALSE;
2418                 }
2419             }
2420         }
2421     }
2422 
2423     opj_free(l_current_data);
2424 
2425     return OPJ_TRUE;
2426 }
2427 
2428 /**
2429  * Executes the given procedures on the given codec.
2430  *
2431  * @param   p_procedure_list    the list of procedures to execute
2432  * @param   jp2                 the jpeg2000 file codec to execute the procedures on.
2433  * @param   stream                  the stream to execute the procedures on.
2434  * @param   p_manager           the user manager.
2435  *
2436  * @return  true                if all the procedures were successfully executed.
2437  */
opj_jp2_exec(opj_jp2_t * jp2,opj_procedure_list_t * p_procedure_list,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2438 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
2439                              opj_procedure_list_t * p_procedure_list,
2440                              opj_stream_private_t *stream,
2441                              opj_event_mgr_t * p_manager
2442                             )
2443 
2444 {
2445     OPJ_BOOL(** l_procedure)(opj_jp2_t * jp2, opj_stream_private_t *,
2446                              opj_event_mgr_t *) = 00;
2447     OPJ_BOOL l_result = OPJ_TRUE;
2448     OPJ_UINT32 l_nb_proc, i;
2449 
2450     /* preconditions */
2451     assert(p_procedure_list != 00);
2452     assert(jp2 != 00);
2453     assert(stream != 00);
2454     assert(p_manager != 00);
2455 
2456     l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2457     l_procedure = (OPJ_BOOL(**)(opj_jp2_t * jp2, opj_stream_private_t *,
2458                                 opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2459 
2460     for (i = 0; i < l_nb_proc; ++i) {
2461         l_result = l_result && (*l_procedure)(jp2, stream, p_manager);
2462         ++l_procedure;
2463     }
2464 
2465     /* and clear the procedure list at the end. */
2466     opj_procedure_list_clear(p_procedure_list);
2467     return l_result;
2468 }
2469 
opj_jp2_start_compress(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)2470 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2471                                 opj_stream_private_t *stream,
2472                                 opj_image_t * p_image,
2473                                 opj_event_mgr_t * p_manager
2474                                )
2475 {
2476     /* preconditions */
2477     assert(jp2 != 00);
2478     assert(stream != 00);
2479     assert(p_manager != 00);
2480 
2481     /* customization of the validation */
2482     if (! opj_jp2_setup_encoding_validation(jp2, p_manager)) {
2483         return OPJ_FALSE;
2484     }
2485 
2486     /* validation of the parameters codec */
2487     if (! opj_jp2_exec(jp2, jp2->m_validation_list, stream, p_manager)) {
2488         return OPJ_FALSE;
2489     }
2490 
2491     /* customization of the encoding */
2492     if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2493         return OPJ_FALSE;
2494     }
2495 
2496     /* write header */
2497     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, stream, p_manager)) {
2498         return OPJ_FALSE;
2499     }
2500 
2501     return opj_j2k_start_compress(jp2->j2k, stream, p_image, p_manager);
2502 }
2503 
opj_jp2_find_handler(OPJ_UINT32 p_id)2504 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id)
2505 {
2506     OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(
2507                                        opj_jp2_header_handler_t);
2508 
2509     for (i = 0; i < l_handler_size; ++i) {
2510         if (jp2_header[i].id == p_id) {
2511             return &jp2_header[i];
2512         }
2513     }
2514     return NULL;
2515 }
2516 
2517 /**
2518  * Finds the image execution function related to the given box id.
2519  *
2520  * @param   p_id    the id of the handler to fetch.
2521  *
2522  * @return  the given handler or 00 if it could not be found.
2523  */
opj_jp2_img_find_handler(OPJ_UINT32 p_id)2524 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
2525     OPJ_UINT32 p_id)
2526 {
2527     OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(
2528                                        opj_jp2_header_handler_t);
2529     for (i = 0; i < l_handler_size; ++i) {
2530         if (jp2_img_header[i].id == p_id) {
2531             return &jp2_img_header[i];
2532         }
2533     }
2534 
2535     return NULL;
2536 }
2537 
2538 /**
2539  * Reads a jpeg2000 file signature box.
2540  *
2541  * @param   p_header_data   the data contained in the signature box.
2542  * @param   jp2             the jpeg2000 file codec.
2543  * @param   p_header_size   the size of the data contained in the signature box.
2544  * @param   p_manager       the user event manager.
2545  *
2546  * @return true if the file signature box is valid.
2547  */
opj_jp2_read_jp(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2548 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2549                                 OPJ_BYTE * p_header_data,
2550                                 OPJ_UINT32 p_header_size,
2551                                 opj_event_mgr_t * p_manager
2552                                )
2553 
2554 {
2555     OPJ_UINT32 l_magic_number;
2556 
2557     /* preconditions */
2558     assert(p_header_data != 00);
2559     assert(jp2 != 00);
2560     assert(p_manager != 00);
2561 
2562     if (jp2->jp2_state != JP2_STATE_NONE) {
2563         opj_event_msg(p_manager, EVT_ERROR,
2564                       "The signature box must be the first box in the file.\n");
2565         return OPJ_FALSE;
2566     }
2567 
2568     /* assure length of data is correct (4 -> magic number) */
2569     if (p_header_size != 4) {
2570         opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2571         return OPJ_FALSE;
2572     }
2573 
2574     /* rearrange data */
2575     opj_read_bytes(p_header_data, &l_magic_number, 4);
2576     if (l_magic_number != 0x0d0a870a) {
2577         opj_event_msg(p_manager, EVT_ERROR,
2578                       "Error with JP Signature : bad magic number\n");
2579         return OPJ_FALSE;
2580     }
2581 
2582     jp2->jp2_state |= JP2_STATE_SIGNATURE;
2583 
2584     return OPJ_TRUE;
2585 }
2586 
2587 /**
2588  * Reads a a FTYP box - File type box
2589  *
2590  * @param   p_header_data   the data contained in the FTYP box.
2591  * @param   jp2             the jpeg2000 file codec.
2592  * @param   p_header_size   the size of the data contained in the FTYP box.
2593  * @param   p_manager       the user event manager.
2594  *
2595  * @return true if the FTYP box is valid.
2596  */
opj_jp2_read_ftyp(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2597 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
2598                                   OPJ_BYTE * p_header_data,
2599                                   OPJ_UINT32 p_header_size,
2600                                   opj_event_mgr_t * p_manager
2601                                  )
2602 {
2603     OPJ_UINT32 i, l_remaining_bytes;
2604 
2605     /* preconditions */
2606     assert(p_header_data != 00);
2607     assert(jp2 != 00);
2608     assert(p_manager != 00);
2609 
2610     if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2611         opj_event_msg(p_manager, EVT_ERROR,
2612                       "The ftyp box must be the second box in the file.\n");
2613         return OPJ_FALSE;
2614     }
2615 
2616     /* assure length of data is correct */
2617     if (p_header_size < 8) {
2618         opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2619         return OPJ_FALSE;
2620     }
2621 
2622     opj_read_bytes(p_header_data, &jp2->brand, 4);      /* BR */
2623     p_header_data += 4;
2624 
2625     opj_read_bytes(p_header_data, &jp2->minversion, 4);     /* MinV */
2626     p_header_data += 4;
2627 
2628     l_remaining_bytes = p_header_size - 8;
2629 
2630     /* the number of remaining bytes should be a multiple of 4 */
2631     if ((l_remaining_bytes & 0x3) != 0) {
2632         opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2633         return OPJ_FALSE;
2634     }
2635 
2636     /* div by 4 */
2637     jp2->numcl = l_remaining_bytes >> 2;
2638     if (jp2->numcl) {
2639         jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2640         if (jp2->cl == 00) {
2641             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2642             return OPJ_FALSE;
2643         }
2644     }
2645 
2646     for (i = 0; i < jp2->numcl; ++i) {
2647         opj_read_bytes(p_header_data, &jp2->cl[i], 4);      /* CLi */
2648         p_header_data += 4;
2649     }
2650 
2651     jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2652 
2653     return OPJ_TRUE;
2654 }
2655 
opj_jp2_skip_jp2c(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2656 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
2657                                   opj_stream_private_t *stream,
2658                                   opj_event_mgr_t * p_manager)
2659 {
2660     /* preconditions */
2661     assert(jp2 != 00);
2662     assert(stream != 00);
2663     assert(p_manager != 00);
2664 
2665     jp2->j2k_codestream_offset = opj_stream_tell(stream);
2666 
2667     if (opj_stream_skip(stream, 8, p_manager) != 8) {
2668         return OPJ_FALSE;
2669     }
2670 
2671     return OPJ_TRUE;
2672 }
2673 
opj_jpip_skip_iptr(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2674 static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2,
2675                                    opj_stream_private_t *stream,
2676                                    opj_event_mgr_t * p_manager)
2677 {
2678     /* preconditions */
2679     assert(jp2 != 00);
2680     assert(stream != 00);
2681     assert(p_manager != 00);
2682 
2683     jp2->jpip_iptr_offset = opj_stream_tell(stream);
2684 
2685     if (opj_stream_skip(stream, 24, p_manager) != 24) {
2686         return OPJ_FALSE;
2687     }
2688 
2689     return OPJ_TRUE;
2690 }
2691 
2692 /**
2693  * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2694  *
2695  * @param   p_header_data   the data contained in the file header box.
2696  * @param   jp2             the jpeg2000 file codec.
2697  * @param   p_header_size   the size of the data contained in the file header box.
2698  * @param   p_manager       the user event manager.
2699  *
2700  * @return true if the JP2 Header box was successfully recognized.
2701 */
opj_jp2_read_jp2h(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2702 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
2703                                   OPJ_BYTE *p_header_data,
2704                                   OPJ_UINT32 p_header_size,
2705                                   opj_event_mgr_t * p_manager
2706                                  )
2707 {
2708     OPJ_UINT32 l_box_size = 0, l_current_data_size = 0;
2709     opj_jp2_box_t box;
2710     const opj_jp2_header_handler_t * l_current_handler;
2711     OPJ_BOOL l_has_ihdr = 0;
2712 
2713     /* preconditions */
2714     assert(p_header_data != 00);
2715     assert(jp2 != 00);
2716     assert(p_manager != 00);
2717 
2718     /* make sure the box is well placed */
2719     if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE) {
2720         opj_event_msg(p_manager, EVT_ERROR,
2721                       "The  box must be the first box in the file.\n");
2722         return OPJ_FALSE;
2723     }
2724 
2725     jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2726 
2727     /* iterate while remaining data */
2728     while (p_header_size > 0) {
2729 
2730         if (! opj_jp2_read_boxhdr_char(&box, p_header_data, &l_box_size, p_header_size,
2731                                        p_manager)) {
2732             opj_event_msg(p_manager, EVT_ERROR,
2733                           "Stream error while reading JP2 Header box\n");
2734             return OPJ_FALSE;
2735         }
2736 
2737         if (box.length > p_header_size) {
2738             opj_event_msg(p_manager, EVT_ERROR,
2739                           "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2740             return OPJ_FALSE;
2741         }
2742 
2743         l_current_handler = opj_jp2_img_find_handler(box.type);
2744         l_current_data_size = box.length - l_box_size;
2745         p_header_data += l_box_size;
2746 
2747         if (l_current_handler != 00) {
2748             if (! l_current_handler->handler(jp2, p_header_data, l_current_data_size,
2749                                              p_manager)) {
2750                 return OPJ_FALSE;
2751             }
2752         } else {
2753             jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2754         }
2755 
2756         if (box.type == JP2_IHDR) {
2757             l_has_ihdr = 1;
2758         }
2759 
2760         p_header_data += l_current_data_size;
2761         p_header_size -= box.length;
2762     }
2763 
2764     if (l_has_ihdr == 0) {
2765         opj_event_msg(p_manager, EVT_ERROR,
2766                       "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2767         return OPJ_FALSE;
2768     }
2769 
2770     jp2->jp2_state |= JP2_STATE_HEADER;
2771     jp2->has_jp2h = 1;
2772 
2773     return OPJ_TRUE;
2774 }
2775 
opj_jp2_read_boxhdr_char(opj_jp2_box_t * box,OPJ_BYTE * p_data,OPJ_UINT32 * p_number_bytes_read,OPJ_UINT32 p_box_max_size,opj_event_mgr_t * p_manager)2776 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
2777         OPJ_BYTE * p_data,
2778         OPJ_UINT32 * p_number_bytes_read,
2779         OPJ_UINT32 p_box_max_size,
2780         opj_event_mgr_t * p_manager
2781                                         )
2782 {
2783     OPJ_UINT32 l_value;
2784 
2785     /* preconditions */
2786     assert(p_data != 00);
2787     assert(box != 00);
2788     assert(p_number_bytes_read != 00);
2789     assert(p_manager != 00);
2790 
2791     if (p_box_max_size < 8) {
2792         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2793         return OPJ_FALSE;
2794     }
2795 
2796     /* process read data */
2797     opj_read_bytes(p_data, &l_value, 4);
2798     p_data += 4;
2799     box->length = (OPJ_UINT32)(l_value);
2800 
2801     opj_read_bytes(p_data, &l_value, 4);
2802     p_data += 4;
2803     box->type = (OPJ_UINT32)(l_value);
2804 
2805     *p_number_bytes_read = 8;
2806 
2807     /* do we have a "special very large box ?" */
2808     /* read then the XLBox */
2809     if (box->length == 1) {
2810         OPJ_UINT32 l_xl_part_size;
2811 
2812         if (p_box_max_size < 16) {
2813             opj_event_msg(p_manager, EVT_ERROR,
2814                           "Cannot handle XL box of less than 16 bytes\n");
2815             return OPJ_FALSE;
2816         }
2817 
2818         opj_read_bytes(p_data, &l_xl_part_size, 4);
2819         p_data += 4;
2820         *p_number_bytes_read += 4;
2821 
2822         if (l_xl_part_size != 0) {
2823             opj_event_msg(p_manager, EVT_ERROR,
2824                           "Cannot handle box sizes higher than 2^32\n");
2825             return OPJ_FALSE;
2826         }
2827 
2828         opj_read_bytes(p_data, &l_value, 4);
2829         *p_number_bytes_read += 4;
2830         box->length = (OPJ_UINT32)(l_value);
2831 
2832         if (box->length == 0) {
2833             opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2834             return OPJ_FALSE;
2835         }
2836     } else if (box->length == 0) {
2837         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2838         return OPJ_FALSE;
2839     }
2840     if (box->length < *p_number_bytes_read) {
2841         opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2842         return OPJ_FALSE;
2843     }
2844     return OPJ_TRUE;
2845 }
2846 
opj_jp2_read_header(opj_stream_private_t * p_stream,opj_jp2_t * jp2,opj_image_t ** p_image,opj_event_mgr_t * p_manager)2847 OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
2848                              opj_jp2_t *jp2,
2849                              opj_image_t ** p_image,
2850                              opj_event_mgr_t * p_manager
2851                             )
2852 {
2853     /* preconditions */
2854     assert(jp2 != 00);
2855     assert(p_stream != 00);
2856     assert(p_manager != 00);
2857 
2858     /* customization of the validation */
2859     if (! opj_jp2_setup_decoding_validation(jp2, p_manager)) {
2860         return OPJ_FALSE;
2861     }
2862 
2863     /* customization of the encoding */
2864     if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2865         return OPJ_FALSE;
2866     }
2867 
2868     /* validation of the parameters codec */
2869     if (! opj_jp2_exec(jp2, jp2->m_validation_list, p_stream, p_manager)) {
2870         return OPJ_FALSE;
2871     }
2872 
2873     /* read header */
2874     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) {
2875         return OPJ_FALSE;
2876     }
2877     if (jp2->has_jp2h == 0) {
2878         opj_event_msg(p_manager, EVT_ERROR, "JP2H box missing. Required.\n");
2879         return OPJ_FALSE;
2880     }
2881     if (jp2->has_ihdr == 0) {
2882         opj_event_msg(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n");
2883         return OPJ_FALSE;
2884     }
2885 
2886     return opj_j2k_read_header(p_stream,
2887                                jp2->j2k,
2888                                p_image,
2889                                p_manager);
2890 }
2891 
opj_jp2_setup_encoding_validation(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2892 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
2893         opj_event_mgr_t * p_manager)
2894 {
2895     /* preconditions */
2896     assert(jp2 != 00);
2897     assert(p_manager != 00);
2898 
2899     if (! opj_procedure_list_add_procedure(jp2->m_validation_list,
2900                                            (opj_procedure)opj_jp2_default_validation, p_manager)) {
2901         return OPJ_FALSE;
2902     }
2903     /* DEVELOPER CORNER, add your custom validation procedure */
2904 
2905     return OPJ_TRUE;
2906 }
2907 
opj_jp2_setup_decoding_validation(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2908 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
2909         opj_event_mgr_t * p_manager)
2910 {
2911     /* preconditions */
2912     assert(jp2 != 00);
2913     assert(p_manager != 00);
2914 
2915     OPJ_UNUSED(jp2);
2916     OPJ_UNUSED(p_manager);
2917 
2918     /* DEVELOPER CORNER, add your custom validation procedure */
2919 
2920     return OPJ_TRUE;
2921 }
2922 
opj_jp2_setup_header_writing(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2923 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
2924         opj_event_mgr_t * p_manager)
2925 {
2926     /* preconditions */
2927     assert(jp2 != 00);
2928     assert(p_manager != 00);
2929 
2930     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2931                                            (opj_procedure)opj_jp2_write_jp, p_manager)) {
2932         return OPJ_FALSE;
2933     }
2934     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2935                                            (opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2936         return OPJ_FALSE;
2937     }
2938     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2939                                            (opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2940         return OPJ_FALSE;
2941     }
2942     if (jp2->jpip_on) {
2943         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2944                                                (opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2945             return OPJ_FALSE;
2946         }
2947     }
2948     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2949                                            (opj_procedure)opj_jp2_skip_jp2c, p_manager)) {
2950         return OPJ_FALSE;
2951     }
2952 
2953     /* DEVELOPER CORNER, insert your custom procedures */
2954 
2955     return OPJ_TRUE;
2956 }
2957 
opj_jp2_setup_header_reading(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2958 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
2959         opj_event_mgr_t * p_manager)
2960 {
2961     /* preconditions */
2962     assert(jp2 != 00);
2963     assert(p_manager != 00);
2964 
2965     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2966                                            (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2967         return OPJ_FALSE;
2968     }
2969 
2970     /* DEVELOPER CORNER, add your custom procedures */
2971 
2972     return OPJ_TRUE;
2973 }
2974 
opj_jp2_read_tile_header(opj_jp2_t * p_jp2,OPJ_UINT32 * p_tile_index,OPJ_UINT32 * p_data_size,OPJ_INT32 * p_tile_x0,OPJ_INT32 * p_tile_y0,OPJ_INT32 * p_tile_x1,OPJ_INT32 * p_tile_y1,OPJ_UINT32 * p_nb_comps,OPJ_BOOL * p_go_on,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2975 OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2,
2976                                   OPJ_UINT32 * p_tile_index,
2977                                   OPJ_UINT32 * p_data_size,
2978                                   OPJ_INT32 * p_tile_x0,
2979                                   OPJ_INT32 * p_tile_y0,
2980                                   OPJ_INT32 * p_tile_x1,
2981                                   OPJ_INT32 * p_tile_y1,
2982                                   OPJ_UINT32 * p_nb_comps,
2983                                   OPJ_BOOL * p_go_on,
2984                                   opj_stream_private_t *p_stream,
2985                                   opj_event_mgr_t * p_manager
2986                                  )
2987 {
2988     return opj_j2k_read_tile_header(p_jp2->j2k,
2989                                     p_tile_index,
2990                                     p_data_size,
2991                                     p_tile_x0, p_tile_y0,
2992                                     p_tile_x1, p_tile_y1,
2993                                     p_nb_comps,
2994                                     p_go_on,
2995                                     p_stream,
2996                                     p_manager);
2997 }
2998 
opj_jp2_write_tile(opj_jp2_t * p_jp2,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2999 OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2,
3000                             OPJ_UINT32 p_tile_index,
3001                             OPJ_BYTE * p_data,
3002                             OPJ_UINT32 p_data_size,
3003                             opj_stream_private_t *p_stream,
3004                             opj_event_mgr_t * p_manager
3005                            )
3006 
3007 {
3008     return opj_j2k_write_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3009                               p_stream, p_manager);
3010 }
3011 
opj_jp2_decode_tile(opj_jp2_t * p_jp2,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)3012 OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2,
3013                              OPJ_UINT32 p_tile_index,
3014                              OPJ_BYTE * p_data,
3015                              OPJ_UINT32 p_data_size,
3016                              opj_stream_private_t *p_stream,
3017                              opj_event_mgr_t * p_manager
3018                             )
3019 {
3020     return opj_j2k_decode_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3021                                p_stream, p_manager);
3022 }
3023 
opj_jp2_destroy(opj_jp2_t * jp2)3024 void opj_jp2_destroy(opj_jp2_t *jp2)
3025 {
3026     if (jp2) {
3027         /* destroy the J2K codec */
3028         opj_j2k_destroy(jp2->j2k);
3029         jp2->j2k = 00;
3030 
3031         if (jp2->comps) {
3032             opj_free(jp2->comps);
3033             jp2->comps = 00;
3034         }
3035 
3036         if (jp2->cl) {
3037             opj_free(jp2->cl);
3038             jp2->cl = 00;
3039         }
3040 
3041         if (jp2->color.icc_profile_buf) {
3042             opj_free(jp2->color.icc_profile_buf);
3043             jp2->color.icc_profile_buf = 00;
3044         }
3045 
3046         if (jp2->color.jp2_cdef) {
3047             if (jp2->color.jp2_cdef->info) {
3048                 opj_free(jp2->color.jp2_cdef->info);
3049                 jp2->color.jp2_cdef->info = NULL;
3050             }
3051 
3052             opj_free(jp2->color.jp2_cdef);
3053             jp2->color.jp2_cdef = 00;
3054         }
3055 
3056         if (jp2->color.jp2_pclr) {
3057             if (jp2->color.jp2_pclr->cmap) {
3058                 opj_free(jp2->color.jp2_pclr->cmap);
3059                 jp2->color.jp2_pclr->cmap = NULL;
3060             }
3061             if (jp2->color.jp2_pclr->channel_sign) {
3062                 opj_free(jp2->color.jp2_pclr->channel_sign);
3063                 jp2->color.jp2_pclr->channel_sign = NULL;
3064             }
3065             if (jp2->color.jp2_pclr->channel_size) {
3066                 opj_free(jp2->color.jp2_pclr->channel_size);
3067                 jp2->color.jp2_pclr->channel_size = NULL;
3068             }
3069             if (jp2->color.jp2_pclr->entries) {
3070                 opj_free(jp2->color.jp2_pclr->entries);
3071                 jp2->color.jp2_pclr->entries = NULL;
3072             }
3073 
3074             opj_free(jp2->color.jp2_pclr);
3075             jp2->color.jp2_pclr = 00;
3076         }
3077 
3078         if (jp2->m_validation_list) {
3079             opj_procedure_list_destroy(jp2->m_validation_list);
3080             jp2->m_validation_list = 00;
3081         }
3082 
3083         if (jp2->m_procedure_list) {
3084             opj_procedure_list_destroy(jp2->m_procedure_list);
3085             jp2->m_procedure_list = 00;
3086         }
3087 
3088         opj_free(jp2);
3089     }
3090 }
3091 
opj_jp2_set_decoded_components(opj_jp2_t * p_jp2,OPJ_UINT32 numcomps,const OPJ_UINT32 * comps_indices,opj_event_mgr_t * p_manager)3092 OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *p_jp2,
3093                                         OPJ_UINT32 numcomps,
3094                                         const OPJ_UINT32* comps_indices,
3095                                         opj_event_mgr_t * p_manager)
3096 {
3097     return opj_j2k_set_decoded_components(p_jp2->j2k,
3098                                           numcomps, comps_indices,
3099                                           p_manager);
3100 }
3101 
opj_jp2_set_decode_area(opj_jp2_t * p_jp2,opj_image_t * p_image,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y,opj_event_mgr_t * p_manager)3102 OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
3103                                  opj_image_t* p_image,
3104                                  OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3105                                  OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3106                                  opj_event_mgr_t * p_manager
3107                                 )
3108 {
3109     return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y,
3110                                    p_end_x, p_end_y, p_manager);
3111 }
3112 
opj_jp2_get_tile(opj_jp2_t * p_jp2,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager,OPJ_UINT32 tile_index)3113 OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
3114                           opj_stream_private_t *p_stream,
3115                           opj_image_t* p_image,
3116                           opj_event_mgr_t * p_manager,
3117                           OPJ_UINT32 tile_index
3118                          )
3119 {
3120     if (!p_image) {
3121         return OPJ_FALSE;
3122     }
3123 
3124     opj_event_msg(p_manager, EVT_WARNING,
3125                   "JP2 box which are after the codestream will not be read by this function.\n");
3126 
3127     if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index)) {
3128         opj_event_msg(p_manager, EVT_ERROR,
3129                       "Failed to decode the codestream in the JP2 file\n");
3130         return OPJ_FALSE;
3131     }
3132 
3133     if (p_jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
3134         /* Bypass all JP2 component transforms */
3135         return OPJ_TRUE;
3136     }
3137 
3138     if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
3139         return OPJ_FALSE;
3140     }
3141 
3142     /* Set Image Color Space */
3143     if (p_jp2->enumcs == 16) {
3144         p_image->color_space = OPJ_CLRSPC_SRGB;
3145     } else if (p_jp2->enumcs == 17) {
3146         p_image->color_space = OPJ_CLRSPC_GRAY;
3147     } else if (p_jp2->enumcs == 18) {
3148         p_image->color_space = OPJ_CLRSPC_SYCC;
3149     } else if (p_jp2->enumcs == 24) {
3150         p_image->color_space = OPJ_CLRSPC_EYCC;
3151     } else if (p_jp2->enumcs == 12) {
3152         p_image->color_space = OPJ_CLRSPC_CMYK;
3153     } else {
3154         p_image->color_space = OPJ_CLRSPC_UNKNOWN;
3155     }
3156 
3157     if (p_jp2->color.jp2_pclr) {
3158         /* Part 1, I.5.3.4: Either both or none : */
3159         if (!p_jp2->color.jp2_pclr->cmap) {
3160             opj_jp2_free_pclr(&(p_jp2->color));
3161         } else {
3162             if (!opj_jp2_apply_pclr(p_image, &(p_jp2->color), p_manager)) {
3163                 return OPJ_FALSE;
3164             }
3165         }
3166     }
3167 
3168     /* Apply the color space if needed */
3169     if (p_jp2->color.jp2_cdef) {
3170         opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager);
3171     }
3172 
3173     if (p_jp2->color.icc_profile_buf) {
3174         p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
3175         p_image->icc_profile_len = p_jp2->color.icc_profile_len;
3176         p_jp2->color.icc_profile_buf = NULL;
3177     }
3178 
3179     return OPJ_TRUE;
3180 }
3181 
3182 /* ----------------------------------------------------------------------- */
3183 /* JP2 encoder interface                                             */
3184 /* ----------------------------------------------------------------------- */
3185 
opj_jp2_create(OPJ_BOOL p_is_decoder)3186 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
3187 {
3188     opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1, sizeof(opj_jp2_t));
3189     if (jp2) {
3190 
3191         /* create the J2K codec */
3192         if (! p_is_decoder) {
3193             jp2->j2k = opj_j2k_create_compress();
3194         } else {
3195             jp2->j2k = opj_j2k_create_decompress();
3196         }
3197 
3198         if (jp2->j2k == 00) {
3199             opj_jp2_destroy(jp2);
3200             return 00;
3201         }
3202 
3203         /* Color structure */
3204         jp2->color.icc_profile_buf = NULL;
3205         jp2->color.icc_profile_len = 0;
3206         jp2->color.jp2_cdef = NULL;
3207         jp2->color.jp2_pclr = NULL;
3208         jp2->color.jp2_has_colr = 0;
3209 
3210         /* validation list creation */
3211         jp2->m_validation_list = opj_procedure_list_create();
3212         if (! jp2->m_validation_list) {
3213             opj_jp2_destroy(jp2);
3214             return 00;
3215         }
3216 
3217         /* execution list creation */
3218         jp2->m_procedure_list = opj_procedure_list_create();
3219         if (! jp2->m_procedure_list) {
3220             opj_jp2_destroy(jp2);
3221             return 00;
3222         }
3223     }
3224 
3225     return jp2;
3226 }
3227 
jp2_dump(opj_jp2_t * p_jp2,OPJ_INT32 flag,FILE * out_stream)3228 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3229 {
3230     /* preconditions */
3231     assert(p_jp2 != 00);
3232 
3233     j2k_dump(p_jp2->j2k,
3234              flag,
3235              out_stream);
3236 }
3237 
jp2_get_cstr_index(opj_jp2_t * p_jp2)3238 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
3239 {
3240     return j2k_get_cstr_index(p_jp2->j2k);
3241 }
3242 
jp2_get_cstr_info(opj_jp2_t * p_jp2)3243 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
3244 {
3245     return j2k_get_cstr_info(p_jp2->j2k);
3246 }
3247 
opj_jp2_set_decoded_resolution_factor(opj_jp2_t * p_jp2,OPJ_UINT32 res_factor,opj_event_mgr_t * p_manager)3248 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
3249         OPJ_UINT32 res_factor,
3250         opj_event_mgr_t * p_manager)
3251 {
3252     return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
3253 }
3254 
3255 /* JPIP specific */
3256 
3257 #ifdef USE_JPIP
opj_jpip_write_iptr(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3258 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3259                                     opj_stream_private_t *cio,
3260                                     opj_event_mgr_t * p_manager)
3261 {
3262     OPJ_OFF_T j2k_codestream_exit;
3263     OPJ_BYTE l_data_header [24];
3264 
3265     /* preconditions */
3266     assert(jp2 != 00);
3267     assert(cio != 00);
3268     assert(p_manager != 00);
3269     assert(opj_stream_has_seek(cio));
3270 
3271     j2k_codestream_exit = opj_stream_tell(cio);
3272     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3273     opj_write_bytes(l_data_header + 4, JPIP_IPTR,
3274                     4);                                      /* IPTR */
3275 #if 0
3276     opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3277     opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3278 #else
3279     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3280     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3281 #endif
3282 
3283     if (! opj_stream_seek(cio, jp2->jpip_iptr_offset, p_manager)) {
3284         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3285         return OPJ_FALSE;
3286     }
3287 
3288     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3289         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3290         return OPJ_FALSE;
3291     }
3292 
3293     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3294         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3295         return OPJ_FALSE;
3296     }
3297 
3298     return OPJ_TRUE;
3299 }
3300 
opj_jpip_write_fidx(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3301 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3302                                     opj_stream_private_t *cio,
3303                                     opj_event_mgr_t * p_manager)
3304 {
3305     OPJ_OFF_T j2k_codestream_exit;
3306     OPJ_BYTE l_data_header [24];
3307 
3308     OPJ_UNUSED(jp2);
3309 
3310     /* preconditions */
3311     assert(jp2 != 00);
3312     assert(cio != 00);
3313     assert(p_manager != 00);
3314     assert(opj_stream_has_seek(cio));
3315 
3316     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3317     opj_write_bytes(l_data_header + 4, JPIP_FIDX,
3318                     4);                                      /* IPTR */
3319     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3320     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3321 
3322     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3323         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3324         return OPJ_FALSE;
3325     }
3326 
3327     j2k_codestream_exit = opj_stream_tell(cio);
3328     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3329         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3330         return OPJ_FALSE;
3331     }
3332 
3333     return OPJ_TRUE;
3334 }
3335 
opj_jpip_write_cidx(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3336 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3337                                     opj_stream_private_t *cio,
3338                                     opj_event_mgr_t * p_manager)
3339 {
3340     OPJ_OFF_T j2k_codestream_exit;
3341     OPJ_BYTE l_data_header [24];
3342 
3343     OPJ_UNUSED(jp2);
3344 
3345     /* preconditions */
3346     assert(jp2 != 00);
3347     assert(cio != 00);
3348     assert(p_manager != 00);
3349     assert(opj_stream_has_seek(cio));
3350 
3351     j2k_codestream_exit = opj_stream_tell(cio);
3352     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3353     opj_write_bytes(l_data_header + 4, JPIP_CIDX,
3354                     4);                                      /* IPTR */
3355 #if 0
3356     opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3357     opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3358 #else
3359     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3360     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3361 #endif
3362 
3363     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3364         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3365         return OPJ_FALSE;
3366     }
3367 
3368     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3369         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3370         return OPJ_FALSE;
3371     }
3372 
3373     j2k_codestream_exit = opj_stream_tell(cio);
3374     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3375         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3376         return OPJ_FALSE;
3377     }
3378 
3379     return OPJ_TRUE;
3380 }
3381 
3382 #if 0
3383 static void write_prxy(int offset_jp2c, int length_jp2c, int offset_idx,
3384                        int length_idx, opj_stream_private_t *cio,
3385                        opj_event_mgr_t * p_manager)
3386 {
3387     OPJ_BYTE l_data_header [8];
3388     OPJ_OFF_T len, lenp;
3389 
3390     lenp = opj_stream_tell(cio);
3391     opj_stream_skip(cio, 4, p_manager);         /* L [at the end] */
3392     opj_write_bytes(l_data_header, JPIP_PRXY, 4); /* IPTR           */
3393     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3394 
3395     opj_write_bytes(l_data_header, offset_jp2c, 8);  /* OOFF           */
3396     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3397     opj_write_bytes(l_data_header, length_jp2c, 4);  /* OBH part 1     */
3398     opj_write_bytes(l_data_header + 4, JP2_JP2C, 4); /* OBH part 2     */
3399     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3400 
3401     opj_write_bytes(l_data_header, 1, 1); /* NI             */
3402     opj_stream_write_data(cio, l_data_header, 1, p_manager);
3403 
3404     opj_write_bytes(l_data_header, offset_idx, 8);   /* IOFF           */
3405     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3406     opj_write_bytes(l_data_header, length_idx, 4);   /* IBH part 1     */
3407     opj_write_bytes(l_data_header + 4, JPIP_CIDX, 4);  /* IBH part 2     */
3408     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3409 
3410     len = opj_stream_tell(cio) - lenp;
3411     opj_stream_skip(cio, lenp, p_manager);
3412     opj_write_bytes(l_data_header, len, 4); /* L              */
3413     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3414     opj_stream_seek(cio, lenp + len, p_manager);
3415 }
3416 #endif
3417 
3418 
3419 #if 0
3420 static int write_fidx(int offset_jp2c, int length_jp2c, int offset_idx,
3421                       int length_idx, opj_stream_private_t *cio,
3422                       opj_event_mgr_t * p_manager)
3423 {
3424     OPJ_BYTE l_data_header [4];
3425     OPJ_OFF_T len, lenp;
3426 
3427     lenp = opj_stream_tell(cio);
3428     opj_stream_skip(cio, 4, p_manager);
3429     opj_write_bytes(l_data_header, JPIP_FIDX, 4); /* FIDX */
3430     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3431 
3432     write_prxy(offset_jp2c, length_jp2c, offset_idx, length_idx, cio, p_manager);
3433 
3434     len = opj_stream_tell(cio) - lenp;
3435     opj_stream_skip(cio, lenp, p_manager);
3436     opj_write_bytes(l_data_header, len, 4); /* L              */
3437     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3438     opj_stream_seek(cio, lenp + len, p_manager);
3439 
3440     return len;
3441 }
3442 #endif
3443 #endif /* USE_JPIP */
3444