2 * Copyright (C) 2018 Chelsio Communications. All rights reserved.
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.
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
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
18 #include <linux/zlib.h>
22 #include "cudbg_lib_common.h"
23 #include "cudbg_zlib.h"
25 static int cudbg_get_compress_hdr(struct cudbg_buffer *pdbg_buff,
26 struct cudbg_buffer *pin_buff)
28 if (pdbg_buff->offset + sizeof(struct cudbg_compress_hdr) >
30 return CUDBG_STATUS_NO_MEM;
32 pin_buff->data = (char *)pdbg_buff->data + pdbg_buff->offset;
34 pin_buff->size = sizeof(struct cudbg_compress_hdr);
35 pdbg_buff->offset += sizeof(struct cudbg_compress_hdr);
39 int cudbg_compress_buff(struct cudbg_init *pdbg_init,
40 struct cudbg_buffer *pin_buff,
41 struct cudbg_buffer *pout_buff)
43 struct cudbg_buffer temp_buff = { 0 };
44 struct z_stream_s compress_stream;
45 struct cudbg_compress_hdr *c_hdr;
48 /* Write compression header to output buffer before compression */
49 rc = cudbg_get_compress_hdr(pout_buff, &temp_buff);
53 c_hdr = (struct cudbg_compress_hdr *)temp_buff.data;
54 c_hdr->compress_id = CUDBG_ZLIB_COMPRESS_ID;
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);
62 return CUDBG_SYSTEM_ERROR;
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;
69 rc = zlib_deflate(&compress_stream, Z_FINISH);
70 if (rc != Z_STREAM_END)
71 return CUDBG_SYSTEM_ERROR;
73 rc = zlib_deflateEnd(&compress_stream);
75 return CUDBG_SYSTEM_ERROR;
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;