lemon-project-template-glpk

diff deps/glpk/src/zlib/gzwrite.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/gzwrite.c	Sun Nov 06 20:59:10 2011 +0100
     1.3 @@ -0,0 +1,531 @@
     1.4 +/* gzwrite.c -- zlib functions for writing gzip files
     1.5 + * Copyright (C) 2004, 2005, 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 +/* Local functions */
    1.12 +local int gz_init OF((gz_statep));
    1.13 +local int gz_comp OF((gz_statep, int));
    1.14 +local int gz_zero OF((gz_statep, z_off64_t));
    1.15 +
    1.16 +/* Initialize state for writing a gzip file.  Mark initialization by setting
    1.17 +   state->size to non-zero.  Return -1 on failure or 0 on success. */
    1.18 +local int gz_init(state)
    1.19 +    gz_statep state;
    1.20 +{
    1.21 +    int ret;
    1.22 +    z_streamp strm = &(state->strm);
    1.23 +
    1.24 +    /* allocate input and output buffers */
    1.25 +    state->in = malloc(state->want);
    1.26 +    state->out = malloc(state->want);
    1.27 +    if (state->in == NULL || state->out == NULL) {
    1.28 +        if (state->out != NULL)
    1.29 +            free(state->out);
    1.30 +        if (state->in != NULL)
    1.31 +            free(state->in);
    1.32 +        gz_error(state, Z_MEM_ERROR, "out of memory");
    1.33 +        return -1;
    1.34 +    }
    1.35 +
    1.36 +    /* allocate deflate memory, set up for gzip compression */
    1.37 +    strm->zalloc = Z_NULL;
    1.38 +    strm->zfree = Z_NULL;
    1.39 +    strm->opaque = Z_NULL;
    1.40 +    ret = deflateInit2(strm, state->level, Z_DEFLATED,
    1.41 +                       15 + 16, 8, state->strategy);
    1.42 +    if (ret != Z_OK) {
    1.43 +        free(state->in);
    1.44 +        gz_error(state, Z_MEM_ERROR, "out of memory");
    1.45 +        return -1;
    1.46 +    }
    1.47 +
    1.48 +    /* mark state as initialized */
    1.49 +    state->size = state->want;
    1.50 +
    1.51 +    /* initialize write buffer */
    1.52 +    strm->avail_out = state->size;
    1.53 +    strm->next_out = state->out;
    1.54 +    state->next = strm->next_out;
    1.55 +    return 0;
    1.56 +}
    1.57 +
    1.58 +/* Compress whatever is at avail_in and next_in and write to the output file.
    1.59 +   Return -1 if there is an error writing to the output file, otherwise 0.
    1.60 +   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
    1.61 +   then the deflate() state is reset to start a new gzip stream. */
    1.62 +local int gz_comp(state, flush)
    1.63 +    gz_statep state;
    1.64 +    int flush;
    1.65 +{
    1.66 +    int ret, got;
    1.67 +    unsigned have;
    1.68 +    z_streamp strm = &(state->strm);
    1.69 +
    1.70 +    /* allocate memory if this is the first time through */
    1.71 +    if (state->size == 0 && gz_init(state) == -1)
    1.72 +        return -1;
    1.73 +
    1.74 +    /* run deflate() on provided input until it produces no more output */
    1.75 +    ret = Z_OK;
    1.76 +    do {
    1.77 +        /* write out current buffer contents if full, or if flushing, but if
    1.78 +           doing Z_FINISH then don't write until we get to Z_STREAM_END */
    1.79 +        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
    1.80 +            (flush != Z_FINISH || ret == Z_STREAM_END))) {
    1.81 +            have = (unsigned)(strm->next_out - state->next);
    1.82 +            if (have && ((got = write(state->fd, state->next, have)) < 0 ||
    1.83 +                         (unsigned)got != have)) {
    1.84 +                gz_error(state, Z_ERRNO, zstrerror());
    1.85 +                return -1;
    1.86 +            }
    1.87 +            if (strm->avail_out == 0) {
    1.88 +                strm->avail_out = state->size;
    1.89 +                strm->next_out = state->out;
    1.90 +            }
    1.91 +            state->next = strm->next_out;
    1.92 +        }
    1.93 +
    1.94 +        /* compress */
    1.95 +        have = strm->avail_out;
    1.96 +        ret = deflate(strm, flush);
    1.97 +        if (ret == Z_STREAM_ERROR) {
    1.98 +            gz_error(state, Z_STREAM_ERROR,
    1.99 +                      "internal error: deflate stream corrupt");
   1.100 +            return -1;
   1.101 +        }
   1.102 +        have -= strm->avail_out;
   1.103 +    } while (have);
   1.104 +
   1.105 +    /* if that completed a deflate stream, allow another to start */
   1.106 +    if (flush == Z_FINISH)
   1.107 +        deflateReset(strm);
   1.108 +
   1.109 +    /* all done, no errors */
   1.110 +    return 0;
   1.111 +}
   1.112 +
   1.113 +/* Compress len zeros to output.  Return -1 on error, 0 on success. */
   1.114 +local int gz_zero(state, len)
   1.115 +    gz_statep state;
   1.116 +    z_off64_t len;
   1.117 +{
   1.118 +    int first;
   1.119 +    unsigned n;
   1.120 +    z_streamp strm = &(state->strm);
   1.121 +
   1.122 +    /* consume whatever's left in the input buffer */
   1.123 +    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
   1.124 +        return -1;
   1.125 +
   1.126 +    /* compress len zeros (len guaranteed > 0) */
   1.127 +    first = 1;
   1.128 +    while (len) {
   1.129 +        n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
   1.130 +            (unsigned)len : state->size;
   1.131 +        if (first) {
   1.132 +            memset(state->in, 0, n);
   1.133 +            first = 0;
   1.134 +        }
   1.135 +        strm->avail_in = n;
   1.136 +        strm->next_in = state->in;
   1.137 +        state->pos += n;
   1.138 +        if (gz_comp(state, Z_NO_FLUSH) == -1)
   1.139 +            return -1;
   1.140 +        len -= n;
   1.141 +    }
   1.142 +    return 0;
   1.143 +}
   1.144 +
   1.145 +/* -- see zlib.h -- */
   1.146 +int ZEXPORT gzwrite(file, buf, len)
   1.147 +    gzFile file;
   1.148 +    voidpc buf;
   1.149 +    unsigned len;
   1.150 +{
   1.151 +    unsigned put = len;
   1.152 +    unsigned n;
   1.153 +    gz_statep state;
   1.154 +    z_streamp strm;
   1.155 +
   1.156 +    /* get internal structure */
   1.157 +    if (file == NULL)
   1.158 +        return 0;
   1.159 +    state = (gz_statep)file;
   1.160 +    strm = &(state->strm);
   1.161 +
   1.162 +    /* check that we're writing and that there's no error */
   1.163 +    if (state->mode != GZ_WRITE || state->err != Z_OK)
   1.164 +        return 0;
   1.165 +
   1.166 +    /* since an int is returned, make sure len fits in one, otherwise return
   1.167 +       with an error (this avoids the flaw in the interface) */
   1.168 +    if ((int)len < 0) {
   1.169 +        gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
   1.170 +        return 0;
   1.171 +    }
   1.172 +
   1.173 +    /* if len is zero, avoid unnecessary operations */
   1.174 +    if (len == 0)
   1.175 +        return 0;
   1.176 +
   1.177 +    /* allocate memory if this is the first time through */
   1.178 +    if (state->size == 0 && gz_init(state) == -1)
   1.179 +        return 0;
   1.180 +
   1.181 +    /* check for seek request */
   1.182 +    if (state->seek) {
   1.183 +        state->seek = 0;
   1.184 +        if (gz_zero(state, state->skip) == -1)
   1.185 +            return 0;
   1.186 +    }
   1.187 +
   1.188 +    /* for small len, copy to input buffer, otherwise compress directly */
   1.189 +    if (len < state->size) {
   1.190 +        /* copy to input buffer, compress when full */
   1.191 +        do {
   1.192 +            if (strm->avail_in == 0)
   1.193 +                strm->next_in = state->in;
   1.194 +            n = state->size - strm->avail_in;
   1.195 +            if (n > len)
   1.196 +                n = len;
   1.197 +            memcpy(strm->next_in + strm->avail_in, buf, n);
   1.198 +            strm->avail_in += n;
   1.199 +            state->pos += n;
   1.200 +            buf = (char *)buf + n;
   1.201 +            len -= n;
   1.202 +            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
   1.203 +                return 0;
   1.204 +        } while (len);
   1.205 +    }
   1.206 +    else {
   1.207 +        /* consume whatever's left in the input buffer */
   1.208 +        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
   1.209 +            return 0;
   1.210 +
   1.211 +        /* directly compress user buffer to file */
   1.212 +        strm->avail_in = len;
   1.213 +        strm->next_in = (voidp)buf;
   1.214 +        state->pos += len;
   1.215 +        if (gz_comp(state, Z_NO_FLUSH) == -1)
   1.216 +            return 0;
   1.217 +    }
   1.218 +
   1.219 +    /* input was all buffered or compressed (put will fit in int) */
   1.220 +    return (int)put;
   1.221 +}
   1.222 +
   1.223 +/* -- see zlib.h -- */
   1.224 +int ZEXPORT gzputc(file, c)
   1.225 +    gzFile file;
   1.226 +    int c;
   1.227 +{
   1.228 +    unsigned char buf[1];
   1.229 +    gz_statep state;
   1.230 +    z_streamp strm;
   1.231 +
   1.232 +    /* get internal structure */
   1.233 +    if (file == NULL)
   1.234 +        return -1;
   1.235 +    state = (gz_statep)file;
   1.236 +    strm = &(state->strm);
   1.237 +
   1.238 +    /* check that we're writing and that there's no error */
   1.239 +    if (state->mode != GZ_WRITE || state->err != Z_OK)
   1.240 +        return -1;
   1.241 +
   1.242 +    /* check for seek request */
   1.243 +    if (state->seek) {
   1.244 +        state->seek = 0;
   1.245 +        if (gz_zero(state, state->skip) == -1)
   1.246 +            return -1;
   1.247 +    }
   1.248 +
   1.249 +    /* try writing to input buffer for speed (state->size == 0 if buffer not
   1.250 +       initialized) */
   1.251 +    if (strm->avail_in < state->size) {
   1.252 +        if (strm->avail_in == 0)
   1.253 +            strm->next_in = state->in;
   1.254 +        strm->next_in[strm->avail_in++] = c;
   1.255 +        state->pos++;
   1.256 +        return c;
   1.257 +    }
   1.258 +
   1.259 +    /* no room in buffer or not initialized, use gz_write() */
   1.260 +    buf[0] = c;
   1.261 +    if (gzwrite(file, buf, 1) != 1)
   1.262 +        return -1;
   1.263 +    return c;
   1.264 +}
   1.265 +
   1.266 +/* -- see zlib.h -- */
   1.267 +int ZEXPORT gzputs(file, str)
   1.268 +    gzFile file;
   1.269 +    const char *str;
   1.270 +{
   1.271 +    int ret;
   1.272 +    unsigned len;
   1.273 +
   1.274 +    /* write string */
   1.275 +    len = (unsigned)strlen(str);
   1.276 +    ret = gzwrite(file, str, len);
   1.277 +    return ret == 0 && len != 0 ? -1 : ret;
   1.278 +}
   1.279 +
   1.280 +#ifdef STDC
   1.281 +#include <stdarg.h>
   1.282 +
   1.283 +/* -- see zlib.h -- */
   1.284 +int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
   1.285 +{
   1.286 +    int size, len;
   1.287 +    gz_statep state;
   1.288 +    z_streamp strm;
   1.289 +    va_list va;
   1.290 +
   1.291 +    /* get internal structure */
   1.292 +    if (file == NULL)
   1.293 +        return -1;
   1.294 +    state = (gz_statep)file;
   1.295 +    strm = &(state->strm);
   1.296 +
   1.297 +    /* check that we're writing and that there's no error */
   1.298 +    if (state->mode != GZ_WRITE || state->err != Z_OK)
   1.299 +        return 0;
   1.300 +
   1.301 +    /* make sure we have some buffer space */
   1.302 +    if (state->size == 0 && gz_init(state) == -1)
   1.303 +        return 0;
   1.304 +
   1.305 +    /* check for seek request */
   1.306 +    if (state->seek) {
   1.307 +        state->seek = 0;
   1.308 +        if (gz_zero(state, state->skip) == -1)
   1.309 +            return 0;
   1.310 +    }
   1.311 +
   1.312 +    /* consume whatever's left in the input buffer */
   1.313 +    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
   1.314 +        return 0;
   1.315 +
   1.316 +    /* do the printf() into the input buffer, put length in len */
   1.317 +    size = (int)(state->size);
   1.318 +    state->in[size - 1] = 0;
   1.319 +    va_start(va, format);
   1.320 +#ifdef NO_vsnprintf
   1.321 +#  ifdef HAS_vsprintf_void
   1.322 +    (void)vsprintf(state->in, format, va);
   1.323 +    va_end(va);
   1.324 +    for (len = 0; len < size; len++)
   1.325 +        if (state->in[len] == 0) break;
   1.326 +#  else
   1.327 +    len = vsprintf(state->in, format, va);
   1.328 +    va_end(va);
   1.329 +#  endif
   1.330 +#else
   1.331 +#  ifdef HAS_vsnprintf_void
   1.332 +    (void)vsnprintf(state->in, size, format, va);
   1.333 +    va_end(va);
   1.334 +    len = strlen(state->in);
   1.335 +#  else
   1.336 +    len = vsnprintf((char *)(state->in), size, format, va);
   1.337 +    va_end(va);
   1.338 +#  endif
   1.339 +#endif
   1.340 +
   1.341 +    /* check that printf() results fit in buffer */
   1.342 +    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
   1.343 +        return 0;
   1.344 +
   1.345 +    /* update buffer and position, defer compression until needed */
   1.346 +    strm->avail_in = (unsigned)len;
   1.347 +    strm->next_in = state->in;
   1.348 +    state->pos += len;
   1.349 +    return len;
   1.350 +}
   1.351 +
   1.352 +#else /* !STDC */
   1.353 +
   1.354 +/* -- see zlib.h -- */
   1.355 +int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
   1.356 +                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
   1.357 +    gzFile file;
   1.358 +    const char *format;
   1.359 +    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
   1.360 +        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
   1.361 +{
   1.362 +    int size, len;
   1.363 +    gz_statep state;
   1.364 +    z_streamp strm;
   1.365 +
   1.366 +    /* get internal structure */
   1.367 +    if (file == NULL)
   1.368 +        return -1;
   1.369 +    state = (gz_statep)file;
   1.370 +    strm = &(state->strm);
   1.371 +
   1.372 +    /* check that we're writing and that there's no error */
   1.373 +    if (state->mode != GZ_WRITE || state->err != Z_OK)
   1.374 +        return 0;
   1.375 +
   1.376 +    /* make sure we have some buffer space */
   1.377 +    if (state->size == 0 && gz_init(state) == -1)
   1.378 +        return 0;
   1.379 +
   1.380 +    /* check for seek request */
   1.381 +    if (state->seek) {
   1.382 +        state->seek = 0;
   1.383 +        if (gz_zero(state, state->skip) == -1)
   1.384 +            return 0;
   1.385 +    }
   1.386 +
   1.387 +    /* consume whatever's left in the input buffer */
   1.388 +    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
   1.389 +        return 0;
   1.390 +
   1.391 +    /* do the printf() into the input buffer, put length in len */
   1.392 +    size = (int)(state->size);
   1.393 +    state->in[size - 1] = 0;
   1.394 +#ifdef NO_snprintf
   1.395 +#  ifdef HAS_sprintf_void
   1.396 +    sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
   1.397 +            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
   1.398 +    for (len = 0; len < size; len++)
   1.399 +        if (state->in[len] == 0) break;
   1.400 +#  else
   1.401 +    len = sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
   1.402 +                a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
   1.403 +#  endif
   1.404 +#else
   1.405 +#  ifdef HAS_snprintf_void
   1.406 +    snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
   1.407 +             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
   1.408 +    len = strlen(state->in);
   1.409 +#  else
   1.410 +    len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
   1.411 +                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
   1.412 +#  endif
   1.413 +#endif
   1.414 +
   1.415 +    /* check that printf() results fit in buffer */
   1.416 +    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
   1.417 +        return 0;
   1.418 +
   1.419 +    /* update buffer and position, defer compression until needed */
   1.420 +    strm->avail_in = (unsigned)len;
   1.421 +    strm->next_in = state->in;
   1.422 +    state->pos += len;
   1.423 +    return len;
   1.424 +}
   1.425 +
   1.426 +#endif
   1.427 +
   1.428 +/* -- see zlib.h -- */
   1.429 +int ZEXPORT gzflush(file, flush)
   1.430 +    gzFile file;
   1.431 +    int flush;
   1.432 +{
   1.433 +    gz_statep state;
   1.434 +
   1.435 +    /* get internal structure */
   1.436 +    if (file == NULL)
   1.437 +        return -1;
   1.438 +    state = (gz_statep)file;
   1.439 +
   1.440 +    /* check that we're writing and that there's no error */
   1.441 +    if (state->mode != GZ_WRITE || state->err != Z_OK)
   1.442 +        return Z_STREAM_ERROR;
   1.443 +
   1.444 +    /* check flush parameter */
   1.445 +    if (flush < 0 || flush > Z_FINISH)
   1.446 +        return Z_STREAM_ERROR;
   1.447 +
   1.448 +    /* check for seek request */
   1.449 +    if (state->seek) {
   1.450 +        state->seek = 0;
   1.451 +        if (gz_zero(state, state->skip) == -1)
   1.452 +            return -1;
   1.453 +    }
   1.454 +
   1.455 +    /* compress remaining data with requested flush */
   1.456 +    gz_comp(state, flush);
   1.457 +    return state->err;
   1.458 +}
   1.459 +
   1.460 +/* -- see zlib.h -- */
   1.461 +int ZEXPORT gzsetparams(file, level, strategy)
   1.462 +    gzFile file;
   1.463 +    int level;
   1.464 +    int strategy;
   1.465 +{
   1.466 +    gz_statep state;
   1.467 +    z_streamp strm;
   1.468 +
   1.469 +    /* get internal structure */
   1.470 +    if (file == NULL)
   1.471 +        return Z_STREAM_ERROR;
   1.472 +    state = (gz_statep)file;
   1.473 +    strm = &(state->strm);
   1.474 +
   1.475 +    /* check that we're writing and that there's no error */
   1.476 +    if (state->mode != GZ_WRITE || state->err != Z_OK)
   1.477 +        return Z_STREAM_ERROR;
   1.478 +
   1.479 +    /* if no change is requested, then do nothing */
   1.480 +    if (level == state->level && strategy == state->strategy)
   1.481 +        return Z_OK;
   1.482 +
   1.483 +    /* check for seek request */
   1.484 +    if (state->seek) {
   1.485 +        state->seek = 0;
   1.486 +        if (gz_zero(state, state->skip) == -1)
   1.487 +            return -1;
   1.488 +    }
   1.489 +
   1.490 +    /* change compression parameters for subsequent input */
   1.491 +    if (state->size) {
   1.492 +        /* flush previous input with previous parameters before changing */
   1.493 +        if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
   1.494 +            return state->err;
   1.495 +        deflateParams(strm, level, strategy);
   1.496 +    }
   1.497 +    state->level = level;
   1.498 +    state->strategy = strategy;
   1.499 +    return Z_OK;
   1.500 +}
   1.501 +
   1.502 +/* -- see zlib.h -- */
   1.503 +int ZEXPORT gzclose_w(file)
   1.504 +    gzFile file;
   1.505 +{
   1.506 +    int ret = 0;
   1.507 +    gz_statep state;
   1.508 +
   1.509 +    /* get internal structure */
   1.510 +    if (file == NULL)
   1.511 +        return Z_STREAM_ERROR;
   1.512 +    state = (gz_statep)file;
   1.513 +
   1.514 +    /* check that we're writing */
   1.515 +    if (state->mode != GZ_WRITE)
   1.516 +        return Z_STREAM_ERROR;
   1.517 +
   1.518 +    /* check for seek request */
   1.519 +    if (state->seek) {
   1.520 +        state->seek = 0;
   1.521 +        ret += gz_zero(state, state->skip);
   1.522 +    }
   1.523 +
   1.524 +    /* flush, free memory, and close file */
   1.525 +    ret += gz_comp(state, Z_FINISH);
   1.526 +    (void)deflateEnd(&(state->strm));
   1.527 +    free(state->out);
   1.528 +    free(state->in);
   1.529 +    gz_error(state, Z_OK, NULL);
   1.530 +    free(state->path);
   1.531 +    ret += close(state->fd);
   1.532 +    free(state);
   1.533 +    return ret ? Z_ERRNO : Z_OK;
   1.534 +}