comments.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #define _GNU_SOURCE //asks stdio.h to include asprintf
  2. #include <stdio.h>
  3. #include "comments.h"
  4. #include "stopif.h"
  5. void printAuthors(const char *authorName, const char *anonName) {
  6. printf("\"%s\" is now \"%s\"\n", authorName, anonName);
  7. }
  8. char* anonymizeAuthor(binn *anonAuthors, const xmlChar *authorName) {
  9. static int authorsCount = 0;
  10. char *name = (char*)authorName;
  11. char *newName = binn_object_str(anonAuthors, name);
  12. if (newName)
  13. return newName;
  14. asprintf(&newName, "Author%d", ++authorsCount);
  15. binn_object_set_str(anonAuthors, name, newName);
  16. binn_object_set_str(anonAuthors, newName, name);
  17. printAuthors(name, newName);
  18. free(newName);
  19. return binn_object_str(anonAuthors, name);
  20. }
  21. void saveAuthors(binn *anonAuthors) {
  22. FILE *fp = fopen(binnFile, "w");
  23. fwrite(binn_ptr(anonAuthors), binn_size(anonAuthors), 1, fp);
  24. fclose(fp);
  25. }
  26. int anonymizeAuthors(const xmlXPathObjectPtr authors) {
  27. binn *anonAuthors = binn_object();
  28. for (int i=0; i < authors->nodesetval->nodeNr; i++){
  29. xmlChar *authorName = (xmlChar*)"";
  30. authorName = xmlNodeGetContent(authors->nodesetval->nodeTab[i]);
  31. char *anonAuthor = anonymizeAuthor(anonAuthors, authorName);
  32. xmlNodeSetContent(authors->nodesetval->nodeTab[i], (xmlChar*)anonAuthor);
  33. xmlFree(authorName);
  34. }
  35. saveAuthors(anonAuthors);
  36. binn_free(anonAuthors);
  37. return 1;
  38. }
  39. char *data;
  40. binn *readAuthors() {
  41. FILE *fp = fopen(binnFile, "rb");
  42. if (fp == NULL) {
  43. printf("Can't read bin file (%s)!\n", binnFile);
  44. return NULL;
  45. }
  46. fseek(fp, 0, SEEK_END);
  47. long fsize = ftell(fp);
  48. fseek(fp, 0, SEEK_SET);
  49. data = malloc(fsize + 1);
  50. fread(data, fsize, 1, fp);
  51. fclose(fp);
  52. data[fsize] = 0;
  53. binn *obj = binn_open(data);
  54. return obj;
  55. }
  56. int deanonymizeAuthors(const xmlXPathObjectPtr authors) {
  57. binn *anonAuthors = readAuthors();
  58. if (anonAuthors == NULL) return 0;
  59. for (int i=0; i < authors->nodesetval->nodeNr; i++){
  60. xmlChar *anonName = (xmlChar*)"";
  61. anonName = xmlNodeGetContent(authors->nodesetval->nodeTab[i]);
  62. char *author = binn_object_str(anonAuthors, (char*)anonName);
  63. if (author != NULL) {
  64. xmlNodeSetContent(authors->nodesetval->nodeTab[i], (xmlChar*)author);
  65. printAuthors((char*)anonName, author);
  66. }
  67. xmlFree(anonName);
  68. }
  69. free(data);
  70. binn_free(anonAuthors);
  71. return 1;
  72. }
  73. int processAuthors(const xmlXPathObjectPtr authors) {
  74. if (deanonymize)
  75. return deanonymizeAuthors(authors);
  76. return anonymizeAuthors(authors);
  77. }
  78. int anonymizeComments(XMLBuff *infile) {
  79. const xmlChar *authorPath = (xmlChar*)"//w:comment/@w:author";
  80. xmlDocPtr doc = xmlReadMemory(infile->data, infile->size, infile->name, NULL, 0);
  81. Stopif(!doc, return 0, "Unable to parse file %s!\n", infile->name);
  82. xmlXPathContextPtr context = xmlXPathNewContext(doc);
  83. Stopif(!context, return 0, "Unable to create new XPath context!\n");
  84. const xmlChar* prefix = (xmlChar*)"w";
  85. const xmlChar* ns = (xmlChar*)"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  86. Stopif(xmlXPathRegisterNs(context, prefix, ns), return 0, "Can't add namespace!\n");
  87. xmlXPathObjectPtr authors = xmlXPathEvalExpression(authorPath, context);
  88. Stopif(!authors, return 0, "Something is wrong with XPATH %s!\n", authorPath);
  89. Stopif(!processAuthors(authors), return 0, "Can't process authors!\n");
  90. xmlChar *buf;
  91. xmlDocDumpMemoryEnc(doc, &buf, &infile->size, "UTF-8");
  92. infile->data = (char*)buf;
  93. Stopif(!infile->size, return 0, "Unable to save file %s!\n", infile->name);
  94. xmlXPathFreeObject(authors);
  95. xmlXPathFreeContext(context);
  96. xmlFreeDoc(doc);
  97. xmlCleanupParser();
  98. return 1;
  99. }