From 24fd8c6740b2bd2db63b3b9f39ecf669c2e2812c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Valverde?= Date: Thu, 16 Sep 2021 14:54:09 +0100 Subject: [PATCH] Move more numerical epan/*to_str() routines to wsutil --- epan/addr_resolv.c | 2 +- epan/address_types.c | 1 - epan/dissectors/packet-geonw.c | 2 +- epan/ftypes/ftype-bytes.c | 2 +- epan/ftypes/ftype-integer.c | 2 +- epan/ftypes/ftype-protocol.c | 2 +- epan/print.c | 2 +- epan/proto.c | 1 - epan/to_str-int.h | 74 -------------------------- epan/to_str.c | 95 ---------------------------------- epan/to_str.h | 33 ------------ wsutil/to_str.c | 70 +++++++++++++++++++++++++ wsutil/to_str.h | 91 ++++++++++++++++++++++++++++++++ 13 files changed, 167 insertions(+), 210 deletions(-) delete mode 100644 epan/to_str-int.h diff --git a/epan/addr_resolv.c b/epan/addr_resolv.c index b46a7daf51..da178a29c7 100644 --- a/epan/addr_resolv.c +++ b/epan/addr_resolv.c @@ -90,7 +90,7 @@ #include #include -#include +#include #include #include #include diff --git a/epan/address_types.c b/epan/address_types.c index cb807bfeef..111c729143 100644 --- a/epan/address_types.c +++ b/epan/address_types.c @@ -13,7 +13,6 @@ #include "packet.h" #include "address_types.h" #include "to_str.h" -#include "to_str-int.h" #include "addr_resolv.h" #include "wsutil/pint.h" #include "wsutil/str_util.h" diff --git a/epan/dissectors/packet-geonw.c b/epan/dissectors/packet-geonw.c index e668845deb..807252a639 100644 --- a/epan/dissectors/packet-geonw.c +++ b/epan/dissectors/packet-geonw.c @@ -63,7 +63,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/epan/ftypes/ftype-bytes.c b/epan/ftypes/ftype-bytes.c index 31952e7521..fececd36bd 100644 --- a/epan/ftypes/ftype-bytes.c +++ b/epan/ftypes/ftype-bytes.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #define CMP_MATCHES cmp_matches diff --git a/epan/ftypes/ftype-integer.c b/epan/ftypes/ftype-integer.c index dc604f1eb2..bf2e5d2b9e 100644 --- a/epan/ftypes/ftype-integer.c +++ b/epan/ftypes/ftype-integer.c @@ -13,7 +13,7 @@ #include "ftypes-int.h" #include #include -#include +#include #include diff --git a/epan/ftypes/ftype-protocol.c b/epan/ftypes/ftype-protocol.c index 760ef39977..21d39b0454 100644 --- a/epan/ftypes/ftype-protocol.c +++ b/epan/ftypes/ftype-protocol.c @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/epan/print.c b/epan/print.c index c53caa0684..722e741502 100644 --- a/epan/print.c +++ b/epan/print.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/epan/proto.c b/epan/proto.c index a49af1cccc..cf2cc6b5b7 100644 --- a/epan/proto.c +++ b/epan/proto.c @@ -40,7 +40,6 @@ #include #include "charsets.h" #include "column-utils.h" -#include "to_str-int.h" #include "to_str.h" #include "osi-utils.h" #include "expert.h" diff --git a/epan/to_str-int.h b/epan/to_str-int.h deleted file mode 100644 index f129e6ea25..0000000000 --- a/epan/to_str-int.h +++ /dev/null @@ -1,74 +0,0 @@ -/* to_str-int.h - * Definitions for utilities to convert various other types to strings. - * - * Wireshark - Network traffic analyzer - * By Gerald Combs - * Copyright 1998 Gerald Combs - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#ifndef __TO_STR_INT_H__ -#define __TO_STR_INT_H__ - -#include - -/** - * word_to_hex_punct() - * - * Output guint16 hex represetation to 'out', and return pointer after last character. - * Each byte will be separated with punct character (cannot be NUL). - * It always output full representation (padded with 0). - * - * String is not NUL terminated by this routine. - * There needs to be at least 5 bytes in the buffer. - */ -char *word_to_hex_punct(char *out, guint16 word, char punct); - -/** - * word_to_hex_npad() - * - * Output guint16 hex represetation to 'out', and return pointer after last character. - * Value is not padded. - * - * String is not NUL terminated by this routine. - * There needs to be at least 4 bytes in the buffer. - */ -char *word_to_hex_npad(char *out, guint16 word); - -/** - * dword_to_hex_punct() - * - * Output guint32 hex represetation to 'out', and return pointer after last character. - * Each byte will be separated with punct character (cannot be NUL). - * It always output full representation (padded with 0). - * - * String is not NUL terminated by this routine. - * There needs to be at least 11 bytes in the buffer. - */ -char *dword_to_hex_punct(char *out, guint32 dword, char punct); - -/** - * qword_to_hex() - * - * Output guint64 hex represetation to 'out', and return pointer after last character. - * It always output full representation (padded with 0). - * - * String is not NUL terminated by this routine. - * There needs to be at least 16 bytes in the buffer. - */ -char *qword_to_hex(char *out, guint64 qword); - -/** - * qword_to_hex_punct() - * - * Output guint64 hex represetation to 'out', and return pointer after last character. - * Each byte will be separated with punct character (cannot be NUL). - * It always output full representation (padded with 0). - * - * String is not NUL terminated by this routine. - * There needs to be at least 22 bytes in the buffer. - */ -char *qword_to_hex_punct(char *out, guint64 qword, char punct); - -#endif /* __TO_STR_INT_H__ */ diff --git a/epan/to_str.c b/epan/to_str.c index 565f3a5210..448bf6f9c7 100644 --- a/epan/to_str.c +++ b/epan/to_str.c @@ -18,7 +18,6 @@ #include #include "proto.h" #include "to_str.h" -#include "to_str-int.h" #include "strutil.h" #include #include @@ -31,100 +30,6 @@ */ #define BUF_TOO_SMALL_ERR "[Buffer too small]" -static inline char -low_nibble_of_octet_to_hex(guint8 oct) -{ - /* At least one version of Apple's C compiler/linker is buggy, causing - a complaint from the linker about the "literal C string section" - not ending with '\0' if we initialize a 16-element "char" array with - a 16-character string, the fact that initializing such an array with - such a string is perfectly legitimate ANSI C nonwithstanding, the 17th - '\0' byte in the string nonwithstanding. */ - static const gchar hex_digits[16] = - { '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - - return hex_digits[oct & 0xF]; -} - -static inline char * -byte_to_hex(char *out, guint32 dword) -{ - *out++ = low_nibble_of_octet_to_hex(dword >> 4); - *out++ = low_nibble_of_octet_to_hex(dword); - return out; -} - -char * -guint8_to_hex(char *out, guint8 val) -{ - return byte_to_hex(out, val); -} - -char * -word_to_hex(char *out, guint16 word) -{ - out = byte_to_hex(out, word >> 8); - out = byte_to_hex(out, word); - return out; -} - -char * -word_to_hex_punct(char *out, guint16 word, char punct) -{ - out = byte_to_hex(out, word >> 8); - *out++ = punct; - out = byte_to_hex(out, word); - return out; -} - -char * -word_to_hex_npad(char *out, guint16 word) -{ - if (word >= 0x1000) - *out++ = low_nibble_of_octet_to_hex((guint8)(word >> 12)); - if (word >= 0x0100) - *out++ = low_nibble_of_octet_to_hex((guint8)(word >> 8)); - if (word >= 0x0010) - *out++ = low_nibble_of_octet_to_hex((guint8)(word >> 4)); - *out++ = low_nibble_of_octet_to_hex((guint8)(word >> 0)); - return out; -} - -char * -dword_to_hex(char *out, guint32 dword) -{ - out = word_to_hex(out, dword >> 16); - out = word_to_hex(out, dword); - return out; -} - -char * -dword_to_hex_punct(char *out, guint32 dword, char punct) -{ - out = word_to_hex_punct(out, dword >> 16, punct); - *out++ = punct; - out = word_to_hex_punct(out, dword, punct); - return out; -} - -char * -qword_to_hex(char *out, guint64 qword) -{ - out = dword_to_hex(out, (guint32)(qword >> 32)); - out = dword_to_hex(out, (guint32)(qword & 0xffffffff)); - return out; -} - -char * -qword_to_hex_punct(char *out, guint64 qword, char punct) -{ - out = dword_to_hex_punct(out, (guint32)(qword >> 32), punct); - *out++ = punct; - out = dword_to_hex_punct(out, (guint32)(qword & 0xffffffff), punct); - return out; -} - static int guint32_to_str_buf_len(const guint32 u) { diff --git a/epan/to_str.h b/epan/to_str.h index 3ed13a0bc8..67de744232 100644 --- a/epan/to_str.h +++ b/epan/to_str.h @@ -136,39 +136,6 @@ WS_DLL_PUBLIC gchar* tvb_address_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, */ WS_DLL_PUBLIC gchar* tvb_address_var_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, address_type type, const gint offset, int length); -/** - * guint8_to_hex() - * - * Output guint8 hex represetation to 'out', and return pointer after last character (out + 4). - * It always output full representation (padded with 0). - * - * String is not NUL terminated by this routine. - * There needs to be at least 2 bytes in the buffer. - */ -WS_DLL_PUBLIC char *guint8_to_hex(char *out, guint8 val); - -/** - * word_to_hex() - * - * Output guint16 hex represetation to 'out', and return pointer after last character (out + 4). - * It always output full representation (padded with 0). - * - * String is not NUL terminated by this routine. - * There needs to be at least 4 bytes in the buffer. - */ -WS_DLL_PUBLIC char *word_to_hex(char *out, guint16 word); - -/** - * dword_to_hex() - * - * Output guint32 hex represetation to 'out', and return pointer after last character. - * It always output full representation (padded with 0). - * - * String is not NUL terminated by this routine. - * There needs to be at least 8 bytes in the buffer. - */ -WS_DLL_PUBLIC char *dword_to_hex(char *out, guint32 dword); - #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/wsutil/to_str.c b/wsutil/to_str.c index 29592575b3..cd035fe17d 100644 --- a/wsutil/to_str.c +++ b/wsutil/to_str.c @@ -80,6 +80,76 @@ byte_to_hex(char *out, guint32 dword) return out; } +char * +guint8_to_hex(char *out, guint8 val) +{ + return byte_to_hex(out, val); +} + +char * +word_to_hex(char *out, guint16 word) +{ + out = byte_to_hex(out, word >> 8); + out = byte_to_hex(out, word); + return out; +} + +char * +word_to_hex_punct(char *out, guint16 word, char punct) +{ + out = byte_to_hex(out, word >> 8); + *out++ = punct; + out = byte_to_hex(out, word); + return out; +} + +char * +word_to_hex_npad(char *out, guint16 word) +{ + if (word >= 0x1000) + *out++ = low_nibble_of_octet_to_hex((guint8)(word >> 12)); + if (word >= 0x0100) + *out++ = low_nibble_of_octet_to_hex((guint8)(word >> 8)); + if (word >= 0x0010) + *out++ = low_nibble_of_octet_to_hex((guint8)(word >> 4)); + *out++ = low_nibble_of_octet_to_hex((guint8)(word >> 0)); + return out; +} + +char * +dword_to_hex(char *out, guint32 dword) +{ + out = word_to_hex(out, dword >> 16); + out = word_to_hex(out, dword); + return out; +} + +char * +dword_to_hex_punct(char *out, guint32 dword, char punct) +{ + out = word_to_hex_punct(out, dword >> 16, punct); + *out++ = punct; + out = word_to_hex_punct(out, dword, punct); + return out; +} + +char * +qword_to_hex(char *out, guint64 qword) +{ + out = dword_to_hex(out, (guint32)(qword >> 32)); + out = dword_to_hex(out, (guint32)(qword & 0xffffffff)); + return out; +} + +char * +qword_to_hex_punct(char *out, guint64 qword, char punct) +{ + out = dword_to_hex_punct(out, (guint32)(qword >> 32), punct); + *out++ = punct; + out = dword_to_hex_punct(out, (guint32)(qword & 0xffffffff), punct); + return out; +} + /* * This does *not* null-terminate the string. It returns a pointer * to the position in the string following the last character it diff --git a/wsutil/to_str.h b/wsutil/to_str.h index 61a321fc77..b513f665d2 100644 --- a/wsutil/to_str.h +++ b/wsutil/to_str.h @@ -20,6 +20,97 @@ extern "C" { #endif /* __cplusplus */ +/** + * guint8_to_hex() + * + * Output guint8 hex represetation to 'out', and return pointer after last character (out + 4). + * It always output full representation (padded with 0). + * + * String is not NUL terminated by this routine. + * There needs to be at least 2 bytes in the buffer. + */ +WS_DLL_PUBLIC char *guint8_to_hex(char *out, guint8 val); + +/** + * word_to_hex() + * + * Output guint16 hex represetation to 'out', and return pointer after last character (out + 4). + * It always output full representation (padded with 0). + * + * String is not NUL terminated by this routine. + * There needs to be at least 4 bytes in the buffer. + */ +WS_DLL_PUBLIC char *word_to_hex(char *out, guint16 word); + +/** + * dword_to_hex() + * + * Output guint32 hex represetation to 'out', and return pointer after last character. + * It always output full representation (padded with 0). + * + * String is not NUL terminated by this routine. + * There needs to be at least 8 bytes in the buffer. + */ +WS_DLL_PUBLIC char *dword_to_hex(char *out, guint32 dword); + +/** + * word_to_hex_punct() + * + * Output guint16 hex represetation to 'out', and return pointer after last character. + * Each byte will be separated with punct character (cannot be NUL). + * It always output full representation (padded with 0). + * + * String is not NUL terminated by this routine. + * There needs to be at least 5 bytes in the buffer. + */ +WS_DLL_PUBLIC char *word_to_hex_punct(char *out, guint16 word, char punct); + +/** + * word_to_hex_npad() + * + * Output guint16 hex represetation to 'out', and return pointer after last character. + * Value is not padded. + * + * String is not NUL terminated by this routine. + * There needs to be at least 4 bytes in the buffer. + */ +WS_DLL_PUBLIC char *word_to_hex_npad(char *out, guint16 word); + +/** + * dword_to_hex_punct() + * + * Output guint32 hex represetation to 'out', and return pointer after last character. + * Each byte will be separated with punct character (cannot be NUL). + * It always output full representation (padded with 0). + * + * String is not NUL terminated by this routine. + * There needs to be at least 11 bytes in the buffer. + */ +WS_DLL_PUBLIC char *dword_to_hex_punct(char *out, guint32 dword, char punct); + +/** + * qword_to_hex() + * + * Output guint64 hex represetation to 'out', and return pointer after last character. + * It always output full representation (padded with 0). + * + * String is not NUL terminated by this routine. + * There needs to be at least 16 bytes in the buffer. + */ +WS_DLL_PUBLIC char *qword_to_hex(char *out, guint64 qword); + +/** + * qword_to_hex_punct() + * + * Output guint64 hex represetation to 'out', and return pointer after last character. + * Each byte will be separated with punct character (cannot be NUL). + * It always output full representation (padded with 0). + * + * String is not NUL terminated by this routine. + * There needs to be at least 22 bytes in the buffer. + */ +WS_DLL_PUBLIC char *qword_to_hex_punct(char *out, guint64 qword, char punct); + /** * bytes_to_hexstr() *