#pragma once

#include "common.h"
#include "fw.h"

#include <zlib.h>

struct spz_stream;
typedef struct spz_stream spz_stream;

#define SPZ_NBUF 4
struct spz_stream {
    z_stream zs;
    read_func_t read_data;	    /* Read input data */
    token_t token;		    /* Read input token */
    uint8_t *optr;		    /* Output data pointer into obuf */
    /* Note: available output data ends at zs->next_out */
    union {
	uint8_t *bufs[SPZ_NBUF];
	struct {
	    uint8_t *ibuf;	    /* Input buffer if compressed */
	    uint8_t *obuf;	    /* Output buffer */
	    uint8_t *dbuf;	    /* Block data buffer */
	    uint8_t *vbuf;	    /* Readback/verify buffer */
	};
    };
    struct fw_header header;	    /* Header of currently processed chunk */
    struct fw_header vmatch;	    /* Matched firmware version string */
    int err;			    /* Error code to return */
    bool eoi;			    /* Reached end of input */
    bool cleanup;		    /* Call inflateEnd() */
    bool esp_updated;		    /* ESP OTA update performed */
    bool fpga_updated;		    /* FPGA initialization performed */
};

int spz_read_data(spz_stream *spz, void *buf, size_t len);
