blob: 8d21897747ea026cbd5daaea9f7b45befdd40466 [file] [log] [blame]
Gaurav Shah322536d2010-01-28 15:01:23 -08001/* 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 Shah322536d2010-01-28 15:01:23 -080013
14void* 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
23void Free(void* ptr) {
24 free(ptr);
25}
26
27void* Memcpy(void* dest, const void* src, size_t n) {
28 return memcpy(dest, src, n);
29}
30
Gaurav Shahd0677122010-02-04 19:35:03 -080031void* Memset(void* dest, const uint8_t c, size_t n) {
32 while (n--) {
Gaurav Shah08df9b82010-02-23 16:16:23 -080033 *((uint8_t*)dest++) = c;
Gaurav Shahd0677122010-02-04 19:35:03 -080034 }
35 return dest;
36}
37
Gaurav Shah322536d2010-01-28 15:01:23 -080038int SafeMemcmp(const void* s1, const void* s2, size_t n) {
Gaurav Shah08df9b82010-02-23 16:16:23 -080039 int match = 0;
Gaurav Shah322536d2010-01-28 15:01:23 -080040 const unsigned char* us1 = s1;
41 const unsigned char* us2 = s2;
42 while (n--) {
43 if (*us1++ != *us2++)
Gaurav Shah322536d2010-01-28 15:01:23 -080044 match = 1;
45 }
46
47 return match;
48}
Gaurav Shahd0677122010-02-04 19:35:03 -080049
50void* StatefulMemcpy(MemcpyState* state, void* dst, int len) {
Gaurav Shahd0677122010-02-04 19:35:03 -080051 if (len > state->remaining_len) {
52 state->remaining_len = -1;
53 return NULL;
54 }
Gaurav Shahf5564fa2010-03-02 15:40:01 -080055 Memcpy(dst, state->remaining_buf, len);
Gaurav Shahd0677122010-02-04 19:35:03 -080056 state->remaining_buf += len;
57 state->remaining_len -= len;
58 return dst;
59}
Gaurav Shahf5564fa2010-03-02 15:40:01 -080060
61const 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}