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.
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
  }
@@ -4359,7 +4359,7 @@ pack_base64_xgetbv (uint32_t index)
4359
4359
  }
4360
4360
 
4361
4361
  #ifndef bit_AVX512vl
4362
- #define bit_AVX512vl (1 << 31)
4362
+ #define bit_AVX512vl (1u << 31)
4363
4363
  #endif
4364
4364
  #ifndef bit_AVX512vbmi
4365
4365
  #define bit_AVX512vbmi (1 << 1)
@@ -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