OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 /* Helper functions/wrappers for memory allocations, manipulation and | 6 /* Helper functions/wrappers for memory allocations, manipulation and |
7 * comparison. | 7 * comparison. |
8 */ | 8 */ |
9 | 9 |
10 #ifndef VBOOT_FIRMWARE_LIB_UTILITY_H_ | 10 #ifndef VBOOT_FIRMWARE_LIB_UTILITY_H_ |
11 #define VBOOT_FIRMWARE_LIB_UTILITY_H_ | 11 #define VBOOT_FIRMWARE_LIB_UTILITY_H_ |
12 | 12 |
13 #include <stdint.h> | 13 #include <stdint.h> |
14 #include <string.h> | 14 #include <string.h> |
15 | 15 |
16 #include "utility.h" | 16 #include "utility.h" |
17 | 17 |
18 /* Track remaining data to be read in a buffer. */ | 18 /* Track remaining data to be read in a buffer. */ |
19 typedef struct MemcpyState { | 19 typedef struct MemcpyState { |
20 void* remaining_buf; | 20 void* remaining_buf; |
21 uint64_t remaining_len; /* Remaining length of the buffer. */ | 21 uint64_t remaining_len; /* Remaining length of the buffer. */ |
22 uint8_t overrun; /* Flag set to 1 when an overrun occurs. */ | 22 uint8_t overrun; /* Flag set to 1 when an overrun occurs. */ |
23 } MemcpyState; | 23 } MemcpyState; |
24 | 24 |
| 25 /* Skip [len] bytes only if there's enough data to skip according |
| 26 * to [state]. |
| 27 * On success, return a meaningless but non-NULL pointer and updates [state]. |
| 28 * On failure, return NULL, set remaining_len in state to -1. |
| 29 * |
| 30 * Useful for iterating through a binary blob to populate a struct. After the |
| 31 * first failure (buffer overrun), successive calls will always fail. |
| 32 */ |
| 33 void* StatefulSkip(MemcpyState* state, uint64_t len); |
| 34 |
25 /* Copy [len] bytes into [dst] only if there's enough data to read according | 35 /* Copy [len] bytes into [dst] only if there's enough data to read according |
26 * to [state]. | 36 * to [state]. |
27 * On success, return [dst] and update [state]. | 37 * On success, return [dst] and update [state]. |
28 * On failure, return NULL, set remaining len in state to -1. | 38 * On failure, return NULL, set remaining len in state to -1. |
29 * | 39 * |
30 * Useful for iterating through a binary blob to populate a struct. After the | 40 * Useful for iterating through a binary blob to populate a struct. After the |
31 * first failure (buffer overrun), successive calls will always fail. | 41 * first failure (buffer overrun), successive calls will always fail. |
32 */ | 42 */ |
33 void* StatefulMemcpy(MemcpyState* state, void* dst, uint64_t len); | 43 void* StatefulMemcpy(MemcpyState* state, void* dst, uint64_t len); |
34 | 44 |
(...skipping 11 matching lines...) Expand all Loading... |
46 * a constant value. | 56 * a constant value. |
47 * On success, return a meaningless but non-NULL pointer and updates [state]. | 57 * On success, return a meaningless but non-NULL pointer and updates [state]. |
48 * On failure, return NULL, set remaining_len in state to -1. | 58 * On failure, return NULL, set remaining_len in state to -1. |
49 * | 59 * |
50 * After the first failure (buffer overrun), successive calls will always fail. | 60 * After the first failure (buffer overrun), successive calls will always fail. |
51 */ | 61 */ |
52 const void* StatefulMemset_r(MemcpyState* state, const uint8_t val, | 62 const void* StatefulMemset_r(MemcpyState* state, const uint8_t val, |
53 uint64_t len); | 63 uint64_t len); |
54 | 64 |
55 #endif /* VBOOT_FIRMWARE_LIB_UTILITY_H_ */ | 65 #endif /* VBOOT_FIRMWARE_LIB_UTILITY_H_ */ |
OLD | NEW |