Exemple #1
0
ZIP_EXTERN int
zip_set_file_comment(struct zip *za, int idx, const char *comment, int len)
{
    char *tmpcom;

    if (idx < 0 || idx >= za->nentry
	|| len < 0 || len > MAXCOMLEN
	|| (len > 0 && comment == NULL)) {
	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
	return -1;
    }

    if (len > 0) {
	if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
	    return -1;
    }
    else
	tmpcom = NULL;

    free(za->entry[idx].ch_comment);
    za->entry[idx].ch_comment = tmpcom;
    za->entry[idx].ch_comment_len = len;
    
    return 0;
}
ZIP_EXTERN int
zip_set_file_extra(struct zip *za, zip_uint64_t idx,
		   const char *extra, int len)
{
    char *tmpext;

    if (idx >= za->nentry
	|| len < 0 || len > MAXEXTLEN
	|| (len > 0 && extra == NULL)) {
	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
	return -1;
    }

    if (ZIP_IS_RDONLY(za)) {
	_zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
	return -1;
    }

    if (len > 0) {
	if ((tmpext=(char *)_zip_memdup(extra, len, &za->error)) == NULL)
	    return -1;
    }
    else
	tmpext = NULL;

    if (za->entry[idx].changes.valid & ZIP_DIRENT_EXTRAFIELD)
	free(za->entry[idx].changes.extrafield);
    za->entry[idx].changes.extrafield = tmpext;
    za->entry[idx].changes.extrafield_len = len;
    za->entry[idx].changes.valid |= ZIP_DIRENT_EXTRAFIELD;

    return 0;
}
Exemple #3
0
zip_set_archive_comment(struct zip *za, const char *comment, int len)
{
    char *tmpcom;

    if (len < 0 || len > MAXCOMLEN
	|| (len > 0 && comment == NULL)) {
	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
	return -1;
    }

    if (ZIP_IS_RDONLY(za)) {
	_zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
	return -1;
    }

    if (len > 0) {
	if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
	    return -1;
    }
    else
	tmpcom = NULL;

    free(za->ch_comment);
    za->ch_comment = tmpcom;
    za->ch_comment_len = len;
    
    return 0;
}
Exemple #4
0
static int
_zip_cdir_set_comment(struct zip_cdir *dest, struct zip *src)
{
    if (src->ch_comment_len != -1) {
        dest->comment = (char*)_zip_memdup(src->ch_comment,
                                           src->ch_comment_len, &src->error);
        if (dest->comment == NULL)
            return -1;
        dest->comment_len = src->ch_comment_len;
    } else {
        if (src->cdir && src->cdir->comment) {
            dest->comment = (char*)_zip_memdup(src->cdir->comment,
                                               src->cdir->comment_len, &src->error);
            if (dest->comment == NULL)
                return -1;
            dest->comment_len = src->cdir->comment_len;
        }
    }

    return 0;
}
Exemple #5
0
struct zip_extra_field *
_zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags)
{
    struct zip_extra_field *ef;

    if ((ef=(struct zip_extra_field *)malloc(sizeof(*ef))) == NULL)
	return NULL;

    ef->next = NULL;
    ef->flags = flags;
    ef->id = id;
    ef->size = size;
    if (size > 0) {
	if ((ef->data=(zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) {
	    free(ef);
	    return NULL;
	}
    }
    else
	ef->data = NULL;

    return ef;
}
Exemple #6
0
static struct zip_cdir *
_zip_readcdir(FILE *fp, unsigned char *buf, unsigned char *eocd, int buflen,
	      int flags, struct zip_error *error)
{
    struct zip_cdir *cd;
    unsigned char *cdp, **bufp;
    int i, comlen, nentry;
    unsigned int left;

    comlen = buf + buflen - eocd - EOCDLEN;
    if (comlen < 0) {
	/* not enough bytes left for comment */
	_zip_error_set(error, ZIP_ER_NOZIP, 0);
	return NULL;
    }

    /* check for end-of-central-dir magic */
    if (memcmp(eocd, EOCD_MAGIC, 4) != 0) {
	_zip_error_set(error, ZIP_ER_NOZIP, 0);
	return NULL;
    }

    if (memcmp(eocd+4, "\0\0\0\0", 4) != 0) {
	_zip_error_set(error, ZIP_ER_MULTIDISK, 0);
	return NULL;
    }

    cdp = eocd + 8;
    /* number of cdir-entries on this disk */
    i = _zip_read2(&cdp);
    /* number of cdir-entries */
    nentry = _zip_read2(&cdp);

    if ((cd=_zip_cdir_new(nentry, error)) == NULL)
	return NULL;

    cd->size = _zip_read4(&cdp);
    cd->offset = _zip_read4(&cdp);
    cd->comment = NULL;
    cd->comment_len = _zip_read2(&cdp);

    if ((comlen < cd->comment_len) || (cd->nentry != i)) {
	_zip_error_set(error, ZIP_ER_NOZIP, 0);
	free(cd);
	return NULL;
    }
    if ((flags & ZIP_CHECKCONS) && comlen != cd->comment_len) {
	_zip_error_set(error, ZIP_ER_INCONS, 0);
	free(cd);
	return NULL;
    }

    if (cd->comment_len) {
	if ((cd->comment=(char *)_zip_memdup(eocd+EOCDLEN,
					     cd->comment_len, error))
	    == NULL) {
	    free(cd);
	    return NULL;
	}
    }

    if (cd->size < (unsigned int)(eocd-buf)) {
	/* if buffer already read in, use it */
	cdp = eocd - cd->size;
	bufp = &cdp;
    }
    else {
	/* go to start of cdir and read it entry by entry */
	bufp = NULL;
	clearerr(fp);
	fseeko(fp, cd->offset, SEEK_SET);
	/* possible consistency check: cd->offset =
	   len-(cd->size+cd->comment_len+EOCDLEN) ? */
	if (ferror(fp) || ((unsigned long)ftello(fp) != cd->offset)) {
	    /* seek error or offset of cdir wrong */
	    if (ferror(fp))
		_zip_error_set(error, ZIP_ER_SEEK, errno);
	    else
		_zip_error_set(error, ZIP_ER_NOZIP, 0);
	    free(cd);
	    return NULL;
	}
    }

    left = cd->size;
    i=0;
    do {
	if (i == cd->nentry && left > 0) {
	    /* Infozip extension for more than 64k entries:
	       nentries wraps around, size indicates correct EOCD */
	    _zip_cdir_grow(cd, cd->nentry+0x10000, error);
	}

	if ((_zip_dirent_read(cd->entry+i, fp, bufp, &left, 0, error)) < 0) {
	    cd->nentry = i;
	    _zip_cdir_free(cd);
	    return NULL;
	}
	i++;
	
    } while (i<cd->nentry);
    
    return cd;
}
Exemple #7
0
ZIP_EXTERN int
zip_close(struct zip *za)
{
    int survivors;
    int i, j, error;
    char *temp;
    FILE *out;
    mode_t mask;
    struct zip_cdir *cd;
    struct zip_dirent de;
    struct filelist *filelist;
    int reopen_on_error;
    int new_torrentzip;

    reopen_on_error = 0;

    if (za == NULL)
        return -1;

    if (!_zip_changed(za, &survivors)) {
        _zip_free(za);
        return 0;
    }

    /* don't create zip files with no entries */
    if (survivors == 0) {
        if (za->zn && za->zp) {
            if (remove(za->zn) != 0) {
                _zip_error_set(&za->error, ZIP_ER_REMOVE, errno);
                return -1;
            }
        }
        _zip_free(za);
        return 0;
    }

    if ((filelist=(struct filelist *)malloc(sizeof(filelist[0])*survivors))
            == NULL)
        return -1;

    if ((cd=_zip_cdir_new(survivors, &za->error)) == NULL) {
        free(filelist);
        return -1;
    }

    for (i=0; i<survivors; i++)
        _zip_dirent_init(&cd->entry[i]);

    /* archive comment is special for torrentzip */
    if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) {
        cd->comment = (char*)_zip_memdup((void*)(TORRENT_SIG "XXXXXXXX"),
                                         TORRENT_SIG_LEN + TORRENT_CRC_LEN,
                                         &za->error);
        if (cd->comment == NULL) {
            _zip_cdir_free(cd);
            free(filelist);
            return -1;
        }
        cd->comment_len = TORRENT_SIG_LEN + TORRENT_CRC_LEN;
    }
    else if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0) {
        if (_zip_cdir_set_comment(cd, za) == -1) {
            _zip_cdir_free(cd);
            free(filelist);
            return -1;
        }
    }

    if ((temp=_zip_create_temp_output(za, &out)) == NULL) {
        _zip_cdir_free(cd);
        free(filelist);
        return -1;
    }


    /* create list of files with index into original archive  */
    for (i=j=0; i<za->nentry; i++) {
        if (za->entry[i].state == ZIP_ST_DELETED)
            continue;

        filelist[j].idx = i;
        filelist[j].name = zip_get_name(za, i, 0);
        j++;
    }

    survivors = j;
    if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
        qsort(filelist, survivors, sizeof(filelist[0]),
              _zip_torrentzip_cmp);

    new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1
                      && zip_get_archive_flag(za, ZIP_AFL_TORRENT,
                              ZIP_FL_UNCHANGED) == 0);
    error = 0;
    for (j=0; j<survivors; j++) {
        i = filelist[j].idx;

        /* create new local directory entry */
        if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) {
            _zip_dirent_init(&de);

            if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
                _zip_dirent_torrent_normalize(&de);

            /* use it as central directory entry */
            memcpy(cd->entry+j, &de, sizeof(cd->entry[j]));

            /* set/update file name */
            if (za->entry[i].ch_filename == NULL) {
                if (za->entry[i].state == ZIP_ST_ADDED) {
                    de.filename = strdup("-");
                    de.filename_len = 1;
                    cd->entry[j].filename = "-";
                    cd->entry[j].filename_len = 1;
                }
                else {
                    de.filename = strdup(za->cdir->entry[i].filename);
                    de.filename_len = strlen(de.filename);
                    cd->entry[j].filename = za->cdir->entry[i].filename;
                    cd->entry[j].filename_len = de.filename_len;
                }
            }
        }
        else {
            /* copy existing directory entries */
            if (fseeko(za->zp, za->cdir->entry[i].offset, SEEK_SET) != 0) {
                _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
                error = 1;
                break;
            }
            if (_zip_dirent_read(&de, za->zp, NULL, NULL, 1,
                                 &za->error) != 0) {
                error = 1;
                break;
            }
            memcpy(cd->entry+j, za->cdir->entry+i, sizeof(cd->entry[j]));
            if (de.bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
                de.crc = za->cdir->entry[i].crc;
                de.comp_size = za->cdir->entry[i].comp_size;
                de.uncomp_size = za->cdir->entry[i].uncomp_size;
                de.bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
                cd->entry[j].bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
            }
        }

        if (za->entry[i].ch_filename) {
            free(de.filename);
            if ((de.filename=strdup(za->entry[i].ch_filename)) == NULL) {
                error = 1;
                break;
            }
            de.filename_len = strlen(de.filename);
            cd->entry[j].filename = za->entry[i].ch_filename;
            cd->entry[j].filename_len = de.filename_len;
        }

        if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0
                && za->entry[i].ch_comment_len != -1) {
            /* as the rest of cd entries, its malloc/free is done by za */
            cd->entry[j].comment = za->entry[i].ch_comment;
            cd->entry[j].comment_len = za->entry[i].ch_comment_len;
        }

        cd->entry[j].offset = (unsigned int)ftello(out);

        if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) {
            struct zip_source *zs;

            zs = NULL;
            if (!ZIP_ENTRY_DATA_CHANGED(za->entry+i)) {
                if ((zs=zip_source_zip(za, za, i, ZIP_FL_RECOMPRESS, 0, -1))
                        == NULL) {
                    error = 1;
                    break;
                }
            }

            if (add_data(za, zs ? zs : za->entry[i].source, &de, out) < 0) {
                error = 1;
                break;
            }
            cd->entry[j].last_mod = de.last_mod;
            cd->entry[j].comp_method = de.comp_method;
            cd->entry[j].comp_size = de.comp_size;
            cd->entry[j].uncomp_size = de.uncomp_size;
            cd->entry[j].crc = de.crc;
        }
        else {
            if (_zip_dirent_write(&de, out, 1, &za->error) < 0) {
                error = 1;
                break;
            }
            /* we just read the local dirent, file is at correct position */
            if (copy_data(za->zp, cd->entry[j].comp_size, out,
                          &za->error) < 0) {
                error = 1;
                break;
            }
        }

        _zip_dirent_finalize(&de);
    }

    free(filelist);

    if (!error) {
        if (write_cdir(za, cd, out) < 0)
            error = 1;
    }

    /* pointers in cd entries are owned by za */
    cd->nentry = 0;
    _zip_cdir_free(cd);

    if (error) {
        _zip_dirent_finalize(&de);
        fclose(out);
        remove(temp);
        free(temp);
        return -1;
    }

    if (fclose(out) != 0) {
        _zip_error_set(&za->error, ZIP_ER_CLOSE, errno);
        remove(temp);
        free(temp);
        return -1;
    }

    if (za->zp) {
        fclose(za->zp);
        za->zp = NULL;
        reopen_on_error = 1;
    }
    if (_zip_rename(temp, za->zn) != 0) {
        _zip_error_set(&za->error, ZIP_ER_RENAME, errno);
        remove(temp);
        free(temp);
        if (reopen_on_error) {
            /* ignore errors, since we're already in an error case */
            za->zp = fopen(za->zn, "rb");
        }
        return -1;
    }
