Path: blob/master/src/java.base/share/native/libzip/zlib/gzread.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* gzread.c -- zlib functions for reading gzip files25* Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler26* For conditions of distribution and use, see copyright notice in zlib.h27*/2829#include "gzguts.h"3031/* Local functions */32local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));33local int gz_avail OF((gz_statep));34local int gz_look OF((gz_statep));35local int gz_decomp OF((gz_statep));36local int gz_fetch OF((gz_statep));37local int gz_skip OF((gz_statep, z_off64_t));38local z_size_t gz_read OF((gz_statep, voidp, z_size_t));3940/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from41state->fd, and update state->eof, state->err, and state->msg as appropriate.42This function needs to loop on read(), since read() is not guaranteed to43read the number of bytes requested, depending on the type of descriptor. */44local int gz_load(state, buf, len, have)45gz_statep state;46unsigned char *buf;47unsigned len;48unsigned *have;49{50int ret;51unsigned get, max = ((unsigned)-1 >> 2) + 1;5253*have = 0;54do {55get = len - *have;56if (get > max)57get = max;58ret = read(state->fd, buf + *have, get);59if (ret <= 0)60break;61*have += (unsigned)ret;62} while (*have < len);63if (ret < 0) {64gz_error(state, Z_ERRNO, zstrerror());65return -1;66}67if (ret == 0)68state->eof = 1;69return 0;70}7172/* Load up input buffer and set eof flag if last data loaded -- return -1 on73error, 0 otherwise. Note that the eof flag is set when the end of the input74file is reached, even though there may be unused data in the buffer. Once75that data has been used, no more attempts will be made to read the file.76If strm->avail_in != 0, then the current data is moved to the beginning of77the input buffer, and then the remainder of the buffer is loaded with the78available data from the input file. */79local int gz_avail(state)80gz_statep state;81{82unsigned got;83z_streamp strm = &(state->strm);8485if (state->err != Z_OK && state->err != Z_BUF_ERROR)86return -1;87if (state->eof == 0) {88if (strm->avail_in) { /* copy what's there to the start */89unsigned char *p = state->in;90unsigned const char *q = strm->next_in;91unsigned n = strm->avail_in;92do {93*p++ = *q++;94} while (--n);95}96if (gz_load(state, state->in + strm->avail_in,97state->size - strm->avail_in, &got) == -1)98return -1;99strm->avail_in += got;100strm->next_in = state->in;101}102return 0;103}104105/* Look for gzip header, set up for inflate or copy. state->x.have must be 0.106If this is the first time in, allocate required memory. state->how will be107left unchanged if there is no more input data available, will be set to COPY108if there is no gzip header and direct copying will be performed, or it will109be set to GZIP for decompression. If direct copying, then leftover input110data from the input buffer will be copied to the output buffer. In that111case, all further file reads will be directly to either the output buffer or112a user buffer. If decompressing, the inflate state will be initialized.113gz_look() will return 0 on success or -1 on failure. */114local int gz_look(state)115gz_statep state;116{117z_streamp strm = &(state->strm);118119/* allocate read buffers and inflate memory */120if (state->size == 0) {121/* allocate buffers */122state->in = (unsigned char *)malloc(state->want);123state->out = (unsigned char *)malloc(state->want << 1);124if (state->in == NULL || state->out == NULL) {125free(state->out);126free(state->in);127gz_error(state, Z_MEM_ERROR, "out of memory");128return -1;129}130state->size = state->want;131132/* allocate inflate memory */133state->strm.zalloc = Z_NULL;134state->strm.zfree = Z_NULL;135state->strm.opaque = Z_NULL;136state->strm.avail_in = 0;137state->strm.next_in = Z_NULL;138if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */139free(state->out);140free(state->in);141state->size = 0;142gz_error(state, Z_MEM_ERROR, "out of memory");143return -1;144}145}146147/* get at least the magic bytes in the input buffer */148if (strm->avail_in < 2) {149if (gz_avail(state) == -1)150return -1;151if (strm->avail_in == 0)152return 0;153}154155/* look for gzip magic bytes -- if there, do gzip decoding (note: there is156a logical dilemma here when considering the case of a partially written157gzip file, to wit, if a single 31 byte is written, then we cannot tell158whether this is a single-byte file, or just a partially written gzip159file -- for here we assume that if a gzip file is being written, then160the header will be written in a single operation, so that reading a161single byte is sufficient indication that it is not a gzip file) */162if (strm->avail_in > 1 &&163strm->next_in[0] == 31 && strm->next_in[1] == 139) {164inflateReset(strm);165state->how = GZIP;166state->direct = 0;167return 0;168}169170/* no gzip header -- if we were decoding gzip before, then this is trailing171garbage. Ignore the trailing garbage and finish. */172if (state->direct == 0) {173strm->avail_in = 0;174state->eof = 1;175state->x.have = 0;176return 0;177}178179/* doing raw i/o, copy any leftover input to output -- this assumes that180the output buffer is larger than the input buffer, which also assures181space for gzungetc() */182state->x.next = state->out;183if (strm->avail_in) {184memcpy(state->x.next, strm->next_in, strm->avail_in);185state->x.have = strm->avail_in;186strm->avail_in = 0;187}188state->how = COPY;189state->direct = 1;190return 0;191}192193/* Decompress from input to the provided next_out and avail_out in the state.194On return, state->x.have and state->x.next point to the just decompressed195data. If the gzip stream completes, state->how is reset to LOOK to look for196the next gzip stream or raw data, once state->x.have is depleted. Returns 0197on success, -1 on failure. */198local int gz_decomp(state)199gz_statep state;200{201int ret = Z_OK;202unsigned had;203z_streamp strm = &(state->strm);204205/* fill output buffer up to end of deflate stream */206had = strm->avail_out;207do {208/* get more input for inflate() */209if (strm->avail_in == 0 && gz_avail(state) == -1)210return -1;211if (strm->avail_in == 0) {212gz_error(state, Z_BUF_ERROR, "unexpected end of file");213break;214}215216/* decompress and handle errors */217ret = inflate(strm, Z_NO_FLUSH);218if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {219gz_error(state, Z_STREAM_ERROR,220"internal error: inflate stream corrupt");221return -1;222}223if (ret == Z_MEM_ERROR) {224gz_error(state, Z_MEM_ERROR, "out of memory");225return -1;226}227if (ret == Z_DATA_ERROR) { /* deflate stream invalid */228gz_error(state, Z_DATA_ERROR,229strm->msg == NULL ? "compressed data error" : strm->msg);230return -1;231}232} while (strm->avail_out && ret != Z_STREAM_END);233234/* update available output */235state->x.have = had - strm->avail_out;236state->x.next = strm->next_out - state->x.have;237238/* if the gzip stream completed successfully, look for another */239if (ret == Z_STREAM_END)240state->how = LOOK;241242/* good decompression */243return 0;244}245246/* Fetch data and put it in the output buffer. Assumes state->x.have is 0.247Data is either copied from the input file or decompressed from the input248file depending on state->how. If state->how is LOOK, then a gzip header is249looked for to determine whether to copy or decompress. Returns -1 on error,250otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the251end of the input file has been reached and all data has been processed. */252local int gz_fetch(state)253gz_statep state;254{255z_streamp strm = &(state->strm);256257do {258switch(state->how) {259case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */260if (gz_look(state) == -1)261return -1;262if (state->how == LOOK)263return 0;264break;265case COPY: /* -> COPY */266if (gz_load(state, state->out, state->size << 1, &(state->x.have))267== -1)268return -1;269state->x.next = state->out;270return 0;271case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */272strm->avail_out = state->size << 1;273strm->next_out = state->out;274if (gz_decomp(state) == -1)275return -1;276}277} while (state->x.have == 0 && (!state->eof || strm->avail_in));278return 0;279}280281/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */282local int gz_skip(state, len)283gz_statep state;284z_off64_t len;285{286unsigned n;287288/* skip over len bytes or reach end-of-file, whichever comes first */289while (len)290/* skip over whatever is in output buffer */291if (state->x.have) {292n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?293(unsigned)len : state->x.have;294state->x.have -= n;295state->x.next += n;296state->x.pos += n;297len -= n;298}299300/* output buffer empty -- return if we're at the end of the input */301else if (state->eof && state->strm.avail_in == 0)302break;303304/* need more data to skip -- load up output buffer */305else {306/* get more output, looking for header if required */307if (gz_fetch(state) == -1)308return -1;309}310return 0;311}312313/* Read len bytes into buf from file, or less than len up to the end of the314input. Return the number of bytes read. If zero is returned, either the315end of file was reached, or there was an error. state->err must be316consulted in that case to determine which. */317local z_size_t gz_read(state, buf, len)318gz_statep state;319voidp buf;320z_size_t len;321{322z_size_t got;323unsigned n;324325/* if len is zero, avoid unnecessary operations */326if (len == 0)327return 0;328329/* process a skip request */330if (state->seek) {331state->seek = 0;332if (gz_skip(state, state->skip) == -1)333return 0;334}335336/* get len bytes to buf, or less than len if at the end */337got = 0;338do {339/* set n to the maximum amount of len that fits in an unsigned int */340n = -1;341if (n > len)342n = (unsigned)len;343344/* first just try copying data from the output buffer */345if (state->x.have) {346if (state->x.have < n)347n = state->x.have;348memcpy(buf, state->x.next, n);349state->x.next += n;350state->x.have -= n;351}352353/* output buffer empty -- return if we're at the end of the input */354else if (state->eof && state->strm.avail_in == 0) {355state->past = 1; /* tried to read past end */356break;357}358359/* need output data -- for small len or new stream load up our output360buffer */361else if (state->how == LOOK || n < (state->size << 1)) {362/* get more output, looking for header if required */363if (gz_fetch(state) == -1)364return 0;365continue; /* no progress yet -- go back to copy above */366/* the copy above assures that we will leave with space in the367output buffer, allowing at least one gzungetc() to succeed */368}369370/* large len -- read directly into user buffer */371else if (state->how == COPY) { /* read directly */372if (gz_load(state, (unsigned char *)buf, n, &n) == -1)373return 0;374}375376/* large len -- decompress directly into user buffer */377else { /* state->how == GZIP */378state->strm.avail_out = n;379state->strm.next_out = (unsigned char *)buf;380if (gz_decomp(state) == -1)381return 0;382n = state->x.have;383state->x.have = 0;384}385386/* update progress */387len -= n;388buf = (char *)buf + n;389got += n;390state->x.pos += n;391} while (len);392393/* return number of bytes read into user buffer */394return got;395}396397/* -- see zlib.h -- */398int ZEXPORT gzread(file, buf, len)399gzFile file;400voidp buf;401unsigned len;402{403gz_statep state;404405/* get internal structure */406if (file == NULL)407return -1;408state = (gz_statep)file;409410/* check that we're reading and that there's no (serious) error */411if (state->mode != GZ_READ ||412(state->err != Z_OK && state->err != Z_BUF_ERROR))413return -1;414415/* since an int is returned, make sure len fits in one, otherwise return416with an error (this avoids a flaw in the interface) */417if ((int)len < 0) {418gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");419return -1;420}421422/* read len or fewer bytes to buf */423len = (unsigned)gz_read(state, buf, len);424425/* check for an error */426if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)427return -1;428429/* return the number of bytes read (this is assured to fit in an int) */430return (int)len;431}432433/* -- see zlib.h -- */434z_size_t ZEXPORT gzfread(buf, size, nitems, file)435voidp buf;436z_size_t size;437z_size_t nitems;438gzFile file;439{440z_size_t len;441gz_statep state;442443/* get internal structure */444if (file == NULL)445return 0;446state = (gz_statep)file;447448/* check that we're reading and that there's no (serious) error */449if (state->mode != GZ_READ ||450(state->err != Z_OK && state->err != Z_BUF_ERROR))451return 0;452453/* compute bytes to read -- error on overflow */454len = nitems * size;455if (size && len / size != nitems) {456gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");457return 0;458}459460/* read len or fewer bytes to buf, return the number of full items read */461return len ? gz_read(state, buf, len) / size : 0;462}463464/* -- see zlib.h -- */465#ifdef Z_PREFIX_SET466# undef z_gzgetc467#else468# undef gzgetc469#endif470int ZEXPORT gzgetc(file)471gzFile file;472{473int ret;474unsigned char buf[1];475gz_statep state;476477/* get internal structure */478if (file == NULL)479return -1;480state = (gz_statep)file;481482/* check that we're reading and that there's no (serious) error */483if (state->mode != GZ_READ ||484(state->err != Z_OK && state->err != Z_BUF_ERROR))485return -1;486487/* try output buffer (no need to check for skip request) */488if (state->x.have) {489state->x.have--;490state->x.pos++;491return *(state->x.next)++;492}493494/* nothing there -- try gz_read() */495ret = (int)gz_read(state, buf, 1);496return ret < 1 ? -1 : buf[0];497}498499int ZEXPORT gzgetc_(file)500gzFile file;501{502return gzgetc(file);503}504505/* -- see zlib.h -- */506int ZEXPORT gzungetc(c, file)507int c;508gzFile file;509{510gz_statep state;511512/* get internal structure */513if (file == NULL)514return -1;515state = (gz_statep)file;516517/* check that we're reading and that there's no (serious) error */518if (state->mode != GZ_READ ||519(state->err != Z_OK && state->err != Z_BUF_ERROR))520return -1;521522/* process a skip request */523if (state->seek) {524state->seek = 0;525if (gz_skip(state, state->skip) == -1)526return -1;527}528529/* can't push EOF */530if (c < 0)531return -1;532533/* if output buffer empty, put byte at end (allows more pushing) */534if (state->x.have == 0) {535state->x.have = 1;536state->x.next = state->out + (state->size << 1) - 1;537state->x.next[0] = (unsigned char)c;538state->x.pos--;539state->past = 0;540return c;541}542543/* if no room, give up (must have already done a gzungetc()) */544if (state->x.have == (state->size << 1)) {545gz_error(state, Z_DATA_ERROR, "out of room to push characters");546return -1;547}548549/* slide output data if needed and insert byte before existing data */550if (state->x.next == state->out) {551unsigned char *src = state->out + state->x.have;552unsigned char *dest = state->out + (state->size << 1);553while (src > state->out)554*--dest = *--src;555state->x.next = dest;556}557state->x.have++;558state->x.next--;559state->x.next[0] = (unsigned char)c;560state->x.pos--;561state->past = 0;562return c;563}564565/* -- see zlib.h -- */566char * ZEXPORT gzgets(file, buf, len)567gzFile file;568char *buf;569int len;570{571unsigned left, n;572char *str;573unsigned char *eol;574gz_statep state;575576/* check parameters and get internal structure */577if (file == NULL || buf == NULL || len < 1)578return NULL;579state = (gz_statep)file;580581/* check that we're reading and that there's no (serious) error */582if (state->mode != GZ_READ ||583(state->err != Z_OK && state->err != Z_BUF_ERROR))584return NULL;585586/* process a skip request */587if (state->seek) {588state->seek = 0;589if (gz_skip(state, state->skip) == -1)590return NULL;591}592593/* copy output bytes up to new line or len - 1, whichever comes first --594append a terminating zero to the string (we don't check for a zero in595the contents, let the user worry about that) */596str = buf;597left = (unsigned)len - 1;598if (left) do {599/* assure that something is in the output buffer */600if (state->x.have == 0 && gz_fetch(state) == -1)601return NULL; /* error */602if (state->x.have == 0) { /* end of file */603state->past = 1; /* read past end */604break; /* return what we have */605}606607/* look for end-of-line in current output buffer */608n = state->x.have > left ? left : state->x.have;609eol = (unsigned char *)memchr(state->x.next, '\n', n);610if (eol != NULL)611n = (unsigned)(eol - state->x.next) + 1;612613/* copy through end-of-line, or remainder if not found */614memcpy(buf, state->x.next, n);615state->x.have -= n;616state->x.next += n;617state->x.pos += n;618left -= n;619buf += n;620} while (left && eol == NULL);621622/* return terminated string, or if nothing, end of file */623if (buf == str)624return NULL;625buf[0] = 0;626return str;627}628629/* -- see zlib.h -- */630int ZEXPORT gzdirect(file)631gzFile file;632{633gz_statep state;634635/* get internal structure */636if (file == NULL)637return 0;638state = (gz_statep)file;639640/* if the state is not known, but we can find out, then do so (this is641mainly for right after a gzopen() or gzdopen()) */642if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)643(void)gz_look(state);644645/* return 1 if transparent, 0 if processing a gzip stream */646return state->direct;647}648649/* -- see zlib.h -- */650int ZEXPORT gzclose_r(file)651gzFile file;652{653int ret, err;654gz_statep state;655656/* get internal structure */657if (file == NULL)658return Z_STREAM_ERROR;659state = (gz_statep)file;660661/* check that we're reading */662if (state->mode != GZ_READ)663return Z_STREAM_ERROR;664665/* free memory and close file */666if (state->size) {667inflateEnd(&(state->strm));668free(state->out);669free(state->in);670}671err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;672gz_error(state, Z_OK, NULL);673free(state->path);674ret = close(state->fd);675free(state);676return ret ? Z_ERRNO : err;677}678679680