static void decompress_gzip(int fd_in, int fd_out, const char *desc) { char buffer[DPKG_BUFFER_SIZE]; gzFile gzfile = gzdopen(fd_in, "r"); if (gzfile == NULL) ohshit(_("%s: error binding input to gzip stream"), desc); for (;;) { int actualread, actualwrite; actualread = gzread(gzfile, buffer, sizeof(buffer)); if (actualread < 0) { int z_errnum = 0; const char *errmsg = gzerror(gzfile, &z_errnum); if (z_errnum == Z_ERRNO) errmsg = strerror(errno); ohshit(_("%s: internal gzip read error: '%s'"), desc, errmsg); } if (actualread == 0) /* EOF. */ break; actualwrite = fd_write(fd_out, buffer, actualread); if (actualwrite != actualread) ohshite(_("%s: internal gzip write error"), desc); } if (close(fd_out)) ohshite(_("%s: internal gzip write error"), desc); }
void GzipOutputFile::write(const void* buf, size_t bufSize) { if (bufSize == 0) return; assert(buf); assert(m_gzfile != NULL && m_fd != -1); gzFile f = static_cast<gzFile>(m_gzfile); size_t written = 0; const char* c = static_cast<const char*>(buf); while(written < bufSize) { const int res = gzwrite(f, &c[written], bufSize - written); if (res == 0)//the was an error; { int code; const char* msg = gzerror(f, &code); if (code == Z_ERRNO)//The error is in the system, not in the zlib; SYS_STOP("gzwrite(" + m_fileName + ")"); assert(msg); GZIP_STOP(msg); } assert(res > 0); written += (size_t)res; assert(written <= bufSize); } }
static int z_fgetc(void *_fh) { struct z_file *fh = _fh; int c; if (fh->err || fh->eof) { c = XEOF; goto done; } c = gzgetc(fh->file); if (c < 0) { int errnum; const char *msg; msg = gzerror(fh->file, &errnum); if (errnum == Z_STREAM_END) fh->eof = 1; else if (errnum == Z_ERRNO) { fh->err = 1; lib_err_msg(strerror(errno)); } else { fh->err = 1; lib_err_msg(msg); } c = XEOF; } else xassert(0x00 <= c && c <= 0xFF); done: return c; }
/* XXX zlib-1.0.4 has not */ static int gzdSeek(FD_t fd, off_t pos, int whence) { off_t p = pos; int rc; #if HAVE_GZSEEK gzFile gzfile; if (fd == NULL) return -2; gzfile = gzdFileno(fd); if (gzfile == NULL) return -2; /* XXX can't happen */ rc = gzseek(gzfile, p, whence); if (rc < 0) { int zerror = 0; fd->errcookie = gzerror(gzfile, &zerror); if (zerror == Z_ERRNO) { fd->syserrno = errno; fd->errcookie = strerror(fd->syserrno); } } #else rc = -2; #endif return rc; }
bool rz3_section_header::read(gzFile gzf) { int32_t bytes_read = gzread(gzf, this, sizeof(rz3_section_header)); if (bytes_read == 0) { /* end of file */ return false; } if (bytes_read != sizeof(rz3_section_header)) { int32_t errnum; fprintf(stderr, "rz3_section_header::read() - gzread error (bytes read=%d, req = %d) %s\n", bytes_read, sizeof(rz3_section_header), gzerror(gzf, &errnum)); fprintf(stderr, "errnum %d\n", errnum); return false; } //jan { //Need to swap multi-byte values on little endian machines nrecords = SWAP_WORD(nrecords); CompressedBufferSize = SWAP_LONG(CompressedBufferSize); //fprintf(stderr, "NRECORDS: %x;\nCOMPRESSEDBUFFERSIZE: %llx;\n", nrecords, CompressedBufferSize); for (int32_t j=0; j<rstzip3::bitarray_count; j++) { rz3_bitarray_counts[j] = SWAP_WORD(rz3_bitarray_counts[j]); //fprintf(stderr, "J: %d; BITARRAYCOUNTS: %x;\n", j, rz3_bitarray_counts[j]); } //jan } // sanity checks return sanity_check(); } // bool rz3_section_header::read(gzFile gzf)
static void _gzerror(gzFile fp, int *errnum, const char **errmsg) { *errmsg = gzerror(fp, errnum); if (*errnum == Z_ERRNO) { *errmsg = strerror(*errnum); } }
int cfread(void *ptr, int size, cfp *fp) { int ret; if (size == 0) return 0; #ifdef HAVE_LIBZ if (fp->compressedfp) { ret = gzread(fp->compressedfp, ptr, size); if (ret != size && !gzeof(fp->compressedfp)) { int errnum; const char *errmsg = gzerror(fp->compressedfp, &errnum); exit_horribly(modulename, "could not read from input file: %s\n", errnum == Z_ERRNO ? strerror(errno) : errmsg); } } else #endif { ret = fread(ptr, 1, size, fp->uncompressedfp); if (ret != size && !feof(fp->uncompressedfp)) READ_ERROR_EXIT(fp->uncompressedfp); } return ret; }
// Check for errors and close void gzutil_fclose(gzFile gz) { if(gz == NULL) return; int ecode; const char *errstr = gzerror(gz, &ecode); if(ecode < 0) warn("GZIP File error: %s [%i]", errstr, ecode); gzclose(gz); }
bool ok() const { if (f == NULL) { return false; } int err; gzerror(f, &err); return (err == 0); }
/* Push data from a gzip file pointer down a stream ** ------------------------------------- ** ** This routine is responsible for creating and PRESENTING any ** graphic (or other) objects described by the file. ** ** ** State of file and target stream on entry: ** gzFile (gzfp) assumed open (should have gzipped content), ** target (sink) assumed valid. ** ** Return values: ** HT_INTERRUPTED Interruption after some data read. ** HT_PARTIAL_CONTENT Error after some data read. ** -1 Error before any data read. ** HT_LOADED Normal end of file indication on reading. ** ** State of file and target stream on return: ** always gzfp still open, target stream still valid. */ PRIVATE int HTGzFileCopy ARGS2( gzFile, gzfp, HTStream*, sink) { HTStreamClass targetClass; int status, bytes; int gzerrnum; int rv = HT_OK; /* Push the data down the stream */ targetClass = *(sink->isa); /* Copy pointers to procedures */ /* read and inflate gzip'd file, and push binary down sink */ HTReadProgress(bytes = 0, 0); for (;;) { status = gzread(gzfp, input_buffer, INPUT_BUFFER_SIZE); if (status <= 0) { /* EOF or error */ if (status == 0) { rv = HT_LOADED; break; } CTRACE((tfp, "HTGzFileCopy: Read error, gzread returns %d\n", status)); CTRACE((tfp, "gzerror : %s\n", gzerror(gzfp, &gzerrnum))); if (TRACE) { if (gzerrnum == Z_ERRNO) perror("gzerror "); } if (bytes) { rv = HT_PARTIAL_CONTENT; } else { rv = -1; } break; } (*targetClass.put_block)(sink, input_buffer, status); bytes += status; HTReadProgress(bytes, -1); HTDisplayPartial(); if (HTCheckForInterrupt()) { _HTProgress (TRANSFER_INTERRUPTED); if (bytes) { rv = HT_INTERRUPTED; } else { rv = -1; } break; } } /* next bufferload */ HTFinishDisplayPartial(); return rv; }
void gt_xgzfputs(const char *str, gzFile file) { int errnum; if (gzputs(file, str) == -1) { fprintf(stderr, "cannot put string to compressed file: %s\n", gzerror(file, &errnum)); exit(EXIT_FAILURE); } }
void gt_xgzfputc(int c, gzFile file) { int errnum; if (gzputc(file, c) == -1) { fprintf(stderr, "cannot put character to compressed file: %s\n", gzerror(file, &errnum)); exit(EXIT_FAILURE); } }
/** * Gunzip a given file and remove the .gz if successful. * @param ci container with filename * @return true if the gunzip completed */ static bool GunzipFile(const ContentInfo *ci) { #if defined(WITH_ZLIB) bool ret = true; FILE *ftmp = fopen(GetFullFilename(ci, true), "rb"); gzFile fin = gzdopen(fileno(ftmp), "rb"); FILE *fout = fopen(GetFullFilename(ci, false), "wb"); if (fin == NULL || fout == NULL) { ret = false; } else { byte buff[8192]; while (1) { int read = gzread(fin, buff, sizeof(buff)); if (read == 0) { /* If gzread() returns 0, either the end-of-file has been * reached or an underlying read error has occurred. * * gzeof() can't be used, because: * 1.2.5 - it is safe, 1 means 'everything was OK' * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly' * 1.2.3.3 - 1 is returned for truncated archive * * So we use gzerror(). When proper end of archive * has been reached, then: * errnum == Z_STREAM_END in 1.2.3.3, * errnum == 0 in 1.2.4 and 1.2.5 */ int errnum; gzerror(fin, &errnum); if (errnum != 0 && errnum != Z_STREAM_END) ret = false; break; } if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) { /* If gzread() returns -1, there was an error in archive */ ret = false; break; } /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3! * if (read < sizeof(buff)) break; */ } } if (fin != NULL) { /* Closes ftmp too! */ gzclose(fin); } else if (ftmp != NULL) { /* In case the gz stream was opened correctly this will * be closed by gzclose. */ fclose(ftmp); } if (fout != NULL) fclose(fout); return ret; #else NOT_REACHED(); #endif /* defined(WITH_ZLIB) */ }
void log_write_packet(struct log_fd *fd, struct packet_object *po) { struct log_header_packet hp; int c, zerr; memset(&hp, 0, sizeof(struct log_header_packet)); /* adjust the timestamp */ memcpy(&hp.tv, &po->ts, sizeof(struct timeval)); hp.tv.tv_sec = htonl(hp.tv.tv_sec); hp.tv.tv_usec = htonl(hp.tv.tv_usec); memcpy(&hp.L2_src, &po->L2.src, MEDIA_ADDR_LEN); memcpy(&hp.L2_dst, &po->L2.dst, MEDIA_ADDR_LEN); memcpy(&hp.L3_src, &po->L3.src, sizeof(struct ip_addr)); memcpy(&hp.L3_dst, &po->L3.dst, sizeof(struct ip_addr)); hp.L4_flags = po->L4.flags; hp.L4_proto = po->L4.proto; hp.L4_src = po->L4.src; hp.L4_dst = po->L4.dst; /* the length of the payload */ hp.len = htonl(po->DATA.disp_len); LOG_LOCK; if (fd->type == LOG_COMPRESSED) { c = gzwrite(fd->cfd, &hp, sizeof(hp)); ON_ERROR(c, -1, "%s", gzerror(fd->cfd, &zerr)); c = gzwrite(fd->cfd, po->DATA.disp_data, po->DATA.disp_len); ON_ERROR(c, -1, "%s", gzerror(fd->cfd, &zerr)); } else { c = write(fd->fd, &hp, sizeof(hp)); ON_ERROR(c, -1, "Can't write to logfile"); c = write(fd->fd, po->DATA.disp_data, po->DATA.disp_len); ON_ERROR(c, -1, "Can't write to logfile"); } LOG_UNLOCK; }
static void compress_gzip(int fd_in, int fd_out, struct compress_params *params, const char *desc) { char buffer[DPKG_BUFFER_SIZE]; char combuf[6]; int strategy; int z_errnum; gzFile gzfile; if (params->strategy == COMPRESSOR_STRATEGY_FILTERED) strategy = 'f'; else if (params->strategy == COMPRESSOR_STRATEGY_HUFFMAN) strategy = 'h'; else if (params->strategy == COMPRESSOR_STRATEGY_RLE) strategy = 'R'; else if (params->strategy == COMPRESSOR_STRATEGY_FIXED) strategy = 'F'; else strategy = ' '; snprintf(combuf, sizeof(combuf), "w%d%c", params->level, strategy); gzfile = gzdopen(fd_out, combuf); if (gzfile == NULL) ohshit(_("%s: error binding output to gzip stream"), desc); for (;;) { int actualread, actualwrite; actualread = fd_read(fd_in, buffer, sizeof(buffer)); if (actualread < 0) ohshite(_("%s: internal gzip read error"), desc); if (actualread == 0) /* EOF. */ break; actualwrite = gzwrite(gzfile, buffer, actualread); if (actualwrite != actualread) { const char *errmsg = gzerror(gzfile, &z_errnum); if (z_errnum == Z_ERRNO) errmsg = strerror(errno); ohshit(_("%s: internal gzip write error: '%s'"), desc, errmsg); } } z_errnum = gzclose(gzfile); if (z_errnum) { const char *errmsg; if (z_errnum == Z_ERRNO) errmsg = strerror(errno); else errmsg = zError(z_errnum); ohshit(_("%s: internal gzip write error: %s"), desc, errmsg); } }
static void gzdSetError(FDSTACK_t fps) { gzFile gzfile = fps->fp; int zerror = 0; fps->errcookie = gzerror(gzfile, &zerror); if (zerror == Z_ERRNO) { fps->syserrno = errno; fps->errcookie = strerror(fps->syserrno); } }
void open_log(char *file) { int zerr; GBL_LOGFILE = strdup(file); GBL_LOG_FD = gzopen(file, "rb"); ON_ERROR(GBL_LOG_FD, NULL, "%s", gzerror(GBL_LOG_FD, &zerr)); }
void gt_xgzwrite(gzFile file, void *buf, unsigned len) { int errnum; gt_assert(buf && len); if (gzwrite(file, buf, len) != len) { fprintf(stderr, "cannot write to compressed file: %s\n", gzerror(file, &errnum)); exit(EXIT_FAILURE); } }
static gboolean uncompress_file (int input, int output, MudError** error) { int len; char buf[GZBUFSIZE]; gzFile gzin = gzdopen (input, "rb"); if (gzin == NULL) { int gzerr; const char* gzmsg = gzerror (gzin, &gzerr); *error = mud_error_new (MUD_GAMELIST_ERROR, gzerr, gzmsg); len = -1; close (input); return FALSE; } do { len = gzread (gzin, buf, GZBUFSIZE); if (len < 0) { int gzerr; const char* gzmsg = gzerror (gzin, &gzerr); *error = mud_error_new (MUD_GAMELIST_ERROR, gzerr, gzmsg); len = -1; } else if (len > 0) { if (write (output, buf, len) != len) { *error = mud_error_new (MUD_GAMELIST_ERROR, errno, strerror (errno)); len = -1; } } } while (len > 0); gzclose (gzin); return len == 0; }
int gt_xgzread(gzFile file, void *buf, unsigned len) { int errnum, rval; if ((rval = gzread(file, buf, len)) == -1) { fprintf(stderr, "cannot read from compressed file: %s\n", gzerror(file, &errnum)); exit(EXIT_FAILURE); } return rval; }
bool GenomicRegionCollection<T>::ReadBED(const std::string & file, const BamHeader& hdr) { m_sorted = false; idx = 0; gzFile fp = NULL; fp = strcmp(file.c_str(), "-")? gzopen(file.c_str(), "r") : gzdopen(fileno(stdin), "r"); if (file.empty() || !fp) { std::cerr << "BED file not readable: " << file << std::endl; return false; } // http://www.lemoda.net/c/gzfile-read/ while (1) { int err; char buffer[GZBUFFER]; gzgets(fp, buffer, GZBUFFER); int bytes_read = strlen(buffer); // get one line if (bytes_read < GZBUFFER - 1) { if (gzeof (fp)) break; else { const char * error_string; error_string = gzerror (fp, &err); if (err) { fprintf (stderr, "Error: %s.\n", error_string); exit (EXIT_FAILURE); } } } // prepare to loop through each field of BED line //size_t counter = 0; std::string chr, pos1, pos2; std::string line(buffer); std::istringstream iss_line(line); std::string val; if (line.find("#") != std::string::npos) continue; // read first three BED columns iss_line >> chr >> pos1 >> pos2; // construct the GenomicRegion T gr(chr, pos1, pos2, hdr); if (gr.chr >= 0) m_grv->push_back(gr); } return true; }
bool CRepo::parse() { assert(!tmpFile.empty()); if (tmpFile.empty()) { LOG_DEBUG("tmpfile empty, repo not initialized?"); return false; } LOG_DEBUG("%s", tmpFile.c_str()); FILE* f = fileSystem->propen(tmpFile, "rb"); if (f == nullptr) { LOG_ERROR("Could not open %s", tmpFile.c_str()); return false; } gzFile fp = gzdopen(fileno(f), "rb"); if (fp == Z_NULL) { fclose(f); LOG_ERROR("Could not gzdopen %s", tmpFile.c_str()); return false; } char buf[IO_BUF_SIZE]; sdps.clear(); while ((gzgets(fp, buf, sizeof(buf))) != Z_NULL) { for (unsigned int i = 0; i < sizeof(buf); i++) { if (buf[i] == '\n') { buf[i] = 0; break; } } const std::string line = buf; std::vector<std::string> items = tokenizeString(line, ','); if (items.size() < 4) { LOG_ERROR("Invalid line: %s", line.c_str()); continue; } // create new repo from url CSdp sdptmp = CSdp(items[0], items[1], items[3], items[2], repourl); rapid->addRemoteSdp(sdptmp); } int errnum = Z_OK; const char* errstr = gzerror(fp, &errnum); switch (errnum) { case Z_OK: case Z_STREAM_END: break; default: LOG_ERROR("%d %s\n", errnum, errstr); } gzclose(fp); fclose(f); return true; }
static const char * get_gz_error(gzFile gzf) { int errnum; const char *errmsg; errmsg = gzerror(gzf, &errnum); if (errnum == Z_ERRNO) return strerror(errno); else return errmsg; }
static size_t zlib_file_write(SDL_RWops* const context, const void *ptr, size_t size, size_t num) { auto const fp = reinterpret_cast<gzFile>(context->hidden.unknown.data1); const size_t nwrote = gzwrite(fp, ptr, size * num); int error; gzerror(fp, &error); if (nwrote == 0 && error) { SDL_Error(SDL_EFWRITE); } return (nwrote); }
static size_t zlib_file_read(SDL_RWops* const context, void *ptr, size_t size, size_t maxnum) { auto const fp = reinterpret_cast<gzFile>(context->hidden.unknown.data1); const size_t nread = gzread(fp, ptr, size * maxnum); int error; gzerror(fp, &error); if (nread == 0 && error) { SDL_Error(SDL_EFREAD); } return nread; }
int dlp_ferror(DLP_FILE *lpZF) { if(!lpZF) return -1; #ifndef __NOZLIB if(lpZF->m_nCompressed){ int nErr; gzerror((gzFile)lpZF->m_lpFile,&nErr); return nErr<0 ? nErr : 0; } #endif /* __NOZLIB */ return ferror((FILE*)lpZF->m_lpFile); }
int err_gzread(gzFile file, void *ptr, unsigned int len) { int ret = gzread(file, ptr, len); if (ret < 0) { int errnum = 0; const char *msg = gzerror(file, &errnum); _err_fatal_simple("gzread", Z_ERRNO == errnum ? strerror(errno) : msg); } return ret; }
static int load_zlib(struct kmod_file *file) { int err = 0; off_t did = 0, total = 0; _cleanup_free_ unsigned char *p = NULL; errno = 0; file->gzf = gzdopen(file->fd, "rb"); if (file->gzf == NULL) return -errno; file->fd = -1; /* now owned by gzf due gzdopen() */ for (;;) { int r; if (did == total) { void *tmp = realloc(p, total + READ_STEP); if (tmp == NULL) { err = -errno; goto error; } total += READ_STEP; p = tmp; } r = gzread(file->gzf, p + did, total - did); if (r == 0) break; else if (r < 0) { int gzerr; const char *gz_errmsg = gzerror(file->gzf, &gzerr); ERR(file->ctx, "gzip: %s\n", gz_errmsg); /* gzip might not set errno here */ err = gzerr == Z_ERRNO ? -errno : -EINVAL; goto error; } did += r; } file->memory = p; file->size = did; p = NULL; return 0; error: gzclose(file->gzf); return err; }
int64_t ZipFile::readImpl(char *buffer, int64_t length) { assert(m_gzFile); int64_t nread = gzread(m_gzFile, buffer, length); if (nread == 0 || gzeof(m_gzFile)) { m_eof = true; } else { errno = 0; gzerror(m_gzFile, &errno); if (errno == 1) { // Z_STREAM_END = 1 m_eof = true; } } return (nread < 0) ? 0 : nread; }
static int close_zp(gzFile *zp) { int e; int ret=0; if(!*zp) return ret; if((e=gzclose(*zp))) { const char *str=NULL; if(e==Z_ERRNO) str=strerror(errno); else str=gzerror(*zp, &e); logp("gzclose failed: %d (%s)\n", e, str?:""); ret=-1; } return ret; }