my_compress.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. /**
  23. @file mysys/my_compress.cc
  24. */
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <zlib.h>
  28. #include <algorithm>
  29. #include "my_compiler.h"
  30. #include "my_dbug.h"
  31. #include "my_inttypes.h"
  32. #include "my_sys.h"
  33. #include "mysql/service_mysql_alloc.h"
  34. #include "mysys/mysys_priv.h"
  35. /*
  36. This replaces the packet with a compressed packet
  37. SYNOPSIS
  38. my_compress()
  39. packet Data to compress. This is is replaced with the compressed data.
  40. len Length of data to compress at 'packet'
  41. complen out: 0 if packet was not compressed
  42. RETURN
  43. 1 error. 'len' is not changed'
  44. 0 ok. In this case 'len' contains the size of the compressed packet
  45. */
  46. bool my_compress(uchar *packet, size_t *len, size_t *complen) {
  47. DBUG_ENTER("my_compress");
  48. if (*len < MIN_COMPRESS_LENGTH) {
  49. *complen = 0;
  50. DBUG_PRINT("note", ("Packet too short: Not compressed"));
  51. } else {
  52. uchar *compbuf = my_compress_alloc(packet, len, complen);
  53. if (!compbuf) DBUG_RETURN(*complen ? 0 : 1);
  54. memcpy(packet, compbuf, *len);
  55. my_free(compbuf);
  56. }
  57. DBUG_RETURN(0);
  58. }
  59. uchar *my_compress_alloc(const uchar *packet, size_t *len, size_t *complen) {
  60. uchar *compbuf;
  61. uLongf tmp_complen;
  62. int res;
  63. *complen = *len * 120 / 100 + 12;
  64. if (!(compbuf = (uchar *)my_malloc(key_memory_my_compress_alloc, *complen,
  65. MYF(MY_WME))))
  66. return 0; /* Not enough memory */
  67. tmp_complen = (uint)*complen;
  68. res = compress(compbuf, &tmp_complen, packet, static_cast<uLong>(*len));
  69. *complen = tmp_complen;
  70. if (res != Z_OK) {
  71. my_free(compbuf);
  72. return 0;
  73. }
  74. if (*complen >= *len) {
  75. *complen = 0;
  76. my_free(compbuf);
  77. DBUG_PRINT("note", ("Packet got longer on compression; Not compressed"));
  78. return 0;
  79. }
  80. /* Store length of compressed packet in *len */
  81. std::swap(*len, *complen);
  82. return compbuf;
  83. }
  84. /*
  85. Uncompress packet
  86. SYNOPSIS
  87. my_uncompress()
  88. packet Compressed data. This is is replaced with the orignal data.
  89. len Length of compressed data
  90. complen Length of the packet buffer (must be enough for the original
  91. data)
  92. RETURN
  93. 1 error
  94. 0 ok. In this case 'complen' contains the updated size of the
  95. real data.
  96. */
  97. bool my_uncompress(uchar *packet, size_t len, size_t *complen) {
  98. uLongf tmp_complen;
  99. DBUG_ENTER("my_uncompress");
  100. if (*complen) /* If compressed */
  101. {
  102. uchar *compbuf =
  103. (uchar *)my_malloc(key_memory_my_compress_alloc, *complen, MYF(MY_WME));
  104. int error;
  105. if (!compbuf) DBUG_RETURN(1); /* Not enough memory */
  106. tmp_complen = (uint)*complen;
  107. error = uncompress(compbuf, &tmp_complen, packet, (uLong)len);
  108. *complen = tmp_complen;
  109. if (error != Z_OK) { /* Probably wrong packet */
  110. DBUG_PRINT("error", ("Can't uncompress packet, error: %d", error));
  111. my_free(compbuf);
  112. DBUG_RETURN(1);
  113. }
  114. memcpy(packet, compbuf, *complen);
  115. my_free(compbuf);
  116. } else
  117. *complen = len;
  118. DBUG_RETURN(0);
  119. }