1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* |
3 | * Copyright (C) 2018 Chelsio Communications. All rights reserved. |
4 | */ |
5 | |
6 | #include <linux/zlib.h> |
7 | |
8 | #include "cxgb4.h" |
9 | #include "cudbg_if.h" |
10 | #include "cudbg_lib_common.h" |
11 | #include "cudbg_zlib.h" |
12 | |
13 | static int cudbg_get_compress_hdr(struct cudbg_buffer *pdbg_buff, |
14 | struct cudbg_buffer *pin_buff) |
15 | { |
16 | if (pdbg_buff->offset + sizeof(struct cudbg_compress_hdr) > |
17 | pdbg_buff->size) |
18 | return CUDBG_STATUS_NO_MEM; |
19 | |
20 | pin_buff->data = (char *)pdbg_buff->data + pdbg_buff->offset; |
21 | pin_buff->offset = 0; |
22 | pin_buff->size = sizeof(struct cudbg_compress_hdr); |
23 | pdbg_buff->offset += sizeof(struct cudbg_compress_hdr); |
24 | return 0; |
25 | } |
26 | |
27 | int cudbg_compress_buff(struct cudbg_init *pdbg_init, |
28 | struct cudbg_buffer *pin_buff, |
29 | struct cudbg_buffer *pout_buff) |
30 | { |
31 | struct cudbg_buffer temp_buff = { 0 }; |
32 | struct z_stream_s compress_stream; |
33 | struct cudbg_compress_hdr *c_hdr; |
34 | int rc; |
35 | |
36 | /* Write compression header to output buffer before compression */ |
37 | rc = cudbg_get_compress_hdr(pdbg_buff: pout_buff, pin_buff: &temp_buff); |
38 | if (rc) |
39 | return rc; |
40 | |
41 | c_hdr = (struct cudbg_compress_hdr *)temp_buff.data; |
42 | c_hdr->compress_id = CUDBG_ZLIB_COMPRESS_ID; |
43 | |
44 | memset(&compress_stream, 0, sizeof(struct z_stream_s)); |
45 | compress_stream.workspace = pdbg_init->workspace; |
46 | rc = zlib_deflateInit2(strm: &compress_stream, Z_DEFAULT_COMPRESSION, |
47 | Z_DEFLATED, CUDBG_ZLIB_WIN_BITS, |
48 | CUDBG_ZLIB_MEM_LVL, Z_DEFAULT_STRATEGY); |
49 | if (rc != Z_OK) |
50 | return CUDBG_SYSTEM_ERROR; |
51 | |
52 | compress_stream.next_in = pin_buff->data; |
53 | compress_stream.avail_in = pin_buff->size; |
54 | compress_stream.next_out = pout_buff->data + pout_buff->offset; |
55 | compress_stream.avail_out = pout_buff->size - pout_buff->offset; |
56 | |
57 | rc = zlib_deflate(strm: &compress_stream, Z_FINISH); |
58 | if (rc != Z_STREAM_END) |
59 | return CUDBG_SYSTEM_ERROR; |
60 | |
61 | rc = zlib_deflateEnd(strm: &compress_stream); |
62 | if (rc != Z_OK) |
63 | return CUDBG_SYSTEM_ERROR; |
64 | |
65 | c_hdr->compress_size = compress_stream.total_out; |
66 | c_hdr->decompress_size = pin_buff->size; |
67 | pout_buff->offset += compress_stream.total_out; |
68 | |
69 | return 0; |
70 | } |
71 | |