~drizzle-trunk/drizzle/development

971.1.64 by Monty Taylor
Merged compress, uncompress and uncompressed_length into one plugin lib. Yay new plugin registration!
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
 */
139.1.9 by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test
19
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
21
#include <drizzled/session.h>
549 by Monty Taylor
Took gettext.h out of header files.
22
#include <drizzled/error.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
23
#include <drizzled/function/str/strfunc.h>
971.1.64 by Monty Taylor
Merged compress, uncompress and uncompressed_length into one plugin lib. Yay new plugin registration!
24
#include "plugin/compression/uncompress.h"
139.1.9 by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test
25
26
#include <zlib.h>
942.1.12 by Monty Taylor
Converted udf_func into a factory.
27
#include <string>
28
29
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
30
using namespace drizzled;
139.1.9 by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test
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,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
50
                        ER_ZLIB_Z_DATA_ERROR,
51
                        ER(ER_ZLIB_Z_DATA_ERROR));
139.1.9 by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test
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,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
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,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
69
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
139.1.9 by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test
70
  {
71
    buffer.length((uint32_t) new_size);
72
    return &buffer;
73
  }
74
75
  code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
942.1.12 by Monty Taylor
Converted udf_func into a factory.
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