~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/compression/uncompress.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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
using namespace drizzled;
 
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;
 
38
  uint32_t code;
 
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
  {
 
49
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
50
                        ER_ZLIB_Z_DATA_ERROR,
 
51
                        ER(ER_ZLIB_Z_DATA_ERROR));
 
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;
 
57
  if (new_size > current_session->variables.max_allowed_packet)
 
58
  {
 
59
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
60
                        ER_TOO_BIG_FOR_UNCOMPRESS,
 
61
                        ER(ER_TOO_BIG_FOR_UNCOMPRESS),
 
62
                        current_session->variables.max_allowed_packet);
 
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,
 
69
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
 
70
  {
 
71
    buffer.length((uint32_t) new_size);
 
72
    return &buffer;
 
73
  }
 
74
 
 
75
  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_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
 
78
 
 
79
err:
 
80
  null_value= 1;
 
81
  return 0;
 
82
}
 
83