jbig2_arith_int.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 */
  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. #include "jbig2.h"
  23. #include "jbig2_priv.h"
  24. #include "jbig2_arith.h"
  25. #include "jbig2_arith_int.h"
  26. struct _Jbig2ArithIntCtx {
  27. Jbig2ArithCx IAx[512];
  28. };
  29. Jbig2ArithIntCtx *
  30. jbig2_arith_int_ctx_new(Jbig2Ctx *ctx)
  31. {
  32. Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1);
  33. if (result == NULL) {
  34. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate arithmetic integer coding state");
  35. return NULL;
  36. } else {
  37. memset(result->IAx, 0, sizeof(result->IAx));
  38. }
  39. return result;
  40. }
  41. /* A.2 */
  42. /* Return value: -1 on error, 0 on normal value, 1 on OOB return. */
  43. int
  44. jbig2_arith_int_decode(Jbig2Ctx *ctx, Jbig2ArithIntCtx *actx, Jbig2ArithState *as, int32_t *p_result)
  45. {
  46. Jbig2ArithCx *IAx = actx->IAx;
  47. int PREV = 1;
  48. int S;
  49. int32_t V;
  50. int bit;
  51. int n_tail, offset;
  52. int i;
  53. S = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  54. if (S < 0)
  55. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx S");
  56. PREV = (PREV << 1) | S;
  57. bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  58. if (bit < 0)
  59. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 0");
  60. PREV = (PREV << 1) | bit;
  61. if (bit) {
  62. bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  63. if (bit < 0)
  64. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 1");
  65. PREV = (PREV << 1) | bit;
  66. if (bit) {
  67. bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  68. if (bit < 0)
  69. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 2");
  70. PREV = (PREV << 1) | bit;
  71. if (bit) {
  72. bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  73. if (bit < 0)
  74. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 3");
  75. PREV = (PREV << 1) | bit;
  76. if (bit) {
  77. bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  78. if (bit < 0)
  79. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 4");
  80. PREV = (PREV << 1) | bit;
  81. if (bit) {
  82. n_tail = 32;
  83. offset = 4436;
  84. } else {
  85. n_tail = 12;
  86. offset = 340;
  87. }
  88. } else {
  89. n_tail = 8;
  90. offset = 84;
  91. }
  92. } else {
  93. n_tail = 6;
  94. offset = 20;
  95. }
  96. } else {
  97. n_tail = 4;
  98. offset = 4;
  99. }
  100. } else {
  101. n_tail = 2;
  102. offset = 0;
  103. }
  104. V = 0;
  105. for (i = 0; i < n_tail; i++) {
  106. bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  107. if (bit < 0)
  108. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx V bit %d", i);
  109. PREV = ((PREV << 1) & 511) | (PREV & 256) | bit;
  110. V = (V << 1) | bit;
  111. }
  112. /* offset is always >=0, so underflow can't happen. */
  113. /* avoid overflow by clamping 32 bit value. */
  114. if (V > INT32_MAX - offset)
  115. V = INT32_MAX;
  116. else
  117. V += offset;
  118. V = S ? -V : V;
  119. *p_result = V;
  120. return S && V == 0 ? 1 : 0;
  121. }
  122. void
  123. jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax)
  124. {
  125. jbig2_free(ctx->allocator, iax);
  126. }