1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
TiMidity++ -- MIDI to WAVE converter and player
Copyright (C) 1999,2000 Masanao Izumo <mo@goice.co.jp>
Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include "timidity.h"
#include "url.h"
#include "mblock.h"
#include "zip.h"
typedef struct _URL_inflate
{
char common[sizeof(struct _URL)];
InflateHandler decoder;
URL instream;
long compsize;
long pos;
int autoclose;
} URL_inflate;
static long url_inflate_read_func(char *buf, long size, void *v);
static long url_inflate_read(URL url, void *buff, long n);
static long url_inflate_tell(URL url);
static void url_inflate_close(URL url);
URL url_inflate_open(URL instream, long compsize, int autoclose)
{
URL_inflate *url;
url = (URL_inflate *)alloc_url(sizeof(URL_inflate));
if(url == NULL)
{
if(autoclose)
url_close(instream);
url_errno = errno;
return NULL;
}
/* common members */
URLm(url, type) = URL_inflate_t;
URLm(url, url_read) = url_inflate_read;
URLm(url, url_gets) = NULL;
URLm(url, url_fgetc) = NULL;
URLm(url, url_seek) = NULL;
URLm(url, url_tell) = url_inflate_tell;
URLm(url, url_close) = url_inflate_close;
/* private members */
url->decoder = NULL;
url->instream = instream;
url->pos = 0;
url->compsize = compsize;
url->autoclose = autoclose;
errno = 0;
url->decoder = open_inflate_handler(url_inflate_read_func, url);
if(url->decoder == NULL)
{
if(autoclose)
url_close(instream);
url_inflate_close((URL)url);
url_errno = errno;
return NULL;
}
return (URL)url;
}
static long url_inflate_read_func(char *buf, long size, void *v)
{
URL_inflate *urlp = (URL_inflate *)v;
long n;
if(urlp->compsize == -1) /* size if unknown */
return url_read(urlp->instream, buf, size);
if(urlp->compsize == 0)
return 0;
n = size;
if(n > urlp->compsize)
n = urlp->compsize;
n = url_read(urlp->instream, buf, n);
if(n == -1)
return -1;
urlp->compsize -= n;
return n;
}
static long url_inflate_read(URL url, void *buff, long n)
{
URL_inflate *urlp = (URL_inflate *)url;
n = zip_inflate(urlp->decoder, (char *)buff, n);
if(n <= 0)
return n;
urlp->pos += n;
return n;
}
static long url_inflate_tell(URL url)
{
return ((URL_inflate *)url)->pos;
}
static void url_inflate_close(URL url)
{
int save_errno = errno;
URL_inflate *urlp = (URL_inflate *)url;
if(urlp->decoder)
close_inflate_handler(urlp->decoder);
if(urlp->autoclose)
url_close(urlp->instream);
free(url);
errno = save_errno;
}
|