Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer committed Dec 19, 2024
1 parent b2ba4a1 commit 2feefd6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 134 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ $(eval $(call add_include_file,kernel/fmt.h))
ifeq ($(ENABLE_ZLIB),1)
$(eval $(call add_include_file,kernel/fstdata.h))
endif
$(eval $(call add_include_file,kernel/gzip.h))
$(eval $(call add_include_file,kernel/hashlib.h))
$(eval $(call add_include_file,kernel/io.h))
$(eval $(call add_include_file,kernel/json.h))
Expand Down Expand Up @@ -610,7 +611,7 @@ $(eval $(call add_include_file,frontends/ast/ast_binding.h))
$(eval $(call add_include_file,frontends/blif/blifparse.h))
$(eval $(call add_include_file,backends/rtlil/rtlil_backend.h))

OBJS += kernel/driver.o kernel/register.o kernel/rtlil.o kernel/log.o kernel/calc.o kernel/yosys.o kernel/io.o
OBJS += kernel/driver.o kernel/register.o kernel/rtlil.o kernel/log.o kernel/calc.o kernel/yosys.o kernel/io.o kernel/gzip.o
OBJS += kernel/binding.o kernel/tclapi.o
OBJS += kernel/cellaigs.o kernel/celledges.o kernel/cost.o kernel/satgen.o kernel/scopeinfo.o kernel/qcsat.o kernel/mem.o kernel/ffmerge.o kernel/ff.o kernel/yw.o kernel/json.o kernel/fmt.o kernel/sexpr.o
OBJS += kernel/drivertools.o kernel/functional.o
Expand Down
45 changes: 45 additions & 0 deletions kernel/gzip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <string>
#include <unistd.h>
#include "kernel/yosys_common.h"

#ifndef YOSYS_GZIP_H
#define YOSYS_GZIP_H

YOSYS_NAMESPACE_BEGIN

#ifdef YOSYS_ENABLE_ZLIB

namespace Zlib {
#include <zlib.h>
} // namespace

/*
An output stream that uses a stringbuf to buffer data internally,
using zlib to write gzip-compressed data every time the stream is flushed.
*/
class gzip_ostream : public std::ostream {
public:
gzip_ostream();
bool open(const std::string &filename);
private:
class gzip_streambuf : public std::stringbuf {
public:
gzip_streambuf();
bool open(const std::string &filename);
virtual int sync() override;
virtual ~gzip_streambuf();
private:
static const int buffer_size = 4096; // Size of the internal buffer
char buffer[buffer_size]; // Internal buffer for compressed data
Zlib::gzFile gzf = nullptr; // Handle to the gzip file
};

gzip_streambuf outbuf; // The stream buffer instance
};
#endif // YOSYS_ENABLE_ZLIB

std::istream* uncompressed(std::ifstream* f, const std::string filename);

YOSYS_NAMESPACE_END

#endif // YOSYS_GZIP_H
101 changes: 3 additions & 98 deletions kernel/io.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "kernel/yosys_common.h"
#include "kernel/log.h"
#include <iostream>
#include <dirent.h>
#include <string>
#include <cstdarg>
#include <cstdio>
#include "kernel/io.h"
#include "kernel/gzip.h"
#include "kernel/log.h"

YOSYS_NAMESPACE_BEGIN

Expand Down Expand Up @@ -360,100 +361,4 @@ std::string escape_filename_spaces(const std::string& filename)
return out;
}

#ifdef YOSYS_ENABLE_ZLIB

PRIVATE_NAMESPACE_BEGIN

using namespace Zlib;

static const size_t GZ_BUFFER_SIZE = 8192;
static void decompress_gzip(const std::string &filename, std::stringstream &out)
{
char buffer[GZ_BUFFER_SIZE];
int bytes_read;
gzFile gzf = gzopen(filename.c_str(), "rb");
while(!gzeof(gzf)) {
bytes_read = gzread(gzf, reinterpret_cast<void *>(buffer), GZ_BUFFER_SIZE);
out.write(buffer, bytes_read);
}
gzclose(gzf);
}

PRIVATE_NAMESPACE_END

