diff options
author | João Valverde <j@v6e.pt> | 2021-09-15 15:44:16 +0100 |
---|---|---|
committer | Wireshark GitLab Utility <gerald+gitlab-utility@wireshark.org> | 2021-09-16 15:53:14 +0000 |
commit | 8a584222f1f818793da41468b66bf6a0be6451ac (patch) | |
tree | 95ea1843d3fd0fd666ac1c5aa283b4786039052f | |
parent | 2de4d40e22ab097b7f87ec668d7b94ba426d4b2f (diff) |
Move epan/*to_str_back() functions to wsutil
-rw-r--r-- | epan/to_str-int.h | 103 | ||||
-rw-r--r-- | epan/to_str.c | 179 | ||||
-rw-r--r-- | epan/to_str.h | 11 | ||||
-rw-r--r-- | wsutil/to_str.c | 219 | ||||
-rw-r--r-- | wsutil/to_str.h | 114 |
5 files changed, 333 insertions, 293 deletions
diff --git a/epan/to_str-int.h b/epan/to_str-int.h index 2e145c5f8c..f129e6ea25 100644 --- a/epan/to_str-int.h +++ b/epan/to_str-int.h @@ -71,107 +71,4 @@ char *qword_to_hex(char *out, guint64 qword); */ char *qword_to_hex_punct(char *out, guint64 qword, char punct); -/** - * oct_to_str_back() - * - * Output guint32 octal representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * - * String is not NUL terminated by this routine. - * There needs to be at least 12 bytes in the buffer. - */ -char *oct_to_str_back(char *ptr, guint32 value); - -/** - * oct64_to_str_back() - * - * Output guint64 octal representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * - * String is not NUL terminated by this routine. - * There needs to be at least 12 bytes in the buffer. - */ -char *oct64_to_str_back(char *ptr, guint64 value); - -/** - * hex_to_str_back() - * - * Output guint32 hex representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * This routine will output for sure (can output more) 'len' decimal characters (number padded with '0'). - * - * String is not NUL terminated by this routine. - * There needs to be at least 2 + MAX(8, len) bytes in the buffer. - */ -char *hex_to_str_back(char *ptr, int len, guint32 value); - -/** - * hex64_to_str_back() - * - * Output guint64 hex representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * This routine will output for sure (can output more) 'len' decimal characters (number padded with '0'). - * - * String is not NUL terminated by this routine. - * There needs to be at least 2 + MAX(16, len) bytes in the buffer. - */ -char *hex64_to_str_back(char *ptr, int len, guint64 value); - -/** - * uint64_str_back() - * - * Output guint64 decimal representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * - * String is not NUL terminated by this routine. - * There needs to be at least 20 bytes in the buffer. - */ -char *uint64_to_str_back(char *ptr, guint64 value); - -/** - * uint_to_str_back_len() - * - * Output guint32 decimal representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * This routine will output for sure (can output more) 'len' decimal characters (number padded with '0'). - * - * String is not NUL terminated by this routine. - * There needs to be at least MAX(10, len) bytes in the buffer. - */ -char *uint_to_str_back_len(char *ptr, guint32 value, int len); - -/** - * uint64_to_str_back_len() - * - * Output guint64 decimal representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * This routine will output for sure (can output more) 'len' decimal characters (number padded with '0'). - * - * String is not NUL terminated by this routine. - * There needs to be at least MAX(20, len) bytes in the buffer. - */ -char *uint64_to_str_back_len(char *ptr, guint64 value, int len); - -/** - * int_to_str_back() - * - * Output gint32 decimal representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * - * String is not NUL terminated by this routine. - * There needs to be at least 11 bytes in the buffer. - */ -char *int_to_str_back(char *ptr, gint32 value); - -/** - * int64_to_str_back() - * - * Output gint64 decimal representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * - * String is not NUL terminated by this routine. - * There needs to be at least 21 bytes in the buffer. - */ -char *int64_to_str_back(char *ptr, gint64 value); - #endif /* __TO_STR_INT_H__ */ diff --git a/epan/to_str.c b/epan/to_str.c index 5ac722223b..565f3a5210 100644 --- a/epan/to_str.c +++ b/epan/to_str.c @@ -1056,185 +1056,6 @@ port_type_to_str (port_type type) } } -char * -oct_to_str_back(char *ptr, guint32 value) -{ - while (value) { - *(--ptr) = '0' + (value & 0x7); - value >>= 3; - } - - *(--ptr) = '0'; - return ptr; -} - -char * -oct64_to_str_back(char *ptr, guint64 value) -{ - while (value) { - *(--ptr) = '0' + (value & 0x7); - value >>= 3; - } - - *(--ptr) = '0'; - return ptr; -} - -char * -hex_to_str_back(char *ptr, int len, guint32 value) -{ - do { - *(--ptr) = low_nibble_of_octet_to_hex(value); - value >>= 4; - len--; - } while (value); - - /* pad */ - while (len > 0) { - *(--ptr) = '0'; - len--; - } - - *(--ptr) = 'x'; - *(--ptr) = '0'; - - return ptr; -} - -char * -hex64_to_str_back(char *ptr, int len, guint64 value) -{ - do { - *(--ptr) = low_nibble_of_octet_to_hex(value & 0xF); - value >>= 4; - len--; - } while (value); - - /* pad */ - while (len > 0) { - *(--ptr) = '0'; - len--; - } - - *(--ptr) = 'x'; - *(--ptr) = '0'; - - return ptr; -} - -char * -uint_to_str_back(char *ptr, guint32 value) -{ - char const *p; - - /* special case */ - if (value == 0) - *(--ptr) = '0'; - - while (value >= 10) { - p = fast_strings[100 + (value % 100)]; - - value /= 100; - - *(--ptr) = p[2]; - *(--ptr) = p[1]; - } - - if (value) - *(--ptr) = (value) | '0'; - - return ptr; -} - -char * -uint64_to_str_back(char *ptr, guint64 value) -{ - char const *p; - - /* special case */ - if (value == 0) - *(--ptr) = '0'; - - while (value >= 10) { - p = fast_strings[100 + (value % 100)]; - - value /= 100; - - *(--ptr) = p[2]; - *(--ptr) = p[1]; - } - - /* value will be 0..9, so using '& 0xF' is safe, and faster than '% 10' */ - if (value) - *(--ptr) = (value & 0xF) | '0'; - - return ptr; -} - -char * -uint_to_str_back_len(char *ptr, guint32 value, int len) -{ - char *new_ptr; - - new_ptr = uint_to_str_back(ptr, value); - - /* substract from len number of generated characters */ - len -= (int)(ptr - new_ptr); - - /* pad remaining with '0' */ - while (len > 0) - { - *(--new_ptr) = '0'; - len--; - } - - return new_ptr; -} - -char * -uint64_to_str_back_len(char *ptr, guint64 value, int len) -{ - char *new_ptr; - - new_ptr = uint64_to_str_back(ptr, value); - - /* substract from len number of generated characters */ - len -= (int)(ptr - new_ptr); - - /* pad remaining with '0' */ - while (len > 0) - { - *(--new_ptr) = '0'; - len--; - } - - return new_ptr; -} - -char * -int_to_str_back(char *ptr, gint32 value) -{ - if (value < 0) { - ptr = uint_to_str_back(ptr, -value); - *(--ptr) = '-'; - } else - ptr = uint_to_str_back(ptr, value); - - return ptr; -} - -char * -int64_to_str_back(char *ptr, gint64 value) -{ - if (value < 0) { - ptr = uint64_to_str_back(ptr, -value); - *(--ptr) = '-'; - } else - ptr = uint64_to_str_back(ptr, value); - - return ptr; -} - /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/epan/to_str.h b/epan/to_str.h index eebb0e7ad4..3ed13a0bc8 100644 --- a/epan/to_str.h +++ b/epan/to_str.h @@ -169,17 +169,6 @@ WS_DLL_PUBLIC char *word_to_hex(char *out, guint16 word); */ WS_DLL_PUBLIC char *dword_to_hex(char *out, guint32 dword); -/** - * uint_to_str_back() - * - * Output guint32 decimal representation backward (last character will be written on ptr - 1), - * and return pointer to first character. - * - * String is not NUL terminated by this routine. - * There needs to be at least 10 bytes in the buffer. - */ -WS_DLL_PUBLIC char *uint_to_str_back(char *ptr, guint32 value); - #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/wsutil/to_str.c b/wsutil/to_str.c index cfdfe2f26c..9fcbbf81e0 100644 --- a/wsutil/to_str.c +++ b/wsutil/to_str.c @@ -21,6 +21,41 @@ #include <wsutil/wslog.h> +static const char fast_strings[][4] = { + "0", "1", "2", "3", "4", "5", "6", "7", + "8", "9", "10", "11", "12", "13", "14", "15", + "16", "17", "18", "19", "20", "21", "22", "23", + "24", "25", "26", "27", "28", "29", "30", "31", + "32", "33", "34", "35", "36", "37", "38", "39", + "40", "41", "42", "43", "44", "45", "46", "47", + "48", "49", "50", "51", "52", "53", "54", "55", + "56", "57", "58", "59", "60", "61", "62", "63", + "64", "65", "66", "67", "68", "69", "70", "71", + "72", "73", "74", "75", "76", "77", "78", "79", + "80", "81", "82", "83", "84", "85", "86", "87", + "88", "89", "90", "91", "92", "93", "94", "95", + "96", "97", "98", "99", "100", "101", "102", "103", + "104", "105", "106", "107", "108", "109", "110", "111", + "112", "113", "114", "115", "116", "117", "118", "119", + "120", "121", "122", "123", "124", "125", "126", "127", + "128", "129", "130", "131", "132", "133", "134", "135", + "136", "137", "138", "139", "140", "141", "142", "143", + "144", "145", "146", "147", "148", "149", "150", "151", + "152", "153", "154", "155", "156", "157", "158", "159", + "160", "161", "162", "163", "164", "165", "166", "167", + "168", "169", "170", "171", "172", "173", "174", "175", + "176", "177", "178", "179", "180", "181", "182", "183", + "184", "185", "186", "187", "188", "189", "190", "191", + "192", "193", "194", "195", "196", "197", "198", "199", + "200", "201", "202", "203", "204", "205", "206", "207", + "208", "209", "210", "211", "212", "213", "214", "215", + "216", "217", "218", "219", "220", "221", "222", "223", + "224", "225", "226", "227", "228", "229", "230", "231", + "232", "233", "234", "235", "236", "237", "238", "239", + "240", "241", "242", "243", "244", "245", "246", "247", + "248", "249", "250", "251", "252", "253", "254", "255" +}; + static inline char low_nibble_of_octet_to_hex(guint8 oct) { @@ -158,6 +193,190 @@ bytes_to_str_max(wmem_allocator_t *scope, const guint8 *bd, size_t bd_len, size_ } /* + * The *_to_str_back() functions measured approx. a x7.5 speed-up versus + * snprintf() on my Linux system with GNU libc. + */ + +char * +oct_to_str_back(char *ptr, guint32 value) +{ + while (value) { + *(--ptr) = '0' + (value & 0x7); + value >>= 3; + } + + *(--ptr) = '0'; + return ptr; +} + +char * +oct64_to_str_back(char *ptr, guint64 value) +{ + while (value) { + *(--ptr) = '0' + (value & 0x7); + value >>= 3; + } + + *(--ptr) = '0'; + return ptr; +} + +char * +hex_to_str_back(char *ptr, int len, guint32 value) +{ + do { + *(--ptr) = low_nibble_of_octet_to_hex(value); + value >>= 4; + len--; + } while (value); + + /* pad */ + while (len > 0) { + *(--ptr) = '0'; + len--; + } + + *(--ptr) = 'x'; + *(--ptr) = '0'; + + return ptr; +} + +char * +hex64_to_str_back(char *ptr, int len, guint64 value) +{ + do { + *(--ptr) = low_nibble_of_octet_to_hex(value & 0xF); + value >>= 4; + len--; + } while (value); + + /* pad */ + while (len > 0) { + *(--ptr) = '0'; + len--; + } + + *(--ptr) = 'x'; + *(--ptr) = '0'; + + return ptr; +} + +char * +uint_to_str_back(char *ptr, guint32 value) +{ + char const *p; + + /* special case */ + if (value == 0) + *(--ptr) = '0'; + + while (value >= 10) { + p = fast_strings[100 + (value % 100)]; + + value /= 100; + + *(--ptr) = p[2]; + *(--ptr) = p[1]; + } + + if (value) + *(--ptr) = (value) | '0'; + + return ptr; +} + +char * +uint64_to_str_back(char *ptr, guint64 value) +{ + char const *p; + + /* special case */ + if (value == 0) + *(--ptr) = '0'; + + while (value >= 10) { + p = fast_strings[100 + (value % 100)]; + + value /= 100; + + *(--ptr) = p[2]; + *(--ptr) = p[1]; + } + + /* value will be 0..9, so using '& 0xF' is safe, and faster than '% 10' */ + if (value) + *(--ptr) = (value & 0xF) | '0'; + + return ptr; +} + +char * +uint_to_str_back_len(char *ptr, guint32 value, int len) +{ + char *new_ptr; + + new_ptr = uint_to_str_back(ptr, value); + + /* substract from len number of generated characters */ + len -= (int)(ptr - new_ptr); + + /* pad remaining with '0' */ + while (len > 0) + { + *(--new_ptr) = '0'; + len--; + } + + return new_ptr; +} + +char * +uint64_to_str_back_len(char *ptr, guint64 value, int len) +{ + char *new_ptr; + + new_ptr = uint64_to_str_back(ptr, value); + + /* substract from len number of generated characters */ + len -= (int)(ptr - new_ptr); + + /* pad remaining with '0' */ + while (len > 0) + { + *(--new_ptr) = '0'; + len--; + } + + return new_ptr; +} + +char * +int_to_str_back(char *ptr, gint32 value) +{ + if (value < 0) { + ptr = uint_to_str_back(ptr, -value); + *(--ptr) = '-'; + } else + ptr = uint_to_str_back(ptr, value); + + return ptr; +} + +char * +int64_to_str_back(char *ptr, gint64 value) +{ + if (value < 0) { + ptr = uint64_to_str_back(ptr, -value); + *(--ptr) = '-'; + } else + ptr = uint64_to_str_back(ptr, value); + + return ptr; +} + +/* * Editor modelines - https://www.wireshark.org/tools/modelines.html * * Local variables: diff --git a/wsutil/to_str.h b/wsutil/to_str.h index 54eacf89af..5a174ed783 100644 --- a/wsutil/to_str.h +++ b/wsutil/to_str.h @@ -74,6 +74,120 @@ WS_DLL_PUBLIC char *bytes_to_str_max(wmem_allocator_t *scope, const guint8 *bd, #define bytes_to_str(scope, bd, len) bytes_to_str_max(scope, bd, len, MAX_BYTE_STR_LEN) +/** + * oct_to_str_back() + * + * Output guint32 octal representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * + * String is not NUL terminated by this routine. + * There needs to be at least 12 bytes in the buffer. + */ +WS_DLL_PUBLIC char *oct_to_str_back(char *ptr, guint32 value); + +/** + * oct64_to_str_back() + * + * Output guint64 octal representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * + * String is not NUL terminated by this routine. + * There needs to be at least 12 bytes in the buffer. + */ +WS_DLL_PUBLIC char *oct64_to_str_back(char *ptr, guint64 value); + +/** + * hex_to_str_back() + * + * Output guint32 hex representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * This routine will output for sure (can output more) 'len' decimal characters (number padded with '0'). + * + * String is not NUL terminated by this routine. + * There needs to be at least 2 + MAX(8, len) bytes in the buffer. + */ +WS_DLL_PUBLIC char *hex_to_str_back(char *ptr, int len, guint32 value); + +/** + * hex64_to_str_back() + * + * Output guint64 hex representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * This routine will output for sure (can output more) 'len' decimal characters (number padded with '0'). + * + * String is not NUL terminated by this routine. + * There needs to be at least 2 + MAX(16, len) bytes in the buffer. + */ +WS_DLL_PUBLIC char *hex64_to_str_back(char *ptr, int len, guint64 value); + +/** + * uint_to_str_back() + * + * Output guint32 decimal representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * + * String is not NUL terminated by this routine. + * There needs to be at least 10 bytes in the buffer. + */ +WS_DLL_PUBLIC char *uint_to_str_back(char *ptr, guint32 value); + +/** + * uint64_str_back() + * + * Output guint64 decimal representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * + * String is not NUL terminated by this routine. + * There needs to be at least 20 bytes in the buffer. + */ +WS_DLL_PUBLIC char *uint64_to_str_back(char *ptr, guint64 value); + +/** + * uint_to_str_back_len() + * + * Output guint32 decimal representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * This routine will output for sure (can output more) 'len' decimal characters (number padded with '0'). + * + * String is not NUL terminated by this routine. + * There needs to be at least MAX(10, len) bytes in the buffer. + */ +WS_DLL_PUBLIC char *uint_to_str_back_len(char *ptr, guint32 value, int len); + +/** + * uint64_to_str_back_len() + * + * Output guint64 decimal representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * This routine will output for sure (can output more) 'len' decimal characters (number padded with '0'). + * + * String is not NUL terminated by this routine. + * There needs to be at least MAX(20, len) bytes in the buffer. + */ +WS_DLL_PUBLIC char *uint64_to_str_back_len(char *ptr, guint64 value, int len); + +/** + * int_to_str_back() + * + * Output gint32 decimal representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * + * String is not NUL terminated by this routine. + * There needs to be at least 11 bytes in the buffer. + */ +WS_DLL_PUBLIC char *int_to_str_back(char *ptr, gint32 value); + +/** + * int64_to_str_back() + * + * Output gint64 decimal representation backward (last character will be written on ptr - 1), + * and return pointer to first character. + * + * String is not NUL terminated by this routine. + * There needs to be at least 21 bytes in the buffer. + */ +WS_DLL_PUBLIC char *int64_to_str_back(char *ptr, gint64 value); + #ifdef __cplusplus } #endif /* __cplusplus */ |