139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
1 |
/* Copyright (C) 2006 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
|
15 |
||
1241.9.36
by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h. |
16 |
#include "config.h" |
1093.1.62
by Monty Taylor
Moved UDFs to slot organization. |
17 |
#include <drizzled/plugin/function.h> |
584.4.6
by Monty Taylor
Moved stuff into item/ |
18 |
#include <drizzled/item/func.h> |
670.1.20
by Monty Taylor
Renamed functions to function... everything else is singular. |
19 |
#include <drizzled/function/str/strfunc.h> |
549
by Monty Taylor
Took gettext.h out of header files. |
20 |
#include <drizzled/error.h> |
584.1.15
by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes. |
21 |
#include <drizzled/sql_error.h> |
670.2.1
by Monty Taylor
Moved pthread keys |
22 |
#include <drizzled/current_session.h> |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
23 |
#include <zlib.h> |
971.1.64
by Monty Taylor
Merged compress, uncompress and uncompressed_length into one plugin lib. Yay new plugin registration! |
24 |
#include "plugin/compression/compress.h" |
25 |
||
942.1.12
by Monty Taylor
Converted udf_func into a factory. |
26 |
#include <string> |
27 |
||
28 |
using namespace std; |
|
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
29 |
using namespace drizzled; |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
30 |
|
31 |
String *Item_func_compress::val_str(String *str) |
|
32 |
{
|
|
2087.3.1
by Brian Aker
Entire convert over to time_t. |
33 |
int err= Z_OK; |
34 |
drizzled::error_t code; |
|
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
35 |
ulong new_size; |
36 |
String *res; |
|
37 |
Byte *body; |
|
38 |
char *tmp, *last_char; |
|
39 |
assert(fixed == 1); |
|
40 |
||
41 |
if (!(res= args[0]->val_str(str))) |
|
42 |
{
|
|
43 |
null_value= 1; |
|
44 |
return 0; |
|
45 |
}
|
|
46 |
null_value= 0; |
|
47 |
if (res->is_empty()) return res; |
|
48 |
||
49 |
/*
|
|
50 |
Citation from zlib.h (comment for compress function):
|
|
51 |
||
52 |
Compresses the source buffer into the destination buffer. sourceLen is
|
|
53 |
the byte length of the source buffer. Upon entry, destLen is the total
|
|
54 |
size of the destination buffer, which must be at least 0.1% larger than
|
|
55 |
sourceLen plus 12 bytes.
|
|
56 |
We assume here that the buffer can't grow more than .25 %.
|
|
57 |
*/
|
|
58 |
new_size= res->length() + res->length() / 5 + 12; |
|
59 |
||
60 |
// Check new_size overflow: new_size <= res->length()
|
|
584.1.15
by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes. |
61 |
if (((uint32_t) (new_size+5) <= res->length()) || |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
62 |
buffer.realloc((uint32_t) new_size + 4 + 1)) |
63 |
{
|
|
64 |
null_value= 1; |
|
65 |
return 0; |
|
66 |
}
|
|
67 |
||
68 |
body= ((Byte*)buffer.ptr()) + 4; |
|
69 |
||
70 |
// As far as we have checked res->is_empty() we can use ptr()
|
|
71 |
if ((err= compress(body, &new_size, |
|
584.1.15
by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes. |
72 |
(const Bytef*)res->ptr(), res->length())) != Z_OK) |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
73 |
{
|
74 |
code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR; |
|
584.1.15
by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes. |
75 |
push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, |
76 |
code, ER(code)); |
|
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
77 |
null_value= 1; |
78 |
return 0; |
|
79 |
}
|
|
80 |
||
81 |
tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects |
|
82 |
int4store(tmp, res->length() & 0x3FFFFFFF); |
|
83 |
||
84 |
/* This is to ensure that things works for CHAR fields, which trim ' ': */
|
|
85 |
last_char= ((char*)body)+new_size-1; |
|
86 |
if (*last_char == ' ') |
|
87 |
{
|
|
88 |
*++last_char= '.'; |
|
89 |
new_size++; |
|
90 |
}
|
|
91 |
||
92 |
buffer.length((uint32_t)new_size + 4); |
|
93 |
return &buffer; |
|
94 |
}
|
|
95 |
||
96 |