comments.c 3.8 KB

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