~drizzle-trunk/drizzle/development

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