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 |
||
520.1.22
by Brian Aker
Second pass of thd cleanup |
16 |
#define DRIZZLE_SERVER 1 /* for session variable max_allowed_packet */ |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
17 |
#include <drizzled/server_includes.h> |
18 |
#include <drizzled/drizzled_error_messages.h> |
|
19 |
||
20 |
#include <zlib.h> |
|
21 |
||
22 |
class Item_func_uncompress: public Item_str_func |
|
23 |
{
|
|
24 |
String buffer; |
|
25 |
public: |
|
26 |
Item_func_uncompress(): Item_str_func(){} |
|
27 |
void fix_length_and_dec(){ maybe_null= 1; max_length= MAX_BLOB_WIDTH; } |
|
28 |
const char *func_name() const{return "uncompress";} |
|
29 |
String *val_str(String *) ; |
|
30 |
};
|
|
31 |
||
32 |
String *Item_func_uncompress::val_str(String *str) |
|
33 |
{
|
|
34 |
assert(fixed == 1); |
|
35 |
String *res= args[0]->val_str(str); |
|
36 |
ulong new_size; |
|
37 |
int err; |
|
482
by Brian Aker
Remove uint. |
38 |
uint32_t code; |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
39 |
|
40 |
if (!res) |
|
41 |
goto err; |
|
42 |
null_value= 0; |
|
43 |
if (res->is_empty()) |
|
44 |
return res; |
|
45 |
||
46 |
/* If length is less than 4 bytes, data is corrupt */
|
|
47 |
if (res->length() <= 4) |
|
48 |
{
|
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
49 |
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
50 |
ER_ZLIB_Z_DATA_ERROR, |
51 |
ER(ER_ZLIB_Z_DATA_ERROR)); |
|
52 |
goto err; |
|
53 |
}
|
|
54 |
||
55 |
/* Size of uncompressed data is stored as first 4 bytes of field */
|
|
56 |
new_size= uint4korr(res->ptr()) & 0x3FFFFFFF; |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
57 |
if (new_size > current_session->variables.max_allowed_packet) |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
58 |
{
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
59 |
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
60 |
ER_TOO_BIG_FOR_UNCOMPRESS, |
61 |
ER(ER_TOO_BIG_FOR_UNCOMPRESS), |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
62 |
current_session->variables.max_allowed_packet); |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
63 |
goto err; |
64 |
}
|
|
65 |
if (buffer.realloc((uint32_t)new_size)) |
|
66 |
goto err; |
|
67 |
||
68 |
if ((err= uncompress((Byte*)buffer.ptr(), &new_size, |
|
69 |
((const Bytef*)res->ptr())+4,res->length())) == Z_OK) |
|
70 |
{
|
|
71 |
buffer.length((uint32_t) new_size); |
|
72 |
return &buffer; |
|
73 |
}
|
|
74 |
||
75 |
code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR : |
|
76 |
((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR)); |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
77 |
push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code)); |
139.1.9
by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test |
78 |
|
79 |
err: |
|
80 |
null_value= 1; |
|
81 |
return 0; |
|
82 |
}
|
|
83 |
||
84 |
||
85 |
Item_func* create_uncompressudf_item(MEM_ROOT* m) |
|
86 |
{
|
|
87 |
return new (m) Item_func_uncompress(); |
|
88 |
}
|
|
89 |
||
90 |
static struct udf_func uncompressudf = { |
|
91 |
{ C_STRING_WITH_LEN("uncompress") }, |
|
92 |
create_uncompressudf_item
|
|
93 |
};
|
|
94 |
||
95 |
static int uncompressudf_plugin_init(void *p) |
|
96 |
{
|
|
97 |
udf_func **f = (udf_func**) p; |
|
98 |
||
99 |
*f= &uncompressudf; |
|
100 |
||
101 |
return 0; |
|
102 |
}
|
|
103 |
||
104 |
static int uncompressudf_plugin_deinit(void *p) |
|
105 |
{
|
|
106 |
udf_func *udff = (udf_func *) p; |
|
107 |
(void)udff; |
|
108 |
return 0; |
|
109 |
}
|
|
110 |
||
111 |
mysql_declare_plugin(uncompress) |
|
112 |
{
|
|
113 |
DRIZZLE_UDF_PLUGIN, |
|
114 |
"uncompress", |
|
115 |
"1.0", |
|
116 |
"Stewart Smith", |
|
117 |
"UDF for compress()", |
|
118 |
PLUGIN_LICENSE_GPL, |
|
119 |
uncompressudf_plugin_init, /* Plugin Init */ |
|
120 |
uncompressudf_plugin_deinit, /* Plugin Deinit */ |
|
121 |
NULL, /* status variables */ |
|
122 |
NULL, /* system variables */ |
|
123 |
NULL /* config options */ |
|
124 |
}
|
|
125 |
mysql_declare_plugin_end; |