FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
commoncode/src/commoncode/codec.py at main · aboutcode-org/commoncode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
aboutcode-org
/
commoncode
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
22
Star
4
Code
Issues
13
Pull requests
7
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
commoncode
/
src
/
commoncode
/
codec.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
65 lines (48 loc) · 1.57 KB
Breadcrumbs
commoncode
/
src
/
commoncode
/
codec.py
Copy path
File metadata and controls
65 lines (48 loc) · 1.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
61
62
63
64
65
#
# 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/aboutcode-org/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.
"""
def
c2i
(
c
):
return
c
def
i2c
(
i
):
return
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
))
Back
|
FazBrowse Home
|
New Git URL