1
/* Copyright (C) 2006 MySQL AB
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.
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.
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 */
16
#define DRIZZLE_SERVER 1 /* for session variable max_allowed_packet */
17
#include <drizzled/server_includes.h>
18
#include <drizzled/error.h>
22
class Item_func_uncompress: public Item_str_func
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 *) ;
32
String *Item_func_uncompress::val_str(String *str)
35
String *res= args[0]->val_str(str);
46
/* If length is less than 4 bytes, data is corrupt */
47
if (res->length() <= 4)
49
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
51
ER(ER_ZLIB_Z_DATA_ERROR));
55
/* Size of uncompressed data is stored as first 4 bytes of field */
56
new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
57
if (new_size > current_session->variables.max_allowed_packet)
59
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
60
ER_TOO_BIG_FOR_UNCOMPRESS,
61
ER(ER_TOO_BIG_FOR_UNCOMPRESS),
62
current_session->variables.max_allowed_packet);
65
if (buffer.realloc((uint32_t)new_size))
68
if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
69
((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
71
buffer.length((uint32_t) new_size);
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));
77
push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
85
Item_func* create_uncompressudf_item(MEM_ROOT* m)
87
return new (m) Item_func_uncompress();
90
static struct udf_func uncompressudf = {
91
{ C_STRING_WITH_LEN("uncompress") },
92
create_uncompressudf_item
95
static int uncompressudf_plugin_init(void *p)
97
udf_func **f = (udf_func**) p;
104
static int uncompressudf_plugin_deinit(void *p)
106
udf_func *udff = (udf_func *) p;
111
mysql_declare_plugin(uncompress)
117
"UDF for compress()",
119
uncompressudf_plugin_init, /* Plugin Init */
120
uncompressudf_plugin_deinit, /* Plugin Deinit */
121
NULL, /* status variables */
122
NULL, /* system variables */
123
NULL /* config options */
125
mysql_declare_plugin_end;