LCOV - code coverage report
Current view: top level - builds/gnutls/coverage/gnutls-git/lib/extras - hex.h (source / functions) Hit Total Coverage
Test: GnuTLS-3.6.14 Code Coverage Lines: 4 4 100.0 %
Date: 2020-10-30 04:50:48 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* CC0 (Public domain) - see LICENSE file for details */
       2             : 
       3             : #ifndef GNUTLS_LIB_EXTRAS_HEX_H
       4             : #define GNUTLS_LIB_EXTRAS_HEX_H
       5             : 
       6             : #include "config.h"
       7             : #include <stdbool.h>
       8             : #include <stdlib.h>
       9             : 
      10             : /**
      11             :  * hex_decode - Unpack a hex string.
      12             :  * @str: the hexadecimal string
      13             :  * @slen: the length of @str
      14             :  * @buf: the buffer to write the data into
      15             :  * @bufsize: the length of @buf
      16             :  *
      17             :  * Returns false if there are any characters which aren't 0-9, a-f or A-F,
      18             :  * of the string wasn't the right length for @bufsize.
      19             :  *
      20             :  * Example:
      21             :  *      unsigned char data[20];
      22             :  *
      23             :  *      if (!hex_decode(argv[1], strlen(argv[1]), data, 20))
      24             :  *              printf("String is malformed!\n");
      25             :  */
      26             : 
      27             : bool hex_decode(const char *str, size_t slen, void *buf, size_t bufsize);
      28             : 
      29             : /**
      30             :  * hex_encode - Create a nul-terminated hex string
      31             :  * @buf: the buffer to read the data from
      32             :  * @bufsize: the length of @buf
      33             :  * @dest: the string to fill
      34             :  * @destsize: the max size of the string
      35             :  *
      36             :  * Returns true if the string, including terminator, fit in @destsize;
      37             :  *
      38             :  * Example:
      39             :  *      unsigned char buf[] = { 0x1F, 0x2F };
      40             :  *      char str[5];
      41             :  *
      42             :  *      if (!hex_encode(buf, sizeof(buf), str, sizeof(str)))
      43             :  *              abort();
      44             :  */
      45             : bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize);
      46             : 
      47             : /**
      48             :  * hex_str_size - Calculate how big a nul-terminated hex string is
      49             :  * @bytes: bytes of data to represent
      50             :  *
      51             :  * Example:
      52             :  *      unsigned char buf[] = { 0x1F, 0x2F };
      53             :  *      char str[hex_str_size(sizeof(buf))];
      54             :  *
      55             :  *      hex_encode(buf, sizeof(buf), str, sizeof(str));
      56             :  */
      57        9480 : static inline size_t hex_str_size(size_t bytes)
      58             : {
      59        9480 :         return 2 * bytes + 1;
      60             : }
      61             : 
      62             : /**
      63             :  * hex_data_size - Calculate how many bytes of data in a hex string
      64             :  * @strlen: the length of the string (with or without NUL)
      65             :  *
      66             :  * Example:
      67             :  *      const char str[] = "1F2F";
      68             :  *      unsigned char buf[hex_data_size(sizeof(str))];
      69             :  *
      70             :  *      hex_decode(str, strlen(str), buf, sizeof(buf));
      71             :  */
      72        8091 : static inline size_t hex_data_size(size_t slen)
      73             : {
      74        8091 :         return slen / 2;
      75             : }
      76             : 
      77             : #endif /* GNUTLS_LIB_EXTRAS_HEX_H */

Generated by: LCOV version 1.14