utility.h revision ce0cc30e55987f3faac9f9bacdf5d66c86ede08e
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 13d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah#include <inttypes.h> 14322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#include <string.h> 15322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 16ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and 17ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * [lsw] forming the most and least signficant 16-bit words. 18ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah */ 19ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define CombineUint16Pair(msw,lsw) (((msw) << 16) | \ 20ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah (((lsw)) & 0xFFFF)) 21ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Return the minimum of (a) or (b). */ 22ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define Min(a, b) (((a) < (b)) ? (a) : (b)) 23ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah 24322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Allocate [size] bytes and return a pointer to the allocated memory. Abort 25322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * on error. 26322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */ 27322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Malloc(size_t size); 28322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 29322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Free memory pointed by [ptr] previously allocated by Malloc(). */ 30322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid Free(void* ptr); 31322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 32322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copy [n] bytes from [src] to [dest]. */ 33322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Memcpy(void* dest, const void* src, size_t n); 34322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 35d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Set [n] bytes starting at [s] to [c]. */ 36d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahvoid* Memset(void *dest, const uint8_t c, size_t n); 37d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 3808df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah/* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match, 3908df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah * 1 if they don't. Time taken to perform the comparison is only dependent on 40322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * [n] and not on the relationship of the match between [s1] and [s2]. 41322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */ 42322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahint SafeMemcmp(const void* s1, const void* s2, size_t n); 43322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 44d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Track remaining data to be read in a buffer. */ 45d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahtypedef struct MemcpyState { 46d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah void* remaining_buf; 47456678b0c45d9fa6ad45d5dc6769051a731207f3Gaurav Shah uint64_t remaining_len; 48d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah} MemcpyState; 49d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 50d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Copy [len] bytes into [dst] only if there's enough data to read according 51d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * to [state]. 52f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On success, return [dst] and update [state]. 53d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * On failure, return NULL, set remaining len in state to -1. 54d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * 55d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * Useful for iterating through a binary blob to populate a struct. After the 56d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * first failure (buffer overrun), successive calls will always fail. 57d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah */ 58d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahvoid* StatefulMemcpy(MemcpyState* state, void* dst, int len); 59d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 60f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah/* Like StatefulMemcpy() but copies in the opposite direction, populating 61f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * data from [src] into the buffer encapsulated in state [state]. 62f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On success, return [src] and update [state]. 63f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On failure, return NULL, set remaining_len in state to -1. 64f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * 65f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * Useful for iterating through a structure to populate a binary blob. After the 66f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * first failure (buffer overrun), successive calls will always fail. 67f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah */ 68f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shahconst void* StatefulMemcpy_r(MemcpyState* state, const void* src, int len); 69d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 70322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif /* VBOOT_REFERENCE_UTILITY_H_ */ 71