1use crate::abi::allocator::{xmlFreeImpl, xmlMallocImpl};
57use crate::abi::types::xmlChar;
58use core::ffi::c_void;
59use core::ptr;
60use std::os::raw::{c_char, c_int};
61use std::slice;
62
63#[inline]
73pub(crate) const unsafe fn xml_strlen(str: *const xmlChar) -> usize {
74 if str.is_null() {
75 return 0;
76 }
77 let mut len: usize = 0;
78 while *str.add(len) != 0 {
79 len += 1;
80 }
81 len
82}
83
84#[inline]
95pub(crate) unsafe fn xml_strdup(str: *const xmlChar) -> *mut xmlChar {
96 if str.is_null() {
97 return ptr::null_mut();
98 }
99 let len = xml_strlen(str);
100 let copy = xmlMallocImpl(len + 1) as *mut xmlChar;
101 if copy.is_null() {
102 return ptr::null_mut();
103 }
104 ptr::copy_nonoverlapping(str, copy, len + 1);
105 copy
106}
107
108#[inline]
114pub(crate) unsafe fn c_strdup(str: *const c_char) -> *mut c_char {
115 if str.is_null() {
116 return ptr::null_mut();
117 }
118 let len = libc::strlen(str);
119 let copy = xmlMallocImpl(len + 1) as *mut c_char;
120 if copy.is_null() {
121 return ptr::null_mut();
122 }
123 ptr::copy_nonoverlapping(str as *const u8, copy as *mut u8, len + 1);
124 copy
125}
126
127pub(crate) unsafe fn bytes_to_xmlstr(bytes: &[u8]) -> *mut xmlChar {
133 let len = bytes.len();
134 let ptr = xmlMallocImpl(len + 1) as *mut xmlChar;
135 if ptr.is_null() {
136 return ptr::null_mut();
137 }
138 ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, len);
139 *ptr.add(len) = 0; ptr
141}
142
143#[inline]
150pub(crate) const unsafe fn xmlstr_to_bytes(str: *const xmlChar) -> &'static [u8] {
151 if str.is_null() {
152 return &[];
153 }
154 let len = xml_strlen(str);
155 slice::from_raw_parts(str, len)
156}
157
158#[inline]
166pub(crate) unsafe fn xml_strcmp(str1: *const xmlChar, str2: *const xmlChar) -> i32 {
167 if str1 == str2 {
168 return 0;
169 }
170 if str1.is_null() {
171 return -1;
172 }
173 if str2.is_null() {
174 return 1;
175 }
176 let mut i: usize = 0;
177 loop {
178 let a = *str1.add(i);
179 let b = *str2.add(i);
180 if a != b {
181 return a as i32 - b as i32;
182 }
183 if a == 0 {
184 return 0;
185 }
186 i += 1;
187 }
188}
189
190#[inline]
198#[allow(dead_code)]
199pub(crate) unsafe fn xml_strcat(str1: *const xmlChar, str2: *const xmlChar) -> *mut xmlChar {
200 let len1 = xml_strlen(str1);
201 let len2 = xml_strlen(str2);
202 let result = xmlMallocImpl(len1 + len2 + 1) as *mut xmlChar;
203 if result.is_null() {
204 return ptr::null_mut();
205 }
206 if !str1.is_null() {
207 ptr::copy_nonoverlapping(str1, result, len1);
208 }
209 if !str2.is_null() {
210 ptr::copy_nonoverlapping(str2, result.add(len1), len2);
211 }
212 *result.add(len1 + len2) = 0;
213 result
214}
215
216#[inline]
224pub(crate) unsafe fn xml_strndup(str: *const xmlChar, len: usize) -> *mut xmlChar {
225 if str.is_null() {
226 return ptr::null_mut();
227 }
228 let p = unsafe { xmlMallocImpl(len + 1) as *mut xmlChar };
229 if p.is_null() {
230 return ptr::null_mut();
231 }
232 unsafe {
233 ptr::copy_nonoverlapping(str, p, len);
234 *p.add(len) = 0;
235 }
236 p
237}
238
239pub(crate) unsafe fn xmlstr_to_string(str: *const xmlChar) -> String {
246 if str.is_null() {
247 return String::new();
248 }
249 let bytes = unsafe { xmlstr_to_bytes(str) };
250 String::from_utf8_lossy(bytes).to_string()
251}
252
253pub unsafe fn build_qname(
263 ncname: *const xmlChar,
264 prefix: *const xmlChar,
265 memory: *mut xmlChar,
266 len: c_int,
267) -> *mut xmlChar {
268 if ncname.is_null() {
269 return ptr::null_mut();
270 }
271 if prefix.is_null() {
272 return ncname as *mut xmlChar;
273 }
274 unsafe {
275 let lenn = xml_strlen(ncname);
276 let lenp = xml_strlen(prefix);
277 let ret = if memory.is_null() || (len as usize) < lenn + lenp + 2 {
278 let p = xmlMallocImpl(lenn + lenp + 2) as *mut xmlChar;
279 if p.is_null() {
280 return ptr::null_mut();
281 }
282 p
283 } else {
284 memory
285 };
286 ptr::copy_nonoverlapping(prefix, ret, lenp);
287 *ret.add(lenp) = b':' as xmlChar;
288 ptr::copy_nonoverlapping(ncname, ret.add(lenp + 1), lenn);
289 *ret.add(lenn + lenp + 1) = 0;
290 ret
291 }
292}
293
294pub unsafe fn split_qname2(name: *const xmlChar, prefix: *mut *mut xmlChar) -> *mut xmlChar {
304 if prefix.is_null() {
305 return ptr::null_mut();
306 }
307 unsafe {
308 *prefix = ptr::null_mut();
309 }
310 if name.is_null() {
311 return ptr::null_mut();
312 }
313 unsafe {
314 if *name == b':' as xmlChar {
316 return ptr::null_mut();
317 }
318 let mut len: usize = 0;
319 while *name.add(len) != 0 && *name.add(len) != b':' as xmlChar {
320 len += 1;
321 }
322 if *name.add(len) == 0 || *name.add(len + 1) == 0 {
323 return ptr::null_mut();
324 }
325 let p = xml_strndup(name, len);
326 if p.is_null() {
327 return ptr::null_mut();
328 }
329 *prefix = p;
330 let ret = xml_strdup(name.add(len + 1));
331 if ret.is_null() {
332 xmlFreeImpl(*prefix as *mut c_void);
333 *prefix = ptr::null_mut();
334 return ptr::null_mut();
335 }
336 ret
337 }
338}
339
340pub unsafe fn split_qname3(name: *const xmlChar, len: *mut c_int) -> *mut xmlChar {
350 if name.is_null() || len.is_null() {
351 return ptr::null_mut();
352 }
353 unsafe {
354 if *name == b':' as xmlChar {
355 return ptr::null_mut();
356 }
357 let mut l: usize = 0;
358 while *name.add(l) != 0 && *name.add(l) != b':' as xmlChar {
359 l += 1;
360 }
361 if *name.add(l) == 0 {
362 return ptr::null_mut();
363 }
364 *len = l as c_int;
365 name.add(l + 1) as *mut xmlChar
366 }
367}
368
369pub const unsafe fn utf8_strlen(utf: *const xmlChar) -> c_int {
376 if utf.is_null() {
377 return 0;
378 }
379 unsafe {
380 let mut n: c_int = 0;
381 let mut cur = utf;
382 while *cur != 0 {
383 let c = *cur;
384 if c & 0x80 == 0 {
385 cur = cur.add(1);
386 } else if c & 0xe0 == 0xc0 {
387 cur = cur.add(2);
388 } else if c & 0xf0 == 0xe0 {
389 cur = cur.add(3);
390 } else if c & 0xf8 == 0xf0 {
391 cur = cur.add(4);
392 } else {
393 return n;
395 }
396 n += 1;
397 }
398 n
399 }
400}
401
402pub const unsafe fn utf8_size(utf: *const xmlChar) -> c_int {
410 if utf.is_null() {
411 return -1;
412 }
413 unsafe {
414 let c = *utf;
415 if c == 0 {
416 return 0;
417 }
418 if c & 0x80 == 0 {
419 1
420 } else if c & 0xe0 == 0xc0 {
421 2
422 } else if c & 0xf0 == 0xe0 {
423 3
424 } else if c & 0xf8 == 0xf0 {
425 4
426 } else {
427 -1
428 }
429 }
430}
431
432pub const unsafe fn check_utf8(utf: *const xmlChar) -> c_int {
439 if utf.is_null() {
440 return 0;
441 }
442 unsafe {
443 let mut cur = utf;
444 while *cur != 0 {
445 let c = *cur;
446 if c & 0x80 == 0 {
447 cur = cur.add(1);
448 } else if c & 0xe0 == 0xc0 {
449 let c1 = *cur.add(1);
451 if c1 & 0xc0 != 0x80 {
452 return 0;
453 }
454 cur = cur.add(2);
455 } else if c & 0xf0 == 0xe0 {
456 let c1 = *cur.add(1);
457 let c2 = *cur.add(2);
458 if c1 & 0xc0 != 0x80 || c2 & 0xc0 != 0x80 {
459 return 0;
460 }
461 cur = cur.add(3);
462 } else if c & 0xf8 == 0xf0 {
463 let c1 = *cur.add(1);
464 let c2 = *cur.add(2);
465 let c3 = *cur.add(3);
466 if c1 & 0xc0 != 0x80 || c2 & 0xc0 != 0x80 || c3 & 0xc0 != 0x80 {
467 return 0;
468 }
469 cur = cur.add(4);
470 } else {
471 return 0;
472 }
473 }
474 1
475 }
476}
477#[inline]
478pub(crate) const unsafe fn xml_str_starts_with(
479 str: *const xmlChar,
480 prefix: *const xmlChar,
481) -> bool {
482 if str.is_null() || prefix.is_null() {
483 return false;
484 }
485 let mut i: usize = 0;
486 loop {
487 let p = *prefix.add(i);
488 if p == 0 {
489 return true; }
491 if *str.add(i) != p {
492 return false;
493 }
494 i += 1;
495 }
496}
497
498#[cfg(test)]
499mod tests {
500 use super::*;
501 use crate::abi::allocator::xmlFreeImpl;
502
503 #[test]
504 fn test_xml_strlen() {
505 unsafe {
506 assert_eq!(xml_strlen(ptr::null()), 0);
507 let s = b"hello\0" as *const u8 as *const xmlChar;
508 assert_eq!(xml_strlen(s), 5);
509 let empty = b"\0" as *const u8 as *const xmlChar;
510 assert_eq!(xml_strlen(empty), 0);
511 }
512 }
513
514 #[test]
515 fn test_xml_strdup() {
516 unsafe {
517 assert!(xml_strdup(ptr::null()).is_null());
518 let s = b"hello\0" as *const u8 as *const xmlChar;
519 let dup = xml_strdup(s);
520 assert!(!dup.is_null());
521 assert_eq!(xml_strlen(dup), 5);
522 assert_eq!(*dup.add(0), b'h');
523 assert_eq!(*dup.add(4), b'o');
524 assert_eq!(*dup.add(5), 0);
525 xmlFreeImpl(dup as *mut c_void);
526 }
527 }
528
529 #[test]
530 fn test_xml_strcmp() {
531 unsafe {
532 assert_eq!(xml_strcmp(ptr::null(), ptr::null()), 0);
533 assert!(xml_strcmp(b"a\0" as *const u8 as *const xmlChar, ptr::null()) > 0);
534 let a = b"abc\0" as *const u8 as *const xmlChar;
535 let b = b"abc\0" as *const u8 as *const xmlChar;
536 assert_eq!(xml_strcmp(a, b), 0);
537 let c = b"abd\0" as *const u8 as *const xmlChar;
538 assert!(xml_strcmp(a, c) < 0);
539 assert!(xml_strcmp(c, a) > 0);
540 }
541 }
542
543 #[test]
544 fn test_xml_strcat() {
545 unsafe {
546 let a = b"hello \0" as *const u8 as *const xmlChar;
547 let b = b"world\0" as *const u8 as *const xmlChar;
548 let result = xml_strcat(a, b);
549 assert!(!result.is_null());
550 assert_eq!(xml_strlen(result), 11);
551 let expected = b"hello world\0";
552 let mut i = 0;
553 while expected[i] != 0 {
554 assert_eq!(*result.add(i), expected[i]);
555 i += 1;
556 }
557 xmlFreeImpl(result as *mut c_void);
558 }
559 }
560
561 #[test]
562 fn test_bytes_to_xmlstr() {
563 unsafe {
564 let bytes = b"hello";
565 let ptr = bytes_to_xmlstr(bytes);
566 assert!(!ptr.is_null());
567 assert_eq!(xml_strlen(ptr), 5);
568 assert_eq!(*ptr.add(5), 0);
569 xmlFreeImpl(ptr as *mut c_void);
570 }
571 }
572
573 #[test]
574 fn test_xml_str_starts_with() {
575 unsafe {
576 let s = b"hello world\0" as *const u8 as *const xmlChar;
577 let prefix = b"hello\0" as *const u8 as *const xmlChar;
578 let not_prefix = b"world\0" as *const u8 as *const xmlChar;
579 assert!(xml_str_starts_with(s, prefix));
580 assert!(!xml_str_starts_with(s, not_prefix));
581 assert!(!xml_str_starts_with(ptr::null(), prefix));
582 assert!(!xml_str_starts_with(s, ptr::null()));
583 }
584 }
585}