1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #ifndef INCLUSION_GUARD_UTILS_HEX_H
7 #define INCLUSION_GUARD_UTILS_HEX_H
8 /*************************************************************************
9 * 1. Includes
10 *************************************************************************/
11 
12 /*************************************************************************
13 * 2. Types, constants and external variables
14 *************************************************************************/
15 
16 #if 0 /* Should format into a buffer instead */
17 /**
18  **************************************************************************
19  * Prints data hexadecimally to stdout
20  *
21  * Each line of binary data is printed as 66 characters on the format
22  * shown below:
23  * XX:XX:XX:XX XX:XX:XX:XX XX:XX:XX:XX XX:XX:XX:XX   xxxxxxxxxxxxxxxx
24  *
25  * @param [in] Prefix_p      String printed first each line, may be NULL
26  *                           to indicate the empty string
27  * @param [in] Postfix_p     String printed last each line, may be NULL
28  *                           to indicate the empty string
29  * @param [in] BufferName_p  Name of the buffer, if != NULL the name of the
30  *                           buffer is printed together with the length of
31  *                           the buffer before the data is printed
32  * @param [in] Buffer_p      Pointer to a buffer holding the data
33  * @param [in] BufferLength  Length of the buffer above
34  *
35  * @sigbased No
36  * @waitmode Wait mode
37  **************************************************************************/
38 void SecUtil_HexPrintBuffer(const char *Prefix_p, const char *Postfix_p,
39 			    const char *BufferName_p, const void *Buffer_p,
40 			    size_t BufferLength);
41 #endif
42 
43 #if 0
44 /**
45  **************************************************************************
46  * Prints data hexadecimally in a format suitable for initialization of
47  * an array in c.
48  *
49  * 0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,
50  * 0xXX,0xXX,
51  *
52  * @param [in] Prefix_p      String printed first each line, may be NULL
53  *                           to indicate the empty string
54  * @param [in] Postfix_p     String printed last each line, may be NULL
55  *                           to indicate the empty string
56  * @param [in] Buffer_p      Pointer to a buffer holding the data
57  * @param [in] BufferLength  Length of the buffer above
58  *
59  * @sigbased No
60  * @waitmode Wait mode
61  **************************************************************************/
62 void SecUtil_CHexPrintBuffer(const char *Prefix_p, const char *Postfix_p,
63 			     const void *Buffer_p, size_t BufferLength);
64 #endif
65 
66 /**************************************************************************
67 * Specifies how hexadecial data should be formated
68 *
69 * @param BytePrefix_p           ASCII string prefix for each hexadecimal
70 *                               byte printed (for example "0x"). If NULL
71 *                               nothing is inserted in front of a byte.
72 * @param ByteSeparator_p        ASCII string to insert between each printed
73 *                               hexadecimal byte. If NULL a ":" is inserted
74 *                               between each hexadecial byte.
75 * @param GroupSeparator_p       ASCII string to insert instead of
76 *                               ByteSeparator_p between each group
77 *                               of printed hexadecimal bytes. If NULL " "
78 *                               is used as group separator.
79 **************************************************************************/
80 typedef struct {
81 	const char *BytePrefix_p;
82 	const char *ByteSeparator_p;
83 	const char *GroupSeparator_p;
84 } SecUtil_HexFormat_t;
85 
86 /*************************************************************************
87 * 3. Functions
88 *************************************************************************/
89 
90 /****************************************************************************
91  * Formats data hexadecimally into an NULL terminated ASCII string in
92  * the format below.
93  *
94  * 0xXX:0xXX:0xXX:0xXX 0xXX:0xXX:0xXX:0xXX
95  * 0xXX:0xXX:0xXX:0xXX 0xXX:0xXX:0xXX:0xXX
96  *
97  * The colons and spaces in the result can be cusomized with ByteSeparator_p
98  * and GroupSeparator_p respectivly.
99  *
100  * @param [in]     Buffer_p          Pointer to a buffer holding the data
101  * @param [in]     BufferLength      Length of the buffer above
102  * @param [in]     HexFormat_p       How the data should be formatted
103  * @param [in, out] Destination_p     Output ASCII buffer
104  * @param [in]     DestinationLength Length of output buffer
105  *
106  * @returns  The size of a needed DestinationLength, if greater than supplied
107  *           DestinationLength the result in Destination_p is truncated but
108  *           still NULL terminated.
109  * @sigbased No
110  * @waitmode Wait mode
111  **************************************************************************/
112 
113 size_t SecUtil_BufferToHex(const void *const Buffer_p, size_t BufferLength,
114 			   const SecUtil_HexFormat_t *const HexFormat_p,
115 			   char *const Destination_p,
116 			   const size_t DestinationLength);
117 
118 #endif /*INCLUSION_GUARD_UTILS_HEX_H*/
119