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
#include <drizzled/common_includes.h>
17
#include <drizzled/item_func.h>
18
#include <drizzled/item_strfunc.h>
19
#include <drizzled/error.h>
22
class Item_func_compress: public Item_str_func
26
Item_func_compress():Item_str_func(){}
27
void fix_length_and_dec(){max_length= (args[0]->max_length*120)/100+12;}
28
const char *func_name() const{return "compress";}
29
String *val_str(String *) ;
32
String *Item_func_compress::val_str(String *str)
38
char *tmp, *last_char;
41
if (!(res= args[0]->val_str(str)))
47
if (res->is_empty()) return res;
50
Citation from zlib.h (comment for compress function):
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 %.
58
new_size= res->length() + res->length() / 5 + 12;
60
// Check new_size overflow: new_size <= res->length()
61
if (((uint32_t) (new_size+5) <= res->length()) ||
62
buffer.realloc((uint32_t) new_size + 4 + 1))
68
body= ((Byte*)buffer.ptr()) + 4;
70
// As far as we have checked res->is_empty() we can use ptr()
71
if ((err= compress(body, &new_size,
72
(const Bytef*)res->ptr(), res->length())) != Z_OK)
74
code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
75
push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
80
tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
81
int4store(tmp, res->length() & 0x3FFFFFFF);
83
/* This is to ensure that things works for CHAR fields, which trim ' ': */
84
last_char= ((char*)body)+new_size-1;
85
if (*last_char == ' ')
91
buffer.length((uint32_t)new_size + 4);
96
Item_func* create_compressudf_item(MEM_ROOT* m)
98
return new (m) Item_func_compress();
101
static struct udf_func compressudf = {
102
{ C_STRING_WITH_LEN("compress") },
103
create_compressudf_item
106
static int compressudf_plugin_init(void *p)
108
udf_func **f = (udf_func**) p;
115
static int compressudf_plugin_deinit(void *p)
117
udf_func *udff = (udf_func *) p;
122
mysql_declare_plugin(compress)
128
"UDF for compress()",
130
compressudf_plugin_init, /* Plugin Init */
131
compressudf_plugin_deinit, /* Plugin Deinit */
132
NULL, /* status variables */
133
NULL, /* system variables */
134
NULL /* config options */
136
mysql_declare_plugin_end;