Commit ab019a96b1fc2a189c34c7e4e627b01586ff2386
1 parent
09c3dc5f
Move QPDFObjectHandle stream methods implementation to QPDF_Stream.cc
Showing
4 changed files
with
197 additions
and
200 deletions
include/qpdf/ObjectHandle.hh
| ... | ... | @@ -39,7 +39,7 @@ namespace qpdf |
| 39 | 39 | class Dictionary; |
| 40 | 40 | class Stream; |
| 41 | 41 | |
| 42 | - enum typed : std::uint8_t { strict = 0, any_flag = 1, optional = 2, any = 3, error = 4}; | |
| 42 | + enum typed : std::uint8_t { strict = 0, any_flag = 1, optional = 2, any = 3, error = 4 }; | |
| 43 | 43 | |
| 44 | 44 | // Basehandle is only used as a base-class for QPDFObjectHandle like classes. Currently the only |
| 45 | 45 | // methods exposed in public API are operators to convert derived objects to QPDFObjectHandle, | ... | ... |
libqpdf/QPDFObjectHandle.cc
| ... | ... | @@ -957,180 +957,7 @@ QPDFObjectHandle::getUniqueResourceName( |
| 957 | 957 | |
| 958 | 958 | // Dictionary mutators are in QPDF_Dictionary.cc |
| 959 | 959 | |
| 960 | -// Stream accessors | |
| 961 | - | |
| 962 | -QPDFObjectHandle | |
| 963 | -QPDFObjectHandle::getDict() const | |
| 964 | -{ | |
| 965 | - return as_stream(error).getDict(); | |
| 966 | -} | |
| 967 | - | |
| 968 | -void | |
| 969 | -QPDFObjectHandle::setFilterOnWrite(bool val) | |
| 970 | -{ | |
| 971 | - as_stream(error).setFilterOnWrite(val); | |
| 972 | -} | |
| 973 | - | |
| 974 | -bool | |
| 975 | -QPDFObjectHandle::getFilterOnWrite() | |
| 976 | -{ | |
| 977 | - return as_stream(error).getFilterOnWrite(); | |
| 978 | -} | |
| 979 | - | |
| 980 | -bool | |
| 981 | -QPDFObjectHandle::isDataModified() | |
| 982 | -{ | |
| 983 | - return as_stream(error).isDataModified(); | |
| 984 | -} | |
| 985 | - | |
| 986 | -void | |
| 987 | -QPDFObjectHandle::replaceDict(QPDFObjectHandle const& new_dict) | |
| 988 | -{ | |
| 989 | - as_stream(error).replaceDict(new_dict); | |
| 990 | -} | |
| 991 | - | |
| 992 | -std::shared_ptr<Buffer> | |
| 993 | -QPDFObjectHandle::getStreamData(qpdf_stream_decode_level_e level) | |
| 994 | -{ | |
| 995 | - return as_stream(error).getStreamData(level); | |
| 996 | -} | |
| 997 | - | |
| 998 | -std::shared_ptr<Buffer> | |
| 999 | -QPDFObjectHandle::getRawStreamData() | |
| 1000 | -{ | |
| 1001 | - return as_stream(error).getRawStreamData(); | |
| 1002 | -} | |
| 1003 | - | |
| 1004 | -bool | |
| 1005 | -QPDFObjectHandle::pipeStreamData( | |
| 1006 | - Pipeline* p, | |
| 1007 | - bool* filtering_attempted, | |
| 1008 | - int encode_flags, | |
| 1009 | - qpdf_stream_decode_level_e decode_level, | |
| 1010 | - bool suppress_warnings, | |
| 1011 | - bool will_retry) | |
| 1012 | -{ | |
| 1013 | - return as_stream(error).pipeStreamData( | |
| 1014 | - p, filtering_attempted, encode_flags, decode_level, suppress_warnings, will_retry); | |
| 1015 | -} | |
| 1016 | - | |
| 1017 | -bool | |
| 1018 | -QPDFObjectHandle::pipeStreamData( | |
| 1019 | - Pipeline* p, | |
| 1020 | - int encode_flags, | |
| 1021 | - qpdf_stream_decode_level_e decode_level, | |
| 1022 | - bool suppress_warnings, | |
| 1023 | - bool will_retry) | |
| 1024 | -{ | |
| 1025 | - bool filtering_attempted; | |
| 1026 | - as_stream(error).pipeStreamData( | |
| 1027 | - p, &filtering_attempted, encode_flags, decode_level, suppress_warnings, will_retry); | |
| 1028 | - return filtering_attempted; | |
| 1029 | -} | |
| 1030 | - | |
| 1031 | -bool | |
| 1032 | -QPDFObjectHandle::pipeStreamData(Pipeline* p, bool filter, bool normalize, bool compress) | |
| 1033 | -{ | |
| 1034 | - int encode_flags = 0; | |
| 1035 | - qpdf_stream_decode_level_e decode_level = qpdf_dl_none; | |
| 1036 | - if (filter) { | |
| 1037 | - decode_level = qpdf_dl_generalized; | |
| 1038 | - if (normalize) { | |
| 1039 | - encode_flags |= qpdf_ef_normalize; | |
| 1040 | - } | |
| 1041 | - if (compress) { | |
| 1042 | - encode_flags |= qpdf_ef_compress; | |
| 1043 | - } | |
| 1044 | - } | |
| 1045 | - return pipeStreamData(p, encode_flags, decode_level, false); | |
| 1046 | -} | |
| 1047 | - | |
| 1048 | -void | |
| 1049 | -QPDFObjectHandle::replaceStreamData( | |
| 1050 | - std::shared_ptr<Buffer> data, | |
| 1051 | - QPDFObjectHandle const& filter, | |
| 1052 | - QPDFObjectHandle const& decode_parms) | |
| 1053 | -{ | |
| 1054 | - as_stream(error).replaceStreamData(data, filter, decode_parms); | |
| 1055 | -} | |
| 1056 | - | |
| 1057 | -void | |
| 1058 | -QPDFObjectHandle::replaceStreamData( | |
| 1059 | - std::string const& data, QPDFObjectHandle const& filter, QPDFObjectHandle const& decode_parms) | |
| 1060 | -{ | |
| 1061 | - auto b = std::make_shared<Buffer>(data.length()); | |
| 1062 | - unsigned char* bp = b->getBuffer(); | |
| 1063 | - if (bp) { | |
| 1064 | - memcpy(bp, data.c_str(), data.length()); | |
| 1065 | - } | |
| 1066 | - as_stream(error).replaceStreamData(b, filter, decode_parms); | |
| 1067 | -} | |
| 1068 | - | |
| 1069 | -void | |
| 1070 | -QPDFObjectHandle::replaceStreamData( | |
| 1071 | - std::shared_ptr<StreamDataProvider> provider, | |
| 1072 | - QPDFObjectHandle const& filter, | |
| 1073 | - QPDFObjectHandle const& decode_parms) | |
| 1074 | -{ | |
| 1075 | - as_stream(error).replaceStreamData(provider, filter, decode_parms); | |
| 1076 | -} | |
| 1077 | - | |
| 1078 | -namespace | |
| 1079 | -{ | |
| 1080 | - class FunctionProvider: public QPDFObjectHandle::StreamDataProvider | |
| 1081 | - { | |
| 1082 | - public: | |
| 1083 | - FunctionProvider(std::function<void(Pipeline*)> provider) : | |
| 1084 | - StreamDataProvider(false), | |
| 1085 | - p1(provider), | |
| 1086 | - p2(nullptr) | |
| 1087 | - { | |
| 1088 | - } | |
| 1089 | - FunctionProvider(std::function<bool(Pipeline*, bool, bool)> provider) : | |
| 1090 | - StreamDataProvider(true), | |
| 1091 | - p1(nullptr), | |
| 1092 | - p2(provider) | |
| 1093 | - { | |
| 1094 | - } | |
| 1095 | - | |
| 1096 | - void | |
| 1097 | - provideStreamData(QPDFObjGen const&, Pipeline* pipeline) override | |
| 1098 | - { | |
| 1099 | - p1(pipeline); | |
| 1100 | - } | |
| 1101 | - | |
| 1102 | - bool | |
| 1103 | - provideStreamData( | |
| 1104 | - QPDFObjGen const&, Pipeline* pipeline, bool suppress_warnings, bool will_retry) override | |
| 1105 | - { | |
| 1106 | - return p2(pipeline, suppress_warnings, will_retry); | |
| 1107 | - } | |
| 1108 | - | |
| 1109 | - private: | |
| 1110 | - std::function<void(Pipeline*)> p1; | |
| 1111 | - std::function<bool(Pipeline*, bool, bool)> p2; | |
| 1112 | - }; | |
| 1113 | -} // namespace | |
| 1114 | - | |
| 1115 | -void | |
| 1116 | -QPDFObjectHandle::replaceStreamData( | |
| 1117 | - std::function<void(Pipeline*)> provider, | |
| 1118 | - QPDFObjectHandle const& filter, | |
| 1119 | - QPDFObjectHandle const& decode_parms) | |
| 1120 | -{ | |
| 1121 | - auto sdp = std::shared_ptr<StreamDataProvider>(new FunctionProvider(provider)); | |
| 1122 | - as_stream(error).replaceStreamData(sdp, filter, decode_parms); | |
| 1123 | -} | |
| 1124 | - | |
| 1125 | -void | |
| 1126 | -QPDFObjectHandle::replaceStreamData( | |
| 1127 | - std::function<bool(Pipeline*, bool, bool)> provider, | |
| 1128 | - QPDFObjectHandle const& filter, | |
| 1129 | - QPDFObjectHandle const& decode_parms) | |
| 1130 | -{ | |
| 1131 | - auto sdp = std::shared_ptr<StreamDataProvider>(new FunctionProvider(provider)); | |
| 1132 | - as_stream(error).replaceStreamData(sdp, filter, decode_parms); | |
| 1133 | -} | |
| 960 | +// Stream accessors are in QPDF_Stream.cc | |
| 1134 | 961 | |
| 1135 | 962 | std::map<std::string, QPDFObjectHandle> |
| 1136 | 963 | QPDFObjectHandle::getPageImages() |
| ... | ... | @@ -1340,17 +1167,6 @@ QPDFObjectHandle::writeJSON( |
| 1340 | 1167 | writeJSON(json_version, jw, dereference_indirect); |
| 1341 | 1168 | } |
| 1342 | 1169 | |
| 1343 | -JSON | |
| 1344 | -QPDFObjectHandle::getStreamJSON( | |
| 1345 | - int json_version, | |
| 1346 | - qpdf_json_stream_data_e json_data, | |
| 1347 | - qpdf_stream_decode_level_e decode_level, | |
| 1348 | - Pipeline* p, | |
| 1349 | - std::string const& data_filename) | |
| 1350 | -{ | |
| 1351 | - return as_stream(error).getStreamJSON(json_version, json_data, decode_level, p, data_filename); | |
| 1352 | -} | |
| 1353 | - | |
| 1354 | 1170 | QPDFObjectHandle |
| 1355 | 1171 | QPDFObjectHandle::wrapInArray() |
| 1356 | 1172 | { | ... | ... |
libqpdf/QPDF_Stream.cc
| ... | ... | @@ -162,18 +162,6 @@ QPDF_Stream::writeJSON(int json_version, JSON::Writer& jw) |
| 162 | 162 | stream_dict.writeJSON(json_version, jw); |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | -QPDF_Stream* | |
| 166 | -Stream::stream() const | |
| 167 | -{ | |
| 168 | - if (obj) { | |
| 169 | - if (auto s = obj->as<QPDF_Stream>()) { | |
| 170 | - return s; | |
| 171 | - } | |
| 172 | - } | |
| 173 | - throw std::runtime_error("operation for stream attempted on object of type dictionary"); | |
| 174 | - return nullptr; // unreachable | |
| 175 | -} | |
| 176 | - | |
| 177 | 165 | JSON |
| 178 | 166 | Stream::getStreamJSON( |
| 179 | 167 | int json_version, |
| ... | ... | @@ -657,3 +645,187 @@ Stream::warn(std::string const& message) |
| 657 | 645 | auto s = stream(); |
| 658 | 646 | s->qpdf->warn(qpdf_e_damaged_pdf, "", s->parsed_offset, message); |
| 659 | 647 | } |
| 648 | + | |
| 649 | +QPDFObjectHandle | |
| 650 | +QPDFObjectHandle::getDict() const | |
| 651 | +{ | |
| 652 | + return as_stream(error).getDict(); | |
| 653 | +} | |
| 654 | + | |
| 655 | +void | |
| 656 | +QPDFObjectHandle::setFilterOnWrite(bool val) | |
| 657 | +{ | |
| 658 | + as_stream(error).setFilterOnWrite(val); | |
| 659 | +} | |
| 660 | + | |
| 661 | +bool | |
| 662 | +QPDFObjectHandle::getFilterOnWrite() | |
| 663 | +{ | |
| 664 | + return as_stream(error).getFilterOnWrite(); | |
| 665 | +} | |
| 666 | + | |
| 667 | +bool | |
| 668 | +QPDFObjectHandle::isDataModified() | |
| 669 | +{ | |
| 670 | + return as_stream(error).isDataModified(); | |
| 671 | +} | |
| 672 | + | |
| 673 | +void | |
| 674 | +QPDFObjectHandle::replaceDict(QPDFObjectHandle const& new_dict) | |
| 675 | +{ | |
| 676 | + as_stream(error).replaceDict(new_dict); | |
| 677 | +} | |
| 678 | + | |
| 679 | +std::shared_ptr<Buffer> | |
| 680 | +QPDFObjectHandle::getStreamData(qpdf_stream_decode_level_e level) | |
| 681 | +{ | |
| 682 | + return as_stream(error).getStreamData(level); | |
| 683 | +} | |
| 684 | + | |
| 685 | +std::shared_ptr<Buffer> | |
| 686 | +QPDFObjectHandle::getRawStreamData() | |
| 687 | +{ | |
| 688 | + return as_stream(error).getRawStreamData(); | |
| 689 | +} | |
| 690 | + | |
| 691 | +bool | |
| 692 | +QPDFObjectHandle::pipeStreamData( | |
| 693 | + Pipeline* p, | |
| 694 | + bool* filtering_attempted, | |
| 695 | + int encode_flags, | |
| 696 | + qpdf_stream_decode_level_e decode_level, | |
| 697 | + bool suppress_warnings, | |
| 698 | + bool will_retry) | |
| 699 | +{ | |
| 700 | + return as_stream(error).pipeStreamData( | |
| 701 | + p, filtering_attempted, encode_flags, decode_level, suppress_warnings, will_retry); | |
| 702 | +} | |
| 703 | + | |
| 704 | +bool | |
| 705 | +QPDFObjectHandle::pipeStreamData( | |
| 706 | + Pipeline* p, | |
| 707 | + int encode_flags, | |
| 708 | + qpdf_stream_decode_level_e decode_level, | |
| 709 | + bool suppress_warnings, | |
| 710 | + bool will_retry) | |
| 711 | +{ | |
| 712 | + bool filtering_attempted; | |
| 713 | + as_stream(error).pipeStreamData( | |
| 714 | + p, &filtering_attempted, encode_flags, decode_level, suppress_warnings, will_retry); | |
| 715 | + return filtering_attempted; | |
| 716 | +} | |
| 717 | + | |
| 718 | +bool | |
| 719 | +QPDFObjectHandle::pipeStreamData(Pipeline* p, bool filter, bool normalize, bool compress) | |
| 720 | +{ | |
| 721 | + int encode_flags = 0; | |
| 722 | + qpdf_stream_decode_level_e decode_level = qpdf_dl_none; | |
| 723 | + if (filter) { | |
| 724 | + decode_level = qpdf_dl_generalized; | |
| 725 | + if (normalize) { | |
| 726 | + encode_flags |= qpdf_ef_normalize; | |
| 727 | + } | |
| 728 | + if (compress) { | |
| 729 | + encode_flags |= qpdf_ef_compress; | |
| 730 | + } | |
| 731 | + } | |
| 732 | + return pipeStreamData(p, encode_flags, decode_level, false); | |
| 733 | +} | |
| 734 | + | |
| 735 | +void | |
| 736 | +QPDFObjectHandle::replaceStreamData( | |
| 737 | + std::shared_ptr<Buffer> data, | |
| 738 | + QPDFObjectHandle const& filter, | |
| 739 | + QPDFObjectHandle const& decode_parms) | |
| 740 | +{ | |
| 741 | + as_stream(error).replaceStreamData(data, filter, decode_parms); | |
| 742 | +} | |
| 743 | + | |
| 744 | +void | |
| 745 | +QPDFObjectHandle::replaceStreamData( | |
| 746 | + std::string const& data, QPDFObjectHandle const& filter, QPDFObjectHandle const& decode_parms) | |
| 747 | +{ | |
| 748 | + auto b = std::make_shared<Buffer>(data.length()); | |
| 749 | + unsigned char* bp = b->getBuffer(); | |
| 750 | + if (bp) { | |
| 751 | + memcpy(bp, data.c_str(), data.length()); | |
| 752 | + } | |
| 753 | + as_stream(error).replaceStreamData(b, filter, decode_parms); | |
| 754 | +} | |
| 755 | + | |
| 756 | +void | |
| 757 | +QPDFObjectHandle::replaceStreamData( | |
| 758 | + std::shared_ptr<StreamDataProvider> provider, | |
| 759 | + QPDFObjectHandle const& filter, | |
| 760 | + QPDFObjectHandle const& decode_parms) | |
| 761 | +{ | |
| 762 | + as_stream(error).replaceStreamData(provider, filter, decode_parms); | |
| 763 | +} | |
| 764 | + | |
| 765 | +namespace | |
| 766 | +{ | |
| 767 | + class FunctionProvider: public QPDFObjectHandle::StreamDataProvider | |
| 768 | + { | |
| 769 | + public: | |
| 770 | + FunctionProvider(std::function<void(Pipeline*)> provider) : | |
| 771 | + StreamDataProvider(false), | |
| 772 | + p1(provider), | |
| 773 | + p2(nullptr) | |
| 774 | + { | |
| 775 | + } | |
| 776 | + FunctionProvider(std::function<bool(Pipeline*, bool, bool)> provider) : | |
| 777 | + StreamDataProvider(true), | |
| 778 | + p1(nullptr), | |
| 779 | + p2(provider) | |
| 780 | + { | |
| 781 | + } | |
| 782 | + | |
| 783 | + void | |
| 784 | + provideStreamData(QPDFObjGen const&, Pipeline* pipeline) override | |
| 785 | + { | |
| 786 | + p1(pipeline); | |
| 787 | + } | |
| 788 | + | |
| 789 | + bool | |
| 790 | + provideStreamData( | |
| 791 | + QPDFObjGen const&, Pipeline* pipeline, bool suppress_warnings, bool will_retry) override | |
| 792 | + { | |
| 793 | + return p2(pipeline, suppress_warnings, will_retry); | |
| 794 | + } | |
| 795 | + | |
| 796 | + private: | |
| 797 | + std::function<void(Pipeline*)> p1; | |
| 798 | + std::function<bool(Pipeline*, bool, bool)> p2; | |
| 799 | + }; | |
| 800 | +} // namespace | |
| 801 | + | |
| 802 | +void | |
| 803 | +QPDFObjectHandle::replaceStreamData( | |
| 804 | + std::function<void(Pipeline*)> provider, | |
| 805 | + QPDFObjectHandle const& filter, | |
| 806 | + QPDFObjectHandle const& decode_parms) | |
| 807 | +{ | |
| 808 | + auto sdp = std::shared_ptr<StreamDataProvider>(new FunctionProvider(provider)); | |
| 809 | + as_stream(error).replaceStreamData(sdp, filter, decode_parms); | |
| 810 | +} | |
| 811 | + | |
| 812 | +void | |
| 813 | +QPDFObjectHandle::replaceStreamData( | |
| 814 | + std::function<bool(Pipeline*, bool, bool)> provider, | |
| 815 | + QPDFObjectHandle const& filter, | |
| 816 | + QPDFObjectHandle const& decode_parms) | |
| 817 | +{ | |
| 818 | + auto sdp = std::shared_ptr<StreamDataProvider>(new FunctionProvider(provider)); | |
| 819 | + as_stream(error).replaceStreamData(sdp, filter, decode_parms); | |
| 820 | +} | |
| 821 | + | |
| 822 | +JSON | |
| 823 | +QPDFObjectHandle::getStreamJSON( | |
| 824 | + int json_version, | |
| 825 | + qpdf_json_stream_data_e json_data, | |
| 826 | + qpdf_stream_decode_level_e decode_level, | |
| 827 | + Pipeline* p, | |
| 828 | + std::string const& data_filename) | |
| 829 | +{ | |
| 830 | + return as_stream(error).getStreamJSON(json_version, json_data, decode_level, p, data_filename); | |
| 831 | +} | ... | ... |
libqpdf/qpdf/QPDFObjectHandle_private.hh
| ... | ... | @@ -182,8 +182,17 @@ namespace qpdf |
| 182 | 182 | std::function<std::shared_ptr<QPDFStreamFilter>()> factory); |
| 183 | 183 | |
| 184 | 184 | private: |
| 185 | - QPDF_Stream* stream() const; | |
| 186 | - | |
| 185 | + QPDF_Stream* | |
| 186 | + stream() const | |
| 187 | + { | |
| 188 | + if (obj) { | |
| 189 | + if (auto s = obj->as<QPDF_Stream>()) { | |
| 190 | + return s; | |
| 191 | + } | |
| 192 | + } | |
| 193 | + throw std::runtime_error("operation for stream attempted on object of type dictionary"); | |
| 194 | + return nullptr; // unreachable | |
| 195 | + } | |
| 187 | 196 | bool filterable( |
| 188 | 197 | std::vector<std::shared_ptr<QPDFStreamFilter>>& filters, |
| 189 | 198 | bool& specialized_compression, | ... | ... |