~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/hex.cc

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

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/util/convert.h>
 
22
#include <drizzled/function/str/hex.h>
 
23
#include "drizzled/internal/m_string.h"
 
24
 
 
25
/**
 
26
  convert a hex digit into number.
 
27
*/
 
28
static int hexchar_to_int(char c)
 
29
{
 
30
  if (c <= '9' && c >= '0')
 
31
    return c-'0';
 
32
  c|=32;
 
33
  if (c <= 'f' && c >= 'a')
 
34
    return c-'a'+10;
 
35
  return -1;
 
36
}
 
37
 
 
38
String *Item_func_hex::val_str(String *str)
 
39
{
 
40
  String *res;
 
41
  assert(fixed == 1);
 
42
  if (args[0]->result_type() != STRING_RESULT)
 
43
  {
 
44
    uint64_t dec;
 
45
    char ans[65],*ptr;
 
46
    /* Return hex of unsigned int64_t value */
 
47
    if (args[0]->result_type() == REAL_RESULT ||
 
48
        args[0]->result_type() == DECIMAL_RESULT)
 
49
    {
 
50
      double val= args[0]->val_real();
 
51
      if ((val <= (double) INT64_MIN) ||
 
52
          (val >= (double) (uint64_t) UINT64_MAX))
 
53
        dec=  ~(int64_t) 0;
 
54
      else
 
55
        dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
 
56
    }
 
57
    else
 
58
      dec= (uint64_t) args[0]->val_int();
 
59
 
 
60
    if ((null_value= args[0]->null_value))
 
61
      return 0;
 
62
    ptr= int64_t2str(dec,ans,16);
 
63
    if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
 
64
      return &my_empty_string;                  // End of memory
 
65
    return str;
 
66
  }
 
67
 
 
68
  /* Convert given string to a hex string, character by character */
 
69
  res= args[0]->val_str(str);
 
70
  if (!res || tmp_value.alloc(res->length()*2+1))
 
71
  {
 
72
    null_value=1;
 
73
    return 0;
 
74
  }
 
75
  null_value=0;
 
76
  tmp_value.length(res->length()*2);
 
77
 
 
78
  (void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
 
79
                                res->length());
 
80
  return &tmp_value;
 
81
}
 
82
 
 
83
  /** Convert given hex string to a binary string. */
 
84
 
 
85
String *Item_func_unhex::val_str(String *str)
 
86
{
 
87
  const char *from, *end;
 
88
  char *to;
 
89
  String *res;
 
90
  uint32_t length;
 
91
  assert(fixed == 1);
 
92
 
 
93
  res= args[0]->val_str(str);
 
94
  if (!res || tmp_value.alloc(length= (1+res->length())/2))
 
95
  {
 
96
    null_value=1;
 
97
    return 0;
 
98
  }
 
99
 
 
100
  from= res->ptr();
 
101
  null_value= 0;
 
102
  tmp_value.length(length);
 
103
  to= (char*) tmp_value.ptr();
 
104
  if (res->length() % 2)
 
105
  {
 
106
    int hex_char;
 
107
    *to++= hex_char= hexchar_to_int(*from++);
 
108
    if ((null_value= (hex_char == -1)))
 
109
      return 0;
 
110
  }
 
111
  for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
 
112
  {
 
113
    int hex_char;
 
114
    *to= (hex_char= hexchar_to_int(from[0])) << 4;
 
115
    if ((null_value= (hex_char == -1)))
 
116
      return 0;
 
117
    *to|= hex_char= hexchar_to_int(from[1]);
 
118
    if ((null_value= (hex_char == -1)))
 
119
      return 0;
 
120
  }
 
121
  return &tmp_value;
 
122
}