utility.h revision d067712ff9caaef6685ea147ba10a0a40f50222c
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 16322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Allocate [size] bytes and return a pointer to the allocated memory. Abort 17322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * on error. 18322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */ 19322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Malloc(size_t size); 20322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 21322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Free memory pointed by [ptr] previously allocated by Malloc(). */ 22322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid Free(void* ptr); 23322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 24322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copy [n] bytes from [src] to [dest]. */ 25322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Memcpy(void* dest, const void* src, size_t n); 26322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 27d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Set [n] bytes starting at [s] to [c]. */ 28d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahvoid* Memset(void *dest, const uint8_t c, size_t n); 29d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 30322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Compare [n] bytes starting at [s1] with [s2] and return 1 if they match, 31322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * 0 if they don't. Time taken to perform the comparison is only dependent on 32322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * [n] and not on the relationship of the match between [s1] and [s2]. 33322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */ 34322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahint SafeMemcmp(const void* s1, const void* s2, size_t n); 35322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah 36d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Track remaining data to be read in a buffer. */ 37d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahtypedef struct MemcpyState { 38d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah void* remaining_buf; 39d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah int remaining_len; 40d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah} MemcpyState; 41d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 42d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Copy [len] bytes into [dst] only if there's enough data to read according 43d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * to [state]. 44d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * On success, return [dst] and update [state].. 45d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * On failure, return NULL, set remaining len in state to -1. 46d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * 47d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * Useful for iterating through a binary blob to populate a struct. After the 48d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah * first failure (buffer overrun), successive calls will always fail. 49d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah */ 50d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahvoid* StatefulMemcpy(MemcpyState* state, void* dst, int len); 51d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 52d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah 53322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif /* VBOOT_REFERENCE_UTILITY_H_ */ 54