Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Durbin | 1240d29 | 2014-03-10 14:13:27 -0500 | [diff] [blame^] | 28 | size_t bgets(struct buffer *input, void *output, size_t len) |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 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 | |
Aaron Durbin | 1240d29 | 2014-03-10 14:13:27 -0500 | [diff] [blame^] | 37 | size_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. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 44 | /* 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 | */ |
| 47 | static uint8_t get8(struct buffer *input) |
| 48 | { |
| 49 | uint8_t ret = *input->data++; |
| 50 | input->size--; |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | static 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 | |
| 62 | static 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 | |
| 70 | static 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 | |
| 79 | static void put8(struct buffer *input, uint8_t val) |
| 80 | { |
| 81 | input->data[input->size] = val; |
| 82 | input->size++; |
| 83 | } |
| 84 | |
| 85 | static void put16be(struct buffer *input, uint16_t val) |
| 86 | { |
| 87 | put8(input, val >> 8); |
| 88 | put8(input, val); |
| 89 | } |
| 90 | |
| 91 | static void put32be(struct buffer *input, uint32_t val) |
| 92 | { |
| 93 | put16be(input, val >> 16); |
| 94 | put16be(input, val); |
| 95 | } |
| 96 | |
| 97 | static void put64be(struct buffer *input, uint64_t val) |
| 98 | { |
| 99 | put32be(input, val >> 32); |
| 100 | put32be(input, val); |
| 101 | } |
| 102 | |
| 103 | static 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 | |
| 111 | static 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 | |
| 119 | static 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 | |
| 130 | static void put16le(struct buffer *input, uint16_t val) |
| 131 | { |
| 132 | put8(input, val); |
| 133 | put8(input, val >> 8); |
| 134 | } |
| 135 | |
| 136 | static void put32le(struct buffer *input, uint32_t val) |
| 137 | { |
| 138 | put16le(input, val); |
| 139 | put16le(input, val >> 16); |
| 140 | } |
| 141 | |
| 142 | static void put64le(struct buffer *input, uint64_t val) |
| 143 | { |
| 144 | put32le(input, val); |
| 145 | put32le(input, val >> 32); |
| 146 | } |
| 147 | |
| 148 | struct xdr xdr_be = { |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 149 | get8, get16be, get32be, get64be, |
| 150 | put8, put16be, put32be, put64be |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | struct xdr xdr_le = { |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 154 | get8, get16le, get32le, get64le, |
| 155 | put8, put16le, put32le, put64le |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 156 | }; |