File: LibIDN2.pm

package info (click to toggle)
libnet-libidn2-perl 1.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 132 kB
  • sloc: perl: 96; makefile: 3
file content (388 lines) | stat: -rw-r--r-- 11,278 bytes parent folder | download
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# vim:set tabstop=4 shiftwidth=4 noexpandtab:

package Net::LibIDN2;

use 5.006;
use strict;
use warnings;

require Exporter;
require DynaLoader;

our @ISA = qw(Exporter DynaLoader);

our $VERSION = '1.02';
bootstrap Net::LibIDN2 $VERSION;

our %EXPORT_TAGS = ( 'all' => [ qw(
	idn2_strerror
	idn2_strerror_name
	idn2_check_version
	idn2_lookup_u8
	idn2_lookup_ul
	idn2_register_u8
	idn2_register_ul
	idn2_to_ascii_8
	idn2_to_ascii_l
	idn2_to_unicode_88
	idn2_to_unicode_8l
	idn2_to_unicode_ll
	idn2_to_unicode_u8
	idn2_to_unicode_ul
	IDN2_OK
	IDN2_MALLOC
	IDN2_NO_CODESET
	IDN2_ICONV_FAIL
	IDN2_ENCODING_ERROR
	IDN2_NFC
	IDN2_PUNYCODE_BAD_INPUT
	IDN2_PUNYCODE_BIG_OUTPUT
	IDN2_PUNYCODE_OVERFLOW
	IDN2_TOO_BIG_DOMAIN
	IDN2_TOO_BIG_LABEL
	IDN2_INVALID_ALABEL
	IDN2_UALABEL_MISMATCH
	IDN2_INVALID_FLAGS
	IDN2_NOT_NFC
	IDN2_2HYPHEN
	IDN2_HYPHEN_STARTEND
	IDN2_LEADING_COMBINING
	IDN2_DISALLOWED
	IDN2_CONTEXTJ
	IDN2_CONTEXTJ_NO_RULE
	IDN2_CONTEXTO
	IDN2_CONTEXTO_NO_RULE
	IDN2_UNASSIGNED
	IDN2_BIDI
	IDN2_DOT_IN_LABEL
	IDN2_INVALID_TRANSITIONAL
	IDN2_INVALID_NONTRANSITIONAL
	IDN2_VERSION
	IDN2_VERSION_NUMBER
	IDN2_VERSION_MAJOR
	IDN2_VERSION_MINOR
	IDN2_VERSION_PATCH
	IDN2_LABEL_MAX_LENGTH
	IDN2_DOMAIN_MAX_LENGTH
	IDN2_NFC_INPUT
	IDN2_ALABEL_ROUNDTRIP
	IDN2_TRANSITIONAL
	IDN2_NONTRANSITIONAL
	IDN2_ALLOW_UNASSIGNED
	IDN2_USE_STD3_ASCII_RULES
) ] );

if (idn2_check_version("2.0.5")) {
	push @{$EXPORT_TAGS{all}}, "IDN2_NO_TR46";
}
if (idn2_check_version("2.2.0")) {
	push @{$EXPORT_TAGS{all}}, "IDN2_ALABEL_ROUNDTRIP_FAILED";
	push @{$EXPORT_TAGS{all}}, "IDN2_NO_ALABEL_ROUNDTRIP";
}

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
	IDN2_VERSION
	IDN2_VERSION_NUMBER
	IDN2_VERSION_MAJOR
	IDN2_VERSION_MINOR
	IDN2_VERSION_PATCH
	IDN2_LABEL_MAX_LENGTH
	IDN2_DOMAIN_MAX_LENGTH
	IDN2_NFC_INPUT
	IDN2_ALABEL_ROUNDTRIP
	IDN2_TRANSITIONAL
	IDN2_NONTRANSITIONAL
	IDN2_ALLOW_UNASSIGNED
	IDN2_USE_STD3_ASCII_RULES
);

if (idn2_check_version("2.0.5")) {
	push @EXPORT, "IDN2_NO_TR46";
}
if (idn2_check_version("2.2.0")) {
	push @EXPORT, "IDN2_NO_ALABEL_ROUNDTRIP";
}

1;
__END__

=encoding utf8

=head1 NAME

Net::LibIDN2 - Perl bindings for GNU Libidn2

=head1 SYNOPSIS

  use Net::LibIDN2 ':all';
  use Encode;

  idn2_lookup_u8(Encode::encode_utf8("m\N{U+00FC}\N{U+00DF}li.de"))
    eq 'xn--mli-5ka8l.de';

  idn2_register_u8(
    Encode::encode_utf8("m\N{U+00FC}\N{U+00DF}li"),
    "xn--mli-5ka8l"
  ) eq 'xn--mli-5ka8l';

  Encode::decode_utf8(idn2_to_unicode_88("xn--mli-5ka8l.de"))
    eq "m\N{U+00FC}\N{U+00DF}li.de"

=head1 DESCRIPTION

