blob: 6a83beef78574b2f6c74c653d35bd0d6d2d0f5ec [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -08002
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <ctype.h>
7#include <unistd.h>
8#include <stdint.h>
9#include "common.h"
10
Aaron Durbin1240d292014-03-10 14:13:27 -050011size_t bgets(struct buffer *input, void *output, size_t len)
Ronald G. Minnich3fcde222014-02-04 17:35:44 -080012{
13 len = input->size < len ? input->size : len;
14 memmove(output, input->data, len);
15 input->data += len;
16 input->size -= len;
17 return len;
18}
19
Aaron Durbin1240d292014-03-10 14:13:27 -050020size_t bputs(struct buffer *b, const void *data, size_t len)
21{
22 memmove(&b->data[b->size], data, len);
23 b->size += len;
24 return len;
25}
26
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080027/* The assumption in all this code is that we're given a pointer to enough data.
28 * Hence, we do not check for underflow.
29 */
30static uint8_t get8(struct buffer *input)
31{
32 uint8_t ret = *input->data++;
33 input->size--;
34 return ret;
35}
36
37static uint16_t get16be(struct buffer *input)
38{
39 uint16_t ret;
40 ret = get8(input) << 8;
41 ret |= get8(input);
42 return ret;
43}
44
45static uint32_t get32be(struct buffer *input)
46{
47 uint32_t ret;
48 ret = get16be(input) << 16;
49 ret |= get16be(input);
50 return ret;
51}
52
53static uint64_t get64be(struct buffer *input)
54{
55 uint64_t ret;
56 ret = get32be(input);
57 ret <<= 32;
58 ret |= get32be(input);
59 return ret;
60}
61
62static void put8(struct buffer *input, uint8_t val)
63{
64 input->data[input->size] = val;
65 input->size++;
66}
67
68static void put16be(struct buffer *input, uint16_t val)
69{
70 put8(input, val >> 8);
71 put8(input, val);
72}
73
74static void put32be(struct buffer *input, uint32_t val)
75{
76 put16be(input, val >> 16);
77 put16be(input, val);
78}
79
80static void put64be(struct buffer *input, uint64_t val)
81{
82 put32be(input, val >> 32);
83 put32be(input, val);
84}
85
86static uint16_t get16le(struct buffer *input)
87{
88 uint16_t ret;
89 ret = get8(input);
90 ret |= get8(input) << 8;
91 return ret;
92}
93
94static uint32_t get32le(struct buffer *input)
95{
96 uint32_t ret;
97 ret = get16le(input);
98 ret |= get16le(input) << 16;
99 return ret;
100}
101
102static uint64_t get64le(struct buffer *input)
103{
104 uint64_t ret;
105 uint32_t low;
106 low = get32le(input);
107 ret = get32le(input);
108 ret <<= 32;
109 ret |= low;
110 return ret;
111}
112
113static void put16le(struct buffer *input, uint16_t val)
114{
115 put8(input, val);
116 put8(input, val >> 8);
117}
118
119static void put32le(struct buffer *input, uint32_t val)
120{
121 put16le(input, val);
122 put16le(input, val >> 16);
123}
124
125static void put64le(struct buffer *input, uint64_t val)
126{
127 put32le(input, val);
128 put32le(input, val >> 32);
129}
130
131struct xdr xdr_be = {
Aaron Durbin01650042014-03-05 16:38:26 -0600132 get8, get16be, get32be, get64be,
133 put8, put16be, put32be, put64be
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800134};
135
136struct xdr xdr_le = {
Aaron Durbin01650042014-03-05 16:38:26 -0600137 get8, get16le, get32le, get64le,
138 put8, put16le, put32le, put64le
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800139};