1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #include "adbg_int.h"
7 
8 #include <stdlib.h>
9 
10 #include <ctype.h>
11 /*************************************************************************
12  * 2. Definition of external constants and variables
13  ************************************************************************/
14 
15 /*************************************************************************
16  * 3. File scope types, constants and variables
17  ************************************************************************/
18 
19 /*************************************************************************
20  * 4. Declaration of file local functions
21  ************************************************************************/
22 
23 /*************************************************************************
24  * 5. Definition of external functions
25  ************************************************************************/
26 
27 /******************************************************************************/
28 /*! @fn void Do_ADBG_Log( void* This_p, char* Format, ...)
29  * @brief
30  * @param [in] This_p
31  * @param [in] Format
32  * @param [in] ...
33  * @return void
34  */
35 /******************************************************************************/
Do_ADBG_Log(const char * const Format,...)36 void Do_ADBG_Log(const char *const Format, ...)
37 {
38 	va_list ap;
39 
40 	va_start(ap, Format);
41 	vprintf(Format, ap);
42 	printf("\n");
43 	va_end(ap);
44 	fflush(stdout);
45 }
46 
Do_ADBG_HexLog(const void * const Buf_p,const size_t Size,const size_t Cols)47 void Do_ADBG_HexLog(
48 	const void *const Buf_p,
49 	const size_t Size,
50 	const size_t Cols
51 	)
52 {
53 	const uint8_t *Data_p = Buf_p;
54 	size_t n = 0;
55 
56 	for (n = 0; n < Size; n += Cols) {
57 		char HexBuffer[ADBG_STRING_LENGTH_MAX];
58 		char AsciiBuffer[ADBG_STRING_LENGTH_MAX / 3];
59 		size_t m, NumCols;
60 
61 		(void)SecUtil_BufferToHex(Data_p + n, MIN(Cols, Size - n), NULL,
62 					  HexBuffer, sizeof(HexBuffer));
63 		NumCols = MIN(MIN(Cols, Size - n), sizeof(AsciiBuffer) - 1);
64 		for (m = 0; m < NumCols; m++) {
65 			int ch = Data_p[n + m];
66 
67 			if (isprint(ch))
68 				AsciiBuffer[m] = (char)ch;
69 			else
70 				AsciiBuffer[m] = '.';
71 		}
72 		AsciiBuffer[m] = '\0';
73 
74 		Do_ADBG_Log("  %-*s %s", (int)Cols * 3, HexBuffer, AsciiBuffer);
75 	}
76 }
77 
78 /*************************************************************************
79  * 6. Definitions of internal functions
80  ************************************************************************/
81