1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-11-23 23:32:45 +00:00

Add function to copy a buffer into a segment

This commit is contained in:
Peter Evans 2018-01-03 21:10:25 -06:00
parent 4f35826791
commit 90d6c637c5
2 changed files with 28 additions and 0 deletions

View File

@ -54,6 +54,7 @@ struct vm_segment {
};
extern int vm_segment_copy(vm_segment *, vm_segment *, size_t, size_t, size_t);
extern int vm_segment_copy_buf(vm_segment *, const vm_8bit *, size_t, size_t, size_t);
extern int vm_segment_fread(vm_segment *, FILE *, size_t, size_t);
extern int vm_segment_read_map(vm_segment *, size_t, vm_segment_read_fn);
extern int vm_segment_set(vm_segment *, size_t, vm_8bit);

View File

@ -186,6 +186,33 @@ vm_segment_copy(vm_segment *dest,
return OK;
}
/*
* Copy the contents of buf into the given dest segment. This is mostly
* governed by the same restrictions that copy() has, except that we
* can't do all of the bounds-checking we do there. This is just saying,
* hey, I have a bunch of bytes and I just need this copied into a
* segment, if you don't mind.
*/
int
vm_segment_copy_buf(vm_segment *dest, const vm_8bit *src,
size_t destoff, size_t srcoff, size_t len)
{
if (destoff + len > dest->size) {
log_critical("Attempt to copy buffer out of bounds (%d + %d >= %d)",
destoff, len, dest->size);
return ERR_OOB;
}
// Heh heh...there's no way of knowing if srcoff + len is out of
// bounds at any point of src, since it's just a dumb buffer. Here's
// hopin' it's not! Also, it'll be a fun day when sizeof(vm_8bit) is
// not 1, BUT HEY. Let's do it right.
memcpy(dest->memory + destoff, src + srcoff,
len * sizeof(vm_8bit));
return OK;
}
/*
* Set the read mapper for a given address. We'll use this function
* instead of the normal logic on a get for that address.