13b921b29Sopenharmony_cidiff --git a/java/TJUnitTest.java b/java/TJUnitTest.java
23b921b29Sopenharmony_ciindex 91ad5fd..07c59d8 100644
33b921b29Sopenharmony_ci--- a/java/TJUnitTest.java
43b921b29Sopenharmony_ci+++ b/java/TJUnitTest.java
53b921b29Sopenharmony_ci@@ -837,6 +837,54 @@ final class TJUnitTest {
63b921b29Sopenharmony_ci     if (tjd != null) tjd.close();
73b921b29Sopenharmony_ci   }
83b921b29Sopenharmony_ci 
93b921b29Sopenharmony_ci+  static void overflowTest() throws Exception {
103b921b29Sopenharmony_ci+    /* Ensure that the various buffer size methods don't overflow */
113b921b29Sopenharmony_ci+    int size = 0;
123b921b29Sopenharmony_ci+    boolean exception = false;
133b921b29Sopenharmony_ci+    try {
143b921b29Sopenharmony_ci+      exception = false;
153b921b29Sopenharmony_ci+      size = TJ.bufSize(18919, 18919, TJ.SAMP_444);
163b921b29Sopenharmony_ci+    } catch (Exception e) { exception = true; }
173b921b29Sopenharmony_ci+    if (!exception || size != 0)
183b921b29Sopenharmony_ci+      throw new Exception("TJ.bufSize() overflow");
193b921b29Sopenharmony_ci+    try {
203b921b29Sopenharmony_ci+      exception = false;
213b921b29Sopenharmony_ci+      size = TJ.bufSizeYUV(26755, 1, 26755, TJ.SAMP_444);
223b921b29Sopenharmony_ci+    } catch (Exception e) { exception = true; }
233b921b29Sopenharmony_ci+    if (!exception || size != 0)
243b921b29Sopenharmony_ci+      throw new Exception("TJ.bufSizeYUV() overflow");
253b921b29Sopenharmony_ci+    try {
263b921b29Sopenharmony_ci+      exception = false;
273b921b29Sopenharmony_ci+      size = TJ.bufSizeYUV(26754, 3, 26754, TJ.SAMP_444);
283b921b29Sopenharmony_ci+    } catch (Exception e) { exception = true; }
293b921b29Sopenharmony_ci+    if (!exception || size != 0)
303b921b29Sopenharmony_ci+      throw new Exception("TJ.bufSizeYUV() overflow");
313b921b29Sopenharmony_ci+    try {
323b921b29Sopenharmony_ci+      exception = false;
333b921b29Sopenharmony_ci+      size = TJ.bufSizeYUV(26754, -1, 26754, TJ.SAMP_444);
343b921b29Sopenharmony_ci+    } catch (Exception e) { exception = true; }
353b921b29Sopenharmony_ci+    if (!exception || size != 0)
363b921b29Sopenharmony_ci+      throw new Exception("TJ.bufSizeYUV() overflow");
373b921b29Sopenharmony_ci+    try {
383b921b29Sopenharmony_ci+      exception = false;
393b921b29Sopenharmony_ci+      size = TJ.planeSizeYUV(0, 46341, 0, 46341, TJ.SAMP_444);
403b921b29Sopenharmony_ci+    } catch (Exception e) { exception = true; }
413b921b29Sopenharmony_ci+    if (!exception || size != 0)
423b921b29Sopenharmony_ci+      throw new Exception("TJ.planeSizeYUV() overflow");
433b921b29Sopenharmony_ci+    try {
443b921b29Sopenharmony_ci+      exception = false;
453b921b29Sopenharmony_ci+      size = TJ.planeWidth(0, Integer.MAX_VALUE, TJ.SAMP_420);
463b921b29Sopenharmony_ci+    } catch (Exception e) { exception = true; }
473b921b29Sopenharmony_ci+    if (!exception || size != 0)
483b921b29Sopenharmony_ci+      throw new Exception("TJ.planeWidth() overflow");
493b921b29Sopenharmony_ci+    try {
503b921b29Sopenharmony_ci+      exception = false;
513b921b29Sopenharmony_ci+      size = TJ.planeHeight(0, Integer.MAX_VALUE, TJ.SAMP_420);
523b921b29Sopenharmony_ci+    } catch (Exception e) { exception = true; }
533b921b29Sopenharmony_ci+    if (!exception || size != 0)
543b921b29Sopenharmony_ci+      throw new Exception("TJ.planeHeight() overflow");
553b921b29Sopenharmony_ci+  }
563b921b29Sopenharmony_ci+
573b921b29Sopenharmony_ci   static void bufSizeTest() throws Exception {
583b921b29Sopenharmony_ci     int w, h, i, subsamp;
593b921b29Sopenharmony_ci     byte[] srcBuf, dstBuf = null;
603b921b29Sopenharmony_ci@@ -912,6 +960,7 @@ final class TJUnitTest {
613b921b29Sopenharmony_ci       }
623b921b29Sopenharmony_ci       if (doYUV)
633b921b29Sopenharmony_ci         FORMATS_4BYTE[4] = -1;
643b921b29Sopenharmony_ci+      overflowTest();
653b921b29Sopenharmony_ci       doTest(35, 39, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_444,
663b921b29Sopenharmony_ci              testName);
673b921b29Sopenharmony_ci       doTest(39, 41, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_444,
683b921b29Sopenharmony_cidiff --git a/jchuff.h b/jchuff.h
693b921b29Sopenharmony_ciindex 314a232..da7809a 100644
703b921b29Sopenharmony_ci--- a/jchuff.h
713b921b29Sopenharmony_ci+++ b/jchuff.h
723b921b29Sopenharmony_ci@@ -3,8 +3,8 @@
733b921b29Sopenharmony_ci  *
743b921b29Sopenharmony_ci  * This file was part of the Independent JPEG Group's software:
753b921b29Sopenharmony_ci  * Copyright (C) 1991-1997, Thomas G. Lane.
763b921b29Sopenharmony_ci- * It was modified by The libjpeg-turbo Project to include only code relevant
773b921b29Sopenharmony_ci- * to libjpeg-turbo.
783b921b29Sopenharmony_ci+ * libjpeg-turbo Modifications:
793b921b29Sopenharmony_ci+ * Copyright (C) 2022, D. R. Commander.
803b921b29Sopenharmony_ci  * For conditions of distribution and use, see the accompanying README.ijg
813b921b29Sopenharmony_ci  * file.
823b921b29Sopenharmony_ci  *
833b921b29Sopenharmony_ci@@ -25,6 +25,14 @@
843b921b29Sopenharmony_ci #define MAX_COEF_BITS  14
853b921b29Sopenharmony_ci #endif
863b921b29Sopenharmony_ci 
873b921b29Sopenharmony_ci+/* The progressive Huffman encoder uses an unsigned 16-bit data type to store
883b921b29Sopenharmony_ci+ * absolute values of coefficients, because it is possible to inject a
893b921b29Sopenharmony_ci+ * coefficient value of -32768 into the encoder by attempting to transform a
903b921b29Sopenharmony_ci+ * malformed 12-bit JPEG image, and the absolute value of -32768 would overflow
913b921b29Sopenharmony_ci+ * a signed 16-bit integer.
923b921b29Sopenharmony_ci+ */
933b921b29Sopenharmony_ci+typedef unsigned short UJCOEF;
943b921b29Sopenharmony_ci+
953b921b29Sopenharmony_ci /* Derived data constructed for each Huffman table */
963b921b29Sopenharmony_ci 
973b921b29Sopenharmony_ci typedef struct {
983b921b29Sopenharmony_cidiff --git a/jcphuff.c b/jcphuff.c
993b921b29Sopenharmony_ciindex 1101987..7c9d7cf 100644
1003b921b29Sopenharmony_ci--- a/jcphuff.c
1013b921b29Sopenharmony_ci+++ b/jcphuff.c
1023b921b29Sopenharmony_ci@@ -5,7 +5,7 @@
1033b921b29Sopenharmony_ci  * Copyright (C) 1995-1997, Thomas G. Lane.
1043b921b29Sopenharmony_ci  * libjpeg-turbo Modifications:
1053b921b29Sopenharmony_ci  * Copyright (C) 2011, 2015, 2018, 2021, D. R. Commander.
1063b921b29Sopenharmony_ci- * Copyright (C) 2016, 2018, Matthieu Darbois.
1073b921b29Sopenharmony_ci+ * Copyright (C) 2016, 2018, 2022, Matthieu Darbois.
1083b921b29Sopenharmony_ci  * Copyright (C) 2020, Arm Limited.
1093b921b29Sopenharmony_ci  * Copyright (C) 2021, Alex Richardson.
1103b921b29Sopenharmony_ci  * For conditions of distribution and use, see the accompanying README.ijg
1113b921b29Sopenharmony_ci@@ -83,11 +83,11 @@ typedef struct {
1123b921b29Sopenharmony_ci   /* Pointer to routine to prepare data for encode_mcu_AC_first() */
1133b921b29Sopenharmony_ci   void (*AC_first_prepare) (const JCOEF *block,
1143b921b29Sopenharmony_ci                             const int *jpeg_natural_order_start, int Sl,
1153b921b29Sopenharmony_ci-                            int Al, JCOEF *values, size_t *zerobits);
1163b921b29Sopenharmony_ci+                            int Al, UJCOEF *values, size_t *zerobits);
1173b921b29Sopenharmony_ci   /* Pointer to routine to prepare data for encode_mcu_AC_refine() */
1183b921b29Sopenharmony_ci   int (*AC_refine_prepare) (const JCOEF *block,
1193b921b29Sopenharmony_ci                             const int *jpeg_natural_order_start, int Sl,
1203b921b29Sopenharmony_ci-                            int Al, JCOEF *absvalues, size_t *bits);
1213b921b29Sopenharmony_ci+                            int Al, UJCOEF *absvalues, size_t *bits);
1223b921b29Sopenharmony_ci 
1233b921b29Sopenharmony_ci   /* Mode flag: TRUE for optimization, FALSE for actual data output */
1243b921b29Sopenharmony_ci   boolean gather_statistics;
1253b921b29Sopenharmony_ci@@ -157,14 +157,14 @@ METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo,
1263b921b29Sopenharmony_ci                                        JBLOCKROW *MCU_data);
1273b921b29Sopenharmony_ci METHODDEF(void) encode_mcu_AC_first_prepare
1283b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
1293b921b29Sopenharmony_ci-   JCOEF *values, size_t *zerobits);
1303b921b29Sopenharmony_ci+   UJCOEF *values, size_t *zerobits);
1313b921b29Sopenharmony_ci METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo,
1323b921b29Sopenharmony_ci                                        JBLOCKROW *MCU_data);
1333b921b29Sopenharmony_ci METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo,
1343b921b29Sopenharmony_ci                                         JBLOCKROW *MCU_data);
1353b921b29Sopenharmony_ci METHODDEF(int) encode_mcu_AC_refine_prepare
1363b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
1373b921b29Sopenharmony_ci-   JCOEF *absvalues, size_t *bits);
1383b921b29Sopenharmony_ci+   UJCOEF *absvalues, size_t *bits);
1393b921b29Sopenharmony_ci METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo,
1403b921b29Sopenharmony_ci                                         JBLOCKROW *MCU_data);
1413b921b29Sopenharmony_ci METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo);
1423b921b29Sopenharmony_ci@@ -584,8 +584,8 @@ encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1433b921b29Sopenharmony_ci       continue; \
1443b921b29Sopenharmony_ci     /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \
1453b921b29Sopenharmony_ci     temp2 ^= temp; \
1463b921b29Sopenharmony_ci-    values[k] = temp; \
1473b921b29Sopenharmony_ci-    values[k + DCTSIZE2] = temp2; \
1483b921b29Sopenharmony_ci+    values[k] = (UJCOEF)temp; \
1493b921b29Sopenharmony_ci+    values[k + DCTSIZE2] = (UJCOEF)temp2; \
1503b921b29Sopenharmony_ci     zerobits |= ((size_t)1U) << k; \
1513b921b29Sopenharmony_ci   } \
1523b921b29Sopenharmony_ci }
1533b921b29Sopenharmony_ci@@ -593,7 +593,7 @@ encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1543b921b29Sopenharmony_ci METHODDEF(void)
1553b921b29Sopenharmony_ci encode_mcu_AC_first_prepare(const JCOEF *block,
1563b921b29Sopenharmony_ci                             const int *jpeg_natural_order_start, int Sl,
1573b921b29Sopenharmony_ci-                            int Al, JCOEF *values, size_t *bits)
1583b921b29Sopenharmony_ci+                            int Al, UJCOEF *values, size_t *bits)
1593b921b29Sopenharmony_ci {
1603b921b29Sopenharmony_ci   register int k, temp, temp2;
1613b921b29Sopenharmony_ci   size_t zerobits = 0U;
1623b921b29Sopenharmony_ci@@ -666,9 +666,9 @@ encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1633b921b29Sopenharmony_ci   register int nbits, r;
1643b921b29Sopenharmony_ci   int Sl = cinfo->Se - cinfo->Ss + 1;
1653b921b29Sopenharmony_ci   int Al = cinfo->Al;
1663b921b29Sopenharmony_ci-  JCOEF values_unaligned[2 * DCTSIZE2 + 15];
1673b921b29Sopenharmony_ci-  JCOEF *values;
1683b921b29Sopenharmony_ci-  const JCOEF *cvalue;
1693b921b29Sopenharmony_ci+  UJCOEF values_unaligned[2 * DCTSIZE2 + 15];
1703b921b29Sopenharmony_ci+  UJCOEF *values;
1713b921b29Sopenharmony_ci+  const UJCOEF *cvalue;
1723b921b29Sopenharmony_ci   size_t zerobits;
1733b921b29Sopenharmony_ci   size_t bits[8 / SIZEOF_SIZE_T];
1743b921b29Sopenharmony_ci 
1753b921b29Sopenharmony_ci@@ -681,7 +681,7 @@ encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1763b921b29Sopenharmony_ci       emit_restart(entropy, entropy->next_restart_num);
1773b921b29Sopenharmony_ci 
1783b921b29Sopenharmony_ci #ifdef WITH_SIMD
1793b921b29Sopenharmony_ci-  cvalue = values = (JCOEF *)PAD((JUINTPTR)values_unaligned, 16);
1803b921b29Sopenharmony_ci+  cvalue = values = (UJCOEF *)PAD((size_t)values_unaligned, 16);
1813b921b29Sopenharmony_ci #else
1823b921b29Sopenharmony_ci   /* Not using SIMD, so alignment is not needed */
1833b921b29Sopenharmony_ci   cvalue = values = values_unaligned;
1843b921b29Sopenharmony_ci@@ -815,7 +815,7 @@ encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1853b921b29Sopenharmony_ci       zerobits |= ((size_t)1U) << k; \
1863b921b29Sopenharmony_ci       signbits |= ((size_t)(temp2 + 1)) << k; \
1873b921b29Sopenharmony_ci     } \
1883b921b29Sopenharmony_ci-    absvalues[k] = (JCOEF)temp; /* save abs value for main pass */ \
1893b921b29Sopenharmony_ci+    absvalues[k] = (UJCOEF)temp; /* save abs value for main pass */ \
1903b921b29Sopenharmony_ci     if (temp == 1) \
1913b921b29Sopenharmony_ci       EOB = k + koffset;        /* EOB = index of last newly-nonzero coef */ \
1923b921b29Sopenharmony_ci   } \
1933b921b29Sopenharmony_ci@@ -824,7 +824,7 @@ encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1943b921b29Sopenharmony_ci METHODDEF(int)
1953b921b29Sopenharmony_ci encode_mcu_AC_refine_prepare(const JCOEF *block,
1963b921b29Sopenharmony_ci                              const int *jpeg_natural_order_start, int Sl,
1973b921b29Sopenharmony_ci-                             int Al, JCOEF *absvalues, size_t *bits)
1983b921b29Sopenharmony_ci+                             int Al, UJCOEF *absvalues, size_t *bits)
1993b921b29Sopenharmony_ci {
2003b921b29Sopenharmony_ci   register int k, temp, temp2;
2013b921b29Sopenharmony_ci   int EOB = 0;
2023b921b29Sopenharmony_ci@@ -931,9 +931,9 @@ encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
2033b921b29Sopenharmony_ci   unsigned int BR;
2043b921b29Sopenharmony_ci   int Sl = cinfo->Se - cinfo->Ss + 1;
2053b921b29Sopenharmony_ci   int Al = cinfo->Al;
2063b921b29Sopenharmony_ci-  JCOEF absvalues_unaligned[DCTSIZE2 + 15];
2073b921b29Sopenharmony_ci-  JCOEF *absvalues;
2083b921b29Sopenharmony_ci-  const JCOEF *cabsvalue, *EOBPTR;
2093b921b29Sopenharmony_ci+  UJCOEF absvalues_unaligned[DCTSIZE2 + 15];
2103b921b29Sopenharmony_ci+  UJCOEF *absvalues;
2113b921b29Sopenharmony_ci+  const UJCOEF *cabsvalue, *EOBPTR;
2123b921b29Sopenharmony_ci   size_t zerobits, signbits;
2133b921b29Sopenharmony_ci   size_t bits[16 / SIZEOF_SIZE_T];
2143b921b29Sopenharmony_ci 
2153b921b29Sopenharmony_ci@@ -946,7 +946,7 @@ encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
2163b921b29Sopenharmony_ci       emit_restart(entropy, entropy->next_restart_num);
2173b921b29Sopenharmony_ci 
2183b921b29Sopenharmony_ci #ifdef WITH_SIMD
2193b921b29Sopenharmony_ci-  cabsvalue = absvalues = (JCOEF *)PAD((JUINTPTR)absvalues_unaligned, 16);
2203b921b29Sopenharmony_ci+  cabsvalue = absvalues = (UJCOEF *)PAD((size_t)absvalues_unaligned, 16);
2213b921b29Sopenharmony_ci #else
2223b921b29Sopenharmony_ci   /* Not using SIMD, so alignment is not needed */
2233b921b29Sopenharmony_ci   cabsvalue = absvalues = absvalues_unaligned;
2243b921b29Sopenharmony_cidiff --git a/jerror.c b/jerror.c
2253b921b29Sopenharmony_ciindex 936c4f5..aa0c6ee 100644
2263b921b29Sopenharmony_ci--- a/jerror.c
2273b921b29Sopenharmony_ci+++ b/jerror.c
2283b921b29Sopenharmony_ci@@ -27,6 +27,8 @@
2293b921b29Sopenharmony_ci #include "jversion.h"
2303b921b29Sopenharmony_ci #include "jerror.h"
2313b921b29Sopenharmony_ci 
2323b921b29Sopenharmony_ci+#include <stdlib.h>
2333b921b29Sopenharmony_ci+
2343b921b29Sopenharmony_ci #ifdef USE_WINDOWS_MESSAGEBOX
2353b921b29Sopenharmony_ci #include <windows.h>
2363b921b29Sopenharmony_ci #endif
2373b921b29Sopenharmony_cidiff --git a/jsimd.h b/jsimd.h
2383b921b29Sopenharmony_ciindex 6c20365..f3a87ee 100644
2393b921b29Sopenharmony_ci--- a/jsimd.h
2403b921b29Sopenharmony_ci+++ b/jsimd.h
2413b921b29Sopenharmony_ci@@ -114,10 +114,10 @@ EXTERN(int) jsimd_can_encode_mcu_AC_first_prepare(void);
2423b921b29Sopenharmony_ci 
2433b921b29Sopenharmony_ci EXTERN(void) jsimd_encode_mcu_AC_first_prepare
2443b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
2453b921b29Sopenharmony_ci-   JCOEF *values, size_t *zerobits);
2463b921b29Sopenharmony_ci+   UJCOEF *values, size_t *zerobits);
2473b921b29Sopenharmony_ci 
2483b921b29Sopenharmony_ci EXTERN(int) jsimd_can_encode_mcu_AC_refine_prepare(void);
2493b921b29Sopenharmony_ci 
2503b921b29Sopenharmony_ci EXTERN(int) jsimd_encode_mcu_AC_refine_prepare
2513b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
2523b921b29Sopenharmony_ci-   JCOEF *absvalues, size_t *bits);
2533b921b29Sopenharmony_ci+   UJCOEF *absvalues, size_t *bits);
2543b921b29Sopenharmony_cidiff --git a/jsimd_none.c b/jsimd_none.c
2553b921b29Sopenharmony_ciindex 5b38a9f..a25db73 100644
2563b921b29Sopenharmony_ci--- a/jsimd_none.c
2573b921b29Sopenharmony_ci+++ b/jsimd_none.c
2583b921b29Sopenharmony_ci@@ -2,8 +2,8 @@
2593b921b29Sopenharmony_ci  * jsimd_none.c
2603b921b29Sopenharmony_ci  *
2613b921b29Sopenharmony_ci  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
2623b921b29Sopenharmony_ci- * Copyright (C) 2009-2011, 2014, D. R. Commander.
2633b921b29Sopenharmony_ci- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
2643b921b29Sopenharmony_ci+ * Copyright (C) 2009-2011, 2014, 2022, D. R. Commander.
2653b921b29Sopenharmony_ci+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
2663b921b29Sopenharmony_ci  * Copyright (C) 2020, Arm Limited.
2673b921b29Sopenharmony_ci  *
2683b921b29Sopenharmony_ci  * Based on the x86 SIMD extension for IJG JPEG library,
2693b921b29Sopenharmony_ci@@ -412,7 +412,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
2703b921b29Sopenharmony_ci GLOBAL(void)
2713b921b29Sopenharmony_ci jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
2723b921b29Sopenharmony_ci                                   const int *jpeg_natural_order_start, int Sl,
2733b921b29Sopenharmony_ci-                                  int Al, JCOEF *values, size_t *zerobits)
2743b921b29Sopenharmony_ci+                                  int Al, UJCOEF *values, size_t *zerobits)
2753b921b29Sopenharmony_ci {
2763b921b29Sopenharmony_ci }
2773b921b29Sopenharmony_ci 
2783b921b29Sopenharmony_ci@@ -425,7 +425,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
2793b921b29Sopenharmony_ci GLOBAL(int)
2803b921b29Sopenharmony_ci jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
2813b921b29Sopenharmony_ci                                    const int *jpeg_natural_order_start, int Sl,
2823b921b29Sopenharmony_ci-                                   int Al, JCOEF *absvalues, size_t *bits)
2833b921b29Sopenharmony_ci+                                   int Al, UJCOEF *absvalues, size_t *bits)
2843b921b29Sopenharmony_ci {
2853b921b29Sopenharmony_ci   return 0;
2863b921b29Sopenharmony_ci }
2873b921b29Sopenharmony_cidiff --git a/simd/arm/aarch32/jsimd.c b/simd/arm/aarch32/jsimd.c
2883b921b29Sopenharmony_ciindex fac55df..d6568c5 100644
2893b921b29Sopenharmony_ci--- a/simd/arm/aarch32/jsimd.c
2903b921b29Sopenharmony_ci+++ b/simd/arm/aarch32/jsimd.c
2913b921b29Sopenharmony_ci@@ -4,7 +4,7 @@
2923b921b29Sopenharmony_ci  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
2933b921b29Sopenharmony_ci  * Copyright (C) 2011, Nokia Corporation and/or its subsidiary(-ies).
2943b921b29Sopenharmony_ci  * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, D. R. Commander.
2953b921b29Sopenharmony_ci- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
2963b921b29Sopenharmony_ci+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
2973b921b29Sopenharmony_ci  * Copyright (C) 2019, Google LLC.
2983b921b29Sopenharmony_ci  * Copyright (C) 2020, Arm Limited.
2993b921b29Sopenharmony_ci  *
3003b921b29Sopenharmony_ci@@ -950,7 +950,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
3013b921b29Sopenharmony_ci GLOBAL(void)
3023b921b29Sopenharmony_ci jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
3033b921b29Sopenharmony_ci                                   const int *jpeg_natural_order_start, int Sl,
3043b921b29Sopenharmony_ci-                                  int Al, JCOEF *values, size_t *zerobits)
3053b921b29Sopenharmony_ci+                                  int Al, UJCOEF *values, size_t *zerobits)
3063b921b29Sopenharmony_ci {
3073b921b29Sopenharmony_ci   jsimd_encode_mcu_AC_first_prepare_neon(block, jpeg_natural_order_start,
3083b921b29Sopenharmony_ci                                          Sl, Al, values, zerobits);
3093b921b29Sopenharmony_ci@@ -975,7 +975,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
3103b921b29Sopenharmony_ci GLOBAL(int)
3113b921b29Sopenharmony_ci jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
3123b921b29Sopenharmony_ci                                    const int *jpeg_natural_order_start, int Sl,
3133b921b29Sopenharmony_ci-                                   int Al, JCOEF *absvalues, size_t *bits)
3143b921b29Sopenharmony_ci+                                   int Al, UJCOEF *absvalues, size_t *bits)
3153b921b29Sopenharmony_ci {
3163b921b29Sopenharmony_ci   return jsimd_encode_mcu_AC_refine_prepare_neon(block,
3173b921b29Sopenharmony_ci                                                  jpeg_natural_order_start, Sl,
3183b921b29Sopenharmony_cidiff --git a/simd/arm/aarch64/jsimd.c b/simd/arm/aarch64/jsimd.c
3193b921b29Sopenharmony_ciindex 8570b82..7de4f9a 100644
3203b921b29Sopenharmony_ci--- a/simd/arm/aarch64/jsimd.c
3213b921b29Sopenharmony_ci+++ b/simd/arm/aarch64/jsimd.c
3223b921b29Sopenharmony_ci@@ -4,7 +4,7 @@
3233b921b29Sopenharmony_ci  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
3243b921b29Sopenharmony_ci  * Copyright (C) 2011, Nokia Corporation and/or its subsidiary(-ies).
3253b921b29Sopenharmony_ci  * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, 2020, D. R. Commander.
3263b921b29Sopenharmony_ci- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
3273b921b29Sopenharmony_ci+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
3283b921b29Sopenharmony_ci  * Copyright (C) 2020, Arm Limited.
3293b921b29Sopenharmony_ci  *
3303b921b29Sopenharmony_ci  * Based on the x86 SIMD extension for IJG JPEG library,
3313b921b29Sopenharmony_ci@@ -1028,7 +1028,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
3323b921b29Sopenharmony_ci GLOBAL(void)
3333b921b29Sopenharmony_ci jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
3343b921b29Sopenharmony_ci                                   const int *jpeg_natural_order_start, int Sl,
3353b921b29Sopenharmony_ci-                                  int Al, JCOEF *values, size_t *zerobits)
3363b921b29Sopenharmony_ci+                                  int Al, UJCOEF *values, size_t *zerobits)
3373b921b29Sopenharmony_ci {
3383b921b29Sopenharmony_ci   jsimd_encode_mcu_AC_first_prepare_neon(block, jpeg_natural_order_start,
3393b921b29Sopenharmony_ci                                          Sl, Al, values, zerobits);
3403b921b29Sopenharmony_ci@@ -1055,7 +1055,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
3413b921b29Sopenharmony_ci GLOBAL(int)
3423b921b29Sopenharmony_ci jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
3433b921b29Sopenharmony_ci                                    const int *jpeg_natural_order_start, int Sl,
3443b921b29Sopenharmony_ci-                                   int Al, JCOEF *absvalues, size_t *bits)
3453b921b29Sopenharmony_ci+                                   int Al, UJCOEF *absvalues, size_t *bits)
3463b921b29Sopenharmony_ci {
3473b921b29Sopenharmony_ci   return jsimd_encode_mcu_AC_refine_prepare_neon(block,
3483b921b29Sopenharmony_ci                                                  jpeg_natural_order_start,
3493b921b29Sopenharmony_cidiff --git a/simd/arm/jcphuff-neon.c b/simd/arm/jcphuff-neon.c
3503b921b29Sopenharmony_ciindex b91c5db..24d756e 100644
3513b921b29Sopenharmony_ci--- a/simd/arm/jcphuff-neon.c
3523b921b29Sopenharmony_ci+++ b/simd/arm/jcphuff-neon.c
3533b921b29Sopenharmony_ci@@ -41,10 +41,10 @@
3543b921b29Sopenharmony_ci 
3553b921b29Sopenharmony_ci void jsimd_encode_mcu_AC_first_prepare_neon
3563b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
3573b921b29Sopenharmony_ci-   JCOEF *values, size_t *zerobits)
3583b921b29Sopenharmony_ci+   UJCOEF *values, size_t *zerobits)
3593b921b29Sopenharmony_ci {
3603b921b29Sopenharmony_ci-  JCOEF *values_ptr = values;
3613b921b29Sopenharmony_ci-  JCOEF *diff_values_ptr = values + DCTSIZE2;
3623b921b29Sopenharmony_ci+  JCOEF *values_ptr = values;
3633b921b29Sopenharmony_ci+  JCOEF *diff_values_ptr = values + DCTSIZE2;
3643b921b29Sopenharmony_ci 
3653b921b29Sopenharmony_ci   /* Rows of coefficients to zero (since they haven't been processed) */
3663b921b29Sopenharmony_ci   int i, rows_to_zero = 8;
3673b921b29Sopenharmony_ci@@ -274,7 +274,7 @@ void jsimd_encode_mcu_AC_first_prepare_neon
3683b921b29Sopenharmony_ci 
3693b921b29Sopenharmony_ci int jsimd_encode_mcu_AC_refine_prepare_neon
3703b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
3713b921b29Sopenharmony_ci-   JCOEF *absvalues, size_t *bits)
3723b921b29Sopenharmony_ci+   UJCOEF *absvalues, size_t *bits)
3733b921b29Sopenharmony_ci {
3743b921b29Sopenharmony_ci   /* Temporary storage buffers for data used to compute the signbits bitmap and
3753b921b29Sopenharmony_ci    * the end-of-block (EOB) position
3763b921b29Sopenharmony_ci@@ -282,7 +282,7 @@ int jsimd_encode_mcu_AC_refine_prepare_neon
3773b921b29Sopenharmony_ci   uint8_t coef_sign_bits[64];
3783b921b29Sopenharmony_ci   uint8_t coef_eq1_bits[64];
3793b921b29Sopenharmony_ci 
3803b921b29Sopenharmony_ci-  JCOEF *absvalues_ptr = absvalues;
3813b921b29Sopenharmony_ci+  UJCOEF *absvalues_ptr = absvalues;
3823b921b29Sopenharmony_ci   uint8_t *coef_sign_bits_ptr = coef_sign_bits;
3833b921b29Sopenharmony_ci   uint8_t *eq1_bits_ptr = coef_eq1_bits;
3843b921b29Sopenharmony_ci 
3853b921b29Sopenharmony_cidiff --git a/simd/i386/jsimd.c b/simd/i386/jsimd.c
3863b921b29Sopenharmony_ciindex 563949a..ebe0767 100644
3873b921b29Sopenharmony_ci--- a/simd/i386/jsimd.c
3883b921b29Sopenharmony_ci+++ b/simd/i386/jsimd.c
3893b921b29Sopenharmony_ci@@ -3,7 +3,7 @@
3903b921b29Sopenharmony_ci  *
3913b921b29Sopenharmony_ci  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
3923b921b29Sopenharmony_ci  * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, D. R. Commander.
3933b921b29Sopenharmony_ci- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
3943b921b29Sopenharmony_ci+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
3953b921b29Sopenharmony_ci  *
3963b921b29Sopenharmony_ci  * Based on the x86 SIMD extension for IJG JPEG library,
3973b921b29Sopenharmony_ci  * Copyright (C) 1999-2006, MIYASAKA Masaru.
3983b921b29Sopenharmony_ci@@ -1219,7 +1219,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
3993b921b29Sopenharmony_ci GLOBAL(void)
4003b921b29Sopenharmony_ci jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
4013b921b29Sopenharmony_ci                                   const int *jpeg_natural_order_start, int Sl,
4023b921b29Sopenharmony_ci-                                  int Al, JCOEF *values, size_t *zerobits)
4033b921b29Sopenharmony_ci+                                  int Al, UJCOEF *values, size_t *zerobits)
4043b921b29Sopenharmony_ci {
4053b921b29Sopenharmony_ci   jsimd_encode_mcu_AC_first_prepare_sse2(block, jpeg_natural_order_start,
4063b921b29Sopenharmony_ci                                          Sl, Al, values, zerobits);
4073b921b29Sopenharmony_ci@@ -1245,7 +1245,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
4083b921b29Sopenharmony_ci GLOBAL(int)
4093b921b29Sopenharmony_ci jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
4103b921b29Sopenharmony_ci                                    const int *jpeg_natural_order_start, int Sl,
4113b921b29Sopenharmony_ci-                                   int Al, JCOEF *absvalues, size_t *bits)
4123b921b29Sopenharmony_ci+                                   int Al, UJCOEF *absvalues, size_t *bits)
4133b921b29Sopenharmony_ci {
4143b921b29Sopenharmony_ci   return jsimd_encode_mcu_AC_refine_prepare_sse2(block,
4153b921b29Sopenharmony_ci                                                  jpeg_natural_order_start,
4163b921b29Sopenharmony_cidiff --git a/simd/jsimd.h b/simd/jsimd.h
4173b921b29Sopenharmony_ciindex 64747c6..abebbf4 100644
4183b921b29Sopenharmony_ci--- a/simd/jsimd.h
4193b921b29Sopenharmony_ci+++ b/simd/jsimd.h
4203b921b29Sopenharmony_ci@@ -5,7 +5,7 @@
4213b921b29Sopenharmony_ci  * Copyright (C) 2011, 2014-2016, 2018, 2020, D. R. Commander.
4223b921b29Sopenharmony_ci  * Copyright (C) 2013-2014, MIPS Technologies, Inc., California.
4233b921b29Sopenharmony_ci  * Copyright (C) 2014, Linaro Limited.
4243b921b29Sopenharmony_ci- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
4253b921b29Sopenharmony_ci+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
4263b921b29Sopenharmony_ci  * Copyright (C) 2016-2018, Loongson Technology Corporation Limited, BeiJing.
4273b921b29Sopenharmony_ci  * Copyright (C) 2020, Arm Limited.
4283b921b29Sopenharmony_ci  *
4293b921b29Sopenharmony_ci@@ -15,6 +15,8 @@
4303b921b29Sopenharmony_ci  *
4313b921b29Sopenharmony_ci  */
4323b921b29Sopenharmony_ci 
4333b921b29Sopenharmony_ci+#include <stdlib.h>
4343b921b29Sopenharmony_ci+
4353b921b29Sopenharmony_ci /* Bitmask for supported acceleration methods */
4363b921b29Sopenharmony_ci 
4373b921b29Sopenharmony_ci #define JSIMD_NONE     0x00
4383b921b29Sopenharmony_ci@@ -1243,16 +1245,16 @@ EXTERN(JOCTET *) jsimd_huff_encode_one_block_neon_slowtbl
4393b921b29Sopenharmony_ci /* Progressive Huffman encoding */
4403b921b29Sopenharmony_ci EXTERN(void) jsimd_encode_mcu_AC_first_prepare_sse2
4413b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
4423b921b29Sopenharmony_ci-   JCOEF *values, size_t *zerobits);
4433b921b29Sopenharmony_ci+   UJCOEF *values, size_t *zerobits);
4443b921b29Sopenharmony_ci 
4453b921b29Sopenharmony_ci EXTERN(void) jsimd_encode_mcu_AC_first_prepare_neon
4463b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
4473b921b29Sopenharmony_ci-   JCOEF *values, size_t *zerobits);
4483b921b29Sopenharmony_ci+   UJCOEF *values, size_t *zerobits);
4493b921b29Sopenharmony_ci 
4503b921b29Sopenharmony_ci EXTERN(int) jsimd_encode_mcu_AC_refine_prepare_sse2
4513b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
4523b921b29Sopenharmony_ci-   JCOEF *absvalues, size_t *bits);
4533b921b29Sopenharmony_ci+   UJCOEF *absvalues, size_t *bits);
4543b921b29Sopenharmony_ci 
4553b921b29Sopenharmony_ci EXTERN(int) jsimd_encode_mcu_AC_refine_prepare_neon
4563b921b29Sopenharmony_ci   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
4573b921b29Sopenharmony_ci-   JCOEF *absvalues, size_t *bits);
4583b921b29Sopenharmony_ci+   UJCOEF *absvalues, size_t *bits);
4593b921b29Sopenharmony_cidiff --git a/simd/mips/jsimd.c b/simd/mips/jsimd.c
4603b921b29Sopenharmony_ciindex d2546ee..9be0a96 100644
4613b921b29Sopenharmony_ci--- a/simd/mips/jsimd.c
4623b921b29Sopenharmony_ci+++ b/simd/mips/jsimd.c
4633b921b29Sopenharmony_ci@@ -4,7 +4,7 @@
4643b921b29Sopenharmony_ci  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
4653b921b29Sopenharmony_ci  * Copyright (C) 2009-2011, 2014, 2016, 2018, 2020, D. R. Commander.
4663b921b29Sopenharmony_ci  * Copyright (C) 2013-2014, MIPS Technologies, Inc., California.
4673b921b29Sopenharmony_ci- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
4683b921b29Sopenharmony_ci+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
4693b921b29Sopenharmony_ci  *
4703b921b29Sopenharmony_ci  * Based on the x86 SIMD extension for IJG JPEG library,
4713b921b29Sopenharmony_ci  * Copyright (C) 1999-2006, MIYASAKA Masaru.
4723b921b29Sopenharmony_ci@@ -1128,7 +1128,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
4733b921b29Sopenharmony_ci GLOBAL(void)
4743b921b29Sopenharmony_ci jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
4753b921b29Sopenharmony_ci                                   const int *jpeg_natural_order_start, int Sl,
4763b921b29Sopenharmony_ci-                                  int Al, JCOEF *values, size_t *zerobits)
4773b921b29Sopenharmony_ci+                                  int Al, UJCOEF *values, size_t *zerobits)
4783b921b29Sopenharmony_ci {
4793b921b29Sopenharmony_ci }
4803b921b29Sopenharmony_ci 
4813b921b29Sopenharmony_ci@@ -1141,7 +1141,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
4823b921b29Sopenharmony_ci GLOBAL(int)
4833b921b29Sopenharmony_ci jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
4843b921b29Sopenharmony_ci                                    const int *jpeg_natural_order_start, int Sl,
4853b921b29Sopenharmony_ci-                                   int Al, JCOEF *absvalues, size_t *bits)
4863b921b29Sopenharmony_ci+                                   int Al, UJCOEF *absvalues, size_t *bits)
4873b921b29Sopenharmony_ci {
4883b921b29Sopenharmony_ci   return 0;
4893b921b29Sopenharmony_ci }
4903b921b29Sopenharmony_cidiff --git a/simd/mips64/jsimd.c b/simd/mips64/jsimd.c
4913b921b29Sopenharmony_ciindex e8f1af5..bb9785b 100644
4923b921b29Sopenharmony_ci--- a/simd/mips64/jsimd.c
4933b921b29Sopenharmony_ci+++ b/simd/mips64/jsimd.c
4943b921b29Sopenharmony_ci@@ -851,7 +851,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
4953b921b29Sopenharmony_ci GLOBAL(void)
4963b921b29Sopenharmony_ci jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
4973b921b29Sopenharmony_ci                                   const int *jpeg_natural_order_start, int Sl,
4983b921b29Sopenharmony_ci-                                  int Al, JCOEF *values, size_t *zerobits)
4993b921b29Sopenharmony_ci+                                  int Al, UJCOEF *values, size_t *zerobits)
5003b921b29Sopenharmony_ci {
5013b921b29Sopenharmony_ci }
5023b921b29Sopenharmony_ci 
5033b921b29Sopenharmony_ci@@ -864,7 +864,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
5043b921b29Sopenharmony_ci GLOBAL(int)
5053b921b29Sopenharmony_ci jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
5063b921b29Sopenharmony_ci                                    const int *jpeg_natural_order_start, int Sl,
5073b921b29Sopenharmony_ci-                                   int Al, JCOEF *absvalues, size_t *bits)
5083b921b29Sopenharmony_ci+                                   int Al, UJCOEF *absvalues, size_t *bits)
5093b921b29Sopenharmony_ci {
5103b921b29Sopenharmony_ci   return 0;
5113b921b29Sopenharmony_ci }
5123b921b29Sopenharmony_cidiff --git a/simd/powerpc/jsimd.c b/simd/powerpc/jsimd.c
5133b921b29Sopenharmony_ciindex d0d3981..8affc5e 100644
5143b921b29Sopenharmony_ci--- a/simd/powerpc/jsimd.c
5153b921b29Sopenharmony_ci+++ b/simd/powerpc/jsimd.c
5163b921b29Sopenharmony_ci@@ -3,7 +3,7 @@
5173b921b29Sopenharmony_ci  *
5183b921b29Sopenharmony_ci  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5193b921b29Sopenharmony_ci  * Copyright (C) 2009-2011, 2014-2016, 2018, D. R. Commander.
5203b921b29Sopenharmony_ci- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
5213b921b29Sopenharmony_ci+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
5223b921b29Sopenharmony_ci  *
5233b921b29Sopenharmony_ci  * Based on the x86 SIMD extension for IJG JPEG library,
5243b921b29Sopenharmony_ci  * Copyright (C) 1999-2006, MIYASAKA Masaru.
5253b921b29Sopenharmony_ci@@ -853,7 +853,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
5263b921b29Sopenharmony_ci GLOBAL(void)
5273b921b29Sopenharmony_ci jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
5283b921b29Sopenharmony_ci                                   const int *jpeg_natural_order_start, int Sl,
5293b921b29Sopenharmony_ci-                                  int Al, JCOEF *values, size_t *zerobits)
5303b921b29Sopenharmony_ci+                                  int Al, UJCOEF *values, size_t *zerobits)
5313b921b29Sopenharmony_ci {
5323b921b29Sopenharmony_ci }
5333b921b29Sopenharmony_ci 
5343b921b29Sopenharmony_ci@@ -866,7 +866,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
5353b921b29Sopenharmony_ci GLOBAL(int)
5363b921b29Sopenharmony_ci jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
5373b921b29Sopenharmony_ci                                    const int *jpeg_natural_order_start, int Sl,
5383b921b29Sopenharmony_ci-                                   int Al, JCOEF *absvalues, size_t *bits)
5393b921b29Sopenharmony_ci+                                   int Al, UJCOEF *absvalues, size_t *bits)
5403b921b29Sopenharmony_ci {
5413b921b29Sopenharmony_ci   return 0;
5423b921b29Sopenharmony_ci }
5433b921b29Sopenharmony_cidiff --git a/simd/x86_64/jsimd.c b/simd/x86_64/jsimd.c
5443b921b29Sopenharmony_ciindex eb76679..1ede507 100644
5453b921b29Sopenharmony_ci--- a/simd/x86_64/jsimd.c
5463b921b29Sopenharmony_ci+++ b/simd/x86_64/jsimd.c
5473b921b29Sopenharmony_ci@@ -3,7 +3,7 @@
5483b921b29Sopenharmony_ci  *
5493b921b29Sopenharmony_ci  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5503b921b29Sopenharmony_ci  * Copyright (C) 2009-2011, 2014, 2016, 2018, D. R. Commander.
5513b921b29Sopenharmony_ci- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
5523b921b29Sopenharmony_ci+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
5533b921b29Sopenharmony_ci  *
5543b921b29Sopenharmony_ci  * Based on the x86 SIMD extension for IJG JPEG library,
5553b921b29Sopenharmony_ci  * Copyright (C) 1999-2006, MIYASAKA Masaru.
5563b921b29Sopenharmony_ci@@ -1040,7 +1040,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
5573b921b29Sopenharmony_ci GLOBAL(void)
5583b921b29Sopenharmony_ci jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
5593b921b29Sopenharmony_ci                                   const int *jpeg_natural_order_start, int Sl,
5603b921b29Sopenharmony_ci-                                  int Al, JCOEF *values, size_t *zerobits)
5613b921b29Sopenharmony_ci+                                  int Al, UJCOEF *values, size_t *zerobits)
5623b921b29Sopenharmony_ci {
5633b921b29Sopenharmony_ci   jsimd_encode_mcu_AC_first_prepare_sse2(block, jpeg_natural_order_start,
5643b921b29Sopenharmony_ci                                          Sl, Al, values, zerobits);
5653b921b29Sopenharmony_ci@@ -1064,7 +1064,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
5663b921b29Sopenharmony_ci GLOBAL(int)
5673b921b29Sopenharmony_ci jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
5683b921b29Sopenharmony_ci                                    const int *jpeg_natural_order_start, int Sl,
5693b921b29Sopenharmony_ci-                                   int Al, JCOEF *absvalues, size_t *bits)
5703b921b29Sopenharmony_ci+                                   int Al, UJCOEF *absvalues, size_t *bits)
5713b921b29Sopenharmony_ci {
5723b921b29Sopenharmony_ci   return jsimd_encode_mcu_AC_refine_prepare_sse2(block,
5733b921b29Sopenharmony_ci                                                  jpeg_natural_order_start,
5743b921b29Sopenharmony_cidiff --git a/tjbench.c b/tjbench.c
5753b921b29Sopenharmony_ciindex 156c906..8c83025 100644
5763b921b29Sopenharmony_ci--- a/tjbench.c
5773b921b29Sopenharmony_ci+++ b/tjbench.c
5783b921b29Sopenharmony_ci@@ -592,10 +592,16 @@ static int decompTest(char *fileName)
5793b921b29Sopenharmony_ci     if ((flags & TJFLAG_NOREALLOC) != 0 &&
5803b921b29Sopenharmony_ci         (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter))
5813b921b29Sopenharmony_ci       for (i = 0; i < ntilesw * ntilesh; i++) {
5823b921b29Sopenharmony_ci-        if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
5833b921b29Sopenharmony_ci+        unsigned long jpegBufSize;
5843b921b29Sopenharmony_ci+
5853b921b29Sopenharmony_ci+        if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
5863b921b29Sopenharmony_ci+            xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270)
5873b921b29Sopenharmony_ci+          jpegBufSize = tjBufSize(tileh, tilew, subsamp);
5883b921b29Sopenharmony_ci+        else
5893b921b29Sopenharmony_ci+          jpegBufSize = tjBufSize(tilew, tileh, subsamp);
5903b921b29Sopenharmony_ci+        if (jpegBufSize > (unsigned long)INT_MAX)
5913b921b29Sopenharmony_ci           THROW("getting buffer size", "Image is too large");
5923b921b29Sopenharmony_ci-        if ((jpegBuf[i] = (unsigned char *)
5933b921b29Sopenharmony_ci-                          tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
5943b921b29Sopenharmony_ci+        if ((jpegBuf[i] = (unsigned char *)tjAlloc(jpegBufSize)) == NULL)
5953b921b29Sopenharmony_ci           THROW_UNIX("allocating JPEG tiles");
5963b921b29Sopenharmony_ci       }
5973b921b29Sopenharmony_ci 
5983b921b29Sopenharmony_cidiff --git a/tjexample.c b/tjexample.c
5993b921b29Sopenharmony_ciindex a9cd865..2ac1fed 100644
6003b921b29Sopenharmony_ci--- a/tjexample.c
6013b921b29Sopenharmony_ci+++ b/tjexample.c
6023b921b29Sopenharmony_ci@@ -32,6 +32,7 @@
6033b921b29Sopenharmony_ci  * images using the TurboJPEG C API
6043b921b29Sopenharmony_ci  */
6053b921b29Sopenharmony_ci 
6063b921b29Sopenharmony_ci+#include <limits.h>
6073b921b29Sopenharmony_ci #include <stdio.h>
6083b921b29Sopenharmony_ci #include <stdlib.h>
6093b921b29Sopenharmony_ci #include <string.h>
6103b921b29Sopenharmony_ci@@ -273,6 +274,8 @@ int main(int argc, char **argv)
6113b921b29Sopenharmony_ci     if (size == 0)
6123b921b29Sopenharmony_ci       THROW("determining input file size", "Input file contains no data");
6133b921b29Sopenharmony_ci     jpegSize = (unsigned long)size;
6143b921b29Sopenharmony_ci+    if (jpegSize > (unsigned long)INT_MAX)
6153b921b29Sopenharmony_ci+      THROW("allocating JPEG buffer", "Input file is too large");
6163b921b29Sopenharmony_ci     if ((jpegBuf = (unsigned char *)tjAlloc(jpegSize)) == NULL)
6173b921b29Sopenharmony_ci       THROW_UNIX("allocating JPEG buffer");
6183b921b29Sopenharmony_ci     if (fread(jpegBuf, jpegSize, 1, jpegFile) < 1)
6193b921b29Sopenharmony_ci@@ -330,8 +333,12 @@ int main(int argc, char **argv)
6203b921b29Sopenharmony_ci       outSubsamp = inSubsamp;
6213b921b29Sopenharmony_ci 
6223b921b29Sopenharmony_ci     pixelFormat = TJPF_BGRX;
6233b921b29Sopenharmony_ci-    if ((imgBuf = (unsigned char *)tjAlloc(width * height *
6243b921b29Sopenharmony_ci-                                           tjPixelSize[pixelFormat])) == NULL)
6253b921b29Sopenharmony_ci+    if ((unsigned long long)width * height * tjPixelSize[pixelFormat] >
6263b921b29Sopenharmony_ci+        (unsigned long long)((size_t)-1))
6273b921b29Sopenharmony_ci+      THROW("allocating uncompressed image buffer", "Image is too large");
6283b921b29Sopenharmony_ci+    if ((imgBuf =
6293b921b29Sopenharmony_ci+         (unsigned char *)malloc(sizeof(unsigned char) * width * height *
6303b921b29Sopenharmony_ci+                                 tjPixelSize[pixelFormat])) == NULL)
6313b921b29Sopenharmony_ci       THROW_UNIX("allocating uncompressed image buffer");
6323b921b29Sopenharmony_ci 
6333b921b29Sopenharmony_ci     if (tjDecompress2(tjInstance, jpegBuf, jpegSize, imgBuf, width, 0, height,
6343b921b29Sopenharmony_cidiff --git a/tjunittest.c b/tjunittest.c
6353b921b29Sopenharmony_ciindex f59939f..38ada63 100644
6363b921b29Sopenharmony_ci--- a/tjunittest.c
6373b921b29Sopenharmony_ci+++ b/tjunittest.c
6383b921b29Sopenharmony_ci@@ -33,6 +33,7 @@
6393b921b29Sopenharmony_ci #include <stdio.h>
6403b921b29Sopenharmony_ci #include <stdlib.h>
6413b921b29Sopenharmony_ci #include <string.h>
6423b921b29Sopenharmony_ci+#include <limits.h>
6433b921b29Sopenharmony_ci #include <errno.h>
6443b921b29Sopenharmony_ci #include "tjutil.h"
6453b921b29Sopenharmony_ci #include "turbojpeg.h"
6463b921b29Sopenharmony_ci@@ -566,11 +567,16 @@ bailout:
6473b921b29Sopenharmony_ci     THROW(#function " overflow"); \
6483b921b29Sopenharmony_ci }
6493b921b29Sopenharmony_ci #endif
6503b921b29Sopenharmony_ci+#define CHECKSIZEINT(function) { \
6513b921b29Sopenharmony_ci+  if (intsize != -1 || !strcmp(tjGetErrorStr2(NULL), "No error")) \
6523b921b29Sopenharmony_ci+    THROW(#function " overflow"); \
6533b921b29Sopenharmony_ci+}
6543b921b29Sopenharmony_ci 
6553b921b29Sopenharmony_ci static void overflowTest(void)
6563b921b29Sopenharmony_ci {
6573b921b29Sopenharmony_ci   /* Ensure that the various buffer size functions don't overflow */
6583b921b29Sopenharmony_ci   unsigned long size;
6593b921b29Sopenharmony_ci+  int intsize;
6603b921b29Sopenharmony_ci 
6613b921b29Sopenharmony_ci   size = tjBufSize(26755, 26755, TJSAMP_444);
6623b921b29Sopenharmony_ci   CHECKSIZE(tjBufSize());
6633b921b29Sopenharmony_ci@@ -584,6 +590,10 @@ static void overflowTest(void)
6643b921b29Sopenharmony_ci   CHECKSIZE(tjBufSizeYUV());
6653b921b29Sopenharmony_ci   size = tjPlaneSizeYUV(0, 65536, 0, 65536, TJSAMP_444);
6663b921b29Sopenharmony_ci   CHECKSIZE(tjPlaneSizeYUV());
6673b921b29Sopenharmony_ci+  intsize = tjPlaneWidth(0, INT_MAX, TJSAMP_420);
6683b921b29Sopenharmony_ci+  CHECKSIZEINT(tjPlaneWidth());
6693b921b29Sopenharmony_ci+  intsize = tjPlaneHeight(0, INT_MAX, TJSAMP_420);
6703b921b29Sopenharmony_ci+  CHECKSIZEINT(tjPlaneHeight());
6713b921b29Sopenharmony_ci 
6723b921b29Sopenharmony_ci bailout:
6733b921b29Sopenharmony_ci   return;
6743b921b29Sopenharmony_cidiff --git a/turbojpeg-jni.c b/turbojpeg-jni.c
6753b921b29Sopenharmony_ciindex 1b728e3..053edbd 100644
6763b921b29Sopenharmony_ci--- a/turbojpeg-jni.c
6773b921b29Sopenharmony_ci+++ b/turbojpeg-jni.c
6783b921b29Sopenharmony_ci@@ -26,6 +26,7 @@
6793b921b29Sopenharmony_ci  * POSSIBILITY OF SUCH DAMAGE.
6803b921b29Sopenharmony_ci  */
6813b921b29Sopenharmony_ci 
6823b921b29Sopenharmony_ci+#include <limits.h>
6833b921b29Sopenharmony_ci #include <stdlib.h>
6843b921b29Sopenharmony_ci #include <string.h>
6853b921b29Sopenharmony_ci #include "turbojpeg.h"
6863b921b29Sopenharmony_ci@@ -128,24 +129,28 @@ bailout:
6873b921b29Sopenharmony_ci JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
6883b921b29Sopenharmony_ci   (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
6893b921b29Sopenharmony_ci {
6903b921b29Sopenharmony_ci-  jint retval = (jint)tjBufSize(width, height, jpegSubsamp);
6913b921b29Sopenharmony_ci+  unsigned long retval = tjBufSize(width, height, jpegSubsamp);
6923b921b29Sopenharmony_ci 
6933b921b29Sopenharmony_ci-  if (retval == -1) THROW_ARG(tjGetErrorStr());
6943b921b29Sopenharmony_ci+  if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
6953b921b29Sopenharmony_ci+  if (retval > (unsigned long)INT_MAX)
6963b921b29Sopenharmony_ci+    THROW_ARG("Image is too large");
6973b921b29Sopenharmony_ci 
6983b921b29Sopenharmony_ci bailout:
6993b921b29Sopenharmony_ci-  return retval;
7003b921b29Sopenharmony_ci+  return (jint)retval;
7013b921b29Sopenharmony_ci }
7023b921b29Sopenharmony_ci 
7033b921b29Sopenharmony_ci /* TurboJPEG 1.4.x: TJ::bufSizeYUV() */
7043b921b29Sopenharmony_ci JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
7053b921b29Sopenharmony_ci   (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp)
7063b921b29Sopenharmony_ci {
7073b921b29Sopenharmony_ci-  jint retval = (jint)tjBufSizeYUV2(width, pad, height, subsamp);
7083b921b29Sopenharmony_ci+  unsigned long retval = tjBufSizeYUV2(width, align, height, subsamp);
7093b921b29Sopenharmony_ci 
7103b921b29Sopenharmony_ci-  if (retval == -1) THROW_ARG(tjGetErrorStr());
7113b921b29Sopenharmony_ci+  if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
7123b921b29Sopenharmony_ci+  if (retval > (unsigned long)INT_MAX)
7133b921b29Sopenharmony_ci+    THROW_ARG("Image is too large");
7143b921b29Sopenharmony_ci 
7153b921b29Sopenharmony_ci bailout:
7163b921b29Sopenharmony_ci-  return retval;
7173b921b29Sopenharmony_ci+  return (jint)retval;
7183b921b29Sopenharmony_ci }
7193b921b29Sopenharmony_ci 
7203b921b29Sopenharmony_ci /* TurboJPEG 1.2.x: TJ::bufSizeYUV() */
7213b921b29Sopenharmony_ci@@ -162,13 +167,15 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII
7223b921b29Sopenharmony_ci   (JNIEnv *env, jclass cls, jint componentID, jint width, jint stride,
7233b921b29Sopenharmony_ci    jint height, jint subsamp)
7243b921b29Sopenharmony_ci {
7253b921b29Sopenharmony_ci-  jint retval = (jint)tjPlaneSizeYUV(componentID, width, stride, height,
7263b921b29Sopenharmony_ci-                                     subsamp);
7273b921b29Sopenharmony_ci+  unsigned long retval = tjPlaneSizeYUV(componentID, width, stride, height,
7283b921b29Sopenharmony_ci+                                        subsamp);
7293b921b29Sopenharmony_ci 
7303b921b29Sopenharmony_ci-  if (retval == -1) THROW_ARG(tjGetErrorStr());
7313b921b29Sopenharmony_ci+  if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
7323b921b29Sopenharmony_ci+  if (retval > (unsigned long)INT_MAX)
7333b921b29Sopenharmony_ci+    THROW_ARG("Image is too large");
7343b921b29Sopenharmony_ci 
7353b921b29Sopenharmony_ci bailout:
7363b921b29Sopenharmony_ci-  return retval;
7373b921b29Sopenharmony_ci+  return (jint)retval;
7383b921b29Sopenharmony_ci }
7393b921b29Sopenharmony_ci 
7403b921b29Sopenharmony_ci /* TurboJPEG 1.4.x: TJ::planeWidth() */
7413b921b29Sopenharmony_ci@@ -1176,6 +1183,10 @@ JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf
7423b921b29Sopenharmony_ci   for (i = 0; i < n; i++) {
7433b921b29Sopenharmony_ci     int w = jpegWidth, h = jpegHeight;
7443b921b29Sopenharmony_ci 
7453b921b29Sopenharmony_ci+    if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
7463b921b29Sopenharmony_ci+        t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
7473b921b29Sopenharmony_ci+      w = jpegHeight;  h = jpegWidth;
7483b921b29Sopenharmony_ci+    }
7493b921b29Sopenharmony_ci     if (t[i].r.w != 0) w = t[i].r.w;
7503b921b29Sopenharmony_ci     if (t[i].r.h != 0) h = t[i].r.h;
7513b921b29Sopenharmony_ci     BAILIF0(jdstBufs[i] = (*env)->GetObjectArrayElement(env, dstobjs, i));
7523b921b29Sopenharmony_cidiff --git a/turbojpeg.c b/turbojpeg.c
7533b921b29Sopenharmony_ciindex 47c5999..6c425be 100644
7543b921b29Sopenharmony_ci--- a/turbojpeg.c
7553b921b29Sopenharmony_ci+++ b/turbojpeg.c
7563b921b29Sopenharmony_ci@@ -33,6 +33,7 @@
7573b921b29Sopenharmony_ci #include <stdio.h>
7583b921b29Sopenharmony_ci #include <stdlib.h>
7593b921b29Sopenharmony_ci #include <ctype.h>
7603b921b29Sopenharmony_ci+#include <limits.h>
7613b921b29Sopenharmony_ci #include <jinclude.h>
7623b921b29Sopenharmony_ci #define JPEG_INTERNALS
7633b921b29Sopenharmony_ci #include <jpeglib.h>
7643b921b29Sopenharmony_ci@@ -599,7 +600,8 @@ DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int subsamp)
7653b921b29Sopenharmony_ci 
7663b921b29Sopenharmony_ci DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp)
7673b921b29Sopenharmony_ci {
7683b921b29Sopenharmony_ci-  int pw, nc, retval = 0;
7693b921b29Sopenharmony_ci+  unsigned long long pw, retval = 0;
7703b921b29Sopenharmony_ci+  int nc;
7713b921b29Sopenharmony_ci 
7723b921b29Sopenharmony_ci   if (width < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
7733b921b29Sopenharmony_ci     THROWG("tjPlaneWidth(): Invalid argument");
7743b921b29Sopenharmony_ci@@ -613,14 +615,18 @@ DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp)
7753b921b29Sopenharmony_ci   else
7763b921b29Sopenharmony_ci     retval = pw * 8 / tjMCUWidth[subsamp];
7773b921b29Sopenharmony_ci 
7783b921b29Sopenharmony_ci+  if (retval > (unsigned long long)INT_MAX)
7793b921b29Sopenharmony_ci+    THROWG("tjPlaneWidth(): Width is too large");
7803b921b29Sopenharmony_ci+
7813b921b29Sopenharmony_ci bailout:
7823b921b29Sopenharmony_ci-  return retval;
7833b921b29Sopenharmony_ci+  return (int)retval;
7843b921b29Sopenharmony_ci }
7853b921b29Sopenharmony_ci 
7863b921b29Sopenharmony_ci 
7873b921b29Sopenharmony_ci DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp)
7883b921b29Sopenharmony_ci {
7893b921b29Sopenharmony_ci-  int ph, nc, retval = 0;
7903b921b29Sopenharmony_ci+  unsigned long long ph, retval = 0;
7913b921b29Sopenharmony_ci+  int nc;
7923b921b29Sopenharmony_ci 
7933b921b29Sopenharmony_ci   if (height < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
7943b921b29Sopenharmony_ci     THROWG("tjPlaneHeight(): Invalid argument");
7953b921b29Sopenharmony_ci@@ -634,8 +640,11 @@ DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp)
7963b921b29Sopenharmony_ci   else
7973b921b29Sopenharmony_ci     retval = ph * 8 / tjMCUHeight[subsamp];
7983b921b29Sopenharmony_ci 
7993b921b29Sopenharmony_ci+  if (retval > (unsigned long long)INT_MAX)
8003b921b29Sopenharmony_ci+    THROWG("tjPlaneHeight(): Height is too large");
8013b921b29Sopenharmony_ci+
8023b921b29Sopenharmony_ci bailout:
8033b921b29Sopenharmony_ci-  return retval;
8043b921b29Sopenharmony_ci+  return (int)retval;
8053b921b29Sopenharmony_ci }
8063b921b29Sopenharmony_ci 
8073b921b29Sopenharmony_ci 
8083b921b29Sopenharmony_ci@@ -1985,6 +1994,10 @@ DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
8093b921b29Sopenharmony_ci 
8103b921b29Sopenharmony_ci     if (!xinfo[i].crop) {
8113b921b29Sopenharmony_ci       w = dinfo->image_width;  h = dinfo->image_height;
8123b921b29Sopenharmony_ci+      if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
8133b921b29Sopenharmony_ci+          t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
8143b921b29Sopenharmony_ci+        w = dinfo->image_height;  h = dinfo->image_width;
8153b921b29Sopenharmony_ci+      }
8163b921b29Sopenharmony_ci     } else {
8173b921b29Sopenharmony_ci       w = xinfo[i].crop_width;  h = xinfo[i].crop_height;
8183b921b29Sopenharmony_ci     }
8193b921b29Sopenharmony_cidiff --git a/libjpeg-turbo-2.1.x-bugfix.patch b/libjpeg-turbo-2.1.x-bugfix.patch
8203b921b29Sopenharmony_ciindex c4c0b10..2eac16b 100644
8213b921b29Sopenharmony_ci--- a/libjpeg-turbo-2.1.x-bugfix.patch
8223b921b29Sopenharmony_ci+++ b/libjpeg-turbo-2.1.x-bugfix.patch
8233b921b29Sopenharmony_ci@@ -1,804 +0,0 @@
8243b921b29Sopenharmony_ci-diff --git a/java/TJUnitTest.java b/java/TJUnitTest.java
8253b921b29Sopenharmony_ci-index 91ad5fd..07c59d8 100644
8263b921b29Sopenharmony_ci---- a/java/TJUnitTest.java
8273b921b29Sopenharmony_ci-+++ b/java/TJUnitTest.java
8283b921b29Sopenharmony_ci-@@ -837,6 +837,54 @@ final class TJUnitTest {
8293b921b29Sopenharmony_ci-     if (tjd != null) tjd.close();
8303b921b29Sopenharmony_ci-   }
8313b921b29Sopenharmony_ci- 
8323b921b29Sopenharmony_ci-+  static void overflowTest() throws Exception {
8333b921b29Sopenharmony_ci-+    /* Ensure that the various buffer size methods don't overflow */
8343b921b29Sopenharmony_ci-+    int size = 0;
8353b921b29Sopenharmony_ci-+    boolean exception = false;
8363b921b29Sopenharmony_ci-+    try {
8373b921b29Sopenharmony_ci-+      exception = false;
8383b921b29Sopenharmony_ci-+      size = TJ.bufSize(18919, 18919, TJ.SAMP_444);
8393b921b29Sopenharmony_ci-+    } catch (Exception e) { exception = true; }
8403b921b29Sopenharmony_ci-+    if (!exception || size != 0)
8413b921b29Sopenharmony_ci-+      throw new Exception("TJ.bufSize() overflow");
8423b921b29Sopenharmony_ci-+    try {
8433b921b29Sopenharmony_ci-+      exception = false;
8443b921b29Sopenharmony_ci-+      size = TJ.bufSizeYUV(26755, 1, 26755, TJ.SAMP_444);
8453b921b29Sopenharmony_ci-+    } catch (Exception e) { exception = true; }
8463b921b29Sopenharmony_ci-+    if (!exception || size != 0)
8473b921b29Sopenharmony_ci-+      throw new Exception("TJ.bufSizeYUV() overflow");
8483b921b29Sopenharmony_ci-+    try {
8493b921b29Sopenharmony_ci-+      exception = false;
8503b921b29Sopenharmony_ci-+      size = TJ.bufSizeYUV(26754, 3, 26754, TJ.SAMP_444);
8513b921b29Sopenharmony_ci-+    } catch (Exception e) { exception = true; }
8523b921b29Sopenharmony_ci-+    if (!exception || size != 0)
8533b921b29Sopenharmony_ci-+      throw new Exception("TJ.bufSizeYUV() overflow");
8543b921b29Sopenharmony_ci-+    try {
8553b921b29Sopenharmony_ci-+      exception = false;
8563b921b29Sopenharmony_ci-+      size = TJ.bufSizeYUV(26754, -1, 26754, TJ.SAMP_444);
8573b921b29Sopenharmony_ci-+    } catch (Exception e) { exception = true; }
8583b921b29Sopenharmony_ci-+    if (!exception || size != 0)
8593b921b29Sopenharmony_ci-+      throw new Exception("TJ.bufSizeYUV() overflow");
8603b921b29Sopenharmony_ci-+    try {
8613b921b29Sopenharmony_ci-+      exception = false;
8623b921b29Sopenharmony_ci-+      size = TJ.planeSizeYUV(0, 46341, 0, 46341, TJ.SAMP_444);
8633b921b29Sopenharmony_ci-+    } catch (Exception e) { exception = true; }
8643b921b29Sopenharmony_ci-+    if (!exception || size != 0)
8653b921b29Sopenharmony_ci-+      throw new Exception("TJ.planeSizeYUV() overflow");
8663b921b29Sopenharmony_ci-+    try {
8673b921b29Sopenharmony_ci-+      exception = false;
8683b921b29Sopenharmony_ci-+      size = TJ.planeWidth(0, Integer.MAX_VALUE, TJ.SAMP_420);
8693b921b29Sopenharmony_ci-+    } catch (Exception e) { exception = true; }
8703b921b29Sopenharmony_ci-+    if (!exception || size != 0)
8713b921b29Sopenharmony_ci-+      throw new Exception("TJ.planeWidth() overflow");
8723b921b29Sopenharmony_ci-+    try {
8733b921b29Sopenharmony_ci-+      exception = false;
8743b921b29Sopenharmony_ci-+      size = TJ.planeHeight(0, Integer.MAX_VALUE, TJ.SAMP_420);
8753b921b29Sopenharmony_ci-+    } catch (Exception e) { exception = true; }
8763b921b29Sopenharmony_ci-+    if (!exception || size != 0)
8773b921b29Sopenharmony_ci-+      throw new Exception("TJ.planeHeight() overflow");
8783b921b29Sopenharmony_ci-+  }
8793b921b29Sopenharmony_ci-+
8803b921b29Sopenharmony_ci-   static void bufSizeTest() throws Exception {
8813b921b29Sopenharmony_ci-     int w, h, i, subsamp;
8823b921b29Sopenharmony_ci-     byte[] srcBuf, dstBuf = null;
8833b921b29Sopenharmony_ci-@@ -912,6 +960,7 @@ final class TJUnitTest {
8843b921b29Sopenharmony_ci-       }
8853b921b29Sopenharmony_ci-       if (doYUV)
8863b921b29Sopenharmony_ci-         FORMATS_4BYTE[4] = -1;
8873b921b29Sopenharmony_ci-+      overflowTest();
8883b921b29Sopenharmony_ci-       doTest(35, 39, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_444,
8893b921b29Sopenharmony_ci-              testName);
8903b921b29Sopenharmony_ci-       doTest(39, 41, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_444,
8913b921b29Sopenharmony_ci-diff --git a/jchuff.h b/jchuff.h
8923b921b29Sopenharmony_ci-index 314a232..da7809a 100644
8933b921b29Sopenharmony_ci---- a/jchuff.h
8943b921b29Sopenharmony_ci-+++ b/jchuff.h
8953b921b29Sopenharmony_ci-@@ -3,8 +3,8 @@
8963b921b29Sopenharmony_ci-  *
8973b921b29Sopenharmony_ci-  * This file was part of the Independent JPEG Group's software:
8983b921b29Sopenharmony_ci-  * Copyright (C) 1991-1997, Thomas G. Lane.
8993b921b29Sopenharmony_ci-- * It was modified by The libjpeg-turbo Project to include only code relevant
9003b921b29Sopenharmony_ci-- * to libjpeg-turbo.
9013b921b29Sopenharmony_ci-+ * libjpeg-turbo Modifications:
9023b921b29Sopenharmony_ci-+ * Copyright (C) 2022, D. R. Commander.
9033b921b29Sopenharmony_ci-  * For conditions of distribution and use, see the accompanying README.ijg
9043b921b29Sopenharmony_ci-  * file.
9053b921b29Sopenharmony_ci-  *
9063b921b29Sopenharmony_ci-@@ -25,6 +25,14 @@
9073b921b29Sopenharmony_ci- #define MAX_COEF_BITS  14
9083b921b29Sopenharmony_ci- #endif
9093b921b29Sopenharmony_ci- 
9103b921b29Sopenharmony_ci-+/* The progressive Huffman encoder uses an unsigned 16-bit data type to store
9113b921b29Sopenharmony_ci-+ * absolute values of coefficients, because it is possible to inject a
9123b921b29Sopenharmony_ci-+ * coefficient value of -32768 into the encoder by attempting to transform a
9133b921b29Sopenharmony_ci-+ * malformed 12-bit JPEG image, and the absolute value of -32768 would overflow
9143b921b29Sopenharmony_ci-+ * a signed 16-bit integer.
9153b921b29Sopenharmony_ci-+ */
9163b921b29Sopenharmony_ci-+typedef unsigned short UJCOEF;
9173b921b29Sopenharmony_ci-+
9183b921b29Sopenharmony_ci- /* Derived data constructed for each Huffman table */
9193b921b29Sopenharmony_ci- 
9203b921b29Sopenharmony_ci- typedef struct {
9213b921b29Sopenharmony_ci-diff --git a/jcphuff.c b/jcphuff.c
9223b921b29Sopenharmony_ci-index 1101987..7c9d7cf 100644
9233b921b29Sopenharmony_ci---- a/jcphuff.c
9243b921b29Sopenharmony_ci-+++ b/jcphuff.c
9253b921b29Sopenharmony_ci-@@ -5,7 +5,7 @@
9263b921b29Sopenharmony_ci-  * Copyright (C) 1995-1997, Thomas G. Lane.
9273b921b29Sopenharmony_ci-  * libjpeg-turbo Modifications:
9283b921b29Sopenharmony_ci-  * Copyright (C) 2011, 2015, 2018, 2021, D. R. Commander.
9293b921b29Sopenharmony_ci-- * Copyright (C) 2016, 2018, Matthieu Darbois.
9303b921b29Sopenharmony_ci-+ * Copyright (C) 2016, 2018, 2022, Matthieu Darbois.
9313b921b29Sopenharmony_ci-  * Copyright (C) 2020, Arm Limited.
9323b921b29Sopenharmony_ci-  * Copyright (C) 2021, Alex Richardson.
9333b921b29Sopenharmony_ci-  * For conditions of distribution and use, see the accompanying README.ijg
9343b921b29Sopenharmony_ci-@@ -83,11 +83,11 @@ typedef struct {
9353b921b29Sopenharmony_ci-   /* Pointer to routine to prepare data for encode_mcu_AC_first() */
9363b921b29Sopenharmony_ci-   void (*AC_first_prepare) (const JCOEF *block,
9373b921b29Sopenharmony_ci-                             const int *jpeg_natural_order_start, int Sl,
9383b921b29Sopenharmony_ci--                            int Al, JCOEF *values, size_t *zerobits);
9393b921b29Sopenharmony_ci-+                            int Al, UJCOEF *values, size_t *zerobits);
9403b921b29Sopenharmony_ci-   /* Pointer to routine to prepare data for encode_mcu_AC_refine() */
9413b921b29Sopenharmony_ci-   int (*AC_refine_prepare) (const JCOEF *block,
9423b921b29Sopenharmony_ci-                             const int *jpeg_natural_order_start, int Sl,
9433b921b29Sopenharmony_ci--                            int Al, JCOEF *absvalues, size_t *bits);
9443b921b29Sopenharmony_ci-+                            int Al, UJCOEF *absvalues, size_t *bits);
9453b921b29Sopenharmony_ci- 
9463b921b29Sopenharmony_ci-   /* Mode flag: TRUE for optimization, FALSE for actual data output */
9473b921b29Sopenharmony_ci-   boolean gather_statistics;
9483b921b29Sopenharmony_ci-@@ -157,14 +157,14 @@ METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo,
9493b921b29Sopenharmony_ci-                                        JBLOCKROW *MCU_data);
9503b921b29Sopenharmony_ci- METHODDEF(void) encode_mcu_AC_first_prepare
9513b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
9523b921b29Sopenharmony_ci--   JCOEF *values, size_t *zerobits);
9533b921b29Sopenharmony_ci-+   UJCOEF *values, size_t *zerobits);
9543b921b29Sopenharmony_ci- METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo,
9553b921b29Sopenharmony_ci-                                        JBLOCKROW *MCU_data);
9563b921b29Sopenharmony_ci- METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo,
9573b921b29Sopenharmony_ci-                                         JBLOCKROW *MCU_data);
9583b921b29Sopenharmony_ci- METHODDEF(int) encode_mcu_AC_refine_prepare
9593b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
9603b921b29Sopenharmony_ci--   JCOEF *absvalues, size_t *bits);
9613b921b29Sopenharmony_ci-+   UJCOEF *absvalues, size_t *bits);
9623b921b29Sopenharmony_ci- METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo,
9633b921b29Sopenharmony_ci-                                         JBLOCKROW *MCU_data);
9643b921b29Sopenharmony_ci- METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo);
9653b921b29Sopenharmony_ci-@@ -584,8 +584,8 @@ encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
9663b921b29Sopenharmony_ci-       continue; \
9673b921b29Sopenharmony_ci-     /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \
9683b921b29Sopenharmony_ci-     temp2 ^= temp; \
9693b921b29Sopenharmony_ci--    values[k] = temp; \
9703b921b29Sopenharmony_ci--    values[k + DCTSIZE2] = temp2; \
9713b921b29Sopenharmony_ci-+    values[k] = (UJCOEF)temp; \
9723b921b29Sopenharmony_ci-+    values[k + DCTSIZE2] = (UJCOEF)temp2; \
9733b921b29Sopenharmony_ci-     zerobits |= ((size_t)1U) << k; \
9743b921b29Sopenharmony_ci-   } \
9753b921b29Sopenharmony_ci- }
9763b921b29Sopenharmony_ci-@@ -593,7 +593,7 @@ encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
9773b921b29Sopenharmony_ci- METHODDEF(void)
9783b921b29Sopenharmony_ci- encode_mcu_AC_first_prepare(const JCOEF *block,
9793b921b29Sopenharmony_ci-                             const int *jpeg_natural_order_start, int Sl,
9803b921b29Sopenharmony_ci--                            int Al, JCOEF *values, size_t *bits)
9813b921b29Sopenharmony_ci-+                            int Al, UJCOEF *values, size_t *bits)
9823b921b29Sopenharmony_ci- {
9833b921b29Sopenharmony_ci-   register int k, temp, temp2;
9843b921b29Sopenharmony_ci-   size_t zerobits = 0U;
9853b921b29Sopenharmony_ci-@@ -666,9 +666,9 @@ encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
9863b921b29Sopenharmony_ci-   register int nbits, r;
9873b921b29Sopenharmony_ci-   int Sl = cinfo->Se - cinfo->Ss + 1;
9883b921b29Sopenharmony_ci-   int Al = cinfo->Al;
9893b921b29Sopenharmony_ci--  JCOEF values_unaligned[2 * DCTSIZE2 + 15];
9903b921b29Sopenharmony_ci--  JCOEF *values;
9913b921b29Sopenharmony_ci--  const JCOEF *cvalue;
9923b921b29Sopenharmony_ci-+  UJCOEF values_unaligned[2 * DCTSIZE2 + 15];
9933b921b29Sopenharmony_ci-+  UJCOEF *values;
9943b921b29Sopenharmony_ci-+  const UJCOEF *cvalue;
9953b921b29Sopenharmony_ci-   size_t zerobits;
9963b921b29Sopenharmony_ci-   size_t bits[8 / SIZEOF_SIZE_T];
9973b921b29Sopenharmony_ci- 
9983b921b29Sopenharmony_ci-@@ -681,7 +681,7 @@ encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
9993b921b29Sopenharmony_ci-       emit_restart(entropy, entropy->next_restart_num);
10003b921b29Sopenharmony_ci- 
10013b921b29Sopenharmony_ci- #ifdef WITH_SIMD
10023b921b29Sopenharmony_ci--  cvalue = values = (JCOEF *)PAD((JUINTPTR)values_unaligned, 16);
10033b921b29Sopenharmony_ci-+  cvalue = values = (UJCOEF *)PAD((size_t)values_unaligned, 16);
10043b921b29Sopenharmony_ci- #else
10053b921b29Sopenharmony_ci-   /* Not using SIMD, so alignment is not needed */
10063b921b29Sopenharmony_ci-   cvalue = values = values_unaligned;
10073b921b29Sopenharmony_ci-@@ -815,7 +815,7 @@ encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
10083b921b29Sopenharmony_ci-       zerobits |= ((size_t)1U) << k; \
10093b921b29Sopenharmony_ci-       signbits |= ((size_t)(temp2 + 1)) << k; \
10103b921b29Sopenharmony_ci-     } \
10113b921b29Sopenharmony_ci--    absvalues[k] = (JCOEF)temp; /* save abs value for main pass */ \
10123b921b29Sopenharmony_ci-+    absvalues[k] = (UJCOEF)temp; /* save abs value for main pass */ \
10133b921b29Sopenharmony_ci-     if (temp == 1) \
10143b921b29Sopenharmony_ci-       EOB = k + koffset;        /* EOB = index of last newly-nonzero coef */ \
10153b921b29Sopenharmony_ci-   } \
10163b921b29Sopenharmony_ci-@@ -824,7 +824,7 @@ encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
10173b921b29Sopenharmony_ci- METHODDEF(int)
10183b921b29Sopenharmony_ci- encode_mcu_AC_refine_prepare(const JCOEF *block,
10193b921b29Sopenharmony_ci-                              const int *jpeg_natural_order_start, int Sl,
10203b921b29Sopenharmony_ci--                             int Al, JCOEF *absvalues, size_t *bits)
10213b921b29Sopenharmony_ci-+                             int Al, UJCOEF *absvalues, size_t *bits)
10223b921b29Sopenharmony_ci- {
10233b921b29Sopenharmony_ci-   register int k, temp, temp2;
10243b921b29Sopenharmony_ci-   int EOB = 0;
10253b921b29Sopenharmony_ci-@@ -931,9 +931,9 @@ encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
10263b921b29Sopenharmony_ci-   unsigned int BR;
10273b921b29Sopenharmony_ci-   int Sl = cinfo->Se - cinfo->Ss + 1;
10283b921b29Sopenharmony_ci-   int Al = cinfo->Al;
10293b921b29Sopenharmony_ci--  JCOEF absvalues_unaligned[DCTSIZE2 + 15];
10303b921b29Sopenharmony_ci--  JCOEF *absvalues;
10313b921b29Sopenharmony_ci--  const JCOEF *cabsvalue, *EOBPTR;
10323b921b29Sopenharmony_ci-+  UJCOEF absvalues_unaligned[DCTSIZE2 + 15];
10333b921b29Sopenharmony_ci-+  UJCOEF *absvalues;
10343b921b29Sopenharmony_ci-+  const UJCOEF *cabsvalue, *EOBPTR;
10353b921b29Sopenharmony_ci-   size_t zerobits, signbits;
10363b921b29Sopenharmony_ci-   size_t bits[16 / SIZEOF_SIZE_T];
10373b921b29Sopenharmony_ci- 
10383b921b29Sopenharmony_ci-@@ -946,7 +946,7 @@ encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
10393b921b29Sopenharmony_ci-       emit_restart(entropy, entropy->next_restart_num);
10403b921b29Sopenharmony_ci- 
10413b921b29Sopenharmony_ci- #ifdef WITH_SIMD
10423b921b29Sopenharmony_ci--  cabsvalue = absvalues = (JCOEF *)PAD((JUINTPTR)absvalues_unaligned, 16);
10433b921b29Sopenharmony_ci-+  cabsvalue = absvalues = (UJCOEF *)PAD((size_t)absvalues_unaligned, 16);
10443b921b29Sopenharmony_ci- #else
10453b921b29Sopenharmony_ci-   /* Not using SIMD, so alignment is not needed */
10463b921b29Sopenharmony_ci-   cabsvalue = absvalues = absvalues_unaligned;
10473b921b29Sopenharmony_ci-diff --git a/jerror.c b/jerror.c
10483b921b29Sopenharmony_ci-index 936c4f5..aa0c6ee 100644
10493b921b29Sopenharmony_ci---- a/jerror.c
10503b921b29Sopenharmony_ci-+++ b/jerror.c
10513b921b29Sopenharmony_ci-@@ -27,6 +27,8 @@
10523b921b29Sopenharmony_ci- #include "jversion.h"
10533b921b29Sopenharmony_ci- #include "jerror.h"
10543b921b29Sopenharmony_ci- 
10553b921b29Sopenharmony_ci-+#include <stdlib.h>
10563b921b29Sopenharmony_ci-+
10573b921b29Sopenharmony_ci- #ifdef USE_WINDOWS_MESSAGEBOX
10583b921b29Sopenharmony_ci- #include <windows.h>
10593b921b29Sopenharmony_ci- #endif
10603b921b29Sopenharmony_ci-diff --git a/jsimd.h b/jsimd.h
10613b921b29Sopenharmony_ci-index 6c20365..f3a87ee 100644
10623b921b29Sopenharmony_ci---- a/jsimd.h
10633b921b29Sopenharmony_ci-+++ b/jsimd.h
10643b921b29Sopenharmony_ci-@@ -114,10 +114,10 @@ EXTERN(int) jsimd_can_encode_mcu_AC_first_prepare(void);
10653b921b29Sopenharmony_ci- 
10663b921b29Sopenharmony_ci- EXTERN(void) jsimd_encode_mcu_AC_first_prepare
10673b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
10683b921b29Sopenharmony_ci--   JCOEF *values, size_t *zerobits);
10693b921b29Sopenharmony_ci-+   UJCOEF *values, size_t *zerobits);
10703b921b29Sopenharmony_ci- 
10713b921b29Sopenharmony_ci- EXTERN(int) jsimd_can_encode_mcu_AC_refine_prepare(void);
10723b921b29Sopenharmony_ci- 
10733b921b29Sopenharmony_ci- EXTERN(int) jsimd_encode_mcu_AC_refine_prepare
10743b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
10753b921b29Sopenharmony_ci--   JCOEF *absvalues, size_t *bits);
10763b921b29Sopenharmony_ci-+   UJCOEF *absvalues, size_t *bits);
10773b921b29Sopenharmony_ci-diff --git a/jsimd_none.c b/jsimd_none.c
10783b921b29Sopenharmony_ci-index 5b38a9f..a25db73 100644
10793b921b29Sopenharmony_ci---- a/jsimd_none.c
10803b921b29Sopenharmony_ci-+++ b/jsimd_none.c
10813b921b29Sopenharmony_ci-@@ -2,8 +2,8 @@
10823b921b29Sopenharmony_ci-  * jsimd_none.c
10833b921b29Sopenharmony_ci-  *
10843b921b29Sopenharmony_ci-  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
10853b921b29Sopenharmony_ci-- * Copyright (C) 2009-2011, 2014, D. R. Commander.
10863b921b29Sopenharmony_ci-- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
10873b921b29Sopenharmony_ci-+ * Copyright (C) 2009-2011, 2014, 2022, D. R. Commander.
10883b921b29Sopenharmony_ci-+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
10893b921b29Sopenharmony_ci-  * Copyright (C) 2020, Arm Limited.
10903b921b29Sopenharmony_ci-  *
10913b921b29Sopenharmony_ci-  * Based on the x86 SIMD extension for IJG JPEG library,
10923b921b29Sopenharmony_ci-@@ -412,7 +412,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
10933b921b29Sopenharmony_ci- GLOBAL(void)
10943b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
10953b921b29Sopenharmony_ci-                                   const int *jpeg_natural_order_start, int Sl,
10963b921b29Sopenharmony_ci--                                  int Al, JCOEF *values, size_t *zerobits)
10973b921b29Sopenharmony_ci-+                                  int Al, UJCOEF *values, size_t *zerobits)
10983b921b29Sopenharmony_ci- {
10993b921b29Sopenharmony_ci- }
11003b921b29Sopenharmony_ci- 
11013b921b29Sopenharmony_ci-@@ -425,7 +425,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
11023b921b29Sopenharmony_ci- GLOBAL(int)
11033b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
11043b921b29Sopenharmony_ci-                                    const int *jpeg_natural_order_start, int Sl,
11053b921b29Sopenharmony_ci--                                   int Al, JCOEF *absvalues, size_t *bits)
11063b921b29Sopenharmony_ci-+                                   int Al, UJCOEF *absvalues, size_t *bits)
11073b921b29Sopenharmony_ci- {
11083b921b29Sopenharmony_ci-   return 0;
11093b921b29Sopenharmony_ci- }
11103b921b29Sopenharmony_ci-diff --git a/simd/arm/aarch32/jsimd.c b/simd/arm/aarch32/jsimd.c
11113b921b29Sopenharmony_ci-index fac55df..d6568c5 100644
11123b921b29Sopenharmony_ci---- a/simd/arm/aarch32/jsimd.c
11133b921b29Sopenharmony_ci-+++ b/simd/arm/aarch32/jsimd.c
11143b921b29Sopenharmony_ci-@@ -4,7 +4,7 @@
11153b921b29Sopenharmony_ci-  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
11163b921b29Sopenharmony_ci-  * Copyright (C) 2011, Nokia Corporation and/or its subsidiary(-ies).
11173b921b29Sopenharmony_ci-  * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, D. R. Commander.
11183b921b29Sopenharmony_ci-- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
11193b921b29Sopenharmony_ci-+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
11203b921b29Sopenharmony_ci-  * Copyright (C) 2019, Google LLC.
11213b921b29Sopenharmony_ci-  * Copyright (C) 2020, Arm Limited.
11223b921b29Sopenharmony_ci-  *
11233b921b29Sopenharmony_ci-@@ -950,7 +950,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
11243b921b29Sopenharmony_ci- GLOBAL(void)
11253b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
11263b921b29Sopenharmony_ci-                                   const int *jpeg_natural_order_start, int Sl,
11273b921b29Sopenharmony_ci--                                  int Al, JCOEF *values, size_t *zerobits)
11283b921b29Sopenharmony_ci-+                                  int Al, UJCOEF *values, size_t *zerobits)
11293b921b29Sopenharmony_ci- {
11303b921b29Sopenharmony_ci-   jsimd_encode_mcu_AC_first_prepare_neon(block, jpeg_natural_order_start,
11313b921b29Sopenharmony_ci-                                          Sl, Al, values, zerobits);
11323b921b29Sopenharmony_ci-@@ -975,7 +975,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
11333b921b29Sopenharmony_ci- GLOBAL(int)
11343b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
11353b921b29Sopenharmony_ci-                                    const int *jpeg_natural_order_start, int Sl,
11363b921b29Sopenharmony_ci--                                   int Al, JCOEF *absvalues, size_t *bits)
11373b921b29Sopenharmony_ci-+                                   int Al, UJCOEF *absvalues, size_t *bits)
11383b921b29Sopenharmony_ci- {
11393b921b29Sopenharmony_ci-   return jsimd_encode_mcu_AC_refine_prepare_neon(block,
11403b921b29Sopenharmony_ci-                                                  jpeg_natural_order_start, Sl,
11413b921b29Sopenharmony_ci-diff --git a/simd/arm/aarch64/jsimd.c b/simd/arm/aarch64/jsimd.c
11423b921b29Sopenharmony_ci-index 8570b82..7de4f9a 100644
11433b921b29Sopenharmony_ci---- a/simd/arm/aarch64/jsimd.c
11443b921b29Sopenharmony_ci-+++ b/simd/arm/aarch64/jsimd.c
11453b921b29Sopenharmony_ci-@@ -4,7 +4,7 @@
11463b921b29Sopenharmony_ci-  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
11473b921b29Sopenharmony_ci-  * Copyright (C) 2011, Nokia Corporation and/or its subsidiary(-ies).
11483b921b29Sopenharmony_ci-  * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, 2020, D. R. Commander.
11493b921b29Sopenharmony_ci-- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
11503b921b29Sopenharmony_ci-+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
11513b921b29Sopenharmony_ci-  * Copyright (C) 2020, Arm Limited.
11523b921b29Sopenharmony_ci-  *
11533b921b29Sopenharmony_ci-  * Based on the x86 SIMD extension for IJG JPEG library,
11543b921b29Sopenharmony_ci-@@ -1028,7 +1028,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
11553b921b29Sopenharmony_ci- GLOBAL(void)
11563b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
11573b921b29Sopenharmony_ci-                                   const int *jpeg_natural_order_start, int Sl,
11583b921b29Sopenharmony_ci--                                  int Al, JCOEF *values, size_t *zerobits)
11593b921b29Sopenharmony_ci-+                                  int Al, UJCOEF *values, size_t *zerobits)
11603b921b29Sopenharmony_ci- {
11613b921b29Sopenharmony_ci-   jsimd_encode_mcu_AC_first_prepare_neon(block, jpeg_natural_order_start,
11623b921b29Sopenharmony_ci-                                          Sl, Al, values, zerobits);
11633b921b29Sopenharmony_ci-@@ -1055,7 +1055,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
11643b921b29Sopenharmony_ci- GLOBAL(int)
11653b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
11663b921b29Sopenharmony_ci-                                    const int *jpeg_natural_order_start, int Sl,
11673b921b29Sopenharmony_ci--                                   int Al, JCOEF *absvalues, size_t *bits)
11683b921b29Sopenharmony_ci-+                                   int Al, UJCOEF *absvalues, size_t *bits)
11693b921b29Sopenharmony_ci- {
11703b921b29Sopenharmony_ci-   return jsimd_encode_mcu_AC_refine_prepare_neon(block,
11713b921b29Sopenharmony_ci-                                                  jpeg_natural_order_start,
11723b921b29Sopenharmony_ci-diff --git a/simd/arm/jcphuff-neon.c b/simd/arm/jcphuff-neon.c
11733b921b29Sopenharmony_ci-index b91c5db..2640097 100644
11743b921b29Sopenharmony_ci---- a/simd/arm/jcphuff-neon.c
11753b921b29Sopenharmony_ci-+++ b/simd/arm/jcphuff-neon.c
11763b921b29Sopenharmony_ci-@@ -41,7 +41,7 @@
11773b921b29Sopenharmony_ci- 
11783b921b29Sopenharmony_ci- void jsimd_encode_mcu_AC_first_prepare_neon
11793b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
11803b921b29Sopenharmony_ci--   JCOEF *values, size_t *zerobits)
11813b921b29Sopenharmony_ci-+   UJCOEF *values, size_t *zerobits)
11823b921b29Sopenharmony_ci- {
11833b921b29Sopenharmony_ci-   JCOEF *values_ptr = values;
11843b921b29Sopenharmony_ci-   JCOEF *diff_values_ptr = values + DCTSIZE2;
11853b921b29Sopenharmony_ci-@@ -274,7 +274,7 @@ void jsimd_encode_mcu_AC_first_prepare_neon
11863b921b29Sopenharmony_ci- 
11873b921b29Sopenharmony_ci- int jsimd_encode_mcu_AC_refine_prepare_neon
11883b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
11893b921b29Sopenharmony_ci--   JCOEF *absvalues, size_t *bits)
11903b921b29Sopenharmony_ci-+   UJCOEF *absvalues, size_t *bits)
11913b921b29Sopenharmony_ci- {
11923b921b29Sopenharmony_ci-   /* Temporary storage buffers for data used to compute the signbits bitmap and
11933b921b29Sopenharmony_ci-    * the end-of-block (EOB) position
11943b921b29Sopenharmony_ci-diff --git a/simd/i386/jsimd.c b/simd/i386/jsimd.c
11953b921b29Sopenharmony_ci-index 563949a..ebe0767 100644
11963b921b29Sopenharmony_ci---- a/simd/i386/jsimd.c
11973b921b29Sopenharmony_ci-+++ b/simd/i386/jsimd.c
11983b921b29Sopenharmony_ci-@@ -3,7 +3,7 @@
11993b921b29Sopenharmony_ci-  *
12003b921b29Sopenharmony_ci-  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
12013b921b29Sopenharmony_ci-  * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, D. R. Commander.
12023b921b29Sopenharmony_ci-- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
12033b921b29Sopenharmony_ci-+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
12043b921b29Sopenharmony_ci-  *
12053b921b29Sopenharmony_ci-  * Based on the x86 SIMD extension for IJG JPEG library,
12063b921b29Sopenharmony_ci-  * Copyright (C) 1999-2006, MIYASAKA Masaru.
12073b921b29Sopenharmony_ci-@@ -1219,7 +1219,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
12083b921b29Sopenharmony_ci- GLOBAL(void)
12093b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
12103b921b29Sopenharmony_ci-                                   const int *jpeg_natural_order_start, int Sl,
12113b921b29Sopenharmony_ci--                                  int Al, JCOEF *values, size_t *zerobits)
12123b921b29Sopenharmony_ci-+                                  int Al, UJCOEF *values, size_t *zerobits)
12133b921b29Sopenharmony_ci- {
12143b921b29Sopenharmony_ci-   jsimd_encode_mcu_AC_first_prepare_sse2(block, jpeg_natural_order_start,
12153b921b29Sopenharmony_ci-                                          Sl, Al, values, zerobits);
12163b921b29Sopenharmony_ci-@@ -1245,7 +1245,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
12173b921b29Sopenharmony_ci- GLOBAL(int)
12183b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
12193b921b29Sopenharmony_ci-                                    const int *jpeg_natural_order_start, int Sl,
12203b921b29Sopenharmony_ci--                                   int Al, JCOEF *absvalues, size_t *bits)
12213b921b29Sopenharmony_ci-+                                   int Al, UJCOEF *absvalues, size_t *bits)
12223b921b29Sopenharmony_ci- {
12233b921b29Sopenharmony_ci-   return jsimd_encode_mcu_AC_refine_prepare_sse2(block,
12243b921b29Sopenharmony_ci-                                                  jpeg_natural_order_start,
12253b921b29Sopenharmony_ci-diff --git a/simd/jsimd.h b/simd/jsimd.h
12263b921b29Sopenharmony_ci-index 64747c6..abebbf4 100644
12273b921b29Sopenharmony_ci---- a/simd/jsimd.h
12283b921b29Sopenharmony_ci-+++ b/simd/jsimd.h
12293b921b29Sopenharmony_ci-@@ -5,7 +5,7 @@
12303b921b29Sopenharmony_ci-  * Copyright (C) 2011, 2014-2016, 2018, 2020, D. R. Commander.
12313b921b29Sopenharmony_ci-  * Copyright (C) 2013-2014, MIPS Technologies, Inc., California.
12323b921b29Sopenharmony_ci-  * Copyright (C) 2014, Linaro Limited.
12333b921b29Sopenharmony_ci-- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
12343b921b29Sopenharmony_ci-+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
12353b921b29Sopenharmony_ci-  * Copyright (C) 2016-2018, Loongson Technology Corporation Limited, BeiJing.
12363b921b29Sopenharmony_ci-  * Copyright (C) 2020, Arm Limited.
12373b921b29Sopenharmony_ci-  *
12383b921b29Sopenharmony_ci-@@ -15,6 +15,8 @@
12393b921b29Sopenharmony_ci-  *
12403b921b29Sopenharmony_ci-  */
12413b921b29Sopenharmony_ci- 
12423b921b29Sopenharmony_ci-+#include <stdlib.h>
12433b921b29Sopenharmony_ci-+
12443b921b29Sopenharmony_ci- /* Bitmask for supported acceleration methods */
12453b921b29Sopenharmony_ci- 
12463b921b29Sopenharmony_ci- #define JSIMD_NONE     0x00
12473b921b29Sopenharmony_ci-@@ -1243,16 +1245,16 @@ EXTERN(JOCTET *) jsimd_huff_encode_one_block_neon_slowtbl
12483b921b29Sopenharmony_ci- /* Progressive Huffman encoding */
12493b921b29Sopenharmony_ci- EXTERN(void) jsimd_encode_mcu_AC_first_prepare_sse2
12503b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
12513b921b29Sopenharmony_ci--   JCOEF *values, size_t *zerobits);
12523b921b29Sopenharmony_ci-+   UJCOEF *values, size_t *zerobits);
12533b921b29Sopenharmony_ci- 
12543b921b29Sopenharmony_ci- EXTERN(void) jsimd_encode_mcu_AC_first_prepare_neon
12553b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
12563b921b29Sopenharmony_ci--   JCOEF *values, size_t *zerobits);
12573b921b29Sopenharmony_ci-+   UJCOEF *values, size_t *zerobits);
12583b921b29Sopenharmony_ci- 
12593b921b29Sopenharmony_ci- EXTERN(int) jsimd_encode_mcu_AC_refine_prepare_sse2
12603b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
12613b921b29Sopenharmony_ci--   JCOEF *absvalues, size_t *bits);
12623b921b29Sopenharmony_ci-+   UJCOEF *absvalues, size_t *bits);
12633b921b29Sopenharmony_ci- 
12643b921b29Sopenharmony_ci- EXTERN(int) jsimd_encode_mcu_AC_refine_prepare_neon
12653b921b29Sopenharmony_ci-   (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
12663b921b29Sopenharmony_ci--   JCOEF *absvalues, size_t *bits);
12673b921b29Sopenharmony_ci-+   UJCOEF *absvalues, size_t *bits);
12683b921b29Sopenharmony_ci-diff --git a/simd/mips/jsimd.c b/simd/mips/jsimd.c
12693b921b29Sopenharmony_ci-index d2546ee..9be0a96 100644
12703b921b29Sopenharmony_ci---- a/simd/mips/jsimd.c
12713b921b29Sopenharmony_ci-+++ b/simd/mips/jsimd.c
12723b921b29Sopenharmony_ci-@@ -4,7 +4,7 @@
12733b921b29Sopenharmony_ci-  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
12743b921b29Sopenharmony_ci-  * Copyright (C) 2009-2011, 2014, 2016, 2018, 2020, D. R. Commander.
12753b921b29Sopenharmony_ci-  * Copyright (C) 2013-2014, MIPS Technologies, Inc., California.
12763b921b29Sopenharmony_ci-- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
12773b921b29Sopenharmony_ci-+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
12783b921b29Sopenharmony_ci-  *
12793b921b29Sopenharmony_ci-  * Based on the x86 SIMD extension for IJG JPEG library,
12803b921b29Sopenharmony_ci-  * Copyright (C) 1999-2006, MIYASAKA Masaru.
12813b921b29Sopenharmony_ci-@@ -1128,7 +1128,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
12823b921b29Sopenharmony_ci- GLOBAL(void)
12833b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
12843b921b29Sopenharmony_ci-                                   const int *jpeg_natural_order_start, int Sl,
12853b921b29Sopenharmony_ci--                                  int Al, JCOEF *values, size_t *zerobits)
12863b921b29Sopenharmony_ci-+                                  int Al, UJCOEF *values, size_t *zerobits)
12873b921b29Sopenharmony_ci- {
12883b921b29Sopenharmony_ci- }
12893b921b29Sopenharmony_ci- 
12903b921b29Sopenharmony_ci-@@ -1141,7 +1141,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
12913b921b29Sopenharmony_ci- GLOBAL(int)
12923b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
12933b921b29Sopenharmony_ci-                                    const int *jpeg_natural_order_start, int Sl,
12943b921b29Sopenharmony_ci--                                   int Al, JCOEF *absvalues, size_t *bits)
12953b921b29Sopenharmony_ci-+                                   int Al, UJCOEF *absvalues, size_t *bits)
12963b921b29Sopenharmony_ci- {
12973b921b29Sopenharmony_ci-   return 0;
12983b921b29Sopenharmony_ci- }
12993b921b29Sopenharmony_ci-diff --git a/simd/mips64/jsimd.c b/simd/mips64/jsimd.c
13003b921b29Sopenharmony_ci-index e8f1af5..bb9785b 100644
13013b921b29Sopenharmony_ci---- a/simd/mips64/jsimd.c
13023b921b29Sopenharmony_ci-+++ b/simd/mips64/jsimd.c
13033b921b29Sopenharmony_ci-@@ -851,7 +851,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
13043b921b29Sopenharmony_ci- GLOBAL(void)
13053b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
13063b921b29Sopenharmony_ci-                                   const int *jpeg_natural_order_start, int Sl,
13073b921b29Sopenharmony_ci--                                  int Al, JCOEF *values, size_t *zerobits)
13083b921b29Sopenharmony_ci-+                                  int Al, UJCOEF *values, size_t *zerobits)
13093b921b29Sopenharmony_ci- {
13103b921b29Sopenharmony_ci- }
13113b921b29Sopenharmony_ci- 
13123b921b29Sopenharmony_ci-@@ -864,7 +864,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
13133b921b29Sopenharmony_ci- GLOBAL(int)
13143b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
13153b921b29Sopenharmony_ci-                                    const int *jpeg_natural_order_start, int Sl,
13163b921b29Sopenharmony_ci--                                   int Al, JCOEF *absvalues, size_t *bits)
13173b921b29Sopenharmony_ci-+                                   int Al, UJCOEF *absvalues, size_t *bits)
13183b921b29Sopenharmony_ci- {
13193b921b29Sopenharmony_ci-   return 0;
13203b921b29Sopenharmony_ci- }
13213b921b29Sopenharmony_ci-diff --git a/simd/powerpc/jsimd.c b/simd/powerpc/jsimd.c
13223b921b29Sopenharmony_ci-index d0d3981..8affc5e 100644
13233b921b29Sopenharmony_ci---- a/simd/powerpc/jsimd.c
13243b921b29Sopenharmony_ci-+++ b/simd/powerpc/jsimd.c
13253b921b29Sopenharmony_ci-@@ -3,7 +3,7 @@
13263b921b29Sopenharmony_ci-  *
13273b921b29Sopenharmony_ci-  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
13283b921b29Sopenharmony_ci-  * Copyright (C) 2009-2011, 2014-2016, 2018, D. R. Commander.
13293b921b29Sopenharmony_ci-- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
13303b921b29Sopenharmony_ci-+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
13313b921b29Sopenharmony_ci-  *
13323b921b29Sopenharmony_ci-  * Based on the x86 SIMD extension for IJG JPEG library,
13333b921b29Sopenharmony_ci-  * Copyright (C) 1999-2006, MIYASAKA Masaru.
13343b921b29Sopenharmony_ci-@@ -853,7 +853,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
13353b921b29Sopenharmony_ci- GLOBAL(void)
13363b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
13373b921b29Sopenharmony_ci-                                   const int *jpeg_natural_order_start, int Sl,
13383b921b29Sopenharmony_ci--                                  int Al, JCOEF *values, size_t *zerobits)
13393b921b29Sopenharmony_ci-+                                  int Al, UJCOEF *values, size_t *zerobits)
13403b921b29Sopenharmony_ci- {
13413b921b29Sopenharmony_ci- }
13423b921b29Sopenharmony_ci- 
13433b921b29Sopenharmony_ci-@@ -866,7 +866,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
13443b921b29Sopenharmony_ci- GLOBAL(int)
13453b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
13463b921b29Sopenharmony_ci-                                    const int *jpeg_natural_order_start, int Sl,
13473b921b29Sopenharmony_ci--                                   int Al, JCOEF *absvalues, size_t *bits)
13483b921b29Sopenharmony_ci-+                                   int Al, UJCOEF *absvalues, size_t *bits)
13493b921b29Sopenharmony_ci- {
13503b921b29Sopenharmony_ci-   return 0;
13513b921b29Sopenharmony_ci- }
13523b921b29Sopenharmony_ci-diff --git a/simd/x86_64/jsimd.c b/simd/x86_64/jsimd.c
13533b921b29Sopenharmony_ci-index eb76679..1ede507 100644
13543b921b29Sopenharmony_ci---- a/simd/x86_64/jsimd.c
13553b921b29Sopenharmony_ci-+++ b/simd/x86_64/jsimd.c
13563b921b29Sopenharmony_ci-@@ -3,7 +3,7 @@
13573b921b29Sopenharmony_ci-  *
13583b921b29Sopenharmony_ci-  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
13593b921b29Sopenharmony_ci-  * Copyright (C) 2009-2011, 2014, 2016, 2018, D. R. Commander.
13603b921b29Sopenharmony_ci-- * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
13613b921b29Sopenharmony_ci-+ * Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
13623b921b29Sopenharmony_ci-  *
13633b921b29Sopenharmony_ci-  * Based on the x86 SIMD extension for IJG JPEG library,
13643b921b29Sopenharmony_ci-  * Copyright (C) 1999-2006, MIYASAKA Masaru.
13653b921b29Sopenharmony_ci-@@ -1040,7 +1040,7 @@ jsimd_can_encode_mcu_AC_first_prepare(void)
13663b921b29Sopenharmony_ci- GLOBAL(void)
13673b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
13683b921b29Sopenharmony_ci-                                   const int *jpeg_natural_order_start, int Sl,
13693b921b29Sopenharmony_ci--                                  int Al, JCOEF *values, size_t *zerobits)
13703b921b29Sopenharmony_ci-+                                  int Al, UJCOEF *values, size_t *zerobits)
13713b921b29Sopenharmony_ci- {
13723b921b29Sopenharmony_ci-   jsimd_encode_mcu_AC_first_prepare_sse2(block, jpeg_natural_order_start,
13733b921b29Sopenharmony_ci-                                          Sl, Al, values, zerobits);
13743b921b29Sopenharmony_ci-@@ -1064,7 +1064,7 @@ jsimd_can_encode_mcu_AC_refine_prepare(void)
13753b921b29Sopenharmony_ci- GLOBAL(int)
13763b921b29Sopenharmony_ci- jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
13773b921b29Sopenharmony_ci-                                    const int *jpeg_natural_order_start, int Sl,
13783b921b29Sopenharmony_ci--                                   int Al, JCOEF *absvalues, size_t *bits)
13793b921b29Sopenharmony_ci-+                                   int Al, UJCOEF *absvalues, size_t *bits)
13803b921b29Sopenharmony_ci- {
13813b921b29Sopenharmony_ci-   return jsimd_encode_mcu_AC_refine_prepare_sse2(block,
13823b921b29Sopenharmony_ci-                                                  jpeg_natural_order_start,
13833b921b29Sopenharmony_ci-diff --git a/tjbench.c b/tjbench.c
13843b921b29Sopenharmony_ci-index 156c906..8c83025 100644
13853b921b29Sopenharmony_ci---- a/tjbench.c
13863b921b29Sopenharmony_ci-+++ b/tjbench.c
13873b921b29Sopenharmony_ci-@@ -592,10 +592,16 @@ static int decompTest(char *fileName)
13883b921b29Sopenharmony_ci-     if ((flags & TJFLAG_NOREALLOC) != 0 &&
13893b921b29Sopenharmony_ci-         (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter))
13903b921b29Sopenharmony_ci-       for (i = 0; i < ntilesw * ntilesh; i++) {
13913b921b29Sopenharmony_ci--        if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
13923b921b29Sopenharmony_ci-+        unsigned long jpegBufSize;
13933b921b29Sopenharmony_ci-+
13943b921b29Sopenharmony_ci-+        if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
13953b921b29Sopenharmony_ci-+            xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270)
13963b921b29Sopenharmony_ci-+          jpegBufSize = tjBufSize(tileh, tilew, subsamp);
13973b921b29Sopenharmony_ci-+        else
13983b921b29Sopenharmony_ci-+          jpegBufSize = tjBufSize(tilew, tileh, subsamp);
13993b921b29Sopenharmony_ci-+        if (jpegBufSize > (unsigned long)INT_MAX)
14003b921b29Sopenharmony_ci-           THROW("getting buffer size", "Image is too large");
14013b921b29Sopenharmony_ci--        if ((jpegBuf[i] = (unsigned char *)
14023b921b29Sopenharmony_ci--                          tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
14033b921b29Sopenharmony_ci-+        if ((jpegBuf[i] = (unsigned char *)tjAlloc(jpegBufSize)) == NULL)
14043b921b29Sopenharmony_ci-           THROW_UNIX("allocating JPEG tiles");
14053b921b29Sopenharmony_ci-       }
14063b921b29Sopenharmony_ci- 
14073b921b29Sopenharmony_ci-diff --git a/tjexample.c b/tjexample.c
14083b921b29Sopenharmony_ci-index a9cd865..2ac1fed 100644
14093b921b29Sopenharmony_ci---- a/tjexample.c
14103b921b29Sopenharmony_ci-+++ b/tjexample.c
14113b921b29Sopenharmony_ci-@@ -32,6 +32,7 @@
14123b921b29Sopenharmony_ci-  * images using the TurboJPEG C API
14133b921b29Sopenharmony_ci-  */
14143b921b29Sopenharmony_ci- 
14153b921b29Sopenharmony_ci-+#include <limits.h>
14163b921b29Sopenharmony_ci- #include <stdio.h>
14173b921b29Sopenharmony_ci- #include <stdlib.h>
14183b921b29Sopenharmony_ci- #include <string.h>
14193b921b29Sopenharmony_ci-@@ -273,6 +274,8 @@ int main(int argc, char **argv)
14203b921b29Sopenharmony_ci-     if (size == 0)
14213b921b29Sopenharmony_ci-       THROW("determining input file size", "Input file contains no data");
14223b921b29Sopenharmony_ci-     jpegSize = (unsigned long)size;
14233b921b29Sopenharmony_ci-+    if (jpegSize > (unsigned long)INT_MAX)
14243b921b29Sopenharmony_ci-+      THROW("allocating JPEG buffer", "Input file is too large");
14253b921b29Sopenharmony_ci-     if ((jpegBuf = (unsigned char *)tjAlloc(jpegSize)) == NULL)
14263b921b29Sopenharmony_ci-       THROW_UNIX("allocating JPEG buffer");
14273b921b29Sopenharmony_ci-     if (fread(jpegBuf, jpegSize, 1, jpegFile) < 1)
14283b921b29Sopenharmony_ci-@@ -330,8 +333,12 @@ int main(int argc, char **argv)
14293b921b29Sopenharmony_ci-       outSubsamp = inSubsamp;
14303b921b29Sopenharmony_ci- 
14313b921b29Sopenharmony_ci-     pixelFormat = TJPF_BGRX;
14323b921b29Sopenharmony_ci--    if ((imgBuf = (unsigned char *)tjAlloc(width * height *
14333b921b29Sopenharmony_ci--                                           tjPixelSize[pixelFormat])) == NULL)
14343b921b29Sopenharmony_ci-+    if ((unsigned long long)width * height * tjPixelSize[pixelFormat] >
14353b921b29Sopenharmony_ci-+        (unsigned long long)((size_t)-1))
14363b921b29Sopenharmony_ci-+      THROW("allocating uncompressed image buffer", "Image is too large");
14373b921b29Sopenharmony_ci-+    if ((imgBuf =
14383b921b29Sopenharmony_ci-+         (unsigned char *)malloc(sizeof(unsigned char) * width * height *
14393b921b29Sopenharmony_ci-+                                 tjPixelSize[pixelFormat])) == NULL)
14403b921b29Sopenharmony_ci-       THROW_UNIX("allocating uncompressed image buffer");
14413b921b29Sopenharmony_ci- 
14423b921b29Sopenharmony_ci-     if (tjDecompress2(tjInstance, jpegBuf, jpegSize, imgBuf, width, 0, height,
14433b921b29Sopenharmony_ci-diff --git a/tjunittest.c b/tjunittest.c
14443b921b29Sopenharmony_ci-index f59939f..38ada63 100644
14453b921b29Sopenharmony_ci---- a/tjunittest.c
14463b921b29Sopenharmony_ci-+++ b/tjunittest.c
14473b921b29Sopenharmony_ci-@@ -33,6 +33,7 @@
14483b921b29Sopenharmony_ci- #include <stdio.h>
14493b921b29Sopenharmony_ci- #include <stdlib.h>
14503b921b29Sopenharmony_ci- #include <string.h>
14513b921b29Sopenharmony_ci-+#include <limits.h>
14523b921b29Sopenharmony_ci- #include <errno.h>
14533b921b29Sopenharmony_ci- #include "tjutil.h"
14543b921b29Sopenharmony_ci- #include "turbojpeg.h"
14553b921b29Sopenharmony_ci-@@ -566,11 +567,16 @@ bailout:
14563b921b29Sopenharmony_ci-     THROW(#function " overflow"); \
14573b921b29Sopenharmony_ci- }
14583b921b29Sopenharmony_ci- #endif
14593b921b29Sopenharmony_ci-+#define CHECKSIZEINT(function) { \
14603b921b29Sopenharmony_ci-+  if (intsize != -1 || !strcmp(tjGetErrorStr2(NULL), "No error")) \
14613b921b29Sopenharmony_ci-+    THROW(#function " overflow"); \
14623b921b29Sopenharmony_ci-+}
14633b921b29Sopenharmony_ci- 
14643b921b29Sopenharmony_ci- static void overflowTest(void)
14653b921b29Sopenharmony_ci- {
14663b921b29Sopenharmony_ci-   /* Ensure that the various buffer size functions don't overflow */
14673b921b29Sopenharmony_ci-   unsigned long size;
14683b921b29Sopenharmony_ci-+  int intsize;
14693b921b29Sopenharmony_ci- 
14703b921b29Sopenharmony_ci-   size = tjBufSize(26755, 26755, TJSAMP_444);
14713b921b29Sopenharmony_ci-   CHECKSIZE(tjBufSize());
14723b921b29Sopenharmony_ci-@@ -584,6 +590,10 @@ static void overflowTest(void)
14733b921b29Sopenharmony_ci-   CHECKSIZE(tjBufSizeYUV());
14743b921b29Sopenharmony_ci-   size = tjPlaneSizeYUV(0, 65536, 0, 65536, TJSAMP_444);
14753b921b29Sopenharmony_ci-   CHECKSIZE(tjPlaneSizeYUV());
14763b921b29Sopenharmony_ci-+  intsize = tjPlaneWidth(0, INT_MAX, TJSAMP_420);
14773b921b29Sopenharmony_ci-+  CHECKSIZEINT(tjPlaneWidth());
14783b921b29Sopenharmony_ci-+  intsize = tjPlaneHeight(0, INT_MAX, TJSAMP_420);
14793b921b29Sopenharmony_ci-+  CHECKSIZEINT(tjPlaneHeight());
14803b921b29Sopenharmony_ci- 
14813b921b29Sopenharmony_ci- bailout:
14823b921b29Sopenharmony_ci-   return;
14833b921b29Sopenharmony_ci-diff --git a/turbojpeg-jni.c b/turbojpeg-jni.c
14843b921b29Sopenharmony_ci-index 1b728e3..053edbd 100644
14853b921b29Sopenharmony_ci---- a/turbojpeg-jni.c
14863b921b29Sopenharmony_ci-+++ b/turbojpeg-jni.c
14873b921b29Sopenharmony_ci-@@ -26,6 +26,7 @@
14883b921b29Sopenharmony_ci-  * POSSIBILITY OF SUCH DAMAGE.
14893b921b29Sopenharmony_ci-  */
14903b921b29Sopenharmony_ci- 
14913b921b29Sopenharmony_ci-+#include <limits.h>
14923b921b29Sopenharmony_ci- #include <stdlib.h>
14933b921b29Sopenharmony_ci- #include <string.h>
14943b921b29Sopenharmony_ci- #include "turbojpeg.h"
14953b921b29Sopenharmony_ci-@@ -128,24 +129,28 @@ bailout:
14963b921b29Sopenharmony_ci- JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
14973b921b29Sopenharmony_ci-   (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
14983b921b29Sopenharmony_ci- {
14993b921b29Sopenharmony_ci--  jint retval = (jint)tjBufSize(width, height, jpegSubsamp);
15003b921b29Sopenharmony_ci-+  unsigned long retval = tjBufSize(width, height, jpegSubsamp);
15013b921b29Sopenharmony_ci- 
15023b921b29Sopenharmony_ci--  if (retval == -1) THROW_ARG(tjGetErrorStr());
15033b921b29Sopenharmony_ci-+  if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
15043b921b29Sopenharmony_ci-+  if (retval > (unsigned long)INT_MAX)
15053b921b29Sopenharmony_ci-+    THROW_ARG("Image is too large");
15063b921b29Sopenharmony_ci- 
15073b921b29Sopenharmony_ci- bailout:
15083b921b29Sopenharmony_ci--  return retval;
15093b921b29Sopenharmony_ci-+  return (jint)retval;
15103b921b29Sopenharmony_ci- }
15113b921b29Sopenharmony_ci- 
15123b921b29Sopenharmony_ci- /* TurboJPEG 1.4.x: TJ::bufSizeYUV() */
15133b921b29Sopenharmony_ci- JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
15143b921b29Sopenharmony_ci-   (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp)
15153b921b29Sopenharmony_ci- {
15163b921b29Sopenharmony_ci--  jint retval = (jint)tjBufSizeYUV2(width, pad, height, subsamp);
15173b921b29Sopenharmony_ci-+  unsigned long retval = tjBufSizeYUV2(width, align, height, subsamp);
15183b921b29Sopenharmony_ci- 
15193b921b29Sopenharmony_ci--  if (retval == -1) THROW_ARG(tjGetErrorStr());
15203b921b29Sopenharmony_ci-+  if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
15213b921b29Sopenharmony_ci-+  if (retval > (unsigned long)INT_MAX)
15223b921b29Sopenharmony_ci-+    THROW_ARG("Image is too large");
15233b921b29Sopenharmony_ci- 
15243b921b29Sopenharmony_ci- bailout:
15253b921b29Sopenharmony_ci--  return retval;
15263b921b29Sopenharmony_ci-+  return (jint)retval;
15273b921b29Sopenharmony_ci- }
15283b921b29Sopenharmony_ci- 
15293b921b29Sopenharmony_ci- /* TurboJPEG 1.2.x: TJ::bufSizeYUV() */
15303b921b29Sopenharmony_ci-@@ -162,13 +167,15 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII
15313b921b29Sopenharmony_ci-   (JNIEnv *env, jclass cls, jint componentID, jint width, jint stride,
15323b921b29Sopenharmony_ci-    jint height, jint subsamp)
15333b921b29Sopenharmony_ci- {
15343b921b29Sopenharmony_ci--  jint retval = (jint)tjPlaneSizeYUV(componentID, width, stride, height,
15353b921b29Sopenharmony_ci--                                     subsamp);
15363b921b29Sopenharmony_ci-+  unsigned long retval = tjPlaneSizeYUV(componentID, width, stride, height,
15373b921b29Sopenharmony_ci-+                                        subsamp);
15383b921b29Sopenharmony_ci- 
15393b921b29Sopenharmony_ci--  if (retval == -1) THROW_ARG(tjGetErrorStr());
15403b921b29Sopenharmony_ci-+  if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
15413b921b29Sopenharmony_ci-+  if (retval > (unsigned long)INT_MAX)
15423b921b29Sopenharmony_ci-+    THROW_ARG("Image is too large");
15433b921b29Sopenharmony_ci- 
15443b921b29Sopenharmony_ci- bailout:
15453b921b29Sopenharmony_ci--  return retval;
15463b921b29Sopenharmony_ci-+  return (jint)retval;
15473b921b29Sopenharmony_ci- }
15483b921b29Sopenharmony_ci- 
15493b921b29Sopenharmony_ci- /* TurboJPEG 1.4.x: TJ::planeWidth() */
15503b921b29Sopenharmony_ci-@@ -1176,6 +1183,10 @@ JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf
15513b921b29Sopenharmony_ci-   for (i = 0; i < n; i++) {
15523b921b29Sopenharmony_ci-     int w = jpegWidth, h = jpegHeight;
15533b921b29Sopenharmony_ci- 
15543b921b29Sopenharmony_ci-+    if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
15553b921b29Sopenharmony_ci-+        t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
15563b921b29Sopenharmony_ci-+      w = jpegHeight;  h = jpegWidth;
15573b921b29Sopenharmony_ci-+    }
15583b921b29Sopenharmony_ci-     if (t[i].r.w != 0) w = t[i].r.w;
15593b921b29Sopenharmony_ci-     if (t[i].r.h != 0) h = t[i].r.h;
15603b921b29Sopenharmony_ci-     BAILIF0(jdstBufs[i] = (*env)->GetObjectArrayElement(env, dstobjs, i));
15613b921b29Sopenharmony_ci-diff --git a/turbojpeg.c b/turbojpeg.c
15623b921b29Sopenharmony_ci-index 47c5999..6c425be 100644
15633b921b29Sopenharmony_ci---- a/turbojpeg.c
15643b921b29Sopenharmony_ci-+++ b/turbojpeg.c
15653b921b29Sopenharmony_ci-@@ -33,6 +33,7 @@
15663b921b29Sopenharmony_ci- #include <stdio.h>
15673b921b29Sopenharmony_ci- #include <stdlib.h>
15683b921b29Sopenharmony_ci- #include <ctype.h>
15693b921b29Sopenharmony_ci-+#include <limits.h>
15703b921b29Sopenharmony_ci- #include <jinclude.h>
15713b921b29Sopenharmony_ci- #define JPEG_INTERNALS
15723b921b29Sopenharmony_ci- #include <jpeglib.h>
15733b921b29Sopenharmony_ci-@@ -599,7 +600,8 @@ DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int subsamp)
15743b921b29Sopenharmony_ci- 
15753b921b29Sopenharmony_ci- DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp)
15763b921b29Sopenharmony_ci- {
15773b921b29Sopenharmony_ci--  int pw, nc, retval = 0;
15783b921b29Sopenharmony_ci-+  unsigned long long pw, retval = 0;
15793b921b29Sopenharmony_ci-+  int nc;
15803b921b29Sopenharmony_ci- 
15813b921b29Sopenharmony_ci-   if (width < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
15823b921b29Sopenharmony_ci-     THROWG("tjPlaneWidth(): Invalid argument");
15833b921b29Sopenharmony_ci-@@ -613,14 +615,18 @@ DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp)
15843b921b29Sopenharmony_ci-   else
15853b921b29Sopenharmony_ci-     retval = pw * 8 / tjMCUWidth[subsamp];
15863b921b29Sopenharmony_ci- 
15873b921b29Sopenharmony_ci-+  if (retval > (unsigned long long)INT_MAX)
15883b921b29Sopenharmony_ci-+    THROWG("tjPlaneWidth(): Width is too large");
15893b921b29Sopenharmony_ci-+
15903b921b29Sopenharmony_ci- bailout:
15913b921b29Sopenharmony_ci--  return retval;
15923b921b29Sopenharmony_ci-+  return (int)retval;
15933b921b29Sopenharmony_ci- }
15943b921b29Sopenharmony_ci- 
15953b921b29Sopenharmony_ci- 
15963b921b29Sopenharmony_ci- DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp)
15973b921b29Sopenharmony_ci- {
15983b921b29Sopenharmony_ci--  int ph, nc, retval = 0;
15993b921b29Sopenharmony_ci-+  unsigned long long ph, retval = 0;
16003b921b29Sopenharmony_ci-+  int nc;
16013b921b29Sopenharmony_ci- 
16023b921b29Sopenharmony_ci-   if (height < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
16033b921b29Sopenharmony_ci-     THROWG("tjPlaneHeight(): Invalid argument");
16043b921b29Sopenharmony_ci-@@ -634,8 +640,11 @@ DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp)
16053b921b29Sopenharmony_ci-   else
16063b921b29Sopenharmony_ci-     retval = ph * 8 / tjMCUHeight[subsamp];
16073b921b29Sopenharmony_ci- 
16083b921b29Sopenharmony_ci-+  if (retval > (unsigned long long)INT_MAX)
16093b921b29Sopenharmony_ci-+    THROWG("tjPlaneHeight(): Height is too large");
16103b921b29Sopenharmony_ci-+
16113b921b29Sopenharmony_ci- bailout:
16123b921b29Sopenharmony_ci--  return retval;
16133b921b29Sopenharmony_ci-+  return (int)retval;
16143b921b29Sopenharmony_ci- }
16153b921b29Sopenharmony_ci- 
16163b921b29Sopenharmony_ci- 
16173b921b29Sopenharmony_ci-@@ -1985,6 +1994,10 @@ DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
16183b921b29Sopenharmony_ci- 
16193b921b29Sopenharmony_ci-     if (!xinfo[i].crop) {
16203b921b29Sopenharmony_ci-       w = dinfo->image_width;  h = dinfo->image_height;
16213b921b29Sopenharmony_ci-+      if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
16223b921b29Sopenharmony_ci-+          t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
16233b921b29Sopenharmony_ci-+        w = dinfo->image_height;  h = dinfo->image_width;
16243b921b29Sopenharmony_ci-+      }
16253b921b29Sopenharmony_ci-     } else {
16263b921b29Sopenharmony_ci-       w = xinfo[i].crop_width;  h = xinfo[i].crop_height;
16273b921b29Sopenharmony_ci-     }
1628