Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Stub implementations of utility functions which call their linux-specific |
| 6 | * equivalents. |
| 7 | */ |
| 8 | |
| 9 | #include "utility.h" |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 13 | |
| 14 | void* Malloc(size_t size) { |
| 15 | void* p = malloc(size); |
| 16 | if (!p) { |
| 17 | /* Fatal Error. We must abort. */ |
| 18 | abort(); |
| 19 | } |
| 20 | return p; |
| 21 | } |
| 22 | |
| 23 | void Free(void* ptr) { |
| 24 | free(ptr); |
| 25 | } |
| 26 | |
| 27 | void* Memcpy(void* dest, const void* src, size_t n) { |
| 28 | return memcpy(dest, src, n); |
| 29 | } |
| 30 | |
Gaurav Shah | d067712 | 2010-02-04 19:35:03 -0800 | [diff] [blame] | 31 | void* Memset(void* dest, const uint8_t c, size_t n) { |
| 32 | while (n--) { |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 33 | *((uint8_t*)dest++) = c; |
Gaurav Shah | d067712 | 2010-02-04 19:35:03 -0800 | [diff] [blame] | 34 | } |
| 35 | return dest; |
| 36 | } |
| 37 | |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 38 | int SafeMemcmp(const void* s1, const void* s2, size_t n) { |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 39 | int match = 0; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 40 | const unsigned char* us1 = s1; |
| 41 | const unsigned char* us2 = s2; |
| 42 | while (n--) { |
| 43 | if (*us1++ != *us2++) |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 44 | match = 1; |
| 45 | } |
| 46 | |
| 47 | return match; |
| 48 | } |
Gaurav Shah | d067712 | 2010-02-04 19:35:03 -0800 | [diff] [blame] | 49 | |
| 50 | void* StatefulMemcpy(MemcpyState* state, void* dst, int len) { |
Gaurav Shah | d067712 | 2010-02-04 19:35:03 -0800 | [diff] [blame] | 51 | if (len > state->remaining_len) { |
| 52 | state->remaining_len = -1; |
| 53 | return NULL; |
| 54 | } |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame^] | 55 | Memcpy(dst, state->remaining_buf, len); |
Gaurav Shah | d067712 | 2010-02-04 19:35:03 -0800 | [diff] [blame] | 56 | state->remaining_buf += len; |
| 57 | state->remaining_len -= len; |
| 58 | return dst; |
| 59 | } |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame^] | 60 | |
| 61 | const void* StatefulMemcpy_r(MemcpyState* state, const void* src, int len) { |
| 62 | if (len > state->remaining_len) { |
| 63 | state->remaining_len = -1; |
| 64 | return NULL; |
| 65 | } |
| 66 | Memcpy(state->remaining_buf, src, len); |
| 67 | state->remaining_buf += len; |
| 68 | state->remaining_len -= len; |
| 69 | return src; |
| 70 | } |