comments.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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("Couldn't open 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. size_t result = fread(data, fsize, 1, fp);
  64. if (result != 1) {
  65. printf("Couldn't read bin file (%s)!\n", binnFile);
  66. return NULL;
  67. }
  68. fclose(fp);
  69. data[fsize] = 0;
  70. binn *obj = binn_open(data);
  71. return obj;
  72. }
  73. int deanonymizeAuthors(const xmlXPathObjectPtr authors) {
  74. binn *anonAuthors = readAuthors();
  75. if (anonAuthors == NULL) return 0;
  76. for (int i=0; i < authors->nodesetval->nodeNr; i++){
  77. xmlChar *anonName = (xmlChar*)"";
  78. anonName = xmlNodeGetContent(authors->nodesetval->nodeTab[i]);
  79. char *author = binn_object_str(anonAuthors, (char*)anonName);
  80. if (author != NULL) {
  81. xmlNodeSetContent(authors->nodesetval->nodeTab[i], (xmlChar*)author);
  82. printAuthors((char*)anonName, author);
  83. }
  84. xmlFree(anonName);
  85. }
  86. free(data);
  87. binn_free(anonAuthors);
  88. return 1;
  89. }
  90. int processAuthors(const xmlXPathObjectPtr authors) {
  91. if (deanonymize)
  92. return deanonymizeAuthors(authors);
  93. return anonymizeAuthors(authors);
  94. }
  95. int anonymizeComments(XMLBuff *infile) {
  96. const xmlChar *authorPath = (xmlChar*)"//w:comment/@w:author";
  97. xmlDocPtr doc = xmlReadMemory(infile->data, infile->size, infile->name, NULL, 0);
  98. Stopif(!doc, return 0, "Unable to parse file %s!\n", infile->name);
  99. xmlXPathContextPtr context = xmlXPathNewContext(doc);
  100. Stopif(!context, return 0, "Unable to create new XPath context!\n");
  101. const xmlChar* prefix = (xmlChar*)"w";
  102. const xmlChar* ns = (xmlChar*)"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  103. Stopif(xmlXPathRegisterNs(context, prefix, ns), return 0, "Can't add namespace!\n");
  104. xmlXPathObjectPtr authors = xmlXPathEvalExpression(authorPath, context);
  105. Stopif(!authors, return 0, "Something is wrong with XPATH %s!\n", authorPath);
  106. Stopif(!processAuthors(authors), return 0, "Can't process authors!\n");
  107. xmlChar *buf;
  108. xmlDocDumpMemoryEnc(doc, &buf, &infile->size, "UTF-8");
  109. infile->data = (char*)buf;
  110. Stopif(!infile->size, return 0, "Unable to save file %s!\n", infile->name);
  111. xmlXPathFreeObject(authors);
  112. xmlXPathFreeContext(context);
  113. xmlFreeDoc(doc);
  114. xmlCleanupParser();
  115. return 1;
  116. }