~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/compression/uncompress.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
20
 
#include "config.h"
21
 
#include <drizzled/session.h>
22
 
#include <drizzled/error.h>
23
 
#include <drizzled/function/str/strfunc.h>
24
 
#include "plugin/compression/uncompress.h"
25
 
 
26
 
#include <zlib.h>
27
 
#include <string>
28
 
 
29
 
using namespace std;
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;
37
 
  uint32_t code;
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
 
  {
48
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
49
 
                        ER_ZLIB_Z_DATA_ERROR,
50
 
                        ER(ER_ZLIB_Z_DATA_ERROR));
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;
56
 
  if (new_size > current_session->variables.max_allowed_packet)
57
 
  {
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);
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,
68
 
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
69
 
  {
70
 
    buffer.length((uint32_t) new_size);
71
 
    return &buffer;
72
 
  }
73
 
 
74
 
  code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
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));
77
 
 
78
 
err:
79
 
  null_value= 1;
80
 
  return 0;
81
 
}
82