File: register.c

package info (click to toggle)
libidn2 2.3.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,768 kB
  • sloc: ansic: 6,049; sh: 1,480; makefile: 499; xml: 50; perl: 15
file content (262 lines) | stat: -rw-r--r-- 7,534 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* register.c - implementation of IDNA2008 register functions
   Copyright (C) 2011-2025 Simon Josefsson

   Libidn2 is free software: you can redistribute it and/or modify it
   under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version.

   or

     * 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.

   or both in parallel, as here.

   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 copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include "idn2.h"

#include <errno.h>		/* errno */
#include <stdlib.h>		/* free */

#include <unitypes.h>
#include <uniconv.h>		/* u8_strconv_from_locale */
#include <unistr.h>		/* u32_to_u8 */

#include "idna.h"		/* _idn2_label_test */

/**
 * idn2_register_u8:
 * @ulabel: input zero-terminated UTF-8 and Unicode NFC string, or NULL.
 * @alabel: input zero-terminated ACE encoded string (xn--), or NULL.
 * @insertname: newly allocated output variable with name to register in DNS.
 * @flags: optional #idn2_flags to modify behaviour.
 *
 * Perform IDNA2008 register string conversion on domain label @ulabel
 * and @alabel, as described in section 4 of RFC 5891.  Note that the
 * input @ulabel must be encoded in UTF-8 and be in Unicode NFC form.
 *
 * Pass %IDN2_NFC_INPUT in @flags to convert input @ulabel to NFC form
 * before further processing.
 *
 * It is recommended to supply both @ulabel and @alabel for better
 * error checking, but supplying just one of them will work.  Passing
 * in only @alabel is better than only @ulabel.  See RFC 5891 section
 * 4 for more information.
 *
 * After version 0.11: @insertname may be NULL to test conversion of @src
 * without allocating memory.
 *
 * Returns: On successful conversion %IDN2_OK is returned, when the
 *   given @ulabel and @alabel does not match each other
 *   %IDN2_UALABEL_MISMATCH is returned, when either of the input
 *   labels are too long %IDN2_TOO_BIG_LABEL is returned, when @alabel
 *   does does not appear to be a proper A-label %IDN2_INVALID_ALABEL
 *   is returned, or another error code is returned.
 **/
