~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
20
#include <drizzled/server_includes.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;
139.1.9 by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test
30
31
String *Item_func_uncompress::val_str(String *str)
32
{
33
  assert(fixed == 1);
34
  String *res= args[0]->val_str(str);
35
  ulong new_size;
36
  int err;
482 by Brian Aker
Remove uint.
37
  uint32_t code;
139.1.9 by Stewart Smith
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test
38
39
  if (!res)
40
    goto err;
41
  null_value= 0;
42
  if (res->is_empty())
43
    return res;
44
45
  /* If length is less than 4 bytes, data is corrupt */
46
  if (res->length() <= 4)
47
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
48
    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.
49
                        ER_ZLIB_Z_DATA_ERROR,
50
                        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
51
    goto err;
52
  }
53
54
  /* Size of uncompressed data is stored as first 4 bytes of field */
55
  new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
520.1.22 by Brian Aker
Second pass of thd cleanup
56
  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
57
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
58
    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.
59
                        ER_TOO_BIG_FOR_UNCOMPRESS,
60
                        ER(ER_TOO_BIG_FOR_UNCOMPRESS),
520.1.22 by Brian Aker
Second pass of thd cleanup
61
                        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
62
    goto err;
63
  }
64
  if (buffer.realloc((uint32_t)new_size))
65
    goto err;
66
67
  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.
68
                       ((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
69
  {
70
    buffer.length((uint32_t) new_size);
71
    return &buffer;
72
  }
73
74
  code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
942.1.12 by Monty Taylor
Converted udf_func into a factory.
75
         ((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
76
  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
77
78
err:
79
  null_value= 1;
80
  return 0;
81
}
82