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