Add a StatefulMemcpy which can be used to safely and iteratively copy blocks of memory.

Review URL: http://codereview.chromium.org/572024
This commit is contained in:
Gaurav Shah
2010-02-04 19:35:03 -08:00
parent 73bfa0768e
commit d067712ff9
3 changed files with 41 additions and 2 deletions

View File

@@ -10,7 +10,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* Malloc(size_t size) {
void* p = malloc(size);
@@ -29,6 +28,13 @@ void* Memcpy(void* dest, const void* src, size_t n) {
return memcpy(dest, src, n);
}
void* Memset(void* dest, const uint8_t c, size_t n) {
while (n--) {
*((uint8_t*)dest) = c;
}
return dest;
}
int SafeMemcmp(const void* s1, const void* s2, size_t n) {
int match = 1;
const unsigned char* us1 = s1;
@@ -42,3 +48,16 @@ int SafeMemcmp(const void* s1, const void* s2, size_t n) {
return match;
}
void* StatefulMemcpy(MemcpyState* state, void* dst, int len) {
void* saved_ptr;
if (len > state->remaining_len) {
state->remaining_len = -1;
return NULL;
}
saved_ptr = state->remaining_buf;
Memcpy(dst, saved_ptr, len);
state->remaining_buf += len;
state->remaining_len -= len;
return dst;
}