Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: src/platform/vboot_reference/vboot_firmware/include/utility.h

Issue 2255006: StatefulMem* functions should be library functions. (Closed) Base URL: ssh://git@chromiumos-git/chromeos
Patch Set: Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h> 13 #include <stdint.h>
14 #include <string.h> 14 #include <string.h>
15 15
16 /* Outputs an error message and quits. */ 16 /* Outputs an error message and quits. */
17 void error(const char *format, ...); 17 void error(const char *format, ...);
18 18
19 /* Outputs debug/warning messages. */ 19 /* Outputs debug/warning messages. */
20 void debug(const char *format, ...); 20 void debug(const char *format, ...);
21 21
22
23 #define assert(expr) do { if (!(expr)) { \ 22 #define assert(expr) do { if (!(expr)) { \
24 error("assert fail: %s at %s:%d\n", \ 23 error("assert fail: %s at %s:%d\n", \
25 #expr, __FILE__, __LINE__); }} while(0) 24 #expr, __FILE__, __LINE__); }} while(0)
26 25
27 /* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and 26 /* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and
28 * [lsw] forming the most and least signficant 16-bit words. 27 * [lsw] forming the most and least signficant 16-bit words.
29 */ 28 */
30 #define CombineUint16Pair(msw,lsw) (((msw) << 16) | \ 29 #define CombineUint16Pair(msw,lsw) (((msw) << 16) | \
31 (((lsw)) & 0xFFFF)) 30 (((lsw)) & 0xFFFF))
32 /* Return the minimum of (a) or (b). */ 31 /* Return the minimum of (a) or (b). */
(...skipping 18 matching lines...) Expand all
51 50
52 /* Set [n] bytes starting at [s] to [c]. */ 51 /* Set [n] bytes starting at [s] to [c]. */
53 void* Memset(void *dest, const uint8_t c, size_t n); 52 void* Memset(void *dest, const uint8_t c, size_t n);
54 53
55 /* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match, 54 /* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match,
56 * 1 if they don't. Time taken to perform the comparison is only dependent on 55 * 1 if they don't. Time taken to perform the comparison is only dependent on
57 * [n] and not on the relationship of the match between [s1] and [s2]. 56 * [n] and not on the relationship of the match between [s1] and [s2].
58 */ 57 */
59 int SafeMemcmp(const void* s1, const void* s2, size_t n); 58 int SafeMemcmp(const void* s1, const void* s2, size_t n);
60 59
61 /* Track remaining data to be read in a buffer. */ 60 /* Ensure that only our stub implementations are used, not standard C */
62 typedef struct MemcpyState { 61 #ifndef _STUB_IMPLEMENTATION_
63 void* remaining_buf; 62 #define malloc _do_not_use_standard_malloc
64 uint64_t remaining_len; /* Remaining length of the buffer. */ 63 #define free _do_not_use_standard_free
65 uint8_t overrun; /* Flag set to 1 when an overrun occurs. */ 64 #define memcmp _do_not_use_standard_memcmp
66 } MemcpyState; 65 #define memcpy _do_not_use_standard_memcpy
67 66 #define memset _do_not_use_standard_memset
68 /* Copy [len] bytes into [dst] only if there's enough data to read according 67 #endif
69 * to [state].
70 * On success, return [dst] and update [state].
71 * On failure, return NULL, set remaining len in state to -1.
72 *
73 * Useful for iterating through a binary blob to populate a struct. After the
74 * first failure (buffer overrun), successive calls will always fail.
75 */
76 void* StatefulMemcpy(MemcpyState* state, void* dst, uint64_t len);
77
78 /* Like StatefulMemcpy() but copies in the opposite direction, populating
79 * data from [src] into the buffer encapsulated in state [state].
80 * On success, return [src] and update [state].
81 * On failure, return NULL, set remaining_len in state to -1.
82 *
83 * Useful for iterating through a structure to populate a binary blob. After the
84 * first failure (buffer overrun), successive calls will always fail.
85 */
86 const void* StatefulMemcpy_r(MemcpyState* state, const void* src, uint64_t len);
87
88 /* Like StatefulMemcpy_r() but fills a portion of the encapsulated buffer with
89 * a constant value.
90 * On success, return a meaningless but non-NULL pointer and updates [state].
91 * On failure, return NULL, set remaining_len in state to -1.
92 *
93 * After the first failure (buffer overrun), successive calls will always fail.
94 */
95 const void* StatefulMemset_r(MemcpyState* state, const uint8_t val,
96 uint64_t len);
97 68
98 69
99 #endif /* VBOOT_REFERENCE_UTILITY_H_ */ 70 #endif /* VBOOT_REFERENCE_UTILITY_H_ */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698