Skip to content

Instantly share code, notes, and snippets.

@mafrasi2
Last active November 18, 2015 06:48
Show Gist options
  • Save mafrasi2/71469b9d9514cff6fd48 to your computer and use it in GitHub Desktop.
Save mafrasi2/71469b9d9514cff6fd48 to your computer and use it in GitHub Desktop.
class VersionExchangeParser {
VersionExchangeState state;
uint16_t num_versions = 0;
uint16_t current_version = 0;
vector<Version> versions;
LengthDecoder aes_key_length_decoder;
length_t aes_key_length;
LengthDecoder hash_length_decoder;
length_t hash_length;
void update(uint8_t const& byte) {
switch(state) {
case FIRST_NUM_VERSIONS:
num_versions = byte;
state = SECOND_NUM_VERSIONS:
break;
case SECOND_NUM_VERSIONS:
num_versions |= byte << 8;
state = MAJ_VERSION:
break;
case MAJ_VERSION:
versions.push_back(Version());
versions[current_version].major = byte;
state = MIN_VERSION:
break;
case MIN_VERSION:
versions[current_version].minor = byte;
++current_version;
if(current_version == num_versions) {
state = KEYLEN:
} else {
state = MAJ_VERSION;
}
break;
case KEYLEN:
aes_key_length_decoder.update(byte);
if(aes_key_length_decoder.is_finished()) {
aes_key_length = aes_key_length_decoder.getValue();
state = HASHLEN_VERSION;
}
break;
case HASHLEN_VERSION:
hash_length_decoder.update(byte);
if(hash_length_decoder.is_finished()) {
hash_length = hash_length_decoder.getValue();
state = FINISHED;
}
break;
}
}
};
class VersionExchange {
uint16_t num_versions = 0;
vector<Version> versions;
length_t aes_key_length;
length_t hash_length;
void parse(Buffer& buf) {
num_versions = buf.read();
num_versions |= buf.read() << 8;
for(uint16_t i = 0; i < num_versions; ++i) {
versions.push_back(Version());
versions[i].major = buf.read();
versions[i].minor = buf.read();
}
aes_key_length = parse_length_t(buf);
hash_length = parse_length_t(buf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment