pack.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. int main(int argc, char** argv){
  9. char *source = NULL;
  10. int len, rlen, i;
  11. FILE *fp = fopen(argv[1], "r");
  12. if (fp != NULL) {
  13. /* Go to the end of the file. */
  14. if (fseek(fp, 0L, SEEK_END) == 0) {
  15. /* Get the size of the file. */
  16. len = ftell(fp);
  17. if (len == -1) { /* Error */ }
  18. /* Allocate our buffer to that size. */
  19. source = malloc(sizeof(char) * (len + 1));
  20. /* Go back to the start of the file. */
  21. if (fseek(fp, 0L, SEEK_SET) != 0) { /* Error */ }
  22. /* Read the entire file into memory. */
  23. size_t newLen = fread(source, sizeof(char), len, fp);
  24. if (newLen == 0) {
  25. fputs("Error reading file", stderr);
  26. } else {
  27. printf("Reading file with size=%i\n", len);
  28. source[newLen++] = '\0'; /* Just to be safe. */
  29. }
  30. }
  31. fclose(fp);
  32. }
  33. MD5_CTX md5_context;
  34. unsigned char c[MD5_DIGEST_LENGTH];
  35. unsigned char *packed;
  36. packed = (char*)malloc(len);
  37. MD5_Init (&md5_context);
  38. MD5_Update (&md5_context, source, len);
  39. MD5_Final (c,&md5_context);
  40. printf("unpacked len=%i md5=", len);
  41. for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
  42. printf("\n");
  43. rlen = fastlz_compress(source, len, packed);
  44. printf("packed len=%i md5=", rlen);
  45. MD5_Init (&md5_context);
  46. MD5_Update (&md5_context, packed, rlen);
  47. MD5_Final (c,&md5_context);
  48. for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
  49. printf("\n");
  50. fp = fopen(argv[2], "wb");
  51. fwrite(packed, rlen, 1, fp);
  52. printf("Wrote %s %l bytes\n", argv[2], len);
  53. fclose(fp);
  54. }