Debian Codegraph @

src/common/mbim-common.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 /*
4 * Copyright (C) 2013 - 2014 Aleksander Morgado <[email protected]>
5 * Copyright (C) 2017 Google Inc.
6 */
7
8 #include <config.h>
9 #include <stdio.h>
10
11 #include "mbim-common.h"
12
13 /*****************************************************************************/
14
15 gchar *
16 mbim_common_str_hex (gconstpointer mem,
17 gsize size,
18 gchar delimiter)
19 {
20 const guint8 *data = mem;
21 gsize i;
22 gsize j;
23 gsize new_str_length;
24 gchar *new_str;
25
26 /* Get new string length. If input string has N bytes, we need:
27 * - 1 byte for last NUL char
28 * - 2N bytes for hexadecimal char representation of each byte...
29 * - N-1 bytes for the separator ':'
30 * So... a total of (1+2N+N-1) = 3N bytes are needed... */
31 new_str_length = 3 * size;
32
33 /* Allocate memory for new array and initialize contents to NUL */
34 new_str = g_malloc0 (new_str_length);
35
36 /* Print hexadecimal representation of each byte... */
37 for (i = 0, j = 0; i < size; i++, j += 3) {
38 /* Print character in output string... */
39 snprintf (&new_str[j], 3, "%02X", data[i]);
40 /* And if needed, add separator */
41 if (i != (size - 1) )
42 new_str[j + 2] = delimiter;
43 }
44
45 /* Set output string */
46 return new_str;
47 }