Browse Source

Able to deanonymize comments

Piotr Czajkowski 5 years ago
parent
commit
5245969d07
4 changed files with 76 additions and 5 deletions
  1. 19 2
      anonymize.c
  2. 50 1
      comments.c
  3. 5 1
      comments.h
  4. 2 1
      zip.h

+ 19 - 2
anonymize.c

@@ -1,14 +1,31 @@
 #include <stdio.h>
 #include "zip.h"
 
+extern int action;
+
 int main(int argc, char **argv) {
 	if (argc < 2) {
 		printf("Usage: %s <path_to_DOCX>\n", argv[0]);
 		printf("Optionaly provide output file as second argument.\n");
+		printf("-d as second argument will deanonymize given file. You can optionaly provide output file as third argument.\n");
+		return 0;
 	}
 
-	if (argc > 2)
+	if (argc > 2) {
+		if (strcmp(argv[2], "-d") == 0) {
+			action = 1;
+
+			if (argc > 3) {
+				process(argv[1], argv[3]);
+				return 0;
+			}
+
+			process(argv[1], NULL);
+			return 0;
+		}
+		
 		process(argv[1], argv[2]);
-	else if (argc > 1)
+	}
+	else
 		process(argv[1], NULL);
 }

+ 50 - 1
comments.c

@@ -31,7 +31,7 @@ void saveAuthors(binn *anonAuthors) {
 	fclose(fp);
 }
 
-int processAuthors(const xmlXPathObjectPtr authors) {
+int anonymizeAuthors(const xmlXPathObjectPtr authors) {
 	binn *anonAuthors = binn_object();
 
 	for (int i=0; i < authors->nodesetval->nodeNr; i++){
@@ -47,6 +47,55 @@ int processAuthors(const xmlXPathObjectPtr authors) {
 	return 1;
 }
 
+char *data;
+
+binn *readAuthors() {
+	FILE *fp = fopen(binnFile, "rb");
+	if (fp == NULL) {
+		printf("Can't read bin file (%s)!\n", binnFile);
+		return NULL;
+	}
+
+        fseek(fp, 0, SEEK_END);
+        long fsize = ftell(fp);
+        fseek(fp, 0, SEEK_SET);
+
+        data = malloc(fsize + 1);
+        fread(data, fsize, 1, fp);
+        fclose(fp);
+
+        data[fsize] = 0;
+
+        binn *obj = binn_open(data);
+	
+	return obj;
+}
+
+int deanonymizeAuthors(const xmlXPathObjectPtr authors) {
+	binn *anonAuthors = readAuthors();
+	if (anonAuthors == NULL) return 0;
+	
+	for (int i=0; i < authors->nodesetval->nodeNr; i++){
+		xmlChar *anonName = (xmlChar*)"";		
+		anonName = xmlNodeGetContent(authors->nodesetval->nodeTab[i]);
+		
+		char *author = binn_object_str(anonAuthors, (char*)anonName);
+		if (author != NULL) xmlNodeSetContent(authors->nodesetval->nodeTab[i], (xmlChar*)author);
+		xmlFree(anonName);
+	}
+	
+	free(data);
+	binn_free(anonAuthors);
+	return 1;
+}
+
+int processAuthors(const xmlXPathObjectPtr authors) {
+	if (action == DEANONYMIZE)
+		return deanonymizeAuthors(authors);
+	
+	return anonymizeAuthors(authors);
+}
+
 int anonymizeComments(XMLBuff *infile) {
 	const xmlChar *authorPath = (xmlChar*)"//w:comment/@w:author";
 

+ 5 - 1
comments.h

@@ -3,6 +3,10 @@
 #include <binn.h>
 #include "xmlbuff.h"
 
-char binnFile[256];
+#define ANONYMIZE 0
+#define DEANONYMIZE 1
+
+extern char binnFile[256];
+extern int action;
 
 int anonymizeComments(XMLBuff *infile);

+ 2 - 1
zip.h

@@ -4,6 +4,7 @@
 #include <string.h>
 #include "comments.h"
 
-extern char binnFile[256];
+char binnFile[256];
+int action;
 
 int process(char const *infile, char *outfile);