Provides bindings for GNU Libidn2, a C library for handling internationalized
domain names based on IDNA 2008, Punycode and TR46. 

=head2 Functions

=over 4

=item B<Net::LibIDN2::idn2_lookup_u8>(I<$src>, [I<$flags>, [I<$rc>]]);

Alternative name idn2_to_ascii_8.

Perform IDNA2008 lookup string conversion on domain name $I<src>, as described in 
section 5 of RFC 5891. Note that the input string must be encoded in UTF-8 and
be in Unicode NFC form.

Pass B<IDN2_NFC_INPUT> in I<$flags> to convert input to NFC form before further
processing. IDN2_TRANSITIONAL and IDN2_NONTRANSITIONAL do already imply IDN2_NFC_INPUT.

Pass B<IDN2_ALABEL_ROUNDTRIP> in flags to convert any input A-labels
to U-labels and perform additional testing. This is default if used with a libidn2
version >= 2.2. To switch this behavior off, pass IDN2_NO_ALABEL_ROUNDTRIP.

Pass IDN2_TRANSITIONAL to enable Unicode TR46 transitional processing, and
IDN2_NONTRANSITIONAL to enable Unicode TR46 non-transitional processing. 

Multiple flags may be specified by binary or:ing them together, for example
B<IDN2_NFC_INPUT> | B<IDN2_ALABEL_ROUNDTRIP>.

If linked to library GNU Libidn2 version > 2.0.3: IDN2_USE_STD3_ASCII_RULES disabled by default.
Previously we were eliminating non-STD3 characters from domain strings such as
_443._tcp.example.com, or IPs 1.2.3.4/24 provided to libidn2 functions.
That was an unexpected regression for applications switching from libidn2
and thus it is no longer applied by default. Use IDN2_USE_STD3_ASCII_RULES
to enable that behavior again.

On error, returns undef. If a scalar variable is provided in I<$rc>, 
returns the internal libidn2 C library result code as well.

=item B<Net::LibIDN2::idn2_lookup_ul>(I<$src>, [I<$flags>, [I<$rc>]]);

Alternative name idn2_to_ascii_l.

Similar to function C<idn2_lookup_u8> but I<$src> is assumed to be encoded in 
the locale's default coding system, and will be transcoded to UTF-8 and NFC 
normalized before returning the result.

=item B<Net::LibIDN2::idn2_register_u8>(I<$ulabel>, [I<$alabel>, [I<$flags>, [I<$rc>]]]);

Perform IDNA2008 register string conversion on domain label I<$ulabel> and I<$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 B<IDN2_NFC_INPUT> in I<$flags> to convert input I<$ulabel> to NFC form before
further processing.

It is recommended to supply both I<$ulabel> and I<$alabel> for better error checking,
but supplying just one of them will work. Passing in only I<$alabel> is better than
only I<$ulabel>. See RFC 5891 section 4 for more information.

On error, returns undef. If a scalar variable is provided in I<$rc>, 
returns the internal libidn2 C library result code as well.

=item B<Net::LibIDN2::idn2_register_ul>(I<$ulabel>, [I<$alabel>, [I<$flags>, [I<$rc>]]]);

Similar to function C<idn2_register_u8> but I<$ulabel> is assumed to be encoded in 
the locale's default coding system, and will be transcoded to UTF-8 and NFC 
normalized before returning the result.

=item B<Net::LibIDN2::idn2_to_unicode_88>(I<$input>, [I<$flags>, [I<$rc>]]);

Converts a possibly ACE encoded domain name in UTF-8 format into a
an UTF-8 encoded string (punycode decoding).

On error, returns undef. If a scalar variable is provided in I<$rc>, 
returns the internal libidn2 C library result code as well.

=item B<Net::LibIDN2::idn2_to_unicode_8l>(I<$input>, [I<$flags>, [I<$rc>]]);

Similar to function C<idn2_to_unicode_88> but the return value is encoded in 
the locale's default coding system.

=item B<Net::LibIDN2::idn2_to_unicode_ll>(I<$input>, [I<$flags>, [I<$rc>]]);

Similar to function C<idn2_to_unicode_8l> but I<$input> is also assumed to be encoded in 
the locale's default coding system.

=item B<Net::LibIDN2::idn2_strerror>(I<$rc>);

Convert internal libidn2 error code I<$rc> to a human-readable string.

=item B<Net::LibIDN2::idn2_strerror_name>(I<$rc>);

Convert internal libidn2 error code I<$rc> to a string corresponding to
internal header file symbols names like IDN2_MALLOC.

=item B<Net::LibIDN2::idn2_check_version>([I<$req_version>])

Checks that the version of the underlying IDN2 C library is at minimum
the one given as a string in I<$req_version> and if that is the case
returns the actual version string  of the underlying C library or undef
if the condition is not met. If no parameter is passed to this function
no check is done and only the version  string is returned.

