all entry points from zip.c splitted

--HG--
branch : HEAD
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 7fc379f..b9bee68 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -5,6 +5,8 @@
 libzip_a_SOURCES=zip_open.c zip_close.c zip_err_str.c zip_name_locate.c \
 	zip_fopen.c zip_fopen_index.c zip_fclose.c zip_fread.c \
 	zip_add.c zip_repalce.c zip_add_zip.c zip_replace_zip.c \
+	zip_add_data.c zip_replace_data.c zip_add_file.c zip_replace_file.c \
+	zip_delete.c zip_unchange.c \
 	zf_new_free.c
 
 BUILT_SOURCES=zip_err_str.c
diff --git a/lib/zip.h b/lib/zip.h
index 9900bcc..c2c89db 100644
--- a/lib/zip.h
+++ b/lib/zip.h
@@ -118,20 +118,26 @@
 
 /* high level routines to modify zip file */
 
-int zip_unchange(struct zip *zf, int idx);
 int zip_rename(struct zip *zf, int idx, char *name);
-int zip_add_file(struct zip *zf, char *name, FILE *file, int start, int len);
-int zip_add_data(struct zip *zf, char *name, char *buf, int start, int len);
-int zip_add_zip(struct zip *zf, char *name, struct zip *srczf, int srcidx,
-		int start, int len);
-int zip_replace_file(struct zip *zf, int idx, char *name, FILE *file,
-		     int start, int len);
-int zip_replace_data(struct zip *zf, int idx, char *name, char *buf,
-		     int start, int len);
-int zip_replace_zip(struct zip *zf, int idx, char *name,
-		    struct zip *srczf, int srcidx, int start, int len);
-
 int zip_delete(struct zip *zf, int idx);
 
+int zip_add_data(struct zip *zf, char *name, struct zip_meta *meta,
+		 char *data, int len, int freep);
+int zip_replace_data(struct zip *zf, int idx, char *name,
+		     struct zip_meta *meta,
+		     char *data, int len, int freep);
+int zip_add_file(struct zip *zf, char *name, struct zip_meta *meta,
+		 FILE *file, int start, int len);
+int zip_replace_file(struct zip *zf, int idx, char *name,
+		     struct zip_meta *meta,
+		     FILE *file, int start, int len);
+int zip_add_zip(struct zip *zf, char *name, struct zip_meta *meta,
+		struct zip *srczf, int srcidx, int start, int len);
+int zip_replace_zip(struct zip *zf, int idx, char *name,
+		    struct zip_meta *meta,
+		    struct zip *srczf, int srcidx, int start, int len)
+
+int zip_unchange(struct zip *zf, int idx);
+
 
 #endif /* _HAD_ZIP_H */
diff --git a/lib/zip_add.c b/lib/zip_add.c
index 1e3fc65..a3ec59b 100644
--- a/lib/zip_add.c
+++ b/lib/zip_add.c
@@ -4,9 +4,8 @@
 
 
 int
-zip_add(struct zip *zf, char *name, zip_read_func *fn, void *state, int comp)
+zip_add(struct zip *zf, char *name, struct zip_meta *meta,
+	zip_read_func *fn, void *state, int comp)
 {
-    _zip_new_entry(zf);
-
-    return zip_replace(zf, zf->nentry-1, name, fn, state, comp);
+    return zip_replace(zf, -1, name, meta, fn, state, comp);
 }
diff --git a/lib/zip_add_data.c b/lib/zip_add_data.c
new file mode 100644
index 0000000..50715bb
--- /dev/null
+++ b/lib/zip_add_data.c
@@ -0,0 +1,11 @@
+#include "zip.h"
+#include "zipint.h"
+
+
+
+int
+zip_add_data(struct zip *zf, char *name, struct zip_meta *meta,
+	    char *data, int len, int freep)
+{
+    return zip_replace_data(zf, -1, name, meta, data, len, freep);
+}
diff --git a/lib/zip_add_file.c b/lib/zip_add_file.c
new file mode 100644
index 0000000..bc7490a
--- /dev/null
+++ b/lib/zip_add_file.c
@@ -0,0 +1,11 @@
+#include "zip.h"
+#include "zipint.h"
+
+
+
+int
+zip_add_file(struct zip *zf, char *name, struct zip_meta *meta,
+	    FILE *file, int start, int len)
+{
+    return zip_replace_file(zf, -1, name, meta, file, start, len);
+}
diff --git a/lib/zip_add_zip.c b/lib/zip_add_zip.c
index 063a19b..772c913 100644
--- a/lib/zip_add_zip.c
+++ b/lib/zip_add_zip.c
@@ -4,8 +4,8 @@
 
 
 int
