zip.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "zip.h"
  2. #include "stopif.h"
  3. int processComments(struct archive *archiveOut, XMLBuff *comments) {
  4. if (!anonymizeComments(comments)) return 0;
  5. struct archive_entry *newEntry = archive_entry_new();
  6. archive_entry_set_pathname(newEntry, comments->name);
  7. archive_entry_set_size(newEntry, comments->size);
  8. archive_entry_set_filetype(newEntry, AE_IFREG);
  9. archive_entry_set_perm(newEntry, 0664);
  10. Stopif(archive_write_header(archiveOut, newEntry) != ARCHIVE_OK, return 0, "Can't write entry header (comments)!\n");
  11. Stopif(archive_write_data(archiveOut, comments->data, comments->size) != comments->size, return 0, "Can't write data (comments)!\n");
  12. archive_entry_free(newEntry);
  13. xmlFree(comments->data);
  14. return 1;
  15. }
  16. int rewriteZIP(struct archive *archiveIn, struct archive *archiveOut) {
  17. const char *commentsFile = "word/comments.xml";
  18. struct archive_entry *entryIn;
  19. while (archive_read_next_header(archiveIn, &entryIn) == ARCHIVE_OK) {
  20. const char* path = archive_entry_pathname(entryIn);
  21. int64_t size = archive_entry_size(entryIn);
  22. char buf[size];
  23. Stopif(archive_read_data(archiveIn, buf, size) != size, return 0, "Archive entry has no size (%s)!\n", path);
  24. if (strcmp(commentsFile, path) == 0){
  25. XMLBuff *comments = XMLBuffNew(buf, path, size);
  26. Stopif(!comments, return 0, "Couldn't obtain comments!\n");
  27. if (!processComments(archiveOut, comments)) {
  28. XMLBuffFree(comments);
  29. return 0;
  30. }
  31. XMLBuffFree(comments);
  32. } else {
  33. Stopif(archive_write_header(archiveOut, entryIn) != ARCHIVE_OK, return 0, "Can't write entry header!\n");
  34. Stopif(archive_write_data(archiveOut, buf, size) != size, return 0, "Can't write data!\n");
  35. }
  36. }
  37. return 1;
  38. }
  39. int processDOCX(const char *infile, const char *outfile) {
  40. struct archive *archiveIn;
  41. struct archive *archiveOut;
  42. archiveIn = archive_read_new();
  43. archive_read_support_format_zip(archiveIn);
  44. Stopif(archive_read_open_filename(archiveIn, infile, 10240) != ARCHIVE_OK, return 0, "Can't read file %s!\n", infile);
  45. archiveOut = archive_write_new();
  46. archive_write_set_format_zip(archiveOut);
  47. Stopif(archive_write_open_filename(archiveOut, outfile) != ARCHIVE_OK, return 0, "Can't create new archive %s!\n", outfile);
  48. if (!rewriteZIP(archiveIn, archiveOut)) return 0;
  49. Stopif(archive_read_free(archiveIn) != ARCHIVE_OK, return 0, "Can't free %s!\n", infile);
  50. Stopif(archive_write_free(archiveOut) != ARCHIVE_OK, return 0, "Can't free %s!\n", outfile);
  51. return 1;
  52. }
  53. int process(const char *infile, char *outfile) {
  54. if (!outfile || deanonymize) {
  55. strcat(binnFile, infile);
  56. } else {
  57. strcat(binnFile, outfile);
  58. }
  59. strcat(binnFile, ".bin");
  60. if (!outfile || strcmp(infile, outfile) == 0){
  61. const char *tmpFile = "tmpFile.docx";
  62. processDOCX(infile, tmpFile);
  63. remove(infile);
  64. rename(tmpFile, infile);
  65. } else {
  66. processDOCX(infile, outfile);
  67. }
  68. return 1;
  69. }