| 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 "core/fxcodec/codec/ccodec_progressivedecoder.h" | 7 #include "core/fxcodec/codec/ccodec_progressivedecoder.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 } | 332 } |
| 333 } else { | 333 } else { |
| 334 uint32_t dwConsume = m_SrcSize - dwAvail; | 334 uint32_t dwConsume = m_SrcSize - dwAvail; |
| 335 if (dwAvail) { | 335 if (dwAvail) { |
| 336 FXSYS_memmove(m_pSrcBuf, m_pSrcBuf + dwConsume, dwAvail); | 336 FXSYS_memmove(m_pSrcBuf, m_pSrcBuf + dwConsume, dwAvail); |
| 337 } | 337 } |
| 338 if (dwSize > dwConsume) { | 338 if (dwSize > dwConsume) { |
| 339 dwSize = dwConsume; | 339 dwSize = dwConsume; |
| 340 } | 340 } |
| 341 } | 341 } |
| 342 if (!m_pFile->ReadBlock(m_pSrcBuf + dwAvail, m_offSet, dwSize)) { | 342 if (m_pFile->ReadBlock(m_pSrcBuf + dwAvail, m_offSet, dwSize) != dwSize && |
| 343 !m_pFile->IsEOF()) { |
| 343 err_status = FXCODEC_STATUS_ERR_READ; | 344 err_status = FXCODEC_STATUS_ERR_READ; |
| 344 return false; | 345 return false; |
| 345 } | 346 } |
| 346 m_offSet += dwSize; | 347 m_offSet += dwSize; |
| 347 pJpegModule->Input(m_pJpegContext, m_pSrcBuf, dwSize + dwAvail); | 348 pJpegModule->Input(m_pJpegContext, m_pSrcBuf, dwSize + dwAvail); |
| 348 return true; | 349 return true; |
| 349 } | 350 } |
| 350 | 351 |
| 351 bool CCodec_ProgressiveDecoder::PngReadHeaderFunc(void* pModule, | 352 bool CCodec_ProgressiveDecoder::PngReadHeaderFunc(void* pModule, |
| 352 int width, | 353 int width, |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 } | 606 } |
| 606 } else { | 607 } else { |
| 607 uint32_t dwConsume = m_SrcSize - dwAvail; | 608 uint32_t dwConsume = m_SrcSize - dwAvail; |
| 608 if (dwAvail) { | 609 if (dwAvail) { |
| 609 FXSYS_memmove(m_pSrcBuf, m_pSrcBuf + dwConsume, dwAvail); | 610 FXSYS_memmove(m_pSrcBuf, m_pSrcBuf + dwConsume, dwAvail); |
| 610 } | 611 } |
| 611 if (dwSize > dwConsume) { | 612 if (dwSize > dwConsume) { |
| 612 dwSize = dwConsume; | 613 dwSize = dwConsume; |
| 613 } | 614 } |
| 614 } | 615 } |
| 615 if (!m_pFile->ReadBlock(m_pSrcBuf + dwAvail, m_offSet, dwSize)) { | 616 if (m_pFile->ReadBlock(m_pSrcBuf + dwAvail, m_offSet, dwSize) != dwSize && |
| 617 !m_pFile->IsEOF()) { |
| 616 err_status = FXCODEC_STATUS_ERR_READ; | 618 err_status = FXCODEC_STATUS_ERR_READ; |
| 617 return false; | 619 return false; |
| 618 } | 620 } |
| 619 m_offSet += dwSize; | 621 m_offSet += dwSize; |
| 620 pGifModule->Input(m_pGifContext, m_pSrcBuf, dwSize + dwAvail); | 622 pGifModule->Input(m_pGifContext, m_pSrcBuf, dwSize + dwAvail); |
| 621 return true; | 623 return true; |
| 622 } | 624 } |
| 623 | 625 |
| 624 void CCodec_ProgressiveDecoder::GifRecordCurrentPositionCallback( | 626 void CCodec_ProgressiveDecoder::GifRecordCurrentPositionCallback( |
| 625 void* pModule, | 627 void* pModule, |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 } | 897 } |
| 896 } else { | 898 } else { |
| 897 uint32_t dwConsume = m_SrcSize - dwAvail; | 899 uint32_t dwConsume = m_SrcSize - dwAvail; |
| 898 if (dwAvail) { | 900 if (dwAvail) { |
| 899 FXSYS_memmove(m_pSrcBuf, m_pSrcBuf + dwConsume, dwAvail); | 901 FXSYS_memmove(m_pSrcBuf, m_pSrcBuf + dwConsume, dwAvail); |
| 900 } | 902 } |
| 901 if (dwSize > dwConsume) { | 903 if (dwSize > dwConsume) { |
| 902 dwSize = dwConsume; | 904 dwSize = dwConsume; |
| 903 } | 905 } |
| 904 } | 906 } |
| 905 if (!m_pFile->ReadBlock(m_pSrcBuf + dwAvail, m_offSet, dwSize)) { | 907 if (m_pFile->ReadBlock(m_pSrcBuf + dwAvail, m_offSet, dwSize) != dwSize && |
| 908 !m_pFile->IsEOF()) { |
| 906 err_status = FXCODEC_STATUS_ERR_READ; | 909 err_status = FXCODEC_STATUS_ERR_READ; |
| 907 return false; | 910 return false; |
| 908 } | 911 } |
| 909 m_offSet += dwSize; | 912 m_offSet += dwSize; |
| 910 pBmpModule->Input(m_pBmpContext, m_pSrcBuf, dwSize + dwAvail); | 913 pBmpModule->Input(m_pBmpContext, m_pSrcBuf, dwSize + dwAvail); |
| 911 return true; | 914 return true; |
| 912 } | 915 } |
| 913 | 916 |
| 914 bool CCodec_ProgressiveDecoder::BmpInputImagePositionBufCallback( | 917 bool CCodec_ProgressiveDecoder::BmpInputImagePositionBufCallback( |
| 915 void* pModule, | 918 void* pModule, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1057 return false; | 1060 return false; |
| 1058 } | 1061 } |
| 1059 pBmpModule->InputImagePositionBufCallback = | 1062 pBmpModule->InputImagePositionBufCallback = |
| 1060 BmpInputImagePositionBufCallback; | 1063 BmpInputImagePositionBufCallback; |
| 1061 pBmpModule->ReadScanlineCallback = BmpReadScanlineCallback; | 1064 pBmpModule->ReadScanlineCallback = BmpReadScanlineCallback; |
| 1062 m_pBmpContext = pBmpModule->Start((void*)this); | 1065 m_pBmpContext = pBmpModule->Start((void*)this); |
| 1063 if (!m_pBmpContext) { | 1066 if (!m_pBmpContext) { |
| 1064 m_status = FXCODEC_STATUS_ERR_MEMORY; | 1067 m_status = FXCODEC_STATUS_ERR_MEMORY; |
| 1065 return false; | 1068 return false; |
| 1066 } | 1069 } |
| 1067 bool bResult = m_pFile->ReadBlock(m_pSrcBuf, 0, size); | 1070 bool bResult = |
| 1071 m_pFile->ReadBlock(m_pSrcBuf, 0, size) == size || m_pFile->IsEOF(); |
| 1068 if (!bResult) { | 1072 if (!bResult) { |
| 1069 m_status = FXCODEC_STATUS_ERR_READ; | 1073 m_status = FXCODEC_STATUS_ERR_READ; |
| 1070 return false; | 1074 return false; |
| 1071 } | 1075 } |
| 1072 m_offSet += size; | 1076 m_offSet += size; |
| 1073 pBmpModule->Input(m_pBmpContext, m_pSrcBuf, size); | 1077 pBmpModule->Input(m_pBmpContext, m_pSrcBuf, size); |
| 1074 uint32_t* pPalette = nullptr; | 1078 uint32_t* pPalette = nullptr; |
| 1075 int32_t readResult = pBmpModule->ReadHeader( | 1079 int32_t readResult = pBmpModule->ReadHeader( |
| 1076 m_pBmpContext, &m_SrcWidth, &m_SrcHeight, &m_BmpIsTopBottom, | 1080 m_pBmpContext, &m_SrcWidth, &m_SrcHeight, &m_BmpIsTopBottom, |
| 1077 &m_SrcComponents, &m_SrcPaletteNumber, &pPalette, pAttribute); | 1081 &m_SrcComponents, &m_SrcPaletteNumber, &pPalette, pAttribute); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1109 CCodec_JpegModule* pJpegModule = m_pCodecMgr->GetJpegModule(); | 1113 CCodec_JpegModule* pJpegModule = m_pCodecMgr->GetJpegModule(); |
| 1110 if (!pJpegModule) { | 1114 if (!pJpegModule) { |
| 1111 m_status = FXCODEC_STATUS_ERR_MEMORY; | 1115 m_status = FXCODEC_STATUS_ERR_MEMORY; |
| 1112 return false; | 1116 return false; |
| 1113 } | 1117 } |
| 1114 m_pJpegContext = pJpegModule->Start(); | 1118 m_pJpegContext = pJpegModule->Start(); |
| 1115 if (!m_pJpegContext) { | 1119 if (!m_pJpegContext) { |
| 1116 m_status = FXCODEC_STATUS_ERR_MEMORY; | 1120 m_status = FXCODEC_STATUS_ERR_MEMORY; |
| 1117 return false; | 1121 return false; |
| 1118 } | 1122 } |
| 1119 bool bResult = m_pFile->ReadBlock(m_pSrcBuf, 0, size); | 1123 bool bResult = |
| 1124 m_pFile->ReadBlock(m_pSrcBuf, 0, size) == size || m_pFile->IsEOF(); |
| 1120 if (!bResult) { | 1125 if (!bResult) { |
| 1121 m_status = FXCODEC_STATUS_ERR_READ; | 1126 m_status = FXCODEC_STATUS_ERR_READ; |
| 1122 return false; | 1127 return false; |
| 1123 } | 1128 } |
| 1124 m_offSet += size; | 1129 m_offSet += size; |
| 1125 pJpegModule->Input(m_pJpegContext, m_pSrcBuf, size); | 1130 pJpegModule->Input(m_pJpegContext, m_pSrcBuf, size); |
| 1126 int32_t readResult = | 1131 int32_t readResult = |
| 1127 pJpegModule->ReadHeader(m_pJpegContext, &m_SrcWidth, &m_SrcHeight, | 1132 pJpegModule->ReadHeader(m_pJpegContext, &m_SrcWidth, &m_SrcHeight, |
| 1128 &m_SrcComponents, pAttribute); | 1133 &m_SrcComponents, pAttribute); |
| 1129 while (readResult == 2) { | 1134 while (readResult == 2) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1158 CCodec_ProgressiveDecoder::PngReadHeaderFunc; | 1163 CCodec_ProgressiveDecoder::PngReadHeaderFunc; |
| 1159 pPngModule->AskScanlineBufCallback = | 1164 pPngModule->AskScanlineBufCallback = |
| 1160 CCodec_ProgressiveDecoder::PngAskScanlineBufFunc; | 1165 CCodec_ProgressiveDecoder::PngAskScanlineBufFunc; |
| 1161 pPngModule->FillScanlineBufCompletedCallback = | 1166 pPngModule->FillScanlineBufCompletedCallback = |
| 1162 CCodec_ProgressiveDecoder::PngFillScanlineBufCompletedFunc; | 1167 CCodec_ProgressiveDecoder::PngFillScanlineBufCompletedFunc; |
| 1163 m_pPngContext = pPngModule->Start((void*)this); | 1168 m_pPngContext = pPngModule->Start((void*)this); |
| 1164 if (!m_pPngContext) { | 1169 if (!m_pPngContext) { |
| 1165 m_status = FXCODEC_STATUS_ERR_MEMORY; | 1170 m_status = FXCODEC_STATUS_ERR_MEMORY; |
| 1166 return false; | 1171 return false; |
| 1167 } | 1172 } |
| 1168 bool bResult = m_pFile->ReadBlock(m_pSrcBuf, 0, size); | 1173 bool bResult = |
| 1174 m_pFile->ReadBlock(m_pSrcBuf, 0, size) == size || m_pFile->IsEOF(); |
| 1169 if (!bResult) { | 1175 if (!bResult) { |
| 1170 m_status = FXCODEC_STATUS_ERR_READ; | 1176 m_status = FXCODEC_STATUS_ERR_READ; |
| 1171 return false; | 1177 return false; |
| 1172 } | 1178 } |
| 1173 m_offSet += size; | 1179 m_offSet += size; |
| 1174 bResult = pPngModule->Input(m_pPngContext, m_pSrcBuf, size, pAttribute); | 1180 bResult = pPngModule->Input(m_pPngContext, m_pSrcBuf, size, pAttribute); |
| 1175 while (bResult) { | 1181 while (bResult) { |
| 1176 uint32_t remain_size = (uint32_t)m_pFile->GetSize() - m_offSet; | 1182 uint32_t remain_size = (uint32_t)m_pFile->GetSize() - m_offSet; |
| 1177 uint32_t input_size = | 1183 uint32_t input_size = |
| 1178 remain_size > FXCODEC_BLOCK_SIZE ? FXCODEC_BLOCK_SIZE : remain_size; | 1184 remain_size > FXCODEC_BLOCK_SIZE ? FXCODEC_BLOCK_SIZE : remain_size; |
| 1179 if (input_size == 0) { | 1185 if (input_size == 0) { |
| 1180 if (m_pPngContext) { | 1186 if (m_pPngContext) { |
| 1181 pPngModule->Finish(m_pPngContext); | 1187 pPngModule->Finish(m_pPngContext); |
| 1182 } | 1188 } |
| 1183 m_pPngContext = nullptr; | 1189 m_pPngContext = nullptr; |
| 1184 m_status = FXCODEC_STATUS_ERR_FORMAT; | 1190 m_status = FXCODEC_STATUS_ERR_FORMAT; |
| 1185 return false; | 1191 return false; |
| 1186 } | 1192 } |
| 1187 if (m_pSrcBuf && input_size > m_SrcSize) { | 1193 if (m_pSrcBuf && input_size > m_SrcSize) { |
| 1188 FX_Free(m_pSrcBuf); | 1194 FX_Free(m_pSrcBuf); |
| 1189 m_pSrcBuf = FX_Alloc(uint8_t, input_size); | 1195 m_pSrcBuf = FX_Alloc(uint8_t, input_size); |
| 1190 FXSYS_memset(m_pSrcBuf, 0, input_size); | 1196 FXSYS_memset(m_pSrcBuf, 0, input_size); |
| 1191 m_SrcSize = input_size; | 1197 m_SrcSize = input_size; |
| 1192 } | 1198 } |
| 1193 bResult = m_pFile->ReadBlock(m_pSrcBuf, m_offSet, input_size); | 1199 bResult = |
| 1200 m_pFile->ReadBlock(m_pSrcBuf, m_offSet, input_size) == input_size || |
| 1201 m_pFile->IsEOF(); |
| 1194 if (!bResult) { | 1202 if (!bResult) { |
| 1195 m_status = FXCODEC_STATUS_ERR_READ; | 1203 m_status = FXCODEC_STATUS_ERR_READ; |
| 1196 return false; | 1204 return false; |
| 1197 } | 1205 } |
| 1198 m_offSet += input_size; | 1206 m_offSet += input_size; |
| 1199 bResult = | 1207 bResult = |
| 1200 pPngModule->Input(m_pPngContext, m_pSrcBuf, input_size, pAttribute); | 1208 pPngModule->Input(m_pPngContext, m_pSrcBuf, input_size, pAttribute); |
| 1201 } | 1209 } |
| 1202 ASSERT(!bResult); | 1210 ASSERT(!bResult); |
| 1203 if (m_pPngContext) { | 1211 if (m_pPngContext) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1222 CCodec_ProgressiveDecoder::GifAskLocalPaletteBufCallback; | 1230 CCodec_ProgressiveDecoder::GifAskLocalPaletteBufCallback; |
| 1223 pGifModule->InputRecordPositionBufCallback = | 1231 pGifModule->InputRecordPositionBufCallback = |
| 1224 CCodec_ProgressiveDecoder::GifInputRecordPositionBufCallback; | 1232 CCodec_ProgressiveDecoder::GifInputRecordPositionBufCallback; |
| 1225 pGifModule->ReadScanlineCallback = | 1233 pGifModule->ReadScanlineCallback = |
| 1226 CCodec_ProgressiveDecoder::GifReadScanlineCallback; | 1234 CCodec_ProgressiveDecoder::GifReadScanlineCallback; |
| 1227 m_pGifContext = pGifModule->Start((void*)this); | 1235 m_pGifContext = pGifModule->Start((void*)this); |
| 1228 if (!m_pGifContext) { | 1236 if (!m_pGifContext) { |
| 1229 m_status = FXCODEC_STATUS_ERR_MEMORY; | 1237 m_status = FXCODEC_STATUS_ERR_MEMORY; |
| 1230 return false; | 1238 return false; |
| 1231 } | 1239 } |
| 1232 bool bResult = m_pFile->ReadBlock(m_pSrcBuf, 0, size); | 1240 bool bResult = |
| 1241 m_pFile->ReadBlock(m_pSrcBuf, 0, size) == size || m_pFile->IsEOF(); |
| 1233 if (!bResult) { | 1242 if (!bResult) { |
| 1234 m_status = FXCODEC_STATUS_ERR_READ; | 1243 m_status = FXCODEC_STATUS_ERR_READ; |
| 1235 return false; | 1244 return false; |
| 1236 } | 1245 } |
| 1237 m_offSet += size; | 1246 m_offSet += size; |
| 1238 pGifModule->Input(m_pGifContext, m_pSrcBuf, size); | 1247 pGifModule->Input(m_pGifContext, m_pSrcBuf, size); |
| 1239 m_SrcComponents = 1; | 1248 m_SrcComponents = 1; |
| 1240 int32_t readResult = pGifModule->ReadHeader( | 1249 int32_t readResult = pGifModule->ReadHeader( |
| 1241 m_pGifContext, &m_SrcWidth, &m_SrcHeight, &m_GifPltNumber, | 1250 m_pGifContext, &m_SrcWidth, &m_SrcHeight, &m_GifPltNumber, |
| 1242 (void**)&m_pGifPalette, &m_GifBgIndex, nullptr); | 1251 (void**)&m_pGifPalette, &m_GifBgIndex, nullptr); |
| (...skipping 887 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2130 m_pFile = nullptr; | 2139 m_pFile = nullptr; |
| 2131 m_status = FXCODEC_STATUS_DECODE_FINISH; | 2140 m_status = FXCODEC_STATUS_DECODE_FINISH; |
| 2132 return m_status; | 2141 return m_status; |
| 2133 } | 2142 } |
| 2134 if (m_pSrcBuf && input_size > m_SrcSize) { | 2143 if (m_pSrcBuf && input_size > m_SrcSize) { |
| 2135 FX_Free(m_pSrcBuf); | 2144 FX_Free(m_pSrcBuf); |
| 2136 m_pSrcBuf = FX_Alloc(uint8_t, input_size); | 2145 m_pSrcBuf = FX_Alloc(uint8_t, input_size); |
| 2137 FXSYS_memset(m_pSrcBuf, 0, input_size); | 2146 FXSYS_memset(m_pSrcBuf, 0, input_size); |
| 2138 m_SrcSize = input_size; | 2147 m_SrcSize = input_size; |
| 2139 } | 2148 } |
| 2140 bool bResult = m_pFile->ReadBlock(m_pSrcBuf, m_offSet, input_size); | 2149 bool bResult = |
| 2150 m_pFile->ReadBlock(m_pSrcBuf, m_offSet, input_size) == input_size || |
| 2151 m_pFile->IsEOF(); |
| 2141 if (!bResult) { | 2152 if (!bResult) { |
| 2142 m_pDeviceBitmap = nullptr; | 2153 m_pDeviceBitmap = nullptr; |
| 2143 m_pFile = nullptr; | 2154 m_pFile = nullptr; |
| 2144 m_status = FXCODEC_STATUS_ERR_READ; | 2155 m_status = FXCODEC_STATUS_ERR_READ; |
| 2145 return m_status; | 2156 return m_status; |
| 2146 } | 2157 } |
| 2147 m_offSet += input_size; | 2158 m_offSet += input_size; |
| 2148 bResult = | 2159 bResult = |
| 2149 pPngModule->Input(m_pPngContext, m_pSrcBuf, input_size, nullptr); | 2160 pPngModule->Input(m_pPngContext, m_pSrcBuf, input_size, nullptr); |
| 2150 if (!bResult) { | 2161 if (!bResult) { |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2366 return m_status; | 2377 return m_status; |
| 2367 } | 2378 } |
| 2368 default: | 2379 default: |
| 2369 return FXCODEC_STATUS_ERROR; | 2380 return FXCODEC_STATUS_ERROR; |
| 2370 } | 2381 } |
| 2371 } | 2382 } |
| 2372 | 2383 |
| 2373 CCodec_ProgressiveDecoder* CCodec_ModuleMgr::CreateProgressiveDecoder() { | 2384 CCodec_ProgressiveDecoder* CCodec_ModuleMgr::CreateProgressiveDecoder() { |
| 2374 return new CCodec_ProgressiveDecoder(this); | 2385 return new CCodec_ProgressiveDecoder(this); |
| 2375 } | 2386 } |
| OLD | NEW |