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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! C ABI exports for the buffer family — xmlBuf* / xmlBuffer* / xmlCharStr*
//! (upstream buf.c, tree.c, xmlstring.c, 2.15.3).
//!
//! In 2.15 `xmlBuf` and `xmlBuffer` are the same C struct (typedef xmlBuffer
//! xmlBuf), but the candidate mirrors them as two distinct structs
//! (`_xmlBuffer` with `{content, use_, size, alloc, contentIO}` and `_xmlBuf`
//! with `{content, use_, size, alloc, error, buffer, io}`). The `xmlBuf*`
//! exports therefore operate on the `_xmlBuf` fields directly, allocating
//! through the candidate allocator (`xmlMalloc`/`xmlFree`), exactly as
//! upstream manipulates `buf->content`/`buf->use`/`buf->size`.
use ptr;
use ;
use crate;
use crate;
use crate;
use crateio;
use cratetree;
// ── libc FILE* plumbing for xmlBufferDump ───────────────────────────────
//
// The FILE* is opaque at the ABI boundary and is passed as *mut c_void.
// fwrite(3) is declared here rather than pulled from the libc crate so the
// dependency stays explicit; `stdout` is the libc data symbol used by
// upstream's `if (file == NULL) file = stdout;` fallback.
extern "C"
// ═══════════════════════════════════════════════════════════════════════════════
// 1. xmlBuf operations (modern replacement, tree.h)
// ═══════════════════════════════════════════════════════════════════════════════
/// Get pointer into buffer content.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlChar *xmlBufContent(const xmlBuf *buf);
/// ```
///
/// buf.c 2.15: returns `buf->content`, or NULL when `buf` is NULL or the
/// buffer is in error state (`BUF_ERROR`).
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuf` (or NULL).
/// - The returned pointer is owned by `buf` and must not be freed by the
/// caller.
pub unsafe extern "C"
/// Return a pointer to the end of the buffer content.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlChar *xmlBufEnd(xmlBuf *buf);
/// ```
///
/// buf.c 2.15: returns `&buf->content[buf->use]`, or NULL when `buf` is NULL
/// or the buffer is in error state.
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuf` (or NULL).
/// - The returned pointer is owned by `buf` and must not be freed by the
/// caller.
pub unsafe extern "C"
/// Append the string value of a node to `buf`.
///
/// For text/CDATA/comment/PI nodes the string value is the node content;
/// otherwise it is the concatenation of the string values of the node's
/// descendants, with entity references substituted. Namespace declaration
/// nodes contribute their href.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlBufGetNodeContent(xmlBuf *buf, const xmlNode *cur);
/// ```
///
/// tree.c 2.15: returns -1 only when `cur` or `buf` is NULL; otherwise 0
/// (append failures are ignored upstream and here, matching the void
/// contract).
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuf` (or NULL).
/// - `cur` must be a valid pointer to a `_xmlNode`/`_xmlNs` (or NULL).
pub unsafe extern "C"
/// Serialize an XML node to an xmlBuf.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// size_t xmlBufNodeDump(xmlBuf *buf, xmlDoc *doc, xmlNode *cur, int level, int format);
/// ```
///
/// xmlsave.c/tree.c 2.15: returns the number of bytes written to `buf`, or
/// `(size_t)-1` when `buf`/`cur` is NULL or serialization fails. The level
/// is clamped to [0, 100] (xmlNodeDumpOutput). `doc` is ignored, matching
/// upstream's `(void) doc`.
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuf` (or NULL).
/// - `cur` must be a valid pointer to a `_xmlNode` (or NULL).
/// - `doc` is unused and may be NULL.
pub unsafe extern "C"
/// Discard bytes at the start of a buffer.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// size_t xmlBufShrink(xmlBuf *buf, size_t len);
/// ```
///
/// buf.c 2.15: removes `len` bytes from the front by advancing
/// `buf->content` and decreasing `buf->use`/`buf->size`; returns the number
/// of bytes removed, or 0 when `buf` is NULL, the buffer is in error state,
/// `len` is 0, or `len` exceeds `buf->use`. Unlike `xmlBufferShrink`, errors
/// return 0 rather than -1 (size_t return type).
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuf` (or NULL).
pub unsafe extern "C"
/// Return the size of the buffer content.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// size_t xmlBufUse(xmlBuf *buf);
/// ```
///
/// buf.c 2.15: returns `buf->use`, or 0 when `buf` is NULL or the buffer is
/// in error state.
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuf` (or NULL).
pub unsafe extern "C"
// ═══════════════════════════════════════════════════════════════════════════════
// 2. xmlBuffer operations (deprecated, tree.h)
// ═══════════════════════════════════════════════════════════════════════════════
/// Append a zero-terminated C string to a buffer.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlBufferCCat(xmlBuffer *buf, const char *str);
/// ```
///
/// buf.c 2.15: forwards to `xmlBufferAdd(buf, (const xmlChar *) str, -1)`,
/// returning XML_ERR_ARGUMENT when `buf` or `str` is NULL, XML_ERR_OK on
/// success (including the empty string), and XML_ERR_NO_MEMORY when growth
/// fails.
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuffer` (or NULL).
/// - `str` must be a valid NUL-terminated C string (or NULL).
pub unsafe extern "C"
/// Dump a buffer to a `FILE`.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlBufferDump(FILE *file, xmlBuffer *buf);
/// ```
///
/// buf.c 2.15: returns 0 when `buf` is NULL or has no content, defaults a
/// NULL `file` to stdout, and otherwise returns the `fwrite` byte count
/// clamped to INT_MAX. Upstream never returns -1 from this function; write
/// errors surface as a short count.
///
/// # SAFETY
///
/// - `file` must be a valid `FILE*` or NULL (then stdout is used).
/// - `buf` must be a valid pointer to a `_xmlBuffer` (or NULL).
pub unsafe extern "C"
/// Resize a buffer to a minimum size.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlBufferResize(xmlBuffer *buf, unsigned int size);
/// ```
///
/// buf.c 2.15: returns 0 when `buf` is NULL, 1 when `size` is below the
/// current capacity, otherwise grows the buffer so its total capacity is at
/// least `size` and returns 1 on success / 0 on allocation failure.
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuffer` (or NULL).
pub unsafe extern "C"
/// Append a zero-terminated `xmlChar` string to a buffer.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlBufferWriteCHAR(xmlBuffer *buf, const xmlChar *string);
/// ```
///
/// buf.c 2.15: `xmlBufferAdd(buf, string, -1)`, i.e. append the
/// NUL-terminated string; failures are ignored (void return).
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuffer` (or NULL).
/// - `string` must be a valid NUL-terminated xmlChar string (or NULL).
pub unsafe extern "C"
/// Append a zero-terminated C string to a buffer.
///
/// Same as `xmlBufferCCat`.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlBufferWriteChar(xmlBuffer *buf, const char *string);
/// ```
///
/// buf.c 2.15: `xmlBufferAdd(buf, (const xmlChar *) string, -1)`; failures
/// are ignored (void return).
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuffer` (or NULL).
/// - `string` must be a valid NUL-terminated C string (or NULL).
pub unsafe extern "C"
/// Append a quoted string to a buffer.
///
/// Appends the string wrapped in quotes. If the string contains both single
/// and double quotes, double quotes are escaped with `"` (upstream
/// buf.c 2.15; no backslash or CR/LF escaping exists in this version).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlBufferWriteQuotedString(xmlBuffer *buf, const xmlChar *string);
/// ```
///
/// buf.c 2.15: with a double-quote-containing string that also contains a
/// single quote, emits `"` + string with `"` → `"` + `"`; with only
/// double quotes emits `'` + string + `'`; otherwise emits `"` + string +
/// `"`. A NULL string yields the bare quote pair, exactly like upstream
/// (xmlStrchr(NULL) misses, xmlBufferCat(NULL) fails silently).
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a `_xmlBuffer` (or NULL).
/// - `string` must be a valid NUL-terminated xmlChar string (or NULL).
pub unsafe extern "C"
// ═══════════════════════════════════════════════════════════════════════════════
// 3. xmlChar string duplication (xmlstring.h)
// ═══════════════════════════════════════════════════════════════════════════════
/// Duplicate a `char *` string to a new `xmlChar *` string.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlChar *xmlCharStrdup(const char *cur);
/// ```
///
/// xmlstring.c 2.15: returns NULL for a NULL input, otherwise
/// `xmlCharStrndup(cur, strlen(cur))`.
///
/// # SAFETY
///
/// - `cur` must be a valid NUL-terminated C string or NULL.
/// - The returned pointer is allocated with `xmlMalloc` and must be freed
/// with `xmlFree`.
pub unsafe extern "C"
/// Duplicate `len` bytes of a `char *` string to a new `xmlChar *` string.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlChar *xmlCharStrndup(const char *cur, int len);
/// ```
///
/// xmlstring.c 2.15: returns NULL when `cur` is NULL or `len` is negative;
/// otherwise allocates `len + 1` bytes, copies at most `len` bytes, stops
/// early (and returns immediately) if an embedded NUL is copied, and always
/// NUL-terminates.
///
/// # SAFETY
///
/// - `cur` must be a valid pointer to at least `len` readable bytes or NULL.
/// - The returned pointer is allocated with `xmlMalloc` and must be freed
/// with `xmlFree`.
pub unsafe extern "C"