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