zip.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <stdio.h>
  2. #include <archive.h>
  3. #include <archive_entry.h>
  4. #include <string.h>
  5. #include "comments.h"
  6. #include "stopif.h"
  7. static int processComments(struct archive *archiveOut, char buf[], size_t size, const char* path) {
  8. XMLBuff *comments = XMLBuffNew();
  9. *comments = (XMLBuff){.data=buf, .size=size, .name=path};
  10. anonymizeComments(comments);
  11. struct archive_entry *newEntry = archive_entry_new();
  12. archive_entry_set_pathname(newEntry, path);
  13. archive_entry_set_size(newEntry, comments->size);
  14. archive_entry_set_filetype(newEntry, AE_IFREG);
  15. archive_entry_set_perm(newEntry, 0664);
  16. Stopif(archive_write_header(archiveOut, newEntry) != ARCHIVE_OK, return -2, "Can't write entry header (comments)!\n");
  17. Stopif(archive_write_data(archiveOut, comments->data, comments->size) != comments->size, return -3, "Can't write data (comments)!\n");
  18. archive_entry_free(newEntry);
  19. XMLBuffFree(comments);
  20. return 1;
  21. }
  22. static int rewriteZIP(struct archive *archiveIn, struct archive *archiveOut) {
  23. const char *commentsFile = "word/comments.xml";
  24. struct archive_entry *entryIn;
  25. while (archive_read_next_header(archiveIn, &entryIn) == ARCHIVE_OK) {
  26. const char* path = archive_entry_pathname(entryIn);
  27. size_t size = archive_entry_size(entryIn);
  28. char buf[size];
  29. Stopif(archive_read_data(archiveIn, buf, size) != size, return -2, "Archive entry has no size (%s)!\n", path);
  30. if (strcmp(commentsFile, path) == 0){
  31. Stopif(!processComments(archiveOut, buf, size, path), return -1, "Can't process comments!\n");
  32. } else {
  33. Stopif(archive_write_header(archiveOut, entryIn) != ARCHIVE_OK, return -2, "Can't write entry header!\n");
  34. Stopif(archive_write_data(archiveOut, buf, size) != size, return -3, "Can't write data!\n");
  35. }
  36. }
  37. return 1;
  38. }
  39. static int processDOCX(char const *infile, char const *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), return -1, "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 -1, "Can't create new archive %s\n", outfile);
  48. Stopif(!rewriteZIP(archiveIn, archiveOut), return -1, "Problems rewriting zip!\n");
  49. Stopif(archive_read_free(archiveIn) != ARCHIVE_OK, return -1, "Can't free %s!\n", infile);
  50. Stopif(archive_write_free(archiveOut) != ARCHIVE_OK, return -1, "Can't free %s!\n", outfile);
  51. return 1;
  52. }
  53. int process(char const *infile, char *outfile) {
  54. if (!outfile || strcmp(infile, outfile) == 0){
  55. const char *outfile = "tmpFile.docx";
  56. processDOCX(infile, outfile);
  57. rename(outfile, infile);
  58. } else {
  59. processDOCX(infile, outfile);
  60. }
  61. return 1;
  62. }