treewide: Move device_tree to commonlib
[coreboot2.git] / util / cbfstool / xdr.c
blob6a83beef78574b2f6c74c653d35bd0d6d2d0f5ec
1 /* SPDX-License-Identifier: GPL-2.0-only */
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"
11 size_t bgets(struct buffer *input, void *output, size_t len)
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;
20 size_t bputs(struct buffer *b, const void *data, size_t len)
22 memmove(&b->data[b->size], data, len);
23 b->size += len;
24 return len;
27 /* The assumption in all this code is that we're given a pointer to enough data.
28 * Hence, we do not check for underflow.
30 static uint8_t get8(struct buffer *input)
32 uint8_t ret = *input->data++;
33 input->size--;
34 return ret;
37 static uint16_t get16be(struct buffer *input)
39 uint16_t ret;
40 ret = get8(input) << 8;
41 ret |= get8(input);
42 return ret;
45 static uint32_t get32be(struct buffer *input)
47 uint32_t ret;
48 ret = get16be(input) << 16;
49 ret |= get16be(input);
50 return ret;
53 static uint64_t get64be(struct buffer *input)
55 uint64_t ret;
56 ret = get32be(input);
57 ret <<= 32;
58 ret |= get32be(input);
59 return ret;
62 static void put8(struct buffer *input, uint8_t val)
64 input->data[input->size] = val;
65 input->size++;
68 static void put16be(struct buffer *input, uint16_t val)
70 put8(input, val >> 8);
71 put8(input, val);
74 static void put32be(struct buffer *input, uint32_t val)
76 put16be(input, val >> 16);
77 put16be(input, val);
80 static void put64be(struct buffer *input, uint64_t val)
82 put32be(input, val >> 32);
83 put32be(input, val);
86 static uint16_t get16le(struct buffer *input)
88 uint16_t ret;
89 ret = get8(input);
90 ret |= get8(input) << 8;
91 return ret;
94 static uint32_t get32le(struct buffer *input)
96 uint32_t ret;
97 ret = get16le(input);
98 ret |= get16le(input) << 16;
99 return ret;
102 static uint64_t get64le(struct buffer *input)
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;
113 static void put16le(struct buffer *input, uint16_t val)
115 put8(input, val);
116 put8(input, val >> 8);
119 static void put32le(struct buffer *input, uint32_t val)
121 put16le(input, val);
122 put16le(input, val >> 16);
125 static void put64le(struct buffer *input, uint64_t val)
127 put32le(input, val);
128 put32le(input, val >> 32);
131 struct xdr xdr_be = {
132 get8, get16be, get32be, get64be,
133 put8, put16be, put32be, put64be
136 struct xdr xdr_le = {
137 get8, get16le, get32le, get64le,
138 put8, put16le, put32le, put64le