nb/intel/i440bx: Move to RELOCATABLE_RAMSTAGE
[coreboot.git] / util / cbfstool / xdr.c
blob06cc91f40978696112e9678f749559c0c59e9c44
1 /*
2 * cbfstool, CLI utility for CBFS file manipulation
4 * Copyright 2013 Google Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include <unistd.h>
21 #include <stdint.h>
22 #include "common.h"
24 size_t bgets(struct buffer *input, void *output, size_t len)
26 len = input->size < len ? input->size : len;
27 memmove(output, input->data, len);
28 input->data += len;
29 input->size -= len;
30 return len;
33 size_t bputs(struct buffer *b, const void *data, size_t len)
35 memmove(&b->data[b->size], data, len);
36 b->size += len;
37 return len;
40 /* The assumption in all this code is that we're given a pointer to enough data.
41 * Hence, we do not check for underflow.
43 static uint8_t get8(struct buffer *input)
45 uint8_t ret = *input->data++;
46 input->size--;
47 return ret;
50 static uint16_t get16be(struct buffer *input)
52 uint16_t ret;
53 ret = get8(input) << 8;
54 ret |= get8(input);
55 return ret;
58 static uint32_t get32be(struct buffer *input)
60 uint32_t ret;
61 ret = get16be(input) << 16;
62 ret |= get16be(input);
63 return ret;
66 static uint64_t get64be(struct buffer *input)
68 uint64_t ret;
69 ret = get32be(input);
70 ret <<= 32;
71 ret |= get32be(input);
72 return ret;
75 static void put8(struct buffer *input, uint8_t val)
77 input->data[input->size] = val;
78 input->size++;
81 static void put16be(struct buffer *input, uint16_t val)
83 put8(input, val >> 8);
84 put8(input, val);
87 static void put32be(struct buffer *input, uint32_t val)
89 put16be(input, val >> 16);
90 put16be(input, val);
93 static void put64be(struct buffer *input, uint64_t val)
95 put32be(input, val >> 32);
96 put32be(input, val);
99 static uint16_t get16le(struct buffer *input)
101 uint16_t ret;
102 ret = get8(input);
103 ret |= get8(input) << 8;
104 return ret;
107 static uint32_t get32le(struct buffer *input)
109 uint32_t ret;
110 ret = get16le(input);
111 ret |= get16le(input) << 16;
112 return ret;
115 static uint64_t get64le(struct buffer *input)
117 uint64_t ret;
118 uint32_t low;
119 low = get32le(input);
120 ret = get32le(input);
121 ret <<= 32;
122 ret |= low;
123 return ret;
126 static void put16le(struct buffer *input, uint16_t val)
128 put8(input, val);
129 put8(input, val >> 8);
132 static void put32le(struct buffer *input, uint32_t val)
134 put16le(input, val);
135 put16le(input, val >> 16);
138 static void put64le(struct buffer *input, uint64_t val)
140 put32le(input, val);
141 put32le(input, val >> 32);
144 struct xdr xdr_be = {
145 get8, get16be, get32be, get64be,
146 put8, put16be, put32be, put64be
149 struct xdr xdr_le = {
150 get8, get16le, get32le, get64le,
151 put8, put16le, put32le, put64le