Advertisement
Guest User

Untitled

a guest
Aug 13th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5.  
  6. const uint8_t input[] = {
  7.     0x47, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x72,
  8.     0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x75, 0x00,
  9.     0x00, 0x00, 0x76, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00,
  10.     0x00, 0x79, 0x00, 0x00, 0x00, 0x7a, 0x47, 0x00, 0x09, 0x00, 0x04, 0x00, 0x00,
  11.     0x00, 0x01, 0x00, 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  12.     0x03, 0x00, 0x00, 0x00, 0x7c, 0x03, 0x04, 0x00, 0x02, 0x64, 0x2f, 0x0f, 0x00,
  13.     0x00, 0x00, 0x17, 0x80, 0x00, 0x00, 0x02, 0x80, 0x00, 0x00, 0x03, 0x80, 0x00,
  14.     0x00, 0x0b, 0x7f, 0xff, 0xff, 0xfd, 0x0f, 0x00, 0x00, 0x00, 0x19, 0x80, 0x00,
  15.     0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x07, 0x7f, 0xff, 0xff,
  16.     0xfd, 0x0f, 0x00, 0x00, 0x00, 0x1b, 0x80, 0x00, 0x00, 0x03, 0x7f, 0xff, 0xff,
  17.     0xfc, 0x7f, 0xff, 0xff, 0xfb, 0x7f, 0xff, 0xff, 0xfd, 0x0f, 0x00, 0x00, 0x00,
  18.     0x1e, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x05,
  19.     0x0f, 0x00, 0x00, 0x00, 0x1f, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x04,
  20.     0x80, 0x00, 0x00, 0x05, 0x0f, 0x00, 0x00, 0x00, 0x20, 0x80, 0x00, 0x00, 0x00,
  21.     0x80, 0x00, 0x00, 0x06, 0x80, 0x00, 0x00, 0x07, 0x0f, 0x00, 0x00, 0x00, 0x21,
  22.     0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x23,
  23.     0x7f, 0xff, 0xff, 0xff, 0x0c, 0x00, 0x00, 0x00, 0xa8, 0x01, 0x00, 0x00, 0x00,
  24.     0x7d, 0x3d, 0x00, 0x00, 0x00, 0x01, 0x35, 0x10, 0x00, 0x00, 0x00, 0x24, 0x7f,
  25.     0xff, 0xff, 0xf4, 0x80, 0x00, 0x00, 0x09, 0x80, 0x00, 0x00, 0x08, 0x10, 0x00,
  26.     0x00, 0x00, 0x25, 0x80, 0x00, 0x00, 0x0a, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x01,
  27.     0x00, 0x00, 0x00, 0xae, 0x0f, 0x00, 0x00, 0x00, 0x26, 0x01, 0x00, 0x00, 0x00,
  28.     0xb1, 0x0f, 0x00, 0x00, 0x00, 0x27, 0x01,
  29. };
  30.  
  31.  
  32. typedef struct {
  33.     uint8_t *mem;
  34.     size_t len;
  35. } buf_t;
  36.  
  37. #define CHUNK_SIZE 8
  38.  
  39. static void add_byte(buf_t *buf, uint8_t byte)
  40. {
  41.     if (buf->len % CHUNK_SIZE == 0) {
  42.         uint8_t *old = NULL;
  43.  
  44.         if (buf->len) {
  45.             old = calloc(1, buf->len);
  46.  
  47.             if (!old) {
  48.                 fprintf(stderr, "calloc failure\n");
  49.                 exit(1);
  50.             }
  51.  
  52.             memcpy(old, buf->mem, buf->len);
  53.         }
  54.  
  55.         uint8_t *tmp = realloc(buf->mem, buf->len + CHUNK_SIZE);
  56.  
  57.         if (!tmp) {
  58.             fprintf(stderr, "realloc failure\n");
  59.             exit(1);
  60.         }
  61.  
  62.         if (tmp != buf->mem) {
  63.             fprintf(stderr, "base address change [%p->%p]\n",
  64.                 buf->mem, tmp);
  65.  
  66.             buf->mem = tmp;
  67.         }
  68.  
  69.         if (old && memcmp(old, buf->mem, buf->len) != 0) {
  70.             fprintf(stderr, "MEMORY CORRUPTION???\n\n");
  71.             fprintf(stderr, "OLD MEMORY:");
  72.             for (size_t i = 0; i < buf->len; i++) {
  73.                 if (i % 26 == 0) fprintf(stderr, "\n");
  74.                 fprintf(stderr, "%02hhx ", old[i]);
  75.             }
  76.  
  77.             fprintf(stderr, "\n\nNEW MEMORY:");
  78.             for (size_t i = 0; i < buf->len; i++) {
  79.                 if (i % 26 == 0) fprintf(stderr, "\n");
  80.                 fprintf(stderr, "%02hhx ", buf->mem[i]);
  81.             }
  82.  
  83.             fprintf(stderr, "\n\n");
  84.         }
  85.  
  86.         free(old);
  87.     }
  88.  
  89.     buf->mem[buf->len++] = byte;
  90. }
  91.  
  92. int main(int argc, char **argv)
  93. {
  94.     buf_t buf = { 0 };
  95.  
  96.     for (size_t i = 0; i < sizeof(input); i++)
  97.         add_byte(&buf, input[i]);
  98.  
  99.     fprintf(stderr, "FINAL MEMORY:");
  100.  
  101.     for (size_t i = 0; i < buf.len; i++) {
  102.         if (i % 26 == 0) fprintf(stderr, "\n");
  103.         fprintf(stderr, "%02hhx ", buf.mem[i]);
  104.     }
  105.  
  106.     fprintf(stderr, "\n\n");
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement