Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdint.h>
- #include <stdlib.h>
- const uint8_t input[] = {
- 0x47, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x72,
- 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x75, 0x00,
- 0x00, 0x00, 0x76, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00,
- 0x00, 0x79, 0x00, 0x00, 0x00, 0x7a, 0x47, 0x00, 0x09, 0x00, 0x04, 0x00, 0x00,
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x03, 0x00, 0x00, 0x00, 0x7c, 0x03, 0x04, 0x00, 0x02, 0x64, 0x2f, 0x0f, 0x00,
- 0x00, 0x00, 0x17, 0x80, 0x00, 0x00, 0x02, 0x80, 0x00, 0x00, 0x03, 0x80, 0x00,
- 0x00, 0x0b, 0x7f, 0xff, 0xff, 0xfd, 0x0f, 0x00, 0x00, 0x00, 0x19, 0x80, 0x00,
- 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x07, 0x7f, 0xff, 0xff,
- 0xfd, 0x0f, 0x00, 0x00, 0x00, 0x1b, 0x80, 0x00, 0x00, 0x03, 0x7f, 0xff, 0xff,
- 0xfc, 0x7f, 0xff, 0xff, 0xfb, 0x7f, 0xff, 0xff, 0xfd, 0x0f, 0x00, 0x00, 0x00,
- 0x1e, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x05,
- 0x0f, 0x00, 0x00, 0x00, 0x1f, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x04,
- 0x80, 0x00, 0x00, 0x05, 0x0f, 0x00, 0x00, 0x00, 0x20, 0x80, 0x00, 0x00, 0x00,
- 0x80, 0x00, 0x00, 0x06, 0x80, 0x00, 0x00, 0x07, 0x0f, 0x00, 0x00, 0x00, 0x21,
- 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x23,
- 0x7f, 0xff, 0xff, 0xff, 0x0c, 0x00, 0x00, 0x00, 0xa8, 0x01, 0x00, 0x00, 0x00,
- 0x7d, 0x3d, 0x00, 0x00, 0x00, 0x01, 0x35, 0x10, 0x00, 0x00, 0x00, 0x24, 0x7f,
- 0xff, 0xff, 0xf4, 0x80, 0x00, 0x00, 0x09, 0x80, 0x00, 0x00, 0x08, 0x10, 0x00,
- 0x00, 0x00, 0x25, 0x80, 0x00, 0x00, 0x0a, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x01,
- 0x00, 0x00, 0x00, 0xae, 0x0f, 0x00, 0x00, 0x00, 0x26, 0x01, 0x00, 0x00, 0x00,
- 0xb1, 0x0f, 0x00, 0x00, 0x00, 0x27, 0x01,
- };
- typedef struct {
- uint8_t *mem;
- size_t len;
- } buf_t;
- #define CHUNK_SIZE 8
- static void add_byte(buf_t *buf, uint8_t byte)
- {
- if (buf->len % CHUNK_SIZE == 0) {
- uint8_t *old = NULL;
- if (buf->len) {
- old = calloc(1, buf->len);
- if (!old) {
- fprintf(stderr, "calloc failure\n");
- exit(1);
- }
- memcpy(old, buf->mem, buf->len);
- }
- uint8_t *tmp = realloc(buf->mem, buf->len + CHUNK_SIZE);
- if (!tmp) {
- fprintf(stderr, "realloc failure\n");
- exit(1);
- }
- if (tmp != buf->mem) {
- fprintf(stderr, "base address change [%p->%p]\n",
- buf->mem, tmp);
- buf->mem = tmp;
- }
- if (old && memcmp(old, buf->mem, buf->len) != 0) {
- fprintf(stderr, "MEMORY CORRUPTION???\n\n");
- fprintf(stderr, "OLD MEMORY:");
- for (size_t i = 0; i < buf->len; i++) {
- if (i % 26 == 0) fprintf(stderr, "\n");
- fprintf(stderr, "%02hhx ", old[i]);
- }
- fprintf(stderr, "\n\nNEW MEMORY:");
- for (size_t i = 0; i < buf->len; i++) {
- if (i % 26 == 0) fprintf(stderr, "\n");
- fprintf(stderr, "%02hhx ", buf->mem[i]);
- }
- fprintf(stderr, "\n\n");
- }
- free(old);
- }
- buf->mem[buf->len++] = byte;
- }
- int main(int argc, char **argv)
- {
- buf_t buf = { 0 };
- for (size_t i = 0; i < sizeof(input); i++)
- add_byte(&buf, input[i]);
- fprintf(stderr, "FINAL MEMORY:");
- for (size_t i = 0; i < buf.len; i++) {
- if (i % 26 == 0) fprintf(stderr, "\n");
- fprintf(stderr, "%02hhx ", buf.mem[i]);
- }
- fprintf(stderr, "\n\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement