| 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 "../../../../third_party/base/nonstd_unique_ptr.h" | 7 #include "../../../../third_party/base/nonstd_unique_ptr.h" |
| 8 #include "../../../include/fxcodec/fx_codec.h" | 8 #include "../../../include/fxcodec/fx_codec.h" |
| 9 #include "../../fx_zlib.h" | 9 #include "../../fx_zlib.h" |
| 10 #include "codec_int.h" | 10 #include "codec_int.h" |
| (...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 } | 576 } |
| 577 TIFF_PredictLine(scan_line, row_size, BitsPerComponent, Colors, Columns)
; | 577 TIFF_PredictLine(scan_line, row_size, BitsPerComponent, Colors, Columns)
; |
| 578 } | 578 } |
| 579 return TRUE; | 579 return TRUE; |
| 580 } | 580 } |
| 581 class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder | 581 class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder |
| 582 { | 582 { |
| 583 public: | 583 public: |
| 584 CCodec_FlateScanlineDecoder(); | 584 CCodec_FlateScanlineDecoder(); |
| 585 ~CCodec_FlateScanlineDecoder(); | 585 ~CCodec_FlateScanlineDecoder(); |
| 586 FX_BOOL» » Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width,
int height, int nComps, int bpc, | 586 void Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, in
t nComps, int bpc, |
| 587 int predictor, int Colors, int BitsPerComponent, int Colu
mns); | 587 int predictor, int Colors, int BitsPerComponent, int Columns); |
| 588 virtual void Destroy() | 588 virtual void Destroy() |
| 589 { | 589 { |
| 590 delete this; | 590 delete this; |
| 591 } | 591 } |
| 592 virtual void v_DownScale(int dest_width, int dest_height) {} | 592 virtual void v_DownScale(int dest_width, int dest_height) {} |
| 593 virtual FX_BOOL v_Rewind(); | 593 virtual FX_BOOL v_Rewind(); |
| 594 virtual FX_LPBYTE v_GetNextLine(); | 594 virtual FX_LPBYTE v_GetNextLine(); |
| 595 virtual FX_DWORD GetSrcOffset(); | 595 virtual FX_DWORD GetSrcOffset(); |
| 596 void* m_pFlate; | 596 void* m_pFlate; |
| 597 FX_LPCBYTE m_SrcBuf; | 597 FX_LPCBYTE m_SrcBuf; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 623 if (m_pPredictBuffer) { | 623 if (m_pPredictBuffer) { |
| 624 FX_Free(m_pPredictBuffer); | 624 FX_Free(m_pPredictBuffer); |
| 625 } | 625 } |
| 626 if (m_pPredictRaw) { | 626 if (m_pPredictRaw) { |
| 627 FX_Free(m_pPredictRaw); | 627 FX_Free(m_pPredictRaw); |
| 628 } | 628 } |
| 629 if (m_pFlate) { | 629 if (m_pFlate) { |
| 630 FPDFAPI_FlateEnd(m_pFlate); | 630 FPDFAPI_FlateEnd(m_pFlate); |
| 631 } | 631 } |
| 632 } | 632 } |
| 633 FX_BOOL CCodec_FlateScanlineDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_siz
e, int width, int height, | 633 void CCodec_FlateScanlineDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size,
int width, int height, |
| 634 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) | 634 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) |
| 635 { | 635 { |
| 636 m_SrcBuf = src_buf; | 636 m_SrcBuf = src_buf; |
| 637 m_SrcSize = src_size; | 637 m_SrcSize = src_size; |
| 638 m_OutputWidth = m_OrigWidth = width; | 638 m_OutputWidth = m_OrigWidth = width; |
| 639 m_OutputHeight = m_OrigHeight = height; | 639 m_OutputHeight = m_OrigHeight = height; |
| 640 m_nComps = nComps; | 640 m_nComps = nComps; |
| 641 m_bpc = bpc; | 641 m_bpc = bpc; |
| 642 m_bColorTransformed = FALSE; | 642 m_bColorTransformed = FALSE; |
| 643 m_Pitch = (width * nComps * bpc + 7) / 8; | 643 m_Pitch = (width * nComps * bpc + 7) / 8; |
| 644 m_pScanline = FX_Alloc(FX_BYTE, m_Pitch); | 644 m_pScanline = FX_Alloc(FX_BYTE, m_Pitch); |
| 645 if (m_pScanline == NULL) { | |
| 646 return FALSE; | |
| 647 } | |
| 648 m_Predictor = 0; | 645 m_Predictor = 0; |
| 649 if (predictor) { | 646 if (predictor) { |
| 650 if (predictor >= 10) { | 647 if (predictor >= 10) { |
| 651 m_Predictor = 2; | 648 m_Predictor = 2; |
| 652 } else if (predictor == 2) { | 649 } else if (predictor == 2) { |
| 653 m_Predictor = 1; | 650 m_Predictor = 1; |
| 654 } | 651 } |
| 655 if (m_Predictor) { | 652 if (m_Predictor) { |
| 656 if (BitsPerComponent * Colors * Columns == 0) { | 653 if (BitsPerComponent * Colors * Columns == 0) { |
| 657 BitsPerComponent = m_bpc; | 654 BitsPerComponent = m_bpc; |
| 658 Colors = m_nComps; | 655 Colors = m_nComps; |
| 659 Columns = m_OrigWidth; | 656 Columns = m_OrigWidth; |
| 660 } | 657 } |
| 661 m_Colors = Colors; | 658 m_Colors = Colors; |
| 662 m_BitsPerComponent = BitsPerComponent; | 659 m_BitsPerComponent = BitsPerComponent; |
| 663 m_Columns = Columns; | 660 m_Columns = Columns; |
| 664 m_PredictPitch = (m_BitsPerComponent * m_Colors * m_Columns + 7) / 8
; | 661 m_PredictPitch = (m_BitsPerComponent * m_Colors * m_Columns + 7) / 8
; |
| 665 m_pLastLine = FX_Alloc(FX_BYTE, m_PredictPitch); | 662 m_pLastLine = FX_Alloc(FX_BYTE, m_PredictPitch); |
| 666 if (m_pLastLine == NULL) { | |
| 667 return FALSE; | |
| 668 } | |
| 669 m_pPredictRaw = FX_Alloc(FX_BYTE, m_PredictPitch + 1); | 663 m_pPredictRaw = FX_Alloc(FX_BYTE, m_PredictPitch + 1); |
| 670 if (m_pPredictRaw == NULL) { | |
| 671 return FALSE; | |
| 672 } | |
| 673 m_pPredictBuffer = FX_Alloc(FX_BYTE, m_PredictPitch); | 664 m_pPredictBuffer = FX_Alloc(FX_BYTE, m_PredictPitch); |
| 674 if (m_pPredictBuffer == NULL) { | |
| 675 return FALSE; | |
| 676 } | |
| 677 } | 665 } |
| 678 } | 666 } |
| 679 return TRUE; | |
| 680 } | 667 } |
| 681 FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind() | 668 FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind() |
| 682 { | 669 { |
| 683 if (m_pFlate) { | 670 if (m_pFlate) { |
| 684 FPDFAPI_FlateEnd(m_pFlate); | 671 FPDFAPI_FlateEnd(m_pFlate); |
| 685 } | 672 } |
| 686 m_pFlate = FPDFAPI_FlateInit(my_alloc_func, my_free_func); | 673 m_pFlate = FPDFAPI_FlateInit(my_alloc_func, my_free_func); |
| 687 if (m_pFlate == NULL) { | 674 if (m_pFlate == NULL) { |
| 688 return FALSE; | 675 return FALSE; |
| 689 } | 676 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 if (guess_size > kMaxInitialAllocSize) { | 732 if (guess_size > kMaxInitialAllocSize) { |
| 746 guess_size = kMaxInitialAllocSize; | 733 guess_size = kMaxInitialAllocSize; |
| 747 alloc_step = kMaxInitialAllocSize; | 734 alloc_step = kMaxInitialAllocSize; |
| 748 } | 735 } |
| 749 FX_DWORD buf_size = guess_size; | 736 FX_DWORD buf_size = guess_size; |
| 750 FX_DWORD last_buf_size = buf_size; | 737 FX_DWORD last_buf_size = buf_size; |
| 751 void* context = nullptr; | 738 void* context = nullptr; |
| 752 | 739 |
| 753 FX_LPBYTE guess_buf = FX_Alloc(FX_BYTE, guess_size + 1); | 740 FX_LPBYTE guess_buf = FX_Alloc(FX_BYTE, guess_size + 1); |
| 754 FX_LPBYTE cur_buf = guess_buf; | 741 FX_LPBYTE cur_buf = guess_buf; |
| 755 if (!guess_buf) | |
| 756 goto fail; | |
| 757 guess_buf[guess_size] = '\0'; | 742 guess_buf[guess_size] = '\0'; |
| 758 context = FPDFAPI_FlateInit(my_alloc_func, my_free_func); | 743 context = FPDFAPI_FlateInit(my_alloc_func, my_free_func); |
| 759 if (!context) | 744 if (!context) |
| 760 goto fail; | 745 goto fail; |
| 761 FPDFAPI_FlateInput(context, src_buf, src_size); | 746 FPDFAPI_FlateInput(context, src_buf, src_size); |
| 762 if (useOldImpl) { | 747 if (useOldImpl) { |
| 763 while (1) { | 748 while (1) { |
| 764 FX_INT32 ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); | 749 FX_INT32 ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); |
| 765 if (ret != Z_OK) | 750 if (ret != Z_OK) |
| 766 break; | 751 break; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 } | 787 } |
| 803 if (avail_buf_size != 0) { | 788 if (avail_buf_size != 0) { |
| 804 last_buf_size = buf_size - avail_buf_size; | 789 last_buf_size = buf_size - avail_buf_size; |
| 805 result_tmp_bufs.Add(cur_buf); | 790 result_tmp_bufs.Add(cur_buf); |
| 806 break; | 791 break; |
| 807 } | 792 } |
| 808 | 793 |
| 809 // |avail_buf_size| == 0 case. | 794 // |avail_buf_size| == 0 case. |
| 810 result_tmp_bufs.Add(cur_buf); | 795 result_tmp_bufs.Add(cur_buf); |
| 811 cur_buf = FX_Alloc(FX_BYTE, buf_size + 1); | 796 cur_buf = FX_Alloc(FX_BYTE, buf_size + 1); |
| 812 if (!cur_buf) { | |
| 813 for (FX_INT32 i = 0; i < result_tmp_bufs.GetSize(); i++) { | |
| 814 FX_Free(result_tmp_bufs[i]); | |
| 815 } | |
| 816 goto fail; | |
| 817 } | |
| 818 cur_buf[buf_size] = '\0'; | 797 cur_buf[buf_size] = '\0'; |
| 819 } | 798 } |
| 820 dest_size = FPDFAPI_FlateGetTotalOut(context); | 799 dest_size = FPDFAPI_FlateGetTotalOut(context); |
| 821 offset = FPDFAPI_FlateGetTotalIn(context); | 800 offset = FPDFAPI_FlateGetTotalIn(context); |
| 822 if (result_tmp_bufs.GetSize() == 1) { | 801 if (result_tmp_bufs.GetSize() == 1) { |
| 823 dest_buf = result_tmp_bufs[0]; | 802 dest_buf = result_tmp_bufs[0]; |
| 824 } else { | 803 } else { |
| 825 FX_LPBYTE result_buf = FX_Alloc(FX_BYTE, dest_size); | 804 FX_LPBYTE result_buf = FX_Alloc(FX_BYTE, dest_size); |
| 826 if (!result_buf) { | |
| 827 for (FX_INT32 i = 0; i < result_tmp_bufs.GetSize(); i++) { | |
| 828 FX_Free(result_tmp_bufs[i]); | |
| 829 } | |
| 830 goto fail; | |
| 831 } | |
| 832 FX_DWORD result_pos = 0; | 805 FX_DWORD result_pos = 0; |
| 833 for (FX_INT32 i = 0; i < result_tmp_bufs.GetSize(); i++) { | 806 for (FX_INT32 i = 0; i < result_tmp_bufs.GetSize(); i++) { |
| 834 FX_LPBYTE tmp_buf = result_tmp_bufs[i]; | 807 FX_LPBYTE tmp_buf = result_tmp_bufs[i]; |
| 835 FX_DWORD tmp_buf_size = buf_size; | 808 FX_DWORD tmp_buf_size = buf_size; |
| 836 if (i == result_tmp_bufs.GetSize() - 1) { | 809 if (i == result_tmp_bufs.GetSize() - 1) { |
| 837 tmp_buf_size = last_buf_size; | 810 tmp_buf_size = last_buf_size; |
| 838 } | 811 } |
| 839 FXSYS_memcpy32(result_buf + result_pos, tmp_buf, tmp_buf_size); | 812 FXSYS_memcpy32(result_buf + result_pos, tmp_buf, tmp_buf_size); |
| 840 result_pos += tmp_buf_size; | 813 result_pos += tmp_buf_size; |
| 841 FX_Free(result_tmp_bufs[i]); | 814 FX_Free(result_tmp_bufs[i]); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 880 offset = src_size; | 853 offset = src_size; |
| 881 int err = decoder->Decode(NULL, dest_size, src_buf, offset, | 854 int err = decoder->Decode(NULL, dest_size, src_buf, offset, |
| 882 bEarlyChange); | 855 bEarlyChange); |
| 883 if (err || dest_size == 0 || dest_size + 1 < dest_size) { | 856 if (err || dest_size == 0 || dest_size + 1 < dest_size) { |
| 884 return -1; | 857 return -1; |
| 885 } | 858 } |
| 886 } | 859 } |
| 887 { | 860 { |
| 888 nonstd::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); | 861 nonstd::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); |
| 889 dest_buf = FX_Alloc( FX_BYTE, dest_size + 1); | 862 dest_buf = FX_Alloc( FX_BYTE, dest_size + 1); |
| 890 if (dest_buf == NULL) { | |
| 891 return -1; | |
| 892 } | |
| 893 dest_buf[dest_size] = '\0'; | 863 dest_buf[dest_size] = '\0'; |
| 894 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); | 864 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); |
| 895 } | 865 } |
| 896 } else { | 866 } else { |
| 897 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size,
offset); | 867 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size,
offset); |
| 898 } | 868 } |
| 899 if (predictor_type == 0) { | 869 if (predictor_type == 0) { |
| 900 return offset; | 870 return offset; |
| 901 } | 871 } |
| 902 FX_BOOL ret = TRUE; | 872 FX_BOOL ret = TRUE; |
| 903 if (predictor_type == 2) { | 873 if (predictor_type == 2) { |
| 904 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, | 874 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, |
| 905 Columns); | 875 Columns); |
| 906 } else if (predictor_type == 1) { | 876 } else if (predictor_type == 1) { |
| 907 ret = TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, | 877 ret = TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, |
| 908 Columns); | 878 Columns); |
| 909 } | 879 } |
| 910 return ret ? offset : -1; | 880 return ret ? offset : -1; |
| 911 } | 881 } |
| 912 FX_BOOL CCodec_FlateModule::Encode(const FX_BYTE* src_buf, FX_DWORD src_size, | 882 FX_BOOL CCodec_FlateModule::Encode(const FX_BYTE* src_buf, FX_DWORD src_size, |
| 913 int predictor, int Colors, int BitsPerCompone
nt, int Columns, | 883 int predictor, int Colors, int BitsPerCompone
nt, int Columns, |
| 914 FX_LPBYTE& dest_buf, FX_DWORD& dest_size) | 884 FX_LPBYTE& dest_buf, FX_DWORD& dest_size) |
| 915 { | 885 { |
| 916 if (predictor != 2 && predictor < 10) { | 886 if (predictor != 2 && predictor < 10) { |
| 917 return Encode(src_buf, src_size, dest_buf, dest_size); | 887 return Encode(src_buf, src_size, dest_buf, dest_size); |
| 918 } | 888 } |
| 919 FX_LPBYTE pSrcBuf = NULL; | 889 FX_LPBYTE pSrcBuf = NULL; |
| 920 pSrcBuf = FX_Alloc(FX_BYTE, src_size); | 890 pSrcBuf = FX_Alloc(FX_BYTE, src_size); |
| 921 if (pSrcBuf == NULL) { | |
| 922 return FALSE; | |
| 923 } | |
| 924 FXSYS_memcpy32(pSrcBuf, src_buf, src_size); | 891 FXSYS_memcpy32(pSrcBuf, src_buf, src_size); |
| 925 FX_BOOL ret = TRUE; | 892 FX_BOOL ret = TRUE; |
| 926 if (predictor == 2) { | 893 if (predictor == 2) { |
| 927 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, | 894 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, |
| 928 Columns); | 895 Columns); |
| 929 } else if (predictor >= 10) { | 896 } else if (predictor >= 10) { |
| 930 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, | 897 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, |
| 931 BitsPerComponent, Columns); | 898 BitsPerComponent, Columns); |
| 932 } | 899 } |
| 933 if (ret) | 900 if (ret) |
| 934 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); | 901 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); |
| 935 FX_Free(pSrcBuf); | 902 FX_Free(pSrcBuf); |
| 936 return ret; | 903 return ret; |
| 937 } | 904 } |
| 938 FX_BOOL CCodec_FlateModule::Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPB
YTE& dest_buf, FX_DWORD& dest_size) | 905 FX_BOOL CCodec_FlateModule::Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPB
YTE& dest_buf, FX_DWORD& dest_size) |
| 939 { | 906 { |
| 940 dest_size = src_size + src_size / 1000 + 12; | 907 dest_size = src_size + src_size / 1000 + 12; |
| 941 dest_buf = FX_Alloc( FX_BYTE, dest_size); | 908 dest_buf = FX_Alloc( FX_BYTE, dest_size); |
| 942 if (dest_buf == NULL) { | |
| 943 return FALSE; | |
| 944 } | |
| 945 unsigned long temp_size = dest_size; | 909 unsigned long temp_size = dest_size; |
| 946 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); | 910 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); |
| 947 dest_size = (FX_DWORD)temp_size; | 911 dest_size = (FX_DWORD)temp_size; |
| 948 return TRUE; | 912 return TRUE; |
| 949 } | 913 } |
| OLD | NEW |