Home | History | Annotate | Line # | Download | only in mDNSCore
      1  1.1  christos /* -*- Mode: C; tab-width: 4 -*-
      2  1.1  christos  *
      3  1.1  christos  * Copyright (c) 2020-2022 Apple Inc. All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Licensed under the Apache License, Version 2.0 (the "License");
      6  1.1  christos  * you may not use this file except in compliance with the License.
      7  1.1  christos  * You may obtain a copy of the License at
      8  1.1  christos  *
      9  1.1  christos  *     https://www.apache.org/licenses/LICENSE-2.0
     10  1.1  christos  *
     11  1.1  christos  * Unless required by applicable law or agreed to in writing, software
     12  1.1  christos  * distributed under the License is distributed on an "AS IS" BASIS,
     13  1.1  christos  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  1.1  christos  * See the License for the specific language governing permissions and
     15  1.1  christos  * limitations under the License.
     16  1.1  christos  */
     17  1.1  christos 
     18  1.1  christos #ifndef __MDNS_STRICT_H__
     19  1.1  christos #define __MDNS_STRICT_H__
     20  1.1  christos 
     21  1.1  christos #ifndef MDNS_NO_STRICT
     22  1.1  christos 	#if !defined(__APPLE__)
     23  1.1  christos 		#define MDNS_NO_STRICT				1
     24  1.1  christos 	#else // !defined(__APPLE__)
     25  1.1  christos 		#define MDNS_NO_STRICT				0
     26  1.1  christos 	#endif // !defined(__APPLE__)
     27  1.1  christos #endif // MDNS_NO_STRICT
     28  1.1  christos 
     29  1.1  christos 	#define APPLE_OSX_mDNSResponder			0
     30  1.1  christos 
     31  1.1  christos #ifndef DEBUG
     32  1.1  christos 	#define DEBUG 							0
     33  1.1  christos #endif
     34  1.1  christos 
     35  1.1  christos #ifndef _MDNS_STRICT_DISPOSE_TEMPLATE
     36  1.1  christos 	#if MDNS_NO_STRICT
     37  1.1  christos 		#define _MDNS_STRICT_DISPOSE_TEMPLATE(ptr, function) \
     38  1.1  christos 			do {                                        \
     39  1.1  christos 				if ((ptr) != NULL) {                    \
     40  1.1  christos 					function(ptr);                      \
     41  1.1  christos 					(ptr) = NULL;                       \
     42  1.1  christos 				}                                       \
     43  1.1  christos 			} while(0)
     44  1.1  christos 	#else // MDNS_NO_STRICT
     45  1.1  christos 		#define _MDNS_STRICT_DISPOSE_TEMPLATE _STRICT_DISPOSE_TEMPLATE
     46  1.1  christos 	#endif // MDNS_NO_STRICT
     47  1.1  christos #endif // _MDNS_STRICT_DISPOSE_TEMPLATE
     48  1.1  christos 
     49  1.1  christos #if !MDNS_NO_STRICT
     50  1.1  christos #include <CoreFoundation/CoreFoundation.h>
     51  1.1  christos #include <os/log.h>
     52  1.1  christos 
     53  1.1  christos #include "../mDNSMacOSX/secure_coding/strict.h"
     54  1.1  christos 
     55  1.1  christos #pragma mark -- Alloc --
     56  1.1  christos 
     57  1.1  christos #define mdns_malloc strict_malloc
     58  1.1  christos #define MDNS_MALLOC_TYPE STRICT_MALLOC_TYPE
     59  1.1  christos #define mdns_calloc strict_calloc
     60  1.1  christos #define MDNS_CALLOC_TYPE STRICT_CALLOC_TYPE
     61  1.1  christos #define mdns_reallocf strict_reallocf
     62  1.1  christos #define MDNS_REALLOCF_TYPE STRICT_REALLOCF_TYPE
     63  1.1  christos #define mdns_memalign strict_memalign
     64  1.1  christos #define MDNS_ALLOC_ALIGN_TYPE STRICT_ALLOC_ALIGN_TYPE
     65  1.1  christos #define mdns_strdup strict_strdup
     66  1.1  christos #define mdns_strlcpy strict_strlcpy
     67  1.1  christos 
     68  1.1  christos #pragma mark -- Dispose --
     69  1.1  christos 
     70  1.1  christos #define MDNS_DISPOSE_XPC STRICT_DISPOSE_XPC
     71  1.1  christos #define MDNS_DISPOSE_XPC_PROPERTY(obj, prop) MDNS_DISPOSE_XPC(obj->prop)
     72  1.1  christos 
     73  1.1  christos #define MDNS_DISPOSE_ALLOCATED STRICT_DISPOSE_ALLOCATED
     74  1.1  christos #define MDNS_DISPOSE_ALLOCATED_PROPERTY(obj, prop) MDNS_DISPOSE_ALLOCATED(obj->prop)
     75  1.1  christos #define mdns_free(ptr) MDNS_DISPOSE_ALLOCATED(ptr)
     76  1.1  christos 
     77  1.1  christos #define MDNS_DISPOSE_DISPATCH STRICT_DISPOSE_DISPATCH
     78  1.1  christos #define MDNS_DISPOSE_DISPATCH_PROPERTY(obj, prop) MDNS_DISPOSE_DISPATCH(obj->prop)
     79  1.1  christos 
     80  1.1  christos #define MDNS_RESET_BLOCK STRICT_RESET_BLOCK
     81  1.1  christos #define MDNS_RESET_BLOCK_PROPERTY(obj, prop, new_block) MDNS_RESET_BLOCK(obj->prop, new_block)
     82  1.1  christos 
     83  1.1  christos #define MDNS_DISPOSE_BLOCK STRICT_DISPOSE_BLOCK
     84  1.1  christos #define MDNS_DISPOSE_BLOCK_PROPERTY(obj, prop) MDNS_DISPOSE_BLOCK(obj->prop)
     85  1.1  christos 
     86  1.1  christos #define MDNS_DISPOSE_CF_OBJECT STRICT_DISPOSE_CF_OBJECT
     87  1.1  christos #define MDNS_DISPOSE_CF_PROPERTY(obj, prop) MDNS_DISPOSE_CF_OBJECT(obj->prop)
     88  1.1  christos 
     89  1.1  christos #define MDNS_DISPOSE_ADDRINFO STRICT_DISPOSE_ADDRINFO
     90  1.1  christos 
     91  1.1  christos #define MDNS_DISPOSE_NW(obj) _MDNS_STRICT_DISPOSE_TEMPLATE(obj, nw_release)
     92  1.1  christos 
     93  1.1  christos #define MDNS_DISPOSE_SEC(obj) _MDNS_STRICT_DISPOSE_TEMPLATE(obj, sec_release)
     94  1.1  christos 
     95  1.1  christos #define MDNS_DISPOSE_DNS_SERVICE_REF(obj) _MDNS_STRICT_DISPOSE_TEMPLATE(obj, DNSServiceRefDeallocate)
     96  1.1  christos 
     97  1.1  christos #ifdef BlockForget
     98  1.1  christos // Redfine BlockForget to bypass poisoned Block_release
     99  1.1  christos #undef BlockForget
    100  1.1  christos #if( COMPILER_ARC )
    101  1.1  christos 	#define	BlockForget( X )			do { *(X) = nil; } while( 0 )
    102  1.1  christos #else
    103  1.1  christos 	#define	BlockForget( X )			ForgetCustom( X, _Block_release )
    104  1.1  christos #endif
    105  1.1  christos #endif
    106  1.1  christos 
    107  1.1  christos #else  // !MDNS_NO_STRICT
    108  1.1  christos 
    109  1.1  christos #include <stddef.h>
    110  1.1  christos #include <stdlib.h>
    111  1.1  christos 
    112  1.1  christos #define mdns_malloc 			malloc
    113  1.1  christos #define mdns_calloc 			calloc
    114  1.1  christos #define mdns_strdup 			strdup
    115  1.1  christos #define mdns_free(obj) \
    116  1.1  christos 	_MDNS_STRICT_DISPOSE_TEMPLATE(obj, free)
    117  1.1  christos 
    118  1.1  christos static
    119  1.1  christos #if defined(_WIN32)
    120  1.1  christos __forceinline
    121  1.1  christos #else
    122  1.1  christos inline __attribute__((always_inline))
    123  1.1  christos #endif
    124  1.1  christos void _mdns_strict_strlcpy(char * const restrict dst, const char * const restrict src, const size_t dst_len)
    125  1.1  christos {
    126  1.1  christos 	if (dst_len == 0) {
    127  1.1  christos 		return;
    128  1.1  christos 	}
    129  1.1  christos 
    130  1.1  christos 	char *d = dst;
    131  1.1  christos 	const char *s = src;
    132  1.1  christos 	for (size_t n = dst_len - 1; n > 0; n--) {
    133  1.1  christos 		if ((*d++ = *s++) == '\0') {
    134  1.1  christos 			return;
    135  1.1  christos 		}
    136  1.1  christos 	}
    137  1.1  christos 	*d = '\0';
    138  1.1  christos }
    139  1.1  christos #define mdns_strlcpy			_mdns_strict_strlcpy
    140  1.1  christos 
    141  1.1  christos 
    142  1.1  christos #define MDNS_DISPOSE_DNS_SERVICE_REF(obj) _MDNS_STRICT_DISPOSE_TEMPLATE(obj, DNSServiceRefDeallocate)
    143  1.1  christos 
    144  1.1  christos 
    145  1.1  christos 
    146  1.1  christos #endif // !MDNS_NO_STRICT
    147  1.1  christos 
    148  1.1  christos #endif // __MDNS_STRICT_H__
    149