blob: 101211296d8c11e2246c4c441b7cbb976d75792d [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) {
51 void* saved_ptr;
52 if (len > state->remaining_len) {
53 state->remaining_len = -1;
54 return NULL;
55 }
56 saved_ptr = state->remaining_buf;
57 Memcpy(dst, saved_ptr, len);
58 state->remaining_buf += len;
59 state->remaining_len -= len;
60 return dst;
61}