File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,30 @@ def read_compressed_int(data) -> Optional[Tuple[int, int]]:
57
57
return None
58
58
59
59
60
+ def compress_int (i : int ) -> Optional [bytes ]:
61
+ """
62
+ Given integer, return bytes representing a compressed integer per
63
+ spec ECMA-335 II.23.2 Blobs and signatures.
64
+ """
65
+
66
+ if not isinstance (i , int ):
67
+ raise TypeError (f"expected int, given { type (i )} " )
68
+ if i < 0 :
69
+ raise ValueError (f"expected positive int, given { i } " )
70
+
71
+ if i <= 0x7f :
72
+ return int .to_bytes (i , length = 1 , byteorder = "big" )
73
+ elif i <= 0x3fff :
74
+ b = int .to_bytes (i , length = 2 , byteorder = "big" )
75
+ return bytes ((0x80 | b [0 ], b [1 ]))
76
+ elif i <= 0x1fffffff :
77
+ b = int .to_bytes (i , length = 4 , byteorder = "big" )
78
+ return bytes ((0x80 | 0x40 | b [0 ], b [1 ], b [2 ], b [3 ]))
79
+ else :
80
+ logger .warning (f"invalid int { i } , max value 0x1fffffff" )
81
+ return None
82
+
83
+
60
84
def two_way_dict (pairs ):
61
85
return dict ([(e [1 ], e [0 ]) for e in pairs ] + pairs )
62
86
You can’t perform that action at this time.
0 commit comments