/* * Copyright (c) 1999-2000 Image Power, Inc. and the University of * British Columbia. * Copyright (c) 2001-2002 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /* * JPEG-2000 Code Stream Library * * $Id$ */ /******************************************************************************\* Includes.\******************************************************************************/ #include "jpc_cs.h" #include "jasper/jas_malloc.h"#include "jasper/jas_debug.h"#include "jasper/jas_image.h" /******************************************************************************\* Types.\******************************************************************************/ /* Marker segment table entry. */typedef struct { int id; const char *name; jpc_msops_t ops;} jpc_mstabent_t; /******************************************************************************\* Local prototypes.\******************************************************************************/ static const jpc_mstabent_t *jpc_mstab_lookup(int id); static int jpc_poc_dumpparms(jpc_ms_t *ms);static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static void jpc_poc_destroyparms(jpc_ms_t *ms); static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_sot_dumpparms(jpc_ms_t *ms);static int jpc_siz_dumpparms(jpc_ms_t *ms);static int jpc_cod_dumpparms(jpc_ms_t *ms);static int jpc_coc_dumpparms(jpc_ms_t *ms);static int jpc_qcd_dumpparms(jpc_ms_t *ms);static int jpc_qcc_dumpparms(jpc_ms_t *ms);static int jpc_rgn_dumpparms(jpc_ms_t *ms);static int jpc_unk_dumpparms(jpc_ms_t *ms);static int jpc_sop_dumpparms(jpc_ms_t *ms);static int jpc_ppm_dumpparms(jpc_ms_t *ms);static int jpc_ppt_dumpparms(jpc_ms_t *ms);static int jpc_crg_dumpparms(jpc_ms_t *ms);static int jpc_com_dumpparms(jpc_ms_t *ms); static void jpc_siz_destroyparms(jpc_ms_t *ms);static void jpc_qcd_destroyparms(jpc_ms_t *ms);static void jpc_qcc_destroyparms(jpc_ms_t *ms);static void jpc_cod_destroyparms(jpc_ms_t *ms);static void jpc_coc_destroyparms(jpc_ms_t *ms);static void jpc_unk_destroyparms(jpc_ms_t *ms);static void jpc_ppm_destroyparms(jpc_ms_t *ms);static void jpc_ppt_destroyparms(jpc_ms_t *ms);static void jpc_crg_destroyparms(jpc_ms_t *ms);static void jpc_com_destroyparms(jpc_ms_t *ms); static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms);static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, jas_stream_t *in, uint_fast16_t len);static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, jas_stream_t *out);static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms);static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms);static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms); /******************************************************************************\* Global data.\******************************************************************************/ static const jpc_mstabent_t jpc_mstab[] = { {JPC_MS_SOC, "SOC", {0, 0, 0, 0}}, {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms, jpc_sot_dumpparms}}, {JPC_MS_SOD, "SOD", {0, 0, 0, 0}}, {JPC_MS_EOC, "EOC", {0, 0, 0, 0}}, {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms, jpc_siz_putparms, jpc_siz_dumpparms}}, {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms, jpc_cod_putparms, jpc_cod_dumpparms}}, {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms, jpc_coc_putparms, jpc_coc_dumpparms}}, {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms, jpc_rgn_dumpparms}}, {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms, jpc_qcd_putparms, jpc_qcd_dumpparms}}, {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms, jpc_qcc_putparms, jpc_qcc_dumpparms}}, {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms, jpc_poc_putparms, jpc_poc_dumpparms}}, {JPC_MS_TLM, "TLM", {jpc_unk_destroyparms, jpc_unk_getparms, jpc_unk_putparms, 0}}, {JPC_MS_PLM, "PLM", {jpc_unk_destroyparms, jpc_unk_getparms, jpc_unk_putparms, 0}}, {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms, jpc_ppm_putparms, jpc_ppm_dumpparms}}, {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms, jpc_ppt_putparms, jpc_ppt_dumpparms}}, {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms, jpc_sop_dumpparms}}, {JPC_MS_EPH, "EPH", {0, 0, 0, 0}}, {JPC_MS_CRG, "CRG", {jpc_crg_destroyparms, jpc_crg_getparms, jpc_crg_putparms, jpc_crg_dumpparms}}, {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms, jpc_com_putparms, jpc_com_dumpparms}}, {-1, "UNKNOWN", {jpc_unk_destroyparms, jpc_unk_getparms, jpc_unk_putparms, jpc_unk_dumpparms}}}; /******************************************************************************\* Code stream manipulation functions.\******************************************************************************/ /* Create a code stream state object. */jpc_cstate_t *jpc_cstate_create(){ jpc_cstate_t *cstate; if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) { return 0; } cstate->numcomps = 0; return cstate;} /* Destroy a code stream state object. */void jpc_cstate_destroy(jpc_cstate_t *cstate){ jas_free(cstate);} /* Read a marker segment from a stream. */jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate){ jpc_ms_t *ms; const jpc_mstabent_t *mstabent; jas_stream_t *tmpstream; if (!(ms = jpc_ms_create(0))) { return 0; } /* Get the marker type. */ if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN) { jpc_ms_destroy(ms); return 0; } mstabent = jpc_mstab_lookup(ms->id); ms->ops = &mstabent->ops; /* Get the marker segment length and parameters if present. */ /* Note: It is tacitly assumed that a marker segment cannot have parameters unless it has a length field. That is, there cannot be a parameters field without a length field and vice versa. */ if (JPC_MS_HASPARMS(ms->id)) { /* Get the length of the marker segment. */ if (jpc_getuint16(in, &ms->len) || ms->len < 3) { jpc_ms_destroy(ms); return 0; } /* Calculate the length of the marker segment parameters. */ ms->len -= 2; /* Create and prepare a temporary memory stream from which to read the marker segment parameters. */ /* Note: This approach provides a simple way of ensuring that we never read beyond the end of the marker segment (even if the marker segment length is errantly set too small). */ if (!(tmpstream = jas_stream_memopen(0, 0))) { jpc_ms_destroy(ms); return 0; } if (jas_stream_copy(tmpstream, in, ms->len) || jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) { jas_stream_close(tmpstream); jpc_ms_destroy(ms); return 0; } /* Get the marker segment parameters. */ if ((*ms->ops->getparms)(ms, cstate, tmpstream)) { ms->ops = 0; jpc_ms_destroy(ms); jas_stream_close(tmpstream); return 0; } if (jas_get_debug_level() > 0) { jpc_ms_dump(ms); } if (JAS_CAST(jas_ulong, jas_stream_tell(tmpstream)) != ms->len) { jas_logwarnf( "warning: trailing garbage in marker segment (%ld bytes)\n", ms->len - jas_stream_tell(tmpstream)); } /* Close the temporary stream. */ jas_stream_close(tmpstream); } else { /* There are no marker segment parameters. */ ms->len = 0; if (jas_get_debug_level() > 0) { jpc_ms_dump(ms); } } /* Update the code stream state information based on the type of marker segment read. */ /* Note: This is a bit of a hack, but I'm not going to define another type of virtual function for this one special case. */ if (ms->id == JPC_MS_SIZ) { cstate->numcomps = ms->parms.siz.numcomps; } return ms;} /* Write a marker segment to a stream. */int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms){ jas_stream_t *tmpstream; int len; /* Output the marker segment type. */ if (jpc_putuint16(out, ms->id)) { return -1; } /* Output the marker segment length and parameters if necessary. */ if (ms->ops->putparms) { /* Create a temporary stream in which to buffer the parameter data. */ if (!(tmpstream = jas_stream_memopen(0, 0))) { return -1; } if ((*ms->ops->putparms)(ms, cstate, tmpstream)) { jas_stream_close(tmpstream); return -1; } /* Get the number of bytes of parameter data written. */ if ((len = jas_stream_tell(tmpstream)) < 0) { jas_stream_close(tmpstream); return -1; } ms->len = len; /* Write the marker segment length and parameter data to the output stream. */ if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 || jpc_putuint16(out, ms->len + 2) || jas_stream_copy(out, tmpstream, ms->len) < 0) { jas_stream_close(tmpstream); return -1; } /* Close the temporary stream. */ jas_stream_close(tmpstream); } /* This is a bit of a hack, but I'm not going to define another type of virtual function for this one special case. */ if (ms->id == JPC_MS_SIZ) { cstate->numcomps = ms->parms.siz.numcomps; } if (jas_get_debug_level() > 0) { jpc_ms_dump(ms); } return 0;} /******************************************************************************\* Marker segment operations.\******************************************************************************/ /* Create a marker segment of the specified type. */jpc_ms_t *jpc_ms_create(int type){ jpc_ms_t *ms; const jpc_mstabent_t *mstabent; if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) { return 0; } ms->id = type; ms->len = 0; mstabent = jpc_mstab_lookup(ms->id); ms->ops = &mstabent->ops; memset(&ms->parms, 0, sizeof(jpc_msparms_t)); return ms;} /* Destroy a marker segment. */void jpc_ms_destroy(jpc_ms_t *ms){ if (ms->ops && ms->ops->destroyparms) { (*ms->ops->destroyparms)(ms); } jas_free(ms);} /* Dump a marker segment to a stream for debugging. */void jpc_ms_dump(jpc_ms_t *ms){ const jpc_mstabent_t *mstabent; mstabent = jpc_mstab_lookup(ms->id); jas_logprintf("type = 0x%04"PRIxFAST16" (%s);", ms->id, mstabent->name); if (JPC_MS_HASPARMS(ms->id)) { jas_logprintf(" len = %"PRIuFAST16";", ms->len + 2); if (ms->ops->dumpparms) { (*ms->ops->dumpparms)(ms); } else { jas_logprintf("\n"); } } else { jas_logprintf("\n"); }} /******************************************************************************\* SOT marker segment operations.\******************************************************************************/ static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in){ jpc_sot_t *sot = &ms->parms.sot; JAS_UNUSED(cstate); if (jpc_getuint16(in, &sot->tileno) || jpc_getuint32(in, &sot->len) || jpc_getuint8(in, &sot->partno) || jpc_getuint8(in, &sot->numparts)) { return -1; } if (sot->tileno > 65534 || sot->len < 12 || sot->partno > 254) { return -1; } if (jas_stream_eof(in)) { return -1; } return 0;} static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out){ jpc_sot_t *sot = &ms->parms.sot; JAS_UNUSED(cstate); if (jpc_putuint16(out, sot->tileno) || jpc_putuint32(out, sot->len) || jpc_putuint8(out, sot->partno) || jpc_putuint8(out, sot->numparts)) { return -1; } return 0;} static int jpc_sot_dumpparms(jpc_ms_t *ms){ jpc_sot_t *sot = &ms->parms.sot; jas_logprintf( "tileno = %"PRIuFAST16"; len = %"PRIuFAST32"; partno = %d; numparts = %d\n", sot->tileno, sot->len, sot->partno, sot->numparts); return 0;} /******************************************************************************\* SIZ marker segment operations.\******************************************************************************/ static void jpc_siz_destroyparms(jpc_ms_t *ms){ jpc_siz_t *siz = &ms->parms.siz; if (siz->comps) { jas_free(siz->comps); }} static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in){ jpc_siz_t *siz = &ms->parms.siz; unsigned int i; uint_fast8_t tmp; siz->comps = 0; JAS_UNUSED(cstate); if (jpc_getuint16(in, &siz->caps) || jpc_getuint32(in, &siz->width) || jpc_getuint32(in, &siz->height) || jpc_getuint32(in, &siz->xoff) || jpc_getuint32(in, &siz->yoff) || jpc_getuint32(in, &siz->tilewidth) || jpc_getuint32(in, &siz->tileheight) || jpc_getuint32(in, &siz->tilexoff) || jpc_getuint32(in, &siz->tileyoff) || jpc_getuint16(in, &siz->numcomps)) { goto error; }Lines 1–500 of 1687. Load more
/* * Copyright (c) 1999-2000 Image Power, Inc. and the University of * British Columbia. * Copyright (c) 2001-2002 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /* * JPEG-2000 Code Stream Library * * $Id$ */ /******************************************************************************\ * Includes. \******************************************************************************/ #include "jpc_cs.h" #include "jasper/jas_malloc.h" #include "jasper/jas_debug.h" #include "jasper/jas_image.h" /******************************************************************************\ * Types. \******************************************************************************/ /* Marker segment table entry. */ typedef struct { int id; const char *name; jpc_msops_t ops; } jpc_mstabent_t; /******************************************************************************\ * Local prototypes. \******************************************************************************/ static const jpc_mstabent_t *jpc_mstab_lookup(int id); static int jpc_poc_dumpparms(jpc_ms_t *ms); static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static void jpc_poc_destroyparms(jpc_ms_t *ms); static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); static int jpc_sot_dumpparms(jpc_ms_t *ms); static int jpc_siz_dumpparms(jpc_ms_t *ms); static int jpc_cod_dumpparms(jpc_ms_t *ms); static int jpc_coc_dumpparms(jpc_ms_t *ms); static int jpc_qcd_dumpparms(jpc_ms_t *ms); static int jpc_qcc_dumpparms(jpc_ms_t *ms); static int jpc_rgn_dumpparms(jpc_ms_t *ms); static int jpc_unk_dumpparms(jpc_ms_t *ms); static int jpc_sop_dumpparms(jpc_ms_t *ms); static int jpc_ppm_dumpparms(jpc_ms_t *ms); static int jpc_ppt_dumpparms(jpc_ms_t *ms); static int jpc_crg_dumpparms(jpc_ms_t *ms); static int jpc_com_dumpparms(jpc_ms_t *ms); static void jpc_siz_destroyparms(jpc_ms_t *ms); static void jpc_qcd_destroyparms(jpc_ms_t *ms); static void jpc_qcc_destroyparms(jpc_ms_t *ms); static void jpc_cod_destroyparms(jpc_ms_t *ms); static void jpc_coc_destroyparms(jpc_ms_t *ms); static void jpc_unk_destroyparms(jpc_ms_t *ms); static void jpc_ppm_destroyparms(jpc_ms_t *ms); static void jpc_ppt_destroyparms(jpc_ms_t *ms); static void jpc_crg_destroyparms(jpc_ms_t *ms); static void jpc_com_destroyparms(jpc_ms_t *ms); static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms); static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, jas_stream_t *in, uint_fast16_t len); static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, jas_stream_t *out); static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms); static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms); static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms); /******************************************************************************\ * Global data. \******************************************************************************/ static const jpc_mstabent_t jpc_mstab[] = { {JPC_MS_SOC, "SOC", {0, 0, 0, 0}}, {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms, jpc_sot_dumpparms}}, {JPC_MS_SOD, "SOD", {0, 0, 0, 0}}, {JPC_MS_EOC, "EOC", {0, 0, 0, 0}}, {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms, jpc_siz_putparms, jpc_siz_dumpparms}}, {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms, jpc_cod_putparms, jpc_cod_dumpparms}}, {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms, jpc_coc_putparms, jpc_coc_dumpparms}}, {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms, jpc_rgn_dumpparms}}, {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms, jpc_qcd_putparms, jpc_qcd_dumpparms}}, {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms, jpc_qcc_putparms, jpc_qcc_dumpparms}}, {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms, jpc_poc_putparms, jpc_poc_dumpparms}}, {JPC_MS_TLM, "TLM", {jpc_unk_destroyparms, jpc_unk_getparms, jpc_unk_putparms, 0}}, {JPC_MS_PLM, "PLM", {jpc_unk_destroyparms, jpc_unk_getparms, jpc_unk_putparms, 0}}, {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms, jpc_ppm_putparms, jpc_ppm_dumpparms}}, {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms, jpc_ppt_putparms, jpc_ppt_dumpparms}}, {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms, jpc_sop_dumpparms}}, {JPC_MS_EPH, "EPH", {0, 0, 0, 0}}, {JPC_MS_CRG, "CRG", {jpc_crg_destroyparms, jpc_crg_getparms, jpc_crg_putparms, jpc_crg_dumpparms}}, {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms, jpc_com_putparms, jpc_com_dumpparms}}, {-1, "UNKNOWN", {jpc_unk_destroyparms, jpc_unk_getparms, jpc_unk_putparms, jpc_unk_dumpparms}} }; /******************************************************************************\ * Code stream manipulation functions. \******************************************************************************/ /* Create a code stream state object. */ jpc_cstate_t *jpc_cstate_create() { jpc_cstate_t *cstate; if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) { return 0; } cstate->numcomps = 0; return cstate; } /* Destroy a code stream state object. */ void jpc_cstate_destroy(jpc_cstate_t *cstate) { jas_free(cstate); } /* Read a marker segment from a stream. */ jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate) { jpc_ms_t *ms; const jpc_mstabent_t *mstabent; jas_stream_t *tmpstream; if (!(ms = jpc_ms_create(0))) { return 0; } /* Get the marker type. */ if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN) { jpc_ms_destroy(ms); return 0; } mstabent = jpc_mstab_lookup(ms->id); ms->ops = &mstabent->ops; /* Get the marker segment length and parameters if present. */ /* Note: It is tacitly assumed that a marker segment cannot have parameters unless it has a length field. That is, there cannot be a parameters field without a length field and vice versa. */ if (JPC_MS_HASPARMS(ms->id)) { /* Get the length of the marker segment. */ if (jpc_getuint16(in, &ms->len) || ms->len < 3) { jpc_ms_destroy(ms); return 0; } /* Calculate the length of the marker segment parameters. */ ms->len -= 2; /* Create and prepare a temporary memory stream from which to read the marker segment parameters. */ /* Note: This approach provides a simple way of ensuring that we never read beyond the end of the marker segment (even if the marker segment length is errantly set too small). */ if (!(tmpstream = jas_stream_memopen(0, 0))) { jpc_ms_destroy(ms); return 0; } if (jas_stream_copy(tmpstream, in, ms->len) || jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) { jas_stream_close(tmpstream); jpc_ms_destroy(ms); return 0; } /* Get the marker segment parameters. */ if ((*ms->ops->getparms)(ms, cstate, tmpstream)) { ms->ops = 0; jpc_ms_destroy(ms); jas_stream_close(tmpstream); return 0; } if (jas_get_debug_level() > 0) { jpc_ms_dump(ms); } if (JAS_CAST(jas_ulong, jas_stream_tell(tmpstream)) != ms->len) { jas_logwarnf( "warning: trailing garbage in marker segment (%ld bytes)\n", ms->len - jas_stream_tell(tmpstream)); } /* Close the temporary stream. */ jas_stream_close(tmpstream); } else { /* There are no marker segment parameters. */ ms->len = 0; if (jas_get_debug_level() > 0) { jpc_ms_dump(ms); } } /* Update the code stream state information based on the type of marker segment read. */ /* Note: This is a bit of a hack, but I'm not going to define another type of virtual function for this one special case. */ if (ms->id == JPC_MS_SIZ) { cstate->numcomps = ms->parms.siz.numcomps; } return ms; } /* Write a marker segment to a stream. */ int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms) { jas_stream_t *tmpstream; int len; /* Output the marker segment type. */ if (jpc_putuint16(out, ms->id)) { return -1; } /* Output the marker segment length and parameters if necessary. */ if (ms->ops->putparms) { /* Create a temporary stream in which to buffer the parameter data. */ if (!(tmpstream = jas_stream_memopen(0, 0))) { return -1; } if ((*ms->ops->putparms)(ms, cstate, tmpstream)) { jas_stream_close(tmpstream); return -1; } /* Get the number of bytes of parameter data written. */ if ((len = jas_stream_tell(tmpstream)) < 0) { jas_stream_close(tmpstream); return -1; } ms->len = len; /* Write the marker segment length and parameter data to the output stream. */ if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 || jpc_putuint16(out, ms->len + 2) || jas_stream_copy(out, tmpstream, ms->len) < 0) { jas_stream_close(tmpstream); return -1; } /* Close the temporary stream. */ jas_stream_close(tmpstream); } /* This is a bit of a hack, but I'm not going to define another type of virtual function for this one special case. */ if (ms->id == JPC_MS_SIZ) { cstate->numcomps = ms->parms.siz.numcomps; } if (jas_get_debug_level() > 0) { jpc_ms_dump(ms); } return 0; } /******************************************************************************\ * Marker segment operations. \******************************************************************************/ /* Create a marker segment of the specified type. */ jpc_ms_t *jpc_ms_create(int type) { jpc_ms_t *ms; const jpc_mstabent_t *mstabent; if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) { return 0; } ms->id = type; ms->len = 0; mstabent = jpc_mstab_lookup(ms->id); ms->ops = &mstabent->ops; memset(&ms->parms, 0, sizeof(jpc_msparms_t)); return ms; } /* Destroy a marker segment. */ void jpc_ms_destroy(jpc_ms_t *ms) { if (ms->ops && ms->ops->destroyparms) { (*ms->ops->destroyparms)(ms); } jas_free(ms); } /* Dump a marker segment to a stream for debugging. */ void jpc_ms_dump(jpc_ms_t *ms) { const jpc_mstabent_t *mstabent; mstabent = jpc_mstab_lookup(ms->id); jas_logprintf("type = 0x%04"PRIxFAST16" (%s);", ms->id, mstabent->name); if (JPC_MS_HASPARMS(ms->id)) { jas_logprintf(" len = %"PRIuFAST16";", ms->len + 2); if (ms->ops->dumpparms) { (*ms->ops->dumpparms)(ms); } else { jas_logprintf("\n"); } } else { jas_logprintf("\n"); } } /******************************************************************************\ * SOT marker segment operations. \******************************************************************************/ static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { jpc_sot_t *sot = &ms->parms.sot; JAS_UNUSED(cstate); if (jpc_getuint16(in, &sot->tileno) || jpc_getuint32(in, &sot->len) || jpc_getuint8(in, &sot->partno) || jpc_getuint8(in, &sot->numparts)) { return -1; } if (sot->tileno > 65534 || sot->len < 12 || sot->partno > 254) { return -1; } if (jas_stream_eof(in)) { return -1; } return 0; } static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { jpc_sot_t *sot = &ms->parms.sot; JAS_UNUSED(cstate); if (jpc_putuint16(out, sot->tileno) || jpc_putuint32(out, sot->len) || jpc_putuint8(out, sot->partno) || jpc_putuint8(out, sot->numparts)) { return -1; } return 0; } static int jpc_sot_dumpparms(jpc_ms_t *ms) { jpc_sot_t *sot = &ms->parms.sot; jas_logprintf( "tileno = %"PRIuFAST16"; len = %"PRIuFAST32"; partno = %d; numparts = %d\n", sot->tileno, sot->len, sot->partno, sot->numparts); return 0; } /******************************************************************************\ * SIZ marker segment operations. \******************************************************************************/ static void jpc_siz_destroyparms(jpc_ms_t *ms) { jpc_siz_t *siz = &ms->parms.siz; if (siz->comps) { jas_free(siz->comps); } } static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { jpc_siz_t *siz = &ms->parms.siz; unsigned int i; uint_fast8_t tmp; siz->comps = 0; JAS_UNUSED(cstate); if (jpc_getuint16(in, &siz->caps) || jpc_getuint32(in, &siz->width) || jpc_getuint32(in, &siz->height) || jpc_getuint32(in, &siz->xoff) || jpc_getuint32(in, &siz->yoff) || jpc_getuint32(in, &siz->tilewidth) || jpc_getuint32(in, &siz->tileheight) || jpc_getuint32(in, &siz->tilexoff) || jpc_getuint32(in, &siz->tileyoff) || jpc_getuint16(in, &siz->numcomps)) { goto error; }