1//! Crate wrapping what we need from ICU’s C API for JIDs.
2//!
3//! See <http://site.icu-project.org/>
4
5use std::os::raw::c_char;
6
7// From unicode/umachine.h
8pub(crate) type UChar = u16;
9
10// From unicode/utypes.h
11pub(crate) type UErrorCode = u32;
12pub(crate) const U_ZERO_ERROR: UErrorCode = 0;
13
14pub(crate) type UStringPrepProfile = u32;
15type UParseError = u32;
16
17// From unicode/usprep.h
18pub(crate) const USPREP_DEFAULT: i32 = 0;
19pub(crate) const USPREP_ALLOW_UNASSIGNED: i32 = 1;
20
21pub(crate) type UStringPrepProfileType = u32;
22pub(crate) const USPREP_RFC3491_NAMEPREP: UStringPrepProfileType = 0;
23pub(crate) const USPREP_RFC3920_NODEPREP: UStringPrepProfileType = 7;
24pub(crate) const USPREP_RFC3920_RESOURCEPREP: UStringPrepProfileType = 8;
25pub(crate) const USPREP_RFC4013_SASLPREP: UStringPrepProfileType = 10;
26
27// From unicode/utrace.h
28type UTraceLevel = i32;
29pub(crate) const UTRACE_VERBOSE: UTraceLevel = 9;
30
31// From unicode/uidna.h
32#[repr(C)]
33pub(crate) struct UIDNA {
34 _unused: [u8; 0],
35}
36type UBool = i8;
37
38#[repr(C)]
39pub(crate) struct UIDNAInfo {
40 size: i16,
41 is_transitional_different: UBool,
42 reserved_b3: UBool,
43 errors: u32,
44 reserved_i2: i32,
45 reserved_i3: i32,
46}
47
48impl UIDNAInfo {
49 pub(crate) fn new() -> UIDNAInfo {
50 assert_eq!(std::mem::size_of::<UIDNAInfo>(), 16);
51 UIDNAInfo {
52 size: std::mem::size_of::<UIDNAInfo>() as i16,
53 is_transitional_different: false as UBool,
54 reserved_b3: false as UBool,
55 errors: 0,
56 reserved_i2: 0,
57 reserved_i3: 0,
58 }
59 }
60
61 // TODO: Return a String instead, or a custom error type, this is a bitflag (defined in
62 // uidna.h) where multiple errors can be accumulated.
63 pub(crate) fn get_errors(&self) -> u32 {
64 self.errors
65 }
66}
67
68pub(crate) const UIDNA_DEFAULT: u32 = 0;
69pub(crate) const UIDNA_USE_STD3_RULES: u32 = 2;
70
71pub(crate) type UIdnaFunction = unsafe extern "C" fn(
72 *const UIDNA,
73 *const u8,
74 i32,
75 *mut u8,
76 i32,
77 *mut UIDNAInfo,
78 *mut u32,
79) -> i32;
80
81// From unicode/uspoof.h
82#[repr(C)]
83pub(crate) struct USpoofChecker {
84 _unused: [u8; 0],
85}
86pub(crate) const USPOOF_CONFUSABLE: i32 = 7;
87
88#[link(name = "bindings")]
89extern "C" {
90 // From unicode/ustring.h
91 pub(crate) fn icu_error_code_to_name(code: UErrorCode) -> *const c_char;
92
93 // From unicode/usprep.h
94 pub(crate) fn icu_stringprep_open(
95 type_: UStringPrepProfileType,
96 status: *mut UErrorCode,
97 ) -> *mut UStringPrepProfile;
98 pub(crate) fn icu_stringprep_prepare(
99 prep: *const UStringPrepProfile,
100 src: *const UChar,
101 srcLength: i32,
102 dest: *mut UChar,
103 destCapacity: i32,
104 options: i32,
105 parseError: *mut UParseError,
106 status: *mut UErrorCode,
107 ) -> i32;
108
109 // From unicode/utrace.h
110 pub(crate) fn icu_trace_set_level(traceLevel: UTraceLevel);
111
112 // From unicode/uidna.h
113 pub(crate) fn icu_idna_open(options: u32, pErrorCode: *mut UErrorCode) -> *mut UIDNA;
114 pub(crate) fn icu_idna_name_to_ascii(
115 idna: *const UIDNA,
116 name: *const u8,
117 length: i32,
118 dest: *mut u8,
119 capacity: i32,
120 pInfo: *mut UIDNAInfo,
121 pErrorCode: *mut UErrorCode,
122 ) -> i32;
123 pub(crate) fn icu_idna_name_to_unicode(
124 idna: *const UIDNA,
125 name: *const u8,
126 length: i32,
127 dest: *mut u8,
128 capacity: i32,
129 pInfo: *mut UIDNAInfo,
130 pErrorCode: *mut UErrorCode,
131 ) -> i32;
132
133 // From unicode/uspoof.h
134 pub(crate) fn icu_spoof_open(status: *mut UErrorCode) -> *mut USpoofChecker;
135 pub(crate) fn icu_spoof_set_checks(
136 sc: *mut USpoofChecker,
137 checks: i32,
138 status: *mut UErrorCode,
139 );
140 pub(crate) fn icu_spoof_get_skeleton(
141 sc: *const USpoofChecker,
142 type_: u32,
143 id: *const u8,
144 length: i32,
145 dest: *mut u8,
146 destCapacity: i32,
147 status: *mut UErrorCode,
148 ) -> i32;
149}