See B<IDN2_VERSION> for a suitable I<$req_version> string, it corresponds
to  the idn2.h C header file version at compile time of this Perl module.
Normally these two version numbers match, but if you compiled this Perl
module against an older libidn2  and then run it with a newer libidn2
shared library, they will be different.

=back

=head2 Constants

=over 4

=item B<IDN2_VERSION>

Pre-processor symbol with a string that describes the C header file version
number at compile time of this Perl module. Used together with idn2_check_version()
to verify header file and run-time library consistency.

=item B<IDN2_VERSION_NUMBER>

Pre-processor symbol with a hexadecimal value describing the C header file
version number at compile time of this Perl module. For example, when the header 
version is 1.2.4711 this symbol will have the value 0x01021267. The last four
digits are used to enumerate development snapshots, but for all public releases
they will be 0000.

=item B<IDN2_VERSION_MAJOR>

Pre-processor symbol for the major version number (decimal).
The version scheme is major.minor.patchlevel.

=item B<IDN2_VERSION_MINOR>

Pre-processor symbol for the minor version number (decimal).
The version scheme is major.minor.patchlevel.

=item B<IDN2_VERSION_PATCH>

Pre-processor symbol for the patch level number (decimal).
The version scheme is major.minor.patchlevel.

=item B<IDN2_LABEL_MAX_LENGTH>

Constant specifying the maximum length of a DNS label to 63 characters,
as specified in RFC 1034.

=item B<IDN2_DOMAIN_MAX_LENGTH>

Constant specifying the maximum size of the wire encoding of a DNS domain to 255
characters, as specified in RFC 1034. Note that the usual printed representation
of a domain name is limited to 253 characters if it does not end with a period
or 254 characters if it ends with a period. 

=back

=head2 Result codes

=over 4

=item B<"Net::LibIDN2::IDN2_OK">
Successful return.

=item B<"Net::LibIDN2::IDN2_MALLOC">
Memory allocation error.

=item B<"Net::LibIDN2::IDN2_NO_CODESET">
Could not determine locale string encoding format.

=item B<"Net::LibIDN2::IDN2_ICONV_FAIL">
Could not transcode locale string to UTF-8.

=item B<"Net::LibIDN2::IDN2_ENCODING_ERROR">
Unicode data encoding error.

=item B<"Net::LibIDN2::IDN2_NFC">
Error normalizing string.

=item B<"Net::LibIDN2::IDN2_PUNYCODE_BAD_INPUT">
Punycode invalid input.

=item B<"Net::LibIDN2::IDN2_PUNYCODE_BIG_OUTPUT">
Punycode output buffer too small.

=item B<"Net::LibIDN2::IDN2_PUNYCODE_OVERFLOW">
Punycode conversion would overflow.

=item B<"Net::LibIDN2::IDN2_TOO_BIG_DOMAIN">
Domain name longer than 255 characters.

=item B<"Net::LibIDN2::IDN2_TOO_BIG_LABEL">
Domain label longer than 63 characters.

=item B<"Net::LibIDN2::IDN2_INVALID_ALABEL">
Input A-label is not valid.

=item B<"Net::LibIDN2::IDN2_UALABEL_MISMATCH">
Input A-label and U-label does not match.

=item B<"Net::LibIDN2::IDN2_NOT_NFC">
String is not NFC.

=item B<"Net::LibIDN2::IDN2_2HYPHEN">
String has forbidden two hyphens.

=item B<"Net::LibIDN2::IDN2_HYPHEN_STARTEND">
String has forbidden starting/ending hyphen.

=item B<"Net::LibIDN2::IDN2_LEADING_COMBINING">
String has forbidden leading combining character.

=item B<"Net::LibIDN2::IDN2_DISALLOWED">
String has disallowed character.

=item B<"Net::LibIDN2::IDN2_CONTEXTJ">
String has forbidden context-j character.

=item B<"Net::LibIDN2::IDN2_CONTEXTJ_NO_RULE">
String has context-j character with no rule.

=item B<"Net::LibIDN2::IDN2_CONTEXTO">
String has forbidden context-o character.

=item B<"Net::LibIDN2::IDN2_CONTEXTO_NO_RULE">
String has context-o character with no rule.

=item B<"Net::LibIDN2::IDN2_UNASSIGNED">
String has forbidden unassigned character.

=item B<"Net::LibIDN2::IDN2_BIDI">
String has forbidden bi-directional properties.

=item B<"Net::LibIDN2::IDN2_DOT_IN_LABEL">
Label has forbidden dot (TR46).

=item B<"Net::LibIDN2::IDN2_INVALID_TRANSITIONAL">
Label has character forbidden in transitional mode (TR46).

=item B<"Net::LibIDN2::IDN2_INVALID_NONTRANSITIONAL">
Label has character forbidden in non-transitional mode (TR46).

=back

=head1 AUTHOR

Thomas Jacob, https://github.com/gnuthor

=head1 SEE ALSO

perl(1), RFC 5890-5893, TR 46, https://gitlab.com/libidn/libidn2.

=cut