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