~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/compression/compress.cc

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

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
#include "config.h"
 
17
#include <drizzled/plugin/function.h>
 
18
#include <drizzled/item/func.h>
 
19
#include <drizzled/function/str/strfunc.h>
 
20
#include <drizzled/error.h>
 
21
#include <drizzled/sql_error.h>
 
22
#include <drizzled/current_session.h>
 
23
#include <zlib.h>
 
24
#include "plugin/compression/compress.h"
 
25
 
 
26
#include <string>
 
27
 
 
28
using namespace std;
 
29
 
 
30
String *Item_func_compress::val_str(String *str)
 
31
{
 
32
  int err= Z_OK, code;
 
33
  ulong new_size;
 
34
  String *res;
 
35
  Byte *body;
 
36
  char *tmp, *last_char;
 
37
  assert(fixed == 1);
 
38
 
 
39
  if (!(res= args[0]->val_str(str)))
 
40
  {
 
41
    null_value= 1;
 
42
    return 0;
 
43
  }
 
44
  null_value= 0;
 
45
  if (res->is_empty()) return res;
 
46
 
 
47
  /*
 
48
    Citation from zlib.h (comment for compress function):
 
49
 
 
50
    Compresses the source buffer into the destination buffer.  sourceLen is
 
51
    the byte length of the source buffer. Upon entry, destLen is the total
 
52
    size of the destination buffer, which must be at least 0.1% larger than
 
53
    sourceLen plus 12 bytes.
 
54
    We assume here that the buffer can't grow more than .25 %.
 
55
  */
 
56
  new_size= res->length() + res->length() / 5 + 12;
 
57
 
 
58
  // Check new_size overflow: new_size <= res->length()
 
59
  if (((uint32_t) (new_size+5) <= res->length()) ||
 
60
      buffer.realloc((uint32_t) new_size + 4 + 1))
 
61
  {
 
62
    null_value= 1;
 
63
    return 0;
 
64
  }
 
65
 
 
66
  body= ((Byte*)buffer.ptr()) + 4;
 
67
 
 
68
  // As far as we have checked res->is_empty() we can use ptr()
 
69
  if ((err= compress(body, &new_size,
 
70
                     (const Bytef*)res->ptr(), res->length())) != Z_OK)
 
71
  {
 
72
    code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
 
73
    push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
74
                 code, ER(code));
 
75
    null_value= 1;
 
76
    return 0;
 
77
  }
 
78
 
 
79
  tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
 
80
  int4store(tmp, res->length() & 0x3FFFFFFF);
 
81
 
 
82
  /* This is to ensure that things works for CHAR fields, which trim ' ': */
 
83
  last_char= ((char*)body)+new_size-1;
 
84
  if (*last_char == ' ')
 
85
  {
 
86
    *++last_char= '.';
 
87
    new_size++;
 
88
  }
 
89
 
 
90
  buffer.length((uint32_t)new_size + 4);
 
91
  return &buffer;
 
92
}
 
93
 
 
94