From 8eb6bdeb353f293f00b58d5c4aef71341a6b5e94 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 9 Jul 2017 17:36:51 -0700 Subject: [PATCH 1/2] Clean up config_state.c and fix readString --- right/src/config/config_state.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/right/src/config/config_state.c b/right/src/config/config_state.c index 458d676..7f741f9 100644 --- a/right/src/config/config_state.c +++ b/right/src/config/config_state.c @@ -8,27 +8,27 @@ uint8_t readUInt8(serialized_buffer_t *buffer) { } uint16_t readUInt16(serialized_buffer_t *buffer) { - uint8_t firstByte = buffer->buffer[buffer->offset++]; - return firstByte + (buffer->buffer[buffer->offset++] << 8); + uint16_t uInt16 = *(uint16_t *)(buffer->buffer + buffer->offset); + + buffer->offset += 2; + return uInt16; } bool readBool(serialized_buffer_t *buffer) { - return buffer->buffer[buffer->offset++] == 1; + return readUInt8(buffer); } uint16_t readCompactLength(serialized_buffer_t *buffer) { - uint16_t length = readUInt8(buffer); - if (length == 0xFF) { - length = readUInt16(buffer); - } - return length; + uint16_t compactLength = readUInt8(buffer); + + return compactLength == 0xFF ? readUInt16(buffer) : compactLength; } const char *readString(serialized_buffer_t *buffer, uint16_t *len) { - const char *str = (const char *)&(buffer->buffer[buffer->offset]); + const char *string; *len = readCompactLength(buffer); + string = buffer->buffer + buffer->offset; buffer->offset += *len; - - return str; + return string; } From 679021744e56eb40bcfcfcf41fc7d0328e3ba008 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 9 Jul 2017 17:41:18 -0700 Subject: [PATCH 2/2] Convert a few loop indices from uint8_t to uint16_t --- right/src/config/parse_config.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/right/src/config/parse_config.c b/right/src/config/parse_config.c index cde0ecc..7449f2e 100644 --- a/right/src/config/parse_config.c +++ b/right/src/config/parse_config.c @@ -22,21 +22,21 @@ parser_error_t ParseConfig(serialized_buffer_t *buffer) { uint16_t keymapCount; (void)dataModelVersion; - for (uint8_t moduleConfigurationIdx = 0; moduleConfigurationIdx < moduleConfigurationCount; moduleConfigurationIdx++) { + for (uint16_t moduleConfigurationIdx = 0; moduleConfigurationIdx < moduleConfigurationCount; moduleConfigurationIdx++) { errorCode = parseModuleConfiguration(buffer); if (errorCode != ParserError_Success) { return errorCode; } } macroCount = readCompactLength(buffer); - for (uint8_t macroIdx = 0; macroIdx < macroCount; macroIdx++) { + for (uint16_t macroIdx = 0; macroIdx < macroCount; macroIdx++) { // errorCode = ParseMacro(buffer); // if (errorCode != ParserError_Success) { // return errorCode; // } } keymapCount = readCompactLength(buffer); - for (uint8_t keymapIdx = 0; keymapIdx < keymapCount; keymapIdx++) { + for (uint16_t keymapIdx = 0; keymapIdx < keymapCount; keymapIdx++) { errorCode = ParseKeymap(buffer); if (errorCode != ParserError_Success) { return errorCode;