-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Protocol module #669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Protocol module #669
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2a5f30b
Add protocol.py
groutr 27cf63b
Use protocol module to decode ints.
groutr 54fcd32
Add packet checking functions.
groutr db275f3
Use protocol packet checking functions.
groutr fe72db9
Move read_string to protocol.py
groutr 61928a7
Move length coded integer to protocol.py
groutr 1dcdae7
Move reading byte strings to protocol.py
groutr dd6ad87
Raise an error if reading a null terminated string fails.
groutr 3be686e
Move reading length encoded strings to protocol.py
groutr 6ae4ce9
Remove old comment.
groutr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
from __future__ import print_function | ||
from ._compat import PY2 | ||
|
||
from struct import unpack_from | ||
|
||
from . import err | ||
|
||
DEBUG = False | ||
|
||
if PY2: | ||
def read_uint8(data, offset=0): | ||
"""Read 1 byte of data""" | ||
return ord(data[offset]) | ||
else: | ||
def read_uint8(data, offset=0): | ||
"""Read 1 byte of data""" | ||
return data[offset] | ||
|
||
|
||
def read_uint16(data, offset=0): | ||
"""Read 2 bytes of data beginning at offset""" | ||
return unpack_from('<H', data, offset=offset)[0] | ||
|
||
|
||
def read_uint24(data, offset=0): | ||
"""Read 3 bytes of data beginning at offset""" | ||
low, high = unpack_from('<HB', data, offset=offset) | ||
return low + (high << 16) | ||
|
||
|
||
def read_uint32(data, offset=0): | ||
"""Read 4 bytes of data beginning at offset""" | ||
return unpack_from('<I', data, offset=offset)[0] | ||
|
||
|
||
def read_uint64(data, offset=0): | ||
"""Read 8 bytes of data beginning at offset""" | ||
return unpack_from('<Q', data, offset=offset)[0] | ||
|
||
|
||
def is_ok_packet(packet): | ||
# https://dev.mysql.com/doc/internals/en/packet-OK_Packet.html | ||
return read_uint8(packet) == 0 and len(packet) >= 7 | ||
|
||
|
||
def is_eof_packet(packet): | ||
# http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-EOF_Packet | ||
# Caution: \xFE may be LengthEncodedInteger. | ||
# If \xFE is LengthEncodedInteger header, 8bytes followed. | ||
return read_uint8(packet) == 254 and len(packet) < 9 | ||
|
||
|
||
def is_auth_switch_request(packet): | ||
# http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest | ||
return read_uint8(packet) == 254 | ||
|
||
|
||
def is_load_local(packet): | ||
return read_uint8(packet) == 251 | ||
|
||
|
||
def is_resultset_packet(packet): | ||
return 1 <= read_uint8(packet) <= 250 | ||
|
||
|
||
def check_error(packet): | ||
if read_uint8(packet) == 255: | ||
errno = read_uint16(packet, offset=1) | ||
if DEBUG: print("errno = ", errno) | ||
err.raise_mysql_exception(packet) | ||
|
||
|
||
def read_string(data, offset=0): | ||
end = data.find(b'\0', offset) | ||
if end >= 0: | ||
result = data[offset:end] | ||
# Add one to length to account for the null byte | ||
return len(result) + 1, result | ||
else: | ||
raise ValueError("Invalid read on non-null terminated string") | ||
|
||
|
||
def read_length_encoded_integer(data, offset=0): | ||
col = read_uint8(data, offset=offset) | ||
bytes_read = 1 | ||
|
||
if col == 251: | ||
return bytes_read, None | ||
|
||
# Unsigned char column | ||
if col < 251: | ||
return bytes_read, col | ||
# Unsigned short column | ||
elif col == 252: | ||
return bytes_read + 2, read_uint16(data, offset=offset+bytes_read) | ||
# Unsigned int24 column | ||
elif col == 253: | ||
return bytes_read + 3, read_uint24(data, offset=offset+bytes_read) | ||
# Unsigned int64 column | ||
elif col == 254: | ||
return bytes_read + 8, read_uint64(data, offset=offset+bytes_read) | ||
else: | ||
raise ValueError | ||
|
||
|
||
def read_bytes(data, nbytes, offset=0): | ||
if nbytes is None: | ||
result = data[offset:] | ||
return len(result), result | ||
else: | ||
result = data[offset:offset+nbytes] | ||
if len(result) == nbytes: | ||
return nbytes, result | ||
|
||
error = ('Result length not requested length:\n' | ||
'Expected=%s Actual=%s Position: %s Data Length: %s' | ||
% (nbytes, len(result), offset, len(data))) | ||
if DEBUG: | ||
print(error) | ||
raise AssertionError(error) | ||
|
||
|
||
def read_length_coded_string(data, offset=0): | ||
bytes_read, length = read_length_encoded_integer(data, offset=offset) | ||
if length is not None: | ||
_br, result = read_bytes(data, length, offset=offset+bytes_read) | ||
return bytes_read + _br, result | ||
else: | ||
# Null column | ||
return bytes_read, None |
313E
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't split methods. Just move.