blob: ca12fa610ccf9473018f59c3b87aa0f1f0995d3b [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--) {
33 *((uint8_t*)dest) = c;
34 }
35 return dest;
36}
37
Gaurav Shah322536d2010-01-28 15:01:23 -080038int SafeMemcmp(const void* s1, const void* s2, size_t n) {
39 int match = 1;
40 const unsigned char* us1 = s1;
41 const unsigned char* us2 = s2;
42 while (n--) {
43 if (*us1++ != *us2++)
44 match = 0;
45 else
46 match = 1;
47 }
48
49 return match;
50}
Gaurav Shahd0677122010-02-04 19:35:03 -080051
52void* StatefulMemcpy(MemcpyState* state, void* dst, int len) {
53 void* saved_ptr;
54 if (len > state->remaining_len) {
55 state->remaining_len = -1;
56 return NULL;
57 }
58 saved_ptr = state->remaining_buf;
59 Memcpy(dst, saved_ptr, len);
60 state->remaining_buf += len;
61 state->remaining_len -= len;
62 return dst;
63}