ob64 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ob64
4
+ module UU
5
+ # Writes the {UU.encode} of the bytes written to it to an io.
6
+ class Writer < Ob64::Writer
7
+ # @param io [IO] - responds to +write+; must not keep the String it is
8
+ # given, as it is reused
9
+ def initialize(io)
10
+ super(io, 45)
11
+ end
12
+
13
+ private
14
+
15
+ def encode(dst, src, off, len) = __uuencode_into(dst, src, off, len)
16
+ end
17
+
18
+ # Reads the {UU.decode} of the text in an io. Like +decode+, never raises
19
+ # for malformed input.
20
+ class Reader < Ob64::Reader
21
+ private
22
+
23
+ # Decodes whole lines. Like +decode+, the output is capped at 3/4 of the
24
+ # text's length, and a cap reached may change how later lines parse: a
25
+ # piece reaching the cap before eof waits for more text, its bytes
26
+ # removed. Malformed lines that keep reaching it are decoded again on
27
+ # each pull.
28
+ def decode(text, eof, dst)
29
+ cut = eof ? text.bytesize : (text.rindex("\n") || -1) + 1
30
+ size = dst.bytesize
31
+ cap = pulled * 3 / 4 - decoded
32
+ stopped = __uudecode_capped_into(dst, text, cut, cap)
33
+ if dst.bytesize - size == cap && !eof
34
+ dst.slice!(size..)
35
+ return
36
+ end
37
+
38
+ stop if stopped
39
+ __consume(text, cut)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "reader"
4
+ require_relative "writer"
5
+ require_relative "streams/rfc4648"
6
+ require_relative "streams/rfc2045"
7
+ require_relative "streams/uu"
data/lib/ob64/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ob64
4
- VERSION = "0.7.0"
4
+ VERSION = "0.9.0"
5
5
  end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ob64
