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.
- checksums.yaml +4 -4
- data/.github/scripts/install-sde +21 -0
- data/.github/workflows/main.yml +54 -4
- data/CHANGELOG.md +9 -0
- data/README.md +69 -8
- data/Rakefile +11 -0
- data/ext/ob64/ob64.h +4 -0
- data/ext/ob64/ob64_ext.c +163 -13
- data/ext/ob64/ob64_pack.c +184 -42
- data/ext/ob64/pack_base64.c +1 -1
- data/ext/ob64/pack_uuencode.c +13 -5
- data/ext/ob64/pack_uuencode.h +6 -2
- data/lib/ob64/reader.rb +149 -0
- data/lib/ob64/streams/rfc2045.rb +73 -0
- data/lib/ob64/streams/rfc4648.rb +56 -0
- data/lib/ob64/streams/uu.rb +43 -0
- data/lib/ob64/streams.rb +7 -0
- data/lib/ob64/version.rb +1 -1
- data/lib/ob64/writer.rb +124 -0
- data/lib/ob64.rb +2 -0
- metadata +10 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a9b61f1de15353d43ea9786125d9d960adbf5f37aeb5bf4a46d55b2a85eaa82e
|
|
4
|
+
data.tar.gz: '08237edfc6bf9a5d977941d73ce9dc535579a6ba23cdb58d8eff14acbf69c03d'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f20d33b04fc3ea300516c556239d6e164957e66984686736415c5e904d1058df2a5b8a015d3d8d803e17e73a03225ec48afecac8e2a889e9c8913380f54706b5
|
|
7
|
+
data.tar.gz: 9b6b010c3dda40535733cb4f922befb61facad728c18a53846fb6b3b796a213d06de77a8b02cccbb4262a516c245116482bf8958d49408db8d05087aa5bf93c6
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Installs Intel SDE if this CPU can't run the avx512 codec, and exports SDE,
|
|
3
|
+
# a command prefix that emulates a CPU that can.
|
|
4
|
+
set -euo pipefail
|
|
5
|
+
|
|
6
|
+
missing=
|
|
7
|
+
for flag in avx512vl avx512vbmi avx512_vbmi2; do
|
|
8
|
+
grep -qw "$flag" /proc/cpuinfo || missing=1
|
|
9
|
+
done
|
|
10
|
+
[ -n "$missing" ] || exit 0
|
|
11
|
+
|
|
12
|
+
url=https://downloadmirror.intel.com/924984/sde-external-10.13.1-2026-07-28-lin.tar.xz
|
|
13
|
+
sha256=94e97d623fec54385686e1e7ba65ebc9941748c05ee451423948334892bf2b50
|
|
14
|
+
dir="$RUNNER_TEMP/sde"
|
|
15
|
+
|
|
16
|
+
curl -fsSL -o "$dir.tar.xz" "$url"
|
|
17
|
+
echo "$sha256 $dir.tar.xz" | sha256sum -c --quiet
|
|
18
|
+
mkdir -p "$dir"
|
|
19
|
+
tar xJf "$dir.tar.xz" -C "$dir" --strip-components=1
|
|
20
|
+
# Ice Lake: AVX512 with VBMI and VBMI2
|
|
21
|
+
echo "SDE=$dir/sde64 -icl --" >>"$GITHUB_ENV"
|
data/.github/workflows/main.yml
CHANGED
|
@@ -36,8 +36,15 @@ jobs:
|
|
|
36
36
|
ruby: ["3.3", "3.4", "4.0"]
|
|
37
37
|
os: [ubuntu-24.04, ubuntu-24.04-arm]
|
|
38
38
|
cc: [gcc, clang]
|
|
39
|
+
include:
|
|
40
|
+
# Ruby built with AddressSanitizer, x86-64 only: memory errors in the
|
|
41
|
+
# C code fail the specs. ubsan adds UndefinedBehaviorSanitizer.
|
|
42
|
+
- ruby: asan-release
|
|
43
|
+
os: ubuntu-24.04
|
|
44
|
+
cc: clang
|
|
45
|
+
ubsan: true
|
|
39
46
|
runs-on: ${{ matrix.os }}
|
|
40
|
-
timeout-minutes:
|
|
47
|
+
timeout-minutes: 30
|
|
41
48
|
steps:
|
|
42
49
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
43
50
|
with:
|
|
@@ -55,14 +62,57 @@ jobs:
|
|
|
55
62
|
- name: Compile
|
|
56
63
|
env:
|
|
57
64
|
# Same warnings for both compilers; Ruby's defaults are GCC-only.
|
|
58
|
-
MAKEFLAGS:
|
|
65
|
+
MAKEFLAGS: >-
|
|
66
|
+
CC=${{ matrix.cc }}${{ matrix.ubsan && '\ -fsanitize=undefined\ -fno-sanitize-recover=all' || '' }}
|
|
67
|
+
warnflags=-Wall\ -Wextra\ -Wno-unused-parameter\ -Werror
|
|
59
68
|
run: bundle exec rake compile
|
|
60
69
|
|
|
70
|
+
- name: Install Intel SDE if the CPU lacks AVX512
|
|
71
|
+
if: runner.arch == 'X64'
|
|
72
|
+
run: .github/scripts/install-sde
|
|
73
|
+
|
|
61
74
|
- name: Show codecs
|
|
62
|
-
run: bundle exec ruby -Ilib -rob64 -e 'p Ob64.codecs, Ob64.codec'
|
|
75
|
+
run: $SDE bundle exec ruby -Ilib -rob64 -e 'p Ob64.codecs, Ob64.codec'
|
|
76
|
+
|
|
77
|
+
- name: Test
|
|
78
|
+
env:
|
|
79
|
+
# Fails the codec spec if one is missing
|
|
80
|
+
OB64_CODECS: ${{ runner.arch == 'X64' && 'scalar,avx2,avx512' || 'scalar,neon64' }}
|
|
81
|
+
run: $SDE bundle exec rake spec
|
|
82
|
+
|
|
83
|
+
codecs:
|
|
84
|
+
# The C codecs alone under ASan and UBSan, on exact-size buffers.
|
|
85
|
+
strategy:
|
|
86
|
+
fail-fast: false
|
|
87
|
+
matrix:
|
|
88
|
+
os: [ubuntu-24.04, ubuntu-24.04-arm]
|
|
89
|
+
cc: [gcc, clang]
|
|
90
|
+
runs-on: ${{ matrix.os }}
|
|
91
|
+
# Long under SDE, which emulates AVX512 slowly
|
|
92
|
+
timeout-minutes: 90
|
|
93
|
+
steps:
|
|
94
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
95
|
+
with:
|
|
96
|
+
persist-credentials: false
|
|
97
|
+
|
|
98
|
+
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
|
|
99
|
+
with:
|
|
100
|
+
ruby-version: "3.4"
|
|
101
|
+
bundler-cache: true
|
|
102
|
+
|
|
103
|
+
- name: Install Clang
|
|
104
|
+
if: matrix.cc == 'clang'
|
|
105
|
+
run: command -v clang || { sudo apt-get update && sudo apt-get install -y clang; }
|
|
106
|
+
|
|
107
|
+
- name: Install Intel SDE if the CPU lacks AVX512
|
|
108
|
+
if: runner.arch == 'X64'
|
|
109
|
+
run: .github/scripts/install-sde
|
|
63
110
|
|
|
64
111
|
- name: Test
|
|
65
|
-
|
|
112
|
+
env:
|
|
113
|
+
CC: ${{ matrix.cc }}
|
|
114
|
+
OB64_CODECS: ${{ runner.arch == 'X64' && 'scalar,avx2,avx512' || 'scalar,neon64' }}
|
|
115
|
+
run: bundle exec rake spec:c
|
|
66
116
|
|
|
67
117
|
package:
|
|
68
118
|
runs-on: ubuntu-24.04
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
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
|
+
|
|
5
|
+
## [0.8.0] - 2026-10-27
|
|
6
|
+
|
|
7
|
+
- Fix undefined behavior in AVX512 detection.
|
|
8
|
+
- `Ob64::Base64::RFC4648.encode` and `encode_urlsafe` return US-ASCII Strings, like `pack("m0")` (were BINARY).
|
|
9
|
+
|
|
1
10
|
## [0.7.0] - 2026-10-26
|
|
2
11
|
|
|
3
12
|
- **Breaking:** Split `Ob64::Base64` into one module per RFC. `Ob64::Base64::RFC4648` has `encode`, `decode`, `encode_urlsafe`, `decode_urlsafe`, `encoded_length_of` and `decoded_length_of` (were `Ob64::Base64.encode`, `decode`, `urlsafe_encode`, `urlsafe_decode`, `encoded_length_of` and `decoded_length_of`).
|
data/README.md
CHANGED
|
@@ -2,13 +2,56 @@
|
|
|
2
2
|
|
|
3
3
|
[](http://rubygems.org/gems/ob64)
|
|
4
4
|
[](https://github.com/jcmfernandes/ob64/actions/workflows/main.yml)
|
|
5
|
-
[](http://www.rubydoc.info/gems/ob64/0.
|
|
5
|
+
[](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
|
|
|
9
|
-
How fast?
|
|
9
|
+
How fast? Times faster than Ruby's own `pack` / `unpack1`, per call, measured with `benchmark.rb` on an AMD Ryzen 9 9900X (AVX512) with Ruby 3.4:
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
```mermaid
|
|
12
|
+
%%{init: {"themeVariables": {"xyChart": {"plotColorPalette": "#2a78d6"}}}}%%
|
|
13
|
+
xychart-beta
|
|
14
|
+
title "1 kB input: times faster than Ruby"
|
|
15
|
+
x-axis ["m0 enc", "m0 dec", "url enc", "url dec", "m enc", "m dec", "u enc", "u dec"]
|
|
16
|
+
y-axis "times faster" 0 --> 45
|
|
17
|
+
bar [3.1, 4.8, 11.1, 12.6, 5.2, 3.9, 5.9, 4.2]
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```mermaid
|
|
21
|
+
%%{init: {"themeVariables": {"xyChart": {"plotColorPalette": "#2a78d6"}}}}%%
|
|
22
|
+
xychart-beta
|
|
23
|
+
title "4 kB input: times faster than Ruby"
|
|
24
|
+
x-axis ["m0 enc", "m0 dec", "url enc", "url dec", "m enc", "m dec", "u enc", "u dec"]
|
|
25
|
+
y-axis "times faster" 0 --> 45
|
|
26
|
+
bar [6.3, 8.4, 17.6, 22.9, 8.4, 6.1, 7.5, 5.2]
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```mermaid
|
|
30
|
+
%%{init: {"themeVariables": {"xyChart": {"plotColorPalette": "#2a78d6"}}}}%%
|
|
31
|
+
xychart-beta
|
|
32
|
+
title "16 kB input: times faster than Ruby"
|
|
33
|
+
x-axis ["m0 enc", "m0 dec", "url enc", "url dec", "m enc", "m dec", "u enc", "u dec"]
|
|
34
|
+
y-axis "times faster" 0 --> 45
|
|
35
|
+
bar [11.1, 12.6, 24.3, 32.8, 6.0, 7.0, 7.2, 5.3]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```mermaid
|
|
39
|
+
%%{init: {"themeVariables": {"xyChart": {"plotColorPalette": "#2a78d6"}}}}%%
|
|
40
|
+
xychart-beta
|
|
41
|
+
title "512 kB input: times faster than Ruby"
|
|
42
|
+
x-axis ["m0 enc", "m0 dec", "url enc", "url dec", "m enc", "m dec", "u enc", "u dec"]
|
|
43
|
+
y-axis "times faster" 0 --> 45
|
|
44
|
+
bar [14.0, 13.8, 39.2, 42.0, 7.7, 7.6, 7.3, 5.3]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- `m0`: RFC 4648 (`Ob64::Base64::RFC4648.encode` / `decode`)
|
|
48
|
+
- `url`: RFC 4648 URL-safe (`encode_urlsafe` / `decode_urlsafe`), against Ruby's `pack("m0")` plus `tr`
|
|
49
|
+
- `m`: RFC 2045 (`Ob64::Base64::RFC2045.encode` / `decode`)
|
|
50
|
+
- `u`: uuencode (`Ob64::UU.encode` / `decode`)
|
|
51
|
+
|
|
52
|
+
Try it yourself:
|
|
53
|
+
|
|
54
|
+
$ bin/rake compile benchmark
|
|
12
55
|
|
|
13
56
|
## Acknowledgements
|
|
14
57
|
|
|
@@ -80,6 +123,28 @@ Ob64::UU.decode(string)
|
|
|
80
123
|
# "This is data!"
|
|
81
124
|
```
|
|
82
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
|
+
|
|
83
148
|
### Codecs
|
|
84
149
|
|
|
85
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).
|
|
@@ -115,7 +180,7 @@ See [{file:CHANGELOG.md}](CHANGELOG.md).
|
|
|
115
180
|
|
|
116
181
|
## Development
|
|
117
182
|
|
|
118
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
183
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests, and `rake spec:c` to check the C codecs for memory errors. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
119
184
|
|
|
120
185
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
121
186
|
|
|
@@ -123,10 +188,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
123
188
|
|
|
124
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).
|
|
125
190
|
|
|
126
|
-
## TODO
|
|
127
|
-
|
|
128
|
-
* Streaming interface
|
|
129
|
-
|
|
130
191
|
## License
|
|
131
192
|
|
|
132
193
|
Released under the MIT License. See [{file:LICENSE}](LICENSE).
|
data/Rakefile
CHANGED
|
@@ -14,6 +14,17 @@ Rake::ExtensionTask.new("ob64_ext") do |ext|
|
|
|
14
14
|
ext.ext_dir = "ext/ob64"
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
+
desc "Check the SIMD codecs against scalar in C, under ASan and UBSan; SEED=n repeats a run"
|
|
18
|
+
task "spec:c" do
|
|
19
|
+
mkdir_p "tmp"
|
|
20
|
+
sh "#{ENV.fetch("CC", "cc")} -std=gnu99 -O3 -g -fsanitize=address,undefined -fno-sanitize-recover=all " \
|
|
21
|
+
"-fno-omit-frame-pointer -Wall -Wextra -Wno-unused-parameter -Werror -Iext/ob64 -o tmp/codecs_test " \
|
|
22
|
+
"spec/c/codecs_test.c ext/ob64/pack_base64.c ext/ob64/pack_uuencode.c"
|
|
23
|
+
# SDE: an emulator prefix, see .github/scripts/install-sde. It must run the test
|
|
24
|
+
# itself: under SDE, ASan programs started by another one exit 0 unrun.
|
|
25
|
+
sh "#{ENV["SDE"]} tmp/codecs_test #{ENV["SEED"]}"
|
|
26
|
+
end
|
|
27
|
+
|
|
17
28
|
desc "Run benchmark"
|
|
18
29
|
task :benchmark do
|
|
19
30
|
load "#{__dir__}/benchmark.rb"
|
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,5 +1,7 @@
|
|
|
1
|
+
#include <limits.h>
|
|
1
2
|
#include <stdbool.h>
|
|
2
3
|
#include "ob64.h"
|
|
4
|
+
#include "ruby/encoding.h"
|
|
3
5
|
|
|
4
6
|
VALUE mOb64 = Qnil;
|
|
5
7
|
VALUE mOb64_Ext = Qnil;
|
|
@@ -9,7 +11,12 @@ static const char *const codec_names[PACK_CODECS] = { "scalar", "avx2", "avx512"
|
|
|
9
11
|
static ID codec_ids[PACK_CODECS];
|
|
10
12
|
|
|
11
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);
|
|
12
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);
|
|
13
20
|
VALUE Ob64_encoded_length_of(VALUE self, VALUE bin, VALUE withPadding);
|
|
14
21
|
VALUE Ob64_decoded_length_of(VALUE self, VALUE string);
|
|
15
22
|
VALUE Ob64_codec(VALUE self);
|
|
@@ -32,7 +39,12 @@ Init_ob64_ext(void)
|
|
|
32
39
|
mOb64_Ext = rb_define_module_under(mOb64, "Ext");
|
|
33
40
|
|
|
34
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);
|
|
35
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);
|
|
36
48
|
rb_define_private_method(mOb64_Ext, "__encoded_length_of", Ob64_encoded_length_of, 2);
|
|
37
49
|
rb_define_private_method(mOb64_Ext, "__decoded_length_of", Ob64_decoded_length_of, 1);
|
|
38
50
|
rb_define_private_method(mOb64_Ext, "__codec", Ob64_codec, 0);
|
|
@@ -42,38 +54,175 @@ Init_ob64_ext(void)
|
|
|
42
54
|
Init_ob64_pack(mOb64_Ext);
|
|
43
55
|
}
|
|
44
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
|
+
|
|
45
106
|
VALUE Ob64_encode_string(VALUE self, VALUE bin, VALUE urlsafe, VALUE padding)
|
|
46
107
|
{
|
|
47
108
|
StringValue(bin);
|
|
48
|
-
|
|
49
|
-
VALUE result = rb_str_buf_new(__encoded_length_of(bin_len, true));
|
|
50
|
-
int flags = RTEST(urlsafe) ? PACK_BASE64_URL_SAFE : 0;
|
|
109
|
+
VALUE result = rb_str_buf_new(0);
|
|
51
110
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
111
|
+
ob64_encode_append(result, bin, 0, RSTRING_LEN(bin), urlsafe, padding);
|
|
112
|
+
/* US-ASCII, as pack("m0") */
|
|
113
|
+
ENCODING_CODERANGE_SET(result, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
|
|
55
114
|
|
|
56
115
|
return result;
|
|
57
116
|
}
|
|
58
117
|
|
|
59
|
-
VALUE
|
|
118
|
+
VALUE Ob64_encode_into(VALUE self, VALUE dst, VALUE src, VALUE off, VALUE len, VALUE urlsafe, VALUE padding)
|
|
60
119
|
{
|
|
61
|
-
StringValue(
|
|
62
|
-
|
|
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);
|
|
63
135
|
/* Room for unpadded input */
|
|
64
|
-
|
|
136
|
+
char *out = ob64_reserve(dst, len / 4 * 3 + (len % 4 ? 3 : 0));
|
|
65
137
|
int flags = (RTEST(urlsafe) ? PACK_BASE64_URL_SAFE : 0) | (RTEST(loose) ? PACK_BASE64_LOOSE : 0);
|
|
66
138
|
size_t nout;
|
|
67
139
|
|
|
68
|
-
if (!pack_base64_decode(RSTRING_PTR(
|
|
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);
|
|
69
143
|
rb_raise(rb_eArgError, "invalid base64");
|
|
70
144
|
}
|
|
71
|
-
rb_str_set_len(
|
|
72
|
-
RB_GC_GUARD(
|
|
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);
|
|
73
155
|
|
|
74
156
|
return result;
|
|
75
157
|
}
|
|
76
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
|
+
|
|
77
226
|
VALUE Ob64_encoded_length_of(VALUE self, VALUE bin, VALUE withPadding)
|
|
78
227
|
{
|
|
79
228
|
StringValue(bin);
|
|
@@ -87,6 +236,7 @@ VALUE Ob64_decoded_length_of(VALUE self, VALUE string)
|
|
|
87
236
|
|
|
88
237
|
size_t __encoded_length_of(size_t bin_len, bool withPadding)
|
|
89
238
|
{
|
|
239
|
+
ob64_check_encode_len(bin_len);
|
|
90
240
|
if (withPadding) {
|
|
91
241
|
return (bin_len + 2) / 3 * 4;
|
|
92
242
|
} else {
|