Remove core/fxcodec/codec/codec_int.h.
Move remaining JPX bits into core/fxcodec/jpx/jpx_decode_utils.h.
Reorganize code so jpx_decode_utils.h and jpx_decode_utils.cpp match.
Also move affected code into namespace fxcodec.
Change-Id: I53a5b9843d76f4528ff0308f418a5a8ac998dca7
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/56354
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcodec/BUILD.gn b/core/fxcodec/BUILD.gn
index cab5d9e..c964be9 100644
--- a/core/fxcodec/BUILD.gn
+++ b/core/fxcodec/BUILD.gn
@@ -12,7 +12,6 @@
"basic/basicmodule.h",
"codec/cfx_codec_memory.cpp",
"codec/cfx_codec_memory.h",
- "codec/codec_int.h",
"codec/codec_module_iface.h",
"codec/scanlinedecoder.cpp",
"codec/scanlinedecoder.h",
@@ -67,6 +66,8 @@
"jpeg/jpegmodule.h",
"jpx/cjpx_decoder.cpp",
"jpx/cjpx_decoder.h",
+ "jpx/jpx_decode_utils.cpp",
+ "jpx/jpx_decode_utils.h",
"jpx/jpxmodule.cpp",
"jpx/jpxmodule.h",
]
diff --git a/core/fxcodec/bmp/bmpmodule.cpp b/core/fxcodec/bmp/bmpmodule.cpp
index 21b9390..5cdb681 100644
--- a/core/fxcodec/bmp/bmpmodule.cpp
+++ b/core/fxcodec/bmp/bmpmodule.cpp
@@ -10,7 +10,6 @@
#include "core/fxcodec/bmp/cfx_bmpcontext.h"
#include "core/fxcodec/codec/cfx_codec_memory.h"
-#include "core/fxcodec/codec/codec_int.h"
#include "core/fxcodec/fx_codec.h"
#include "core/fxge/fx_dib.h"
#include "third_party/base/ptr_util.h"
diff --git a/core/fxcodec/codec/fx_codec_jpx_unittest.cpp b/core/fxcodec/codec/fx_codec_jpx_unittest.cpp
index 4acd4e0..def5065 100644
--- a/core/fxcodec/codec/fx_codec_jpx_unittest.cpp
+++ b/core/fxcodec/codec/fx_codec_jpx_unittest.cpp
@@ -6,10 +6,14 @@
#include <limits>
-#include "core/fxcodec/codec/codec_int.h"
+#include "core/fxcodec/jpx/cjpx_decoder.h"
+#include "core/fxcodec/jpx/jpx_decode_utils.h"
+#include "core/fxcrt/fx_memory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libopenjpeg20/opj_malloc.h"
+namespace fxcodec {
+
static const OPJ_OFF_T kSkipError = static_cast<OPJ_OFF_T>(-1);
static const OPJ_SIZE_T kReadError = static_cast<OPJ_SIZE_T>(-1);
@@ -425,7 +429,7 @@
img.comps[0] = y;
img.comps[1] = u;
img.comps[2] = v;
- sycc420_to_rgb(&img);
+ CJPX_Decoder::Sycc420ToRgbForTesting(&img);
if (testcase.expected) {
EXPECT_EQ(img.comps[0].w, img.comps[1].w);
EXPECT_EQ(img.comps[0].h, img.comps[1].h);
@@ -443,3 +447,5 @@
}
FX_Free(img.comps);
}
+
+} // namespace fxcodec
diff --git a/core/fxcodec/fax/faxmodule.cpp b/core/fxcodec/fax/faxmodule.cpp
index 75e79a2..60bec6a 100644
--- a/core/fxcodec/fax/faxmodule.cpp
+++ b/core/fxcodec/fax/faxmodule.cpp
@@ -12,7 +12,6 @@
#include <vector>
#include "build/build_config.h"
-#include "core/fxcodec/codec/codec_int.h"
#include "core/fxcodec/codec/scanlinedecoder.h"
#include "core/fxcodec/fx_codec.h"
#include "core/fxcrt/cfx_binarybuf.h"
diff --git a/core/fxcodec/fx_codec.cpp b/core/fxcodec/fx_codec.cpp
index 18c745e..5625534 100644
--- a/core/fxcodec/fx_codec.cpp
+++ b/core/fxcodec/fx_codec.cpp
@@ -11,7 +11,6 @@
#include <memory>
#include <utility>
-#include "core/fxcodec/codec/codec_int.h"
#include "core/fxcodec/jbig2/jbig2module.h"
#include "core/fxcodec/jpeg/jpegmodule.h"
#include "core/fxcrt/fx_extension.h"
diff --git a/core/fxcodec/gif/gifmodule.cpp b/core/fxcodec/gif/gifmodule.cpp
index 1c3cdb5..cc82e38 100644
--- a/core/fxcodec/gif/gifmodule.cpp
+++ b/core/fxcodec/gif/gifmodule.cpp
@@ -7,7 +7,6 @@
#include "core/fxcodec/gif/gifmodule.h"
#include "core/fxcodec/codec/cfx_codec_memory.h"
-#include "core/fxcodec/codec/codec_int.h"
#include "core/fxcodec/fx_codec.h"
#include "core/fxcodec/gif/cfx_gif.h"
#include "core/fxcodec/gif/cfx_gifcontext.h"
diff --git a/core/fxcodec/icc/iccmodule.cpp b/core/fxcodec/icc/iccmodule.cpp
index 6927749..e9a8cd6 100644
--- a/core/fxcodec/icc/iccmodule.cpp
+++ b/core/fxcodec/icc/iccmodule.cpp
@@ -10,7 +10,6 @@
#include <memory>
#include <vector>
-#include "core/fxcodec/codec/codec_int.h"
#include "third_party/base/ptr_util.h"
#include "third_party/base/stl_util.h"
diff --git a/core/fxcodec/jpx/cjpx_decoder.cpp b/core/fxcodec/jpx/cjpx_decoder.cpp
index 118aaf2..6e8ba7a 100644
--- a/core/fxcodec/jpx/cjpx_decoder.cpp
+++ b/core/fxcodec/jpx/cjpx_decoder.cpp
@@ -8,21 +8,20 @@
#include <algorithm>
#include <limits>
-#include <memory>
-#include <type_traits>
#include <utility>
-#include <vector>
+#include "core/fxcodec/jpx/jpx_decode_utils.h"
#include "core/fxcrt/fx_safe_types.h"
#include "third_party/base/optional.h"
#include "third_party/base/ptr_util.h"
#include "third_party/base/stl_util.h"
-#ifndef USE_SYSTEM_LIBOPENJPEG2
-#include "third_party/libopenjpeg20/openjpeg.h"
+#if !defined(USE_SYSTEM_LIBOPENJPEG2)
#include "third_party/libopenjpeg20/opj_malloc.h"
#endif
+namespace fxcodec {
+
namespace {
// Used with std::unique_ptr to call opj_image_data_free on raw memory.
@@ -144,184 +143,10 @@
(img->comps[0].h + 1) / 2 == img->comps[1].h;
}
-bool sycc422_size_is_valid(opj_image_t* img) {
- return sycc420_422_size_is_valid(img) && img->comps[0].h == img->comps[1].h;
-}
-
-void sycc422_to_rgb(opj_image_t* img) {
- if (!sycc422_size_is_valid(img))
- return;
-
- int prec = img->comps[0].prec;
- if (prec <= 0 || prec >= 32)
- return;
-
- int offset = 1 << (prec - 1);
- int upb = (1 << prec) - 1;
- OPJ_UINT32 maxw = img->comps[0].w;
- OPJ_UINT32 maxh = img->comps[0].h;
- FX_SAFE_SIZE_T max_size = maxw;
- max_size *= maxh;
- max_size *= sizeof(int);
- if (!max_size.IsValid())
- return;
-
- const int* y = img->comps[0].data;
- const int* cb = img->comps[1].data;
- const int* cr = img->comps[2].data;
- if (!y || !cb || !cr)
- return;
-
- Optional<OpjImageRgbData> data = alloc_rgb(max_size.ValueOrDie());
- if (!data.has_value())
- return;
-
- int* r = data.value().r.get();
- int* g = data.value().g.get();
- int* b = data.value().b.get();
- for (uint32_t i = 0; i < maxh; ++i) {
- OPJ_UINT32 j;
- for (j = 0; j < (maxw & ~static_cast<OPJ_UINT32>(1)); j += 2) {
- sycc_to_rgb(offset, upb, *y++, *cb, *cr, r++, g++, b++);
- sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
- }
- if (j < maxw) {
- sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
- }
- }
-
- opj_image_data_free(img->comps[0].data);
- opj_image_data_free(img->comps[1].data);
- opj_image_data_free(img->comps[2].data);
- img->comps[0].data = data.value().r.release();
- img->comps[1].data = data.value().g.release();
- img->comps[2].data = data.value().b.release();
- img->comps[1].w = maxw;
- img->comps[1].h = maxh;
- img->comps[2].w = maxw;
- img->comps[2].h = maxh;
- img->comps[1].dx = img->comps[0].dx;
- img->comps[2].dx = img->comps[0].dx;
- img->comps[1].dy = img->comps[0].dy;
- img->comps[2].dy = img->comps[0].dy;
-}
-
bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) {
return (y & 1) && (cbcr == y / 2);
}
-bool is_sycc420(const opj_image_t* img) {
- return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
- img->comps[1].dx == 2 && img->comps[1].dy == 2 &&
- img->comps[2].dx == 2 && img->comps[2].dy == 2;
-}
-
-bool is_sycc422(const opj_image_t* img) {
- return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
- img->comps[1].dx == 2 && img->comps[1].dy == 1 &&
- img->comps[2].dx == 2 && img->comps[2].dy == 1;
-}
-
-bool is_sycc444(const opj_image_t* img) {
- return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
- img->comps[1].dx == 1 && img->comps[1].dy == 1 &&
- img->comps[2].dx == 1 && img->comps[2].dy == 1;
-}
-
-void color_sycc_to_rgb(opj_image_t* img) {
- if (img->numcomps < 3) {
- img->color_space = OPJ_CLRSPC_GRAY;
- return;
- }
- if (is_sycc420(img))
- sycc420_to_rgb(img);
- else if (is_sycc422(img))
- sycc422_to_rgb(img);
- else if (is_sycc444(img))
- sycc444_to_rgb(img);
- else
- return;
-
- img->color_space = OPJ_CLRSPC_SRGB;
-}
-
-} // namespace
-
-OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
- OPJ_SIZE_T nb_bytes,
- void* p_user_data) {
- DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
- if (!srcData || !srcData->src_data || srcData->src_size == 0)
- return static_cast<OPJ_SIZE_T>(-1);
-
- // Reads at EOF return an error code.
- if (srcData->offset >= srcData->src_size)
- return static_cast<OPJ_SIZE_T>(-1);
-
- OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
- OPJ_SIZE_T readlength = nb_bytes < bufferLength ? nb_bytes : bufferLength;
- memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength);
- srcData->offset += readlength;
- return readlength;
-}
-
-OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) {
- DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
- if (!srcData || !srcData->src_data || srcData->src_size == 0)
- return static_cast<OPJ_OFF_T>(-1);
-
- // Offsets are signed and may indicate a negative skip. Do not support this
- // because of the strange return convention where either bytes skipped or
- // -1 is returned. Following that convention, a successful relative seek of
- // -1 bytes would be required to to give the same result as the error case.
- if (nb_bytes < 0)
- return static_cast<OPJ_OFF_T>(-1);
-
- auto unsigned_nb_bytes =
- static_cast<std::make_unsigned<OPJ_OFF_T>::type>(nb_bytes);
- // Additionally, the offset may take us beyond the range of a size_t (e.g.
- // 32-bit platforms). If so, just clamp at EOF.
- if (unsigned_nb_bytes >
- std::numeric_limits<OPJ_SIZE_T>::max() - srcData->offset) {
- srcData->offset = srcData->src_size;
- } else {
- OPJ_SIZE_T checked_nb_bytes = static_cast<OPJ_SIZE_T>(unsigned_nb_bytes);
- // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
- // clamping at EOF. We can get away with this since we don't actually
- // provide negative relative skips from beyond EOF back to inside the
- // data, which would be the only reason to need to know exactly how far
- // beyond EOF we are.
- srcData->offset =
- std::min(srcData->offset + checked_nb_bytes, srcData->src_size);
- }
- return nb_bytes;
-}
-
-OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) {
- DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
- if (!srcData || !srcData->src_data || srcData->src_size == 0)
- return OPJ_FALSE;
-
- // Offsets are signed and may indicate a negative position, which would
- // be before the start of the file. Do not support this.
- if (nb_bytes < 0)
- return OPJ_FALSE;
-
- auto unsigned_nb_bytes =
- static_cast<std::make_unsigned<OPJ_OFF_T>::type>(nb_bytes);
- // Additionally, the offset may take us beyond the range of a size_t (e.g.
- // 32-bit platforms). If so, just clamp at EOF.
- if (unsigned_nb_bytes > std::numeric_limits<OPJ_SIZE_T>::max()) {
- srcData->offset = srcData->src_size;
- } else {
- OPJ_SIZE_T checked_nb_bytes = static_cast<OPJ_SIZE_T>(nb_bytes);
- // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
- // again clamping at EOF.
- srcData->offset = std::min(checked_nb_bytes, srcData->src_size);
- }
- return OPJ_TRUE;
-}
-
void sycc420_to_rgb(opj_image_t* img) {
if (!sycc420_size_is_valid(img))
return;
@@ -460,6 +285,110 @@
img->comps[2].dy = img->comps[0].dy;
}
+bool sycc422_size_is_valid(opj_image_t* img) {
+ return sycc420_422_size_is_valid(img) && img->comps[0].h == img->comps[1].h;
+}
+
+void sycc422_to_rgb(opj_image_t* img) {
+ if (!sycc422_size_is_valid(img))
+ return;
+
+ int prec = img->comps[0].prec;
+ if (prec <= 0 || prec >= 32)
+ return;
+
+ int offset = 1 << (prec - 1);
+ int upb = (1 << prec) - 1;
+ OPJ_UINT32 maxw = img->comps[0].w;
+ OPJ_UINT32 maxh = img->comps[0].h;
+ FX_SAFE_SIZE_T max_size = maxw;
+ max_size *= maxh;
+ max_size *= sizeof(int);
+ if (!max_size.IsValid())
+ return;
+
+ const int* y = img->comps[0].data;
+ const int* cb = img->comps[1].data;
+ const int* cr = img->comps[2].data;
+ if (!y || !cb || !cr)
+ return;
+
+ Optional<OpjImageRgbData> data = alloc_rgb(max_size.ValueOrDie());
+ if (!data.has_value())
+ return;
+
+ int* r = data.value().r.get();
+ int* g = data.value().g.get();
+ int* b = data.value().b.get();
+ for (uint32_t i = 0; i < maxh; ++i) {
+ OPJ_UINT32 j;
+ for (j = 0; j < (maxw & ~static_cast<OPJ_UINT32>(1)); j += 2) {
+ sycc_to_rgb(offset, upb, *y++, *cb, *cr, r++, g++, b++);
+ sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
+ }
+ if (j < maxw) {
+ sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
+ }
+ }
+
+ opj_image_data_free(img->comps[0].data);
+ opj_image_data_free(img->comps[1].data);
+ opj_image_data_free(img->comps[2].data);
+ img->comps[0].data = data.value().r.release();
+ img->comps[1].data = data.value().g.release();
+ img->comps[2].data = data.value().b.release();
+ img->comps[1].w = maxw;
+ img->comps[1].h = maxh;
+ img->comps[2].w = maxw;
+ img->comps[2].h = maxh;
+ img->comps[1].dx = img->comps[0].dx;
+ img->comps[2].dx = img->comps[0].dx;
+ img->comps[1].dy = img->comps[0].dy;
+ img->comps[2].dy = img->comps[0].dy;
+}
+
+bool is_sycc420(const opj_image_t* img) {
+ return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
+ img->comps[1].dx == 2 && img->comps[1].dy == 2 &&
+ img->comps[2].dx == 2 && img->comps[2].dy == 2;
+}
+
+bool is_sycc422(const opj_image_t* img) {
+ return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
+ img->comps[1].dx == 2 && img->comps[1].dy == 1 &&
+ img->comps[2].dx == 2 && img->comps[2].dy == 1;
+}
+
+bool is_sycc444(const opj_image_t* img) {
+ return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
+ img->comps[1].dx == 1 && img->comps[1].dy == 1 &&
+ img->comps[2].dx == 1 && img->comps[2].dy == 1;
+}
+
+void color_sycc_to_rgb(opj_image_t* img) {
+ if (img->numcomps < 3) {
+ img->color_space = OPJ_CLRSPC_GRAY;
+ return;
+ }
+ if (is_sycc420(img))
+ sycc420_to_rgb(img);
+ else if (is_sycc422(img))
+ sycc422_to_rgb(img);
+ else if (is_sycc444(img))
+ sycc444_to_rgb(img);
+ else
+ return;
+
+ img->color_space = OPJ_CLRSPC_SRGB;
+}
+
+} // namespace
+
+// static
+void CJPX_Decoder::Sycc420ToRgbForTesting(opj_image_t* img) {
+ sycc420_to_rgb(img);
+}
+
CJPX_Decoder::CJPX_Decoder(ColorSpaceOption option)
: m_ColorSpaceOption(option) {}
@@ -512,7 +441,7 @@
return false;
m_Image = pTempImage;
-#ifndef USE_SYSTEM_LIBOPENJPEG2
+#if !defined(USE_SYSTEM_LIBOPENJPEG2)
m_Image->pdfium_use_colorspace = (m_ColorSpaceOption != kNoColorSpace);
#endif
return true;
@@ -551,7 +480,7 @@
// TODO(palmer): Using |opj_free| here resolves the crash described in
// https://crbug.com/737033, but ultimately we need to harmonize the
// memory allocation strategy across OpenJPEG and its PDFium callers.
-#ifndef USE_SYSTEM_LIBOPENJPEG2
+#if !defined(USE_SYSTEM_LIBOPENJPEG2)
opj_free(m_Image->icc_profile_buf);
#else
free(m_Image->icc_profile_buf);
@@ -643,3 +572,5 @@
}
return true;
}
+
+} // namespace fxcodec
diff --git a/core/fxcodec/jpx/cjpx_decoder.h b/core/fxcodec/jpx/cjpx_decoder.h
index 8c607e2..11d94c1 100644
--- a/core/fxcodec/jpx/cjpx_decoder.h
+++ b/core/fxcodec/jpx/cjpx_decoder.h
@@ -10,16 +10,19 @@
#include <memory>
#include <vector>
-#include "core/fxcodec/codec/codec_int.h"
#include "core/fxcrt/unowned_ptr.h"
#include "third_party/base/span.h"
-#ifdef USE_SYSTEM_LIBOPENJPEG2
+#if defined(USE_SYSTEM_LIBOPENJPEG2)
#include <openjpeg.h>
#else
#include "third_party/libopenjpeg20/openjpeg.h"
#endif
+namespace fxcodec {
+
+struct DecodeData;
+
class CJPX_Decoder {
public:
enum ColorSpaceOption {
@@ -28,6 +31,8 @@
kIndexedColorSpace
};
+ static void Sycc420ToRgbForTesting(opj_image_t* img);
+
explicit CJPX_Decoder(ColorSpaceOption option);
~CJPX_Decoder();
@@ -48,4 +53,8 @@
opj_dparameters_t m_Parameters;
};
+} // namespace fxcodec
+
+using fxcodec::CJPX_Decoder;
+
#endif // CORE_FXCODEC_JPX_CJPX_DECODER_H_
diff --git a/core/fxcodec/jpx/jpx_decode_utils.cpp b/core/fxcodec/jpx/jpx_decode_utils.cpp
new file mode 100644
index 0000000..d3842d7
--- /dev/null
+++ b/core/fxcodec/jpx/jpx_decode_utils.cpp
@@ -0,0 +1,91 @@
+// Copyright 2019 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "core/fxcodec/jpx/jpx_decode_utils.h"
+
+#include <string.h>
+
+#include <algorithm>
+#include <limits>
+
+namespace fxcodec {
+
+OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
+ OPJ_SIZE_T nb_bytes,
+ void* p_user_data) {
+ DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
+ if (!srcData || !srcData->src_data || srcData->src_size == 0)
+ return static_cast<OPJ_SIZE_T>(-1);
+
+ // Reads at EOF return an error code.
+ if (srcData->offset >= srcData->src_size)
+ return static_cast<OPJ_SIZE_T>(-1);
+
+ OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
+ OPJ_SIZE_T readlength = nb_bytes < bufferLength ? nb_bytes : bufferLength;
+ memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength);
+ srcData->offset += readlength;
+ return readlength;
+}
+
+OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) {
+ DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
+ if (!srcData || !srcData->src_data || srcData->src_size == 0)
+ return static_cast<OPJ_OFF_T>(-1);
+
+ // Offsets are signed and may indicate a negative skip. Do not support this
+ // because of the strange return convention where either bytes skipped or
+ // -1 is returned. Following that convention, a successful relative seek of
+ // -1 bytes would be required to to give the same result as the error case.
+ if (nb_bytes < 0)
+ return static_cast<OPJ_OFF_T>(-1);
+
+ auto unsigned_nb_bytes =
+ static_cast<std::make_unsigned<OPJ_OFF_T>::type>(nb_bytes);
+ // Additionally, the offset may take us beyond the range of a size_t (e.g.
+ // 32-bit platforms). If so, just clamp at EOF.
+ if (unsigned_nb_bytes >
+ std::numeric_limits<OPJ_SIZE_T>::max() - srcData->offset) {
+ srcData->offset = srcData->src_size;
+ } else {
+ OPJ_SIZE_T checked_nb_bytes = static_cast<OPJ_SIZE_T>(unsigned_nb_bytes);
+ // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
+ // clamping at EOF. We can get away with this since we don't actually
+ // provide negative relative skips from beyond EOF back to inside the
+ // data, which would be the only reason to need to know exactly how far
+ // beyond EOF we are.
+ srcData->offset =
+ std::min(srcData->offset + checked_nb_bytes, srcData->src_size);
+ }
+ return nb_bytes;
+}
+
+OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) {
+ DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
+ if (!srcData || !srcData->src_data || srcData->src_size == 0)
+ return OPJ_FALSE;
+
+ // Offsets are signed and may indicate a negative position, which would
+ // be before the start of the file. Do not support this.
+ if (nb_bytes < 0)
+ return OPJ_FALSE;
+
+ auto unsigned_nb_bytes =
+ static_cast<std::make_unsigned<OPJ_OFF_T>::type>(nb_bytes);
+ // Additionally, the offset may take us beyond the range of a size_t (e.g.
+ // 32-bit platforms). If so, just clamp at EOF.
+ if (unsigned_nb_bytes > std::numeric_limits<OPJ_SIZE_T>::max()) {
+ srcData->offset = srcData->src_size;
+ } else {
+ OPJ_SIZE_T checked_nb_bytes = static_cast<OPJ_SIZE_T>(nb_bytes);
+ // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
+ // again clamping at EOF.
+ srcData->offset = std::min(checked_nb_bytes, srcData->src_size);
+ }
+ return OPJ_TRUE;
+}
+
+} // namespace fxcodec
diff --git a/core/fxcodec/codec/codec_int.h b/core/fxcodec/jpx/jpx_decode_utils.h
similarity index 76%
rename from core/fxcodec/codec/codec_int.h
rename to core/fxcodec/jpx/jpx_decode_utils.h
index e5f75d5..4403cb8 100644
--- a/core/fxcodec/codec/codec_int.h
+++ b/core/fxcodec/jpx/jpx_decode_utils.h
@@ -4,19 +4,19 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-#ifndef CORE_FXCODEC_CODEC_CODEC_INT_H_
-#define CORE_FXCODEC_CODEC_CODEC_INT_H_
+#ifndef CORE_FXCODEC_JPX_JPX_DECODE_UTILS_H_
+#define CORE_FXCODEC_JPX_JPX_DECODE_UTILS_H_
-#include <limits.h>
+#include <stdint.h>
-#include "core/fxcodec/jbig2/JBig2_Context.h"
-
-#ifdef USE_SYSTEM_LIBOPENJPEG2
+#if defined(USE_SYSTEM_LIBOPENJPEG2)
#include <openjpeg.h>
#else
#include "third_party/libopenjpeg20/openjpeg.h"
#endif
+namespace fxcodec {
+
struct DecodeData {
DecodeData(const uint8_t* data, OPJ_SIZE_T size)
: src_data(data), src_size(size), offset(0) {}
@@ -26,8 +26,6 @@
OPJ_SIZE_T offset;
};
-void sycc420_to_rgb(opj_image_t* img);
-
/* Wrappers for C-style callbacks. */
OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
OPJ_SIZE_T nb_bytes,
@@ -35,4 +33,6 @@
OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data);
OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data);
-#endif // CORE_FXCODEC_CODEC_CODEC_INT_H_
+} // namespace fxcodec
+
+#endif // CORE_FXCODEC_JPX_JPX_DECODE_UTILS_H_
diff --git a/core/fxcodec/png/pngmodule.cpp b/core/fxcodec/png/pngmodule.cpp
index f74fc50..9742bc6 100644
--- a/core/fxcodec/png/pngmodule.cpp
+++ b/core/fxcodec/png/pngmodule.cpp
@@ -9,7 +9,6 @@
#include <algorithm>
#include "core/fxcodec/codec/cfx_codec_memory.h"
-#include "core/fxcodec/codec/codec_int.h"
#include "core/fxcodec/fx_codec.h"
#include "core/fxcrt/unowned_ptr.h"
#include "core/fxge/fx_dib.h"
diff --git a/core/fxcodec/tiff/tiffmodule.cpp b/core/fxcodec/tiff/tiffmodule.cpp
index 5633e95..c02b55b 100644
--- a/core/fxcodec/tiff/tiffmodule.cpp
+++ b/core/fxcodec/tiff/tiffmodule.cpp
@@ -10,7 +10,6 @@
#include <memory>
#include "core/fxcodec/codec/cfx_codec_memory.h"
-#include "core/fxcodec/codec/codec_int.h"
#include "core/fxcodec/fx_codec.h"
#include "core/fxcrt/fx_safe_types.h"
#include "core/fxcrt/fx_stream.h"