From a4175d4cce037c0d4c4944fd75dc99b9356d6e54 Mon Sep 17 00:00:00 2001 From: "7u83@mail.ru" <7u83@mail.ru@noemail.net> Date: Mon, 6 Apr 2015 12:32:37 +0000 Subject: [PATCH] Format functions. FossilOrigin-Name: 4dbb9fb2ec4da95253efd52af964cce008771e14a70099511fcb05877e9e0242 --- src/capwap/format.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/capwap/format.h | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/capwap/format.c create mode 100644 src/capwap/format.h diff --git a/src/capwap/format.c b/src/capwap/format.c new file mode 100644 index 00000000..77b60d6e --- /dev/null +++ b/src/capwap/format.c @@ -0,0 +1,56 @@ +/* + This file is part of libcapwap. + + libcapwap is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libcapwap is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Foobar. If not, see . + +*/ +/** + * @file + * @brief Implementation of various format functions. + */ + +#include +#include + +/** + * Format bytes as hex string. + * @param dst destination buffer + * @param format format to use when writing a byte. + * @param delim delimiter + * @param src bytes to format + * @param len number of bytes to format + * @return the number character written to dst + * + * This function is used by macros like #cw_format_hexl, #cw_format_hex ... \n + * The size of the destination buffer must be at least x * len + strlen(delim) * (len-1) +1, + * where x is the number of characters a formatted hex byte needs (typically 2). + * + */ +int cw_format_hex_bytes(char *dst, const char *format, const char *delim, + const uint8_t * src, int len) +{ + char *d = dst; + int i; + const char *cdelim = NULL; + for (i = 0; i < len; i++) { + if (cdelim) + d += sprintf(d, "%s", cdelim); + else + cdelim = delim; + + d += sprintf(d, format, src[i]); + + } + return d - dst; +} diff --git a/src/capwap/format.h b/src/capwap/format.h new file mode 100644 index 00000000..a1d49cf3 --- /dev/null +++ b/src/capwap/format.h @@ -0,0 +1,57 @@ +/* + This file is part of libcapwap. + + libcapwap is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libcapwap is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Foobar. If not, see . + +*/ +/** + *@file + *@brief prototypes for format-functions + */ + +#ifndef __CW_FORMAT_H +#define __CW_FORMAT_H + + +extern int cw_format_hex_bytes(char *dst, const char *format, const char *delim, + const uint8_t * src, int len); + + +/** + * Format bytes as a hex string. Hexadecimal letters are lower-case. + * @param dst destination buffer + * @param bytes bytes to format + * @param len number of bytes to format + * @return number of characters written to the destination buffer + * + * Make sure, the destination buffer can hold at least len * 2 characters + the trailing + * zero for strings. + */ +#define cw_format_hexl(dst,bytes,len)\ + cw_format_hex_bytes(dst,"%02x","",bytes,len) + +/** + * Format bytes as hex string. Same as #cw_format_hexl, but + * hexadecimal letters are upper-case. + */ +#define cw_format_hexu\ + cw_format_hex_bytes(dst,"%02X","",bytes,len) + +/** + * Alias for #cw_format_hexl. + */ +#define cw_format_hex cw_format_hexl + + +#endif