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_REFERENCE_UTILITY_H_ | 10 #ifndef VBOOT_REFERENCE_UTILITY_H_ |
11 #define VBOOT_REFERENCE_UTILITY_H_ | 11 #define VBOOT_REFERENCE_UTILITY_H_ |
12 | 12 |
13 #include <inttypes.h> | 13 #include <inttypes.h> |
14 #include <string.h> | 14 #include <string.h> |
15 | 15 |
16 /* Allocate [size] bytes and return a pointer to the allocated memory. Abort | 16 /* Allocate [size] bytes and return a pointer to the allocated memory. Abort |
17 * on error. | 17 * on error. |
18 */ | 18 */ |
19 void* Malloc(size_t size); | 19 void* Malloc(size_t size); |
20 | 20 |
21 /* Free memory pointed by [ptr] previously allocated by Malloc(). */ | 21 /* Free memory pointed by [ptr] previously allocated by Malloc(). */ |
22 void Free(void* ptr); | 22 void Free(void* ptr); |
23 | 23 |
24 /* Copy [n] bytes from [src] to [dest]. */ | 24 /* Copy [n] bytes from [src] to [dest]. */ |
25 void* Memcpy(void* dest, const void* src, size_t n); | 25 void* Memcpy(void* dest, const void* src, size_t n); |
26 | 26 |
27 /* Set [n] bytes starting at [s] to [c]. */ | 27 /* Set [n] bytes starting at [s] to [c]. */ |
28 void* Memset(void *dest, const uint8_t c, size_t n); | 28 void* Memset(void *dest, const uint8_t c, size_t n); |
29 | 29 |
30 /* Compare [n] bytes starting at [s1] with [s2] and return 1 if they match, | 30 /* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match, |
31 * 0 if they don't. Time taken to perform the comparison is only dependent on | 31 * 1 if they don't. Time taken to perform the comparison is only dependent on |
32 * [n] and not on the relationship of the match between [s1] and [s2]. | 32 * [n] and not on the relationship of the match between [s1] and [s2]. |
33 */ | 33 */ |
34 int SafeMemcmp(const void* s1, const void* s2, size_t n); | 34 int SafeMemcmp(const void* s1, const void* s2, size_t n); |
35 | 35 |
36 /* Track remaining data to be read in a buffer. */ | 36 /* Track remaining data to be read in a buffer. */ |
37 typedef struct MemcpyState { | 37 typedef struct MemcpyState { |
38 void* remaining_buf; | 38 void* remaining_buf; |
39 int remaining_len; | 39 int remaining_len; |
40 } MemcpyState; | 40 } MemcpyState; |
41 | 41 |
42 /* Copy [len] bytes into [dst] only if there's enough data to read according | 42 /* Copy [len] bytes into [dst] only if there's enough data to read according |
43 * to [state]. | 43 * to [state]. |
44 * On success, return [dst] and update [state].. | 44 * On success, return [dst] and update [state].. |
45 * On failure, return NULL, set remaining len in state to -1. | 45 * On failure, return NULL, set remaining len in state to -1. |
46 * | 46 * |
47 * Useful for iterating through a binary blob to populate a struct. After the | 47 * Useful for iterating through a binary blob to populate a struct. After the |
48 * first failure (buffer overrun), successive calls will always fail. | 48 * first failure (buffer overrun), successive calls will always fail. |
49 */ | 49 */ |
50 void* StatefulMemcpy(MemcpyState* state, void* dst, int len); | 50 void* StatefulMemcpy(MemcpyState* state, void* dst, int len); |
51 | 51 |
52 | 52 |
53 #endif /* VBOOT_REFERENCE_UTILITY_H_ */ | 53 #endif /* VBOOT_REFERENCE_UTILITY_H_ */ |
OLD | NEW |