blob: ddc282ed481065dcfd1eca610cf2ef6b585bc76f [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
Ronald G. Minnich3fcde222014-02-04 17:35:44 -080028int bgets(struct buffer *input, void *output, size_t len)
29{
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
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080037/* The assumption in all this code is that we're given a pointer to enough data.
38 * Hence, we do not check for underflow.
39 */
40static uint8_t get8(struct buffer *input)
41{
42 uint8_t ret = *input->data++;
43 input->size--;
44 return ret;
45}
46
47static uint16_t get16be(struct buffer *input)
48{
49 uint16_t ret;
50 ret = get8(input) << 8;
51 ret |= get8(input);
52 return ret;
53}
54
55static uint32_t get32be(struct buffer *input)
56{
57 uint32_t ret;
58 ret = get16be(input) << 16;
59 ret |= get16be(input);
60 return ret;
61}
62
63static uint64_t get64be(struct buffer *input)
64{
65 uint64_t ret;
66 ret = get32be(input);
67 ret <<= 32;
68 ret |= get32be(input);
69 return ret;
70}
71
72static void put8(struct buffer *input, uint8_t val)
73{
74 input->data[input->size] = val;
75 input->size++;
76}
77
78static void put16be(struct buffer *input, uint16_t val)
79{
80 put8(input, val >> 8);
81 put8(input, val);
82}
83
84static void put32be(struct buffer *input, uint32_t val)
85{
86 put16be(input, val >> 16);
87 put16be(input, val);
88}
89
90static void put64be(struct buffer *input, uint64_t val)
91{
92 put32be(input, val >> 32);
93 put32be(input, val);
94}
95
96static uint16_t get16le(struct buffer *input)
97{
98 uint16_t ret;
99 ret = get8(input);
100 ret |= get8(input) << 8;
101 return ret;
102}
103
104static uint32_t get32le(struct buffer *input)
105{
106 uint32_t ret;
107 ret = get16le(input);
108 ret |= get16le(input) << 16;
109 return ret;
110}
111
112static uint64_t get64le(struct buffer *input)
113{
114 uint64_t ret;
115 uint32_t low;
116 low = get32le(input);
117 ret = get32le(input);
118 ret <<= 32;
119 ret |= low;
120 return ret;
121}
122
123static void put16le(struct buffer *input, uint16_t val)
124{
125 put8(input, val);
126 put8(input, val >> 8);
127}
128
129static void put32le(struct buffer *input, uint32_t val)
130{
131 put16le(input, val);
132 put16le(input, val >> 16);
133}
134
135static void put64le(struct buffer *input, uint64_t val)
136{
137 put32le(input, val);
138 put32le(input, val >> 32);
139}
140
141struct xdr xdr_be = {
142 get16be, get32be, get64be,
143 put16be, put32be, put64be
144};
145
146struct xdr xdr_le = {
147 get16le, get32le, get64le,
148 put16le, put32le, put64le
149};