-zip_add_zip(struct zip *zf, char *name,
+zip_add_zip(struct zip *zf, char *name, struct zip_meta *meta,
 	    struct zip *srczf, int srcidx, int start, int len)
 {
-    return zip_replace_zip(zf, -1, name, srczf, srcidx, start, len);
+    return zip_replace_zip(zf, -1, name, meta, srczf, srcidx, start, len);
 }
diff --git a/lib/zip_delete.c b/lib/zip_delete.c
new file mode 100644
index 0000000..332fe1e
--- /dev/null
+++ b/lib/zip_delete.c
@@ -0,0 +1,23 @@
+#include "zip.h"
+#include "zipint.h"
+
+
+
+int
+zip_delete(struct zip *zf, int idx)
+{
+    if (idx >= zf->nentry || idx < 0) {
+	zip_err = ZIP_NOENT;
+	return -1;
+    }
+
+    if (_zip_unchange_data(zf, idx) != 0)
+	return -1;
+
+    zf->changes = 1;
+    zf->entry[idx].state = Z_DELETED;
+
+    return 0;
+}
+
+
diff --git a/lib/zip_rename.c b/lib/zip_rename.c
new file mode 100644
index 0000000..f094003
--- /dev/null
+++ b/lib/zip_rename.c
@@ -0,0 +1,22 @@
+#include "zip.h"
+#include "zipint.h"
+
+
+
+int
+zip_rename(struct zip *zf, int idx, char *name)
+{
+    if (idx >= zf->nentry || idx < 0) {
+	zip_err = ZIP_NOENT;
+	return -1;
+    }
+
+    if (zf->entry[idx].state == Z_UNCHANGED) 
+	zf->entry[idx].state = Z_RENAMED;
+    
+    zf->changes = 1;
+
+    _zip_set_name(zf, idx, name);
+
+    return 0;
+}
diff --git a/lib/zip_replace.c b/lib/zip_replace.c
index 9adf5b7..84d9523 100644
--- a/lib/zip_replace.c
+++ b/lib/zip_replace.c
@@ -4,11 +4,15 @@
 
 
 int
-zip_replace(struct zip *zf, int idx, char *name,
+zip_replace(struct zip *zf, int idx, char *name, struct zip_meta *meta,
 	    zip_read_func *fn, void *state, int comp)
 {
-    if (idx == -1)
-	zip_add(zf, name, fn, state, comp);
+    if (idx == -1) {
+	if (_zip_new_entry(zf) == NULL)
+	    return -1;
+
+	idx = zf->nentry - 1;
+    }
     
     if (idx < 0 || idx >= zf->nentry) {
 	zip_err = ZERR_NOENT;
@@ -23,7 +27,7 @@
     
     zf->changes = 1;
     zf->entry[idx].state = Z_REPLACED;
-
+    zf->entry[idx].ch_meta = meta;
     zf->entry[idx].ch_func = fn;
     zf->entry[idx].ch_data = state;
     zf->entry[idx].ch_comp = comp;
diff --git a/lib/zip_replace_data.c b/lib/zip_replace_data.c
new file mode 100644
index 0000000..994e4cc
--- /dev/null
+++ b/lib/zip_replace_data.c
@@ -0,0 +1,75 @@
+#include <stdlib.h>
+
+#include "zip.h"
+#include "zipint.h"
+
+struct read_data {
+    char *buf, *data;
+    int size;
+    int freep;
+};
+
+static zip_read_func read_data;
+
+
+
+int
+zip_replace_data(struct zf *zf, int idx, char *name, struct zip_meta *meta,
+		 char *data, int len, int freep)
+{
+    struct read_data *f;
+    
+    if (srcidx < -1 || srcidx >= srczf->nentry) {
+	zip_err = ZERR_NOENT;
+	return -1;
+    }
+
+    if ((f=(struct read_data *)malloc(sizeof(struct read_data))) == NULL) {
+	zip_err = ZERR_MEMORY;
+	return -1;
+    }
+
+    f->data = data;
+    f->len = len;
+    f->freep = freep;
+    
+    return zip_replace(zf, idx, name, meta, read_data, f, 0);
+}
+
+
+
+static int
+read_data(void *state, void *data, int len, enum zip_cmd cmd)
+{
+    struct read_data *z;
+    char *buf;
+    int n;
+
+    z = (struct read_file *)state;
+    buf = (char *)data;
+
+    switch (cmd) {
+    case ZIP_CMD_INIT:
+	z->buf = z->data;
+	return 0;
+	
+    case ZIP_CMD_READ:
+	n = len > z->len ? z->len : len;
+
+	memcpy(buf, z->buf, n);
+	z->buf += n;
+	z->len -= n;
+
+	return n;
+	
+    case ZIP_CMD_META:
+	return 0;
+
+    case ZIP_CMD_CLOSE:
+	if (z->freep) {
+	    free(z->data);
+	    z->data = NULL;
+	}
+	return 0;
+    }
+}
diff --git a/lib/zip_replace_file.c b/lib/zip_replace_file.c
new file mode 100644
index 0000000..a26066e
--- /dev/null
+++ b/lib/zip_replace_file.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+
+#include "zip.h"
+#include "zipint.h"
+
+struct read_file {
+    FILE *f;
+    int off, size;
+};
+
+static zip_read_func read_file;
+
+
+
+int
+zip_replace_file(struct zip *zf, int idx, char *name, struct zip_meta *meta,
+		 FILE *file, int start, int len)
+{
+    struct read_file *f;
+    
+    if (srcidx < -1 || srcidx >= srczf->nentry) {
+	zip_err = ZERR_NOENT;
+	return -1;
+    }
+
+    if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
+	zip_err = ZERR_MEMORY;
+	return -1;
+    }
+
+    f->f = file;
+    f->off = start;
+    f->len = len;
+    
+    return zip_replace(zf, idx, name, meta, read_file, f, 0);
+}
+
+
+
+static int
+read_file(void *state, void *data, int len, enum zip_cmd cmd)
+{
+    struct read_file *z;
+    char *buf;
+    int i, n;
+
+    z = (struct read_file *)state;
+    buf = (char *)data;
+
+    switch (cmd) {
+    case ZIP_CMD_INIT:
+	if (fseek(f->f, f->off, SEEK_SET) < 0) {
+	    zip_err = ZERR_SEEK;
+	    return -1;
+	}
+	return 0;
+	
+    case ZIP_CMD_READ:
+	if (z->len != -1)
+	    n = len > z->len ? z->len : len;
+	else
+	    n = len;
+	
+	if ((i=fread(z->f, 1, buf, n)) < 0)
+	    return -1;
+
+	if (z->len != -1)
+	    z->len -= i;
+
+	return i;
+	
+    case ZIP_CMD_META:
+	return 0;
+
+    case ZIP_CMD_CLOSE:
+	if (z->f) {
+	    fclose(z->f);
+	    z->f = NULL;
+	}
+	return 0;
+    }
+}
diff --git a/lib/zip_replace_zip.c b/lib/zip_replace_zip.c
index 9bd4d24..486f6c6 100644
--- a/lib/zip_replace_zip.c
+++ b/lib/zip_replace_zip.c
@@ -21,13 +21,13 @@
 
 
 int
-zip_replace_zip(struct zip *zf, int idx, char *name,
+zip_replace_zip(struct zip *zf, int idx, char *name, struct zip_meta *meta,
 		struct zip *srczf, int srcidx, int start, int len)
 {
     struct read_zip *z;
     struct read_part *p;
 
-    if (srcidx < 0 || srcidx >= srczf->nentry) {
+    if (srcidx < -1 || srcidx >= srczf->nentry) {
 	zip_err = ZERR_NOENT;
 	return -1;
     }
@@ -39,7 +39,7 @@
 	}
 	z->zf = srczf;
 	z->idx = srcidx;
-	return zip_replace(zf, idx, name, read_zip, z, 1);
+	return zip_replace(zf, idx, name, meta, read_zip, z, 1);
     }
     else {
 	if ((p=(struct read_part *)malloc(sizeof(struct read_part))) == NULL) {
@@ -51,14 +51,14 @@
 	p->off = start;
 	p->len = len;
 	p->zff = NULL;
-	return zip_replace(zf, idx, name, read_part, z, 0);
+	return zip_replace(zf, idx, name, meta, read_part, z, 0);
     }
 }
 
 
 
-static unsigned long
-read_zip(void *state, void *data, int len, enum zip_cmd cmd);
+static int
+read_zip(void *state, void *data, int len, enum zip_cmd cmd)
 {
     struct read_zip *z;
 
@@ -84,8 +84,8 @@
 
 
 
-static unsigned long
-read_part(void *state, void *data, int len, enum zip_cmd cmd);
+static int
+read_part(void *state, void *data, int len, enum zip_cmd cmd)
 {
     struct read_part *z;
     char b[8192], *buf;
@@ -123,8 +123,8 @@
 
 	return i;
 	
-    case ZIP_CMD_CRC:
-	return -1;
+    case ZIP_CMD_META:
+	return 0;
 
     case ZIP_CMD_CLOSE:
 	if (z->zff) {
diff --git a/lib/zip_unchange.c b/lib/zip_unchange.c
new file mode 100644
index 0000000..17e7c70
--- /dev/null
+++ b/lib/zip_unchange.c
@@ -0,0 +1,20 @@
+#include "zip.h"
+
+
+
+int
+zip_unchange(struct zip *zf, int idx)
+{
+    if (idx >= zf->nentry || idx < 0) {
+	zip_err = ZERR_NOENT;
+	return -1;
+    }
+
+    if (zf->entry[idx].fn_old) {
+	free(zf->entry[idx].fn);
+	zf->entry[idx].fn = zf->entry[idx].fn_old;
+	zf->entry[idx].fn_old = NULL;
+    }
+
+    return _zip_unchange_data(zf, idx);
+}
diff --git a/zip.c b/zip.c
index e4e0d01..8c8662e 100644
--- a/zip.c
+++ b/zip.c
@@ -6,181 +6,9 @@
 static int zip_unchange_data(struct zf *zf, int idx);
 static int zip_set_name(struct zf *zf, int idx, char *name);
 
-
-
-int
-zip_rename(struct zf *zf, int idx, char *name)
-{
-    if (idx >= zf->nentry || idx < 0)
-	return -1;
-
-    if (zf->entry[idx].state == Z_UNCHANGED) 
-	zf->entry[idx].state = Z_RENAMED;
-    zf->changes = 1;
-
-    zip_set_name(zf, idx, name);
-
-    return idx;
-}
-
-
-
-int
-zip_add_file(struct zf *zf, char *name, FILE *file,
-	int start, int len)
-{
-    zip_new_entry(zf);
-
-    zf->changes = 1;
-    zip_set_name(zf, zf->nentry-1, name ? name : "-");
-    zf->entry[zf->nentry-1].ch_data_fp = file;
-    zf->entry[zf->nentry-1].ch_data_offset = start;
-    zf->entry[zf->nentry-1].ch_data_len = len;
-
-    return zf->nentry-1;
-}
-
-
-
-int
-zip_add_data(struct zf *zf, char *name, char *buf,
-	int start, int len)
-{
-    zip_new_entry(zf);
-
-    zf->changes = 1;
-    zip_set_name(zf, zf->nentry-1, name ? name : "-");
-    zf->entry[zf->nentry-1].ch_data_buf = buf;
-    zf->entry[zf->nentry-1].ch_data_offset = start;
-    zf->entry[zf->nentry-1].ch_data_len = len;
-
-    return zf->nentry-1;
-}
-
-
-
-int
-zip_add_zip(struct zf *zf, char *name, struct zf *srczf, int srcidx,
-	    int start, int len)
-{
-    if (srcidx >= srczf->nentry || srcidx < 0)
-	return -1;
-
-    zip_new_entry(zf);
-
-    zf->changes = 1;
-    zip_set_name(zf, zf->nentry-1, name ? name : srczf->entry[srcidx].fn);
-    zf->entry[zf->nentry-1].ch_data_zf = srczf;
-    zf->entry[zf->nentry-1].ch_data_zf_fileno = srcidx;
-    zf->entry[zf->nentry-1].ch_data_offset = start;
-    zf->entry[zf->nentry-1].ch_data_len = len;
-
-    return zf->nentry-1;
-}
-
-
-
-int
-zip_replace_file(struct zf *zf, int idx, char *name, FILE *file,
-	    int start, int len)
-{
-    if (idx >= zf->nentry || idx < 0)
-	return -1;
-
-    zip_unchange_data(zf, idx);
-
-    zf->changes = 1;
-    zf->entry[idx].state = Z_REPLACED;
-    zip_set_name(zf, idx, name);
-    
-    zf->entry[idx].ch_data_fp = file;
-    zf->entry[idx].ch_data_offset = start;
-    zf->entry[idx].ch_data_len = len;
-
-    return idx;
-}
-
-
-
-int
-zip_replace_data(struct zf *zf, int idx, char *name, char *buf,
-	    int start, int len)
-{
-    if (idx >= zf->nentry || idx < 0)
-	return -1;
-
-    zip_unchange_data(zf, idx);
-
-    zf->changes = 1;
-    zf->entry[idx].state = Z_REPLACED;
-    zip_set_name(zf, idx, name);
-    
-    zf->entry[idx].ch_data_buf = buf;
-    zf->entry[idx].ch_data_offset = start;
-    zf->entry[idx].ch_data_len = len;
-
-    return idx;
-}
-
-
-
-int
-zip_replace_zip(struct zf *zf, int idx, char *name, struct zf *srczf,
-		int srcidx, int start, int len)
-{
-    if (idx >= zf->nentry || idx < 0)
-	return -1;
-
-    if (srcidx >= srczf->nentry || srcidx < 0)
-	return -1;
-
-    zip_unchange_data(zf, idx);
-
-    zf->changes = 1;
-    zf->entry[idx].state = Z_REPLACED;
-    zip_set_name(zf, idx, name);
-    
-    zf->entry[idx].ch_data_zf = srczf;
-    zf->entry[idx].ch_data_zf_fileno = srcidx;
-    zf->entry[idx].ch_data_offset = start;
-    zf->entry[idx].ch_data_len = len;
-
-    return idx;
-}
-
-
-
-int
-zip_delete(struct zf *zf, int idx)
-{
-    if (idx >= zf->nentry || idx < 0)
-	return -1;
-
-    zip_unchange(zf, idx);
-
-    zf->changes = 1;
-    zf->entry[idx].state = Z_DELETED;
-
-    return idx;
-}
-
-
-
-int
-zip_unchange(struct zf *zf, int idx)
-{
-    if (idx >= zf->nentry || idx < 0)
-	return -1;
-
-    if (zf->entry[idx].fn_old) {
-	free(zf->entry[idx].fn);
-	zf->entry[idx].fn = zf->entry[idx].fn_old;
-	zf->entry[idx].fn_old = NULL;
-	zf->entry[idx].fnlen = strlen(zf->entry[idx].fn);
-    }
-
-    return zip_unchange_data(zf, idx);
-}
+/* XXX: place in internal header file. */
+void zip_new_entry(struct zf *zf);
+void zip_entry_init(struct zf *zf, int idx);
 
 
 
@@ -211,23 +39,6 @@
 
 
 
-static int
-zip_set_name(struct zf *zf, int idx, char *name)
-{
-    if (idx >= zf->nentry || idx < 0)
-	return -1;
-
-    if (name != NULL) {
-	if (zf->entry[idx].fn_old == NULL)
-	    zf->entry[idx].fn_old = zf->entry[idx].fn;
-	else
-	    free(zf->entry[idx].fn);
-	zf->entry[idx].fn = xstrdup(name);
-	zf->entry[idx].fnlen = strlen(name);
-    }
-
-    return idx;
-}
 
 
 
diff --git a/zip_err_str.c b/zip_err_str.c
new file mode 100644
index 0000000..bbed5f7
--- /dev/null
+++ b/zip_err_str.c
@@ -0,0 +1,22 @@
+/* This file was autogenerated by ./make_zip_err_str.sh from ./zip.h
+   -- don't change here */
+#include "zip.h"
+char *zip_err_str[] = {
+    "no error",
+    "multi-disk zip-files not supported",
+    "replacing zipfile with tempfile failed",
+    "closing zipfile failed",
+    "seek error",
+    "read error",
+    "write error",
+    "CRC error",
+    "containing zip file closed",
+    "no such file",
+    "file already exists",
+    "can't open file",
+    "failure to create temporary file",
+    "zlib error",
+    "malloc failure",
+    "entry has been changed",
+    "compression method not supported",
+};