summaryrefslogtreecommitdiff
path: root/firmware/lib/stateful_util.c
blob: f0718f6d0904b3e2a961869a85a7e8fbf8031c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Implementations of stateful memory operations.
 */

#include "sysincludes.h"

#include "stateful_util.h"
#include "utility.h"

void StatefulInit(MemcpyState *state, void *buf, uint64_t len)
{
	state->remaining_buf = buf;
	state->remaining_len = len;
	state->overrun = 0;
}

void *StatefulSkip(MemcpyState *state, uint64_t len)
{
	if (state->overrun)
		return NULL;
	if (len > state->remaining_len) {
		state->overrun = 1;
		return NULL;
	}
	state->remaining_buf += len;
	state->remaining_len -= len;
	return state; /* Must return something non-NULL. */
}

void *StatefulMemcpy(MemcpyState *state, void *dst, uint64_t len)
{
	if (state->overrun)
		return NULL;
	if (len > state->remaining_len) {
		state->overrun = 1;
		return NULL;
	}
	memcpy(dst, state->remaining_buf, len);
	state->remaining_buf += len;
	state->remaining_len -= len;
	return dst;
}

const void *StatefulMemcpy_r(MemcpyState *state, const void *src, uint64_t len)
{
	if (state->overrun)
		return NULL;
	if (len > state->remaining_len) {
		state->overrun = 1;
		return NULL;
	}
	memcpy(state->remaining_buf, src, len);
	state->remaining_buf += len;
	state->remaining_len -= len;
	return src;
}

const void *StatefulMemset_r(MemcpyState *state, const uint8_t val,
			     uint64_t len)
{
	if (state->overrun)
		return NULL;
	if (len > state->remaining_len) {
		state->overrun = 1;
		return NULL;
	}
	memset(state->remaining_buf, val, len);
	state->remaining_buf += len;
	state->remaining_len -= len;
	return state; /* Must return something non-NULL. */
}