ob64 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93d2eaeaa970d18c2ab92ff9f5bb4621c22135c16faa0f1ef09ac71babe94b95
4
- data.tar.gz: 7bad60e17ca1b3189634cb2043d58bf1ffa85c701deff0b99886dde4a34082e1
3
+ metadata.gz: a9b61f1de15353d43ea9786125d9d960adbf5f37aeb5bf4a46d55b2a85eaa82e
4
+ data.tar.gz: '08237edfc6bf9a5d977941d73ce9dc535579a6ba23cdb58d8eff14acbf69c03d'
5
5
  SHA512:
6
- metadata.gz: fc8c44a79e33c420a13c1d4460a2773ed6d60ff0e4eab47ea5ad0a1db4365d85c4e642e442dfa4a2753ef0153f8bde876dca4867005f28c124f4b7251b06e55d
7
- data.tar.gz: 61b2550bc2035667a86d6bfd87149fcba0bd3faa33d9e29c6f42f89cef8be2b5e5df6a36a35682e2cb9d193ea1252ee7d37bcb1d787d0c62cfa78d1618218144
6
+ metadata.gz: f20d33b04fc3ea300516c556239d6e164957e66984686736415c5e904d1058df2a5b8a015d3d8d803e17e73a03225ec48afecac8e2a889e9c8913380f54706b5
7
+ data.tar.gz: 9b6b010c3dda40535733cb4f922befb61facad728c18a53846fb6b3b796a213d06de77a8b02cccbb4262a516c245116482bf8958d49408db8d05087aa5bf93c6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.9.0] - 2026-10-30
2
+
3
+ - Add streams: `Writer` and `Reader` in `Ob64::Base64::RFC4648`, `Ob64::Base64::RFC2045` and `Ob64::UU` wrap an IO, encoding on write and decoding on read, with the same output as `encode` and `decode`.
4
+
1
5
  ## [0.8.0] - 2026-10-27
2
6
 
