forked from aboutcode-org/commoncode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.py
More file actions
60 lines (46 loc) · 1.54 KB
/
codec.py
File metadata and controls
60 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/commoncode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from base64 import standard_b64decode as stddecode
from base64 import urlsafe_b64encode as b64encode
"""
Numbers to bytes or strings and URLs coder/decoders.
"""
c2i = lambda c: c
i2c = lambda i: bytes([i])
def num_to_bin(num):
"""
Convert a `num` integer or long to a binary string byte-ordered such that
the least significant bytes are at the beginning of the string (aka. big
endian).
"""
# Zero is not encoded but returned as an empty value
if num == 0:
return b'\x00'
return num.to_bytes((num.bit_length() + 7) // 8, 'big')
def bin_to_num(binstr):
"""
Convert a big endian byte-ordered binary string to an integer or long.
"""
return int.from_bytes(binstr, byteorder='big', signed=False)
def urlsafe_b64encode(s):
"""
Encode a binary string to a url safe base64 encoding.
"""
return b64encode(s)
def urlsafe_b64decode(b64):
"""
Decode a url safe base64-encoded string.
Note that we use stddecode to work around a bug in the standard library.
"""
b = b64.replace(b'-', b'+').replace(b'_', b'/')
return stddecode(b)
def urlsafe_b64encode_int(num):
"""
Encode a number (int or long) in url safe base64.
"""
return b64encode(num_to_bin(num))