MCPcopy Index your code
hub / github.com/RustPython/RustPython / _b32encode

Function _b32encode

Lib/base64.py:163–196  ·  view source on GitHub ↗
(alphabet, s)

Source from the content-addressed store, hash-verified

161_b32rev = {}
162
163def _b32encode(alphabet, s):
164 # Delay the initialization of the table to not waste memory
165 # if the function is never called
166 if alphabet not in _b32tab2:
167 b32tab = [bytes((i,)) for i in alphabet]
168 _b32tab2[alphabet] = [a + b for a in b32tab for b in b32tab]
169 b32tab = None
170
171 if not isinstance(s, bytes_types):
172 s = memoryview(s).tobytes()
173 leftover = len(s) % 5
174 # Pad the last quantum with zero bits if necessary
175 if leftover:
176 s = s + b'\0' * (5 - leftover) # Don't use += !
177 encoded = bytearray()
178 from_bytes = int.from_bytes
179 b32tab2 = _b32tab2[alphabet]
180 for i in range(0, len(s), 5):
181 c = from_bytes(s[i: i + 5]) # big endian
182 encoded += (b32tab2[c >> 30] + # bits 1 - 10
183 b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
184 b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
185 b32tab2[c & 0x3ff] # bits 31 - 40
186 )
187 # Adjust for any leftover partial quanta
188 if leftover == 1:
189 encoded[-6:] = b'======'
190 elif leftover == 2:
191 encoded[-4:] = b'===='
192 elif leftover == 3:
193 encoded[-3:] = b'==='
194 elif leftover == 4:
195 encoded[-1:] = b'='
196 return bytes(encoded)
197
198def _b32decode(alphabet, s, casefold=False, map01=None):
199 # Delay the initialization of the table to not waste memory

Callers 2

b32encodeFunction · 0.85
b32hexencodeFunction · 0.85

Calls 3

isinstanceFunction · 0.85
lenFunction · 0.85
tobytesMethod · 0.45

Tested by

no test coverage detected