From e850469da885e4951c6f16eab4327ad1fe69525e Mon Sep 17 00:00:00 2001 From: skarg Date: Thu, 2 Dec 2010 19:33:22 +0000 Subject: [PATCH] Added character string helper to test for printable. Added to WP checks. --- bacnet-stack/demo/handler/h_wp.c | 5 +++++ bacnet-stack/include/bacstr.h | 2 ++ bacnet-stack/src/bacstr.c | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/bacnet-stack/demo/handler/h_wp.c b/bacnet-stack/demo/handler/h_wp.c index b1ec68c8..56707274 100644 --- a/bacnet-stack/demo/handler/h_wp.c +++ b/bacnet-stack/demo/handler/h_wp.c @@ -177,6 +177,11 @@ bool WPValidateString( (characterstring_length(&pValue->type.Character_String) == 0)) { *pErrorCode = ERROR_CODE_VALUE_OUT_OF_RANGE; + } else if ((bEmptyAllowed == false) && + (!characterstring_printable( + &pValue->type.Character_String))) { + /* assumption: non-empty also means must be "printable" */ + *pErrorCode = ERROR_CODE_VALUE_OUT_OF_RANGE; } else if (characterstring_length(&pValue-> type.Character_String) >= iMaxLen) { *pErrorClass = ERROR_CLASS_RESOURCES; diff --git a/bacnet-stack/include/bacstr.h b/bacnet-stack/include/bacstr.h index 68fd805c..12f7c199 100644 --- a/bacnet-stack/include/bacstr.h +++ b/bacnet-stack/include/bacstr.h @@ -151,6 +151,8 @@ extern "C" { BACNET_CHARACTER_STRING * char_string); size_t characterstring_capacity( BACNET_CHARACTER_STRING * char_string); + bool characterstring_printable( + BACNET_CHARACTER_STRING * char_string); /* returns false if the string exceeds capacity initialize by using length=0 */ diff --git a/bacnet-stack/src/bacstr.c b/bacnet-stack/src/bacstr.c index f55c6ae9..a4473011 100644 --- a/bacnet-stack/src/bacstr.c +++ b/bacnet-stack/src/bacstr.c @@ -454,6 +454,42 @@ uint8_t characterstring_encoding( return encoding; } +/* returns true if string is printable */ +/* used to assist in the requirement that + "The set of characters used in the Object_Name shall be + restricted to printable characters." */ +/* printable character: a character that represents a printable +symbol as opposed to a device control character. These +include, but are not limited to, upper- and lowercase letters, +punctuation marks, and mathematical symbols. The exact set +depends upon the character set being used. In ANSI X3.4 the +printable characters are represented by single octets in the range +X'20' - X'7E'.*/ +bool characterstring_printable( + BACNET_CHARACTER_STRING * char_string) +{ + bool status = false; /* return value */ + size_t i; /* counter */ + + if (char_string) { + if (char_string->encoding == CHARACTER_ANSI_X34) { + status = true; + for (i = 0; i < MAX_CHARACTER_STRING_BYTES; i++) { + if (i < char_string->length) { + if ((char_string->value[i] < 0x20) || + (char_string->value[i] > 0x7E)) { + status = false; + } + } else { + break; + } + } + } + } + + return status; +} + /* returns false if the string exceeds capacity initialize by using value=NULL */ bool octetstring_init(