+ return(EXIT_SUCCESS);
+
+}
+
+
+int zlib_decompress(FILE *infile, FILE *outfile)
+{
+ #define DECOMPRESS_BUF_SIZE 1024
+ uint8_t outbuf[DECOMPRESS_BUF_SIZE];
+ uint8_t inbuf[DECOMPRESS_BUF_SIZE];
+ int32_t ret;
+
+ z_stream compressed_fpga_stream;
+
+ // initialize zlib structures
+ compressed_fpga_stream.next_in = inbuf;
+ compressed_fpga_stream.avail_in = 0;
+ compressed_fpga_stream.next_out = outbuf;
+ compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
+ compressed_fpga_stream.zalloc = fpga_deflate_malloc;
+ compressed_fpga_stream.zfree = fpga_deflate_free;
+ compressed_fpga_stream.opaque = Z_NULL;
+
+ ret = inflateInit2(&compressed_fpga_stream, 0);
+
+ do {
+ if (compressed_fpga_stream.avail_in == 0) {
+ compressed_fpga_stream.next_in = inbuf;
+ uint16_t i = 0;
+ do {
+ int32_t c = fgetc(infile);
+ if (!feof(infile)) {
+ inbuf[i++] = c & 0xFF;
+ compressed_fpga_stream.avail_in++;
+ } else {
+ break;
+ }
+ } while (i < DECOMPRESS_BUF_SIZE);
+ }
+
+ ret = inflate(&compressed_fpga_stream, Z_SYNC_FLUSH);
+
+ if (ret != Z_OK && ret != Z_STREAM_END) {
+ break;
+ }
+
+ if (compressed_fpga_stream.avail_out == 0) {
+ for (uint16_t i = 0; i < DECOMPRESS_BUF_SIZE; i++) {
+ fputc(outbuf[i], outfile);
+ }
+ compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
+ compressed_fpga_stream.next_out = outbuf;
+ }
+ } while (ret == Z_OK);
+
+ if (ret == Z_STREAM_END) { // reached end of input
+ uint16_t i = 0;
+ while (compressed_fpga_stream.avail_out < DECOMPRESS_BUF_SIZE) {
+ fputc(outbuf[i++], outfile);
+ compressed_fpga_stream.avail_out++;
+ }
+ fclose(outfile);
+ fclose(infile);
+ return(EXIT_SUCCESS);
+ } else {
+ fprintf(stderr, "Error. Inflate() returned error %i, %s", ret, compressed_fpga_stream.msg);
+ fclose(outfile);
+ fclose(infile);
+ return(EXIT_FAILURE);
+ }
+
+}
+
+
+/* Simple Xilinx .bit parser. The file starts with the fixed opaque byte sequence
+ * 00 09 0f f0 0f f0 0f f0 0f f0 00 00 01
+ * After that the format is 1 byte section type (ASCII character), 2 byte length
+ * (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
+ * length.
+ */
+static int bitparse_find_section(FILE *infile, char section_name, unsigned int *section_length)
+{
+ int result = 0;
+ #define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100 // maximum number of bytes to search for the requested section
+ uint16_t numbytes = 0;
+ while(numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH) {
+ char current_name = (char)fgetc(infile);
+ numbytes++;
+ if(current_name < 'a' || current_name > 'e') {
+ /* Strange section name, abort */
+ break;
+ }
+ unsigned int current_length = 0;
+ switch(current_name) {
+ case 'e':
+ /* Four byte length field */
+ current_length += fgetc(infile) << 24;
+ current_length += fgetc(infile) << 16;
+ numbytes += 2;
+ default: /* Fall through, two byte length field */
+ current_length += fgetc(infile) << 8;
+ current_length += fgetc(infile) << 0;
+ numbytes += 2;
+ }
+
+ if(current_name != 'e' && current_length > 255) {
+ /* Maybe a parse error */
+ break;
+ }
+
+ if(current_name == section_name) {
+ /* Found it */
+ *section_length = current_length;
+ result = 1;
+ break;
+ }
+
+ for (uint16_t i = 0; i < current_length && numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH; i++) {
+ (void)fgetc(infile);
+ numbytes++;
+ }
+ }
+
+ return result;
+}
+
+
+static int FpgaGatherVersion(FILE *infile, char* infile_name, char *dst, int len)
+{
+ unsigned int fpga_info_len;
+ char tempstr[40] = {0x00};