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
//! LDAP output encoders for DN (RFC 4514) and filter (RFC 4515) contexts.
//!
//! Provides two encoders:
//! - [`LdapDnEncoder`] — escapes characters special in LDAP Distinguished Names
//! - [`LdapFilterEncoder`] — hex-escapes characters special in LDAP search filters
//!
//! Both implement the [`OutputEncoder`] trait for generic contexts, but the
//! **primary API is convenience free functions**: [`encode_dn()`] and [`encode_filter()`].
//!
//! # LDAP DN encoding (RFC 4514)
//!
//! The following characters are backslash-escaped: `,`, `+`, `"`, `\`, `<`, `>`, `;`, `=`, `#`
//! (when leading). Leading and trailing spaces are escaped. Null bytes are hex-escaped as `\00`.
//!
//! # LDAP filter encoding (RFC 4515)
//!
//! The following characters are hex-escaped: `*`, `(`, `)`, `\`, NUL (`\x00`).
use Cow;
use crateOutputEncoder;
/// Encodes strings for safe use in LDAP Distinguished Name components (RFC 4514).
///
/// Escapes the special characters `,`, `+`, `"`, `\`, `<`, `>`, `;`, and `=`.
/// Leading `#` and leading/trailing spaces are also escaped. Null bytes are
/// hex-escaped as `\00`.
///
/// Returns [`Cow::Borrowed`] when the input needs no encoding (zero-allocation
/// fast path).
///
/// # Examples
///
/// ```
/// use secure_output::ldap::LdapDnEncoder;
/// use secure_output::encode::OutputEncoder;
///
/// let encoder = LdapDnEncoder;
/// assert_eq!(encoder.encode("John Smith"), "John Smith");
/// assert_eq!(encoder.encode("a+b,c=d"), "a\\+b\\,c\\=d");
/// ```
;
/// Characters that must be backslash-escaped in DN attribute values per RFC 4514 §2.4.
/// Returns `true` if the input requires any DN encoding.
/// Encodes a string for safe use in an LDAP Distinguished Name component (RFC 4514).
///
/// This is the primary convenience API. It delegates to [`LdapDnEncoder`] but
/// does not require constructing an encoder instance.
///
/// # Examples
///
/// ```
/// use secure_output::ldap::encode_dn;
///
/// assert_eq!(encode_dn("John Smith"), "John Smith");
/// assert_eq!(encode_dn("a+b,c=d"), "a\\+b\\,c\\=d");
/// assert_eq!(encode_dn(" admin "), "\\ admin\\ ");
/// ```
///
/// # Errors
///
/// This function is infallible — it always returns a valid encoded string.
// ──────────────────────────────────────────────
// LDAP Filter Encoder (RFC 4515)
// ──────────────────────────────────────────────
/// Encodes strings for safe use in LDAP search filter assertions (RFC 4515).
///
/// Hex-escapes the characters `*`, `(`, `)`, `\`, and NUL (`\x00`) using the
/// `\XX` notation defined in RFC 4515 §3.
///
/// Returns [`Cow::Borrowed`] when the input needs no encoding.
///
/// # Examples
///
/// ```
/// use secure_output::ldap::LdapFilterEncoder;
/// use secure_output::encode::OutputEncoder;
///
/// let encoder = LdapFilterEncoder;
/// assert_eq!(encoder.encode("john"), "john");
/// assert_eq!(encoder.encode("user*admin"), "user\\2aadmin");
/// assert_eq!(encoder.encode("(admin)"), "\\28admin\\29");
/// ```
;
/// Characters that must be hex-escaped in LDAP filter values per RFC 4515 §3.
/// Encodes a string for safe use in an LDAP search filter assertion (RFC 4515).
///
/// This is the primary convenience API. It delegates to [`LdapFilterEncoder`]
/// but does not require constructing an encoder instance.
///
/// # Examples
///
/// ```
/// use secure_output::ldap::encode_filter;
///
/// assert_eq!(encode_filter("john"), "john");
/// assert_eq!(encode_filter("user*admin"), "user\\2aadmin");
/// assert_eq!(encode_filter("(admin)"), "\\28admin\\29");
/// ```
///
/// # Errors
///
/// This function is infallible — it always returns a valid encoded string.