Home | History | Annotate | Line # | Download | only in isc
      1 /*	$NetBSD: xml.c,v 1.3 2025/05/21 14:48:05 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 #include <isc/mem.h>
     17 #include <isc/util.h>
     18 #include <isc/xml.h>
     19 
     20 #ifdef HAVE_LIBXML2
     21 #include <libxml/parser.h>
     22 #include <libxml/xmlversion.h>
     23 
     24 #ifndef LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS
     25 static isc_mem_t *isc__xml_mctx = NULL;
     26 
     27 static void *
     28 isc__xml_malloc(size_t size) {
     29 	return isc_mem_allocate(isc__xml_mctx, size);
     30 }
     31 
     32 static void *
     33 isc__xml_realloc(void *ptr, size_t size) {
     34 	return isc_mem_reallocate(isc__xml_mctx, ptr, size);
     35 }
     36 
     37 static char *
     38 isc__xml_strdup(const char *str) {
     39 	return isc_mem_strdup(isc__xml_mctx, str);
     40 }
     41 
     42 static void
     43 isc__xml_free(void *ptr) {
     44 	if (ptr == NULL) {
     45 		return;
     46 	}
     47 	isc_mem_free(isc__xml_mctx, ptr);
     48 }
     49 
     50 #endif /* !LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS) */
     51 #endif /* HAVE_LIBXML2 */
     52 
     53 void
     54 isc__xml_initialize(void) {
     55 #ifdef HAVE_LIBXML2
     56 #ifndef LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS
     57 	isc_mem_create(&isc__xml_mctx);
     58 	isc_mem_setname(isc__xml_mctx, "libxml2");
     59 	isc_mem_setdestroycheck(isc__xml_mctx, false);
     60 
     61 	RUNTIME_CHECK(xmlMemSetup(isc__xml_free, isc__xml_malloc,
     62 				  isc__xml_realloc, isc__xml_strdup) == 0);
     63 #endif /* !LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS */
     64 
     65 	xmlInitParser();
     66 #endif /* HAVE_LIBXML2 */
     67 }
     68 
     69 void
     70 isc__xml_shutdown(void) {
     71 #ifdef HAVE_LIBXML2
     72 	xmlCleanupParser();
     73 
     74 #ifndef LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS
     75 	isc_mem_destroy(&isc__xml_mctx);
     76 #endif /* !LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS */
     77 #endif /* HAVE_LIBXML2 */
     78 }
     79 
     80 void
     81 isc__xml_setdestroycheck(bool check ISC_ATTR_UNUSED) {
     82 #ifdef HAVE_LIBXML2
     83 #ifndef LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS
     84 	isc_mem_setdestroycheck(isc__xml_mctx, check);
     85 #endif /* LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS */
     86 #endif /* HAVE_LIBXML2 */
     87 }
     88