Back to index

php5  5.3.10
zip_source_buffer.c
Go to the documentation of this file.
00001 /*
00002   zip_source_buffer.c -- create zip data source from buffer
00003   Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
00004 
00005   This file is part of libzip, a library to manipulate ZIP archives.
00006   The authors can be contacted at <libzip@nih.at>
00007 
00008   Redistribution and use in source and binary forms, with or without
00009   modification, are permitted provided that the following conditions
00010   are met:
00011   1. Redistributions of source code must retain the above copyright
00012      notice, this list of conditions and the following disclaimer.
00013   2. Redistributions in binary form must reproduce the above copyright
00014      notice, this list of conditions and the following disclaimer in
00015      the documentation and/or other materials provided with the
00016      distribution.
00017   3. The names of the authors may not be used to endorse or promote
00018      products derived from this software without specific prior
00019      written permission.
00020  
00021   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
00022   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
00025   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00027   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00029   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00030   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00031   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 */
00033 
00034 
00035 
00036 #include <stdlib.h>
00037 #include <string.h>
00038 
00039 #include "zipint.h"
00040 
00041 struct read_data {
00042     const char *buf, *data, *end;
00043     time_t mtime;
00044     int freep;
00045 };
00046 
00047 static ssize_t read_data(void *state, void *data, size_t len,
00048                       enum zip_source_cmd cmd);
00049 
00050 
00051 
00052 ZIP_EXTERN(struct zip_source *)
00053 zip_source_buffer(struct zip *za, const void *data, off_t len, int freep)
00054 {
00055     struct read_data *f;
00056     struct zip_source *zs;
00057 
00058     if (za == NULL)
00059        return NULL;
00060 
00061     if (len < 0 || (data == NULL && len > 0)) {
00062        _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
00063        return NULL;
00064     }
00065 
00066     if ((f=(struct read_data *)malloc(sizeof(*f))) == NULL) {
00067        _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
00068        return NULL;
00069     }
00070 
00071     f->data = (const char *)data;
00072     f->end = ((const char *)data)+len;
00073     f->freep = freep;
00074     f->mtime = time(NULL);
00075     
00076     if ((zs=zip_source_function(za, read_data, f)) == NULL) {
00077        free(f);
00078        return NULL;
00079     }
00080 
00081     return zs;
00082 }
00083 
00084 
00085 
00086 static ssize_t
00087 read_data(void *state, void *data, size_t len, enum zip_source_cmd cmd)
00088 {
00089     struct read_data *z;
00090     char *buf;
00091     size_t n;
00092 
00093     z = (struct read_data *)state;
00094     buf = (char *)data;
00095 
00096     switch (cmd) {
00097     case ZIP_SOURCE_OPEN:
00098        z->buf = z->data;
00099        return 0;
00100        
00101     case ZIP_SOURCE_READ:
00102        n = z->end - z->buf;
00103        if (n > len)
00104            n = len;
00105 
00106        if (n) {
00107            memcpy(buf, z->buf, n);
00108            z->buf += n;
00109        }
00110 
00111        return n;
00112        
00113     case ZIP_SOURCE_CLOSE:
00114        return 0;
00115 
00116     case ZIP_SOURCE_STAT:
00117         {
00118            struct zip_stat *st;
00119            
00120            if (len < sizeof(*st))
00121               return -1;
00122 
00123            st = (struct zip_stat *)data;
00124 
00125            zip_stat_init(st);
00126            st->mtime = z->mtime;
00127            st->size = z->end - z->data;
00128            
00129            return sizeof(*st);
00130        }
00131 
00132     case ZIP_SOURCE_ERROR:
00133        {
00134            int *e;
00135 
00136            if (len < sizeof(int)*2)
00137               return -1;
00138 
00139            e = (int *)data;
00140            e[0] = e[1] = 0;
00141        }
00142        return sizeof(int)*2;
00143 
00144     case ZIP_SOURCE_FREE:
00145        if (z->freep) {
00146            free((void *)z->data);
00147            z->data = NULL;
00148        }
00149        free(z);
00150        return 0;
00151 
00152     default:
00153        ;
00154     }
00155 
00156     return -1;
00157 }