~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/compression/uncompress.cc

  • Committer: Monty Taylor
  • Date: 2009-04-25 20:29:54 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425202954-slopmkjj7vxal5lk
Migrated archive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
16
 
#define DRIZZLE_SERVER 1 /* for thd variable max_allowed_packet */
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
17
20
#include <drizzled/server_includes.h>
18
 
#include <drizzled/drizzled_error_messages.h>
 
21
#include <drizzled/session.h>
 
22
#include <drizzled/error.h>
 
23
#include <drizzled/function/str/strfunc.h>
 
24
#include "plugin/compression/uncompress.h"
19
25
 
20
26
#include <zlib.h>
 
27
#include <string>
21
28
 
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
 
};
 
29
using namespace std;
31
30
 
32
31
String *Item_func_uncompress::val_str(String *str)
33
32
{
46
45
  /* If length is less than 4 bytes, data is corrupt */
47
46
  if (res->length() <= 4)
48
47
  {
49
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
50
 
                        ER_ZLIB_Z_DATA_ERROR,
51
 
                        ER(ER_ZLIB_Z_DATA_ERROR));
 
48
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
49
                        ER_ZLIB_Z_DATA_ERROR,
 
50
                        ER(ER_ZLIB_Z_DATA_ERROR));
52
51
    goto err;
53
52
  }
54
53
 
55
54
  /* Size of uncompressed data is stored as first 4 bytes of field */
56
55
  new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
57
 
  if (new_size > current_thd->variables.max_allowed_packet)
 
56
  if (new_size > current_session->variables.max_allowed_packet)
58
57
  {
59
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
60
 
                        ER_TOO_BIG_FOR_UNCOMPRESS,
61
 
                        ER(ER_TOO_BIG_FOR_UNCOMPRESS),
62
 
                        current_thd->variables.max_allowed_packet);
 
58
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
59
                        ER_TOO_BIG_FOR_UNCOMPRESS,
 
60
                        ER(ER_TOO_BIG_FOR_UNCOMPRESS),
 
61
                        current_session->variables.max_allowed_packet);
63
62
    goto err;
64
63
  }
65
64
  if (buffer.realloc((uint32_t)new_size))
66
65
    goto err;
67
66
 
68
67
  if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
69
 
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
 
68
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
70
69
  {
71
70
    buffer.length((uint32_t) new_size);
72
71
    return &buffer;
73
72
  }
74
73
 
75
74
  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_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
 
75
         ((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR));
 
76
  push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
78
77
 
79
78
err:
80
79
  null_value= 1;
81
80
  return 0;
82
81
}
83
82
 
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;