1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* qmicli -- Command line interface to control QMI devices
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org>
*/
#include <stdio.h>
#include <string.h>
#include "qmicli-helpers.h"
gchar *
qmicli_get_raw_data_printable (const GArray *data,
gsize max_line_length,
const gchar *line_prefix)
{
gsize i;
gsize j;
gsize k;
gsize new_str_length;
gchar *new_str;
gsize prefix_len;
guint n_lines;
gboolean is_new_line;
g_return_val_if_fail (max_line_length >= 3, NULL);
if (!data)
return g_strdup ("");
/* Get new string length. If input string has N bytes, we need:
* - 1 byte for last NUL char
* - 2N bytes for hexadecimal char representation of each byte...
* - N-1 bytes for the separator ':'
* So... a total of (1+2N+N-1) = 3N bytes are needed... */
new_str_length = 3 * data->len;
/* Effective max line length needs to be multiple of 3, we don't want to
* split in half a given byte representation */
while (max_line_length % 3 != 0)
max_line_length--;
/* Prefix len includes the newline character plus the length of the input
* prefix */
prefix_len = strlen (line_prefix) + 1;
/* We don't consider the last NUL byte when counting lines to generate */
n_lines = (new_str_length - 1) / max_line_length;
if ((new_str_length - 1) % max_line_length != 0)
n_lines++;
/* Build new str length expected when we prefix the string and we limit the
* line length */
new_str_length += (n_lines * prefix_len);
/* Allocate memory for new array and initialize contents to NUL */
new_str = g_malloc0 (new_str_length);
/* Print hexadecimal representation of each byte... */
is_new_line = TRUE;
for (i = 0, j = 0, k = 0; i < data->len; i++) {
if (is_new_line) {
strcpy (&new_str[j], line_prefix);
j += strlen (line_prefix);
is_new_line = FALSE;
}
/* Print character in output string... */
snprintf (&new_str[j], 3, "%02X", g_array_index (data, guint8, i));
j+=2;
k+=2;
if (i != (data->len - 1) ) {
new_str[j] = ':';
j++;
k++;
}
if (k % max_line_length == 0 ||
i == (data->len -1)) {
new_str[j] = '\n';
j++;
is_new_line = TRUE;
}
}
/* Set output string */
return new_str;
}
|