| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "core/fxcodec/codec/codec_int.h" | 11 #include "core/fxcodec/codec/codec_int.h" |
| 12 #include "core/include/fpdfapi/fpdf_resource.h" | 12 #include "core/include/fpdfapi/fpdf_resource.h" |
| 13 #include "core/include/fxcodec/fx_codec.h" | 13 #include "core/include/fxcodec/fx_codec.h" |
| 14 #include "core/include/fxcrt/fx_safe_types.h" | 14 #include "core/include/fxcrt/fx_safe_types.h" |
| 15 #include "third_party/lcms2-2.6/include/lcms2.h" | 15 #include "third_party/lcms2-2.6/include/lcms2.h" |
| 16 #include "third_party/libopenjpeg20/openjpeg.h" | 16 #include "third_party/libopenjpeg20/openjpeg.h" |
| 17 | 17 |
| 18 static void fx_error_callback(const char* msg, void* client_data) { | 18 static void fx_error_callback(const char* msg, void* client_data) { |
| 19 (void)client_data; | 19 (void)client_data; |
| 20 } | 20 } |
| 21 static void fx_warning_callback(const char* msg, void* client_data) { | 21 static void fx_warning_callback(const char* msg, void* client_data) { |
| 22 (void)client_data; | 22 (void)client_data; |
| 23 } | 23 } |
| 24 static void fx_info_callback(const char* msg, void* client_data) { | 24 static void fx_info_callback(const char* msg, void* client_data) { |
| 25 (void)client_data; | 25 (void)client_data; |
| 26 } | 26 } |
| 27 |
| 27 OPJ_SIZE_T opj_read_from_memory(void* p_buffer, | 28 OPJ_SIZE_T opj_read_from_memory(void* p_buffer, |
| 28 OPJ_SIZE_T nb_bytes, | 29 OPJ_SIZE_T nb_bytes, |
| 29 void* p_user_data) { | 30 void* p_user_data) { |
| 30 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); | 31 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); |
| 31 if (!srcData || !srcData->src_data || srcData->src_size == 0) { | 32 if (!srcData || !srcData->src_data || srcData->src_size == 0) { |
| 32 return -1; | 33 return static_cast<OPJ_SIZE_T>(-1); |
| 33 } | 34 } |
| 34 // Reads at EOF return an error code. | 35 // Reads at EOF return an error code. |
| 35 if (srcData->offset >= srcData->src_size) { | 36 if (srcData->offset >= srcData->src_size) { |
| 36 return -1; | 37 return static_cast<OPJ_SIZE_T>(-1); |
| 37 } | 38 } |
| 38 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; | 39 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; |
| 39 OPJ_SIZE_T readlength = nb_bytes < bufferLength ? nb_bytes : bufferLength; | 40 OPJ_SIZE_T readlength = nb_bytes < bufferLength ? nb_bytes : bufferLength; |
| 40 memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength); | 41 memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength); |
| 41 srcData->offset += readlength; | 42 srcData->offset += readlength; |
| 42 return readlength; | 43 return readlength; |
| 43 } | 44 } |
| 45 |
| 44 OPJ_SIZE_T opj_write_from_memory(void* p_buffer, | 46 OPJ_SIZE_T opj_write_from_memory(void* p_buffer, |
| 45 OPJ_SIZE_T nb_bytes, | 47 OPJ_SIZE_T nb_bytes, |
| 46 void* p_user_data) { | 48 void* p_user_data) { |
| 47 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); | 49 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); |
| 48 if (!srcData || !srcData->src_data || srcData->src_size == 0) { | 50 if (!srcData || !srcData->src_data || srcData->src_size == 0) { |
| 49 return -1; | 51 return static_cast<OPJ_SIZE_T>(-1); |
| 50 } | 52 } |
| 51 // Writes at EOF return an error code. | 53 // Writes at EOF return an error code. |
| 52 if (srcData->offset >= srcData->src_size) { | 54 if (srcData->offset >= srcData->src_size) { |
| 53 return -1; | 55 return static_cast<OPJ_SIZE_T>(-1); |
| 54 } | 56 } |
| 55 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; | 57 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; |
| 56 OPJ_SIZE_T writeLength = nb_bytes < bufferLength ? nb_bytes : bufferLength; | 58 OPJ_SIZE_T writeLength = nb_bytes < bufferLength ? nb_bytes : bufferLength; |
| 57 memcpy(&srcData->src_data[srcData->offset], p_buffer, writeLength); | 59 memcpy(&srcData->src_data[srcData->offset], p_buffer, writeLength); |
| 58 srcData->offset += writeLength; | 60 srcData->offset += writeLength; |
| 59 return writeLength; | 61 return writeLength; |
| 60 } | 62 } |
| 63 |
| 61 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) { | 64 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) { |
| 62 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); | 65 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); |
| 63 if (!srcData || !srcData->src_data || srcData->src_size == 0) { | 66 if (!srcData || !srcData->src_data || srcData->src_size == 0) { |
| 64 return -1; | 67 return static_cast<OPJ_OFF_T>(-1); |
| 65 } | 68 } |
| 66 // Offsets are signed and may indicate a negative skip. Do not support this | 69 // Offsets are signed and may indicate a negative skip. Do not support this |
| 67 // because of the strange return convention where either bytes skipped or | 70 // because of the strange return convention where either bytes skipped or |
| 68 // -1 is returned. Following that convention, a successful relative seek of | 71 // -1 is returned. Following that convention, a successful relative seek of |
| 69 // -1 bytes would be required to to give the same result as the error case. | 72 // -1 bytes would be required to to give the same result as the error case. |
| 70 if (nb_bytes < 0) { | 73 if (nb_bytes < 0) { |
| 71 return -1; | 74 return static_cast<OPJ_OFF_T>(-1); |
| 72 } | 75 } |
| 73 // FIXME: use std::make_unsigned<OPJ_OFF_T>::type once c++11 lib is OK'd. | 76 // FIXME: use std::make_unsigned<OPJ_OFF_T>::type once c++11 lib is OK'd. |
| 74 uint64_t unsignedNbBytes = static_cast<uint64_t>(nb_bytes); | 77 uint64_t unsignedNbBytes = static_cast<uint64_t>(nb_bytes); |
| 75 // Additionally, the offset may take us beyond the range of a size_t (e.g. | 78 // Additionally, the offset may take us beyond the range of a size_t (e.g. |
| 76 // 32-bit platforms). If so, just clamp at EOF. | 79 // 32-bit platforms). If so, just clamp at EOF. |
| 77 if (unsignedNbBytes > | 80 if (unsignedNbBytes > |
| 78 std::numeric_limits<OPJ_SIZE_T>::max() - srcData->offset) { | 81 std::numeric_limits<OPJ_SIZE_T>::max() - srcData->offset) { |
| 79 srcData->offset = srcData->src_size; | 82 srcData->offset = srcData->src_size; |
| 80 } else { | 83 } else { |
| 81 OPJ_SIZE_T checkedNbBytes = static_cast<OPJ_SIZE_T>(unsignedNbBytes); | 84 OPJ_SIZE_T checkedNbBytes = static_cast<OPJ_SIZE_T>(unsignedNbBytes); |
| 82 // Otherwise, mimic fseek() semantics to always succeed, even past EOF, | 85 // Otherwise, mimic fseek() semantics to always succeed, even past EOF, |
| 83 // clamping at EOF. We can get away with this since we don't actually | 86 // clamping at EOF. We can get away with this since we don't actually |
| 84 // provide negative relative skips from beyond EOF back to inside the | 87 // provide negative relative skips from beyond EOF back to inside the |
| 85 // data, which would be the only reason to need to know exactly how far | 88 // data, which would be the only reason to need to know exactly how far |
| 86 // beyond EOF we are. | 89 // beyond EOF we are. |
| 87 srcData->offset = | 90 srcData->offset = |
| 88 std::min(srcData->offset + checkedNbBytes, srcData->src_size); | 91 std::min(srcData->offset + checkedNbBytes, srcData->src_size); |
| 89 } | 92 } |
| 90 return nb_bytes; | 93 return nb_bytes; |
| 91 } | 94 } |
| 95 |
| 92 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) { | 96 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) { |
| 93 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); | 97 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); |
| 94 if (!srcData || !srcData->src_data || srcData->src_size == 0) { | 98 if (!srcData || !srcData->src_data || srcData->src_size == 0) { |
| 95 return OPJ_FALSE; | 99 return OPJ_FALSE; |
| 96 } | 100 } |
| 97 // Offsets are signed and may indicate a negative position, which would | 101 // Offsets are signed and may indicate a negative position, which would |
| 98 // be before the start of the file. Do not support this. | 102 // be before the start of the file. Do not support this. |
| 99 if (nb_bytes < 0) { | 103 if (nb_bytes < 0) { |
| 100 return OPJ_FALSE; | 104 return OPJ_FALSE; |
| 101 } | 105 } |
| (...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 886 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, | 890 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, |
| 887 uint8_t* dest_data, | 891 uint8_t* dest_data, |
| 888 int pitch, | 892 int pitch, |
| 889 const std::vector<uint8_t>& offsets) { | 893 const std::vector<uint8_t>& offsets) { |
| 890 return pDecoder->Decode(dest_data, pitch, offsets); | 894 return pDecoder->Decode(dest_data, pitch, offsets); |
| 891 } | 895 } |
| 892 | 896 |
| 893 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { | 897 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { |
| 894 delete pDecoder; | 898 delete pDecoder; |
| 895 } | 899 } |
| OLD | NEW |