Drizzled Public API Documentation

compress.cc

00001 /* Copyright (C) 2006 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 #include <config.h>
00017 #include <drizzled/plugin/function.h>
00018 #include <drizzled/item/func.h>
00019 #include <drizzled/function/str/strfunc.h>
00020 #include <drizzled/error.h>
00021 #include <drizzled/sql_error.h>
00022 #include <drizzled/current_session.h>
00023 #include <zlib.h>
00024 #include <plugin/compression/compress.h>
00025 
00026 #include <string>
00027 
00028 using namespace std;
00029 using namespace drizzled;
00030 
00031 String *Item_func_compress::val_str(String *str)
00032 {
00033   int err= Z_OK;
00034   drizzled::error_t code;
00035   ulong new_size;
00036   String *res;
00037   Byte *body;
00038   char *tmp, *last_char;
00039   assert(fixed == 1);
00040 
00041   if (!(res= args[0]->val_str(str)))
00042   {
00043     null_value= 1;
00044     return 0;
00045   }
00046   null_value= 0;
00047   if (res->is_empty()) return res;
00048 
00049   /*
00050     Citation from zlib.h (comment for compress function):
00051 
00052     Compresses the source buffer into the destination buffer.  sourceLen is
00053     the byte length of the source buffer. Upon entry, destLen is the total
00054     size of the destination buffer, which must be at least 0.1% larger than
00055     sourceLen plus 12 bytes.
00056     We assume here that the buffer can't grow more than .25 %.
00057   */
00058   new_size= res->length() + res->length() / 5 + 12;
00059 
00060   // Check new_size overflow: new_size <= res->length()
00061   if (((uint32_t) (new_size+5) <= res->length()) ||
00062       buffer.realloc((uint32_t) new_size + 4 + 1))
00063   {
00064     null_value= 1;
00065     return 0;
00066   }
00067 
00068   body= ((Byte*)buffer.ptr()) + 4;
00069 
00070   // As far as we have checked res->is_empty() we can use ptr()
00071   if ((err= compress(body, &new_size,
00072                      (const Bytef*)res->ptr(), res->length())) != Z_OK)
00073   {
00074     code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
00075     push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
00076                  code, ER(code));
00077     null_value= 1;
00078     return 0;
00079   }
00080 
00081   tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
00082   int4store(tmp, res->length() & 0x3FFFFFFF);
00083 
00084   /* This is to ensure that things works for CHAR fields, which trim ' ': */
00085   last_char= ((char*)body)+new_size-1;
00086   if (*last_char == ' ')
00087   {
00088     *++last_char= '.';
00089     new_size++;
00090   }
00091 
00092   buffer.length((uint32_t)new_size + 4);
00093   return &buffer;
00094 }
00095 
00096