1 /*
2 * Copyright (C) 2018 Chelsio Communications. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
15 *
16 */
17
18 #include <linux/zlib.h>
19
20 #include "cxgb4.h"
21 #include "cudbg_if.h"
22 #include "cudbg_lib_common.h"
23 #include "cudbg_zlib.h"
24
cudbg_get_compress_hdr(struct cudbg_buffer * pdbg_buff,struct cudbg_buffer * pin_buff)25 static int cudbg_get_compress_hdr(struct cudbg_buffer *pdbg_buff,
26 struct cudbg_buffer *pin_buff)
27 {
28 if (pdbg_buff->offset + sizeof(struct cudbg_compress_hdr) >
29 pdbg_buff->size)
30 return CUDBG_STATUS_NO_MEM;
31
32 pin_buff->data = (char *)pdbg_buff->data + pdbg_buff->offset;
33 pin_buff->offset = 0;
34 pin_buff->size = sizeof(struct cudbg_compress_hdr);
35 pdbg_buff->offset += sizeof(struct cudbg_compress_hdr);
36 return 0;
37 }
38
cudbg_compress_buff(struct cudbg_init * pdbg_init,struct cudbg_buffer * pin_buff,struct cudbg_buffer * pout_buff)39 int cudbg_compress_buff(struct cudbg_init *pdbg_init,
40 struct cudbg_buffer *pin_buff,
41 struct cudbg_buffer *pout_buff)
42 {
43 struct cudbg_buffer temp_buff = { 0 };
44 struct z_stream_s compress_stream;
45 struct cudbg_compress_hdr *c_hdr;
46 int rc;
47
48 /* Write compression header to output buffer before compression */
49 rc = cudbg_get_compress_hdr(pout_buff, &temp_buff);
50 if (rc)
51 return rc;
52
53 c_hdr = (struct cudbg_compress_hdr *)temp_buff.data;
54 c_hdr->compress_id = CUDBG_ZLIB_COMPRESS_ID;
55
56 memset(&compress_stream, 0, sizeof(struct z_stream_s));
57 compress_stream.workspace = pdbg_init->workspace;
58 rc = zlib_deflateInit2(&compress_stream, Z_DEFAULT_COMPRESSION,
59 Z_DEFLATED, CUDBG_ZLIB_WIN_BITS,
60 CUDBG_ZLIB_MEM_LVL, Z_DEFAULT_STRATEGY);
61 if (rc != Z_OK)
62 return CUDBG_SYSTEM_ERROR;
63
64 compress_stream.next_in = pin_buff->data;
65 compress_stream.avail_in = pin_buff->size;
66 compress_stream.next_out = pout_buff->data + pout_buff->offset;
67 compress_stream.avail_out = pout_buff->size - pout_buff->offset;
68
69 rc = zlib_deflate(&compress_stream, Z_FINISH);
70 if (rc != Z_STREAM_END)
71 return CUDBG_SYSTEM_ERROR;
72
73 rc = zlib_deflateEnd(&compress_stream);
74 if (rc != Z_OK)
75 return CUDBG_SYSTEM_ERROR;
76
77 c_hdr->compress_size = compress_stream.total_out;
78 c_hdr->decompress_size = pin_buff->size;
79 pout_buff->offset += compress_stream.total_out;
80
81 return 0;
82 }
83