1 /*
2  * Copyright (c) 2012, 2014, 2021 by Farsight Security, Inc.
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #include "dnstable-private.h"
18 
19 size_t
triplet_pack(uint8_t * orig_buf,uint64_t val1,uint64_t val2,uint64_t val3)20 triplet_pack(uint8_t *orig_buf, uint64_t val1, uint64_t val2, uint64_t val3)
21 {
22 	uint8_t *buf = orig_buf;
23 	buf += mtbl_varint_encode64(buf, val1);
24 	buf += mtbl_varint_encode64(buf, val2);
25 	buf += mtbl_varint_encode64(buf, val3);
26 	return (buf - orig_buf);
27 }
28 
29 dnstable_res
triplet_unpack(const uint8_t * buf,size_t len_buf,uint64_t * val1,uint64_t * val2,uint64_t * val3)30 triplet_unpack(const uint8_t *buf, size_t len_buf, uint64_t *val1, uint64_t *val2, uint64_t *val3)
31 {
32 	size_t bytes_read = 0;
33 	bytes_read += mtbl_varint_decode64(buf + bytes_read, val1);
34 	bytes_read += mtbl_varint_decode64(buf + bytes_read, val2);
35 	bytes_read += mtbl_varint_decode64(buf + bytes_read, val3);
36 	if (bytes_read == len_buf)
37 		return (dnstable_res_success);
38 	return (dnstable_res_failure);
39 }
40 
41 size_t
pair_pack(uint8_t * orig_buf,uint64_t val1,uint64_t val2)42 pair_pack(uint8_t *orig_buf, uint64_t val1, uint64_t val2)
43 {
44 	uint8_t *buf = orig_buf;
45 	buf += mtbl_varint_encode64(buf, val1);
46 	buf += mtbl_varint_encode64(buf, val2);
47 	return (buf - orig_buf);
48 }
49 
50 dnstable_res
pair_unpack(const uint8_t * buf,size_t len_buf,uint64_t * val1,uint64_t * val2)51 pair_unpack(const uint8_t *buf, size_t len_buf, uint64_t *val1, uint64_t *val2)
52 {
53 	size_t bytes_read = 0;
54 	bytes_read += mtbl_varint_decode64(buf + bytes_read, val1);
55 	bytes_read += mtbl_varint_decode64(buf + bytes_read, val2);
56 	if (bytes_read == len_buf)
57 		return (dnstable_res_success);
58 	return (dnstable_res_failure);
59 }
60