static void filter_unlzma_init(struct io_lzma *io, lzma_stream *s) { uint64_t memlimit = UINT64_MAX; lzma_ret ret; io->status |= DPKG_STREAM_DECOMPRESS; ret = lzma_alone_decoder(s, memlimit); if (ret != LZMA_OK) filter_lzma_error(io, ret); }
static int xc_try_lzma_decode( struct xc_dom_image *dom, void **blob, size_t *size) { lzma_stream stream = LZMA_STREAM_INIT; if ( lzma_alone_decoder(&stream, LZMA_BLOCK_SIZE) != LZMA_OK ) { DOMPRINTF("LZMA: Failed to init decoder"); return -1; } return _xc_try_lzma_decode(dom, blob, size, &stream, "LZMA"); }
static int _lzma_LZMADecompressor___init___impl(Decompressor *self, int format, PyObject *memlimit, PyObject *filters) /*[clinic end generated code: output=9b119f6f2cc2d7a8 input=458ca6132ef29801]*/ { const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; uint64_t memlimit_ = UINT64_MAX; lzma_ret lzret; if (memlimit != Py_None) { if (format == FORMAT_RAW) { PyErr_SetString(PyExc_ValueError, "Cannot specify memory limit with FORMAT_RAW"); return -1; } memlimit_ = PyLong_AsUnsignedLongLong(memlimit); if (PyErr_Occurred()) return -1; } if (format == FORMAT_RAW && filters == Py_None) { PyErr_SetString(PyExc_ValueError, "Must specify filters for FORMAT_RAW"); return -1; } else if (format != FORMAT_RAW && filters != Py_None) { PyErr_SetString(PyExc_ValueError, "Cannot specify filters except with FORMAT_RAW"); return -1; } self->alloc.opaque = NULL; self->alloc.alloc = PyLzma_Malloc; self->alloc.free = PyLzma_Free; self->lzs.allocator = &self->alloc; #ifdef WITH_THREAD self->lock = PyThread_allocate_lock(); if (self->lock == NULL) { PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); return -1; } #endif self->check = LZMA_CHECK_UNKNOWN; self->unused_data = PyBytes_FromStringAndSize(NULL, 0); if (self->unused_data == NULL) goto error; switch (format) { case FORMAT_AUTO: lzret = lzma_auto_decoder(&self->lzs, memlimit_, decoder_flags); if (catch_lzma_error(lzret)) break; return 0; case FORMAT_XZ: lzret = lzma_stream_decoder(&self->lzs, memlimit_, decoder_flags); if (catch_lzma_error(lzret)) break; return 0; case FORMAT_ALONE: self->check = LZMA_CHECK_NONE; lzret = lzma_alone_decoder(&self->lzs, memlimit_); if (catch_lzma_error(lzret)) break; return 0; case FORMAT_RAW: self->check = LZMA_CHECK_NONE; if (Decompressor_init_raw(&self->lzs, filters) == -1) break; return 0; default: PyErr_Format(PyExc_ValueError, "Invalid container format: %d", format); break; } error: Py_CLEAR(self->unused_data); #ifdef WITH_THREAD PyThread_free_lock(self->lock); self->lock = NULL; #endif return -1; }
/* * Setup the callbacks. */ static int xz_lzma_bidder_init(struct archive_read_filter *self) { static const size_t out_block_size = 64 * 1024; void *out_block; struct private_data *state; int ret; state = (struct private_data *)calloc(sizeof(*state), 1); out_block = (unsigned char *)malloc(out_block_size); if (state == NULL || out_block == NULL) { tk_archive_set_error(&self->archive->archive, ENOMEM, "Can't allocate data for xz decompression"); free(out_block); free(state); return (ARCHIVE_FATAL); } self->data = state; state->out_block_size = out_block_size; state->out_block = out_block; self->read = xz_filter_read; self->skip = NULL; /* not supported */ self->close = xz_filter_close; state->stream.avail_in = 0; state->stream.next_out = state->out_block; state->stream.avail_out = state->out_block_size; /* Initialize compression library. * TODO: I don't know what value is best for memlimit. * maybe, it needs to check memory size which * running system has. */ if (self->code == ARCHIVE_COMPRESSION_XZ) ret = lzma_stream_decoder(&(state->stream), (1U << 30),/* memlimit */ LZMA_CONCATENATED); else ret = lzma_alone_decoder(&(state->stream), (1U << 30));/* memlimit */ if (ret == LZMA_OK) return (ARCHIVE_OK); /* Library setup failed: Choose an error message and clean up. */ switch (ret) { case LZMA_MEM_ERROR: tk_archive_set_error(&self->archive->archive, ENOMEM, "Internal error initializing compression library: " "Cannot allocate memory"); break; case LZMA_OPTIONS_ERROR: tk_archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC, "Internal error initializing compression library: " "Invalid or unsupported options"); break; default: tk_archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC, "Internal error initializing lzma library"); break; } free(state->out_block); free(state); self->data = NULL; return (ARCHIVE_FATAL); }
static int xc_try_lzma_decode( struct xc_dom_image *dom, void **blob, size_t *size) { lzma_stream stream = LZMA_STREAM_INIT; lzma_ret ret; lzma_action action = LZMA_RUN; unsigned char *out_buf; unsigned char *tmp_buf; int retval = -1; int outsize; const char *msg; ret = lzma_alone_decoder(&stream, physmem() / 3); if ( ret != LZMA_OK ) { xc_dom_printf("LZMA: Failed to init stream decoder\n"); return -1; } /* sigh. We don't know up-front how much memory we are going to need * for the output buffer. Allocate the output buffer to be equal * the input buffer to start, and we'll realloc as needed. */ outsize = dom->kernel_size; out_buf = malloc(outsize); if ( out_buf == NULL ) { xc_dom_printf("LZMA: Failed to alloc memory\n"); goto lzma_cleanup; } stream.next_in = dom->kernel_blob; stream.avail_in = dom->kernel_size; stream.next_out = out_buf; stream.avail_out = dom->kernel_size; for ( ; ; ) { ret = lzma_code(&stream, action); if ( (stream.avail_out == 0) || (ret != LZMA_OK) ) { tmp_buf = realloc(out_buf, outsize * 2); if ( tmp_buf == NULL ) { xc_dom_printf("LZMA: Failed to realloc memory\n"); free(out_buf); goto lzma_cleanup; } out_buf = tmp_buf; stream.next_out = out_buf + outsize; stream.avail_out = (outsize * 2) - outsize; outsize *= 2; } if ( ret != LZMA_OK ) { if ( ret == LZMA_STREAM_END ) { xc_dom_printf("LZMA: Saw data stream end\n"); retval = 0; break; } switch ( ret ) { case LZMA_MEM_ERROR: msg = strerror(ENOMEM); break; case LZMA_MEMLIMIT_ERROR: msg = "Memory usage limit reached"; break; case LZMA_FORMAT_ERROR: msg = "File format not recognized"; break; case LZMA_OPTIONS_ERROR: // FIXME: Better message? msg = "Unsupported compression options"; break; case LZMA_DATA_ERROR: msg = "File is corrupt"; break; case LZMA_BUF_ERROR: msg = "Unexpected end of input"; break; default: msg = "Internal program error (bug)"; break; } xc_dom_printf("%s: LZMA decompression error %s\n", __FUNCTION__, msg); break; } } xc_dom_printf("%s: LZMA decompress OK, 0x%zx -> 0x%zx\n", __FUNCTION__, *size, (size_t)stream.total_out); *blob = out_buf; *size = stream.total_out; lzma_cleanup: lzma_end(&stream); return retval; }
static inline int grep_refill(struct file *f) { ssize_t nr; if (filebehave == FILE_MMAP) return (0); bufpos = buffer; bufrem = 0; if (filebehave == FILE_GZIP) { nr = gzread(gzbufdesc, buffer, MAXBUFSIZ); #ifndef WITHOUT_BZIP2 } else if (filebehave == FILE_BZIP && bzbufdesc != NULL) { int bzerr; nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ); switch (bzerr) { case BZ_OK: case BZ_STREAM_END: /* No problem, nr will be okay */ break; case BZ_DATA_ERROR_MAGIC: /* * As opposed to gzread(), which simply returns the * plain file data, if it is not in the correct * compressed format, BZ2_bzRead() instead aborts. * * So, just restart at the beginning of the file again, * and use plain reads from now on. */ BZ2_bzReadClose(&bzerr, bzbufdesc); bzbufdesc = NULL; if (lseek(f->fd, 0, SEEK_SET) == -1) return (-1); nr = read(f->fd, buffer, MAXBUFSIZ); break; default: /* Make sure we exit with an error */ nr = -1; } #endif #ifndef WITHOUT_LZMA } else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) { lzma_action action = LZMA_RUN; uint8_t in_buf[MAXBUFSIZ]; lzma_ret ret; ret = (filebehave == FILE_XZ) ? lzma_stream_decoder(&lstrm, UINT64_MAX, LZMA_CONCATENATED) : lzma_alone_decoder(&lstrm, UINT64_MAX); if (ret != LZMA_OK) return (-1); lstrm.next_out = buffer; lstrm.avail_out = MAXBUFSIZ; lstrm.next_in = in_buf; nr = read(f->fd, in_buf, MAXBUFSIZ); if (nr < 0) return (-1); else if (nr == 0) action = LZMA_FINISH; lstrm.avail_in = nr; ret = lzma_code(&lstrm, action); if (ret != LZMA_OK && ret != LZMA_STREAM_END) return (-1); bufrem = MAXBUFSIZ - lstrm.avail_out; return (0); #endif /* WIHTOUT_LZMA */ } else nr = read(f->fd, buffer, MAXBUFSIZ); if (nr < 0) return (-1); bufrem = nr; return (0); }
/* * Opens a file for processing. */ struct file * grep_open(const char *path) { struct file *f; f = grep_malloc(sizeof *f); memset(f, 0, sizeof *f); if (path == NULL) { /* Processing stdin implies --line-buffered. */ lbflag = true; f->fd = STDIN_FILENO; } else if ((f->fd = open(path, O_RDONLY)) == -1) goto error1; if (filebehave == FILE_MMAP) { struct stat st; if ((fstat(f->fd, &st) == -1) || (st.st_size > OFF_MAX) || (!S_ISREG(st.st_mode))) filebehave = FILE_STDIO; else { int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC; #ifdef MAP_PREFAULT_READ flags |= MAP_PREFAULT_READ; #endif fsiz = st.st_size; buffer = mmap(NULL, fsiz, PROT_READ, flags, f->fd, (off_t)0); if (buffer == MAP_FAILED) filebehave = FILE_STDIO; else { bufrem = st.st_size; bufpos = buffer; madvise(buffer, st.st_size, MADV_SEQUENTIAL); } } } if ((buffer == NULL) || (buffer == MAP_FAILED)) buffer = grep_malloc(MAXBUFSIZ); if (filebehave == FILE_GZIP && (gzbufdesc = gzdopen(f->fd, "r")) == NULL) goto error2; #ifndef WITHOUT_BZIP2 if (filebehave == FILE_BZIP && (bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL) goto error2; #endif #ifndef WITHOUT_LZMA else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) { lzma_ret ret; ret = (filebehave == FILE_XZ) ? lzma_stream_decoder(&lstrm, UINT64_MAX, LZMA_CONCATENATED) : lzma_alone_decoder(&lstrm, UINT64_MAX); if (ret != LZMA_OK) goto error2; lstrm.avail_in = 0; lstrm.avail_out = MAXBUFSIZ; laction = LZMA_RUN; } #endif /* Fill read buffer, also catches errors early */ if (bufrem == 0 && grep_refill(f) != 0) goto error2; /* Check for binary stuff, if necessary */ if (binbehave != BINFILE_TEXT && memchr(bufpos, '\0', bufrem) != NULL) f->binary = true; return (f); error2: close(f->fd); error1: free(f); return (NULL); }