-
Notifications
You must be signed in to change notification settings - Fork 19
/
inflate.R
61 lines (57 loc) · 2.12 KB
/
inflate.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#' Uncompress a raw GZIP stream
#'
#' @param buffer Raw vector, containing the data to uncompress.
#' @param pos Start position of data to uncompress in `buffer`.
#' @param size Uncompressed size estimate, or `NULL`. If not given, or too
#' small, the output buffer is resized multiple times.
#' @return Named list with three entries:
#' - `output`: raw vector, the uncompressed data,
#' - `bytes_read`: number of bytes used from `buffer`,
#' - `bytes_written`: number of bytes written to the output buffer.
#'
#' @seealso [base::memDecompress()] does the same with `type = "gzip"`,
#' but it does not tell you the number of bytes read from the input.
#'
#' @export
#' @examples
#' data_gz <- deflate(charToRaw("Hello world!"))
#' inflate(data_gz$output)
inflate <- function(buffer, pos = 1L, size = NULL) {
stopifnot(
is.raw(buffer),
is_count(pos),
is.null(size) || is_count(size)
)
if (!is.null(size)) size <- as.integer(size)
.Call(c_R_inflate, buffer, as.integer(pos), size)
}
#' Compress a raw GZIP stream
#'
#' @param buffer Raw vector, containing the data to compress.
#' @param level Compression level, integer between 1 (fatest) and 9 (best).
#' @param pos Start position of data to compress in `buffer`.
#' @param size Compressed size estimate, or `NULL`. If not given, or too
#' small, the output buffer is resized multiple times.
#' @return Named list with three entries:
#' - `output`: raw vector, the compressed data,
#' - `bytes_read`: number of bytes used from `buffer`,
#' - `bytes_written`: number of bytes written to the output buffer.
#'
#' @seealso [base::memCompress()] does the same with `type = "gzip"`,
#' but it does not tell you the number of bytes read from the input.
#'
#' @export
#' @examples
#' data_gz <- deflate(charToRaw("Hello world!"))
#' inflate(data_gz$output)
deflate <- function(buffer, level = 6L, pos = 1L, size = NULL) {
stopifnot(
is.raw(buffer),
is_count(level),
level >= 1L && level <= 9L,
is_count(pos),
is.null(size) || is_count(size)
)
if (!is.null(size)) size <- as.integer(size)
.Call(c_R_deflate, buffer, as.integer(level), as.integer(pos), size)
}