Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Created January 16, 2013 22:16
Show Gist options
  • Save sw17ch/4551451 to your computer and use it in GitHub Desktop.
Save sw17ch/4551451 to your computer and use it in GitHub Desktop.
#include "memcat.h"
#define MIN(a,b) ((a) < (b) ? (a) : (b))
size_t memcat(uint8_t * dst,
struct memdesc * descriptors,
size_t descriptor_count,
size_t max_len)
{
size_t remaining = max_len;
uint8_t * next = dst;
for (size_t i = 0; i < descriptor_count; i++)
{
struct memdesc * desc = &descriptors[i];
size_t copy_len = MIN(remaining, desc->length);
memcpy(next, desc->ptr, copy_len);
next += copy_len;
remaining -= copy_len;
if (remaining <= 0)
break;
}
return (max_len - remaining);
}
#ifndef memcat_h
#define memcat_h
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define OBJ_MEMDESC(o) { sizeof(o), (uint8_t*)&(o) }
#define DESC_COUNT(descs) (sizeof(descs) / sizeof((descs)[0]))
struct memdesc
{
size_t length;
uint8_t * ptr;
};
/*
* memcat - concatenate several regions of memory into one region
*
* dst - the destination pointer
* descriptors - an ordered array of memory descriptors to be copied
* descriptor_count - the number of descriptors in the array
* max_len - the maximum number of bytes to copy into dst
*/
size_t memcat(uint8_t * dst,
struct memdesc * descriptors,
size_t descriptor_count,
size_t max_len);
#endif /* memcat_h */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment