/* returns the total size of the encoded data */
int zlib_encode(ZlibEncoder *zlib, int level, int input_size,
                uint8_t *io_ptr, unsigned int num_io_bytes)
{
    int flush;
    int enc_size = 0;
    int out_size = 0;
    int z_ret;

    z_ret = deflateReset(&zlib->strm);

    if (z_ret != Z_OK) {
        red_error("deflateReset failed");
    }

    zlib->strm.next_out = io_ptr;
    zlib->strm.avail_out = num_io_bytes;

    if (level != zlib->last_level) {
        if (zlib->strm.avail_out == 0) {
            zlib->strm.avail_out = zlib->usr->more_space(zlib->usr, &zlib->strm.next_out);
            if (zlib->strm.avail_out == 0) {
                red_error("not enough space");
            }
        }
        z_ret = deflateParams(&zlib->strm, level, Z_DEFAULT_STRATEGY);
        if (z_ret != Z_OK) {
            red_error("deflateParams failed");
        }
        zlib->last_level = level;
    }


    do {
        zlib->strm.avail_in = zlib->usr->more_input(zlib->usr, &zlib->strm.next_in);
        if (zlib->strm.avail_in <= 0) {
            red_error("more input failed\n");
        }
        enc_size += zlib->strm.avail_in;
        flush = (enc_size == input_size) ?  Z_FINISH : Z_NO_FLUSH;
        while (1) {
            int deflate_size = zlib->strm.avail_out;
            z_ret = deflate(&zlib->strm, flush);
            ASSERT(z_ret != Z_STREAM_ERROR);
            out_size += deflate_size - zlib->strm.avail_out;
            if (zlib->strm.avail_out) {
                break;
            }

            zlib->strm.avail_out = zlib->usr->more_space(zlib->usr, &zlib->strm.next_out);
            if (zlib->strm.avail_out == 0) {
                red_error("not enough space");
            }
        }
    } while (flush != Z_FINISH);

    ASSERT(z_ret == Z_STREAM_END);
    return out_size;
}
Esempio n. 2
0
File: red.c Progetto: jalcim/42sh
int			double_right(char *line, t_sh *t)
{
	int		i;
	int		j;
	char	*l_p;

	i = t->pv2 + 2;
	j = t->pv2 + 2;
	l_p = NULL;
	t->fd1 = 0;
	i += ft_wh_not_ope(&line[i], t);
	t->wf = ft_strsub(line, j, i - j);
	if ((t->fd1 = open(t->wf, O_CREAT | O_RDWR, 0666)) != -1)
		return (double_rightbis(line, t, l_p, i));
	return (red_error(line, t));
}