Browse Source

Updated and improved

Piotr Czajkowski 3 years ago
parent
commit
224dfee436
3 changed files with 13 additions and 15 deletions
  1. 5 10
      xmlbuff.c
  2. 1 1
      xmlbuff.h
  3. 7 4
      zip.c

+ 5 - 10
xmlbuff.c

@@ -1,19 +1,14 @@
 #include <stdlib.h>
-#include <stdio.h>
 #include "xmlbuff.h"
 
-XMLBuff *XMLBuffNew(void) {
+XMLBuff *XMLBuffNew(char *data, const char *name, int size) {
 	XMLBuff *out = malloc(sizeof(XMLBuff));
-	if (out == NULL) {
-		puts("Couldn't allocate memory for XMLBuff!");
-		return NULL;
-	}
-	
-	*out = (XMLBuff){ .data=NULL };                          
+	if (!out) return NULL;
+
+	*out = (XMLBuff){ .data=data, .name=name, .size=size };
 	return out;
 }
 
 void XMLBuffFree(XMLBuff *in) {
-	free(in->data);
-	free(in);
+	if (in) free(in);
 }

+ 1 - 1
xmlbuff.h

@@ -5,5 +5,5 @@ typedef struct XMLBuff
 	int size;
 } XMLBuff;
 
-XMLBuff *XMLBuffNew(void);
+XMLBuff *XMLBuffNew(char *data, const char *name, int size);
 void XMLBuffFree(XMLBuff *in);

+ 7 - 4
zip.c

@@ -27,11 +27,14 @@ int rewriteZIP(struct archive *archiveIn, struct archive *archiveOut) {
 		Stopif(archive_read_data(archiveIn, buf, size) != size, return 0, "Archive entry has no size (%s)!\n", path);
 
 		if (strcmp(commentsFile, path) == 0){
-			XMLBuff *comments = XMLBuffNew();
-			Stopif(comments == NULL, return 0, "Couldn't obtain comments!\n");
-			*comments = (XMLBuff){.data=buf, .size=size, .name=path};
+			XMLBuff *comments = XMLBuffNew(buf, path, size);
+			Stopif(!comments, return 0, "Couldn't obtain comments!\n");
+
+			if (!processComments(archiveOut, comments)) {
+				XMLBuffFree(comments);
+				return 0;
+			}
 
-			if (!processComments(archiveOut, comments)) return 0;
 			XMLBuffFree(comments);
 		} else {
 			Stopif(archive_write_header(archiveOut, entryIn) != ARCHIVE_OK, return 0, "Can't write entry header!\n");