Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: src/platform/vboot_reference/common/utility_stub.c

Issue 572024: Add a StatefulMemcpy which can be used to safely and iteratively copy blocks of memory. (Closed)
Patch Set: Must be consistent.. Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/platform/vboot_reference/include/utility.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 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 2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file. 3 * found in the LICENSE file.
4 * 4 *
5 * Stub implementations of utility functions which call their linux-specific 5 * Stub implementations of utility functions which call their linux-specific
6 * equivalents. 6 * equivalents.
7 */ 7 */
8 8
9 #include "utility.h" 9 #include "utility.h"
10 10
11 #include <stdio.h> 11 #include <stdio.h>
12 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <string.h>
14 13
15 void* Malloc(size_t size) { 14 void* Malloc(size_t size) {
16 void* p = malloc(size); 15 void* p = malloc(size);
17 if (!p) { 16 if (!p) {
18 /* Fatal Error. We must abort. */ 17 /* Fatal Error. We must abort. */
19 abort(); 18 abort();
20 } 19 }
21 return p; 20 return p;
22 } 21 }
23 22
24 void Free(void* ptr) { 23 void Free(void* ptr) {
25 free(ptr); 24 free(ptr);
26 } 25 }
27 26
28 void* Memcpy(void* dest, const void* src, size_t n) { 27 void* Memcpy(void* dest, const void* src, size_t n) {
29 return memcpy(dest, src, n); 28 return memcpy(dest, src, n);
30 } 29 }
31 30
31 void* Memset(void* dest, const uint8_t c, size_t n) {
32 while (n--) {
33 *((uint8_t*)dest) = c;
34 }
35 return dest;
36 }
37
32 int SafeMemcmp(const void* s1, const void* s2, size_t n) { 38 int SafeMemcmp(const void* s1, const void* s2, size_t n) {
33 int match = 1; 39 int match = 1;
34 const unsigned char* us1 = s1; 40 const unsigned char* us1 = s1;
35 const unsigned char* us2 = s2; 41 const unsigned char* us2 = s2;
36 while (n--) { 42 while (n--) {
37 if (*us1++ != *us2++) 43 if (*us1++ != *us2++)
38 match = 0; 44 match = 0;
39 else 45 else
40 match = 1; 46 match = 1;
41 } 47 }
42 48
43 return match; 49 return match;
44 } 50 }
51
52 void* 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 }
OLDNEW
« no previous file with comments | « no previous file | src/platform/vboot_reference/include/utility.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698