comments.c 3.6 KB

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