feat: bootstrap moon-i2p core protocol foundation
This commit is contained in:
commit
e527b84bc3
47 files changed
+5350
No files matched your search
@@ -0,0 +1,110 @@
|
||||
///|
|
||||
/// I2P custom Base64 alphabet: `A-Z`, `a-z`, `0-9`, `-`, `~` with `=` padding.
|
||||
let i2p_b64_alphabet : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~"
|
||||
|
||||
///|
|
||||
fn b64_decode_char(c : Int32) -> Int32 raise EncodingError {
|
||||
if c >= 65 && c <= 90 {
|
||||
c - 65
|
||||
} else if c >= 97 && c <= 122 {
|
||||
c - 97 + 26
|
||||
} else if c >= 48 && c <= 57 {
|
||||
c - 48 + 52
|
||||
} else if c == 45 {
|
||||
// '-'
|
||||
62
|
||||
} else if c == 126 {
|
||||
// '~'
|
||||
63
|
||||
} else {
|
||||
raise InvalidCharacter("invalid I2P Base64 character code: \{c}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Encodes bytes using the I2P Base64 alphabet (`-` and `~`, padded with `=`).
|
||||
pub fn base64_encode(data : Bytes) -> String {
|
||||
let len = data.length()
|
||||
if len == 0 {
|
||||
return ""
|
||||
}
|
||||
let buf = StringBuilder(capacity=(len + 2) / 3 * 4)
|
||||
let mut i = 0
|
||||
while i < len {
|
||||
let b0 = data[i].to_int()
|
||||
let has_b1 = i + 1 < len
|
||||
let has_b2 = i + 2 < len
|
||||
let b1 = if has_b1 { data[i + 1].to_int() } else { 0 }
|
||||
let b2 = if has_b2 { data[i + 2].to_int() } else { 0 }
|
||||
|
||||
let c0 = b0 >> 2
|
||||
let c1 = ((b0 & 0x03) << 4) | (b1 >> 4)
|
||||
let c2 = ((b1 & 0x0F) << 2) | (b2 >> 6)
|
||||
let c3 = b2 & 0x3F
|
||||
|
||||
buf.write_char(i2p_b64_alphabet[c0].to_int().unsafe_to_char())
|
||||
buf.write_char(i2p_b64_alphabet[c1].to_int().unsafe_to_char())
|
||||
if has_b1 {
|
||||
buf.write_char(i2p_b64_alphabet[c2].to_int().unsafe_to_char())
|
||||
} else {
|
||||
buf.write_char('=')
|
||||
}
|
||||
if has_b2 {
|
||||
buf.write_char(i2p_b64_alphabet[c3].to_int().unsafe_to_char())
|
||||
} else {
|
||||
buf.write_char('=')
|
||||
}
|
||||
i += 3
|
||||
}
|
||||
buf.to_string()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Decodes an I2P Base64 string (`-` and `~`, optional `=` padding) into `Bytes`.
|
||||
pub fn base64_decode(input : String) -> Bytes raise EncodingError {
|
||||
let chars : Array[Int32] = []
|
||||
for ch in input {
|
||||
let c = ch.to_int()
|
||||
if c == 32 || c == 9 || c == 10 || c == 13 {
|
||||
continue
|
||||
}
|
||||
chars.push(c)
|
||||
}
|
||||
let len = chars.length()
|
||||
if len == 0 {
|
||||
return b""
|
||||
}
|
||||
if len % 4 != 0 {
|
||||
raise InvalidLength("I2P Base64 length must be a multiple of 4, got \{len}")
|
||||
}
|
||||
let out : Array[Byte] = Array(capacity=len / 4 * 3)
|
||||
let mut i = 0
|
||||
while i < len {
|
||||
let c0 = chars[i]
|
||||
let c1 = chars[i + 1]
|
||||
let c2 = chars[i + 2]
|
||||
let c3 = chars[i + 3]
|
||||
let v0 = b64_decode_char(c0)
|
||||
let v1 = b64_decode_char(c1)
|
||||
let pad2 = c2 == 61 // '='
|
||||
let pad3 = c3 == 61 // '='
|
||||
if pad2 && !pad3 {
|
||||
raise InvalidCharacter("invalid Base64 padding")
|
||||
}
|
||||
if (pad2 || pad3) && i + 4 < len {
|
||||
raise InvalidCharacter("Base64 padding before end of input")
|
||||
}
|
||||
let v2 = if pad2 { 0 } else { b64_decode_char(c2) }
|
||||
let v3 = if pad3 { 0 } else { b64_decode_char(c3) }
|
||||
|
||||
out.push(((v0 << 2) | (v1 >> 4)).to_byte())
|
||||
if !pad2 {
|
||||
out.push((((v1 & 0x0F) << 4) | (v2 >> 2)).to_byte())
|
||||
}
|
||||
if !pad3 {
|
||||
out.push((((v2 & 0x03) << 6) | v3).to_byte())
|
||||
}
|
||||
i += 4
|
||||
}
|
||||
Bytes::from_array(out)
|
||||
}
|
||||
Reference in new issue
Block a user