| 297 | _A85END = b"~>" |
| 298 | |
| 299 | def _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False): |
| 300 | # Helper function for a85encode and b85encode |
| 301 | if not isinstance(b, bytes_types): |
| 302 | b = memoryview(b).tobytes() |
| 303 | |
| 304 | padding = (-len(b)) % 4 |
| 305 | if padding: |
| 306 | b = b + b'\0' * padding |
| 307 | words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b) |
| 308 | |
| 309 | chunks = [b'z' if foldnuls and not word else |
| 310 | b'y' if foldspaces and word == 0x20202020 else |
| 311 | (chars2[word // 614125] + |
| 312 | chars2[word // 85 % 7225] + |
| 313 | chars[word % 85]) |
| 314 | for word in words] |
| 315 | |
| 316 | if padding and not pad: |
| 317 | if chunks[-1] == b'z': |
| 318 | chunks[-1] = chars[0] * 5 |
| 319 | chunks[-1] = chunks[-1][:-padding] |
| 320 | |
| 321 | return b''.join(chunks) |
| 322 | |
| 323 | def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False): |
| 324 | """Encode bytes-like object b using Ascii85 and return a bytes object. |