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. |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 14 | */ |
| 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 Durbin | 1240d29 | 2014-03-10 14:13:27 -0500 | [diff] [blame] | 24 | size_t bgets(struct buffer *input, void *output, size_t len) |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 25 | { |
| 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 Durbin | 1240d29 | 2014-03-10 14:13:27 -0500 | [diff] [blame] | 33 | size_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. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 40 | /* 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 | */ |
| 43 | static uint8_t get8(struct buffer *input) |
| 44 | { |
| 45 | uint8_t ret = *input->data++; |
| 46 | input->size--; |
| 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | static 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 | |
| 58 | static 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 | |
| 66 | static 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 | |
| 75 | static void put8(struct buffer *input, uint8_t val) |
| 76 | { |
| 77 | input->data[input->size] = val; |
| 78 | input->size++; |
| 79 | } |
| 80 | |
| 81 | static void put16be(struct buffer *input, uint16_t val) |
| 82 | { |
| 83 | put8(input, val >> 8); |
| 84 | put8(input, val); |
| 85 | } |
| 86 | |
| 87 | static void put32be(struct buffer *input, uint32_t val) |
| 88 | { |
| 89 | put16be(input, val >> 16); |
| 90 | put16be(input, val); |
| 91 | } |
| 92 | |
| 93 | static void put64be(struct buffer *input, uint64_t val) |
| 94 | { |
| 95 | put32be(input, val >> 32); |
| 96 | put32be(input, val); |
| 97 | } |
| 98 | |
| 99 | static 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 | |
| 107 | static 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 | |
| 115 | static 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 | |
| 126 | static void put16le(struct buffer *input, uint16_t val) |
| 127 | { |
| 128 | put8(input, val); |
| 129 | put8(input, val >> 8); |
| 130 | } |
| 131 | |
| 132 | static void put32le(struct buffer *input, uint32_t val) |
| 133 | { |
| 134 | put16le(input, val); |
| 135 | put16le(input, val >> 16); |
| 136 | } |
| 137 | |
| 138 | static void put64le(struct buffer *input, uint64_t val) |
| 139 | { |
| 140 | put32le(input, val); |
| 141 | put32le(input, val >> 32); |
| 142 | } |
| 143 | |
| 144 | struct xdr xdr_be = { |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 145 | get8, get16be, get32be, get64be, |
| 146 | put8, put16be, put32be, put64be |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | struct xdr xdr_le = { |
Aaron Durbin | 0165004 | 2014-03-05 16:38:26 -0600 | [diff] [blame] | 150 | get8, get16le, get32le, get64le, |
| 151 | put8, put16le, put32le, put64le |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 152 | }; |