summaryrefslogtreecommitdiff
path: root/lib/str-unicode.c
blob: 20e9ca10bd54bc54fa9381907bf5b4787660bbca (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
/*
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS 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 library 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, see <http://www.gnu.org/licenses/>
 *
 */

#include "gnutls_int.h"
#include "errors.h"
#include "str.h"

#if defined(HAVE_LIBUNISTRING)
#include <uninorm.h>
#include <unistr.h>
#include <unictype.h>

uint8_t *_gnutls_normalize_u8_password(const uint8_t *password)
{
	size_t plen = strlen((char*)password);
	size_t ucs4_size = 0, nrm_size = 0;
	size_t out_size = 0;
	uint8_t *out = NULL;
	uint32_t *ucs4 = NULL;
	uint32_t *nrm = NULL;
	uint8_t *nrmu8 = NULL;
	unsigned i;

	if (plen == 0) {
		return (uint8_t*)strdup("");
	}

	/* check for invalid UTF-8 */
	if (u8_check((uint8_t*)password, plen) != NULL) {
		gnutls_assert();
		return NULL;
	}

	/* convert to UTF-32 */
	ucs4 = u8_to_u32((uint8_t*)password, plen, NULL, &ucs4_size);
	if (ucs4 == NULL) {
		gnutls_assert();
		goto fail;
	}

	/* convert all spaces to the ASCII-space */
	for (i=0;i<ucs4_size;i++) {
		if (uc_is_general_category(ucs4[i], UC_CATEGORY_Zs)) {
			ucs4[i] = 0x20;
		}
	}

	/* normalize to NFC */
	nrm = u32_normalize(UNINORM_NFC, ucs4, ucs4_size, NULL, &nrm_size);
	if (nrm == NULL) {
		gnutls_assert();
		goto fail;
	}

	/* convert back to UTF-8 */
	out_size = 0;
	nrmu8 = u32_to_u8(nrm, nrm_size, NULL, &out_size);
	if (nrmu8 == NULL) {
		gnutls_assert();
		goto fail;
	}

	/* copy to output with null terminator */
	out = gnutls_malloc(out_size+1);
	if (out == NULL) {
		gnutls_assert();
		goto fail;
	}

	memcpy(out, nrmu8, out_size);
	out[out_size] = 0;

	gnutls_free(ucs4);
	gnutls_free(nrm);
	gnutls_free(nrmu8);

	return out;

 fail:
	gnutls_free(out);
	gnutls_free(ucs4);
	gnutls_free(nrm);
	gnutls_free(nrmu8);
	return NULL;
}

uint8_t *_gnutls_normalize_u8_nfc(const uint8_t *str, size_t str_size)
{
	size_t nrm_size = 0;
	size_t out_size = 0;
	uint8_t *out = NULL;
	uint8_t *nrm = NULL;

	if (str_size == 0) {
		return (uint8_t*)strdup("");
	}

	/* check for invalid UTF-8 */
	if (u8_check(str, str_size) != NULL) {
		gnutls_assert();
		return NULL;
	}

	/* normalize to NFC */
	nrm = u8_normalize(UNINORM_NFC, str, str_size, NULL, &nrm_size);
	if (nrm == NULL) {
		gnutls_assert();
		goto fail;
	}

	out_size = nrm_size;

	/* copy to output with null terminator */
	out = gnutls_malloc(out_size+1);
	if (out == NULL) {
		gnutls_assert();
		goto fail;
	}

	memcpy(out, nrm, out_size);
	out[out_size] = 0;

	gnutls_free(nrm);

	return out;

 fail:
	gnutls_free(out);
	gnutls_free(nrm);
	return NULL;
}

#endif