1 /* Copyright (c) 2013 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 * Implementations of stateful memory operations.
6 */
7
8 #include "sysincludes.h"
9
10 #include "stateful_util.h"
11 #include "utility.h"
12
StatefulInit(MemcpyState * state,void * buf,uint64_t len)13 void StatefulInit(MemcpyState *state, void *buf, uint64_t len)
14 {
15 state->remaining_buf = buf;
16 state->remaining_len = len;
17 state->overrun = 0;
18 }
19
StatefulSkip(MemcpyState * state,uint64_t len)20 void *StatefulSkip(MemcpyState *state, uint64_t len)
21 {
22 if (state->overrun)
23 return NULL;
24 if (len > state->remaining_len) {
25 state->overrun = 1;
26 return NULL;
27 }
28 state->remaining_buf += len;
29 state->remaining_len -= len;
30 return state; /* Must return something non-NULL. */
31 }
32
StatefulMemcpy(MemcpyState * state,void * dst,uint64_t len)33 void *StatefulMemcpy(MemcpyState *state, void *dst, uint64_t len)
34 {
35 if (state->overrun)
36 return NULL;
37 if (len > state->remaining_len) {
38 state->overrun = 1;
39 return NULL;
40 }
41 Memcpy(dst, state->remaining_buf, len);
42 state->remaining_buf += len;
43 state->remaining_len -= len;
44 return dst;
45 }
46
StatefulMemcpy_r(MemcpyState * state,const void * src,uint64_t len)47 const void *StatefulMemcpy_r(MemcpyState *state, const void *src, uint64_t len)
48 {
49 if (state->overrun)
50 return NULL;
51 if (len > state->remaining_len) {
52 state->overrun = 1;
53 return NULL;
54 }
55 Memcpy(state->remaining_buf, src, len);
56 state->remaining_buf += len;
57 state->remaining_len -= len;
58 return src;
59 }
60
StatefulMemset_r(MemcpyState * state,const uint8_t val,uint64_t len)61 const void *StatefulMemset_r(MemcpyState *state, const uint8_t val,
62 uint64_t len)
63 {
64 if (state->overrun)
65 return NULL;
66 if (len > state->remaining_len) {
67 state->overrun = 1;
68 return NULL;
69 }
70 Memset(state->remaining_buf, val, len);
71 state->remaining_buf += len;
72 state->remaining_len -= len;
73 return state; /* Must return something non-NULL. */
74 }
75