]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/mdns_objects/mdns_tlv.c
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * https://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <CoreUtils/CoreUtils.h>
21 //======================================================================================================================
22 // MARK: - Type-Length Headers
30 check_compile_time(sizeof(mdns_tl16_t
) == 4);
32 //======================================================================================================================
33 // MARK: - Local Prototypes
36 _mdns_tl16_get_type(const mdns_tl16_t
*tl
);
39 _mdns_tl16_get_length(const mdns_tl16_t
*tl
);
42 _mdns_tl16_init(mdns_tl16_t
*tl
, uint16_t type
, uint16_t length
);
44 //======================================================================================================================
45 // MARK: - Public Functions
48 mdns_tlv16_get_value(const uint8_t * const start
, const uint8_t * const end
, const uint16_t type
,
49 size_t * const out_length
, const uint8_t ** const out_value
, const uint8_t ** const out_ptr
)
52 require_action_quiet(start
<= end
, exit
, err
= kRangeErr
);
54 const uint8_t *ptr
= start
;
55 while ((end
- ptr
) > 0) {
56 const mdns_tl16_t
*tl
;
57 require_action_quiet((end
- ptr
) >= (ptrdiff_t)sizeof(*tl
), exit
, err
= kUnderrunErr
);
59 tl
= (const mdns_tl16_t
*)ptr
;
60 const uint16_t tlv_type
= _mdns_tl16_get_type(tl
);
61 const uint16_t tlv_length
= _mdns_tl16_get_length(tl
);
62 const uint8_t * const tlv_value
= (const uint8_t *)&tl
[1];
63 require_action_quiet((end
- tlv_value
) >= tlv_length
, exit
, err
= kUnderrunErr
);
65 ptr
= &tlv_value
[tlv_length
];
66 if (tlv_type
== type
) {
68 *out_length
= tlv_length
;
71 *out_value
= tlv_value
;
85 //======================================================================================================================
88 mdns_tlv16_set(uint8_t * const dst
, const uint8_t * const limit
, const uint16_t type
, const uint16_t length
,
89 const uint8_t * const value
, uint8_t ** const out_end
)
93 require_action_quiet(!limit
|| ((limit
- dst
) >= (ptrdiff_t)(sizeof(tl
) + length
)), exit
, err
= kUnderrunErr
);
95 tl
= (mdns_tl16_t
*)dst
;
96 _mdns_tl16_init(tl
, type
, length
);
97 uint8_t * const dst_value
= (uint8_t *)&tl
[1];
99 memcpy(dst_value
, value
, length
);
102 *out_end
= &dst_value
[length
];
110 //======================================================================================================================
113 mdns_tlv16_get_required_length(const uint16_t value_length
)
115 return (sizeof(mdns_tl16_t
) + value_length
);
118 //======================================================================================================================
119 // MARK: - Private Functions
122 _mdns_tl16_get_type(const mdns_tl16_t
* const tl
)
124 return ReadBig16(tl
->type
);
127 //======================================================================================================================
130 _mdns_tl16_get_length(const mdns_tl16_t
* const tl
)
132 return ReadBig16(tl
->length
);
135 //======================================================================================================================
138 _mdns_tl16_init(mdns_tl16_t
* const tl
, const uint16_t type
, const uint16_t length
)
140 WriteBig16(tl
->type
, type
);
141 WriteBig16(tl
->length
, length
);