int
idn2_register_u8 (const uint8_t *ulabel, const uint8_t *alabel,
		  uint8_t **insertname, int flags)
{
  int rc;

  if (ulabel == NULL && alabel == NULL)
    {
      if (insertname)
	*insertname = NULL;
      return IDN2_OK;
    }

  if (alabel)
    {
      size_t alabellen = strlen ((char *) alabel), u32len =
	IDN2_LABEL_MAX_LENGTH * 4;
      uint32_t u32[IDN2_DOMAIN_MAX_LENGTH * 4];
      uint8_t *tmp;
      uint8_t u8[IDN2_DOMAIN_MAX_LENGTH + 1];
      size_t u8len;

      if (alabellen > IDN2_LABEL_MAX_LENGTH)
	return IDN2_TOO_BIG_LABEL;

      if (alabellen <= 4)
	return IDN2_INVALID_ALABEL;
      if (alabel[0] != 'x'
	  || alabel[1] != 'n' || alabel[2] != '-' || alabel[3] != '-')
	return IDN2_INVALID_ALABEL;

      if (!_idn2_ascii_p (alabel, alabellen))
	return IDN2_INVALID_ALABEL;

      rc = idn2_punycode_decode ((char *) alabel + 4, alabellen - 4,
				 u32, &u32len);
      if (rc != IDN2_OK)
	return rc;

      u8len = sizeof (u8);
      if (u32_to_u8 (u32, u32len, u8, &u8len) == NULL)
	return IDN2_ENCODING_ERROR;
      u8[u8len] = '\0';

      if (ulabel)
	{
	  if (strcmp ((char *) ulabel, (char *) u8) != 0)
	    return IDN2_UALABEL_MISMATCH;
	}

      rc = idn2_register_u8 (u8, NULL, &tmp, 0);
      if (rc != IDN2_OK)
	return rc;

      rc = strcmp ((char *) alabel, (char *) tmp);
      free (tmp);
      if (rc != 0)
	return IDN2_UALABEL_MISMATCH;

      if (insertname)
	{
	  uint8_t *m = (uint8_t *) strdup ((char *) alabel);
	  if (!m)
	    return IDN2_MALLOC;

	  *insertname = m;
	}
    }
  else				/* ulabel only */
    {
      size_t ulabellen = u8_strlen (ulabel);
      uint32_t *u32;
      size_t u32len;
      size_t tmpl;
      uint8_t tmp[IDN2_LABEL_MAX_LENGTH + 1];

      if (_idn2_ascii_p (ulabel, ulabellen))
	{
	  if (ulabellen > IDN2_LABEL_MAX_LENGTH)
	    return IDN2_TOO_BIG_LABEL;

	  if (insertname)
	    {
	      uint8_t *m = (uint8_t *) strdup ((char *) ulabel);
	      if (!m)
		return IDN2_MALLOC;
	      *insertname = m;
	    }
	  return IDN2_OK;
	}

      rc = _idn2_u8_to_u32_nfc (ulabel, ulabellen, &u32, &u32len,
				flags & IDN2_NFC_INPUT);
      if (rc != IDN2_OK)
	return rc;

      rc = _idn2_label_test (TEST_NFC
			     | TEST_DISALLOWED
			     | TEST_UNASSIGNED
			     | TEST_2HYPHEN
			     | TEST_HYPHEN_STARTEND
			     | TEST_LEADING_COMBINING
			     | TEST_CONTEXTJ_RULE
			     | TEST_CONTEXTO_RULE | TEST_BIDI, u32, u32len);
      if (rc != IDN2_OK)
	{
	  free (u32);
	  return rc;
	}

      tmp[0] = 'x';
      tmp[1] = 'n';
      tmp[2] = '-';
      tmp[3] = '-';

      tmpl = IDN2_LABEL_MAX_LENGTH - 4;
      rc = idn2_punycode_encode (u32, u32len, (char *) tmp + 4, &tmpl);
      free (u32);
      if (rc != IDN2_OK)
	return rc;

      tmp[4 + tmpl] = '\0';

      if (insertname)
	{
	  uint8_t *m = (uint8_t *) strdup ((char *) tmp);
	  if (!m)
	    return IDN2_MALLOC;
	  *insertname = m;
	}
    }

  return IDN2_OK;
}

/**
 * idn2_register_ul:
 * @ulabel: input zero-terminated locale encoded string, or NULL.
 * @alabel: input zero-terminated ACE encoded string (xn--), or NULL.
 * @insertname: newly allocated output variable with name to register in DNS.
 * @flags: optional #idn2_flags to modify behaviour.
 *
 * Perform IDNA2008 register string conversion on domain label @ulabel
 * and @alabel, as described in section 4 of RFC 5891.  Note that the
 * input @ulabel is assumed to be encoded in the locale's default
 * coding system, and will be transcoded to UTF-8 and NFC normalized
 * by this function.
 *
 * It is recommended to supply both @ulabel and @alabel for better
 * error checking, but supplying just one of them will work.  Passing
 * in only @alabel is better than only @ulabel.  See RFC 5891 section
 * 4 for more information.
 *
 * After version 0.11: @insertname may be NULL to test conversion of @src
 * without allocating memory.
 *
 * Returns: On successful conversion %IDN2_OK is returned, when the
 *   given @ulabel and @alabel does not match each other
 *   %IDN2_UALABEL_MISMATCH is returned, when either of the input
 *   labels are too long %IDN2_TOO_BIG_LABEL is returned, when @alabel
 *   does does not appear to be a proper A-label %IDN2_INVALID_ALABEL
 *   is returned, when @ulabel locale to UTF-8 conversion failed
 *   %IDN2_ICONV_FAIL is returned, or another error code is returned.
 **/
int
idn2_register_ul (const char *ulabel, const char *alabel,
		  char **insertname, int flags)
{
  uint8_t *utf8ulabel = NULL;
  int rc;

  if (ulabel)
    {
      const char *encoding = locale_charset ();

      utf8ulabel = u8_strconv_from_encoding (ulabel, encoding, iconveh_error);

      if (utf8ulabel == NULL)
	{
	  if (errno == ENOMEM)
	    return IDN2_MALLOC;
	  return IDN2_ICONV_FAIL;
	}
    }

  rc = idn2_register_u8 (utf8ulabel, (const uint8_t *) alabel,
			 (uint8_t **) insertname, flags | IDN2_NFC_INPUT);

  free (utf8ulabel);

  return rc;
}