lemon-project-template-glpk
diff deps/glpk/src/zlib/gzlib.c @ 9:33de93886c88
Import GLPK 4.47
author | Alpar Juttner <alpar@cs.elte.hu> |
---|---|
date | Sun, 06 Nov 2011 20:59:10 +0100 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/deps/glpk/src/zlib/gzlib.c Sun Nov 06 20:59:10 2011 +0100 1.3 @@ -0,0 +1,537 @@ 1.4 +/* gzlib.c -- zlib functions common to reading and writing gzip files 1.5 + * Copyright (C) 2004, 2010 Mark Adler 1.6 + * For conditions of distribution and use, see copyright notice in zlib.h 1.7 + */ 1.8 + 1.9 +#include "gzguts.h" 1.10 + 1.11 +#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 1.12 +# define LSEEK lseek64 1.13 +#else 1.14 +# define LSEEK lseek 1.15 +#endif 1.16 + 1.17 +/* Local functions */ 1.18 +local void gz_reset OF((gz_statep)); 1.19 +local gzFile gz_open OF((const char *, int, const char *)); 1.20 + 1.21 +#if defined UNDER_CE 1.22 + 1.23 +/* Map the Windows error number in ERROR to a locale-dependent error message 1.24 + string and return a pointer to it. Typically, the values for ERROR come 1.25 + from GetLastError. 1.26 + 1.27 + The string pointed to shall not be modified by the application, but may be 1.28 + overwritten by a subsequent call to gz_strwinerror 1.29 + 1.30 + The gz_strwinerror function does not change the current setting of 1.31 + GetLastError. */ 1.32 +char ZLIB_INTERNAL *gz_strwinerror (error) 1.33 + DWORD error; 1.34 +{ 1.35 + static char buf[1024]; 1.36 + 1.37 + wchar_t *msgbuf; 1.38 + DWORD lasterr = GetLastError(); 1.39 + DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM 1.40 + | FORMAT_MESSAGE_ALLOCATE_BUFFER, 1.41 + NULL, 1.42 + error, 1.43 + 0, /* Default language */ 1.44 + (LPVOID)&msgbuf, 1.45 + 0, 1.46 + NULL); 1.47 + if (chars != 0) { 1.48 + /* If there is an \r\n appended, zap it. */ 1.49 + if (chars >= 2 1.50 + && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { 1.51 + chars -= 2; 1.52 + msgbuf[chars] = 0; 1.53 + } 1.54 + 1.55 + if (chars > sizeof (buf) - 1) { 1.56 + chars = sizeof (buf) - 1; 1.57 + msgbuf[chars] = 0; 1.58 + } 1.59 + 1.60 + wcstombs(buf, msgbuf, chars + 1); 1.61 + LocalFree(msgbuf); 1.62 + } 1.63 + else { 1.64 + sprintf(buf, "unknown win32 error (%ld)", error); 1.65 + } 1.66 + 1.67 + SetLastError(lasterr); 1.68 + return buf; 1.69 +} 1.70 + 1.71 +#endif /* UNDER_CE */ 1.72 + 1.73 +/* Reset gzip file state */ 1.74 +local void gz_reset(state) 1.75 + gz_statep state; 1.76 +{ 1.77 + if (state->mode == GZ_READ) { /* for reading ... */ 1.78 + state->have = 0; /* no output data available */ 1.79 + state->eof = 0; /* not at end of file */ 1.80 + state->how = LOOK; /* look for gzip header */ 1.81 + state->direct = 1; /* default for empty file */ 1.82 + } 1.83 + state->seek = 0; /* no seek request pending */ 1.84 + gz_error(state, Z_OK, NULL); /* clear error */ 1.85 + state->pos = 0; /* no uncompressed data yet */ 1.86 + state->strm.avail_in = 0; /* no input data yet */ 1.87 +} 1.88 + 1.89 +/* Open a gzip file either by name or file descriptor. */ 1.90 +local gzFile gz_open(path, fd, mode) 1.91 + const char *path; 1.92 + int fd; 1.93 + const char *mode; 1.94 +{ 1.95 + gz_statep state; 1.96 + 1.97 + /* allocate gzFile structure to return */ 1.98 + state = malloc(sizeof(gz_state)); 1.99 + if (state == NULL) 1.100 + return NULL; 1.101 + state->size = 0; /* no buffers allocated yet */ 1.102 + state->want = GZBUFSIZE; /* requested buffer size */ 1.103 + state->msg = NULL; /* no error message yet */ 1.104 + 1.105 + /* interpret mode */ 1.106 + state->mode = GZ_NONE; 1.107 + state->level = Z_DEFAULT_COMPRESSION; 1.108 + state->strategy = Z_DEFAULT_STRATEGY; 1.109 + while (*mode) { 1.110 + if (*mode >= '0' && *mode <= '9') 1.111 + state->level = *mode - '0'; 1.112 + else 1.113 + switch (*mode) { 1.114 + case 'r': 1.115 + state->mode = GZ_READ; 1.116 + break; 1.117 +#ifndef NO_GZCOMPRESS 1.118 + case 'w': 1.119 + state->mode = GZ_WRITE; 1.120 + break; 1.121 + case 'a': 1.122 + state->mode = GZ_APPEND; 1.123 + break; 1.124 +#endif 1.125 + case '+': /* can't read and write at the same time */ 1.126 + free(state); 1.127 + return NULL; 1.128 + case 'b': /* ignore -- will request binary anyway */ 1.129 + break; 1.130 + case 'f': 1.131 + state->strategy = Z_FILTERED; 1.132 + break; 1.133 + case 'h': 1.134 + state->strategy = Z_HUFFMAN_ONLY; 1.135 + break; 1.136 + case 'R': 1.137 + state->strategy = Z_RLE; 1.138 + break; 1.139 + case 'F': 1.140 + state->strategy = Z_FIXED; 1.141 + default: /* could consider as an error, but just ignore */ 1.142 + ; 1.143 + } 1.144 + mode++; 1.145 + } 1.146 + 1.147 + /* must provide an "r", "w", or "a" */ 1.148 + if (state->mode == GZ_NONE) { 1.149 + free(state); 1.150 + return NULL; 1.151 + } 1.152 + 1.153 + /* save the path name for error messages */ 1.154 + state->path = malloc(strlen(path) + 1); 1.155 + if (state->path == NULL) { 1.156 + free(state); 1.157 + return NULL; 1.158 + } 1.159 + strcpy(state->path, path); 1.160 + 1.161 + /* open the file with the appropriate mode (or just use fd) */ 1.162 + state->fd = fd != -1 ? fd : 1.163 + open(path, 1.164 +#ifdef O_LARGEFILE 1.165 + O_LARGEFILE | 1.166 +#endif 1.167 +#ifdef O_BINARY 1.168 + O_BINARY | 1.169 +#endif 1.170 + (state->mode == GZ_READ ? 1.171 + O_RDONLY : 1.172 + (O_WRONLY | O_CREAT | ( 1.173 + state->mode == GZ_WRITE ? 1.174 + O_TRUNC : 1.175 + O_APPEND))), 1.176 + 0666); 1.177 + if (state->fd == -1) { 1.178 + free(state->path); 1.179 + free(state); 1.180 + return NULL; 1.181 + } 1.182 + if (state->mode == GZ_APPEND) 1.183 + state->mode = GZ_WRITE; /* simplify later checks */ 1.184 + 1.185 + /* save the current position for rewinding (only if reading) */ 1.186 + if (state->mode == GZ_READ) { 1.187 + state->start = LSEEK(state->fd, 0, SEEK_CUR); 1.188 + if (state->start == -1) state->start = 0; 1.189 + } 1.190 + 1.191 + /* initialize stream */ 1.192 + gz_reset(state); 1.193 + 1.194 + /* return stream */ 1.195 + return (gzFile)state; 1.196 +} 1.197 + 1.198 +/* -- see zlib.h -- */ 1.199 +gzFile ZEXPORT gzopen(path, mode) 1.200 + const char *path; 1.201 + const char *mode; 1.202 +{ 1.203 + return gz_open(path, -1, mode); 1.204 +} 1.205 + 1.206 +/* -- see zlib.h -- */ 1.207 +gzFile ZEXPORT gzopen64(path, mode) 1.208 + const char *path; 1.209 + const char *mode; 1.210 +{ 1.211 + return gz_open(path, -1, mode); 1.212 +} 1.213 + 1.214 +/* -- see zlib.h -- */ 1.215 +gzFile ZEXPORT gzdopen(fd, mode) 1.216 + int fd; 1.217 + const char *mode; 1.218 +{ 1.219 + char *path; /* identifier for error messages */ 1.220 + gzFile gz; 1.221 + 1.222 + if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL) 1.223 + return NULL; 1.224 + sprintf(path, "<fd:%d>", fd); /* for debugging */ 1.225 + gz = gz_open(path, fd, mode); 1.226 + free(path); 1.227 + return gz; 1.228 +} 1.229 + 1.230 +/* -- see zlib.h -- */ 1.231 +int ZEXPORT gzbuffer(file, size) 1.232 + gzFile file; 1.233 + unsigned size; 1.234 +{ 1.235 + gz_statep state; 1.236 + 1.237 + /* get internal structure and check integrity */ 1.238 + if (file == NULL) 1.239 + return -1; 1.240 + state = (gz_statep)file; 1.241 + if (state->mode != GZ_READ && state->mode != GZ_WRITE) 1.242 + return -1; 1.243 + 1.244 + /* make sure we haven't already allocated memory */ 1.245 + if (state->size != 0) 1.246 + return -1; 1.247 + 1.248 + /* check and set requested size */ 1.249 + if (size == 0) 1.250 + return -1; 1.251 + state->want = size; 1.252 + return 0; 1.253 +} 1.254 + 1.255 +/* -- see zlib.h -- */ 1.256 +int ZEXPORT gzrewind(file) 1.257 + gzFile file; 1.258 +{ 1.259 + gz_statep state; 1.260 + 1.261 + /* get internal structure */ 1.262 + if (file == NULL) 1.263 + return -1; 1.264 + state = (gz_statep)file; 1.265 + 1.266 + /* check that we're reading and that there's no error */ 1.267 + if (state->mode != GZ_READ || state->err != Z_OK) 1.268 + return -1; 1.269 + 1.270 + /* back up and start over */ 1.271 + if (LSEEK(state->fd, state->start, SEEK_SET) == -1) 1.272 + return -1; 1.273 + gz_reset(state); 1.274 + return 0; 1.275 +} 1.276 + 1.277 +/* -- see zlib.h -- */ 1.278 +z_off64_t ZEXPORT gzseek64(file, offset, whence) 1.279 + gzFile file; 1.280 + z_off64_t offset; 1.281 + int whence; 1.282 +{ 1.283 + unsigned n; 1.284 + z_off64_t ret; 1.285 + gz_statep state; 1.286 + 1.287 + /* get internal structure and check integrity */ 1.288 + if (file == NULL) 1.289 + return -1; 1.290 + state = (gz_statep)file; 1.291 + if (state->mode != GZ_READ && state->mode != GZ_WRITE) 1.292 + return -1; 1.293 + 1.294 + /* check that there's no error */ 1.295 + if (state->err != Z_OK) 1.296 + return -1; 1.297 + 1.298 + /* can only seek from start or relative to current position */ 1.299 + if (whence != SEEK_SET && whence != SEEK_CUR) 1.300 + return -1; 1.301 + 1.302 + /* normalize offset to a SEEK_CUR specification */ 1.303 + if (whence == SEEK_SET) 1.304 + offset -= state->pos; 1.305 + else if (state->seek) 1.306 + offset += state->skip; 1.307 + state->seek = 0; 1.308 + 1.309 + /* if within raw area while reading, just go there */ 1.310 + if (state->mode == GZ_READ && state->how == COPY && 1.311 + state->pos + offset >= state->raw) { 1.312 + ret = LSEEK(state->fd, offset - state->have, SEEK_CUR); 1.313 + if (ret == -1) 1.314 + return -1; 1.315 + state->have = 0; 1.316 + state->eof = 0; 1.317 + state->seek = 0; 1.318 + gz_error(state, Z_OK, NULL); 1.319 + state->strm.avail_in = 0; 1.320 + state->pos += offset; 1.321 + return state->pos; 1.322 + } 1.323 + 1.324 + /* calculate skip amount, rewinding if needed for back seek when reading */ 1.325 + if (offset < 0) { 1.326 + if (state->mode != GZ_READ) /* writing -- can't go backwards */ 1.327 + return -1; 1.328 + offset += state->pos; 1.329 + if (offset < 0) /* before start of file! */ 1.330 + return -1; 1.331 + if (gzrewind(file) == -1) /* rewind, then skip to offset */ 1.332 + return -1; 1.333 + } 1.334 + 1.335 + /* if reading, skip what's in output buffer (one less gzgetc() check) */ 1.336 + if (state->mode == GZ_READ) { 1.337 + n = GT_OFF(state->have) || (z_off64_t)state->have > offset ? 1.338 + (unsigned)offset : state->have; 1.339 + state->have -= n; 1.340 + state->next += n; 1.341 + state->pos += n; 1.342 + offset -= n; 1.343 + } 1.344 + 1.345 + /* request skip (if not zero) */ 1.346 + if (offset) { 1.347 + state->seek = 1; 1.348 + state->skip = offset; 1.349 + } 1.350 + return state->pos + offset; 1.351 +} 1.352 + 1.353 +/* -- see zlib.h -- */ 1.354 +z_off_t ZEXPORT gzseek(file, offset, whence) 1.355 + gzFile file; 1.356 + z_off_t offset; 1.357 + int whence; 1.358 +{ 1.359 + z_off64_t ret; 1.360 + 1.361 + ret = gzseek64(file, (z_off64_t)offset, whence); 1.362 + return ret == (z_off_t)ret ? (z_off_t)ret : -1; 1.363 +} 1.364 + 1.365 +/* -- see zlib.h -- */ 1.366 +z_off64_t ZEXPORT gztell64(file) 1.367 + gzFile file; 1.368 +{ 1.369 + gz_statep state; 1.370 + 1.371 + /* get internal structure and check integrity */ 1.372 + if (file == NULL) 1.373 + return -1; 1.374 + state = (gz_statep)file; 1.375 + if (state->mode != GZ_READ && state->mode != GZ_WRITE) 1.376 + return -1; 1.377 + 1.378 + /* return position */ 1.379 + return state->pos + (state->seek ? state->skip : 0); 1.380 +} 1.381 + 1.382 +/* -- see zlib.h -- */ 1.383 +z_off_t ZEXPORT gztell(file) 1.384 + gzFile file; 1.385 +{ 1.386 + z_off64_t ret; 1.387 + 1.388 + ret = gztell64(file); 1.389 + return ret == (z_off_t)ret ? (z_off_t)ret : -1; 1.390 +} 1.391 + 1.392 +/* -- see zlib.h -- */ 1.393 +z_off64_t ZEXPORT gzoffset64(file) 1.394 + gzFile file; 1.395 +{ 1.396 + z_off64_t offset; 1.397 + gz_statep state; 1.398 + 1.399 + /* get internal structure and check integrity */ 1.400 + if (file == NULL) 1.401 + return -1; 1.402 + state = (gz_statep)file; 1.403 + if (state->mode != GZ_READ && state->mode != GZ_WRITE) 1.404 + return -1; 1.405 + 1.406 + /* compute and return effective offset in file */ 1.407 + offset = LSEEK(state->fd, 0, SEEK_CUR); 1.408 + if (offset == -1) 1.409 + return -1; 1.410 + if (state->mode == GZ_READ) /* reading */ 1.411 + offset -= state->strm.avail_in; /* don't count buffered input */ 1.412 + return offset; 1.413 +} 1.414 + 1.415 +/* -- see zlib.h -- */ 1.416 +z_off_t ZEXPORT gzoffset(file) 1.417 + gzFile file; 1.418 +{ 1.419 + z_off64_t ret; 1.420 + 1.421 + ret = gzoffset64(file); 1.422 + return ret == (z_off_t)ret ? (z_off_t)ret : -1; 1.423 +} 1.424 + 1.425 +/* -- see zlib.h -- */ 1.426 +int ZEXPORT gzeof(file) 1.427 + gzFile file; 1.428 +{ 1.429 + gz_statep state; 1.430 + 1.431 + /* get internal structure and check integrity */ 1.432 + if (file == NULL) 1.433 + return 0; 1.434 + state = (gz_statep)file; 1.435 + if (state->mode != GZ_READ && state->mode != GZ_WRITE) 1.436 + return 0; 1.437 + 1.438 + /* return end-of-file state */ 1.439 + return state->mode == GZ_READ ? 1.440 + (state->eof && state->strm.avail_in == 0 && state->have == 0) : 0; 1.441 +} 1.442 + 1.443 +/* -- see zlib.h -- */ 1.444 +const char * ZEXPORT gzerror(file, errnum) 1.445 + gzFile file; 1.446 + int *errnum; 1.447 +{ 1.448 + gz_statep state; 1.449 + 1.450 + /* get internal structure and check integrity */ 1.451 + if (file == NULL) 1.452 + return NULL; 1.453 + state = (gz_statep)file; 1.454 + if (state->mode != GZ_READ && state->mode != GZ_WRITE) 1.455 + return NULL; 1.456 + 1.457 + /* return error information */ 1.458 + if (errnum != NULL) 1.459 + *errnum = state->err; 1.460 + return state->msg == NULL ? "" : state->msg; 1.461 +} 1.462 + 1.463 +/* -- see zlib.h -- */ 1.464 +void ZEXPORT gzclearerr(file) 1.465 + gzFile file; 1.466 +{ 1.467 + gz_statep state; 1.468 + 1.469 + /* get internal structure and check integrity */ 1.470 + if (file == NULL) 1.471 + return; 1.472 + state = (gz_statep)file; 1.473 + if (state->mode != GZ_READ && state->mode != GZ_WRITE) 1.474 + return; 1.475 + 1.476 + /* clear error and end-of-file */ 1.477 + if (state->mode == GZ_READ) 1.478 + state->eof = 0; 1.479 + gz_error(state, Z_OK, NULL); 1.480 +} 1.481 + 1.482 +/* Create an error message in allocated memory and set state->err and 1.483 + state->msg accordingly. Free any previous error message already there. Do 1.484 + not try to free or allocate space if the error is Z_MEM_ERROR (out of 1.485 + memory). Simply save the error message as a static string. If there is an 1.486 + allocation failure constructing the error message, then convert the error to 1.487 + out of memory. */ 1.488 +void ZLIB_INTERNAL gz_error(state, err, msg) 1.489 + gz_statep state; 1.490 + int err; 1.491 + const char *msg; 1.492 +{ 1.493 + /* free previously allocated message and clear */ 1.494 + if (state->msg != NULL) { 1.495 + if (state->err != Z_MEM_ERROR) 1.496 + free(state->msg); 1.497 + state->msg = NULL; 1.498 + } 1.499 + 1.500 + /* set error code, and if no message, then done */ 1.501 + state->err = err; 1.502 + if (msg == NULL) 1.503 + return; 1.504 + 1.505 + /* for an out of memory error, save as static string */ 1.506 + if (err == Z_MEM_ERROR) { 1.507 + state->msg = (char *)msg; 1.508 + return; 1.509 + } 1.510 + 1.511 + /* construct error message with path */ 1.512 + if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) { 1.513 + state->err = Z_MEM_ERROR; 1.514 + state->msg = (char *)"out of memory"; 1.515 + return; 1.516 + } 1.517 + strcpy(state->msg, state->path); 1.518 + strcat(state->msg, ": "); 1.519 + strcat(state->msg, msg); 1.520 + return; 1.521 +} 1.522 + 1.523 +#ifndef INT_MAX 1.524 +/* portably return maximum value for an int (when limits.h presumed not 1.525 + available) -- we need to do this to cover cases where 2's complement not 1.526 + used, since C standard permits 1's complement and sign-bit representations, 1.527 + otherwise we could just use ((unsigned)-1) >> 1 */ 1.528 +unsigned ZLIB_INTERNAL gz_intmax() 1.529 +{ 1.530 + unsigned p, q; 1.531 + 1.532 + p = 1; 1.533 + do { 1.534 + q = p; 1.535 + p <<= 1; 1.536 + p++; 1.537 + } while (p > q); 1.538 + return q >> 1; 1.539 +} 1.540 +#endif