utility.h revision d6ff721eb077759ac8f672dc15094e65968bc45e
1322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * Use of this source code is governed by a BSD-style license that can be
3322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * found in the LICENSE file.
4322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
5322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
6322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Helper functions/wrappers for memory allocations, manipulation and
7322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * comparison.
8322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
9322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
10322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#ifndef VBOOT_REFERENCE_UTILITY_H_
11322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define VBOOT_REFERENCE_UTILITY_H_
12322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
13ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah#include <stdint.h>
14322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#include <string.h>
15322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
16ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah/* Outputs an error message and quits. */
17ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shahvoid error(const char *format, ...);
18ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah
19ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah/* Outputs debug/warning messages. */
20ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shahvoid debug(const char *format, ...);
21ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah
22ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah#define assert(expr) do { if (!(expr)) { \
23ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah      error("assert fail: %s at %s:%d\n", \
24ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah            #expr, __FILE__, __LINE__); }} while(0)
25ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah
26ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and
27ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * [lsw] forming the most and least signficant 16-bit words.
28ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah */
29ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define CombineUint16Pair(msw,lsw) (((msw) << 16) |     \
30ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah                                    (((lsw)) & 0xFFFF))
31ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Return the minimum of (a) or (b). */
32ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define Min(a, b) (((a) < (b)) ? (a) : (b))
33ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah
34322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Allocate [size] bytes and return a pointer to the allocated memory. Abort
35322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * on error.
36322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
37322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Malloc(size_t size);
38322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
39322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Free memory pointed by [ptr] previously allocated by Malloc(). */
40322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid Free(void* ptr);
41322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
4237f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo/* Compare [n] bytes in [src1] and [src2]
4337f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * Returns an integer less than, equal to, or greater than zero if the first [n]
4437f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * bytes of [src1] is found, respectively, to be less than, to match, or be
4537f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * greater than the first n bytes of [src2]. */
4637f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Loint Memcmp(const void* src1, const void* src2, size_t n);
4737f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo
48322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copy [n] bytes from [src] to [dest]. */
49322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Memcpy(void* dest, const void* src, size_t n);
50322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
51d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Set [n] bytes starting at [s] to [c]. */
52d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahvoid* Memset(void *dest, const uint8_t c, size_t n);
53d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah
5408df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah/* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match,
5508df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah * 1 if they don't. Time taken to perform the comparison is only dependent on
56322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * [n] and not on the relationship of the match between [s1] and [s2].
57322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
58322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahint SafeMemcmp(const void* s1, const void* s2, size_t n);
59322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
60d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson/* Ensure that only our stub implementations are used, not standard C */
61d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#ifndef _STUB_IMPLEMENTATION_
62d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define malloc _do_not_use_standard_malloc
63d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define free _do_not_use_standard_free
64d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcmp _do_not_use_standard_memcmp
65d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcpy _do_not_use_standard_memcpy
66d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memset _do_not_use_standard_memset
67d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#endif
68f5db4b86fa5569285dfc8373fd8977e1998b8066Bill Richardson
69f5db4b86fa5569285dfc8373fd8977e1998b8066Bill Richardson
70322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif  /* VBOOT_REFERENCE_UTILITY_H_ */
71