return ret;
}
-int ByteExtractString(uint64_t *res, int base, uint16_t len, const char *str)
+int ByteExtractString(uint64_t *res, int base, uint16_t len, const char *str, bool strict)
{
const char *ptr = str;
char *endptr = NULL;
SCLogDebug("invalid numeric value");
return -1;
}
- /* This will interfere with some rules that do not know the length
- * in advance and instead are just using the max.
- */
-#if 0
- else if (len && *endptr != '\0') {
- fprintf(stderr, "ByteExtractString: Extra characters following numeric value\n");
+ else if (strict && len && *endptr != '\0') {
+ SCLogError(SC_ERR_INVALID_NUMERIC_VALUE, "Extra characters following numeric value");
return -1;
}
-#endif
return (endptr - ptr);
}
int ByteExtractStringUint64(uint64_t *res, int base, uint16_t len, const char *str)
{
- return ByteExtractString(res, base, len, str);
+ return ByteExtractString(res, base, len, str, false);
}
int ByteExtractStringUint32(uint32_t *res, int base, uint16_t len, const char *str)
{
uint64_t i64;
- int ret = ByteExtractString(&i64, base, len, str);
+ int ret = ByteExtractString(&i64, base, len, str, false);
if (ret <= 0) {
return ret;
}
{
uint64_t i64;
- int ret = ByteExtractString(&i64, base, len, str);
+ int ret = ByteExtractString(&i64, base, len, str, false);
if (ret <= 0) {
return ret;
}
{
uint64_t i64;
- int ret = ByteExtractString(&i64, base, len, str);
+ int ret = ByteExtractString(&i64, base, len, str, false);
if (ret <= 0) {
return ret;
}
return ret;
}
-int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str)
+int StringParseUint64(uint64_t *res, int base, uint16_t len, const char *str)
+{
+ return ByteExtractString(res, base, len, str, true);
+}
+
+int StringParseUint32(uint32_t *res, int base, uint16_t len, const char *str)
+{
+ uint64_t i64;
+
+ int ret = ByteExtractString(&i64, base, len, str, true);
+ if (ret <= 0) {
+ return ret;
+ }
+
+ *res = (uint32_t)i64;
+
+ if ((uint64_t)(*res) != i64) {
+ SCLogError("Numeric value out of range (%" PRIu64 " > %" PRIuMAX ")",
+ i64, (uintmax_t)UINT_MAX);
+ return -1;
+ }
+
+ return ret;
+}
+
+int StringParseUint16(uint16_t *res, int base, uint16_t len, const char *str)
+{
+ uint64_t i64;
+
+ int ret = ByteExtractString(&i64, base, len, str, true);
+ if (ret <= 0) {
+ return ret;
+ }
+
+ *res = (uint16_t)i64;
+
+ if ((uint64_t)(*res) != i64) {
+ SCLogError("Numeric value out of range (%" PRIu64 " > %" PRIuMAX ")",
+ i64, (uintmax_t)USHRT_MAX);
+ return -1;
+ }
+
+ return ret;
+}
+
+int StringParseUint8(uint8_t *res, int base, uint16_t len, const char *str)
+{
+ uint64_t i64;
+
+ int ret = ByteExtractString(&i64, base, len, str, true);
+ if (ret <= 0) {
+ return ret;
+ }
+
+ *res = (uint8_t)i64;
+
+ if ((uint64_t)(*res) != i64) {
+ SCLogError("Numeric value out of range (%" PRIu64 " > %" PRIuMAX ")",
+ i64, (uintmax_t)UCHAR_MAX);
+ return -1;
+ }
+
+ return ret;
+}
+int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str, bool strict)
{
const char *ptr = str;
char *endptr;
SCLogError(SC_ERR_INVALID_NUMERIC_VALUE, "Invalid numeric value");
return -1;
}
- /* This will interfere with some rules that do not know the length
- * in advance and instead are just using the max.
- */
-#if 0
- else if (len && *endptr != '\0') {
- fprintf(stderr, "ByteExtractStringSigned: Extra characters following numeric value\n");
+ else if (strict && len && *endptr != '\0') {
+ SCLogError(SC_ERR_INVALID_NUMERIC_VALUE, "Extra characters following numeric value");
return -1;
}
-#endif
//fprintf(stderr, "ByteExtractStringSigned: Extracted base %d: 0x%" PRIx64 "\n", base, *res);
int ByteExtractStringInt64(int64_t *res, int base, uint16_t len, const char *str)
{
- return ByteExtractStringSigned(res, base, len, str);
+ return ByteExtractStringSigned(res, base, len, str, false);
}
int ByteExtractStringInt32(int32_t *res, int base, uint16_t len, const char *str)
int64_t i64;
int ret;
- ret = ByteExtractStringSigned(&i64, base, len, str);
+ ret = ByteExtractStringSigned(&i64, base, len, str, false);
if (ret <= 0) {
return ret;
}
int64_t i64;
int ret;
- ret = ByteExtractStringSigned(&i64, base, len, str);
+ ret = ByteExtractStringSigned(&i64, base, len, str, false);
if (ret <= 0) {
return ret;
}
int64_t i64;
int ret;
- ret = ByteExtractStringSigned(&i64, base, len, str);
+ ret = ByteExtractStringSigned(&i64, base, len, str, false);
if (ret <= 0) {
return ret;
}
return ret;
}
+int StringParseInt64(int64_t *res, int base, uint16_t len, const char *str)
+{
+ return ByteExtractStringSigned(res, base, len, str, true);
+}
+
+int StringParseInt32(int32_t *res, int base, uint16_t len, const char *str)
+{
+ int64_t i64;
+ int ret;
+
+ ret = ByteExtractStringSigned(&i64, base, len, str, true);
+ if (ret <= 0) {
+ return ret;
+ }
+
+ *res = (int32_t)i64;
+
+ if ((int64_t)(*res) != i64) {
+ SCLogError(SC_ERR_NUMERIC_VALUE_ERANGE, "Numeric value out of range "
+ "(%" PRIi64 " > %" PRIiMAX ")\n", i64, (intmax_t)INT_MAX);
+ return -1;
+ }
+
+ return ret;
+}
+
+int StringParseInt16(int16_t *res, int base, uint16_t len, const char *str)
+{
+ int64_t i64;
+ int ret;
+
+ ret = ByteExtractStringSigned(&i64, base, len, str, true);
+ if (ret <= 0) {
+ return ret;
+ }
+
+ *res = (int16_t)i64;
+
+ if ((int64_t)(*res) != i64) {
+ SCLogError(SC_ERR_NUMERIC_VALUE_ERANGE, "Numeric value out of range "
+ "(%" PRIi64 " > %" PRIiMAX ")\n", i64, (intmax_t)SHRT_MAX);
+ return -1;
+ }
+
+ return ret;
+}
+
+int StringParseInt8(int8_t *res, int base, uint16_t len, const char *str)
+{
+ int64_t i64;
+ int ret;
+
+ ret = ByteExtractStringSigned(&i64, base, len, str, true);
+ if (ret <= 0) {
+ return ret;
+ }
+
+ *res = (int8_t)i64;
+
+ if ((int64_t)(*res) != i64) {
+ SCLogError(SC_ERR_NUMERIC_VALUE_ERANGE, "Numeric value out of range "
+ "(%" PRIi64 " > %" PRIiMAX ")\n", i64, (intmax_t)CHAR_MAX);
+ return -1;
+ }
+
+ return ret;
+}
/* UNITTESTS */
#ifdef UNITTESTS
* \param base Base of the number to extract
* \param len Number of bytes to extract (23 max or 0 for unbounded)
* \param str String to extract from
+ * \param bool Enable strict check for parsers
*
* \return n Number of bytes extracted on success
* \return -1 On error
*/
-int ByteExtractString(uint64_t *res, int base, uint16_t len, const char *str);
+int ByteExtractString(uint64_t *res, int base, uint16_t len, const char *str, bool strict);
/**
* Extract unsigned integer value from a string as uint64_t.
* \param base Base of the number to extract
* \param len Number of bytes to extract (23 max or 0 for unbounded)
* \param str String to extract from
+ * \param bool Enable strict check for parsers
*
* \return n Number of bytes extracted on success
* \return -1 On error
*/
-int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str);
+int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str, bool strict);
/**
* Extract signed integer value from a string as uint64_t.
*/
int ByteExtractStringInt8(int8_t *res, int base, uint16_t len, const char *str);
+/**
+ * Extract unsigned integer value from a string as uint64_t strictly.
+ *
+ * \param res Stores result
+ * \param base Base of the number to extract
+ * \param len Number of bytes to extract (23 max or 0 for unbounded)
+ * \param len Number of bytes to extract (23 max)
+ * \param str String to extract from
+ *
+ * \return n Number of bytes extracted on success
+ * \return -1 On error
+ */
+int StringParseUint64(uint64_t *res, int base, uint16_t len, const char *str);
+
+/**
+ * Extract unsigned integer value from a string as uint32_t strictly.
+ *
+ * \param res Stores result
+ * \param base Base of the number to extract
+ * \param len Number of bytes to extract (23 max or 0 for unbounded)
+ * \param str String to extract from
+ *
+ * \return n Number of bytes extracted on success
+ * \return -1 On error
+ */
+int StringParseUint32(uint32_t *res, int base, uint16_t len, const char *str);
+
+/**
+ * Extract unsigned integer value from a string as uint16_t strictly.
+ *
+ * \param res Stores result
+ * \param base Base of the number to extract
+ * \param len Number of bytes to extract (23 max or 0 for unbounded)
+ * \param str String to extract from
+ *
+ * \return n Number of bytes extracted on success
+ * \return -1 On error
+ */
+int StringParseUint16(uint16_t *res, int base, uint16_t len, const char *str);
+
+/**
+ * Extract unsigned integer value from a string as uint8_t strictly.
+ *
+ * \param res Stores result
+ * \param base Base of the number to extract
+ * \param len Number of bytes to extract (23 max or 0 for unbounded)
+ * \param str String to extract from
+ *
+ * \return n Number of bytes extracted on success
+ * \return -1 On error
+ */
+int StringParseUint8(uint8_t *res, int base, uint16_t len, const char *str);
+
+/**
+ * Extract signed integer value from a string as int64_t strictly.
+ *
+ * \param res Stores result
+ * \param base Base of the number to extract
+ * \param len Number of bytes to extract (23 max or 0 for unbounded)
+ * \param str String to extract from
+ *
+ * \return n Number of bytes extracted on success
+ * \return -1 On error
+ */
+int StringParseInt64(int64_t *res, int base, uint16_t len, const char *str);
+
+/**
+ * Extract signed integer value from a string as int32_t strictly.
+ *
+ * \param res Stores result
+ * \param base Base of the number to extract
+ * \param len Number of bytes to extract (23 max or 0 for unbounded)
+ * \param str String to extract from
+ *
+ * \return n Number of bytes extracted on success
+ * \return -1 On error
+ */
+int StringParseInt32(int32_t *res, int base, uint16_t len, const char *str);
+
+/**
+ * Extract signed integer value from a string as int16_t strictly.
+ *
+ * \param res Stores result
+ * \param base Base of the number to extract
+ * \param len Number of bytes to extract (23 max or 0 for unbounded)
+ * \param str String to extract from
+ *
+ * \return n Number of bytes extracted on success
+ * \return -1 On error
+ */
+int StringParseInt16(int16_t *res, int base, uint16_t len, const char *str);
+
+/**
+ * Extract signed integer value from a string as int8_t strictly.
+ *
+ * \param res Stores result
+ * \param base Base of the number to extract
+ * \param len Number of bytes to extract (23 max or 0 for unbounded)
+ * \param str String to extract from
+ *
+ * \return n Number of bytes extracted on success
+ * \return -1 On error
+ */
+int StringParseInt8(int8_t *res, int base, uint16_t len, const char *str);
+
#ifdef UNITTESTS
void ByteRegisterTests(void);
#endif /* UNITTESTS */