Module: Ascii85Native
- Extended by:
- FFI::Library
- Defined in:
- lib/ascii85_native.rb
Class Method Summary collapse
- .decode(input, force_delimiter = true) ⇒ Object
- .encode(input, include_delimiter = false) ⇒ Object
- .find_end_slice(input) ⇒ Object
- .find_start_slice(input) ⇒ Object
Class Method Details
.decode(input, force_delimiter = true) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ascii85_native.rb', line 57 def self.decode(input, force_delimiter=true) return "" if input.nil? || input.size == 0 if force_delimiter input = input[2..-3] else start_slice = find_start_slice(input) end_slice = find_end_slice(input) input = input[start_slice..end_slice] if start_slice != 0 || end_slice != -1 end FFI::MemoryPointer.new(:char, input.size) do |in_char| FFI::MemoryPointer.new(:char, input.size) do |filtered_char| in_char.write_string(input) self.a85_filter_before_decode(in_char, input.size, filtered_char) filtered_length = self.strlen(filtered_char.read_string()) puts "filtered_length: #{filtered_length}" out_size = self.a85_decoded_size(filtered_length) FFI::MemoryPointer.new(:uint8, out_size) do |output| self.a85_decode(filtered_char, filtered_length, output) return output.read_string() end end end end |
.encode(input, include_delimiter = false) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ascii85_native.rb', line 28 def self.encode(input, include_delimiter=false) if input.nil? || input.size == 0 return '<~~>' if include_delimiter return '' end if input.class == String input_data = [] input.each_byte { |byte| input_data << byte } else input_data = input end FFI::MemoryPointer.new(:uint8, input_data.size) do |in_uint8| in_uint8.write_array_of_type(FFI::TYPE_UINT8, :put_uint8, input_data) out_size = self.a85_encoded_size(input_data.size, true) FFI::MemoryPointer.new(:uint8, out_size) do |output| self.a85_encode(in_uint8, input_data.size, output, true) if include_delimiter return '<~' + (output.read_string() || '') + '~>' else return output.read_string() end end end end |
.find_end_slice(input) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ascii85_native.rb', line 105 def self.find_end_slice(input) end_slice = -1 cursor = -1 input.size.times do |i| if ['\n', '\r', ' '].include?(input[cursor]) cursor -= 1 next elsif input[cursor] == '>' && input[cursor-1] == '~' end_slice = cursor - 2 break else break # input is not delimited end end return end_slice end |
.find_start_slice(input) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ascii85_native.rb', line 86 def self.find_start_slice(input) start_slice = 0 cursor = 0 input.size.times do |i| if ['\n', '\r', ' '].include?(input[cursor]) cursor += 1 next elsif input[cursor] == '<' && input[cursor+1] == '~' start_slice = cursor + 2 break else break # input is not delimited end end return start_slice end |