xref: /aosp_15_r20/external/pdfium/third_party/libopenjpeg/0003-dwt-decode.patch (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1diff --git a/third_party/libopenjpeg/dwt.c b/third_party/libopenjpeg/dwt.c
2index 4164ba090..a36b7ed10 100644
3--- a/third_party/libopenjpeg/dwt.c
4+++ b/third_party/libopenjpeg/dwt.c
5@@ -63,9 +63,6 @@
6 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
7 /*@{*/
8
9-#define OPJ_WS(i) v->mem[(i)*2]
10-#define OPJ_WD(i) v->mem[(1+(i)*2)]
11-
12 #ifdef __AVX2__
13 /** Number of int32 values in a AVX2 register */
14 #define VREG_INT_COUNT       8
15@@ -82,6 +79,7 @@
16
17 typedef struct dwt_local {
18     OPJ_INT32* mem;
19+    OPJ_SIZE_T mem_count;
20     OPJ_INT32 dn;   /* number of elements in high pass band */
21     OPJ_INT32 sn;   /* number of elements in low pass band */
22     OPJ_INT32 cas;  /* 0 = start on even coord, 1 = start on odd coord */
23@@ -140,7 +138,7 @@ static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
24 Inverse wavelet transform in 2-D.
25 */
26 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
27-                                    opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
28+                                    const opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
29
30 static OPJ_BOOL opj_dwt_decode_partial_tile(
31     opj_tcd_tilecomp_t* tilec,
32@@ -181,13 +179,20 @@ static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
33
34 /*@}*/
35
36-#define OPJ_S(i) a[(i)*2]
37-#define OPJ_D(i) a[(1+(i)*2)]
38-#define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
39-#define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
40+#define IDX_S(i) (i)*2
41+#define IDX_D(i) 1 + (i)* 2
42+#define UNDERFLOW_SN(i) ((i) >= sn&&sn>0)
43+#define UNDERFLOW_DN(i) ((i) >= dn&&dn>0)
44+#define OVERFLOW_S(i) (IDX_S(i) >= a_count)
45+#define OVERFLOW_D(i) (IDX_D(i) >= a_count)
46+
47+#define OPJ_S(i) a[IDX_S(i)]
48+#define OPJ_D(i) a[IDX_D(i)]
49+#define OPJ_S_(i) ((i)<0 ? OPJ_S(0) : (UNDERFLOW_SN(i) ? OPJ_S(sn - 1) : OVERFLOW_S(i) ? OPJ_S(i - 1) : OPJ_S(i)))
50+#define OPJ_D_(i) ((i)<0 ? OPJ_D(0) : (UNDERFLOW_DN(i) ? OPJ_D(dn - 1) : OVERFLOW_D(i) ? OPJ_D(i - 1) : OPJ_D(i)))
51 /* new */
52-#define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
53-#define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
54+#define OPJ_SS_(i) ((i)<0 ? OPJ_S(0) : (UNDERFLOW_DN(i) ? OPJ_S(dn - 1) : OVERFLOW_S(i) ? OPJ_S(i - 1) : OPJ_S(i)))
55+#define OPJ_DD_(i) ((i)<0 ? OPJ_D(0) : (UNDERFLOW_SN(i) ? OPJ_D(sn - 1) : OVERFLOW_D(i) ? OPJ_D(i - 1) : OPJ_D(i)))
56
57 /* <summary>                                                              */
58 /* This table contains the norms of the 5-3 wavelets for different bands. */
59@@ -296,8 +301,8 @@ static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
60 /* <summary>                            */
61 /* Inverse 5-3 wavelet transform in 1-D. */
62 /* </summary>                           */
63-static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
64-                              OPJ_INT32 cas)
65+static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_SIZE_T a_count, OPJ_INT32 dn,
66+                              OPJ_INT32 sn, OPJ_INT32 cas)
67 {
68     OPJ_INT32 i;
69
70@@ -326,7 +331,7 @@ static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
71
72 static void opj_dwt_decode_1(const opj_dwt_t *v)
73 {
74-    opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
75+    opj_dwt_decode_1_(v->mem, v->mem_count, v->dn, v->sn, v->cas);
76 }
77
78 #endif /* STANDARD_SLOW_VERSION */
79@@ -2062,7 +2067,7 @@ static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
80 /* Inverse wavelet transform in 2-D.    */
81 /* </summary>                           */
82 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
83-                                    opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
84+        const opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
85 {
86     opj_dwt_t h;
87     opj_dwt_t v;
88@@ -2084,22 +2089,23 @@ static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
89         return OPJ_TRUE;
90     }
91     num_threads = opj_thread_pool_get_thread_count(tp);
92-    h_mem_size = opj_dwt_max_resolution(tr, numres);
93+    h.mem_count = opj_dwt_max_resolution(tr, numres);
94     /* overflow check */
95-    if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
96+    if (h.mem_count > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
97         /* FIXME event manager error callback */
98         return OPJ_FALSE;
99     }
100     /* We need PARALLEL_COLS_53 times the height of the array, */
101     /* since for the vertical pass */
102     /* we process PARALLEL_COLS_53 columns at a time */
103-    h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
104+    h_mem_size = h.mem_count * PARALLEL_COLS_53 * sizeof(OPJ_INT32);
105     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
106     if (! h.mem) {
107         /* FIXME event manager error callback */
108         return OPJ_FALSE;
109     }
110
111+    v.mem_count = h.mem_count;
112     v.mem = h.mem;
113
114     while (--numres) {
115@@ -2277,7 +2283,8 @@ static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
116     OPJ_UNUSED(ret);
117 }
118
119-static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
120+static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_SIZE_T a_count,
121+                                     OPJ_INT32 dn, OPJ_INT32 sn,
122                                      OPJ_INT32 cas,
123                                      OPJ_INT32 win_l_x0,
124                                      OPJ_INT32 win_l_x1,
125@@ -2657,16 +2664,16 @@ static OPJ_BOOL opj_dwt_decode_partial_tile(
126         opj_sparse_array_int32_free(sa);
127         return OPJ_TRUE;
128     }
129-    h_mem_size = opj_dwt_max_resolution(tr, numres);
130+    h.mem_count = opj_dwt_max_resolution(tr, numres);
131     /* overflow check */
132     /* in vertical pass, we process 4 columns at a time */
133-    if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
134+    if (h.mem_count > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
135         /* FIXME event manager error callback */
136         opj_sparse_array_int32_free(sa);
137         return OPJ_FALSE;
138     }
139
140-    h_mem_size *= 4 * sizeof(OPJ_INT32);
141+    h_mem_size = h.mem_count * 4 * sizeof(OPJ_INT32);
142     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
143     if (! h.mem) {
144         /* FIXME event manager error callback */
145@@ -2674,6 +2681,7 @@ static OPJ_BOOL opj_dwt_decode_partial_tile(
146         return OPJ_FALSE;
147     }
148
149+    v.mem_count = h.mem_count;
150     v.mem = h.mem;
151
152     for (resno = 1; resno < numres; resno ++) {
153@@ -2784,7 +2792,7 @@ static OPJ_BOOL opj_dwt_decode_partial_tile(
154                                              win_ll_x1,
155                                              win_hl_x0,
156                                              win_hl_x1);
157-                opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
158+                opj_dwt_decode_partial_1(h.mem, h.mem_count, h.dn, h.sn, h.cas,
159                                          (OPJ_INT32)win_ll_x0,
160                                          (OPJ_INT32)win_ll_x1,
161                                          (OPJ_INT32)win_hl_x0,
162