KeiSeiKit-1.0/_primitives/_rust/kei-provision/src/b64.rs
Parfii-bot a4e667de10 KeiSeiKit-public — clean state
Single-commit clean baseline after security scrub of niche-tells,
project codenames, internal jargon, and contributor-email leaks.

Contents:
- 100 Rust crates (_primitives/_rust/)
- 37 agent manifests (_manifests/) + generated specs (_generated/)
- 67 user-invocable skills (skills/)
- 33 hooks (hooks/)
- Composition blocks (_blocks/)
- Documentation (docs/, README.md)
- TS adapter packages (_ts_packages/)
- Assembler (_assembler/)
- Roles (_roles/)
- Templates (_templates/)
- Forgejo CI (.forgejo/)

Author: Denis Parfionovich <info@greendragon.info>

License: see LICENSE.
2026-05-01 12:09:03 +08:00

49 lines
1.3 KiB
Rust

//! Minimal base64 encoder. Vultr `--userdata` takes base64. We don't pull
//! in the `base64` crate for a single call site.
const TABLE: &[u8; 64] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pub fn encode(raw: &[u8]) -> String {
let mut out = String::with_capacity((raw.len() + 2) / 3 * 4);
for chunk in raw.chunks(3) {
let b0 = chunk[0];
let b1 = chunk.get(1).copied().unwrap_or(0);
let b2 = chunk.get(2).copied().unwrap_or(0);
out.push(TABLE[(b0 >> 2) as usize] as char);
out.push(TABLE[(((b0 & 0x03) << 4) | (b1 >> 4)) as usize] as char);
out.push(if chunk.len() > 1 {
TABLE[(((b1 & 0x0f) << 2) | (b2 >> 6)) as usize] as char
} else {
'='
});
out.push(if chunk.len() > 2 {
TABLE[(b2 & 0x3f) as usize] as char
} else {
'='
});
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty() {
assert_eq!(encode(b""), "");
}
#[test]
fn padding() {
assert_eq!(encode(b"a"), "YQ==");
assert_eq!(encode(b"ab"), "YWI=");
assert_eq!(encode(b"abc"), "YWJj");
}
#[test]
fn hello() {
assert_eq!(encode(b"Hello, World!"), "SGVsbG8sIFdvcmxkIQ==");
}
}