~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2008-11-13 02:56:15 UTC
  • mfrom: (575.4.10 devel)
  • Revision ID: brian@tangent.org-20081113025615-snhsi52yb2ivmx6f
Merging Monty's code.

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