plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
0177fbb
— Peter John
2026-11-10T22:01:10+05:30
Base64: data after padding and a lone last character are errors
- docs/std.md +1 -1
- std/encoding/Base64.plum +14 -10
- tests/std/base64.plum +34 -0
docs/std.md
CHANGED
|
@@ -241,7 +241,7 @@ fun main(io: Io) =
|
|
|
241
241
|
- **`Time`:** `Time.now(io)` and `Duration` values such as `Duration.seconds(5)`
|
|
242
242
|
- **`Random`:** `Random.seeded(n)`. Each call returns the value and the next generator, so the same seed always gives the same sequence. `io.randomSeed()` gives a seed from the clock
|
|
243
243
|
- **`Uuid`:** `Uuid.v4(rng)`, `Uuid.fromStr`, `Uuid.isValid`
|
|
244
|
-
- **`Base64`** (`import std/encoding/Base64`): `Base64.encode(data)` and `Base64.decode(text)` (a `Result`; whitespace and missing padding are fine), the URL alphabet `Base64.encodeUrl(data)` (no padding unless `pad` is true) and `Base64.decodeUrl(text)`, and the line-wrapped `Base64.encodePem` and `Base64.encodeMime`
|
|
244
|
+
- **`Base64`** (`import std/encoding/Base64`): `Base64.encode(data)` and `Base64.decode(text)` (a `Result`; whitespace and missing padding are fine, data after padding and a last group of one character are `Err(InvalidBase64)`), the URL alphabet `Base64.encodeUrl(data)` (no padding unless `pad` is true) and `Base64.decodeUrl(text)`, and the line-wrapped `Base64.encodePem` and `Base64.encodeMime`
|
|
245
245
|
- **`Aabb`:** an axis-aligned box, corners included: `Aabb.of(x, y, w, h)`, `width`, `height`, `center`, `overlaps`, `contains`, `containsPoint`, `union`, `distSq`, `rayEntry`
|
|
246
246
|
- **Reference identity:** the built-in `@sameRef(a, b)` is true when `a` and `b` are the very same object, not just equal ones (see [Built-ins](language.md#built-ins)); there is nothing to import. A persistent structure uses it to keep what didn't change: the scene tree's `input` and `Game`'s tick return a node as it was when its actor came back as the same object (see [the game engine](game.md))
|
|
247
247
|
- **Linear memory:** the built-ins `@memSize`, `@memGrow`, `@memStoreI32`, `@memLoadI32`, `@memStoreU8`, `@memLoadU8`, `@memStoreF32` and `@memLoadF32` read and write the program's wasm memory (see [Built-ins](language.md#linear-memory))
|
std/encoding/Base64.plum
CHANGED
|
@@ -114,36 +114,40 @@ primitive Base64 =
|
|
|
114
114
|
|
|
115
115
|
# Configurable decoding. The defaults are for RFC 4648. Missing padding is
|
|
116
116
|
# not an error. Non-base64 data, other than whitespace (which can appear at
|
|
117
|
-
# any point), fails with `Err(InvalidBase64)`
|
|
117
|
+
# any point), fails with `Err(InvalidBase64)`, and so do data after padding
|
|
118
|
+
# (`Zg==Zg==`) and a last group of one character (`Zm9vY`: 6 bits are no byte).
|
|
118
119
|
fun decode(data: Str, at62: Str = "+", at63: Str = "/", pad: Str = "=") -> Result<Str, Base64Error> =
|
|
119
120
|
at62b := at62.byteAt(0)
|
|
120
121
|
at63b := at63.byteAt(0)
|
|
121
122
|
hasPad := pad.length() > 0
|
|
122
123
|
padb := if hasPad then pad.byteAt(0) else -1
|
|
123
124
|
res := Buffer()
|
|
124
|
-
decodeFrom(data, res, 0, 0, 0, at62b, at63b, padb)
|
|
125
|
+
decodeFrom(data, res, 0, 0, 0, false, at62b, at63b, padb)
|
|
125
126
|
|
|
126
127
|
# `decode`'s loop: the bytes of `data` from `i` on, with `bits` / `nbits`
|
|
127
|
-
# the pending bits not yet written out as a whole byte
|
|
128
|
+
# the pending bits not yet written out as a whole byte, and `padded` true once a
|
|
129
|
+
# padding character was read.
|
|
128
|
-
fun decodeFrom(data: Str, res: Buffer, i: Int, bits: Int, nbits: Int, at62b: Int, at63b: Int, padb: Int) -> Result<Str, Base64Error> =
|
|
130
|
+
fun decodeFrom(data: Str, res: Buffer, i: Int, bits: Int, nbits: Int, padded: Bool, at62b: Int, at63b: Int, padb: Int) -> Result<Str, Base64Error> =
|
|
129
131
|
if i >= data.length()
|
|
130
|
-
Ok(res.toStr())
|
|
132
|
+
if nbits == 6 then Err(InvalidBase64) else Ok(res.toStr())
|
|
131
133
|
else
|
|
132
134
|
b := data.byteAt(i)
|
|
133
|
-
if isBase64Whitespace(b)
|
|
135
|
+
if isBase64Whitespace(b)
|
|
136
|
+
decodeFrom(data, res, i + 1, bits, nbits, padded, at62b, at63b, padb)
|
|
137
|
+
else if b == padb
|
|
134
|
-
decodeFrom(data, res, i + 1, bits, nbits, at62b, at63b, padb)
|
|
138
|
+
decodeFrom(data, res, i + 1, bits, nbits, true, at62b, at63b, padb)
|
|
135
139
|
else
|
|
136
140
|
v := decodeByte(b, at62b, at63b)
|
|
137
|
-
if v < 0
|
|
141
|
+
if v < 0 || padded
|
|
138
142
|
Err(InvalidBase64)
|
|
139
143
|
else
|
|
140
144
|
acc := {bits << 6} | v
|
|
141
145
|
if nbits + 6 >= 8
|
|
142
146
|
left := nbits + 6 - 8
|
|
143
147
|
res.writeByte(Byte({acc >> left} & 0xFF))
|
|
144
|
-
decodeFrom(data, res, i + 1, acc, left, at62b, at63b, padb)
|
|
148
|
+
decodeFrom(data, res, i + 1, acc, left, false, at62b, at63b, padb)
|
|
145
149
|
else
|
|
146
|
-
decodeFrom(data, res, i + 1, acc, nbits + 6, at62b, at63b, padb)
|
|
150
|
+
decodeFrom(data, res, i + 1, acc, nbits + 6, false, at62b, at63b, padb)
|
|
147
151
|
|
|
148
152
|
test "encode: RFC 4648 vectors, with and without padding"
|
|
149
153
|
assert Base64.encode("") == ""
|
tests/std/base64.plum
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import std/Int
|
|
2
|
+
import std/Str
|
|
3
|
+
import std/Buffer
|
|
4
|
+
import std/Byte
|
|
5
|
+
import std/Option
|
|
6
|
+
import std/Result
|
|
7
|
+
import std/encoding/Base64
|
|
8
|
+
|
|
9
|
+
# `n` pseudo-random bytes from the generator state `x`.
|
|
10
|
+
fun base64TestBytes(x: Int, n: Int) -> Str =
|
|
11
|
+
buf := Buffer()
|
|
12
|
+
_ := 0.until(n).fold(x, |s, i|
|
|
13
|
+
next := {s * 6364136223846793005 + 1442695040888963407}
|
|
14
|
+
buf.writeByte(Byte({next >> 33} & 0xFF))
|
|
15
|
+
next
|
|
16
|
+
)
|
|
17
|
+
buf.toStr()
|
|
18
|
+
|
|
19
|
+
test "every variant round-trips every length and byte, padding and line breaks included"
|
|
20
|
+
bad := 0.until(300).fold(0, |acc, n|
|
|
21
|
+
data := base64TestBytes(n * 7919 + 1, n % 100)
|
|
22
|
+
ok := Base64.decode(Base64.encode(data))! == data && Base64.decode(Base64.encode(data, "+", "/", ""))! == data && Base64.decodeUrl(Base64.encodeUrl(data))! == data && Base64.decodeUrl(Base64.encodeUrl(data, true))! == data && Base64.decode(Base64.encodePem(data))! == data && Base64.decode(Base64.encodeMime(data))! == data
|
|
23
|
+
if ok then acc else acc + 1
|
|
24
|
+
)
|
|
25
|
+
assert bad == 0
|
|
26
|
+
assert Base64.encode(base64TestBytes(3, 57)).length() == 76 && Base64.encodeMime(base64TestBytes(3, 58)).split("\r\n", 0).length() == 2
|
|
27
|
+
assert Base64.encodePem(base64TestBytes(3, 48)).length() == 64
|
|
28
|
+
|
|
29
|
+
test "invalid input: data after padding, a lone last character, other alphabets"
|
|
30
|
+
assert Base64.decode("Zg==Zg==").isErr() && Base64.decode("Zg=g").isErr() && Base64.decode("=Zg").isErr()
|
|
31
|
+
assert Base64.decode("Zm9vY").isErr() && Base64.decode("Z").isErr() && Base64.decode("Z===").isErr()
|
|
32
|
+
assert Base64.decode("Zm9vYg")! == "foob" && Base64.decode("Zm9vYmE")! == "fooba"
|
|
33
|
+
assert Base64.decode("Zm9v YmFy\n")! == "foobar" && Base64.decode("Zg= =")! == "f" && Base64.decode("")! == ""
|
|
34
|
+
assert Base64.decode("Zm9v_mFy").isErr() && Base64.decodeUrl("Zm9v/mFy").isErr() && Base64.decode("Zm9v\u0000").isErr()
|