-
Notifications
You must be signed in to change notification settings - Fork 1
/
binascii.repy
161 lines (126 loc) · 5.01 KB
/
binascii.repy
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
<Program Name>
binascii.repy
<Started>
Jun 29, 2009
<Purpose>
The binascii module contains a number of methods to convert between binary
and various ASCII-encoded binary representations. Currently it only
provides binascii_a2b_hex and binascii_b2a_hex but others may be added
in the future as needed.
<Authors>
binascii_a2b_hex and binascii_b2a_hex:
Modified by Anthony
Origionally written by: Maciek Fijalkowski from
http://codespeak.net/pypy/dist/pypy/doc/
pypy source is released under the
The MIT License
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
def binascii_a2b_hex(hexstr):
"""
<Purpose>
Return the binary data represented by the hexadecimal string hexstr.
This function is the inverse of b2a_hex(). hexstr must contain an
even number of hexadecimal digits (which can be upper or lower
case), otherwise a TypeError is raised.
<Arguments>
hexstr: A string containing a even number of hexadecimal digits
(which can be upper or lower case).
<Exception>
TypeError if the number of hexadecimal digits is not even.
<Return>
Return the binary data represented by the hexadecimal string hexstr.
This function is the inverse of b2a_hex().
<Porting Note>
Anthony - I had to remove the yield statement for repy and in the
process I had to do a major rewrite of the function. This
is the origional.
def pairs_gen(s):
while s:
try:
yield table_hex[ord(s[0])], table_hex[ord(s[1])]
except IndexError:
if len(s):
raise TypeError('Odd-length string')
return
s = s[2:]
for a, b in pairs_gen(t):
if a < 0 or b < 0:
raise TypeError('Non-hexadecimal digit found')
result.append(chr((a << 4) + b))
return ''.join(result)
"""
table_hex = [
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1]
result = []
# Cannot be of odd length
if(len(hexstr) % 2 != 0):
raise TypeError('Odd-length string')
# Since two hexidecimal digits will be converted into a character
# hex characters will be read in groups of two.
# A simple walk-through of this code with a trivial example
# 'ff' ->
# left_hex = 15, right_hex = 15 ->
# result.append(chr(255))
for i in range(0,len(hexstr), 2):
left_hex, right_hex = table_hex[ord(hexstr[i])], table_hex[ord(hexstr[i+1])]
if left_hex < 0 or right_hex < 0:
raise TypeError('Non-hexadecimal digit found')
result.append(chr((left_hex << 4) + right_hex))
return ''.join(result)
def binascii_b2a_hex(binary_data):
"""
<Purpose>
Return the hexadecimal representation of the binary data. Every byte of
data is converted into the corresponding 2-digit hex representation. The
resulting string is therefore twice as long as the length of data.
<Arguments>
binary_data:
binary data to be converted.
<Exception>
None
<Side Effects>
None
<Return>
The hexadecimal representation of the binary data.
"""
result = []
for char in binary_data:
# Read four bits from the left of the octet
four_bits = (ord(char) >> 4) & 0xf
if four_bits > 9:
four_bits = four_bits + ord('a') - 10
else:
four_bits = four_bits + ord('0')
result.append(chr(four_bits))
# Read four bits from the right side of the octet
four_bits = ord(char) & 0xf
if four_bits > 9:
four_bits = four_bits + ord('a') - 10
else:
four_bits = four_bits + ord('0')
result.append(chr(four_bits))
return ''.join(result)