3
7
  - Fix undefined behavior in AVX512 detection.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/ob64.svg)](http://rubygems.org/gems/ob64)
4
4
  [![CI](https://github.com/jcmfernandes/ob64/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/jcmfernandes/ob64/actions/workflows/main.yml)
5
- [![Yard Docs](https://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/ob64/0.8.0)
5
+ [![Yard Docs](https://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/ob64/0.9.0)
6
6
 
7
7
  A *fast* Base64 and uuencode encoder and decoder as a Ruby gem.
8
8
 
@@ -123,6 +123,28 @@ Ob64::UU.decode(string)
123
123
  # "This is data!"
124
124
  ```
125
125
 
126
+ ### Streams
127
+
128
+ Each module has a `Writer` and a `Reader` that wrap an IO, encoding on write and decoding on read. Output is the same as `encode` / `decode` on the whole content.
129
+
130
+ ```ruby
131
+ File.open("data.b64", "wb") do |file|
132
+ writer = Ob64::Base64::RFC4648::Writer.new(file)
133
+ writer.write(chunk1)
134
+ writer << chunk2
135
+ writer.finish # writes the padding; close also closes the file
136
+ end
137
+
138
+ reader = Ob64::Base64::RFC4648::Reader.new(File.open("data.b64", "rb"))
139
+ reader.read(1024) # up to 1024 decoded bytes
140
+ reader.read # the rest
141
+ reader.close
142
+
143
+ IO.copy_stream(Ob64::UU::Reader.new(socket), file)
144
+ ```
145
+
146
+ `Writer.new` and `Reader.new` take the same options as `encode` and `decode`: `urlsafe:`, `padding:` and `loose:` for RFC 4648, `line_length` and `crlf:` for RFC 2045. Readers need an IO with `readpartial`.
147
+
126
148
  ### Codecs
127
149
 
128
150
  The best codec this CPU runs is picked at load and used for every call. `Ob64.codec=` forces one for the whole process (e.g. `:scalar`, plain C).
@@ -166,10 +188,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
166
188
 
167
189
  Bug reports and pull requests are welcome on GitHub at https://github.com/jcmfernandes/ob64. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jcmfernandes/ob64/blob/master/CODE_OF_CONDUCT.md).
168
190
 
169
- ## TODO
170
-
171
- * Streaming interface
172
-
173
191
  ## License
174
192
 
175
193
  Released under the MIT License. See [{file:LICENSE}](LICENSE).
data/ext/ob64/ob64.h CHANGED
@@ -5,5 +5,9 @@
5
5
  #include "ruby.h"
6
6
 
7
7
  void Init_ob64_pack(VALUE mExt);
8
+ long ob64_src_range(VALUE dst, VALUE src, long off, VALUE len);
9
+ void ob64_check_encode_len(size_t len);
10
+ char *ob64_reserve(VALUE dst, long n);
11
+ void ob64_encode_append(VALUE dst, VALUE src, long off, long len, VALUE urlsafe, VALUE padding);
8
12
 
9
13
  #endif /* OB64_H */
data/ext/ob64/ob64_ext.c CHANGED
@@ -1,3 +1,4 @@
1
+ #include <limits.h>
1
2
  #include <stdbool.h>
2
3
  #include "ob64.h"
3
4
  #include "ruby/encoding.h"
@@ -10,7 +11,12 @@ static const char *const codec_names[PACK_CODECS] = { "scalar", "avx2", "avx512"
10
11
  static ID codec_ids[PACK_CODECS];
11
12
 
12
13
  VALUE Ob64_encode_string(VALUE self, VALUE bin, VALUE urlsafe, VALUE padding);
14
+ VALUE Ob64_encode_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE len, VALUE urlsafe, VALUE padding);
13
15
  VALUE Ob64_decode_string(VALUE self, VALUE string, VALUE urlsafe, VALUE loose);
16
+ VALUE Ob64_decode_into(VALUE self, VALUE dst, VALUE src, VALUE len, VALUE urlsafe, VALUE loose);
17
+ VALUE Ob64_consume(VALUE self, VALUE str, VALUE n);
18
+ VALUE Ob64_copy_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE n);
19
+ VALUE Ob64_append_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE n);
14
20
  VALUE Ob64_encoded_length_of(VALUE self, VALUE bin, VALUE withPadding);
15
21
  VALUE Ob64_decoded_length_of(VALUE self, VALUE string);
16
22
  VALUE Ob64_codec(VALUE self);
@@ -33,7 +39,12 @@ Init_ob64_ext(void)
33
39
  mOb64_Ext = rb_define_module_under(mOb64, "Ext");
34
40
 
35
41
  rb_define_private_method(mOb64_Ext, "__encode", Ob64_encode_string, 3);
42
+ rb_define_private_method(mOb64_Ext, "__encode_into", Ob64_encode_into, 6);
36
43
  rb_define_private_method(mOb64_Ext, "__decode", Ob64_decode_string, 3);
44
+ rb_define_private_method(mOb64_Ext, "__decode_into", Ob64_decode_into, 5);
45
+ rb_define_private_method(mOb64_Ext, "__consume", Ob64_consume, 2);
46
+ rb_define_private_method(mOb64_Ext, "__copy_into", Ob64_copy_into, 4);
47
+ rb_define_private_method(mOb64_Ext, "__append_into", Ob64_append_into, 4);
37
48
  rb_define_private_method(mOb64_Ext, "__encoded_length_of", Ob64_encoded_length_of, 2);
38
49
  rb_define_private_method(mOb64_Ext, "__decoded_length_of", Ob64_decoded_length_of, 1);
39
50
  rb_define_private_method(mOb64_Ext, "__codec", Ob64_codec, 0);
@@ -43,40 +54,175 @@ Init_ob64_ext(void)
43
54
  Init_ob64_pack(mOb64_Ext);
44
55
  }
45
56
 
57
+ /* Validates the _into arguments; returns len, a length of src from off */
58
+ long ob64_src_range(VALUE dst, VALUE src, long off, VALUE len)
59
+ {
60
+ long n = NUM2LONG(len);
61
+
62
+ if (dst == src) rb_raise(rb_eArgError, "dst and src must differ");
63
+ if (off < 0 || off > RSTRING_LEN(src)) rb_raise(rb_eArgError, "offset out of range: %ld", off);
64
+ if (n < 0 || n > RSTRING_LEN(src) - off) rb_raise(rb_eArgError, "length out of range: %ld", n);
65
+
66
+ return n;
67
+ }
68
+
69
+ /* Raises unless len bytes are few enough for any encode's size to fit a long */
70
+ void ob64_check_encode_len(size_t len)
71
+ {
72
+ if (len > LONG_MAX / 4) rb_raise(rb_eArgError, "argument too big");
73
+ }
74
+
75
+ /*
76
+ * Makes dst modifiable with room for n more bytes past its length; returns
77
+ * where they go. Keeps a large enough capacity, as expanding reallocates.
78
+ */
79
+ char *ob64_reserve(VALUE dst, long n)
80
+ {
81
+ long len = RSTRING_LEN(dst);
82
+
83
+ if ((size_t)n > rb_str_capacity(dst) - (size_t)len) {
84
+ rb_str_modify_expand(dst, n);
85
+ }
86
+ else {
87
+ rb_str_modify(dst);
88
+ }
89
+
90
+ return RSTRING_PTR(dst) + len;
91
+ }
92
+
93
+ /* Appends the encode of src[off, len] to dst */
94
+ void ob64_encode_append(VALUE dst, VALUE src, long off, long len, VALUE urlsafe, VALUE padding)
95
+ {
96
+ long dst_len = RSTRING_LEN(dst);
97
+ /* Room for padded output */
98
+ char *out = ob64_reserve(dst, (long)__encoded_length_of((size_t)len, true));
99
+ int flags = RTEST(urlsafe) ? PACK_BASE64_URL_SAFE : 0;
100
+
101
+ pack_base64_encode(RSTRING_PTR(src) + off, (size_t)len, out, flags);
102
+ rb_str_set_len(dst, dst_len + (long)__encoded_length_of((size_t)len, RTEST(padding)));
103
+ RB_GC_GUARD(src);
104
+ }
105
+
46
106
  VALUE Ob64_encode_string(VALUE self, VALUE bin, VALUE urlsafe, VALUE padding)
47
107
  {
48
108
  StringValue(bin);
49
- size_t bin_len = RSTRING_LEN(bin);
50
- VALUE result = rb_str_buf_new(__encoded_length_of(bin_len, true));
51
- int flags = RTEST(urlsafe) ? PACK_BASE64_URL_SAFE : 0;
109
+ VALUE result = rb_str_buf_new(0);
52
110
 
53
- pack_base64_encode(RSTRING_PTR(bin), bin_len, RSTRING_PTR(result), flags);
54
- rb_str_set_len(result, __encoded_length_of(bin_len, RTEST(padding)));
111
+ ob64_encode_append(result, bin, 0, RSTRING_LEN(bin), urlsafe, padding);
55
112
  /* US-ASCII, as pack("m0") */
56
113
  ENCODING_CODERANGE_SET(result, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
57
- RB_GC_GUARD(bin);
58
114
 
59
115
  return result;
60
116
  }
61
117
 
62
- VALUE Ob64_decode_string(VALUE self, VALUE string, VALUE urlsafe, VALUE loose)
118
+ VALUE Ob64_encode_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE len, VALUE urlsafe, VALUE padding)
63
119
  {
64
- StringValue(string);
65
- size_t string_len = RSTRING_LEN(string);
120
+ StringValue(dst);
121
+ StringValue(src);
122
+ long o = NUM2LONG(off);
123
+
124
+ ob64_encode_append(dst, src, o, ob64_src_range(dst, src, o, len), urlsafe, padding);
125
+ RB_GC_GUARD(dst);
126
+
127
+ return dst;
128
+ }
129
+
130
+ /* Appends the decode of src[0, len] to dst; on invalid input raises, dst unchanged */
131
+ static void
132
+ decode_append(VALUE dst, VALUE src, long len, VALUE urlsafe, VALUE loose)
133
+ {
134
+ long dst_len = RSTRING_LEN(dst);
66
135
  /* Room for unpadded input */
67
- VALUE result = rb_str_buf_new((string_len + 3) / 4 * 3);
136
+ char *out = ob64_reserve(dst, len / 4 * 3 + (len % 4 ? 3 : 0));
68
137
  int flags = (RTEST(urlsafe) ? PACK_BASE64_URL_SAFE : 0) | (RTEST(loose) ? PACK_BASE64_LOOSE : 0);
69
138
  size_t nout;
70
139
 
71
- if (!pack_base64_decode(RSTRING_PTR(string), string_len, RSTRING_PTR(result), &nout, flags)) {
140
+ if (!pack_base64_decode(RSTRING_PTR(src), (size_t)len, out, &nout, flags)) {
141
+ /* Restores the terminator */
142
+ rb_str_set_len(dst, dst_len);
72
143
  rb_raise(rb_eArgError, "invalid base64");
73
144
  }
74
- rb_str_set_len(result, nout);
75
- RB_GC_GUARD(string);
145
+ rb_str_set_len(dst, dst_len + (long)nout);
146
+ RB_GC_GUARD(src);
147
+ }
148
+
149
+ VALUE Ob64_decode_string(VALUE self, VALUE string, VALUE urlsafe, VALUE loose)
150
+ {
151
+ StringValue(string);
152
+ VALUE result = rb_str_buf_new(0);
153
+
154
+ decode_append(result, string, RSTRING_LEN(string), urlsafe, loose);
76
155
 
77
156
  return result;
78
157
  }
79
158
 
159
+ VALUE Ob64_decode_into(VALUE self, VALUE dst, VALUE src, VALUE len, VALUE urlsafe, VALUE loose)
160
+ {
161
+ StringValue(dst);
162
+ StringValue(src);
163
+ decode_append(dst, src, ob64_src_range(dst, src, 0, len), urlsafe, loose);
164
+ RB_GC_GUARD(dst);
165
+
166
+ return dst;
167
+ }
168
+
169
+ /* Drops str's first n bytes */
170
+ VALUE Ob64_consume(VALUE self, VALUE str, VALUE n)
171
+ {
172
+ StringValue(str);
173
+ long c = NUM2LONG(n);
174
+ long len = RSTRING_LEN(str);
175
+
176
+ if (c < 0 || c > len) rb_raise(rb_eArgError, "length out of range: %ld", c);
177
+ rb_str_modify(str);
178
+ memmove(RSTRING_PTR(str), RSTRING_PTR(str) + c, (size_t)(len - c));
179
+ rb_str_set_len(str, len - c);
180
+ RB_GC_GUARD(str);
181
+
182
+ return str;
183
+ }
184
+
185
+ /* Appends src[off, n] to dst */
186
+ static void
187
+ append(VALUE dst, VALUE src, long off, long n)
188
+ {
189
+ long dst_len = RSTRING_LEN(dst);
190
+
191
+ memcpy(ob64_reserve(dst, n), RSTRING_PTR(src) + off, (size_t)n);
192
+ rb_str_set_len(dst, dst_len + n);
193
+ RB_GC_GUARD(src);
194
+ }
195
+
196
+ /* Replaces dst's content with src[off, n] */
197
+ VALUE Ob64_copy_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE n)
198
+ {
199
+ StringValue(dst);
200
+ StringValue(src);
201
+ long o = NUM2LONG(off);
202
+ long c = ob64_src_range(dst, src, o, n);
203
+
204
+ /* Independent before setting the length */
205
+ rb_str_modify(dst);
206
+ rb_str_set_len(dst, 0);
207
+ append(dst, src, o, c);
208
+ RB_GC_GUARD(dst);
209
+
210
+ return dst;
211
+ }
212
+
213
+ /* Appends src[off, n] to dst */
214
+ VALUE Ob64_append_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE n)
215
+ {
216
+ StringValue(dst);
217
+ StringValue(src);
218
+ long o = NUM2LONG(off);
219
+
220
+ append(dst, src, o, ob64_src_range(dst, src, o, n));
221
+ RB_GC_GUARD(dst);
222
+
223
+ return dst;
224
+ }
225
+
80
226
  VALUE Ob64_encoded_length_of(VALUE self, VALUE bin, VALUE withPadding)
81
227
  {
82
228
  StringValue(bin);
@@ -90,6 +236,7 @@ VALUE Ob64_decoded_length_of(VALUE self, VALUE string)
90
236
 
91
237
  size_t __encoded_length_of(size_t bin_len, bool withPadding)
92
238
  {
239
+ ob64_check_encode_len(bin_len);
93
240
  if (withPadding) {
94
241
  return (bin_len + 2) / 3 * 4;
95
242
  } else {
data/ext/ob64/ob64_pack.c CHANGED
@@ -35,43 +35,76 @@ parse_format(VALUE fmt, long *count)
35
35
  return p[0];
36
36
  }
37
37
 
38
- /* Sets res's length to n and makes it a US-ASCII String, as native pack does */
38
+ /* Makes res a US-ASCII String, as native pack does */
39
39
  static VALUE
40
- ascii_result(VALUE res, size_t n)
40
+ ascii_result(VALUE res)
41
41
  {
42
- rb_str_set_len(res, (long)n);
43
42
  ENCODING_CODERANGE_SET(res, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
44
43
 
45
44
  return res;
46
45
  }
47
46
 
47
+ /* Per line of line_len bytes (a multiple of 3): a length byte, 4 chars per 3 bytes and a newline */
48
+ static long
49
+ uuencoded_length(long len, long line_len)
50
+ {
51
+ ob64_check_encode_len((size_t)len);
52
+ long full = len / line_len, rest = len % line_len;
53
+
54
+ return full * (line_len / 3 * 4 + 2) + (rest ? (rest + 2) / 3 * 4 + 2 : 0);
55
+ }
56
+
57
+ /* Appends the uuencode of src[off, len], line_len bytes per line, to dst */
58
+ static void
59
+ uuencode_append(VALUE dst, VALUE src, long off, long len, long line_len)
60
+ {
61
+ long dst_len = RSTRING_LEN(dst);
62
+ char *out = ob64_reserve(dst, uuencoded_length(len, line_len));
63
+ size_t n = pack_uuencode_lines(RSTRING_PTR(src) + off, (size_t)len, out, (size_t)line_len);
64
+ RB_GC_GUARD(src);
65
+
66
+ rb_str_set_len(dst, dst_len + (long)n);
67
+ }
68
+
48
69
  static VALUE
49
70
  uuencode(VALUE from, long line_len)
50
71
  {
51
- long plen = RSTRING_LEN(from);
52
- /* A length byte and a newline per line */
53
- long lines = (plen + line_len - 1) / line_len;
54
- VALUE res = rb_str_buf_new((plen + 2) / 3 * 4 + lines * 2);
55
- const char *src = RSTRING_PTR(from);
56
- char *out = RSTRING_PTR(res);
57
- size_t n = pack_uuencode_lines(src, (size_t)plen, out, (size_t)line_len);
58
- RB_GC_GUARD(from);
59
-
60
- return ascii_result(res, n);
72
+ VALUE res = rb_str_buf_new(0);
73
+
74
+ uuencode_append(res, from, 0, RSTRING_LEN(from), line_len);
75
+
76
+ return ascii_result(res);
77
+ }
78
+
79
+ /* line_bytes: bytes per line, a multiple of 3; 4 chars per 3 bytes, a line break per line */
80
+ static long
81
+ encoded_length_rfc2045(long len, long line_bytes, int crlf)
82
+ {
83
+ ob64_check_encode_len((size_t)len);
84
+
85
+ return (len + 2) / 3 * 4 + (len + line_bytes - 1) / line_bytes * (crlf ? 2 : 1);
86
+ }
87
+
88
+ /* Appends the encode of src[off, len] to dst; line_bytes as above, crlf: lines end in "\r\n" */
89
+ static void
90
+ encode_rfc2045_append(VALUE dst, VALUE src, long off, long len, long line_bytes, int crlf)
91
+ {
92
+ long dst_len = RSTRING_LEN(dst);
93
+ char *out = ob64_reserve(dst, encoded_length_rfc2045(len, line_bytes, crlf));
94
+ size_t n = pack_base64_encode_rfc2045(RSTRING_PTR(src) + off, (size_t)len, out, (int)line_bytes, crlf);
95
+ RB_GC_GUARD(src);
96
+
97
+ rb_str_set_len(dst, dst_len + (long)n);
61
98
  }
62
99
 
63
- /* line_len: bytes per line, a multiple of 3; crlf: lines end in "\r\n" */
64
100
  static VALUE
65
- base64_encode_rfc2045(VALUE from, long line_len, int crlf)
101
+ base64_encode_rfc2045(VALUE from, long line_bytes, int crlf)
66
102
  {
67
- long plen = RSTRING_LEN(from);
68
- /* A line break per line */
69
- long lines = (plen + line_len - 1) / line_len;
70
- VALUE res = rb_str_buf_new((plen + 2) / 3 * 4 + lines * (crlf ? 2 : 1));
71
- size_t n = pack_base64_encode_rfc2045(RSTRING_PTR(from), (size_t)plen, RSTRING_PTR(res), (int)line_len, crlf);
72
- RB_GC_GUARD(from);
73
-
74
- return ascii_result(res, n);
103
+ VALUE res = rb_str_buf_new(0);
104
+
105
+ encode_rfc2045_append(res, from, 0, RSTRING_LEN(from), line_bytes, crlf);
106
+
107
+ return ascii_result(res);
75
108
  }
76
109
 
77
110
  static VALUE
@@ -90,11 +123,11 @@ Ob64_pack(int argc, VALUE *argv, VALUE ary)
90
123
  }
91
124
 
92
125
  if (type == 'm' && count == 0) {
93
- VALUE res = rb_str_buf_new((plen + 2) / 3 * 4);
94
- size_t n = pack_base64_encode(RSTRING_PTR(from), (size_t)plen, RSTRING_PTR(res), 0);
95
- RB_GC_GUARD(from);
126
+ VALUE res = rb_str_buf_new(0);
127
+
128
+ ob64_encode_append(res, from, 0, plen, Qfalse, Qtrue);
96
129
 
97
- return ascii_result(res, n);
130
+ return ascii_result(res);
98
131
  }
99
132
 
100
133
  long len = count;
@@ -179,18 +212,15 @@ Ob64_unpack1(int argc, VALUE *argv, VALUE str)
179
212
  return res;
180
213
  }
181
214
 
182
- /* Bytes per line for a line_length in chars; validates both arguments */
215
+ /* Bytes per line for a line_length in chars; validates it */
183
216
  static long
184
- rfc2045_line_bytes(VALUE bin, VALUE line_length)
217
+ rfc2045_line_bytes(VALUE line_length)
185
218
  {
186
219
  long chars = NUM2LONG(line_length);
187
220
 
188
- StringValue(bin);
189
221
  if (chars < 4 || chars % 4 || chars / 4 * 3 > INT_MAX) {
190
222
  rb_raise(rb_eArgError, "line length must be a positive multiple of 4: %ld", chars);
191
223
  }
192
- /* Bound the output size */
193
- if (RSTRING_LEN(bin) > LONG_MAX / 4) rb_raise(rb_eArgError, "argument too big");
194
224
 
195
225
  return chars / 4 * 3;
196
226
  }
@@ -198,27 +228,72 @@ rfc2045_line_bytes(VALUE bin, VALUE line_length)
198
228
  static VALUE
199
229
  Ob64_encode_rfc_2045(VALUE self, VALUE bin, VALUE line_length, VALUE crlf)
200
230
  {
201
- long line_bytes = rfc2045_line_bytes(bin, line_length);
231
+ long line_bytes = rfc2045_line_bytes(line_length);
232
+
233
+ StringValue(bin);
202
234
 
203
235
  return base64_encode_rfc2045(bin, line_bytes, RTEST(crlf));
204
236
  }
205
237
 
238
+ static VALUE
239
+ Ob64_encode_rfc_2045_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE len, VALUE line_length, VALUE crlf)
240
+ {
241
+ StringValue(dst);
242
+ StringValue(src);
243
+ long o = NUM2LONG(off);
244
+ long n = ob64_src_range(dst, src, o, len);
245
+
246
+ encode_rfc2045_append(dst, src, o, n, rfc2045_line_bytes(line_length), RTEST(crlf));
247
+ RB_GC_GUARD(dst);
248
+
249
+ return dst;
250
+ }
251
+
252
+ /* Appends the decode of src[0, len] to dst; returns the bytes appended */
253
+ static size_t
254
+ decode_rfc2045_append(VALUE dst, VALUE src, long len)
255
+ {
256
+ long dst_len = RSTRING_LEN(dst);
257
+ /* Room for unpadded input and decoder overruns */
258
+ char *out = ob64_reserve(dst, len / 4 * 3 + 3);
259
+ size_t n = pack_base64_decode_rfc2045(RSTRING_PTR(src), (size_t)len, out);
260
+ RB_GC_GUARD(src);
261
+
262
+ rb_str_set_len(dst, dst_len + (long)n);
263
+
264
+ return n;
265
+ }
266
+
206
267
  static VALUE
207
268
  Ob64_decode_rfc_2045(VALUE self, VALUE string)
208
269
  {
209
270
  StringValue(string);
271
+ VALUE res = rb_str_buf_new(0);
272
+
273
+ decode_rfc2045_append(res, string, RSTRING_LEN(string));
210
274
 
211
- return base64_decode(string, 1);
275
+ return res;
276
+ }
277
+
278
+ static VALUE
279
+ Ob64_decode_rfc_2045_into(VALUE self, VALUE dst, VALUE src, VALUE len)
280
+ {
281
+ StringValue(dst);
282
+ StringValue(src);
283
+ size_t n = decode_rfc2045_append(dst, src, ob64_src_range(dst, src, 0, len));
284
+ RB_GC_GUARD(dst);
285
+
286
+ return SIZET2NUM(n);
212
287
  }
213
288
 
214
289
  static VALUE
215
290
  Ob64_encoded_length_of_rfc_2045(VALUE self, VALUE bin, VALUE line_length, VALUE crlf)
216
291
  {
217
- long line_bytes = rfc2045_line_bytes(bin, line_length);
218
- long plen = RSTRING_LEN(bin);
292
+ long line_bytes = rfc2045_line_bytes(line_length);
219
293
 
220
- /* 4 chars per 3 bytes, a line break per line */
221
- return LONG2NUM((plen + 2) / 3 * 4 + (plen + line_bytes - 1) / line_bytes * (RTEST(crlf) ? 2 : 1));
294
+ StringValue(bin);
295
+
296
+ return LONG2NUM(encoded_length_rfc2045(RSTRING_LEN(bin), line_bytes, RTEST(crlf)));
222
297
  }
223
298
 
224
299
  static VALUE
@@ -239,6 +314,19 @@ Ob64_uuencode(VALUE self, VALUE bin)
239
314
  return uuencode(bin, 45);
240
315
  }
241
316
 
317
+ static VALUE
318
+ Ob64_uuencode_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE len)
319
+ {
320
+ StringValue(dst);
321
+ StringValue(src);
322
+ long o = NUM2LONG(off);
323
+
324
+ uuencode_append(dst, src, o, ob64_src_range(dst, src, o, len), 45);
325
+ RB_GC_GUARD(dst);
326
+
327
+ return dst;
328
+ }
329
+
242
330
  static VALUE
243
331
  Ob64_uudecode(VALUE self, VALUE string)
244
332
  {
@@ -247,15 +335,64 @@ Ob64_uudecode(VALUE self, VALUE string)
247
335
  return uudecode(string);
248
336
  }
249
337
 
338
+ /*
339
+ * Appends the uudecode of src[0, len], capped at cap bytes, to dst; returns
340
+ * whether decoding ended before the end
341
+ */
342
+ static VALUE
343
+ uudecode_capped_append(VALUE dst, VALUE src, long len, long c)
344
+ {
345
+ if (c < 0) rb_raise(rb_eArgError, "negative cap: %ld", c);
346
+
347
+ /* Lines take 2+ chars (the last one 1+) and decode to 63 bytes at most: a larger cap is the same */
348
+ long lines = (len + 1) / 2;
349
+ if (c / 63 > lines) c = lines * 63;
350
+
351
+ long dst_len = RSTRING_LEN(dst);
352
+ int stopped;
353
+ /* +3: the decoder stores up to 3 bytes past a line's decoded length */
354
+ char *out = ob64_reserve(dst, c + 3);
355
+ size_t total = pack_uudecode_lines_capped(RSTRING_PTR(src), (size_t)len, out, (size_t)c, &stopped);
356
+ RB_GC_GUARD(src);
357
+
358
+ rb_str_set_len(dst, dst_len + (long)total);
359
+
360
+ return stopped ? Qtrue : Qfalse;
361
+ }
362
+
363
+ /* Returns [decoded, stopped]: uudecode with the output capped at cap bytes; stopped: decoding ended before the end */
364
+ static VALUE
365
+ Ob64_uudecode_capped(VALUE self, VALUE string, VALUE cap)
366
+ {
367
+ long c = NUM2LONG(cap);
368
+
369
+ StringValue(string);
370
+ VALUE buf = rb_str_buf_new(0);
371
+ VALUE stopped = uudecode_capped_append(buf, string, RSTRING_LEN(string), c);
372
+
373
+ return rb_assoc_new(buf, stopped);
374
+ }
375
+
376
+ static VALUE
377
+ Ob64_uudecode_capped_into(VALUE self, VALUE dst, VALUE src, VALUE len, VALUE cap)
378
+ {
379
+ long c = NUM2LONG(cap);
380
+
381
+ StringValue(dst);
382
+ StringValue(src);
383
+ long n = ob64_src_range(dst, src, 0, len);
384
+ VALUE stopped = uudecode_capped_append(dst, src, n, c);
385
+ RB_GC_GUARD(dst);
386
+
387
+ return stopped;
388
+ }
389
+
250
390
  static VALUE
251
391
  Ob64_uuencoded_length_of(VALUE self, VALUE bin)
252
392
  {
253
393
  StringValue(bin);
254
- long plen = RSTRING_LEN(bin);
255
- /* Per line: a length byte, 4 chars per 3 bytes and a newline */
256
- long full = plen / 45, rest = plen % 45;
257
394
 
258
- return LONG2NUM(full * 62 + (rest ? (rest + 2) / 3 * 4 + 2 : 0));
395
+ return LONG2NUM(uuencoded_length(RSTRING_LEN(bin), 45));
259
396
  }
260
397
 
261
398
  static VALUE
@@ -279,11 +416,16 @@ Init_ob64_pack(VALUE mExt)
279
416
  rb_define_method(mUnpack, "unpack1", Ob64_unpack1, -1);
280
417
 
281
418
  rb_define_private_method(mExt, "__encode_rfc_2045", Ob64_encode_rfc_2045, 3);
419
+ rb_define_private_method(mExt, "__encode_rfc_2045_into", Ob64_encode_rfc_2045_into, 6);
282
420
  rb_define_private_method(mExt, "__decode_rfc_2045", Ob64_decode_rfc_2045, 1);
421
+ rb_define_private_method(mExt, "__decode_rfc_2045_into", Ob64_decode_rfc_2045_into, 3);
283
422
  rb_define_private_method(mExt, "__encoded_length_of_rfc_2045", Ob64_encoded_length_of_rfc_2045, 3);
284
423
  rb_define_private_method(mExt, "__decoded_length_of_rfc_2045", Ob64_decoded_length_of_rfc_2045, 1);
285
424
  rb_define_private_method(mExt, "__uuencode", Ob64_uuencode, 1);
425
+ rb_define_private_method(mExt, "__uuencode_into", Ob64_uuencode_into, 4);
286
426
  rb_define_private_method(mExt, "__uudecode", Ob64_uudecode, 1);
427
+ rb_define_private_method(mExt, "__uudecode_capped", Ob64_uudecode_capped, 2);
428
+ rb_define_private_method(mExt, "__uudecode_capped_into", Ob64_uudecode_capped_into, 4);
287
429
  rb_define_private_method(mExt, "__uuencoded_length_of", Ob64_uuencoded_length_of, 1);
288
430
  rb_define_private_method(mExt, "__uudecoded_length_of", Ob64_uudecoded_length_of, 1);
289
431
  }
@@ -1694,11 +1694,9 @@ uu_decode_batch(pack_bulk_loop loop, const uint8_t *src, size_t n, uint8_t *out)
1694
1694
  }
1695
1695
 
1696
1696
  size_t
1697
- pack_uudecode_lines(const char *src, size_t srclen, char *out)
1697
+ pack_uudecode_lines_capped(const char *src, size_t srclen, char *out, size_t cap, int *stopped)
1698
1698
  {
1699
- struct uu_walk w = {
1700
- (const uint8_t *)src, (const uint8_t *)src + srclen, (uint8_t *)out, 0, pack_uudecode_cap(srclen)
1701
- };
1699
+ struct uu_walk w = { (const uint8_t *)src, (const uint8_t *)src + srclen, (uint8_t *)out, 0, cap };
1702
1700
  pack_bulk_loop loop = out ? pack_simd_dec_loop() : NULL;
1703
1701
 
1704
1702
  if (loop) {
@@ -1732,10 +1730,20 @@ pack_uudecode_lines(const char *src, size_t srclen, char *out)
1732
1730
  w.out += n / 4 * 3;
1733
1731
  continue;
1734
1732
  }
1735
- if (!uu_walk_line(&w)) return w.total;
1733
+ if (!uu_walk_line(&w)) break;
1736
1734
  }
1737
1735
  }
1738
1736
 
1737
+ /* Resumes after an invalid batch; a no-op once the loop ended */
1739
1738
  while (uu_walk_line(&w)) {}
1739
+ *stopped = w.s < w.send;
1740
1740
  return w.total;
1741
1741
  }
1742
+
1743
+ size_t
1744
+ pack_uudecode_lines(const char *src, size_t srclen, char *out)
1745
+ {
1746
+ int stopped;
1747
+
1748
+ return pack_uudecode_lines_capped(src, srclen, out, pack_uudecode_cap(srclen), &stopped);
1749
+ }
@@ -10,9 +10,13 @@ size_t pack_uuencode_decode(const char *src, size_t srclen, char *out);
10
10
  /* Upper bound of the decoded length of srclen chars: srclen * 3 / 4 without overflow */
11
11
  size_t pack_uudecode_cap(size_t srclen);
12
12
  /*
13
- * Decodes like native unpack("u") and returns the decoded length. out needs
14
- * pack_uudecode_cap(srclen) + 3 bytes; NULL computes the length only.
13
+ * Decodes like native unpack("u") with the output capped at cap bytes (natively
14
+ * pack_uudecode_cap(srclen)) and returns the decoded length. out needs cap + 3
15
+ * bytes; NULL computes the length only. *stopped: 1 if decoding ended before
16
+ * the end of src (a line not starting with a char in '!'..'`'), 0 otherwise.
15
17
  */
18
+ size_t pack_uudecode_lines_capped(const char *src, size_t srclen, char *out, size_t cap, int *stopped);
19
+ /* pack_uudecode_lines_capped with cap pack_uudecode_cap(srclen) */
16
20
  size_t pack_uudecode_lines(const char *src, size_t srclen, char *out);
17
21
 
18
22
  #endif /* PACK_UUENCODE_H */
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ob64
4
+ # Reads the decoded version of the text in an io, like an IO: {#read},
5
+ # {#readpartial}, {#eof?}, {#close}.
6
+ #
7
+ # Subclasses define +decode(text, eof, dst)+, which decodes from the front of
8
+ # +text+, the pulled text not yet decoded, appending the bytes to +dst+ and
9
+ # removing the decoded text from +text+, in place. The text left is passed
10
+ # again, with more text after it, on the next call. At +eof+ (no more text),
11
+ # it decodes all or raises. It may call +stop+ to ignore the rest of the io.
12
+ class Reader
13
+ include Ext
14
+
15
+ # Bytes of text pulled from the io at a time
16
+ CHUNK = 65_536
17
+ private_constant :CHUNK
18
+
19
+ # @param io [IO] - responds to +readpartial(maxlen, outbuf)+
20
+ def initialize(io)
21
+ @io = io
22
+ # The last text pulled
23
+ @chunk = "".b
24
+ # The text not yet decoded
25
+ @text = "".b
26
+ # The data decoded; bytes before @off are already read
27
+ @data = "".b
28
+ @off = 0
29
+ @pulled = 0
30
+ @decoded = 0
31
+ @done = false
32
+ end
33
+
34
+ # Reads +length+ bytes, fewer only at the end, or all that is left without
35
+ # +length+, like IO#read.
36
+ #
37
+ # @param length [Integer, nil]
38
+ # @param outbuf [String, nil] - if given, its content is replaced by the data and it
39
+ # is returned
40
+ # @return [String, nil] binary; +nil+ at the end if +length+ is positive
41
+ # @raise [ArgumentError] if +length+ is negative or the text cannot be decoded
42
+ def read(length = nil, outbuf = nil)
43
+ raise ArgumentError, "negative length #{length} given" if length&.negative?
44
+
45
+ fill(length || Float::INFINITY)
46
+ if length&.positive? && available.zero?
47
+ outbuf&.clear
48
+ return
49
+ end
50
+ output(length || available, outbuf)
51
+ end
52
+
53
+ # Reads 1 to +maxlen+ bytes, as soon as any are decoded, like
54
+ # IO#readpartial.
55
+ #
56
+ # @param maxlen [Integer]
57
+ # @param outbuf [String, nil] - if given, its content is replaced by the data and it
58
+ # is returned
59
+ # @return [String] binary
60
+ # @raise [EOFError] at the end, if +maxlen+ is positive
61
+ # @raise [ArgumentError] if +maxlen+ is negative or the text cannot be decoded
62
+ def readpartial(maxlen, outbuf = nil)
63
+ raise ArgumentError, "negative length #{maxlen} given" if maxlen.negative?
64
+
65
+ if maxlen.positive?
66
+ fill(1)
67
+ if available.zero?
68
+ outbuf&.clear
69
+ raise EOFError, "end of file reached"
70
+ end
71
+ end
72
+ output(maxlen, outbuf)
73
+ end
74
+
75
+ # Returns whether all the data was read. Pulls text from the io until some
76
+ # data is decoded or the io ends.
77
+ #
78
+ # @return [Boolean]
79
+ # @raise [ArgumentError] if the text cannot be decoded
80
+ def eof?
81
+ fill(1)
82
+ available.zero?
83
+ end
84
+ alias_method :eof, :eof?
85
+
86
+ # Closes the io.
87
+ #
88
+ # @return [nil]
89
+ def close
90
+ @io.close
91
+ nil
92
+ end
93
+
94
+ private
95
+
96
+ # Bytes of text pulled from the io so far
97
+ attr_reader :pulled
98
+
99
+ # Bytes decoded so far: appended by +decode+
100
+ attr_reader :decoded
101
+
102
+ # Ignores the rest of the io: the data ends with what +decode+ appends
103
+ def stop
104
+ @done = true
105
+ end
106
+
107
+ # Bytes decoded and not yet read
108
+ def available = @data.bytesize - @off
109
+
110
+ # Decodes until +length+ bytes are ready or the data ends
111
+ def fill(length)
112
+ until available >= length || @done
113
+ eof = !pull
114
+ # Drops the bytes read, keeping the buffer
115
+ __consume(@data, @off) if @off > 0
116
+ @off = 0
117
+ size = @data.bytesize
118
+ decode(@text, eof, @data)
119
+ @decoded += @data.bytesize - size
120
+ @done ||= eof
121
+ end
122
+ end
123
+
124
+ # Appends the next text from the io to @text; false at its end
125
+ def pull
126
+ chunk = @io.readpartial(CHUNK, @chunk)
127
+ @pulled += chunk.bytesize
128
+ # A copy only if the io returned a String of its own
129
+ @text << (chunk.equal?(@chunk) ? chunk.force_encoding(Encoding::BINARY) : chunk.b)
130
+ true
131
+ rescue EOFError
132
+ false
133
+ end
134
+
135
+ # Returns the next +length+ bytes at most, in +outbuf+ if given
136
+ def output(length, outbuf)
137
+ n = available
138
+ n = length if length < n
139
+ if outbuf
140
+ __copy_into(outbuf, @data, @off, n)
141
+ outbuf.force_encoding(Encoding::BINARY)
142
+ else
143
+ outbuf = @data.byteslice(@off, n)
144
+ end
145
+ @off += n
146
+ outbuf
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ob64
4
+ module Base64
5
+ module RFC2045
6
+ # Writes the {RFC2045.encode} of the bytes written to it to an io.
7
+ class Writer < Ob64::Writer
8
+ # @param io [IO] - responds to +write+; must not keep the String it is
9
+ # given, as it is reused
10
+ # @param line_length [Integer] - characters per line, a positive multiple of 4
11
+ # @param crlf [Boolean] - if lines end with CRLF instead of LF
12
+ # @raise [ArgumentError] if +line_length+ is not a positive multiple of 4
13
+ def initialize(io, line_length = 60, crlf: false)
14
+ __encode_rfc_2045("", line_length, crlf)
15
+ super(io, line_length / 4 * 3)
16
+ @line_length = line_length
17
+ @crlf = crlf
18
+ end
19
+
20
+ private
21
+
22
+ def encode(dst, src, off, len) = __encode_rfc_2045_into(dst, src, off, len, @line_length, @crlf)
23
+ end
24
+
25
+ # Reads the {RFC2045.decode} of the text in an io.
26
+ class Reader < Ob64::Reader
27
+ # Characters that decode; others are skipped, but '=' after 2 or 3 of a
28
+ # group ends the data
29
+ ALPHABET = "A-Za-z0-9+/"
30
+
31
+ # Characters skipped
32
+ SKIPPED = "^#{ALPHABET}"
33
+
34
+ # Whether each byte is in ALPHABET
35
+ ALPHABETIC = Array.new(256) { |b| b.chr.count(ALPHABET) == 1 }.freeze
36
+
37
+ # A group start followed by '=' after 2 or 3 alphabet characters
38
+ ENDS = /\A(?:[^#{ALPHABET}]*[#{ALPHABET}]){2}.*=/mo
39
+ private_constant :ALPHABET, :SKIPPED, :ALPHABETIC, :ENDS
40
+
41
+ private
42
+
43
+ # Decodes up to a whole number of groups of 4 alphabet characters; the
44
+ # rest, at most 3 of them, waits for more text unless a '=' in it ends
45
+ # the data. A piece decoding to fewer bytes than its groups hold had a
46
+ # '=' ending the data.
47
+ def decode(text, eof, dst)
48
+ cut = text.bytesize
49
+ unless eof
50
+ alpha = text.count(ALPHABET)
51
+ (alpha % 4).times do
52
+ cut -= 1
53
+ cut -= 1 until ALPHABETIC[text.getbyte(cut)]
54
+ end
55
+ end
56
+ n = __decode_rfc_2045_into(dst, text, cut)
57
+ __consume(text, cut)
58
+ return if eof
59
+
60
+ if n < alpha / 4 * 3
61
+ stop
62
+ elsif ENDS.match?(text)
63
+ stop
64
+ __decode_rfc_2045_into(dst, text, text.bytesize)
65
+ else
66
+ # Without a '=' ending the data, only its alphabet characters count
67
+ text.delete!(SKIPPED)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ob64
4
+ module Base64
5
+ module RFC4648
6
+ # Writes the {RFC4648.encode} (or, with +urlsafe+,
7
+ # {RFC4648.encode_urlsafe}) of the bytes written to it to an io.
8
+ class Writer < Ob64::Writer
9
+ # @param io [IO] - responds to +write+; must not keep the String it is
10
+ # given, as it is reused
11
+ # @param urlsafe [Boolean] - if the ``URL and Filename Safe'' alphabet is used
12
+ # @param padding [Boolean] - if the output must be padded
13
+ def initialize(io, urlsafe: false, padding: true)
14
+ super(io, 3)
15
+ @urlsafe = urlsafe
16
+ @padding = padding
17
+ end
18
+
19
+ private
20
+
21
+ def encode(dst, src, off, len) = __encode_into(dst, src, off, len, @urlsafe, @padding)
22
+ end
23
+
24
+ # Reads the {RFC4648.decode} (or, with +urlsafe+,
25
+ # {RFC4648.decode_urlsafe}) of the text in an io. ArgumentError is raised
26
+ # where +decode+ raises, possibly after returning the bytes before the bad
27
+ # spot.
28
+ class Reader < Ob64::Reader
29
+ # @param io [IO] - responds to +readpartial(maxlen, outbuf)+
30
+ # @param urlsafe [Boolean] - if the ``URL and Filename Safe'' alphabet is used;
31
+ # padding is then optional
32
+ # @param loose [Boolean] - if unpadded input is accepted
33
+ def initialize(io, urlsafe: false, loose: false)
34
+ super(io)
35
+ @urlsafe = urlsafe
36
+ @loose = loose || urlsafe
37
+ @padded = false
38
+ end
39
+
40
+ private
41
+
42
+ # Decodes groups of 4 strictly and, at eof, the rest as configured.
43
+ # After '=', more text is invalid.
44
+ def decode(text, eof, dst)
45
+ raise ArgumentError, "invalid base64" if @padded && !text.empty?
46
+
47
+ cut = eof ? text.bytesize : text.bytesize / 4 * 4
48
+ pad = text.byteindex("=")
49
+ @padded = true if pad && pad < cut
50
+ __decode_into(dst, text, cut, @urlsafe, eof && @loose)
51
+ __consume(text, cut)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -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.8.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.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Fernandes
@@ -52,7 +52,13 @@ files:
52
52
  - ext/ob64/pack_uuencode.h
53
53
  - lib/ob64.rb
54
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
55
60
  - lib/ob64/version.rb
61
+ - lib/ob64/writer.rb
56
62
  - ob64.gemspec
57
63
  homepage: https://github.com/jcmfernandes/ob64
58
64
  licenses:
@@ -60,8 +66,8 @@ licenses:
60
66
  metadata:
61
67
  homepage_uri: https://github.com/jcmfernandes/ob64
62
68
  changelog_uri: https://github.com/jcmfernandes/ob64/blob/master/CHANGELOG.md
63
- documentation_uri: https://www.rubydoc.info/gems/ob64/0.8.0
64
- source_code_uri: https://github.com/jcmfernandes/ob64/tree/v0.8.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
65
71
  rdoc_options: []
66
72
  require_paths:
67
73
  - lib