jbig2_arith_iaid.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (C) 2001-2023 Artifex Software, Inc.
  2. All Rights Reserved.
  3. This software is provided AS-IS with no warranty, either express or
  4. implied.
  5. This software is distributed under license and may not be copied,
  6. modified or distributed except as expressly authorized under the terms
  7. of the license contained in the file LICENSE in this distribution.
  8. Refer to licensing information at http://www.artifex.com or contact
  9. Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  10. CA 94129, USA, for further information.
  11. */
  12. /*
  13. jbig2dec
  14. */
  15. /* Annex A.3 */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "os_types.h"
  20. #include <stddef.h>
  21. #include <string.h> /* memset() */
  22. #ifdef VERBOSE
  23. #include <stdio.h> /* for debug printing only */
  24. #endif
  25. #include "jbig2.h"
  26. #include "jbig2_priv.h"
  27. #include "jbig2_arith.h"
  28. #include "jbig2_arith_iaid.h"
  29. struct _Jbig2ArithIaidCtx {
  30. uint8_t SBSYMCODELEN;
  31. Jbig2ArithCx *IAIDx;
  32. };
  33. Jbig2ArithIaidCtx *
  34. jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, uint8_t SBSYMCODELEN)
  35. {
  36. Jbig2ArithIaidCtx *result;
  37. size_t ctx_size;
  38. if (sizeof(ctx_size) * 8 <= SBSYMCODELEN)
  39. {
  40. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "requested IAID arithmetic coding state size too large");
  41. return NULL;
  42. }
  43. ctx_size = (size_t) 1U << SBSYMCODELEN;
  44. result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
  45. if (result == NULL) {
  46. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate IAID arithmetic coding state");
  47. return NULL;
  48. }
  49. result->SBSYMCODELEN = SBSYMCODELEN;
  50. result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size);
  51. if (result->IAIDx == NULL)
  52. {
  53. jbig2_free(ctx->allocator, result);
  54. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate symbol ID in IAID arithmetic coding state");
  55. return NULL;
  56. }
  57. memset(result->IAIDx, 0, ctx_size);
  58. return result;
  59. }
  60. /* A.3 */
  61. /* Return value: -1 on error, 0 on normal value */
  62. int
  63. jbig2_arith_iaid_decode(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *actx, Jbig2ArithState *as, int32_t *p_result)
  64. {
  65. Jbig2ArithCx *IAIDx = actx->IAIDx;
  66. uint8_t SBSYMCODELEN = actx->SBSYMCODELEN;
  67. /* A.3 (1) */
  68. int PREV = 1;
  69. int D;
  70. int i;
  71. /* A.3 (2) */
  72. for (i = 0; i < SBSYMCODELEN; i++) {
  73. D = jbig2_arith_decode(ctx, as, &IAIDx[PREV]);
  74. if (D < 0)
  75. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAIDx code");
  76. #ifdef VERBOSE
  77. fprintf(stderr, "IAID%x: D = %d\n", PREV, D);
  78. #endif
  79. PREV = (PREV << 1) | D;
  80. }
  81. /* A.3 (3) */
  82. PREV -= 1 << SBSYMCODELEN;
  83. #ifdef VERBOSE
  84. fprintf(stderr, "IAID result: %d\n", PREV);
  85. #endif
  86. *p_result = PREV;
  87. return 0;
  88. }
  89. void
  90. jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax)
  91. {
  92. if (iax != NULL) {
  93. jbig2_free(ctx->allocator, iax->IAIDx);
  94. jbig2_free(ctx->allocator, iax);
  95. }
  96. }