summaryrefslogtreecommitdiff
path: root/egg/egg-hex.c
blob: f4d0f18b686979fc9a10538c1821a2457a9322e0 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * gnome-keyring
 *
 * Copyright (C) 2008 Stefan Walter
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 *
 * Author: Stef Walter <stefw@thewalter.net>
 */

#include "config.h"

#include "egg-hex.h"

#include <string.h>

static const char HEXC_UPPER[] = "0123456789ABCDEF";
static const char HEXC_LOWER[] = "0123456789abcdef";

gpointer
egg_hex_decode (const gchar *data, gssize n_data, gsize *n_decoded)
{
	return egg_hex_decode_full (data, n_data, 0, 1, n_decoded);
}

gpointer
egg_hex_decode_full (const gchar *data,
                     gssize n_data,
                     const gchar *delim,
                     guint group,
                     gsize *n_decoded)
{
	guchar *result;
	guchar *decoded;
	gsize n_delim;
	gushort j;
	gint state = 0;
	gint part = 0;
	const gchar* pos;

	g_return_val_if_fail (data || !n_data, NULL);
	g_return_val_if_fail (n_decoded, NULL);
	g_return_val_if_fail (group >= 1, NULL);

	if (n_data == -1)
		n_data = strlen (data);
	n_delim = delim ? strlen (delim) : 0;
	decoded = result = g_malloc0 ((n_data / 2) + 1);
	*n_decoded = 0;

	while (n_data > 0 && state == 0) {

		if (decoded != result && delim) {
			if (n_data < n_delim || memcmp (data, delim, n_delim) != 0) {
				state = -1;
				break;
			}

			data += n_delim;
			n_data -= n_delim;
		}

		while (part < group && n_data > 0) {

			/* Find the position */
			pos = strchr (HEXC_UPPER, g_ascii_toupper (*data));
			if (pos == 0) {
				if (n_data > 0)
					state = -1;
				break;
			}

			j = pos - HEXC_UPPER;
			if(!state) {
				*decoded = (j & 0xf) << 4;
				state = 1;
			} else {
				*decoded |= (j & 0xf);
				(*n_decoded)++;
				decoded++;
				state = 0;
				part++;
			}

			++data;
			--n_data;
		}

		part = 0;
	}

	/* Parsing error */
	if (state != 0) {
		g_free (result);
		result = NULL;
	}

	return result;
}

gchar*
egg_hex_encode (gconstpointer data, gsize n_data)
{
	return egg_hex_encode_full (data, n_data, TRUE, NULL, 0);
}

gchar*
egg_hex_encode_full (gconstpointer data,
                     gsize n_data,
                     gboolean upper_case,
                     const gchar *delim,
                     guint group)
{
	GString *result;
	const gchar *input;
	const char *hexc;
	gsize bytes;
	guchar j;

	g_return_val_if_fail (data || !n_data, NULL);

	input = data;
	hexc = upper_case ? HEXC_UPPER : HEXC_LOWER;

	result = g_string_sized_new (n_data * 2 + 1);
	bytes = 0;

	while (n_data > 0) {

		if (delim && group && bytes && (bytes % group) == 0)
			g_string_append (result, delim);

		j = *(input) >> 4 & 0xf;
		g_string_append_c (result, hexc[j]);

		j = *(input++) & 0xf;
		g_string_append_c (result, hexc[j]);

		++bytes;
		--n_data;
	}

	/* Make sure still null terminated */
	return g_string_free (result, FALSE);
}