zip.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. return 1;
  14. }
  15. int rewriteZIP(struct archive *archiveIn, struct archive *archiveOut) {
  16. const char *commentsFile = "word/comments.xml";
  17. struct archive_entry *entryIn;
  18. while (archive_read_next_header(archiveIn, &entryIn) == ARCHIVE_OK) {
  19. const char* path = archive_entry_pathname(entryIn);
  20. int64_t size = archive_entry_size(entryIn);
  21. char buf[size];
  22. Stopif(archive_read_data(archiveIn, buf, size) != size, return 0, "Archive entry has no size (%s)!\n", path);
  23. if (strcmp(commentsFile, path) == 0){
  24. XMLBuff *comments = XMLBuffNew();
  25. *comments = (XMLBuff){.data=buf, .size=size, .name=path};
  26. if (!processComments(archiveOut, comments)) return 0;
  27. XMLBuffFree(comments);
  28. } else {
  29. Stopif(archive_write_header(archiveOut, entryIn) != ARCHIVE_OK, return 0, "Can't write entry header!\n");
  30. Stopif(archive_write_data(archiveOut, buf, size) != size, return 0, "Can't write data!\n");
  31. }
  32. }
  33. return 1;
  34. }
  35. int processDOCX(const char *infile, const char *outfile) {
  36. struct archive *archiveIn;
  37. struct archive *archiveOut;
  38. archiveIn = archive_read_new();
  39. archive_read_support_format_zip(archiveIn);
  40. Stopif(archive_read_open_filename(archiveIn, infile, 10240) != ARCHIVE_OK, return 0, "Can't read file %s!\n", infile);
  41. archiveOut = archive_write_new();
  42. archive_write_set_format_zip(archiveOut);
  43. Stopif(archive_write_open_filename(archiveOut, outfile) != ARCHIVE_OK, return 0, "Can't create new archive %s!\n", outfile);
  44. if (!rewriteZIP(archiveIn, archiveOut)) return 0;
  45. Stopif(archive_read_free(archiveIn) != ARCHIVE_OK, return 0, "Can't free %s!\n", infile);
  46. Stopif(archive_write_free(archiveOut) != ARCHIVE_OK, return 0, "Can't free %s!\n", outfile);
  47. return 1;
  48. }
  49. int process(const char *infile, char *outfile) {
  50. if (!outfile || deanonymize) {
  51. strcat(binnFile, infile);
  52. } else {
  53. strcat(binnFile, outfile);
  54. }
  55. strcat(binnFile, ".bin");
  56. if (!outfile || strcmp(infile, outfile) == 0){
  57. const char *outfile = "tmpFile.docx";
  58. processDOCX(infile, outfile);
  59. remove(infile);
  60. rename(outfile, infile);
  61. } else {
  62. processDOCX(infile, outfile);
  63. }
  64. return 1;
  65. }