utility.h revision 37f6b55a25f337f555da1dfbe585d32cd004103d
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 4337f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo/* Compare [n] bytes in [src1] and [src2] 4437f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * Returns an integer less than, equal to, or greater than zero if the first [n] 4537f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * bytes of [src1] is found, respectively, to be less than, to match, or be 4637f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * greater than the first n bytes of [src2]. */ 4737f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Loint Memcmp(const void* src1, const void* src2, size_t n); 4837f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo 49322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copy [n] bytes from [src] to [dest]. */ 50322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Memcpy(void* dest, const void* src, size_t n); 51322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 52d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Set [n] bytes starting at [s] to [c]. */ 53d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahvoid* Memset(void *dest, const uint8_t c, size_t n); 54d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 5508df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah/* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match, 5608df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah * 1 if they don't. Time taken to perform the comparison is only dependent on 57322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * [n] and not on the relationship of the match between [s1] and [s2]. 58322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */ 59322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahint SafeMemcmp(const void* s1, const void* s2, size_t n); 60322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 61d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Track remaining data to be read in a buffer. */ 62d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahtypedef struct MemcpyState { 63d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah void* remaining_buf; 64e450be4ce4b749ad3a24dd347828b00da2802138Gaurav Shah uint64_t remaining_len; /* Remaining length of the buffer. */ 65e450be4ce4b749ad3a24dd347828b00da2802138Gaurav Shah uint8_t overrun; /* Flag set to 1 when an overrun occurs. */ 66d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah} MemcpyState; 67d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 68d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Copy [len] bytes into [dst] only if there's enough data to read according 69d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * to [state]. 70f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On success, return [dst] and update [state]. 71d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * On failure, return NULL, set remaining len in state to -1. 72d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * 73d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * Useful for iterating through a binary blob to populate a struct. After the 74d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * first failure (buffer overrun), successive calls will always fail. 75d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah */ 76e450be4ce4b749ad3a24dd347828b00da2802138Gaurav Shahvoid* StatefulMemcpy(MemcpyState* state, void* dst, uint64_t len); 77d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 78f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah/* Like StatefulMemcpy() but copies in the opposite direction, populating 79f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * data from [src] into the buffer encapsulated in state [state]. 80f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On success, return [src] and update [state]. 81f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * On failure, return NULL, set remaining_len in state to -1. 82f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * 83f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * Useful for iterating through a structure to populate a binary blob. After the 84f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah * first failure (buffer overrun), successive calls will always fail. 85f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah */ 86e450be4ce4b749ad3a24dd347828b00da2802138Gaurav Shahconst void* StatefulMemcpy_r(MemcpyState* state, const void* src, uint64_t len); 87d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 88322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif /* VBOOT_REFERENCE_UTILITY_H_ */ 89