~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2010-12-08 22:35:56 UTC
  • mfrom: (1819.9.158 update-innobase)
  • Revision ID: brian@tangent.org-20101208223556-37mi4omqg7lkjzf3
Merge in Stewart's changes, 1.3 changes.

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