• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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", i);
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(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
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             assert( cmp == 0 ); // probably wrong.
1129             for (j = 0; j < max; ++j) {
1130                 dst[j] = src[j];
1131             }
1132         } else {
1133             assert( i == pcol ); // probably wrong?
1134             for (j = 0; j < max; ++j) {
1135                 /* The index */
1136                 if ((k = src[j]) < 0) {
1137                     k = 0;
1138                 } else if (k > top_k) {
1139                     k = top_k;
1140                 }
1141 
1142                 /* The colour */
1143                 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
1144             }
1145         }
1146     }
1147 
1148     max = image->numcomps;
1149     for (i = 0; i < max; ++i) {
1150         if (old_comps[i].data) {
1151             opj_image_data_free(old_comps[i].data);
1152         }
1153     }
1154 
1155     opj_free(old_comps);
1156     image->comps = new_comps;
1157     image->numcomps = nr_channels;
1158 
1159     return OPJ_TRUE;
1160 }/* apply_pclr() */
1161 
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)1162 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
1163                                   OPJ_BYTE * p_pclr_header_data,
1164                                   OPJ_UINT32 p_pclr_header_size,
1165                                   opj_event_mgr_t * p_manager
1166                                  )
1167 {
1168     opj_jp2_pclr_t *jp2_pclr;
1169     OPJ_BYTE *channel_size, *channel_sign;
1170     OPJ_UINT32 *entries;
1171     OPJ_UINT16 nr_entries, nr_channels;
1172     OPJ_UINT16 i, j;
1173     OPJ_UINT32 l_value;
1174     OPJ_BYTE *orig_header_data = p_pclr_header_data;
1175 
1176     /* preconditions */
1177     assert(p_pclr_header_data != 00);
1178     assert(jp2 != 00);
1179     assert(p_manager != 00);
1180     (void)p_pclr_header_size;
1181 
1182     if (jp2->color.jp2_pclr) {
1183         return OPJ_FALSE;
1184     }
1185 
1186     if (p_pclr_header_size < 3) {
1187         return OPJ_FALSE;
1188     }
1189 
1190     opj_read_bytes(p_pclr_header_data, &l_value, 2);    /* NE */
1191     p_pclr_header_data += 2;
1192     nr_entries = (OPJ_UINT16) l_value;
1193     if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1194         opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n",
1195                       (int)nr_entries);
1196         return OPJ_FALSE;
1197     }
1198 
1199     opj_read_bytes(p_pclr_header_data, &l_value, 1);    /* NPC */
1200     ++p_pclr_header_data;
1201     nr_channels = (OPJ_UINT16) l_value;
1202     if (nr_channels == 0U) {
1203         opj_event_msg(p_manager, EVT_ERROR,
1204                       "Invalid PCLR box. Reports 0 palette columns\n");
1205         return OPJ_FALSE;
1206     }
1207 
1208     if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels) {
1209         return OPJ_FALSE;
1210     }
1211 
1212     entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * sizeof(
1213                                            OPJ_UINT32));
1214     if (!entries) {
1215         return OPJ_FALSE;
1216     }
1217     channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1218     if (!channel_size) {
1219         opj_free(entries);
1220         return OPJ_FALSE;
1221     }
1222     channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1223     if (!channel_sign) {
1224         opj_free(entries);
1225         opj_free(channel_size);
1226         return OPJ_FALSE;
1227     }
1228 
1229     jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1230     if (!jp2_pclr) {
1231         opj_free(entries);
1232         opj_free(channel_size);
1233         opj_free(channel_sign);
1234         return OPJ_FALSE;
1235     }
1236 
1237     jp2_pclr->channel_sign = channel_sign;
1238     jp2_pclr->channel_size = channel_size;
1239     jp2_pclr->entries = entries;
1240     jp2_pclr->nr_entries = nr_entries;
1241     jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
1242     jp2_pclr->cmap = NULL;
1243 
1244     jp2->color.jp2_pclr = jp2_pclr;
1245 
1246     for (i = 0; i < nr_channels; ++i) {
1247         opj_read_bytes(p_pclr_header_data, &l_value, 1);    /* Bi */
1248         ++p_pclr_header_data;
1249 
1250         channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1251         channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1252     }
1253 
1254     for (j = 0; j < nr_entries; ++j) {
1255         for (i = 0; i < nr_channels; ++i) {
1256             OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i] + 7) >> 3);
1257 
1258             if (bytes_to_read > sizeof(OPJ_UINT32)) {
1259                 bytes_to_read = sizeof(OPJ_UINT32);
1260             }
1261             if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data -
1262                     orig_header_data) + (ptrdiff_t)bytes_to_read) {
1263                 return OPJ_FALSE;
1264             }
1265 
1266             opj_read_bytes(p_pclr_header_data, &l_value, bytes_to_read);    /* Cji */
1267             p_pclr_header_data += bytes_to_read;
1268             *entries = (OPJ_UINT32) l_value;
1269             entries++;
1270         }
1271     }
1272 
1273     return OPJ_TRUE;
1274 }
1275 
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)1276 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
1277                                   OPJ_BYTE * p_cmap_header_data,
1278                                   OPJ_UINT32 p_cmap_header_size,
1279                                   opj_event_mgr_t * p_manager
1280                                  )
1281 {
1282     opj_jp2_cmap_comp_t *cmap;
1283     OPJ_BYTE i, nr_channels;
1284     OPJ_UINT32 l_value;
1285 
1286     /* preconditions */
1287     assert(jp2 != 00);
1288     assert(p_cmap_header_data != 00);
1289     assert(p_manager != 00);
1290     (void)p_cmap_header_size;
1291 
1292     /* Need nr_channels: */
1293     if (jp2->color.jp2_pclr == NULL) {
1294         opj_event_msg(p_manager, EVT_ERROR,
1295                       "Need to read a PCLR box before the CMAP box.\n");
1296         return OPJ_FALSE;
1297     }
1298 
1299     /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1300      * inside a JP2 Header box' :
1301     */
1302     if (jp2->color.jp2_pclr->cmap) {
1303         opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1304         return OPJ_FALSE;
1305     }
1306 
1307     nr_channels = jp2->color.jp2_pclr->nr_channels;
1308     if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1309         opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1310         return OPJ_FALSE;
1311     }
1312 
1313     cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(
1314                 opj_jp2_cmap_comp_t));
1315     if (!cmap) {
1316         return OPJ_FALSE;
1317     }
1318 
1319 
1320     for (i = 0; i < nr_channels; ++i) {
1321         opj_read_bytes_BE(p_cmap_header_data, &l_value, 2);     /* CMP^i */
1322         p_cmap_header_data += 2;
1323         cmap[i].cmp = (OPJ_UINT16) l_value;
1324 
1325         opj_read_bytes(p_cmap_header_data, &l_value, 1);            /* MTYP^i */
1326         ++p_cmap_header_data;
1327         cmap[i].mtyp = (OPJ_BYTE) l_value;
1328 
1329         opj_read_bytes(p_cmap_header_data, &l_value, 1);            /* PCOL^i */
1330         ++p_cmap_header_data;
1331         cmap[i].pcol = (OPJ_BYTE) l_value;
1332     }
1333 
1334     jp2->color.jp2_pclr->cmap = cmap;
1335 
1336     return OPJ_TRUE;
1337 }
1338 
opj_jp2_apply_cdef(opj_image_t * image,opj_jp2_color_t * color,opj_event_mgr_t * manager)1339 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
1340                                opj_event_mgr_t *manager)
1341 {
1342     opj_jp2_cdef_info_t *info;
1343     OPJ_UINT16 i, n, cn, asoc, acn;
1344 
1345     info = color->jp2_cdef->info;
1346     n = color->jp2_cdef->n;
1347 
1348     for (i = 0; i < n; ++i) {
1349         /* WATCH: acn = asoc - 1 ! */
1350         asoc = info[i].asoc;
1351         cn = info[i].cn;
1352 
1353         if (cn >= image->numcomps) {
1354             opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n",
1355                           cn, image->numcomps);
1356             continue;
1357         }
1358         if (asoc == 0 || asoc == 65535) {
1359             image->comps[cn].alpha = info[i].typ;
1360             continue;
1361         }
1362 
1363         acn = (OPJ_UINT16)(asoc - 1);
1364         if (acn >= image->numcomps) {
1365             opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n",
1366                           acn, image->numcomps);
1367             continue;
1368         }
1369 
1370         /* Swap only if color channel */
1371         if ((cn != acn) && (info[i].typ == 0)) {
1372             opj_image_comp_t saved;
1373             OPJ_UINT16 j;
1374 
1375             memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1376             memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1377             memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1378 
1379             /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1380             for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j) {
1381                 if (info[j].cn == cn) {
1382                     info[j].cn = acn;
1383                 } else if (info[j].cn == acn) {
1384                     info[j].cn = cn;
1385                 }
1386                 /* asoc is related to color index. Do not update. */
1387             }
1388         }
1389 
1390         image->comps[cn].alpha = info[i].typ;
1391     }
1392 
1393     if (color->jp2_cdef->info) {
1394         opj_free(color->jp2_cdef->info);
1395     }
1396 
1397     opj_free(color->jp2_cdef);
1398     color->jp2_cdef = NULL;
1399 
1400 }/* jp2_apply_cdef() */
1401 
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)1402 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
1403                                   OPJ_BYTE * p_cdef_header_data,
1404                                   OPJ_UINT32 p_cdef_header_size,
1405                                   opj_event_mgr_t * p_manager
1406                                  )
1407 {
1408     opj_jp2_cdef_info_t *cdef_info;
1409     OPJ_UINT16 i;
1410     OPJ_UINT32 l_value;
1411 
1412     /* preconditions */
1413     assert(jp2 != 00);
1414     assert(p_cdef_header_data != 00);
1415     assert(p_manager != 00);
1416     (void)p_cdef_header_size;
1417 
1418     /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1419      * inside a JP2 Header box.'*/
1420     if (jp2->color.jp2_cdef) {
1421         return OPJ_FALSE;
1422     }
1423 
1424     if (p_cdef_header_size < 2) {
1425         opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1426         return OPJ_FALSE;
1427     }
1428 
1429     opj_read_bytes(p_cdef_header_data, &l_value, 2);        /* N */
1430     p_cdef_header_data += 2;
1431 
1432     if ((OPJ_UINT16)l_value == 0) { /* szukw000: FIXME */
1433         opj_event_msg(p_manager, EVT_ERROR,
1434                       "Number of channel description is equal to zero in CDEF box.\n");
1435         return OPJ_FALSE;
1436     }
1437 
1438     if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1439         opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1440         return OPJ_FALSE;
1441     }
1442 
1443     cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(
1444                     opj_jp2_cdef_info_t));
1445     if (!cdef_info) {
1446         return OPJ_FALSE;
1447     }
1448 
1449     jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1450     if (!jp2->color.jp2_cdef) {
1451         opj_free(cdef_info);
1452         return OPJ_FALSE;
1453     }
1454     jp2->color.jp2_cdef->info = cdef_info;
1455     jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1456 
1457     for (i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1458         opj_read_bytes(p_cdef_header_data, &l_value, 2);            /* Cn^i */
1459         p_cdef_header_data += 2;
1460         cdef_info[i].cn = (OPJ_UINT16) l_value;
1461 
1462         opj_read_bytes(p_cdef_header_data, &l_value, 2);            /* Typ^i */
1463         p_cdef_header_data += 2;
1464         cdef_info[i].typ = (OPJ_UINT16) l_value;
1465 
1466         opj_read_bytes(p_cdef_header_data, &l_value, 2);            /* Asoc^i */
1467         p_cdef_header_data += 2;
1468         cdef_info[i].asoc = (OPJ_UINT16) l_value;
1469     }
1470 
1471     return OPJ_TRUE;
1472 }
1473 
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)1474 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
1475                                   OPJ_BYTE * p_colr_header_data,
1476                                   OPJ_UINT32 p_colr_header_size,
1477                                   opj_event_mgr_t * p_manager
1478                                  )
1479 {
1480     OPJ_UINT32 l_value;
1481 
1482     /* preconditions */
1483     assert(jp2 != 00);
1484     assert(p_colr_header_data != 00);
1485     assert(p_manager != 00);
1486 
1487     if (p_colr_header_size < 3) {
1488         opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1489         return OPJ_FALSE;
1490     }
1491 
1492     /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1493      * Specification boxes after the first.'
1494     */
1495     if (jp2->color.jp2_has_colr) {
1496         opj_event_msg(p_manager, EVT_INFO,
1497                       "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1498         p_colr_header_data += p_colr_header_size;
1499         return OPJ_TRUE;
1500     }
1501 
1502     opj_read_bytes(p_colr_header_data, &jp2->meth, 1);          /* METH */
1503     ++p_colr_header_data;
1504 
1505     opj_read_bytes(p_colr_header_data, &jp2->precedence, 1);    /* PRECEDENCE */
1506     ++p_colr_header_data;
1507 
1508     opj_read_bytes(p_colr_header_data, &jp2->approx, 1);        /* APPROX */
1509     ++p_colr_header_data;
1510 
1511     if (jp2->meth == 1) {
1512         if (p_colr_header_size < 7) {
1513             opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n",
1514                           p_colr_header_size);
1515             return OPJ_FALSE;
1516         }
1517         if ((p_colr_header_size > 7) &&
1518                 (jp2->enumcs != 14)) { /* handled below for CIELab) */
1519             /* testcase Altona_Technical_v20_x4.pdf */
1520             opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n",
1521                           p_colr_header_size);
1522         }
1523 
1524         opj_read_bytes(p_colr_header_data, &jp2->enumcs, 4);        /* EnumCS */
1525 
1526         p_colr_header_data += 4;
1527 
1528         if (jp2->enumcs == 14) { /* CIELab */
1529             OPJ_UINT32 *cielab;
1530             OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
1531 
1532             cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
1533             if (cielab == NULL) {
1534                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
1535                 return OPJ_FALSE;
1536             }
1537             cielab[0] = 14; /* enumcs */
1538 
1539             /* default values */
1540             rl = ra = rb = ol = oa = ob = 0;
1541             il = 0x00443530; /* D50 */
1542             cielab[1] = 0x44454600;/* DEF */
1543 
1544             if (p_colr_header_size == 35) {
1545                 opj_read_bytes(p_colr_header_data, &rl, 4);
1546                 p_colr_header_data += 4;
1547                 opj_read_bytes(p_colr_header_data, &ol, 4);
1548                 p_colr_header_data += 4;
1549                 opj_read_bytes(p_colr_header_data, &ra, 4);
1550                 p_colr_header_data += 4;
1551                 opj_read_bytes(p_colr_header_data, &oa, 4);
1552                 p_colr_header_data += 4;
1553                 opj_read_bytes(p_colr_header_data, &rb, 4);
1554                 p_colr_header_data += 4;
1555                 opj_read_bytes(p_colr_header_data, &ob, 4);
1556                 p_colr_header_data += 4;
1557                 opj_read_bytes(p_colr_header_data, &il, 4);
1558                 p_colr_header_data += 4;
1559 
1560                 cielab[1] = 0;
1561             } else if (p_colr_header_size != 7) {
1562                 opj_event_msg(p_manager, EVT_WARNING,
1563                               "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size);
1564             }
1565             cielab[2] = rl;
1566             cielab[4] = ra;
1567             cielab[6] = rb;
1568             cielab[3] = ol;
1569             cielab[5] = oa;
1570             cielab[7] = ob;
1571             cielab[8] = il;
1572 
1573             jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
1574             jp2->color.icc_profile_len = 0;
1575         }
1576         jp2->color.jp2_has_colr = 1;
1577     } else if (jp2->meth == 2) {
1578         /* ICC profile */
1579         OPJ_INT32 it_icc_value = 0;
1580         OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1581 
1582         jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1583         jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1, (size_t)icc_len);
1584         if (!jp2->color.icc_profile_buf) {
1585             jp2->color.icc_profile_len = 0;
1586             return OPJ_FALSE;
1587         }
1588 
1589         for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) {
1590             opj_read_bytes(p_colr_header_data, &l_value, 1);    /* icc values */
1591             ++p_colr_header_data;
1592             jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1593         }
1594 
1595         jp2->color.jp2_has_colr = 1;
1596     } else if (jp2->meth > 2) {
1597         /*  ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
1598         conforming JP2 reader shall ignore the entire Colour Specification box.*/
1599         opj_event_msg(p_manager, EVT_INFO,
1600                       "COLR BOX meth value is not a regular value (%d), "
1601                       "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1602     }
1603     if (jp2->color.jp2_has_colr) {
1604         jp2->j2k->enumcs = jp2->enumcs;
1605     }
1606     return OPJ_TRUE;
1607 }
1608 
opj_jp2_decode(opj_jp2_t * jp2,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)1609 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1610                         opj_stream_private_t *p_stream,
1611                         opj_image_t* p_image,
1612                         opj_event_mgr_t * p_manager)
1613 {
1614     if (!p_image) {
1615         return OPJ_FALSE;
1616     }
1617 
1618     /* J2K decoding */
1619     if (! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager)) {
1620         opj_event_msg(p_manager, EVT_ERROR,
1621                       "Failed to decode the codestream in the JP2 file\n");
1622         return OPJ_FALSE;
1623     }
1624 
1625     if (jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
1626         /* Bypass all JP2 component transforms */
1627         return OPJ_TRUE;
1628     }
1629 
1630     if (!jp2->ignore_pclr_cmap_cdef) {
1631         if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1632             return OPJ_FALSE;
1633         }
1634 
1635         /* Set Image Color Space */
1636         if (jp2->enumcs == 16) {
1637             p_image->color_space = OPJ_CLRSPC_SRGB;
1638         } else if (jp2->enumcs == 17) {
1639             p_image->color_space = OPJ_CLRSPC_GRAY;
1640         } else if (jp2->enumcs == 18) {
1641             p_image->color_space = OPJ_CLRSPC_SYCC;
1642         } else if (jp2->enumcs == 24) {
1643             p_image->color_space = OPJ_CLRSPC_EYCC;
1644         } else if (jp2->enumcs == 12) {
1645             p_image->color_space = OPJ_CLRSPC_CMYK;
1646         } else {
1647             p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1648         }
1649 
1650         if (jp2->color.jp2_pclr) {
1651             /* Part 1, I.5.3.4: Either both or none : */
1652             if (!jp2->color.jp2_pclr->cmap) {
1653                 opj_jp2_free_pclr(&(jp2->color));
1654             } else if (p_image->pdfium_use_colorspace) {
1655                 if (!opj_jp2_apply_pclr(p_image, &(jp2->color), p_manager)) {
1656                     return OPJ_FALSE;
1657                 }
1658             }
1659         }
1660 
1661         /* Apply the color space if needed */
1662         if (jp2->color.jp2_cdef) {
1663             opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1664         }
1665 
1666         if (jp2->color.icc_profile_buf) {
1667             p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1668             p_image->icc_profile_len = jp2->color.icc_profile_len;
1669             jp2->color.icc_profile_buf = NULL;
1670         }
1671     }
1672 
1673     return OPJ_TRUE;
1674 }
1675 
opj_jp2_write_jp2h(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)1676 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1677                                    opj_stream_private_t *stream,
1678                                    opj_event_mgr_t * p_manager
1679                                   )
1680 {
1681     opj_jp2_img_header_writer_handler_t l_writers [4];
1682     opj_jp2_img_header_writer_handler_t * l_current_writer;
1683 
1684     OPJ_INT32 i, l_nb_pass;
1685     /* size of data for super box*/
1686     OPJ_UINT32 l_jp2h_size = 8;
1687     OPJ_BOOL l_result = OPJ_TRUE;
1688 
1689     /* to store the data of the super box */
1690     OPJ_BYTE l_jp2h_data [8];
1691 
1692     /* preconditions */
1693     assert(stream != 00);
1694     assert(jp2 != 00);
1695     assert(p_manager != 00);
1696 
1697     memset(l_writers, 0, sizeof(l_writers));
1698 
1699     if (jp2->bpc == 255) {
1700         l_nb_pass = 3;
1701         l_writers[0].handler = opj_jp2_write_ihdr;
1702         l_writers[1].handler = opj_jp2_write_bpcc;
1703         l_writers[2].handler = opj_jp2_write_colr;
1704     } else {
1705         l_nb_pass = 2;
1706         l_writers[0].handler = opj_jp2_write_ihdr;
1707         l_writers[1].handler = opj_jp2_write_colr;
1708     }
1709 
1710     if (jp2->color.jp2_cdef != NULL) {
1711         l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1712         l_nb_pass++;
1713     }
1714 
1715     /* write box header */
1716     /* write JP2H type */
1717     opj_write_bytes(l_jp2h_data + 4, JP2_JP2H, 4);
1718 
1719     l_current_writer = l_writers;
1720     for (i = 0; i < l_nb_pass; ++i) {
1721         l_current_writer->m_data = l_current_writer->handler(jp2,
1722                                    &(l_current_writer->m_size));
1723         if (l_current_writer->m_data == 00) {
1724             opj_event_msg(p_manager, EVT_ERROR,
1725                           "Not enough memory to hold JP2 Header data\n");
1726             l_result = OPJ_FALSE;
1727             break;
1728         }
1729 
1730         l_jp2h_size += l_current_writer->m_size;
1731         ++l_current_writer;
1732     }
1733 
1734     if (! l_result) {
1735         l_current_writer = l_writers;
1736         for (i = 0; i < l_nb_pass; ++i) {
1737             if (l_current_writer->m_data != 00) {
1738                 opj_free(l_current_writer->m_data);
1739             }
1740             ++l_current_writer;
1741         }
1742 
1743         return OPJ_FALSE;
1744     }
1745 
1746     /* write super box size */
1747     opj_write_bytes(l_jp2h_data, l_jp2h_size, 4);
1748 
1749     /* write super box data on stream */
1750     if (opj_stream_write_data(stream, l_jp2h_data, 8, p_manager) != 8) {
1751         opj_event_msg(p_manager, EVT_ERROR,
1752                       "Stream error while writing JP2 Header box\n");
1753         l_result = OPJ_FALSE;
1754     }
1755 
1756     if (l_result) {
1757         l_current_writer = l_writers;
1758         for (i = 0; i < l_nb_pass; ++i) {
1759             if (opj_stream_write_data(stream, l_current_writer->m_data,
1760                                       l_current_writer->m_size, p_manager) != l_current_writer->m_size) {
1761                 opj_event_msg(p_manager, EVT_ERROR,
1762                               "Stream error while writing JP2 Header box\n");
1763                 l_result = OPJ_FALSE;
1764                 break;
1765             }
1766             ++l_current_writer;
1767         }
1768     }
1769 
1770     l_current_writer = l_writers;
1771 
1772     /* cleanup */
1773     for (i = 0; i < l_nb_pass; ++i) {
1774         if (l_current_writer->m_data != 00) {
1775             opj_free(l_current_writer->m_data);
1776         }
1777         ++l_current_writer;
1778     }
1779 
1780     return l_result;
1781 }
1782 
opj_jp2_write_ftyp(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1783 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1784                                    opj_stream_private_t *cio,
1785                                    opj_event_mgr_t * p_manager)
1786 {
1787     OPJ_UINT32 i;
1788     OPJ_UINT32 l_ftyp_size;
1789     OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1790     OPJ_BOOL l_result;
1791 
1792     /* preconditions */
1793     assert(cio != 00);
1794     assert(jp2 != 00);
1795     assert(p_manager != 00);
1796     l_ftyp_size = 16 + 4 * jp2->numcl;
1797 
1798     l_ftyp_data = (OPJ_BYTE *) opj_calloc(1, l_ftyp_size);
1799 
1800     if (l_ftyp_data == 00) {
1801         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1802         return OPJ_FALSE;
1803     }
1804 
1805     l_current_data_ptr = l_ftyp_data;
1806 
1807     opj_write_bytes(l_current_data_ptr, l_ftyp_size, 4); /* box size */
1808     l_current_data_ptr += 4;
1809 
1810     opj_write_bytes(l_current_data_ptr, JP2_FTYP, 4); /* FTYP */
1811     l_current_data_ptr += 4;
1812 
1813     opj_write_bytes(l_current_data_ptr, jp2->brand, 4); /* BR */
1814     l_current_data_ptr += 4;
1815 
1816     opj_write_bytes(l_current_data_ptr, jp2->minversion, 4); /* MinV */
1817     l_current_data_ptr += 4;
1818 
1819     for (i = 0; i < jp2->numcl; i++)  {
1820         opj_write_bytes(l_current_data_ptr, jp2->cl[i], 4); /* CL */
1821     }
1822 
1823     l_result = (opj_stream_write_data(cio, l_ftyp_data, l_ftyp_size,
1824                                       p_manager) == l_ftyp_size);
1825     if (! l_result) {
1826         opj_event_msg(p_manager, EVT_ERROR,
1827                       "Error while writing ftyp data to stream\n");
1828     }
1829 
1830     opj_free(l_ftyp_data);
1831 
1832     return l_result;
1833 }
1834 
opj_jp2_write_jp2c(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1835 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1836                                    opj_stream_private_t *cio,
1837                                    opj_event_mgr_t * p_manager)
1838 {
1839     OPJ_OFF_T j2k_codestream_exit;
1840     OPJ_BYTE l_data_header [8];
1841 
1842     /* preconditions */
1843     assert(jp2 != 00);
1844     assert(cio != 00);
1845     assert(p_manager != 00);
1846     assert(opj_stream_has_seek(cio));
1847 
1848     j2k_codestream_exit = opj_stream_tell(cio);
1849     opj_write_bytes(l_data_header,
1850                     (OPJ_UINT32)(j2k_codestream_exit - jp2->j2k_codestream_offset),
1851                     4); /* size of codestream */
1852     opj_write_bytes(l_data_header + 4, JP2_JP2C,
1853                     4);                                     /* JP2C */
1854 
1855     if (! opj_stream_seek(cio, jp2->j2k_codestream_offset, p_manager)) {
1856         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1857         return OPJ_FALSE;
1858     }
1859 
1860     if (opj_stream_write_data(cio, l_data_header, 8, p_manager) != 8) {
1861         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1862         return OPJ_FALSE;
1863     }
1864 
1865     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
1866         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1867         return OPJ_FALSE;
1868     }
1869 
1870     return OPJ_TRUE;
1871 }
1872 
opj_jp2_write_jp(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1873 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
1874                                  opj_stream_private_t *cio,
1875                                  opj_event_mgr_t * p_manager)
1876 {
1877     /* 12 bytes will be read */
1878     OPJ_BYTE l_signature_data [12];
1879 
1880     /* preconditions */
1881     assert(cio != 00);
1882     assert(jp2 != 00);
1883     assert(p_manager != 00);
1884 
1885     OPJ_UNUSED(jp2);
1886 
1887     /* write box length */
1888     opj_write_bytes(l_signature_data, 12, 4);
1889     /* writes box type */
1890     opj_write_bytes(l_signature_data + 4, JP2_JP, 4);
1891     /* writes magic number*/
1892     opj_write_bytes(l_signature_data + 8, 0x0d0a870a, 4);
1893 
1894     if (opj_stream_write_data(cio, l_signature_data, 12, p_manager) != 12) {
1895         return OPJ_FALSE;
1896     }
1897 
1898     return OPJ_TRUE;
1899 }
1900 
1901 /* ----------------------------------------------------------------------- */
1902 /* JP2 decoder interface                                             */
1903 /* ----------------------------------------------------------------------- */
1904 
opj_jp2_setup_decoder(opj_jp2_t * jp2,opj_dparameters_t * parameters)1905 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1906 {
1907     /* setup the J2K codec */
1908     opj_j2k_setup_decoder(jp2->j2k, parameters);
1909 
1910     /* further JP2 initializations go here */
1911     jp2->color.jp2_has_colr = 0;
1912     jp2->comps = NULL;
1913     jp2->ignore_pclr_cmap_cdef = parameters->flags &
1914                                  OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1915 }
1916 
opj_jp2_set_threads(opj_jp2_t * jp2,OPJ_UINT32 num_threads)1917 OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads)
1918 {
1919     return opj_j2k_set_threads(jp2->j2k, num_threads);
1920 }
1921 
1922 /* ----------------------------------------------------------------------- */
1923 /* JP2 encoder interface                                             */
1924 /* ----------------------------------------------------------------------- */
1925 
opj_jp2_setup_encoder(opj_jp2_t * jp2,opj_cparameters_t * parameters,opj_image_t * image,opj_event_mgr_t * p_manager)1926 OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2,
1927                                opj_cparameters_t *parameters,
1928                                opj_image_t *image,
1929                                opj_event_mgr_t * p_manager)
1930 {
1931     OPJ_UINT32 i;
1932     OPJ_UINT32 depth_0;
1933     OPJ_UINT32 sign;
1934     OPJ_UINT32 alpha_count;
1935     OPJ_UINT32 color_channels = 0U;
1936     OPJ_UINT32 alpha_channel = 0U;
1937 
1938 
1939     if (!jp2 || !parameters || !image) {
1940         return OPJ_FALSE;
1941     }
1942 
1943     /* setup the J2K codec */
1944     /* ------------------- */
1945 
1946     /* Check if number of components respects standard */
1947     if (image->numcomps < 1 || image->numcomps > 16384) {
1948         opj_event_msg(p_manager, EVT_ERROR,
1949                       "Invalid number of components specified while setting up JP2 encoder\n");
1950         return OPJ_FALSE;
1951     }
1952 
1953     if (opj_j2k_setup_encoder(jp2->j2k, parameters, image,
1954                               p_manager) == OPJ_FALSE) {
1955         return OPJ_FALSE;
1956     }
1957 
1958     /* setup the JP2 codec */
1959     /* ------------------- */
1960 
1961     /* Profile box */
1962 
1963     jp2->brand = JP2_JP2;   /* BR */
1964     jp2->minversion = 0;    /* MinV */
1965     jp2->numcl = 1;
1966     jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1967     if (!jp2->cl) {
1968         opj_event_msg(p_manager, EVT_ERROR,
1969                       "Not enough memory when setup the JP2 encoder\n");
1970         return OPJ_FALSE;
1971     }
1972     jp2->cl[0] = JP2_JP2;   /* CL0 : JP2 */
1973 
1974     /* Image Header box */
1975 
1976     jp2->numcomps = image->numcomps;    /* NC */
1977     jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(
1978                      opj_jp2_comps_t));
1979     if (!jp2->comps) {
1980         opj_event_msg(p_manager, EVT_ERROR,
1981                       "Not enough memory when setup the JP2 encoder\n");
1982         /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1983         return OPJ_FALSE;
1984     }
1985 
1986     jp2->h = image->y1 - image->y0;     /* HEIGHT */
1987     jp2->w = image->x1 - image->x0;     /* WIDTH */
1988     /* BPC */
1989     depth_0 = image->comps[0].prec - 1;
1990     sign = image->comps[0].sgnd;
1991     jp2->bpc = depth_0 + (sign << 7);
1992     for (i = 1; i < image->numcomps; i++) {
1993         OPJ_UINT32 depth = image->comps[i].prec - 1;
1994         sign = image->comps[i].sgnd;
1995         if (depth_0 != depth) {
1996             jp2->bpc = 255;
1997         }
1998     }
1999     jp2->C = 7;         /* C : Always 7 */
2000     jp2->UnkC = 0;      /* UnkC, colorspace specified in colr box */
2001     jp2->IPR = 0;       /* IPR, no intellectual property */
2002 
2003     /* BitsPerComponent box */
2004     for (i = 0; i < image->numcomps; i++) {
2005         jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2006     }
2007 
2008     /* Colour Specification box */
2009     if (image->icc_profile_len) {
2010         jp2->meth = 2;
2011         jp2->enumcs = 0;
2012     } else {
2013         jp2->meth = 1;
2014         if (image->color_space == 1) {
2015             jp2->enumcs = 16;    /* sRGB as defined by IEC 61966-2-1 */
2016         } else if (image->color_space == 2) {
2017             jp2->enumcs = 17;    /* greyscale */
2018         } else if (image->color_space == 3) {
2019             jp2->enumcs = 18;    /* YUV */
2020         }
2021     }
2022 
2023     /* Channel Definition box */
2024     /* FIXME not provided by parameters */
2025     /* We try to do what we can... */
2026     alpha_count = 0U;
2027     for (i = 0; i < image->numcomps; i++) {
2028         if (image->comps[i].alpha != 0) {
2029             alpha_count++;
2030             alpha_channel = i;
2031         }
2032     }
2033     if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
2034         switch (jp2->enumcs) {
2035         case 16:
2036         case 18:
2037             color_channels = 3;
2038             break;
2039         case 17:
2040             color_channels = 1;
2041             break;
2042         default:
2043             alpha_count = 0U;
2044             break;
2045         }
2046         if (alpha_count == 0U) {
2047             opj_event_msg(p_manager, EVT_WARNING,
2048                           "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
2049         } else if (image->numcomps < (color_channels + 1)) {
2050             opj_event_msg(p_manager, EVT_WARNING,
2051                           "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
2052             alpha_count = 0U;
2053         } else if ((OPJ_UINT32)alpha_channel < color_channels) {
2054             opj_event_msg(p_manager, EVT_WARNING,
2055                           "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
2056             alpha_count = 0U;
2057         }
2058     } else if (alpha_count > 1) {
2059         opj_event_msg(p_manager, EVT_WARNING,
2060                       "Multiple alpha channels specified. No cdef box will be created.\n");
2061     }
2062     if (alpha_count == 1U) { /* if here, we know what we can do */
2063         jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
2064         if (!jp2->color.jp2_cdef) {
2065             opj_event_msg(p_manager, EVT_ERROR,
2066                           "Not enough memory to setup the JP2 encoder\n");
2067             return OPJ_FALSE;
2068         }
2069         /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
2070         /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
2071         jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(
2072                                         image->numcomps * sizeof(opj_jp2_cdef_info_t));
2073         if (!jp2->color.jp2_cdef->info) {
2074             /* memory will be freed by opj_jp2_destroy */
2075             opj_event_msg(p_manager, EVT_ERROR,
2076                           "Not enough memory to setup the JP2 encoder\n");
2077             return OPJ_FALSE;
2078         }
2079         jp2->color.jp2_cdef->n = (OPJ_UINT16)
2080                                  image->numcomps; /* cast is valid : image->numcomps [1,16384] */
2081         for (i = 0U; i < color_channels; i++) {
2082             jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2083                                               i; /* cast is valid : image->numcomps [1,16384] */
2084             jp2->color.jp2_cdef->info[i].typ = 0U;
2085             jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i +
2086                                                 1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
2087         }
2088         for (; i < image->numcomps; i++) {
2089             if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
2090                 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2091                                                   i; /* cast is valid : image->numcomps [1,16384] */
2092                 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
2093                 jp2->color.jp2_cdef->info[i].asoc =
2094                     0U; /* Apply alpha channel to the whole image */
2095             } else {
2096                 /* Unknown channel */
2097                 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2098                                                   i; /* cast is valid : image->numcomps [1,16384] */
2099                 jp2->color.jp2_cdef->info[i].typ = 65535U;
2100                 jp2->color.jp2_cdef->info[i].asoc = 65535U;
2101             }
2102         }
2103     }
2104 
2105     jp2->precedence = 0;    /* PRECEDENCE */
2106     jp2->approx = 0;        /* APPROX */
2107 
2108     jp2->jpip_on = parameters->jpip_on;
2109 
2110     return OPJ_TRUE;
2111 }
2112 
opj_jp2_encode(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2113 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
2114                         opj_stream_private_t *stream,
2115                         opj_event_mgr_t * p_manager)
2116 {
2117     return opj_j2k_encode(jp2->j2k, stream, p_manager);
2118 }
2119 
opj_jp2_end_decompress(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2120 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
2121                                 opj_stream_private_t *cio,
2122                                 opj_event_mgr_t * p_manager
2123                                )
2124 {
2125     /* preconditions */
2126     assert(jp2 != 00);
2127     assert(cio != 00);
2128     assert(p_manager != 00);
2129 
2130     /* customization of the end encoding */
2131     if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
2132         return OPJ_FALSE;
2133     }
2134 
2135     /* write header */
2136     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager)) {
2137         return OPJ_FALSE;
2138     }
2139 
2140     return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
2141 }
2142 
opj_jp2_end_compress(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2143 OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2,
2144                               opj_stream_private_t *cio,
2145                               opj_event_mgr_t * p_manager
2146                              )
2147 {
2148     /* preconditions */
2149     assert(jp2 != 00);
2150     assert(cio != 00);
2151     assert(p_manager != 00);
2152 
2153     /* customization of the end encoding */
2154     if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
2155         return OPJ_FALSE;
2156     }
2157 
2158     if (! opj_j2k_end_compress(jp2->j2k, cio, p_manager)) {
2159         return OPJ_FALSE;
2160     }
2161 
2162     /* write header */
2163     return opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager);
2164 }
2165 
opj_jp2_setup_end_header_writing(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2166 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
2167         opj_event_mgr_t * p_manager)
2168 {
2169     /* preconditions */
2170     assert(jp2 != 00);
2171     assert(p_manager != 00);
2172 
2173 #ifdef USE_JPIP
2174     if (jp2->jpip_on) {
2175         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2176                                                (opj_procedure)opj_jpip_write_iptr, p_manager)) {
2177             return OPJ_FALSE;
2178         }
2179     }
2180 #endif
2181     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2182                                            (opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2183         return OPJ_FALSE;
2184     }
2185     /* DEVELOPER CORNER, add your custom procedures */
2186 #ifdef USE_JPIP
2187     if (jp2->jpip_on) {
2188         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2189                                                (opj_procedure)opj_jpip_write_cidx, p_manager)) {
2190             return OPJ_FALSE;
2191         }
2192         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2193                                                (opj_procedure)opj_jpip_write_fidx, p_manager)) {
2194             return OPJ_FALSE;
2195         }
2196     }
2197 #endif
2198     return OPJ_TRUE;
2199 }
2200 
opj_jp2_setup_end_header_reading(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2201 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
2202         opj_event_mgr_t * p_manager)
2203 {
2204     /* preconditions */
2205     assert(jp2 != 00);
2206     assert(p_manager != 00);
2207 
2208     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2209                                            (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2210         return OPJ_FALSE;
2211     }
2212     /* DEVELOPER CORNER, add your custom procedures */
2213 
2214     return OPJ_TRUE;
2215 }
2216 
opj_jp2_default_validation(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2217 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
2218         opj_stream_private_t *cio,
2219         opj_event_mgr_t * p_manager
2220                                           )
2221 {
2222     OPJ_BOOL l_is_valid = OPJ_TRUE;
2223     OPJ_UINT32 i;
2224 
2225     /* preconditions */
2226     assert(jp2 != 00);
2227     assert(cio != 00);
2228     assert(p_manager != 00);
2229 
2230     OPJ_UNUSED(p_manager);
2231 
2232     /* JPEG2000 codec validation */
2233 
2234     /* STATE checking */
2235     /* make sure the state is at 0 */
2236     l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2237 
2238     /* make sure not reading a jp2h ???? WEIRD */
2239     l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2240 
2241     /* POINTER validation */
2242     /* make sure a j2k codec is present */
2243     l_is_valid &= (jp2->j2k != 00);
2244 
2245     /* make sure a procedure list is present */
2246     l_is_valid &= (jp2->m_procedure_list != 00);
2247 
2248     /* make sure a validation list is present */
2249     l_is_valid &= (jp2->m_validation_list != 00);
2250 
2251     /* PARAMETER VALIDATION */
2252     /* number of components */
2253     l_is_valid &= (jp2->numcl > 0);
2254     /* width */
2255     l_is_valid &= (jp2->h > 0);
2256     /* height */
2257     l_is_valid &= (jp2->w > 0);
2258     /* precision */
2259     for (i = 0; i < jp2->numcomps; ++i) {
2260         l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) <
2261                        38U); /* 0 is valid, ignore sign for check */
2262     }
2263 
2264     /* METH */
2265     l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2266 
2267     /* stream validation */
2268     /* back and forth is needed */
2269     l_is_valid &= opj_stream_has_seek(cio);
2270 
2271     return l_is_valid;
2272 }
2273 
opj_jp2_read_header_procedure(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2274 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
2275         opj_stream_private_t *stream,
2276         opj_event_mgr_t * p_manager
2277                                              )
2278 {
2279     opj_jp2_box_t box;
2280     OPJ_UINT32 l_nb_bytes_read;
2281     const opj_jp2_header_handler_t * l_current_handler;
2282     const opj_jp2_header_handler_t * l_current_handler_misplaced;
2283     OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
2284     OPJ_UINT32 l_current_data_size;
2285     OPJ_BYTE * l_current_data = 00;
2286 
2287     /* preconditions */
2288     assert(stream != 00);
2289     assert(jp2 != 00);
2290     assert(p_manager != 00);
2291 
2292     l_current_data = (OPJ_BYTE*)opj_calloc(1, l_last_data_size);
2293 
2294     if (l_current_data == 00) {
2295         opj_event_msg(p_manager, EVT_ERROR,
2296                       "Not enough memory to handle jpeg2000 file header\n");
2297         return OPJ_FALSE;
2298     }
2299 
2300     while (opj_jp2_read_boxhdr(&box, &l_nb_bytes_read, stream, p_manager)) {
2301         /* is it the codestream box ? */
2302         if (box.type == JP2_JP2C) {
2303             if (jp2->jp2_state & JP2_STATE_HEADER) {
2304                 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2305                 opj_free(l_current_data);
2306                 return OPJ_TRUE;
2307             } else {
2308                 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2309                 opj_free(l_current_data);
2310                 return OPJ_FALSE;
2311             }
2312         } else if (box.length == 0) {
2313             opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2314             opj_free(l_current_data);
2315             return OPJ_FALSE;
2316         }
2317         /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2318         else if (box.length < l_nb_bytes_read) {
2319             opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length,
2320                           box.type);
2321             opj_free(l_current_data);
2322             return OPJ_FALSE;
2323         }
2324 
2325         l_current_handler = opj_jp2_find_handler(box.type);
2326         l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
2327         l_current_data_size = box.length - l_nb_bytes_read;
2328 
2329         if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
2330             if (l_current_handler == 00) {
2331                 opj_event_msg(p_manager, EVT_WARNING,
2332                               "Found a misplaced '%c%c%c%c' box outside jp2h box\n",
2333                               (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2334                               (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2335                 if (jp2->jp2_state & JP2_STATE_HEADER) {
2336                     /* read anyway, we already have jp2h */
2337                     l_current_handler = l_current_handler_misplaced;
2338                 } else {
2339                     opj_event_msg(p_manager, EVT_WARNING,
2340                                   "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n",
2341                                   (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2342                                   (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2343                     jp2->jp2_state |= JP2_STATE_UNKNOWN;
2344                     if (opj_stream_skip(stream, l_current_data_size,
2345                                         p_manager) != l_current_data_size) {
2346                         opj_event_msg(p_manager, EVT_ERROR,
2347                                       "Problem with skipping JPEG2000 box, stream error\n");
2348                         opj_free(l_current_data);
2349                         return OPJ_FALSE;
2350                     }
2351                     continue;
2352                 }
2353             }
2354             if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2355                 /* do not even try to malloc if we can't read */
2356                 opj_event_msg(p_manager, EVT_ERROR,
2357                               "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n",
2358                               box.length, (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2359                               (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0), l_current_data_size,
2360                               (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
2361                 opj_free(l_current_data);
2362                 return OPJ_FALSE;
2363             }
2364             if (l_current_data_size > l_last_data_size) {
2365                 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,
2366                                              l_current_data_size);
2367                 if (!new_current_data) {
2368                     opj_free(l_current_data);
2369                     opj_event_msg(p_manager, EVT_ERROR,
2370                                   "Not enough memory to handle jpeg2000 box\n");
2371                     return OPJ_FALSE;
2372                 }
2373                 l_current_data = new_current_data;
2374                 l_last_data_size = l_current_data_size;
2375             }
2376 
2377             l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream, l_current_data,
2378                               l_current_data_size, p_manager);
2379             if (l_nb_bytes_read != l_current_data_size) {
2380                 opj_event_msg(p_manager, EVT_ERROR,
2381                               "Problem with reading JPEG2000 box, stream error\n");
2382                 opj_free(l_current_data);
2383                 return OPJ_FALSE;
2384             }
2385 
2386             if (! l_current_handler->handler(jp2, l_current_data, l_current_data_size,
2387                                              p_manager)) {
2388                 opj_free(l_current_data);
2389                 return OPJ_FALSE;
2390             }
2391         } else {
2392             if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2393                 opj_event_msg(p_manager, EVT_ERROR,
2394                               "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2395                 opj_free(l_current_data);
2396                 return OPJ_FALSE;
2397             }
2398             if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2399                 opj_event_msg(p_manager, EVT_ERROR,
2400                               "Malformed JP2 file format: second box must be file type box\n");
2401                 opj_free(l_current_data);
2402                 return OPJ_FALSE;
2403             }
2404             jp2->jp2_state |= JP2_STATE_UNKNOWN;
2405             if (opj_stream_skip(stream, l_current_data_size,
2406                                 p_manager) != l_current_data_size) {
2407                 if (jp2->jp2_state & JP2_STATE_CODESTREAM) {
2408                     /* If we already read the codestream, do not error out */
2409                     /* Needed for data/input/nonregression/issue254.jp2 */
2410                     opj_event_msg(p_manager, EVT_WARNING,
2411                                   "Problem with skipping JPEG2000 box, stream error\n");
2412                     opj_free(l_current_data);
2413                     return OPJ_TRUE;
2414                 } else {
2415                     opj_event_msg(p_manager, EVT_ERROR,
2416                                   "Problem with skipping JPEG2000 box, stream error\n");
2417                     opj_free(l_current_data);
2418                     return OPJ_FALSE;
2419                 }
2420             }
2421         }
2422     }
2423 
2424     opj_free(l_current_data);
2425 
2426     return OPJ_TRUE;
2427 }
2428 
2429 /**
2430  * Executes the given procedures on the given codec.
2431  *
2432  * @param   p_procedure_list    the list of procedures to execute
2433  * @param   jp2                 the jpeg2000 file codec to execute the procedures on.
2434  * @param   stream                  the stream to execute the procedures on.
2435  * @param   p_manager           the user manager.
2436  *
2437  * @return  true                if all the procedures were successfully executed.
2438  */
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)2439 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
2440                              opj_procedure_list_t * p_procedure_list,
2441                              opj_stream_private_t *stream,
2442                              opj_event_mgr_t * p_manager
2443                             )
2444 
2445 {
2446     OPJ_BOOL(** l_procedure)(opj_jp2_t * jp2, opj_stream_private_t *,
2447                              opj_event_mgr_t *) = 00;
2448     OPJ_BOOL l_result = OPJ_TRUE;
2449     OPJ_UINT32 l_nb_proc, i;
2450 
2451     /* preconditions */
2452     assert(p_procedure_list != 00);
2453     assert(jp2 != 00);
2454     assert(stream != 00);
2455     assert(p_manager != 00);
2456 
2457     l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2458     l_procedure = (OPJ_BOOL(**)(opj_jp2_t * jp2, opj_stream_private_t *,
2459                                 opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2460 
2461     for (i = 0; i < l_nb_proc; ++i) {
2462         l_result = l_result && (*l_procedure)(jp2, stream, p_manager);
2463         ++l_procedure;
2464     }
2465 
2466     /* and clear the procedure list at the end. */
2467     opj_procedure_list_clear(p_procedure_list);
2468     return l_result;
2469 }
2470 
opj_jp2_start_compress(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)2471 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2472                                 opj_stream_private_t *stream,
2473                                 opj_image_t * p_image,
2474                                 opj_event_mgr_t * p_manager
2475                                )
2476 {
2477     /* preconditions */
2478     assert(jp2 != 00);
2479     assert(stream != 00);
2480     assert(p_manager != 00);
2481 
2482     /* customization of the validation */
2483     if (! opj_jp2_setup_encoding_validation(jp2, p_manager)) {
2484         return OPJ_FALSE;
2485     }
2486 
2487     /* validation of the parameters codec */
2488     if (! opj_jp2_exec(jp2, jp2->m_validation_list, stream, p_manager)) {
2489         return OPJ_FALSE;
2490     }
2491 
2492     /* customization of the encoding */
2493     if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2494         return OPJ_FALSE;
2495     }
2496 
2497     /* write header */
2498     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, stream, p_manager)) {
2499         return OPJ_FALSE;
2500     }
2501 
2502     return opj_j2k_start_compress(jp2->j2k, stream, p_image, p_manager);
2503 }
2504 
opj_jp2_find_handler(OPJ_UINT32 p_id)2505 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id)
2506 {
2507     OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(
2508                                        opj_jp2_header_handler_t);
2509 
2510     for (i = 0; i < l_handler_size; ++i) {
2511         if (jp2_header[i].id == p_id) {
2512             return &jp2_header[i];
2513         }
2514     }
2515     return NULL;
2516 }
2517 
2518 /**
2519  * Finds the image execution function related to the given box id.
2520  *
2521  * @param   p_id    the id of the handler to fetch.
2522  *
2523  * @return  the given handler or 00 if it could not be found.
2524  */
opj_jp2_img_find_handler(OPJ_UINT32 p_id)2525 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
2526     OPJ_UINT32 p_id)
2527 {
2528     OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(
2529                                        opj_jp2_header_handler_t);
2530     for (i = 0; i < l_handler_size; ++i) {
2531         if (jp2_img_header[i].id == p_id) {
2532             return &jp2_img_header[i];
2533         }
2534     }
2535 
2536     return NULL;
2537 }
2538 
2539 /**
2540  * Reads a jpeg2000 file signature box.
2541  *
2542  * @param   p_header_data   the data contained in the signature box.
2543  * @param   jp2             the jpeg2000 file codec.
2544  * @param   p_header_size   the size of the data contained in the signature box.
2545  * @param   p_manager       the user event manager.
2546  *
2547  * @return true if the file signature box is valid.
2548  */
opj_jp2_read_jp(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2549 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2550                                 OPJ_BYTE * p_header_data,
2551                                 OPJ_UINT32 p_header_size,
2552                                 opj_event_mgr_t * p_manager
2553                                )
2554 
2555 {
2556     OPJ_UINT32 l_magic_number;
2557 
2558     /* preconditions */
2559     assert(p_header_data != 00);
2560     assert(jp2 != 00);
2561     assert(p_manager != 00);
2562 
2563     if (jp2->jp2_state != JP2_STATE_NONE) {
2564         opj_event_msg(p_manager, EVT_ERROR,
2565                       "The signature box must be the first box in the file.\n");
2566         return OPJ_FALSE;
2567     }
2568 
2569     /* assure length of data is correct (4 -> magic number) */
2570     if (p_header_size != 4) {
2571         opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2572         return OPJ_FALSE;
2573     }
2574 
2575     /* rearrange data */
2576     opj_read_bytes(p_header_data, &l_magic_number, 4);
2577     if (l_magic_number != 0x0d0a870a) {
2578         opj_event_msg(p_manager, EVT_ERROR,
2579                       "Error with JP Signature : bad magic number\n");
2580         return OPJ_FALSE;
2581     }
2582 
2583     jp2->jp2_state |= JP2_STATE_SIGNATURE;
2584 
2585     return OPJ_TRUE;
2586 }
2587 
2588 /**
2589  * Reads a a FTYP box - File type box
2590  *
2591  * @param   p_header_data   the data contained in the FTYP box.
2592  * @param   jp2             the jpeg2000 file codec.
2593  * @param   p_header_size   the size of the data contained in the FTYP box.
2594  * @param   p_manager       the user event manager.
2595  *
2596  * @return true if the FTYP box is valid.
2597  */
opj_jp2_read_ftyp(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2598 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
2599                                   OPJ_BYTE * p_header_data,
2600                                   OPJ_UINT32 p_header_size,
2601                                   opj_event_mgr_t * p_manager
2602                                  )
2603 {
2604     OPJ_UINT32 i, l_remaining_bytes;
2605 
2606     /* preconditions */
2607     assert(p_header_data != 00);
2608     assert(jp2 != 00);
2609     assert(p_manager != 00);
2610 
2611     if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2612         opj_event_msg(p_manager, EVT_ERROR,
2613                       "The ftyp box must be the second box in the file.\n");
2614         return OPJ_FALSE;
2615     }
2616 
2617     /* assure length of data is correct */
2618     if (p_header_size < 8) {
2619         opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2620         return OPJ_FALSE;
2621     }
2622 
2623     opj_read_bytes(p_header_data, &jp2->brand, 4);      /* BR */
2624     p_header_data += 4;
2625 
2626     opj_read_bytes(p_header_data, &jp2->minversion, 4);     /* MinV */
2627     p_header_data += 4;
2628 
2629     l_remaining_bytes = p_header_size - 8;
2630 
2631     /* the number of remaining bytes should be a multiple of 4 */
2632     if ((l_remaining_bytes & 0x3) != 0) {
2633         opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2634         return OPJ_FALSE;
2635     }
2636 
2637     /* div by 4 */
2638     jp2->numcl = l_remaining_bytes >> 2;
2639     if (jp2->numcl) {
2640         jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2641         if (jp2->cl == 00) {
2642             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2643             return OPJ_FALSE;
2644         }
2645     }
2646 
2647     for (i = 0; i < jp2->numcl; ++i) {
2648         opj_read_bytes(p_header_data, &jp2->cl[i], 4);      /* CLi */
2649         p_header_data += 4;
2650     }
2651 
2652     jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2653 
2654     return OPJ_TRUE;
2655 }
2656 
opj_jp2_skip_jp2c(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2657 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
2658                                   opj_stream_private_t *stream,
2659                                   opj_event_mgr_t * p_manager)
2660 {
2661     /* preconditions */
2662     assert(jp2 != 00);
2663     assert(stream != 00);
2664     assert(p_manager != 00);
2665 
2666     jp2->j2k_codestream_offset = opj_stream_tell(stream);
2667 
2668     if (opj_stream_skip(stream, 8, p_manager) != 8) {
2669         return OPJ_FALSE;
2670     }
2671 
2672     return OPJ_TRUE;
2673 }
2674 
opj_jpip_skip_iptr(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2675 static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2,
2676                                    opj_stream_private_t *stream,
2677                                    opj_event_mgr_t * p_manager)
2678 {
2679     /* preconditions */
2680     assert(jp2 != 00);
2681     assert(stream != 00);
2682     assert(p_manager != 00);
2683 
2684     jp2->jpip_iptr_offset = opj_stream_tell(stream);
2685 
2686     if (opj_stream_skip(stream, 24, p_manager) != 24) {
2687         return OPJ_FALSE;
2688     }
2689 
2690     return OPJ_TRUE;
2691 }
2692 
2693 /**
2694  * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2695  *
2696  * @param   p_header_data   the data contained in the file header box.
2697  * @param   jp2             the jpeg2000 file codec.
2698  * @param   p_header_size   the size of the data contained in the file header box.
2699  * @param   p_manager       the user event manager.
2700  *
2701  * @return true if the JP2 Header box was successfully recognized.
2702 */
opj_jp2_read_jp2h(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2703 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
2704                                   OPJ_BYTE *p_header_data,
2705                                   OPJ_UINT32 p_header_size,
2706                                   opj_event_mgr_t * p_manager
2707                                  )
2708 {
2709     OPJ_UINT32 l_box_size = 0, l_current_data_size = 0;
2710     opj_jp2_box_t box;
2711     const opj_jp2_header_handler_t * l_current_handler;
2712     OPJ_BOOL l_has_ihdr = 0;
2713 
2714     /* preconditions */
2715     assert(p_header_data != 00);
2716     assert(jp2 != 00);
2717     assert(p_manager != 00);
2718 
2719     /* make sure the box is well placed */
2720     if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE) {
2721         opj_event_msg(p_manager, EVT_ERROR,
2722                       "The  box must be the first box in the file.\n");
2723         return OPJ_FALSE;
2724     }
2725 
2726     jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2727 
2728     /* iterate while remaining data */
2729     while (p_header_size > 0) {
2730 
2731         if (! opj_jp2_read_boxhdr_char(&box, p_header_data, &l_box_size, p_header_size,
2732                                        p_manager)) {
2733             opj_event_msg(p_manager, EVT_ERROR,
2734                           "Stream error while reading JP2 Header box\n");
2735             return OPJ_FALSE;
2736         }
2737 
2738         if (box.length > p_header_size) {
2739             opj_event_msg(p_manager, EVT_ERROR,
2740                           "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2741             return OPJ_FALSE;
2742         }
2743 
2744         l_current_handler = opj_jp2_img_find_handler(box.type);
2745         l_current_data_size = box.length - l_box_size;
2746         p_header_data += l_box_size;
2747 
2748         if (l_current_handler != 00) {
2749             if (! l_current_handler->handler(jp2, p_header_data, l_current_data_size,
2750                                              p_manager)) {
2751                 return OPJ_FALSE;
2752             }
2753         } else {
2754             jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2755         }
2756 
2757         if (box.type == JP2_IHDR) {
2758             l_has_ihdr = 1;
2759         }
2760 
2761         p_header_data += l_current_data_size;
2762         p_header_size -= box.length;
2763     }
2764 
2765     if (l_has_ihdr == 0) {
2766         opj_event_msg(p_manager, EVT_ERROR,
2767                       "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2768         return OPJ_FALSE;
2769     }
2770 
2771     jp2->jp2_state |= JP2_STATE_HEADER;
2772     jp2->has_jp2h = 1;
2773 
2774     return OPJ_TRUE;
2775 }
2776 
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)2777 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
2778         OPJ_BYTE * p_data,
2779         OPJ_UINT32 * p_number_bytes_read,
2780         OPJ_UINT32 p_box_max_size,
2781         opj_event_mgr_t * p_manager
2782                                         )
2783 {
2784     OPJ_UINT32 l_value;
2785 
2786     /* preconditions */
2787     assert(p_data != 00);
2788     assert(box != 00);
2789     assert(p_number_bytes_read != 00);
2790     assert(p_manager != 00);
2791 
2792     if (p_box_max_size < 8) {
2793         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2794         return OPJ_FALSE;
2795     }
2796 
2797     /* process read data */
2798     opj_read_bytes(p_data, &l_value, 4);
2799     p_data += 4;
2800     box->length = (OPJ_UINT32)(l_value);
2801 
2802     opj_read_bytes(p_data, &l_value, 4);
2803     p_data += 4;
2804     box->type = (OPJ_UINT32)(l_value);
2805 
2806     *p_number_bytes_read = 8;
2807 
2808     /* do we have a "special very large box ?" */
2809     /* read then the XLBox */
2810     if (box->length == 1) {
2811         OPJ_UINT32 l_xl_part_size;
2812 
2813         if (p_box_max_size < 16) {
2814             opj_event_msg(p_manager, EVT_ERROR,
2815                           "Cannot handle XL box of less than 16 bytes\n");
2816             return OPJ_FALSE;
2817         }
2818 
2819         opj_read_bytes(p_data, &l_xl_part_size, 4);
2820         p_data += 4;
2821         *p_number_bytes_read += 4;
2822 
2823         if (l_xl_part_size != 0) {
2824             opj_event_msg(p_manager, EVT_ERROR,
2825                           "Cannot handle box sizes higher than 2^32\n");
2826             return OPJ_FALSE;
2827         }
2828 
2829         opj_read_bytes(p_data, &l_value, 4);
2830         *p_number_bytes_read += 4;
2831         box->length = (OPJ_UINT32)(l_value);
2832 
2833         if (box->length == 0) {
2834             opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2835             return OPJ_FALSE;
2836         }
2837     } else if (box->length == 0) {
2838         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2839         return OPJ_FALSE;
2840     }
2841     if (box->length < *p_number_bytes_read) {
2842         opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2843         return OPJ_FALSE;
2844     }
2845     return OPJ_TRUE;
2846 }
2847 
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)2848 OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
2849                              opj_jp2_t *jp2,
2850                              opj_image_t ** p_image,
2851                              opj_event_mgr_t * p_manager
2852                             )
2853 {
2854     /* preconditions */
2855     assert(jp2 != 00);
2856     assert(p_stream != 00);
2857     assert(p_manager != 00);
2858 
2859     /* customization of the validation */
2860     if (! opj_jp2_setup_decoding_validation(jp2, p_manager)) {
2861         return OPJ_FALSE;
2862     }
2863 
2864     /* customization of the encoding */
2865     if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2866         return OPJ_FALSE;
2867     }
2868 
2869     /* validation of the parameters codec */
2870     if (! opj_jp2_exec(jp2, jp2->m_validation_list, p_stream, p_manager)) {
2871         return OPJ_FALSE;
2872     }
2873 
2874     /* read header */
2875     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) {
2876         return OPJ_FALSE;
2877     }
2878     if (jp2->has_jp2h == 0) {
2879         opj_event_msg(p_manager, EVT_ERROR, "JP2H box missing. Required.\n");
2880         return OPJ_FALSE;
2881     }
2882     if (jp2->has_ihdr == 0) {
2883         opj_event_msg(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n");
2884         return OPJ_FALSE;
2885     }
2886 
2887     return opj_j2k_read_header(p_stream,
2888                                jp2->j2k,
2889                                p_image,
2890                                p_manager);
2891 }
2892 
opj_jp2_setup_encoding_validation(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2893 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
2894         opj_event_mgr_t * p_manager)
2895 {
2896     /* preconditions */
2897     assert(jp2 != 00);
2898     assert(p_manager != 00);
2899 
2900     if (! opj_procedure_list_add_procedure(jp2->m_validation_list,
2901                                            (opj_procedure)opj_jp2_default_validation, p_manager)) {
2902         return OPJ_FALSE;
2903     }
2904     /* DEVELOPER CORNER, add your custom validation procedure */
2905 
2906     return OPJ_TRUE;
2907 }
2908 
opj_jp2_setup_decoding_validation(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2909 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
2910         opj_event_mgr_t * p_manager)
2911 {
2912     /* preconditions */
2913     assert(jp2 != 00);
2914     assert(p_manager != 00);
2915 
2916     OPJ_UNUSED(jp2);
2917     OPJ_UNUSED(p_manager);
2918 
2919     /* DEVELOPER CORNER, add your custom validation procedure */
2920 
2921     return OPJ_TRUE;
2922 }
2923 
opj_jp2_setup_header_writing(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2924 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
2925         opj_event_mgr_t * p_manager)
2926 {
2927     /* preconditions */
2928     assert(jp2 != 00);
2929     assert(p_manager != 00);
2930 
2931     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2932                                            (opj_procedure)opj_jp2_write_jp, p_manager)) {
2933         return OPJ_FALSE;
2934     }
2935     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2936                                            (opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2937         return OPJ_FALSE;
2938     }
2939     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2940                                            (opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2941         return OPJ_FALSE;
2942     }
2943     if (jp2->jpip_on) {
2944         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2945                                                (opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2946             return OPJ_FALSE;
2947         }
2948     }
2949     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2950                                            (opj_procedure)opj_jp2_skip_jp2c, p_manager)) {
2951         return OPJ_FALSE;
2952     }
2953 
2954     /* DEVELOPER CORNER, insert your custom procedures */
2955 
2956     return OPJ_TRUE;
2957 }
2958 
opj_jp2_setup_header_reading(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2959 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
2960         opj_event_mgr_t * p_manager)
2961 {
2962     /* preconditions */
2963     assert(jp2 != 00);
2964     assert(p_manager != 00);
2965 
2966     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2967                                            (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2968         return OPJ_FALSE;
2969     }
2970 
2971     /* DEVELOPER CORNER, add your custom procedures */
2972 
2973     return OPJ_TRUE;
2974 }
2975 
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)2976 OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2,
2977                                   OPJ_UINT32 * p_tile_index,
2978                                   OPJ_UINT32 * p_data_size,
2979                                   OPJ_INT32 * p_tile_x0,
2980                                   OPJ_INT32 * p_tile_y0,
2981                                   OPJ_INT32 * p_tile_x1,
2982                                   OPJ_INT32 * p_tile_y1,
2983                                   OPJ_UINT32 * p_nb_comps,
2984                                   OPJ_BOOL * p_go_on,
2985                                   opj_stream_private_t *p_stream,
2986                                   opj_event_mgr_t * p_manager
2987                                  )
2988 {
2989     return opj_j2k_read_tile_header(p_jp2->j2k,
2990                                     p_tile_index,
2991                                     p_data_size,
2992                                     p_tile_x0, p_tile_y0,
2993                                     p_tile_x1, p_tile_y1,
2994                                     p_nb_comps,
2995                                     p_go_on,
2996                                     p_stream,
2997                                     p_manager);
2998 }
2999 
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)3000 OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2,
3001                             OPJ_UINT32 p_tile_index,
3002                             OPJ_BYTE * p_data,
3003                             OPJ_UINT32 p_data_size,
3004                             opj_stream_private_t *p_stream,
3005                             opj_event_mgr_t * p_manager
3006                            )
3007 
3008 {
3009     return opj_j2k_write_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3010                               p_stream, p_manager);
3011 }
3012 
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)3013 OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2,
3014                              OPJ_UINT32 p_tile_index,
3015                              OPJ_BYTE * p_data,
3016                              OPJ_UINT32 p_data_size,
3017                              opj_stream_private_t *p_stream,
3018                              opj_event_mgr_t * p_manager
3019                             )
3020 {
3021     return opj_j2k_decode_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3022                                p_stream, p_manager);
3023 }
3024 
opj_jp2_destroy(opj_jp2_t * jp2)3025 void opj_jp2_destroy(opj_jp2_t *jp2)
3026 {
3027     if (jp2) {
3028         /* destroy the J2K codec */
3029         opj_j2k_destroy(jp2->j2k);
3030         jp2->j2k = 00;
3031 
3032         if (jp2->comps) {
3033             opj_free(jp2->comps);
3034             jp2->comps = 00;
3035         }
3036 
3037         if (jp2->cl) {
3038             opj_free(jp2->cl);
3039             jp2->cl = 00;
3040         }
3041 
3042         if (jp2->color.icc_profile_buf) {
3043             opj_free(jp2->color.icc_profile_buf);
3044             jp2->color.icc_profile_buf = 00;
3045         }
3046 
3047         if (jp2->color.jp2_cdef) {
3048             if (jp2->color.jp2_cdef->info) {
3049                 opj_free(jp2->color.jp2_cdef->info);
3050                 jp2->color.jp2_cdef->info = NULL;
3051             }
3052 
3053             opj_free(jp2->color.jp2_cdef);
3054             jp2->color.jp2_cdef = 00;
3055         }
3056 
3057         if (jp2->color.jp2_pclr) {
3058             if (jp2->color.jp2_pclr->cmap) {
3059                 opj_free(jp2->color.jp2_pclr->cmap);
3060                 jp2->color.jp2_pclr->cmap = NULL;
3061             }
3062             if (jp2->color.jp2_pclr->channel_sign) {
3063                 opj_free(jp2->color.jp2_pclr->channel_sign);
3064                 jp2->color.jp2_pclr->channel_sign = NULL;
3065             }
3066             if (jp2->color.jp2_pclr->channel_size) {
3067                 opj_free(jp2->color.jp2_pclr->channel_size);
3068                 jp2->color.jp2_pclr->channel_size = NULL;
3069             }
3070             if (jp2->color.jp2_pclr->entries) {
3071                 opj_free(jp2->color.jp2_pclr->entries);
3072                 jp2->color.jp2_pclr->entries = NULL;
3073             }
3074 
3075             opj_free(jp2->color.jp2_pclr);
3076             jp2->color.jp2_pclr = 00;
3077         }
3078 
3079         if (jp2->m_validation_list) {
3080             opj_procedure_list_destroy(jp2->m_validation_list);
3081             jp2->m_validation_list = 00;
3082         }
3083 
3084         if (jp2->m_procedure_list) {
3085             opj_procedure_list_destroy(jp2->m_procedure_list);
3086             jp2->m_procedure_list = 00;
3087         }
3088 
3089         opj_free(jp2);
3090     }
3091 }
3092 
opj_jp2_set_decoded_components(opj_jp2_t * p_jp2,OPJ_UINT32 numcomps,const OPJ_UINT32 * comps_indices,opj_event_mgr_t * p_manager)3093 OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *p_jp2,
3094                                         OPJ_UINT32 numcomps,
3095                                         const OPJ_UINT32* comps_indices,
3096                                         opj_event_mgr_t * p_manager)
3097 {
3098     return opj_j2k_set_decoded_components(p_jp2->j2k,
3099                                           numcomps, comps_indices,
3100                                           p_manager);
3101 }
3102 
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)3103 OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
3104                                  opj_image_t* p_image,
3105                                  OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3106                                  OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3107                                  opj_event_mgr_t * p_manager
3108                                 )
3109 {
3110     return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y,
3111                                    p_end_x, p_end_y, p_manager);
3112 }
3113 
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)3114 OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
3115                           opj_stream_private_t *p_stream,
3116                           opj_image_t* p_image,
3117                           opj_event_mgr_t * p_manager,
3118                           OPJ_UINT32 tile_index
3119                          )
3120 {
3121     if (!p_image) {
3122         return OPJ_FALSE;
3123     }
3124 
3125     opj_event_msg(p_manager, EVT_WARNING,
3126                   "JP2 box which are after the codestream will not be read by this function.\n");
3127 
3128     if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index)) {
3129         opj_event_msg(p_manager, EVT_ERROR,
3130                       "Failed to decode the codestream in the JP2 file\n");
3131         return OPJ_FALSE;
3132     }
3133 
3134     if (p_jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
3135         /* Bypass all JP2 component transforms */
3136         return OPJ_TRUE;
3137     }
3138 
3139     if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
3140         return OPJ_FALSE;
3141     }
3142 
3143     /* Set Image Color Space */
3144     if (p_jp2->enumcs == 16) {
3145         p_image->color_space = OPJ_CLRSPC_SRGB;
3146     } else if (p_jp2->enumcs == 17) {
3147         p_image->color_space = OPJ_CLRSPC_GRAY;
3148     } else if (p_jp2->enumcs == 18) {
3149         p_image->color_space = OPJ_CLRSPC_SYCC;
3150     } else if (p_jp2->enumcs == 24) {
3151         p_image->color_space = OPJ_CLRSPC_EYCC;
3152     } else if (p_jp2->enumcs == 12) {
3153         p_image->color_space = OPJ_CLRSPC_CMYK;
3154     } else {
3155         p_image->color_space = OPJ_CLRSPC_UNKNOWN;
3156     }
3157 
3158     if (p_jp2->color.jp2_pclr) {
3159         /* Part 1, I.5.3.4: Either both or none : */
3160         if (!p_jp2->color.jp2_pclr->cmap) {
3161             opj_jp2_free_pclr(&(p_jp2->color));
3162         } else {
3163             if (!opj_jp2_apply_pclr(p_image, &(p_jp2->color), p_manager)) {
3164                 return OPJ_FALSE;
3165             }
3166         }
3167     }
3168 
3169     /* Apply the color space if needed */
3170     if (p_jp2->color.jp2_cdef) {
3171         opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager);
3172     }
3173 
3174     if (p_jp2->color.icc_profile_buf) {
3175         p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
3176         p_image->icc_profile_len = p_jp2->color.icc_profile_len;
3177         p_jp2->color.icc_profile_buf = NULL;
3178     }
3179 
3180     return OPJ_TRUE;
3181 }
3182 
3183 /* ----------------------------------------------------------------------- */
3184 /* JP2 encoder interface                                             */
3185 /* ----------------------------------------------------------------------- */
3186 
opj_jp2_create(OPJ_BOOL p_is_decoder)3187 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
3188 {
3189     opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1, sizeof(opj_jp2_t));
3190     if (jp2) {
3191 
3192         /* create the J2K codec */
3193         if (! p_is_decoder) {
3194             jp2->j2k = opj_j2k_create_compress();
3195         } else {
3196             jp2->j2k = opj_j2k_create_decompress();
3197         }
3198 
3199         if (jp2->j2k == 00) {
3200             opj_jp2_destroy(jp2);
3201             return 00;
3202         }
3203 
3204         /* Color structure */
3205         jp2->color.icc_profile_buf = NULL;
3206         jp2->color.icc_profile_len = 0;
3207         jp2->color.jp2_cdef = NULL;
3208         jp2->color.jp2_pclr = NULL;
3209         jp2->color.jp2_has_colr = 0;
3210 
3211         /* validation list creation */
3212         jp2->m_validation_list = opj_procedure_list_create();
3213         if (! jp2->m_validation_list) {
3214             opj_jp2_destroy(jp2);
3215             return 00;
3216         }
3217 
3218         /* execution list creation */
3219         jp2->m_procedure_list = opj_procedure_list_create();
3220         if (! jp2->m_procedure_list) {
3221             opj_jp2_destroy(jp2);
3222             return 00;
3223         }
3224     }
3225 
3226     return jp2;
3227 }
3228 
jp2_dump(opj_jp2_t * p_jp2,OPJ_INT32 flag,FILE * out_stream)3229 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3230 {
3231     /* preconditions */
3232     assert(p_jp2 != 00);
3233 
3234     j2k_dump(p_jp2->j2k,
3235              flag,
3236              out_stream);
3237 }
3238 
jp2_get_cstr_index(opj_jp2_t * p_jp2)3239 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
3240 {
3241     return j2k_get_cstr_index(p_jp2->j2k);
3242 }
3243 
jp2_get_cstr_info(opj_jp2_t * p_jp2)3244 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
3245 {
3246     return j2k_get_cstr_info(p_jp2->j2k);
3247 }
3248 
opj_jp2_set_decoded_resolution_factor(opj_jp2_t * p_jp2,OPJ_UINT32 res_factor,opj_event_mgr_t * p_manager)3249 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
3250         OPJ_UINT32 res_factor,
3251         opj_event_mgr_t * p_manager)
3252 {
3253     return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
3254 }
3255 
3256 /* JPIP specific */
3257 
3258 #ifdef USE_JPIP
opj_jpip_write_iptr(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3259 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3260                                     opj_stream_private_t *cio,
3261                                     opj_event_mgr_t * p_manager)
3262 {
3263     OPJ_OFF_T j2k_codestream_exit;
3264     OPJ_BYTE l_data_header [24];
3265 
3266     /* preconditions */
3267     assert(jp2 != 00);
3268     assert(cio != 00);
3269     assert(p_manager != 00);
3270     assert(opj_stream_has_seek(cio));
3271 
3272     j2k_codestream_exit = opj_stream_tell(cio);
3273     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3274     opj_write_bytes(l_data_header + 4, JPIP_IPTR,
3275                     4);                                      /* IPTR */
3276 #if 0
3277     opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3278     opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3279 #else
3280     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3281     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3282 #endif
3283 
3284     if (! opj_stream_seek(cio, jp2->jpip_iptr_offset, p_manager)) {
3285         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3286         return OPJ_FALSE;
3287     }
3288 
3289     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3290         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3291         return OPJ_FALSE;
3292     }
3293 
3294     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3295         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3296         return OPJ_FALSE;
3297     }
3298 
3299     return OPJ_TRUE;
3300 }
3301 
opj_jpip_write_fidx(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3302 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3303                                     opj_stream_private_t *cio,
3304                                     opj_event_mgr_t * p_manager)
3305 {
3306     OPJ_OFF_T j2k_codestream_exit;
3307     OPJ_BYTE l_data_header [24];
3308 
3309     OPJ_UNUSED(jp2);
3310 
3311     /* preconditions */
3312     assert(jp2 != 00);
3313     assert(cio != 00);
3314     assert(p_manager != 00);
3315     assert(opj_stream_has_seek(cio));
3316 
3317     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3318     opj_write_bytes(l_data_header + 4, JPIP_FIDX,
3319                     4);                                      /* IPTR */
3320     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3321     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3322 
3323     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3324         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3325         return OPJ_FALSE;
3326     }
3327 
3328     j2k_codestream_exit = opj_stream_tell(cio);
3329     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3330         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3331         return OPJ_FALSE;
3332     }
3333 
3334     return OPJ_TRUE;
3335 }
3336 
opj_jpip_write_cidx(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3337 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3338                                     opj_stream_private_t *cio,
3339                                     opj_event_mgr_t * p_manager)
3340 {
3341     OPJ_OFF_T j2k_codestream_exit;
3342     OPJ_BYTE l_data_header [24];
3343 
3344     OPJ_UNUSED(jp2);
3345 
3346     /* preconditions */
3347     assert(jp2 != 00);
3348     assert(cio != 00);
3349     assert(p_manager != 00);
3350     assert(opj_stream_has_seek(cio));
3351 
3352     j2k_codestream_exit = opj_stream_tell(cio);
3353     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3354     opj_write_bytes(l_data_header + 4, JPIP_CIDX,
3355                     4);                                      /* IPTR */
3356 #if 0
3357     opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3358     opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3359 #else
3360     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3361     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3362 #endif
3363 
3364     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3365         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3366         return OPJ_FALSE;
3367     }
3368 
3369     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3370         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3371         return OPJ_FALSE;
3372     }
3373 
3374     j2k_codestream_exit = opj_stream_tell(cio);
3375     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3376         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3377         return OPJ_FALSE;
3378     }
3379 
3380     return OPJ_TRUE;
3381 }
3382 
3383 #if 0
3384 static void write_prxy(int offset_jp2c, int length_jp2c, int offset_idx,
3385                        int length_idx, opj_stream_private_t *cio,
3386                        opj_event_mgr_t * p_manager)
3387 {
3388     OPJ_BYTE l_data_header [8];
3389     OPJ_OFF_T len, lenp;
3390 
3391     lenp = opj_stream_tell(cio);
3392     opj_stream_skip(cio, 4, p_manager);         /* L [at the end] */
3393     opj_write_bytes(l_data_header, JPIP_PRXY, 4); /* IPTR           */
3394     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3395 
3396     opj_write_bytes(l_data_header, offset_jp2c, 8);  /* OOFF           */
3397     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3398     opj_write_bytes(l_data_header, length_jp2c, 4);  /* OBH part 1     */
3399     opj_write_bytes(l_data_header + 4, JP2_JP2C, 4); /* OBH part 2     */
3400     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3401 
3402     opj_write_bytes(l_data_header, 1, 1); /* NI             */
3403     opj_stream_write_data(cio, l_data_header, 1, p_manager);
3404 
3405     opj_write_bytes(l_data_header, offset_idx, 8);   /* IOFF           */
3406     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3407     opj_write_bytes(l_data_header, length_idx, 4);   /* IBH part 1     */
3408     opj_write_bytes(l_data_header + 4, JPIP_CIDX, 4);  /* IBH part 2     */
3409     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3410 
3411     len = opj_stream_tell(cio) - lenp;
3412     opj_stream_skip(cio, lenp, p_manager);
3413     opj_write_bytes(l_data_header, len, 4); /* L              */
3414     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3415     opj_stream_seek(cio, lenp + len, p_manager);
3416 }
3417 #endif
3418 
3419 
3420 #if 0
3421 static int write_fidx(int offset_jp2c, int length_jp2c, int offset_idx,
3422                       int length_idx, opj_stream_private_t *cio,
3423                       opj_event_mgr_t * p_manager)
3424 {
3425     OPJ_BYTE l_data_header [4];
3426     OPJ_OFF_T len, lenp;
3427 
3428     lenp = opj_stream_tell(cio);
3429     opj_stream_skip(cio, 4, p_manager);
3430     opj_write_bytes(l_data_header, JPIP_FIDX, 4); /* FIDX */
3431     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3432 
3433     write_prxy(offset_jp2c, length_jp2c, offset_idx, length_idx, cio, p_manager);
3434 
3435     len = opj_stream_tell(cio) - lenp;
3436     opj_stream_skip(cio, lenp, p_manager);
3437     opj_write_bytes(l_data_header, len, 4); /* L              */
3438     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3439     opj_stream_seek(cio, lenp + len, p_manager);
3440 
3441     return len;
3442 }
3443 #endif
3444 #endif /* USE_JPIP */
3445