utility.h revision ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3c
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
23ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah#define assert(expr) do { if (!(expr)) { \
24ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah      error("assert fail: %s at %s:%d\n", \
25ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah            #expr, __FILE__, __LINE__); }} while(0)
26ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah
27ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and
28ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * [lsw] forming the most and least signficant 16-bit words.
29ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah */
30ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define CombineUint16Pair(msw,lsw) (((msw) << 16) |     \
31ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah                                    (((lsw)) & 0xFFFF))
32ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Return the minimum of (a) or (b). */
33ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define Min(a, b) (((a) < (b)) ? (a) : (b))
34ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah
35322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Allocate [size] bytes and return a pointer to the allocated memory. Abort
36322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * on error.
37322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
38322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Malloc(size_t size);
39322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
40322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Free memory pointed by [ptr] previously allocated by Malloc(). */
41322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid Free(void* ptr);
42322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
43322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copy [n] bytes from [src] to [dest]. */
44322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Memcpy(void* dest, const void* src, size_t n);
45322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
46d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Set [n] bytes starting at [s] to [c]. */
47d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahvoid* Memset(void *dest, const uint8_t c, size_t n);
48d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah
4908df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah/* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match,
5008df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah * 1 if they don't. Time taken to perform the comparison is only dependent on
51322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * [n] and not on the relationship of the match between [s1] and [s2].
52322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
53322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahint SafeMemcmp(const void* s1, const void* s2, size_t n);
54322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
55d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Track remaining data to be read in a buffer. */
56d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahtypedef struct MemcpyState {
57d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah  void* remaining_buf;
58e450be4ce4b749ad3a24dd347828b00da2802138Gaurav Shah  uint64_t remaining_len;  /* Remaining length of the buffer. */
59e450be4ce4b749ad3a24dd347828b00da2802138Gaurav Shah  uint8_t overrun;  /* Flag set to 1 when an overrun occurs. */
60d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah} MemcpyState;
61d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah
62d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Copy [len] bytes into [dst] only if there's enough data to read according
63d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * to [state].
64f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On success, return [dst] and update [state].
65d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * On failure, return NULL, set remaining len in state to -1.
66d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah *
67d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * Useful for iterating through a binary blob to populate a struct. After the
68d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * first failure (buffer overrun), successive calls will always fail.
69d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah */
70e450be4ce4b749ad3a24dd347828b00da2802138Gaurav Shahvoid* StatefulMemcpy(MemcpyState* state, void* dst, uint64_t len);
71d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah
72f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah/* Like StatefulMemcpy() but copies in the opposite direction, populating
73f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * data from [src] into the buffer encapsulated in state [state].
74f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On success, return [src] and update [state].
75f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On failure, return NULL, set remaining_len in state to -1.
76f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah *
77f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * Useful for iterating through a structure to populate a binary blob. After the
78f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * first failure (buffer overrun), successive calls will always fail.
79f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah */
80e450be4ce4b749ad3a24dd347828b00da2802138Gaurav Shahconst void* StatefulMemcpy_r(MemcpyState* state, const void* src, uint64_t len);
81d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah
82322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif  /* VBOOT_REFERENCE_UTILITY_H_ */
83