gzip_ostream::gzip_ostream() : std::ostream(nullptr) {
rdbuf(&outbuf);
}

bool gzip_ostream::open(const std::string &filename) {
return outbuf.open(filename);
}

gzip_ostream::gzip_streambuf::gzip_streambuf() {
setp(buffer, buffer + buffer_size - 1);
}

bool gzip_ostream::gzip_streambuf::open(const std::string &filename) {
gzf = gzopen(filename.c_str(), "wb");
return gzf != nullptr;
}

int gzip_ostream::gzip_streambuf::sync() {
int num = pptr() - pbase();
if (num > 0) {
if (gzwrite(gzf, reinterpret_cast<const void*>(pbase()), num) != num) {
return -1;
}
pbump(-num);
}
return 0;
}

gzip_ostream::gzip_streambuf::~gzip_streambuf() {
if (gzf) {
sync();
gzclose(gzf);
}
}

#endif // YOSYS_ENABLE_ZLIB


// Takes a successfully opened ifstream. If it's gzipped, returns an istream
// over a buffer of the file fully decompressed in memory. Otherwise,
// returns the original ifstream, rewound to the start.
std::istream* uncompressed(std::ifstream* f, const std::string filename) {
if (!f)
return nullptr;
// Check for gzip magic
unsigned char magic[3];
int n = 0;
while (n < 3)
{
int c = f->get();
if (c != EOF) {
magic[n] = (unsigned char) c;
}
n++;
}
if (n == 3 && magic[0] == 0x1f && magic[1] == 0x8b) {
#ifdef YOSYS_ENABLE_ZLIB
log("Found gzip magic in file `%s', decompressing using zlib.\n", filename.c_str());
if (magic[2] != 8)
log_cmd_error("gzip file `%s' uses unsupported compression type %02x\n",
filename.c_str(), unsigned(magic[2]));
delete f;
std::stringstream *df = new std::stringstream();
decompress_gzip(filename, *df);
return df;
#else
log_cmd_error("File `%s' is a gzip file, but Yosys is compiled without zlib.\n", filename.c_str());
#endif // YOSYS_ENABLE_ZLIB
} else {
f->clear();
f->seekg(0, std::ios::beg);
return f;
}
}

YOSYS_NAMESPACE_END
37 changes: 2 additions & 35 deletions kernel/io.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#include "kernel/yosys_common.h"
#include <string>
#include <unistd.h>
#include <stdarg.h>
#include "kernel/yosys_common.h"

#ifndef YOSYS_IO_H
#define YOSYS_IO_H

#ifdef YOSYS_ENABLE_ZLIB
namespace Zlib {
#include <zlib.h>
} // namespace
#endif

YOSYS_NAMESPACE_BEGIN

inline std::string vstringf(const char *fmt, va_list ap)
Expand Down Expand Up @@ -70,34 +65,6 @@ inline std::string stringf(const char *fmt, ...)
return string;
}

#ifdef YOSYS_ENABLE_ZLIB
/*
An output stream that uses a stringbuf to buffer data internally,
using zlib to write gzip-compressed data every time the stream is flushed.
*/
class gzip_ostream : public std::ostream {
public:
gzip_ostream();
bool open(const std::string &filename);
private:
class gzip_streambuf : public std::stringbuf {
public:
gzip_streambuf();
bool open(const std::string &filename);
virtual int sync() override;
virtual ~gzip_streambuf();
private:
static const int buffer_size = 4096; // Size of the internal buffer
char buffer[buffer_size]; // Internal buffer for compressed data
Zlib::gzFile gzf = nullptr; // Handle to the gzip file
};

gzip_streambuf outbuf; // The stream buffer instance
};
#endif // YOSYS_ENABLE_ZLIB

std::istream* uncompressed(std::ifstream* f, const std::string filename);

YOSYS_NAMESPACE_END

#endif // YOSYS_IO_H
1 change: 1 addition & 0 deletions kernel/register.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "kernel/yosys.h"
#include "kernel/satgen.h"
#include "kernel/json.h"
#include "kernel/gzip.h"

#include <string.h>
#include <stdlib.h>
Expand Down

0 comments on commit 2feefd6

Please sign in to comment.