cudbg_zlib.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include <linux/zlib.h>
  18. #include "cxgb4.h"
  19. #include "cudbg_if.h"
  20. #include "cudbg_lib_common.h"
  21. #include "cudbg_zlib.h"
  22. static int cudbg_get_compress_hdr(struct cudbg_buffer *pdbg_buff,
  23. struct cudbg_buffer *pin_buff)
  24. {
  25. if (pdbg_buff->offset + sizeof(struct cudbg_compress_hdr) >
  26. pdbg_buff->size)
  27. return CUDBG_STATUS_NO_MEM;
  28. pin_buff->data = (char *)pdbg_buff->data + pdbg_buff->offset;
  29. pin_buff->offset = 0;
  30. pin_buff->size = sizeof(struct cudbg_compress_hdr);
  31. pdbg_buff->offset += sizeof(struct cudbg_compress_hdr);
  32. return 0;
  33. }
  34. int cudbg_compress_buff(struct cudbg_init *pdbg_init,
  35. struct cudbg_buffer *pin_buff,
  36. struct cudbg_buffer *pout_buff)
  37. {
  38. struct cudbg_buffer temp_buff = { 0 };
  39. struct z_stream_s compress_stream;
  40. struct cudbg_compress_hdr *c_hdr;
  41. int rc;
  42. /* Write compression header to output buffer before compression */
  43. rc = cudbg_get_compress_hdr(pout_buff, &temp_buff);
  44. if (rc)
  45. return rc;
  46. c_hdr = (struct cudbg_compress_hdr *)temp_buff.data;
  47. c_hdr->compress_id = CUDBG_ZLIB_COMPRESS_ID;
  48. memset(&compress_stream, 0, sizeof(struct z_stream_s));
  49. compress_stream.workspace = pdbg_init->workspace;
  50. rc = zlib_deflateInit2(&compress_stream, Z_DEFAULT_COMPRESSION,
  51. Z_DEFLATED, CUDBG_ZLIB_WIN_BITS,
  52. CUDBG_ZLIB_MEM_LVL, Z_DEFAULT_STRATEGY);
  53. if (rc != Z_OK)
  54. return CUDBG_SYSTEM_ERROR;
  55. compress_stream.next_in = pin_buff->data;
  56. compress_stream.avail_in = pin_buff->size;
  57. compress_stream.next_out = pout_buff->data + pout_buff->offset;
  58. compress_stream.avail_out = pout_buff->size - pout_buff->offset;
  59. rc = zlib_deflate(&compress_stream, Z_FINISH);
  60. if (rc != Z_STREAM_END)
  61. return CUDBG_SYSTEM_ERROR;
  62. rc = zlib_deflateEnd(&compress_stream);
  63. if (rc != Z_OK)
  64. return CUDBG_SYSTEM_ERROR;
  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. return 0;
  69. }