|
| 1 | +import LeanPython.Interpreter.Types |
| 2 | + |
| 3 | +set_option autoImplicit false |
| 4 | + |
| 5 | +namespace LeanPython.Stdlib.Base64 |
| 6 | + |
| 7 | +open LeanPython.Runtime |
| 8 | +open LeanPython.Interpreter |
| 9 | + |
| 10 | +-- ============================================================ |
| 11 | +-- Base64 encoding/decoding |
| 12 | +-- ============================================================ |
| 13 | + |
| 14 | +private def stdAlphabet : String := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 15 | +private def urlAlphabet : String := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" |
| 16 | + |
| 17 | +private def charToIndex (c : Char) (urlSafe : Bool) : Option Nat := |
| 18 | + let alpha := if urlSafe then urlAlphabet else stdAlphabet |
| 19 | + alpha.toList.findIdx? (· == c) |>.map id |
| 20 | + |
| 21 | +private partial def encodeBase64 (data : ByteArray) (urlSafe : Bool) : ByteArray := Id.run do |
| 22 | + let alpha := if urlSafe then urlAlphabet else stdAlphabet |
| 23 | + let alphaArr := alpha.toList.toArray |
| 24 | + let mut result := ByteArray.empty |
| 25 | + let mut i : Nat := 0 |
| 26 | + while i + 2 < data.size do |
| 27 | + let b0 := (data.get! i).toNat |
| 28 | + let b1 := (data.get! (i + 1)).toNat |
| 29 | + let b2 := (data.get! (i + 2)).toNat |
| 30 | + let n := b0 * 65536 + b1 * 256 + b2 |
| 31 | + result := result.push (alphaArr[n >>> 18 &&& 63]!).toUInt8 |
| 32 | + result := result.push (alphaArr[n >>> 12 &&& 63]!).toUInt8 |
| 33 | + result := result.push (alphaArr[n >>> 6 &&& 63]!).toUInt8 |
| 34 | + result := result.push (alphaArr[n &&& 63]!).toUInt8 |
| 35 | + i := i + 3 |
| 36 | + let remaining := data.size - i |
| 37 | + if remaining == 2 then |
| 38 | + let b0 := (data.get! i).toNat |
| 39 | + let b1 := (data.get! (i + 1)).toNat |
| 40 | + let n := b0 * 256 + b1 |
| 41 | + result := result.push (alphaArr[n >>> 10 &&& 63]!).toUInt8 |
| 42 | + result := result.push (alphaArr[n >>> 4 &&& 63]!).toUInt8 |
| 43 | + result := result.push (alphaArr[(n <<< 2) &&& 63]!).toUInt8 |
| 44 | + result := result.push '='.toUInt8 |
| 45 | + else if remaining == 1 then |
| 46 | + let b0 := (data.get! i).toNat |
| 47 | + result := result.push (alphaArr[b0 >>> 2]!).toUInt8 |
| 48 | + result := result.push (alphaArr[(b0 <<< 4) &&& 63]!).toUInt8 |
| 49 | + result := result.push '='.toUInt8 |
| 50 | + result := result.push '='.toUInt8 |
| 51 | + return result |
| 52 | + |
| 53 | +private partial def decodeBase64 (data : ByteArray) (urlSafe : Bool) : Except String ByteArray := do |
| 54 | + -- Strip padding and whitespace |
| 55 | + let chars := data.toList.filter (fun b => b.toNat != '='.toNat && b.toNat != '\n'.toNat && b.toNat != '\r'.toNat) |
| 56 | + let mut result := ByteArray.empty |
| 57 | + let mut buf : Nat := 0 |
| 58 | + let mut bits : Nat := 0 |
| 59 | + for b in chars do |
| 60 | + let c := Char.ofNat b.toNat |
| 61 | + match charToIndex c urlSafe with |
| 62 | + | some idx => |
| 63 | + buf := (buf <<< 6) ||| idx |
| 64 | + bits := bits + 6 |
| 65 | + if bits >= 8 then |
| 66 | + bits := bits - 8 |
| 67 | + result := result.push ((buf >>> bits) &&& 255).toUInt8 |
| 68 | + buf := buf &&& ((1 <<< bits) - 1) |
| 69 | + | none => .error s!"Invalid character in base64: '{c}'" |
| 70 | + return result |
| 71 | + |
| 72 | +-- ============================================================ |
| 73 | +-- Base16 (hex) encoding/decoding |
| 74 | +-- ============================================================ |
| 75 | + |
| 76 | +private def hexCharsUpper : String := "0123456789ABCDEF" |
| 77 | + |
| 78 | +private partial def encodeBase16 (data : ByteArray) : ByteArray := Id.run do |
| 79 | + let hexArr := hexCharsUpper.toList.toArray |
| 80 | + let mut result := ByteArray.empty |
| 81 | + for i in [:data.size] do |
| 82 | + let b := (data.get! i).toNat |
| 83 | + result := result.push (hexArr[b >>> 4]!).toUInt8 |
| 84 | + result := result.push (hexArr[b &&& 15]!).toUInt8 |
| 85 | + return result |
| 86 | + |
| 87 | +private def hexCharToNibble (c : Char) : Option Nat := |
| 88 | + if '0' ≤ c && c ≤ '9' then some (c.toNat - '0'.toNat) |
| 89 | + else if 'A' ≤ c && c ≤ 'F' then some (c.toNat - 'A'.toNat + 10) |
| 90 | + else if 'a' ≤ c && c ≤ 'f' then some (c.toNat - 'a'.toNat + 10) |
| 91 | + else none |
| 92 | + |
| 93 | +private partial def decodeBase16 (data : ByteArray) : Except String ByteArray := do |
| 94 | + if data.size % 2 != 0 then .error "Odd-length string" |
| 95 | + let mut result := ByteArray.empty |
| 96 | + let mut i : Nat := 0 |
| 97 | + while i + 1 < data.size do |
| 98 | + let c1 := Char.ofNat (data.get! i).toNat |
| 99 | + let c2 := Char.ofNat (data.get! (i + 1)).toNat |
| 100 | + match hexCharToNibble c1, hexCharToNibble c2 with |
| 101 | + | some h, some l => result := result.push (h * 16 + l).toUInt8 |
| 102 | + | _, _ => .error s!"Non-hexadecimal digit found" |
| 103 | + i := i + 2 |
| 104 | + return result |
| 105 | + |
| 106 | +-- ============================================================ |
| 107 | +-- Python-facing functions |
| 108 | +-- ============================================================ |
| 109 | + |
| 110 | +/-- Coerce args to ByteArray for base64 functions. -/ |
| 111 | +private def toByteArray (v : Value) : InterpM ByteArray := do |
| 112 | + match v with |
| 113 | + | .bytes b => return b |
| 114 | + | .str s => return s.toUTF8 |
| 115 | + | _ => throwTypeError "a bytes-like object is required" |
| 116 | + |
| 117 | +/-- Python base64.b64encode(s) -/ |
| 118 | +partial def b64encode (args : List Value) : InterpM Value := do |
| 119 | + match args with |
| 120 | + | [v] => do |
| 121 | + let data ← toByteArray v |
| 122 | + return .bytes (encodeBase64 data false) |
| 123 | + | _ => throwTypeError "b64encode() takes exactly 1 argument" |
| 124 | + |
| 125 | +/-- Python base64.b64decode(s) -/ |
| 126 | +partial def b64decode (args : List Value) : InterpM Value := do |
| 127 | + match args with |
| 128 | + | [v] => do |
| 129 | + let data ← toByteArray v |
| 130 | + match decodeBase64 data false with |
| 131 | + | .ok result => return .bytes result |
| 132 | + | .error e => throwValueError e |
| 133 | + | _ => throwTypeError "b64decode() takes exactly 1 argument" |
| 134 | + |
| 135 | +/-- Python base64.urlsafe_b64encode(s) -/ |
| 136 | +partial def urlsafeB64encode (args : List Value) : InterpM Value := do |
| 137 | + match args with |
| 138 | + | [v] => do |
| 139 | + let data ← toByteArray v |
| 140 | + return .bytes (encodeBase64 data true) |
| 141 | + | _ => throwTypeError "urlsafe_b64encode() takes exactly 1 argument" |
| 142 | + |
| 143 | +/-- Python base64.urlsafe_b64decode(s) -/ |
| 144 | +partial def urlsafeB64decode (args : List Value) : InterpM Value := do |
| 145 | + match args with |
| 146 | + | [v] => do |
| 147 | + let data ← toByteArray v |
| 148 | + match decodeBase64 data true with |
| 149 | + | .ok result => return .bytes result |
| 150 | + | .error e => throwValueError e |
| 151 | + | _ => throwTypeError "urlsafe_b64decode() takes exactly 1 argument" |
| 152 | + |
| 153 | +/-- Python base64.b16encode(s) -/ |
| 154 | +partial def b16encode (args : List Value) : InterpM Value := do |
| 155 | + match args with |
| 156 | + | [v] => do |
| 157 | + let data ← toByteArray v |
| 158 | + return .bytes (encodeBase16 data) |
| 159 | + | _ => throwTypeError "b16encode() takes exactly 1 argument" |
| 160 | + |
| 161 | +/-- Python base64.b16decode(s) -/ |
| 162 | +partial def b16decode (args : List Value) : InterpM Value := do |
| 163 | + match args with |
| 164 | + | [v] => do |
| 165 | + let data ← toByteArray v |
| 166 | + match decodeBase16 data with |
| 167 | + | .ok result => return .bytes result |
| 168 | + | .error e => throwValueError e |
| 169 | + | _ => throwTypeError "b16decode() takes exactly 1 argument" |
| 170 | + |
| 171 | +end LeanPython.Stdlib.Base64 |
0 commit comments