From f262138572ea666a3970d8ae7f3f2be733e5c806 Mon Sep 17 00:00:00 2001 From: Michael Heimpold Date: Sat, 18 Jan 2014 20:43:57 +0100 Subject: [PATCH] mbus_manufacturer_id: add error checking code Signed-off-by: Michael Heimpold --- mbus/mbus-protocol.c | 24 ++++++++++++++++++++++-- mbus/mbus-protocol.h | 3 ++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/mbus/mbus-protocol.c b/mbus/mbus-protocol.c index 852e28f..29a6d77 100755 --- a/mbus/mbus-protocol.c +++ b/mbus/mbus-protocol.c @@ -22,18 +22,38 @@ static char error_str[512]; #define NITEMS(x) (sizeof(x)/sizeof(x[0])) //------------------------------------------------------------------------------ -// Return the manufacturer ID according to the manufacturer's 3 byte ASCII code +// Returns the manufacturer ID according to the manufacturer's 3 byte ASCII code +// or zero when there was an error. //------------------------------------------------------------------------------ unsigned int mbus_manufacturer_id(char *manufacturer) { unsigned int id; + /* + * manufacturer must consist of at least 3 alphabetic characters, + * additional chars are silently ignored. + */ + + if (!manufacturer || strlen(manufacturer) < 3) + return 0; + + if (!isalpha(manufacturer[0]) || + !isalpha(manufacturer[1]) || + !isalpha(manufacturer[2])) + return 0; + id = (toupper(manufacturer[0]) - 64) * 32 * 32 + (toupper(manufacturer[1]) - 64) * 32 + (toupper(manufacturer[2]) - 64); - return id; + /* + * Valid input data should be in the range of 'AAA' to 'ZZZ' according to + * the FLAG Association (http://www.dlms.com/flag/), thus resulting in + * an ID from 0x0421 to 0x6b5a. If the conversion results in anything not + * in this range, simply discard it and return 0 instead. + */ + return (0x0421 <= id && id <= 0x6b5a) ? id : 0; } //------------------------------------------------------------------------------ diff --git a/mbus/mbus-protocol.h b/mbus/mbus-protocol.h index 29b1813..3fb39f0 100755 --- a/mbus/mbus-protocol.h +++ b/mbus/mbus-protocol.h @@ -493,7 +493,8 @@ typedef struct _mbus_data_secondary_address { #define MBUS_VARIABLE_DATA_MEDIUM_ADC 0x19 // -// Return manufacturer ID +// Returns the manufacturer ID or zero if the given +// string could not be converted into an ID // unsigned int mbus_manufacturer_id(char *manufacturer);