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   ulong new_size;
00034   String *res;
00035   Byte *body;
00036   char *tmp, *last_char;
00037   assert(fixed == 1);
00038 
00039   if (!(res= args[0]->val_str(str)))
00040   {
00041     null_value= 1;
00042     return 0;
00043   }
00044   null_value= 0;
00045   if (res->empty()) return res;
00046 
00047   /*
00048     Citation from zlib.h (comment for compress function):
00049 
00050     Compresses the source buffer into the destination buffer.  sourceLen is
00051     the byte length of the source buffer. Upon entry, destLen is the total
00052     size of the destination buffer, which must be at least 0.1% larger than
00053     sourceLen plus 12 bytes.
00054     We assume here that the buffer can't grow more than .25 %.
00055   */
00056   new_size= res->length() + res->length() / 5 + 12;
00057 
00058   // Check new_size overflow: new_size <= res->length()
00059   if ((uint32_t) (new_size+5) <= res->length())
00060   {
00061     null_value= 1;
00062     return 0;
00063   }
00064   buffer.realloc((uint32_t) new_size + 4 + 1);
00065 
00066   body= ((Byte*)buffer.ptr()) + 4;
00067 
00068   // As far as we have checked res->empty() we can use ptr()
00069   int err= compress(body, &new_size, (const Bytef*)res->ptr(), res->length());
00070   if (err != Z_OK)
00071   {
00072     drizzled::error_t code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
00073     push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
00074     null_value= 1;
00075     return 0;
00076   }
00077 
00078   tmp= buffer.ptr(); // int4store is a macro; avoid side effects
00079   int4store(tmp, res->length() & 0x3FFFFFFF);
00080 
00081   /* This is to ensure that things works for CHAR fields, which trim ' ': */
00082   last_char= ((char*)body)+new_size-1;
00083   if (*last_char == ' ')
00084   {
00085     *++last_char= '.';
00086     new_size++;
00087   }
00088 
00089   buffer.length((uint32_t)new_size + 4);
00090   return &buffer;
00091 }