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