summaryrefslogtreecommitdiff
path: root/core/fxcodec/codec
diff options
context:
space:
mode:
Diffstat (limited to 'core/fxcodec/codec')
-rw-r--r--core/fxcodec/codec/codec_int.h9
-rw-r--r--core/fxcodec/codec/fx_codec_jpx_opj.cpp19
-rw-r--r--core/fxcodec/codec/fx_codec_jpx_unittest.cpp106
3 files changed, 11 insertions, 123 deletions
diff --git a/core/fxcodec/codec/codec_int.h b/core/fxcodec/codec/codec_int.h
index edd5a67a25..4c451c733d 100644
--- a/core/fxcodec/codec/codec_int.h
+++ b/core/fxcodec/codec/codec_int.h
@@ -21,10 +21,10 @@
class CPDF_ColorSpace;
struct DecodeData {
- public:
- DecodeData(uint8_t* data, OPJ_SIZE_T size)
+ DecodeData(const uint8_t* data, OPJ_SIZE_T size)
: src_data(data), src_size(size), offset(0) {}
- uint8_t* src_data;
+
+ const uint8_t* src_data;
OPJ_SIZE_T src_size;
OPJ_SIZE_T offset;
};
@@ -35,9 +35,6 @@ void sycc420_to_rgb(opj_image_t* img);
OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
OPJ_SIZE_T nb_bytes,
void* p_user_data);
-OPJ_SIZE_T opj_write_from_memory(void* p_buffer,
- OPJ_SIZE_T nb_bytes,
- void* p_user_data);
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);
diff --git a/core/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/fxcodec/codec/fx_codec_jpx_opj.cpp
index fa163567f4..eccf876218 100644
--- a/core/fxcodec/codec/fx_codec_jpx_opj.cpp
+++ b/core/fxcodec/codec/fx_codec_jpx_opj.cpp
@@ -44,24 +44,6 @@ OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
return readlength;
}
-OPJ_SIZE_T opj_write_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);
- }
- // Writes 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 writeLength = nb_bytes < bufferLength ? nb_bytes : bufferLength;
- memcpy(&srcData->src_data[srcData->offset], p_buffer, writeLength);
- srcData->offset += writeLength;
- return writeLength;
-}
-
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) {
@@ -132,7 +114,6 @@ opj_stream_t* fx_opj_stream_create_memory_stream(DecodeData* data,
opj_stream_set_user_data(l_stream, data, nullptr);
opj_stream_set_user_data_length(l_stream, data->src_size);
opj_stream_set_read_function(l_stream, opj_read_from_memory);
- opj_stream_set_write_function(l_stream, opj_write_from_memory);
opj_stream_set_skip_function(l_stream, opj_skip_from_memory);
opj_stream_set_seek_function(l_stream, opj_seek_from_memory);
return l_stream;
diff --git a/core/fxcodec/codec/fx_codec_jpx_unittest.cpp b/core/fxcodec/codec/fx_codec_jpx_unittest.cpp
index 3ef14e62c3..4d0564af67 100644
--- a/core/fxcodec/codec/fx_codec_jpx_unittest.cpp
+++ b/core/fxcodec/codec/fx_codec_jpx_unittest.cpp
@@ -12,9 +12,8 @@
static const OPJ_OFF_T kSkipError = static_cast<OPJ_OFF_T>(-1);
static const OPJ_SIZE_T kReadError = static_cast<OPJ_SIZE_T>(-1);
-static const OPJ_SIZE_T kWriteError = static_cast<OPJ_SIZE_T>(-1);
-static unsigned char stream_data[] = {
+static const uint8_t stream_data[] = {
0x00, 0x01, 0x02, 0x03,
0x84, 0x85, 0x86, 0x87, // Include some hi-bytes, too.
};
@@ -43,19 +42,18 @@ TEST(fxcodec, CMYK_Rounding) {
}
TEST(fxcodec, DecodeDataNullDecodeData) {
- unsigned char buffer[16];
+ uint8_t buffer[16];
DecodeData* ptr = nullptr;
// Error codes, not segvs, should callers pass us a nullptr pointer.
EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), ptr));
- EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), ptr));
EXPECT_EQ(kSkipError, opj_skip_from_memory(1, ptr));
EXPECT_FALSE(opj_seek_from_memory(1, ptr));
}
TEST(fxcodec, DecodeDataNullStream) {
DecodeData dd(nullptr, 0);
- unsigned char buffer[16];
+ uint8_t buffer[16];
// Reads of size 0 do nothing but return an error code.
memset(buffer, 0xbd, sizeof(buffer));
@@ -67,12 +65,6 @@ TEST(fxcodec, DecodeDataNullStream) {
EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
EXPECT_EQ(0xbd, buffer[0]);
- // writes of size 0 do nothing but return an error code.
- EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, 0, &dd));
-
- // writes of nonzero size do nothing but return an error code.
- EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), &dd));
-
// Skips of size 0 always return an error code.
EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd));
@@ -88,7 +80,7 @@ TEST(fxcodec, DecodeDataNullStream) {
TEST(fxcodec, DecodeDataZeroSize) {
DecodeData dd(stream_data, 0);
- unsigned char buffer[16];
+ uint8_t buffer[16];
// Reads of size 0 do nothing but return an error code.
memset(buffer, 0xbd, sizeof(buffer));
@@ -100,12 +92,6 @@ TEST(fxcodec, DecodeDataZeroSize) {
EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
EXPECT_EQ(0xbd, buffer[0]);
- // writes of size 0 do nothing but return an error code.
- EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, 0, &dd));
-
- // writes of nonzero size do nothing but return an error code.
- EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), &dd));
-
// Skips of size 0 always return an error code.
EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd));
@@ -120,7 +106,7 @@ TEST(fxcodec, DecodeDataZeroSize) {
}
TEST(fxcodec, DecodeDataReadInBounds) {
- unsigned char buffer[16];
+ uint8_t buffer[16];
{
DecodeData dd(stream_data, sizeof(stream_data));
@@ -171,7 +157,7 @@ TEST(fxcodec, DecodeDataReadInBounds) {
}
TEST(fxcodec, DecodeDataReadBeyondBounds) {
- unsigned char buffer[16];
+ uint8_t buffer[16];
{
DecodeData dd(stream_data, sizeof(stream_data));
@@ -234,86 +220,10 @@ TEST(fxcodec, DecodeDataReadBeyondBounds) {
}
}
-TEST(fxcodec, DecodeDataWriteInBounds) {
- unsigned char stream[16];
- static unsigned char buffer_data[] = {
- 0x00, 0x01, 0x02, 0x03, 0x80, 0x80, 0x81, 0x82, 0x83, 0x84,
- };
- {
- // Pretend the stream can only hold 4 bytes.
- DecodeData dd(stream, 4);
-
- memset(stream, 0xbd, sizeof(stream));
- EXPECT_EQ(4u, opj_write_from_memory(buffer_data, 4, &dd));
- EXPECT_EQ(0x00, stream[0]);
- EXPECT_EQ(0x01, stream[1]);
- EXPECT_EQ(0x02, stream[2]);
- EXPECT_EQ(0x03, stream[3]);
- EXPECT_EQ(0xbd, stream[4]);
- }
- {
- // Pretend the stream can only hold 4 bytes.
- DecodeData dd(stream, 4);
-
- memset(stream, 0xbd, sizeof(stream));
- EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
- EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
- EXPECT_EQ(0x00, stream[0]);
- EXPECT_EQ(0x01, stream[1]);
- EXPECT_EQ(0x00, stream[2]);
- EXPECT_EQ(0x01, stream[3]);
- EXPECT_EQ(0xbd, stream[4]);
- }
-}
-
-TEST(fxcodec, DecodeDataWriteBeyondBounds) {
- unsigned char stream[16];
- static unsigned char buffer_data[] = {
- 0x10, 0x11, 0x12, 0x13, 0x94, 0x95, 0x96, 0x97,
- };
- {
- // Pretend the stream can only hold 4 bytes.
- DecodeData dd(stream, 4);
-
- // Write ending past EOF transfers up til EOF.
- memset(stream, 0xbd, sizeof(stream));
- EXPECT_EQ(4u, opj_write_from_memory(buffer_data, 5, &dd));
- EXPECT_EQ(0x10, stream[0]);
- EXPECT_EQ(0x11, stream[1]);
- EXPECT_EQ(0x12, stream[2]);
- EXPECT_EQ(0x13, stream[3]);
- EXPECT_EQ(0xbd, stream[4]);
-
- // Subsequent writes fail.
- memset(stream, 0xbd, sizeof(stream));
- EXPECT_EQ(kWriteError, opj_write_from_memory(buffer_data, 5, &dd));
- EXPECT_EQ(0xbd, stream[0]);
- }
- {
- // Pretend the stream can only hold 4 bytes.
- DecodeData dd(stream, 4);
-
- // Write ending past EOF (two steps) transfers up til EOF.
- memset(stream, 0xbd, sizeof(stream));
- EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
- EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 4, &dd));
- EXPECT_EQ(0x10, stream[0]);
- EXPECT_EQ(0x11, stream[1]);
- EXPECT_EQ(0x10, stream[2]);
- EXPECT_EQ(0x11, stream[3]);
- EXPECT_EQ(0xbd, stream[4]);
-
- // Subsequent writes fail.
- memset(stream, 0xbd, sizeof(stream));
- EXPECT_EQ(kWriteError, opj_write_from_memory(buffer_data, 5, &dd));
- EXPECT_EQ(0xbd, stream[0]);
- }
-}
-
// Note: Some care needs to be taken here because the skip/seek functions
// take OPJ_OFF_T's as arguments, which are typically a signed type.
TEST(fxcodec, DecodeDataSkip) {
- unsigned char buffer[16];
+ uint8_t buffer[16];
{
DecodeData dd(stream_data, sizeof(stream_data));
@@ -430,7 +340,7 @@ TEST(fxcodec, DecodeDataSkip) {
}
TEST(fxcodec, DecodeDataSeek) {
- unsigned char buffer[16];
+ uint8_t buffer[16];
DecodeData dd(stream_data, sizeof(stream_data));
// Seeking within buffer is allowed and read succeeds