#ifndef _MSC_VER
    mask = umask(0);
    umask(mask);
    chmod(za->zn, 0666&~mask);
#endif
    _zip_free(za);
    free(temp);

    return 0;
}
Exemple #8
0
static struct zip_cdir *
_zip_readcdir(FILE *fp, off_t buf_offset, unsigned char *buf, unsigned char *eocd, int buflen,
	      int flags, struct zip_error *error)
{
    struct zip_cdir *cd;
    unsigned char *cdp, **bufp;
    int i, comlen, nentry;
    zip_uint32_t left;

    comlen = buf + buflen - eocd - EOCDLEN;
    if (comlen < 0) {
	/* not enough bytes left for comment */
	_zip_error_set(error, ZIP_ER_NOZIP, 0);
	return NULL;
    }

    /* check for end-of-central-dir magic */
    if (memcmp(eocd, EOCD_MAGIC, 4) != 0) {
	_zip_error_set(error, ZIP_ER_NOZIP, 0);
	return NULL;
    }

    if (memcmp(eocd+4, "\0\0\0\0", 4) != 0) {
	_zip_error_set(error, ZIP_ER_MULTIDISK, 0);
	return NULL;
    }

    cdp = eocd + 8;
    /* number of cdir-entries on this disk */
    i = _zip_read2(&cdp);
    /* number of cdir-entries */
    nentry = _zip_read2(&cdp);

    if ((cd=_zip_cdir_new(nentry, error)) == NULL)
	return NULL;

    cd->size = _zip_read4(&cdp);
    cd->offset = _zip_read4(&cdp);
    cd->comment = NULL;
    cd->comment_len = _zip_read2(&cdp);

	/* without checking the ZIP_CHECKCONS flag we'll not able even to open inconsistent
	   archives at this place, which would break bc in PHP */
    if ((ZIP_CHECKCONS == (flags & ZIP_CHECKCONS)) && ((zip_uint64_t)cd->offset)+cd->size > buf_offset + (eocd-buf)) {
	/* cdir spans past EOCD record */
	_zip_error_set(error, ZIP_ER_INCONS, 0);
	cd->nentry = 0;
	_zip_cdir_free(cd);
	return NULL;
    }

    if ((comlen < cd->comment_len) || (cd->nentry != i)) {
	_zip_error_set(error, ZIP_ER_NOZIP, 0);
	cd->nentry = 0;
	_zip_cdir_free(cd);
	return NULL;
    }
    if ((flags & ZIP_CHECKCONS) && comlen != cd->comment_len) {
	_zip_error_set(error, ZIP_ER_INCONS, 0);
	cd->nentry = 0;
	_zip_cdir_free(cd);
	return NULL;
    }

    if (cd->comment_len) {
	if ((cd->comment=(char *)_zip_memdup(eocd+EOCDLEN,
					     cd->comment_len, error))
	    == NULL) {
	    cd->nentry = 0;
	    _zip_cdir_free(cd);
	    return NULL;
	}
    }

	/* the first if branch goes the old way of libzip 0.9 so we don't loose 
	   the bc for reading inconsistent files */
	if ((ZIP_CHECKCONS != (flags & ZIP_CHECKCONS)) && cd->size < (unsigned int)(eocd-buf)) {
	cdp = eocd - cd->size;
	bufp = &cdp;
	}
	else if (cd->offset >= buf_offset) {
	/* if buffer already read in, use it */
	cdp = buf + (cd->offset - buf_offset);
	bufp = &cdp;
    }
    else {
	/* go to start of cdir and read it entry by entry */
	bufp = NULL;
	clearerr(fp);
	fseeko(fp, cd->offset, SEEK_SET);
	/* possible consistency check: cd->offset =
	   len-(cd->size+cd->comment_len+EOCDLEN) ? */
	if (ferror(fp) || ((unsigned long)ftello(fp) != cd->offset)) {
	    /* seek error or offset of cdir wrong */
	    if (ferror(fp))
		_zip_error_set(error, ZIP_ER_SEEK, errno);
	    else
		_zip_error_set(error, ZIP_ER_NOZIP, 0);
	    cd->nentry = 0;
	    _zip_cdir_free(cd);
	    return NULL;
	}
    }

    left = cd->size;
    i=0;
    while (i<cd->nentry && left > 0) {
	if ((_zip_dirent_read(cd->entry+i, fp, bufp, &left, 0, error)) < 0) {
	    cd->nentry = i;
	    _zip_cdir_free(cd);
	    return NULL;
	}
	i++;
	
	if (i == cd->nentry && left > 0) {
	    /* Infozip extension for more than 64k entries:
	       nentries wraps around, size indicates correct EOCD */
	    if (_zip_cdir_grow(cd, cd->nentry+ZIP_UINT16_MAX, error) < 0) {
		cd->nentry = i;
		_zip_cdir_free(cd);
		return NULL;
	    }
	}
    }

    cd->nentry = i;
    
    return cd;
}