blob: df2c4ba4a0bd40628703ca6e200525b446a5784e [file] [log] [blame]
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -08001 /*
2 * cbfstool, CLI utility for CBFS file manipulation
3 *
4 * Copyright 2013 Google Inc.
5 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <ctype.h>
24#include <unistd.h>
25#include <stdint.h>
26#include "common.h"
27
Aaron Durbin1240d292014-03-10 14:13:27 -050028size_t bgets(struct buffer *input, void *output, size_t len)
Ronald G. Minnich3fcde222014-02-04 17:35:44 -080029{
30 len = input->size < len ? input->size : len;
31 memmove(output, input->data, len);
32 input->data += len;
33 input->size -= len;
34 return len;
35}
36
Aaron Durbin1240d292014-03-10 14:13:27 -050037size_t bputs(struct buffer *b, const void *data, size_t len)
38{
39 memmove(&b->data[b->size], data, len);
40 b->size += len;
41 return len;
42}
43
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080044/* The assumption in all this code is that we're given a pointer to enough data.
45 * Hence, we do not check for underflow.
46 */
47static uint8_t get8(struct buffer *input)
48{
49 uint8_t ret = *input->data++;
50 input->size--;
51 return ret;
52}
53
54static uint16_t get16be(struct buffer *input)
55{
56 uint16_t ret;
57 ret = get8(input) << 8;
58 ret |= get8(input);
59 return ret;
60}
61
62static uint32_t get32be(struct buffer *input)
63{
64 uint32_t ret;
65 ret = get16be(input) << 16;
66 ret |= get16be(input);
67 return ret;
68}
69
70static uint64_t get64be(struct buffer *input)
71{
72 uint64_t ret;
73 ret = get32be(input);
74 ret <<= 32;
75 ret |= get32be(input);
76 return ret;
77}
78
79static void put8(struct buffer *input, uint8_t val)
80{
81 input->data[input->size] = val;
82 input->size++;
83}
84
85static void put16be(struct buffer *input, uint16_t val)
86{
87 put8(input, val >> 8);
88 put8(input, val);
89}
90
91static void put32be(struct buffer *input, uint32_t val)
92{
93 put16be(input, val >> 16);
94 put16be(input, val);
95}
96
97static void put64be(struct buffer *input, uint64_t val)
98{
99 put32be(input, val >> 32);
100 put32be(input, val);
101}
102
103static uint16_t get16le(struct buffer *input)
104{
105 uint16_t ret;
106 ret = get8(input);
107 ret |= get8(input) << 8;
108 return ret;
109}
110
111static uint32_t get32le(struct buffer *input)
112{
113 uint32_t ret;
114 ret = get16le(input);
115 ret |= get16le(input) << 16;
116 return ret;
117}
118
119static uint64_t get64le(struct buffer *input)
120{
121 uint64_t ret;
122 uint32_t low;
123 low = get32le(input);
124 ret = get32le(input);
125 ret <<= 32;
126 ret |= low;
127 return ret;
128}
129
130static void put16le(struct buffer *input, uint16_t val)
131{
132 put8(input, val);
133 put8(input, val >> 8);
134}
135
136static void put32le(struct buffer *input, uint32_t val)
137{
138 put16le(input, val);
139 put16le(input, val >> 16);
140}
141
142static void put64le(struct buffer *input, uint64_t val)
143{
144 put32le(input, val);
145 put32le(input, val >> 32);
146}
147
148struct xdr xdr_be = {
Aaron Durbin01650042014-03-05 16:38:26 -0600149 get8, get16be, get32be, get64be,
150 put8, put16be, put32be, put64be
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800151};
152
153struct xdr xdr_le = {
Aaron Durbin01650042014-03-05 16:38:26 -0600154 get8, get16le, get32le, get64le,
155 put8, put16le, put32le, put64le
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800156};