4
+ # Writes the encoded version of the bytes written to it to an io, like an IO:
5
+ # {#write}, {#<<}, {#flush}, {#close}. {#finish} writes the end of the
6
+ # encoding.
7
+ #
8
+ # The io is given the same String on every write, refilled each time: it must
9
+ # not keep it.
10
+ #
11
+ # Subclasses pass +group+, the bytes that encode on their own (3, or a line's
12
+ # worth), and define +encode(dst, src, off, len)+, which appends the encoding
13
+ # of +src[off, len]+, whole groups or the tail at {#finish}, to +dst+.
14
+ class Writer
15
+ include Ext
16
+
17
+ # @param io [IO] - responds to +write+; must not keep the String it is
18
+ # given, as it is reused
19
+ # @param group [Integer] - bytes that encode on their own
20
+ def initialize(io, group)
21
+ @io = io
22
+ @group = group
23
+ # The bytes taken short of a group
24
+ @bin = "".b
25
+ # The encoding written to the io, refilled on every write
26
+ @out = String.new(encoding: Encoding::US_ASCII)
27
+ @finished = false
28
+ end
29
+
30
+ # Encodes +strings+ and writes the encoding of their whole groups to the io.
31
+ # The rest waits for more bytes or {#finish}.
32
+ #
33
+ # @param strings [Array<String>]
34
+ # @return [Integer] bytes taken: the sum of the bytesizes of +strings+ (converted
35
+ # with +to_s+)
36
+ # @raise [IOError] after {#finish} or {#close}
37
+ def write(*strings)
38
+ check_open
39
+ strings.map! { |string| String(string) }
40
+ strings.sum { |string| take(string) }
41
+ end
42
+
43
+ # Like {#write}.
44
+ #
45
+ # @param string [String]
46
+ # @return [Writer] self
47
+ # @raise [IOError] after {#finish} or {#close}
48
+ def <<(string)
49
+ check_open
50
+ take(String(string))
51
+ self
52
+ end
53
+
54
+ # Flushes the io, if it responds to +flush+. Bytes short of a group still
55
+ # wait.
56
+ #
57
+ # @return [Writer] self
58
+ # @raise [IOError] after {#finish} or {#close}
59
+ def flush
60
+ check_open
61
+ @io.flush if @io.respond_to?(:flush)
62
+ self
63
+ end
64
+
65
+ # Writes the end of the encoding (the last bytes, padding, the last line) to
66
+ # the io and keeps it open.
67
+ #
68
+ # @return [IO] the io
69
+ # @raise [IOError] after {#finish} or {#close}
70
+ def finish
71
+ check_open
72
+ encode(@out, @bin, 0, @bin.bytesize) unless @bin.empty?
73
+ write_out
74
+ @finished = true
75
+ @io
76
+ end
77
+
78
+ # Like {#finish} unless already finished, then closes the io. Closing again
79
+ # does nothing.
80
+ #
81
+ # @return [nil]
82
+ def close
83
+ finish unless @finished
84
+ @io.close
85
+ nil
86
+ end
87
+
88
+ private
89
+
90
+ def check_open
91
+ raise IOError, "closed stream" if @finished
92
+ end
93
+
94
+ # Encodes the whole groups of @bin and +string+, straight from +string+,
95
+ # and writes them; keeps the rest in @bin. Returns the bytes taken.
96
+ def take(string)
97
+ size = string.bytesize
98
+ off = 0
99
+ unless @bin.empty?
100
+ off = @group - @bin.bytesize
101
+ off = size if size < off
102
+ __append_into(@bin, string, 0, off)
103
+ if @bin.bytesize == @group
104
+ encode(@out, @bin, 0, @group)
105
+ __consume(@bin, @group)
106
+ end
107
+ end
108
+ whole = (size - off) / @group * @group
109
+ encode(@out, string, off, whole) if whole > 0
110
+ __append_into(@bin, string, off + whole, size - off - whole)
111
+ write_out
112
+ size
113
+ end
114
+
115
+ # Writes @out and empties it, keeping its buffer. If the io raises, @out
116
+ # is written with the next write.
117
+ def write_out
118
+ return if @out.empty?
119
+
120
+ @io.write(@out)
121
+ __consume(@out, @out.bytesize)
122
+ end
123
+ end
124
+ end
data/lib/ob64.rb CHANGED
@@ -242,3 +242,5 @@ module Ob64
242
242
  end
243
243
  end
244
244
  end
245
+
246
+ require_relative "ob64/streams"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ob64
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Fernandes
@@ -19,6 +19,7 @@ extensions:
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - ".github/dependabot.yml"
22
+ - ".github/scripts/install-sde"
22
23
  - ".github/workflows/main.yml"
23
24
  - ".gitignore"
24
25
  - ".rspec"
@@ -51,7 +52,13 @@ files:
51
52
  - ext/ob64/pack_uuencode.h
52
53
  - lib/ob64.rb
53
54
  - lib/ob64/core_ext.rb
55
+ - lib/ob64/reader.rb
56
+ - lib/ob64/streams.rb
57
+ - lib/ob64/streams/rfc2045.rb
58
+ - lib/ob64/streams/rfc4648.rb
59
+ - lib/ob64/streams/uu.rb
54
60
  - lib/ob64/version.rb
61
+ - lib/ob64/writer.rb
55
62
  - ob64.gemspec
56
63
  homepage: https://github.com/jcmfernandes/ob64
57
64
  licenses:
@@ -59,8 +66,8 @@ licenses:
59
66
  metadata:
60
67
  homepage_uri: https://github.com/jcmfernandes/ob64
61
68
  changelog_uri: https://github.com/jcmfernandes/ob64/blob/master/CHANGELOG.md
62
- documentation_uri: https://www.rubydoc.info/gems/ob64/0.7.0
63
- source_code_uri: https://github.com/jcmfernandes/ob64/tree/v0.7.0
69
+ documentation_uri: https://www.rubydoc.info/gems/ob64/0.9.0
70
+ source_code_uri: https://github.com/jcmfernandes/ob64/tree/v0.9.0
64
71
  rdoc_options: []
65
72
  require_paths:
66
73
  - lib