hexapdf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTERS +3 -0
- data/LICENSE +26 -0
- data/README.md +88 -0
- data/Rakefile +121 -0
- data/VERSION +1 -0
- data/agpl-3.0.txt +661 -0
- data/bin/hexapdf +6 -0
- data/data/hexapdf/afm/Courier-Bold.afm +342 -0
- data/data/hexapdf/afm/Courier-BoldOblique.afm +342 -0
- data/data/hexapdf/afm/Courier-Oblique.afm +342 -0
- data/data/hexapdf/afm/Courier.afm +342 -0
- data/data/hexapdf/afm/Helvetica-Bold.afm +2827 -0
- data/data/hexapdf/afm/Helvetica-BoldOblique.afm +2827 -0
- data/data/hexapdf/afm/Helvetica-Oblique.afm +3051 -0
- data/data/hexapdf/afm/Helvetica.afm +3051 -0
- data/data/hexapdf/afm/MustRead.html +1 -0
- data/data/hexapdf/afm/Symbol.afm +213 -0
- data/data/hexapdf/afm/Times-Bold.afm +2588 -0
- data/data/hexapdf/afm/Times-BoldItalic.afm +2384 -0
- data/data/hexapdf/afm/Times-Italic.afm +2667 -0
- data/data/hexapdf/afm/Times-Roman.afm +2419 -0
- data/data/hexapdf/afm/ZapfDingbats.afm +225 -0
- data/data/hexapdf/encoding/glyphlist.txt +4305 -0
- data/data/hexapdf/encoding/zapfdingbats.txt +225 -0
- data/examples/arc.rb +50 -0
- data/examples/graphics.rb +274 -0
- data/examples/hello_world.rb +16 -0
- data/examples/machupicchu.jpg +0 -0
- data/examples/merging.rb +24 -0
- data/examples/optimizing.rb +20 -0
- data/examples/show_char_bboxes.rb +55 -0
- data/examples/standard_pdf_fonts.rb +72 -0
- data/examples/truetype.rb +45 -0
- data/lib/hexapdf/cli/extract.rb +128 -0
- data/lib/hexapdf/cli/info.rb +121 -0
- data/lib/hexapdf/cli/inspect.rb +157 -0
- data/lib/hexapdf/cli/modify.rb +218 -0
- data/lib/hexapdf/cli.rb +121 -0
- data/lib/hexapdf/configuration.rb +392 -0
- data/lib/hexapdf/content/canvas.rb +1974 -0
- data/lib/hexapdf/content/color_space.rb +364 -0
- data/lib/hexapdf/content/graphic_object/arc.rb +267 -0
- data/lib/hexapdf/content/graphic_object/endpoint_arc.rb +208 -0
- data/lib/hexapdf/content/graphic_object/solid_arc.rb +173 -0
- data/lib/hexapdf/content/graphic_object.rb +81 -0
- data/lib/hexapdf/content/graphics_state.rb +579 -0
- data/lib/hexapdf/content/operator.rb +1072 -0
- data/lib/hexapdf/content/parser.rb +204 -0
- data/lib/hexapdf/content/processor.rb +451 -0
- data/lib/hexapdf/content/transformation_matrix.rb +172 -0
- data/lib/hexapdf/content.rb +47 -0
- data/lib/hexapdf/data_dir.rb +51 -0
- data/lib/hexapdf/dictionary.rb +303 -0
- data/lib/hexapdf/dictionary_fields.rb +382 -0
- data/lib/hexapdf/document.rb +589 -0
- data/lib/hexapdf/document_utils.rb +209 -0
- data/lib/hexapdf/encryption/aes.rb +206 -0
- data/lib/hexapdf/encryption/arc4.rb +93 -0
- data/lib/hexapdf/encryption/fast_aes.rb +79 -0
- data/lib/hexapdf/encryption/fast_arc4.rb +67 -0
- data/lib/hexapdf/encryption/identity.rb +63 -0
- data/lib/hexapdf/encryption/ruby_aes.rb +447 -0
- data/lib/hexapdf/encryption/ruby_arc4.rb +96 -0
- data/lib/hexapdf/encryption/security_handler.rb +494 -0
- data/lib/hexapdf/encryption/standard_security_handler.rb +616 -0
- data/lib/hexapdf/encryption.rb +94 -0
- data/lib/hexapdf/error.rb +73 -0
- data/lib/hexapdf/filter/ascii85_decode.rb +160 -0
- data/lib/hexapdf/filter/ascii_hex_decode.rb +87 -0
- data/lib/hexapdf/filter/dct_decode.rb +57 -0
- data/lib/hexapdf/filter/encryption.rb +59 -0
- data/lib/hexapdf/filter/flate_decode.rb +93 -0
- data/lib/hexapdf/filter/jpx_decode.rb +56 -0
- data/lib/hexapdf/filter/lzw_decode.rb +191 -0
- data/lib/hexapdf/filter/predictor.rb +266 -0
- data/lib/hexapdf/filter/run_length_decode.rb +108 -0
- data/lib/hexapdf/filter.rb +176 -0
- data/lib/hexapdf/font/cmap/parser.rb +146 -0
- data/lib/hexapdf/font/cmap/writer.rb +176 -0
- data/lib/hexapdf/font/cmap.rb +90 -0
- data/lib/hexapdf/font/encoding/base.rb +77 -0
- data/lib/hexapdf/font/encoding/difference_encoding.rb +64 -0
- data/lib/hexapdf/font/encoding/glyph_list.rb +150 -0
- data/lib/hexapdf/font/encoding/mac_expert_encoding.rb +221 -0
- data/lib/hexapdf/font/encoding/mac_roman_encoding.rb +265 -0
- data/lib/hexapdf/font/encoding/standard_encoding.rb +205 -0
- data/lib/hexapdf/font/encoding/symbol_encoding.rb +244 -0
- data/lib/hexapdf/font/encoding/win_ansi_encoding.rb +280 -0
- data/lib/hexapdf/font/encoding/zapf_dingbats_encoding.rb +250 -0
- data/lib/hexapdf/font/encoding.rb +68 -0
- data/lib/hexapdf/font/true_type/font.rb +179 -0
- data/lib/hexapdf/font/true_type/table/cmap.rb +103 -0
- data/lib/hexapdf/font/true_type/table/cmap_subtable.rb +384 -0
- data/lib/hexapdf/font/true_type/table/directory.rb +92 -0
- data/lib/hexapdf/font/true_type/table/glyf.rb +166 -0
- data/lib/hexapdf/font/true_type/table/head.rb +143 -0
- data/lib/hexapdf/font/true_type/table/hhea.rb +109 -0
- data/lib/hexapdf/font/true_type/table/hmtx.rb +79 -0
- data/lib/hexapdf/font/true_type/table/loca.rb +79 -0
- data/lib/hexapdf/font/true_type/table/maxp.rb +112 -0
- data/lib/hexapdf/font/true_type/table/name.rb +218 -0
- data/lib/hexapdf/font/true_type/table/os2.rb +200 -0
- data/lib/hexapdf/font/true_type/table/post.rb +230 -0
- data/lib/hexapdf/font/true_type/table.rb +155 -0
- data/lib/hexapdf/font/true_type.rb +48 -0
- data/lib/hexapdf/font/true_type_wrapper.rb +240 -0
- data/lib/hexapdf/font/type1/afm_parser.rb +230 -0
- data/lib/hexapdf/font/type1/character_metrics.rb +67 -0
- data/lib/hexapdf/font/type1/font.rb +123 -0
- data/lib/hexapdf/font/type1/font_metrics.rb +117 -0
- data/lib/hexapdf/font/type1/pfb_parser.rb +71 -0
- data/lib/hexapdf/font/type1.rb +52 -0
- data/lib/hexapdf/font/type1_wrapper.rb +193 -0
- data/lib/hexapdf/font_loader/from_configuration.rb +70 -0
- data/lib/hexapdf/font_loader/standard14.rb +98 -0
- data/lib/hexapdf/font_loader.rb +85 -0
- data/lib/hexapdf/font_utils.rb +89 -0
- data/lib/hexapdf/image_loader/jpeg.rb +166 -0
- data/lib/hexapdf/image_loader/pdf.rb +89 -0
- data/lib/hexapdf/image_loader/png.rb +410 -0
- data/lib/hexapdf/image_loader.rb +68 -0
- data/lib/hexapdf/importer.rb +139 -0
- data/lib/hexapdf/name_tree_node.rb +78 -0
- data/lib/hexapdf/number_tree_node.rb +67 -0
- data/lib/hexapdf/object.rb +363 -0
- data/lib/hexapdf/parser.rb +349 -0
- data/lib/hexapdf/rectangle.rb +99 -0
- data/lib/hexapdf/reference.rb +98 -0
- data/lib/hexapdf/revision.rb +206 -0
- data/lib/hexapdf/revisions.rb +194 -0
- data/lib/hexapdf/serializer.rb +326 -0
- data/lib/hexapdf/stream.rb +279 -0
- data/lib/hexapdf/task/dereference.rb +109 -0
- data/lib/hexapdf/task/optimize.rb +230 -0
- data/lib/hexapdf/task.rb +68 -0
- data/lib/hexapdf/tokenizer.rb +406 -0
- data/lib/hexapdf/type/catalog.rb +107 -0
- data/lib/hexapdf/type/embedded_file.rb +87 -0
- data/lib/hexapdf/type/file_specification.rb +232 -0
- data/lib/hexapdf/type/font.rb +81 -0
- data/lib/hexapdf/type/font_descriptor.rb +109 -0
- data/lib/hexapdf/type/font_simple.rb +190 -0
- data/lib/hexapdf/type/font_true_type.rb +47 -0
- data/lib/hexapdf/type/font_type1.rb +162 -0
- data/lib/hexapdf/type/form.rb +103 -0
- data/lib/hexapdf/type/graphics_state_parameter.rb +79 -0
- data/lib/hexapdf/type/image.rb +73 -0
- data/lib/hexapdf/type/info.rb +70 -0
- data/lib/hexapdf/type/names.rb +69 -0
- data/lib/hexapdf/type/object_stream.rb +224 -0
- data/lib/hexapdf/type/page.rb +355 -0
- data/lib/hexapdf/type/page_tree_node.rb +269 -0
- data/lib/hexapdf/type/resources.rb +212 -0
- data/lib/hexapdf/type/trailer.rb +128 -0
- data/lib/hexapdf/type/viewer_preferences.rb +73 -0
- data/lib/hexapdf/type/xref_stream.rb +204 -0
- data/lib/hexapdf/type.rb +67 -0
- data/lib/hexapdf/utils/bit_field.rb +87 -0
- data/lib/hexapdf/utils/bit_stream.rb +148 -0
- data/lib/hexapdf/utils/lru_cache.rb +65 -0
- data/lib/hexapdf/utils/math_helpers.rb +55 -0
- data/lib/hexapdf/utils/object_hash.rb +130 -0
- data/lib/hexapdf/utils/pdf_doc_encoding.rb +93 -0
- data/lib/hexapdf/utils/sorted_tree_node.rb +339 -0
- data/lib/hexapdf/version.rb +39 -0
- data/lib/hexapdf/writer.rb +199 -0
- data/lib/hexapdf/xref_section.rb +152 -0
- data/lib/hexapdf.rb +34 -0
- data/man/man1/hexapdf.1 +249 -0
- data/test/data/aes-test-vectors/CBCGFSbox-128-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-128-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-192-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-192-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-256-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-256-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-128-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-128-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-192-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-192-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-256-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-256-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-128-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-128-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-192-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-192-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-256-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-256-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-128-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-128-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-192-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-192-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-256-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-256-encrypt.data.gz +0 -0
- data/test/data/fonts/Ubuntu-Title.ttf +0 -0
- data/test/data/images/cmyk.jpg +0 -0
- data/test/data/images/fillbytes.jpg +0 -0
- data/test/data/images/gray.jpg +0 -0
- data/test/data/images/greyscale-1bit.png +0 -0
- data/test/data/images/greyscale-2bit.png +0 -0
- data/test/data/images/greyscale-4bit.png +0 -0
- data/test/data/images/greyscale-8bit.png +0 -0
- data/test/data/images/greyscale-alpha-8bit.png +0 -0
- data/test/data/images/greyscale-trns-8bit.png +0 -0
- data/test/data/images/greyscale-with-gamma1.0.png +0 -0
- data/test/data/images/greyscale-with-gamma1.5.png +0 -0
- data/test/data/images/indexed-1bit.png +0 -0
- data/test/data/images/indexed-2bit.png +0 -0
- data/test/data/images/indexed-4bit.png +0 -0
- data/test/data/images/indexed-8bit.png +0 -0
- data/test/data/images/indexed-alpha-4bit.png +0 -0
- data/test/data/images/indexed-alpha-8bit.png +0 -0
- data/test/data/images/rgb.jpg +0 -0
- data/test/data/images/truecolour-8bit.png +0 -0
- data/test/data/images/truecolour-alpha-8bit.png +0 -0
- data/test/data/images/truecolour-gama-chrm-8bit.png +0 -0
- data/test/data/images/truecolour-srgb-8bit.png +0 -0
- data/test/data/minimal.pdf +44 -0
- data/test/data/standard-security-handler/README +9 -0
- data/test/data/standard-security-handler/bothpwd-aes-128bit-V4.pdf +44 -0
- data/test/data/standard-security-handler/bothpwd-aes-256bit-V5.pdf +0 -0
- data/test/data/standard-security-handler/bothpwd-arc4-128bit-V2.pdf +43 -0
- data/test/data/standard-security-handler/bothpwd-arc4-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/bothpwd-arc4-40bit-V1.pdf +0 -0
- data/test/data/standard-security-handler/nopwd-aes-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/nopwd-aes-256bit-V5.pdf +0 -0
- data/test/data/standard-security-handler/nopwd-arc4-128bit-V2.pdf +43 -0
- data/test/data/standard-security-handler/nopwd-arc4-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/nopwd-arc4-40bit-V1.pdf +43 -0
- data/test/data/standard-security-handler/ownerpwd-aes-128bit-V4.pdf +0 -0
- data/test/data/standard-security-handler/ownerpwd-aes-256bit-V5.pdf +43 -0
- data/test/data/standard-security-handler/ownerpwd-arc4-128bit-V2.pdf +43 -0
- data/test/data/standard-security-handler/ownerpwd-arc4-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/ownerpwd-arc4-40bit-V1.pdf +43 -0
- data/test/data/standard-security-handler/userpwd-aes-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/userpwd-aes-256bit-V5.pdf +43 -0
- data/test/data/standard-security-handler/userpwd-arc4-128bit-V2.pdf +0 -0
- data/test/data/standard-security-handler/userpwd-arc4-128bit-V4.pdf +0 -0
- data/test/data/standard-security-handler/userpwd-arc4-40bit-V1.pdf +43 -0
- data/test/hexapdf/common_tokenizer_tests.rb +204 -0
- data/test/hexapdf/content/common.rb +31 -0
- data/test/hexapdf/content/graphic_object/test_arc.rb +93 -0
- data/test/hexapdf/content/graphic_object/test_endpoint_arc.rb +91 -0
- data/test/hexapdf/content/graphic_object/test_solid_arc.rb +86 -0
- data/test/hexapdf/content/test_canvas.rb +1113 -0
- data/test/hexapdf/content/test_color_space.rb +97 -0
- data/test/hexapdf/content/test_graphics_state.rb +138 -0
- data/test/hexapdf/content/test_operator.rb +619 -0
- data/test/hexapdf/content/test_parser.rb +66 -0
- data/test/hexapdf/content/test_processor.rb +156 -0
- data/test/hexapdf/content/test_transformation_matrix.rb +64 -0
- data/test/hexapdf/encryption/common.rb +87 -0
- data/test/hexapdf/encryption/test_aes.rb +121 -0
- data/test/hexapdf/encryption/test_arc4.rb +39 -0
- data/test/hexapdf/encryption/test_fast_aes.rb +17 -0
- data/test/hexapdf/encryption/test_fast_arc4.rb +12 -0
- data/test/hexapdf/encryption/test_identity.rb +21 -0
- data/test/hexapdf/encryption/test_ruby_aes.rb +23 -0
- data/test/hexapdf/encryption/test_ruby_arc4.rb +20 -0
- data/test/hexapdf/encryption/test_security_handler.rb +356 -0
- data/test/hexapdf/encryption/test_standard_security_handler.rb +274 -0
- data/test/hexapdf/filter/common.rb +53 -0
- data/test/hexapdf/filter/test_ascii85_decode.rb +60 -0
- data/test/hexapdf/filter/test_ascii_hex_decode.rb +33 -0
- data/test/hexapdf/filter/test_encryption.rb +24 -0
- data/test/hexapdf/filter/test_flate_decode.rb +35 -0
- data/test/hexapdf/filter/test_lzw_decode.rb +52 -0
- data/test/hexapdf/filter/test_predictor.rb +183 -0
- data/test/hexapdf/filter/test_run_length_decode.rb +32 -0
- data/test/hexapdf/font/cmap/test_parser.rb +67 -0
- data/test/hexapdf/font/cmap/test_writer.rb +58 -0
- data/test/hexapdf/font/encoding/test_base.rb +35 -0
- data/test/hexapdf/font/encoding/test_difference_encoding.rb +21 -0
- data/test/hexapdf/font/encoding/test_glyph_list.rb +59 -0
- data/test/hexapdf/font/encoding/test_zapf_dingbats_encoding.rb +16 -0
- data/test/hexapdf/font/test_encoding.rb +27 -0
- data/test/hexapdf/font/test_true_type_wrapper.rb +110 -0
- data/test/hexapdf/font/test_type1_wrapper.rb +66 -0
- data/test/hexapdf/font/true_type/common.rb +19 -0
- data/test/hexapdf/font/true_type/table/test_cmap.rb +59 -0
- data/test/hexapdf/font/true_type/table/test_cmap_subtable.rb +133 -0
- data/test/hexapdf/font/true_type/table/test_directory.rb +35 -0
- data/test/hexapdf/font/true_type/table/test_glyf.rb +58 -0
- data/test/hexapdf/font/true_type/table/test_head.rb +76 -0
- data/test/hexapdf/font/true_type/table/test_hhea.rb +40 -0
- data/test/hexapdf/font/true_type/table/test_hmtx.rb +38 -0
- data/test/hexapdf/font/true_type/table/test_loca.rb +43 -0
- data/test/hexapdf/font/true_type/table/test_maxp.rb +62 -0
- data/test/hexapdf/font/true_type/table/test_name.rb +95 -0
- data/test/hexapdf/font/true_type/table/test_os2.rb +65 -0
- data/test/hexapdf/font/true_type/table/test_post.rb +89 -0
- data/test/hexapdf/font/true_type/test_font.rb +120 -0
- data/test/hexapdf/font/true_type/test_table.rb +41 -0
- data/test/hexapdf/font/type1/test_afm_parser.rb +51 -0
- data/test/hexapdf/font/type1/test_font.rb +68 -0
- data/test/hexapdf/font/type1/test_pfb_parser.rb +37 -0
- data/test/hexapdf/font_loader/test_from_configuration.rb +28 -0
- data/test/hexapdf/font_loader/test_standard14.rb +22 -0
- data/test/hexapdf/image_loader/test_jpeg.rb +83 -0
- data/test/hexapdf/image_loader/test_pdf.rb +47 -0
- data/test/hexapdf/image_loader/test_png.rb +258 -0
- data/test/hexapdf/task/test_dereference.rb +46 -0
- data/test/hexapdf/task/test_optimize.rb +137 -0
- data/test/hexapdf/test_configuration.rb +82 -0
- data/test/hexapdf/test_data_dir.rb +32 -0
- data/test/hexapdf/test_dictionary.rb +284 -0
- data/test/hexapdf/test_dictionary_fields.rb +185 -0
- data/test/hexapdf/test_document.rb +574 -0
- data/test/hexapdf/test_document_utils.rb +144 -0
- data/test/hexapdf/test_filter.rb +96 -0
- data/test/hexapdf/test_font_utils.rb +47 -0
- data/test/hexapdf/test_importer.rb +78 -0
- data/test/hexapdf/test_object.rb +177 -0
- data/test/hexapdf/test_parser.rb +394 -0
- data/test/hexapdf/test_rectangle.rb +36 -0
- data/test/hexapdf/test_reference.rb +41 -0
- data/test/hexapdf/test_revision.rb +139 -0
- data/test/hexapdf/test_revisions.rb +93 -0
- data/test/hexapdf/test_serializer.rb +169 -0
- data/test/hexapdf/test_stream.rb +262 -0
- data/test/hexapdf/test_tokenizer.rb +30 -0
- data/test/hexapdf/test_writer.rb +120 -0
- data/test/hexapdf/test_xref_section.rb +35 -0
- data/test/hexapdf/type/test_catalog.rb +30 -0
- data/test/hexapdf/type/test_embedded_file.rb +16 -0
- data/test/hexapdf/type/test_file_specification.rb +148 -0
- data/test/hexapdf/type/test_font.rb +35 -0
- data/test/hexapdf/type/test_font_descriptor.rb +51 -0
- data/test/hexapdf/type/test_font_simple.rb +190 -0
- data/test/hexapdf/type/test_font_type1.rb +128 -0
- data/test/hexapdf/type/test_form.rb +60 -0
- data/test/hexapdf/type/test_info.rb +14 -0
- data/test/hexapdf/type/test_names.rb +9 -0
- data/test/hexapdf/type/test_object_stream.rb +84 -0
- data/test/hexapdf/type/test_page.rb +260 -0
- data/test/hexapdf/type/test_page_tree_node.rb +255 -0
- data/test/hexapdf/type/test_resources.rb +167 -0
- data/test/hexapdf/type/test_trailer.rb +109 -0
- data/test/hexapdf/type/test_xref_stream.rb +131 -0
- data/test/hexapdf/utils/test_bit_field.rb +47 -0
- data/test/hexapdf/utils/test_lru_cache.rb +22 -0
- data/test/hexapdf/utils/test_object_hash.rb +115 -0
- data/test/hexapdf/utils/test_pdf_doc_encoding.rb +18 -0
- data/test/hexapdf/utils/test_sorted_tree_node.rb +232 -0
- data/test/test_helper.rb +56 -0
- metadata +427 -0
@@ -0,0 +1,209 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# This file is part of HexaPDF.
|
5
|
+
#
|
6
|
+
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
|
7
|
+
# Copyright (C) 2016 Thomas Leitner
|
8
|
+
#
|
9
|
+
# HexaPDF is free software: you can redistribute it and/or modify it
|
10
|
+
# under the terms of the GNU Affero General Public License version 3 as
|
11
|
+
# published by the Free Software Foundation with the addition of the
|
12
|
+
# following permission added to Section 15 as permitted in Section 7(a):
|
13
|
+
# FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
|
14
|
+
# THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
|
15
|
+
# INFRINGEMENT OF THIRD PARTY RIGHTS.
|
16
|
+
#
|
17
|
+
# HexaPDF is distributed in the hope that it will be useful, but WITHOUT
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
20
|
+
# License for more details.
|
21
|
+
#
|
22
|
+
# You should have received a copy of the GNU Affero General Public License
|
23
|
+
# along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
|
24
|
+
#
|
25
|
+
# The interactive user interfaces in modified source and object code
|
26
|
+
# versions of HexaPDF must display Appropriate Legal Notices, as required
|
27
|
+
# under Section 5 of the GNU Affero General Public License version 3.
|
28
|
+
#
|
29
|
+
# In accordance with Section 7(b) of the GNU Affero General Public
|
30
|
+
# License, a covered work must retain the producer line in every PDF that
|
31
|
+
# is created or manipulated using HexaPDF.
|
32
|
+
#++
|
33
|
+
|
34
|
+
require 'hexapdf/configuration'
|
35
|
+
|
36
|
+
module HexaPDF
|
37
|
+
|
38
|
+
# This class provides utility functions for PDF documents. It is available through the
|
39
|
+
# HexaPDF::Document#utils method.
|
40
|
+
#
|
41
|
+
# Some functions can't be attributed to a single "manager" object. For example, while embedding a
|
42
|
+
# file can be done within a HexaPDF::Type::Filespecification object, loading an image from a file
|
43
|
+
# as a PDF object doesn't have such a place. Such functions are available via this class.
|
44
|
+
class DocumentUtils
|
45
|
+
|
46
|
+
# This module provides methods for managing the images embedded in a PDF file; images
|
47
|
+
# themselves are represented by the HexaPDF::Type::Image class.
|
48
|
+
#
|
49
|
+
# Since an image can be used as a mask for another image, not all image objects found in a PDF
|
50
|
+
# are really used as images. Such cases are all handled by this class automatically.
|
51
|
+
module Images
|
52
|
+
|
53
|
+
# :call-seq:
|
54
|
+
# images.add_image(file) -> image
|
55
|
+
# images.add_image(io) -> image
|
56
|
+
#
|
57
|
+
# Adds the image from the given file or IO to the PDF and returns the image object.
|
58
|
+
#
|
59
|
+
# If the image has been added to the PDF before (i.e. if there is an image object with the
|
60
|
+
# same path name), the already existing image object is returned.
|
61
|
+
def add_image(file_or_io)
|
62
|
+
name = if file_or_io.kind_of?(String)
|
63
|
+
file_or_io
|
64
|
+
elsif file_or_io.respond_to?(:to_path)
|
65
|
+
file_or_io.to_path
|
66
|
+
end
|
67
|
+
if name
|
68
|
+
name = File.absolute_path(name)
|
69
|
+
image = each_image.find {|im| im.source_path == name}
|
70
|
+
end
|
71
|
+
unless image
|
72
|
+
image = image_loader_for(file_or_io).load(@document, file_or_io)
|
73
|
+
image.source_path = name
|
74
|
+
end
|
75
|
+
image
|
76
|
+
end
|
77
|
+
|
78
|
+
# :call-seq:
|
79
|
+
# images.each_image {|image| block } -> images
|
80
|
+
# images.each_image -> Enumerator
|
81
|
+
#
|
82
|
+
# Iterates over all images in the PDF.
|
83
|
+
#
|
84
|
+
# Note that only real images are yielded which means, for example, that images used as soft
|
85
|
+
# mask are not.
|
86
|
+
def each_image(&block)
|
87
|
+
images = @document.each(current: false).select do |obj|
|
88
|
+
obj[:Subtype] == :Image && !obj[:ImageMask]
|
89
|
+
end
|
90
|
+
masks = images.each_with_object([]) do |image, temp|
|
91
|
+
temp << image[:Mask] if image[:Mask].kind_of?(Stream)
|
92
|
+
temp << image[:SMask] if image[:SMask].kind_of?(Stream)
|
93
|
+
end
|
94
|
+
(images - masks).each(&block)
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
# Returns the image loader (see ImageLoader) for the given file or IO stream or raises an
|
100
|
+
# error if no suitable image loader is found.
|
101
|
+
def image_loader_for(file_or_io)
|
102
|
+
GlobalConfiguration['image_loader'].each_index do |index|
|
103
|
+
loader = GlobalConfiguration.constantize('image_loader', index) do
|
104
|
+
raise HexaPDF::Error, "Couldn't retrieve image loader from configuration"
|
105
|
+
end
|
106
|
+
return loader if loader.handles?(file_or_io)
|
107
|
+
end
|
108
|
+
|
109
|
+
raise HexaPDF::Error, "Couldn't find suitable image loader"
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
# This module provides methods for managing file specification of a PDF file.
|
116
|
+
#
|
117
|
+
# Note that for a given PDF file not all file specifications may be found, e.g. when a file
|
118
|
+
# specification is only a string. Therefore this module can only handle those file
|
119
|
+
# specifications that are indirect file specification dictionaries with the /Type key set.
|
120
|
+
module Files
|
121
|
+
|
122
|
+
# :call-seq:
|
123
|
+
# files.add_file(filename, name: File.basename(filename), description: nil, embed: true) -> file_spec
|
124
|
+
# files.add_file(io, name:, description: nil) -> file_spec
|
125
|
+
#
|
126
|
+
# Adds the file or IO to the PDF and returns the corresponding file specification object.
|
127
|
+
#
|
128
|
+
# Options:
|
129
|
+
#
|
130
|
+
# name::
|
131
|
+
# The name that should be used for the file path. This name is also for registering the
|
132
|
+
# file in the EmbeddedFiles name tree.
|
133
|
+
#
|
134
|
+
# description::
|
135
|
+
# A description of the file.
|
136
|
+
#
|
137
|
+
# embed::
|
138
|
+
# When an IO object is given, it is always embedded and this option is ignored.
|
139
|
+
#
|
140
|
+
# When a filename is given and this option is +true+, then the file is embedded. Otherwise
|
141
|
+
# only a reference to it is stored.
|
142
|
+
#
|
143
|
+
# See: HexaPDF::Type::FileSpecification
|
144
|
+
def add_file(file_or_io, name: nil, description: nil, embed: true)
|
145
|
+
name ||= File.basename(file_or_io) if file_or_io.kind_of?(String)
|
146
|
+
if name.nil?
|
147
|
+
raise ArgumentError, "The name argument is mandatory when given an IO object"
|
148
|
+
end
|
149
|
+
|
150
|
+
spec = @document.add(Type: :Filespec)
|
151
|
+
spec.path = name
|
152
|
+
spec[:Desc] = description if description
|
153
|
+
spec.embed(file_or_io, name: name, register: true) if embed || !file_or_io.kind_of?(String)
|
154
|
+
spec
|
155
|
+
end
|
156
|
+
|
157
|
+
# :call-seq:
|
158
|
+
# files.each_file(search: false) {|file_spec| block } -> files
|
159
|
+
# files.each_file(search: false) -> Enumerator
|
160
|
+
#
|
161
|
+
# Iterates over indirect file specification dictionaries of the PDF.
|
162
|
+
#
|
163
|
+
# By default, only the file specifications in their standard locations, namely in the
|
164
|
+
# EmbeddedFiles name tree and in the page annotations, are returned. If the +search+ option is
|
165
|
+
# +true+, then all indirect objects are searched for file specification dictionaries which is
|
166
|
+
# much slower.
|
167
|
+
def each_file(search: false)
|
168
|
+
return to_enum(__method__, search: search) unless block_given?
|
169
|
+
|
170
|
+
if search
|
171
|
+
@document.each(current: false) do |obj|
|
172
|
+
yield(obj) if obj.type == :Filespec
|
173
|
+
end
|
174
|
+
else
|
175
|
+
seen = {}
|
176
|
+
tree = @document.catalog[:Names] && @document.catalog[:Names][:EmbeddedFiles]
|
177
|
+
tree.each_entry do |_, spec|
|
178
|
+
seen[spec] = true
|
179
|
+
yield(spec)
|
180
|
+
end if tree
|
181
|
+
|
182
|
+
@document.pages.each_page do |page|
|
183
|
+
next unless page[:Annots]
|
184
|
+
page[:Annots].each do |annot|
|
185
|
+
annot = @document.deref(annot)
|
186
|
+
next unless annot[:Subtype] == :FileAttachment
|
187
|
+
spec = @document.deref(annot[:FS])
|
188
|
+
yield(spec) unless seen.key?(spec)
|
189
|
+
seen[spec] = true
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
self
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
include Images
|
200
|
+
include Files
|
201
|
+
|
202
|
+
# Creates a new DocumentUtils object for the given PDF document.
|
203
|
+
def initialize(document)
|
204
|
+
@document = document
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# This file is part of HexaPDF.
|
5
|
+
#
|
6
|
+
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
|
7
|
+
# Copyright (C) 2016 Thomas Leitner
|
8
|
+
#
|
9
|
+
# HexaPDF is free software: you can redistribute it and/or modify it
|
10
|
+
# under the terms of the GNU Affero General Public License version 3 as
|
11
|
+
# published by the Free Software Foundation with the addition of the
|
12
|
+
# following permission added to Section 15 as permitted in Section 7(a):
|
13
|
+
# FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
|
14
|
+
# THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
|
15
|
+
# INFRINGEMENT OF THIRD PARTY RIGHTS.
|
16
|
+
#
|
17
|
+
# HexaPDF is distributed in the hope that it will be useful, but WITHOUT
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
20
|
+
# License for more details.
|
21
|
+
#
|
22
|
+
# You should have received a copy of the GNU Affero General Public License
|
23
|
+
# along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
|
24
|
+
#
|
25
|
+
# The interactive user interfaces in modified source and object code
|
26
|
+
# versions of HexaPDF must display Appropriate Legal Notices, as required
|
27
|
+
# under Section 5 of the GNU Affero General Public License version 3.
|
28
|
+
#
|
29
|
+
# In accordance with Section 7(b) of the GNU Affero General Public
|
30
|
+
# License, a covered work must retain the producer line in every PDF that
|
31
|
+
# is created or manipulated using HexaPDF.
|
32
|
+
#++
|
33
|
+
|
34
|
+
require 'securerandom'
|
35
|
+
require 'hexapdf/error'
|
36
|
+
|
37
|
+
module HexaPDF
|
38
|
+
module Encryption
|
39
|
+
|
40
|
+
# Common interface for AES algorithms
|
41
|
+
#
|
42
|
+
# This module defines the common interface that is used by the security handlers to encrypt or
|
43
|
+
# decrypt data with AES. It has to be *prepended* by any AES algorithm class.
|
44
|
+
#
|
45
|
+
# See the ClassMethods module for available class level methods of AES algorithms.
|
46
|
+
#
|
47
|
+
# == Implementing an AES Class
|
48
|
+
#
|
49
|
+
# An AES class needs to define at least the following methods:
|
50
|
+
#
|
51
|
+
# initialize(key, iv, mode)::
|
52
|
+
# Initializes the AES algorithm with the given key and initialization vector. The mode
|
53
|
+
# determines how the AES algorithm object works: If the mode is :encrypt, the object
|
54
|
+
# encrypts the data, if the mode is :decrypt, the object decrypts the data.
|
55
|
+
#
|
56
|
+
# process(data)::
|
57
|
+
# Processes the data and returns the encrypted/decrypted data. The method can assume that
|
58
|
+
# the passed in data always has a length that is a multiple of BLOCK_SIZE.
|
59
|
+
module AES
|
60
|
+
|
61
|
+
# Valid AES key lengths
|
62
|
+
VALID_KEY_LENGTH = [16, 24, 32]
|
63
|
+
|
64
|
+
# The AES block size
|
65
|
+
BLOCK_SIZE = 16
|
66
|
+
|
67
|
+
# Convenience methods for decryption and encryption that operate according to the PDF
|
68
|
+
# specification.
|
69
|
+
#
|
70
|
+
# These methods will be available on the class object that prepends the AES module.
|
71
|
+
module ClassMethods
|
72
|
+
|
73
|
+
# Encrypts the given +data+ using the +key+ and a randomly generated initialization
|
74
|
+
# vector.
|
75
|
+
#
|
76
|
+
# The data is padded using the PKCS#5 padding scheme and the initialization vector is
|
77
|
+
# prepended to the encrypted data,
|
78
|
+
#
|
79
|
+
# See: PDF1.7 s7.6.2.
|
80
|
+
def encrypt(key, data)
|
81
|
+
iv = random_bytes(BLOCK_SIZE)
|
82
|
+
iv << new(key, iv, :encrypt).process(pad(data))
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns a Fiber object that encrypts the data from the given source fiber with the
|
86
|
+
# +key+.
|
87
|
+
#
|
88
|
+
# Padding and the initialization vector are handled like in #encrypt.
|
89
|
+
def encryption_fiber(key, source)
|
90
|
+
Fiber.new do
|
91
|
+
data = random_bytes(BLOCK_SIZE)
|
92
|
+
algorithm = new(key, data, :encrypt)
|
93
|
+
Fiber.yield(data)
|
94
|
+
|
95
|
+
data = ''.b
|
96
|
+
while source.alive? && (new_data = source.resume)
|
97
|
+
data << new_data
|
98
|
+
next if data.length < BLOCK_SIZE
|
99
|
+
new_data = data.slice!(0, data.length - data.length % BLOCK_SIZE)
|
100
|
+
Fiber.yield(algorithm.process(new_data))
|
101
|
+
end
|
102
|
+
|
103
|
+
algorithm.process(pad(data))
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Decrypts the given +data+ using the +key+.
|
108
|
+
#
|
109
|
+
# It is assumed that the initialization vector is included in the first BLOCK_SIZE bytes
|
110
|
+
# of the data. After the decryption the PKCS#5 padding is removed.
|
111
|
+
#
|
112
|
+
# See: PDF1.7 s7.6.2.
|
113
|
+
def decrypt(key, data)
|
114
|
+
if data.length % BLOCK_SIZE != 0 || data.length < 2 * BLOCK_SIZE
|
115
|
+
raise HexaPDF::EncryptionError, "Invalid data for decryption, need 32 + 16*n bytes"
|
116
|
+
end
|
117
|
+
unpad(new(key, data.slice!(0, BLOCK_SIZE), :decrypt).process(data))
|
118
|
+
end
|
119
|
+
|
120
|
+
# Returns a Fiber object that decrypts the data from the given source fiber with the
|
121
|
+
# +key+.
|
122
|
+
#
|
123
|
+
# Padding and the initialization vector are handled like in #decrypt.
|
124
|
+
def decryption_fiber(key, source)
|
125
|
+
Fiber.new do
|
126
|
+
data = ''.b
|
127
|
+
while data.length < BLOCK_SIZE && source.alive? && (new_data = source.resume)
|
128
|
+
data << new_data
|
129
|
+
end
|
130
|
+
|
131
|
+
algorithm = new(key, data.slice!(0, BLOCK_SIZE), :decrypt)
|
132
|
+
|
133
|
+
while source.alive? && (new_data = source.resume)
|
134
|
+
data << new_data
|
135
|
+
next if data.length < 2 * BLOCK_SIZE
|
136
|
+
new_data = data.slice!(0, data.length - BLOCK_SIZE - data.length % BLOCK_SIZE)
|
137
|
+
Fiber.yield(algorithm.process(new_data))
|
138
|
+
end
|
139
|
+
|
140
|
+
if data.length < BLOCK_SIZE || data.length % BLOCK_SIZE != 0
|
141
|
+
raise HexaPDF::EncryptionError, "Invalid data for decryption, need 32 + 16*n bytes"
|
142
|
+
end
|
143
|
+
|
144
|
+
unpad(algorithm.process(data))
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Returns a string of n random bytes.
|
149
|
+
#
|
150
|
+
# The specific AES algorithm class can override this class method to provide another
|
151
|
+
# method for generating random bytes.
|
152
|
+
def random_bytes(n)
|
153
|
+
SecureRandom.random_bytes(n)
|
154
|
+
end
|
155
|
+
|
156
|
+
private
|
157
|
+
|
158
|
+
# Pads the data to a muliple of BLOCK_SIZE using the PKCS#5 padding scheme and returns the
|
159
|
+
# result.
|
160
|
+
#
|
161
|
+
# See: PDF1.7 s7.6.2
|
162
|
+
def pad(data)
|
163
|
+
padding_length = BLOCK_SIZE - data.size % BLOCK_SIZE
|
164
|
+
data + padding_length.chr * padding_length
|
165
|
+
end
|
166
|
+
|
167
|
+
# Removes the padding from the data according to the PKCS#5 padding scheme and returns the
|
168
|
+
# result.
|
169
|
+
#
|
170
|
+
# See: PDF1.7 s7.6.2
|
171
|
+
def unpad(data)
|
172
|
+
padding_length = data.getbyte(-1)
|
173
|
+
if padding_length > BLOCK_SIZE || padding_length == 0
|
174
|
+
raise HexaPDF::EncryptionError, "Invalid AES padding length #{padding_length}"
|
175
|
+
end
|
176
|
+
data[0...-padding_length]
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
# Automatically extends the klass with the necessary class level methods.
|
182
|
+
def self.prepended(klass) # :nodoc:
|
183
|
+
klass.extend(ClassMethods)
|
184
|
+
end
|
185
|
+
|
186
|
+
# Creates a new AES object using the given encryption key and initialization vector.
|
187
|
+
#
|
188
|
+
# The mode must either be :encrypt or :decrypt.
|
189
|
+
#
|
190
|
+
# Classes prepending this module have to have their own initialization method as this method
|
191
|
+
# just performs basic checks.
|
192
|
+
def initialize(key, iv, mode)
|
193
|
+
unless VALID_KEY_LENGTH.include?(key.length)
|
194
|
+
raise HexaPDF::EncryptionError, "AES key length must be 128, 192 or 256 bit"
|
195
|
+
end
|
196
|
+
unless iv.length == BLOCK_SIZE
|
197
|
+
raise HexaPDF::EncryptionError, "AES initialization vector length must be 128 bit"
|
198
|
+
end
|
199
|
+
mode = mode.intern
|
200
|
+
super
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
end
|
206
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# This file is part of HexaPDF.
|
5
|
+
#
|
6
|
+
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
|
7
|
+
# Copyright (C) 2016 Thomas Leitner
|
8
|
+
#
|
9
|
+
# HexaPDF is free software: you can redistribute it and/or modify it
|
10
|
+
# under the terms of the GNU Affero General Public License version 3 as
|
11
|
+
# published by the Free Software Foundation with the addition of the
|
12
|
+
# following permission added to Section 15 as permitted in Section 7(a):
|
13
|
+
# FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
|
14
|
+
# THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
|
15
|
+
# INFRINGEMENT OF THIRD PARTY RIGHTS.
|
16
|
+
#
|
17
|
+
# HexaPDF is distributed in the hope that it will be useful, but WITHOUT
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
20
|
+
# License for more details.
|
21
|
+
#
|
22
|
+
# You should have received a copy of the GNU Affero General Public License
|
23
|
+
# along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
|
24
|
+
#
|
25
|
+
# The interactive user interfaces in modified source and object code
|
26
|
+
# versions of HexaPDF must display Appropriate Legal Notices, as required
|
27
|
+
# under Section 5 of the GNU Affero General Public License version 3.
|
28
|
+
#
|
29
|
+
# In accordance with Section 7(b) of the GNU Affero General Public
|
30
|
+
# License, a covered work must retain the producer line in every PDF that
|
31
|
+
# is created or manipulated using HexaPDF.
|
32
|
+
#++
|
33
|
+
|
34
|
+
module HexaPDF
|
35
|
+
module Encryption
|
36
|
+
|
37
|
+
# Common interface for ARC4 algorithms
|
38
|
+
#
|
39
|
+
# This module defines the common interface that is used by the security handlers to encrypt or
|
40
|
+
# decrypt data with ARC4. It has to be *prepended* by any ARC4 algorithm class.
|
41
|
+
#
|
42
|
+
# See the ClassMethods module for available class level methods of ARC4 algorithms.
|
43
|
+
#
|
44
|
+
# == Implementing an ARC4 Class
|
45
|
+
#
|
46
|
+
# An ARC4 class needs to define at least the following methods:
|
47
|
+
#
|
48
|
+
# initialize(key)::
|
49
|
+
# Initializes the ARC4 algorithm with the given key.
|
50
|
+
#
|
51
|
+
# process(data)::
|
52
|
+
# Processes the data and returns the encrypted/decrypted data. Since the ARC4 algorithm is
|
53
|
+
# symmetric in regards to its inner workings, the same method can be used for encryption and
|
54
|
+
# decryption.
|
55
|
+
module ARC4
|
56
|
+
|
57
|
+
# Convenience methods for decryption and encryption that operate according to the PDF
|
58
|
+
# specification.
|
59
|
+
#
|
60
|
+
# These methods will be available on the class object that prepends the ARC4 module.
|
61
|
+
module ClassMethods
|
62
|
+
|
63
|
+
# Encrypts the given +data+ with the +key+.
|
64
|
+
#
|
65
|
+
# See: PDF1.7 s7.6.2.
|
66
|
+
def encrypt(key, data)
|
67
|
+
new(key).process(data)
|
68
|
+
end
|
69
|
+
alias :decrypt :encrypt
|
70
|
+
|
71
|
+
# Returns a Fiber object that encrypts the data from the given source fiber with the
|
72
|
+
# +key+.
|
73
|
+
def encryption_fiber(key, source)
|
74
|
+
Fiber.new do
|
75
|
+
algorithm = new(key)
|
76
|
+
while source.alive? && (data = source.resume)
|
77
|
+
Fiber.yield(algorithm.process(data)) unless data.empty?
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
alias :decryption_fiber :encryption_fiber
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
# Automatically extends the klass with the necessary class level methods.
|
86
|
+
def self.prepended(klass) # :nodoc:
|
87
|
+
klass.extend(ClassMethods)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# This file is part of HexaPDF.
|
5
|
+
#
|
6
|
+
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
|
7
|
+
# Copyright (C) 2016 Thomas Leitner
|
8
|
+
#
|
9
|
+
# HexaPDF is free software: you can redistribute it and/or modify it
|
10
|
+
# under the terms of the GNU Affero General Public License version 3 as
|
11
|
+
# published by the Free Software Foundation with the addition of the
|
12
|
+
# following permission added to Section 15 as permitted in Section 7(a):
|
13
|
+
# FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
|
14
|
+
# THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
|
15
|
+
# INFRINGEMENT OF THIRD PARTY RIGHTS.
|
16
|
+
#
|
17
|
+
# HexaPDF is distributed in the hope that it will be useful, but WITHOUT
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
20
|
+
# License for more details.
|
21
|
+
#
|
22
|
+
# You should have received a copy of the GNU Affero General Public License
|
23
|
+
# along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
|
24
|
+
#
|
25
|
+
# The interactive user interfaces in modified source and object code
|
26
|
+
# versions of HexaPDF must display Appropriate Legal Notices, as required
|
27
|
+
# under Section 5 of the GNU Affero General Public License version 3.
|
28
|
+
#
|
29
|
+
# In accordance with Section 7(b) of the GNU Affero General Public
|
30
|
+
# License, a covered work must retain the producer line in every PDF that
|
31
|
+
# is created or manipulated using HexaPDF.
|
32
|
+
#++
|
33
|
+
|
34
|
+
require 'openssl'
|
35
|
+
require 'hexapdf/error'
|
36
|
+
require 'hexapdf/encryption/aes'
|
37
|
+
|
38
|
+
module HexaPDF
|
39
|
+
module Encryption
|
40
|
+
|
41
|
+
# Implementation of the general encryption algorithm AES using OpenSSL as backend.
|
42
|
+
#
|
43
|
+
# Since OpenSSL is a native Ruby extension (that comes bundled with Ruby) it is much faster
|
44
|
+
# than the pure Ruby version and it can use the AES-NI instruction set on CPUs when available.
|
45
|
+
#
|
46
|
+
# This implementation is using AES in Cipher Block Chaining (CBC) mode.
|
47
|
+
#
|
48
|
+
# See: PDF1.7 s7.6.2
|
49
|
+
class FastAES
|
50
|
+
|
51
|
+
prepend AES
|
52
|
+
|
53
|
+
# Uses OpenSSL to generate the requested random bytes.
|
54
|
+
#
|
55
|
+
# See AES::ClassMethods#random_bytes for more information.
|
56
|
+
def self.random_bytes(n)
|
57
|
+
OpenSSL::Random.random_bytes(n)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Creates a new FastAES object using the given encryption key and initialization vector.
|
61
|
+
#
|
62
|
+
# The mode must either be :encrypt or :decrypt.
|
63
|
+
def initialize(key, iv, mode)
|
64
|
+
@cipher = OpenSSL::Cipher.new("AES-#{key.length << 3}-CBC")
|
65
|
+
@cipher.send(mode)
|
66
|
+
@cipher.key = key
|
67
|
+
@cipher.iv = iv
|
68
|
+
@cipher.padding = 0
|
69
|
+
end
|
70
|
+
|
71
|
+
# Encrypts or decrypts the given data whose length must be a multiple of 16.
|
72
|
+
def process(data)
|
73
|
+
@cipher.update(data)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# This file is part of HexaPDF.
|
5
|
+
#
|
6
|
+
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
|
7
|
+
# Copyright (C) 2016 Thomas Leitner
|
8
|
+
#
|
9
|
+
# HexaPDF is free software: you can redistribute it and/or modify it
|
10
|
+
# under the terms of the GNU Affero General Public License version 3 as
|
11
|
+
# published by the Free Software Foundation with the addition of the
|
12
|
+
# following permission added to Section 15 as permitted in Section 7(a):
|
13
|
+
# FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
|
14
|
+
# THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
|
15
|
+
# INFRINGEMENT OF THIRD PARTY RIGHTS.
|
16
|
+
#
|
17
|
+
# HexaPDF is distributed in the hope that it will be useful, but WITHOUT
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
20
|
+
# License for more details.
|
21
|
+
#
|
22
|
+
# You should have received a copy of the GNU Affero General Public License
|
23
|
+
# along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
|
24
|
+
#
|
25
|
+
# The interactive user interfaces in modified source and object code
|
26
|
+
# versions of HexaPDF must display Appropriate Legal Notices, as required
|
27
|
+
# under Section 5 of the GNU Affero General Public License version 3.
|
28
|
+
#
|
29
|
+
# In accordance with Section 7(b) of the GNU Affero General Public
|
30
|
+
# License, a covered work must retain the producer line in every PDF that
|
31
|
+
# is created or manipulated using HexaPDF.
|
32
|
+
#++
|
33
|
+
|
34
|
+
require 'openssl'
|
35
|
+
require 'hexapdf/encryption/arc4'
|
36
|
+
|
37
|
+
module HexaPDF
|
38
|
+
module Encryption
|
39
|
+
|
40
|
+
# Implementation of the general encryption algorithm ARC4 using OpenSSL as backend.
|
41
|
+
#
|
42
|
+
# See: PDF1.7 s7.6.2
|
43
|
+
class FastARC4
|
44
|
+
|
45
|
+
prepend ARC4
|
46
|
+
|
47
|
+
# Creates a new FastARC4 object using the given encryption key.
|
48
|
+
def initialize(key)
|
49
|
+
@cipher = OpenSSL::Cipher::RC4.new
|
50
|
+
@cipher.key_len = key.length
|
51
|
+
@cipher.key = key
|
52
|
+
end
|
53
|
+
|
54
|
+
# Processes the given data.
|
55
|
+
#
|
56
|
+
# Since this is a symmetric algorithm, the same method can be used for encryption and
|
57
|
+
# decryption.
|
58
|
+
def process(data)
|
59
|
+
@cipher.update(data)
|
60
|
+
end
|
61
|
+
alias :decrypt :process
|
62
|
+
alias :encrypt :process
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|