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