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