poc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "fastlz.h"
  6. #include <openssl/md5.h>
  7. #define HEXDUMP_COLS 8
  8. void hexdump(void *mem, unsigned int len);
  9. unsigned char *unpacked;
  10. unsigned char *packed;
  11. void init(long len){
  12. unpacked = (char*)malloc(len);
  13. packed = (char*)malloc(len);
  14. memset(unpacked, 0, len);
  15. memset(packed, 0, len);
  16. }
  17. int main(int argc, char** argv){
  18. char *source = NULL;
  19. char *source_part1 = NULL;
  20. char *source_part2 = NULL;
  21. int len, len1, len2, rlen, rlen1, rlen2, i;
  22. FILE *fp = fopen("/Users/david/Devel/arch/avr/code/quickdev16/roms/qd16boot02.smc", "r");
  23. if (fp != NULL) {
  24. /* Go to the end of the file. */
  25. if (fseek(fp, 0L, SEEK_END) == 0) {
  26. /* Get the size of the file. */
  27. len = ftell(fp);
  28. if (len == -1) { /* Error */ }
  29. /* Allocate our buffer to that size. */
  30. source = malloc(sizeof(char) * (len + 1));
  31. /* Go back to the start of the file. */
  32. if (fseek(fp, 0L, SEEK_SET) != 0) { /* Error */ }
  33. /* Read the entire file into memory. */
  34. size_t newLen = fread(source, sizeof(char), len, fp);
  35. if (newLen == 0) {
  36. fputs("Error reading file", stderr);
  37. } else {
  38. printf("Reading file with size=%i\n", len);
  39. source[newLen++] = '\0'; /* Just to be safe. */
  40. }
  41. }
  42. fclose(fp);
  43. }
  44. char sample[] = "Lorem ipsum dolor sit amet, labore commodo platonem no vis, in ius atqui dicunt, omnium euismod nam in. Ei integre euismod philosophia mea. Qui fabulas comprehensam at, omnium corrumpit comprehensam mel no, sed audiam maiestatis et. Brute perfecto et his, mea cu inermis facilis scriptorem, eam te admodum urbanitas intellegam. At vis regione invidunt tractatos, ea modus errem has.";
  45. MD5_CTX md5_context;
  46. unsigned char c[MD5_DIGEST_LENGTH];
  47. init(len);
  48. memcpy(unpacked, source, len);
  49. MD5_Init (&md5_context);
  50. MD5_Update (&md5_context, unpacked, len);
  51. MD5_Final (c,&md5_context);
  52. printf("unpacked len=%i md5=", len);
  53. for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
  54. printf("\n");
  55. rlen = fastlz_compress(unpacked, len, packed);
  56. //hexdump(packed, rlen);
  57. printf("packed len=%i md5=", rlen);
  58. MD5_Init (&md5_context);
  59. MD5_Update (&md5_context, packed, rlen);
  60. MD5_Final (c,&md5_context);
  61. for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
  62. printf("\n");
  63. memset(unpacked, 0, len);
  64. printf("-----\n");
  65. unsigned char *packed1;
  66. unsigned char *packed2;
  67. rlen1 = 32768;
  68. packed1 = malloc(rlen1);
  69. packed2 = malloc(rlen1);
  70. memcpy(packed1, packed, rlen1);
  71. memcpy(packed2, packed + rlen1, (rlen - rlen1));
  72. fastlz_decompress2(packed1, packed2, rlen, unpacked);
  73. MD5_Init (&md5_context);
  74. MD5_Update (&md5_context, unpacked, len);
  75. MD5_Final (c,&md5_context);
  76. printf("unpacked len=%i md5=", len);
  77. for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
  78. printf("\n");
  79. fp = fopen("out02.smc", "wb");
  80. fwrite(unpacked, len, 1, fp);
  81. printf("Wrote out02.smc %l bytes\n", len);
  82. fclose(fp);
  83. }
  84. void hexdump(void *mem, unsigned int len){
  85. unsigned int i, j;
  86. for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
  87. {
  88. /* print offset */
  89. if(i % HEXDUMP_COLS == 0)
  90. {
  91. printf("0x%06x: ", i);
  92. }
  93. /* print hex data */
  94. if(i < len)
  95. {
  96. printf("%02x ", 0xFF & ((char*)mem)[i]);
  97. }
  98. else /* end of block, just aligning for ASCII dump */
  99. {
  100. printf(" ");
  101. }
  102. /* print ASCII dump */
  103. if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
  104. {
  105. for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
  106. {
  107. if(j >= len) /* end of block, not really printing */
  108. {
  109. putchar(' ');
  110. }
  111. else if(isprint(((char*)mem)[j])) /* printable char */
  112. {
  113. putchar(0xFF & ((char*)mem)[j]);
  114. }
  115. else /* other char */
  116. {
  117. putchar('.');
  118. }
  119. }
  120. putchar('\n');
  121. }
  122. }
  123. }