~drizzle-trunk/drizzle/development

971.3.10 by Eric Day
Added new convert file for hex function.
1
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
971.3.10 by Eric Day
Added new convert file for hex function.
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; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1192.3.64 by Monty Taylor
Fixed some non-linux build issues.
22
971.3.10 by Eric Day
Added new convert file for hex function.
23
#include <drizzled/util/convert.h>
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
24
#include <string>
25
#include <iomanip>
26
#include <sstream>
27
28
using namespace std;
971.3.10 by Eric Day
Added new convert file for hex function.
29
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
30
namespace drizzled
31
{
32
971.3.10 by Eric Day
Added new convert file for hex function.
33
uint64_t drizzled_string_to_hex(char *to, const char *from, uint64_t from_size)
34
{
35
  static const char hex_map[]= "0123456789ABCDEF";
36
  const char *from_end;
37
38
  for (from_end= from + from_size; from != from_end; from++)
39
  {
40
    *to++= hex_map[((unsigned char) *from) >> 4];
41
    *to++= hex_map[((unsigned char) *from) & 0xF];
42
  }
43
44
  *to= 0;
45
46
  return from_size * 2;
47
}
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
48
1337.4.4 by Eric Day
Fixed authentication plugin checks, added required functionality for protocol/auth plugins to specify password type and context.
49
static inline char hex_char_value(char c)
50
{
51
  if (c >= '0' && c <= '9')
52
    return c - '0';
53
  else if (c >= 'A' && c <= 'F')
54
    return c - 'A' + 10;
55
  else if (c >= 'a' && c <= 'f')
56
    return c - 'a' + 10;
57
  return 0;
58
}
59
60
void drizzled_hex_to_string(char *to, const char *from, uint64_t from_size)
61
{
62
  const char *from_end = from + from_size;
63
64
  while (from != from_end)
65
  {
66
    *to= hex_char_value(*from++) << 4;
67
    if (from != from_end)
68
      *to++|= hex_char_value(*from++);
69
  }
70
}
71
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
72
void bytesToHexdumpFormat(string &to, const unsigned char *from, size_t from_length)
73
{
74
  static const char hex_map[]= "0123456789abcdef";
75
  unsigned int x, y;
76
  ostringstream line_number;
77
78
  for (x= 0; x < from_length; x+= 16)
79
  {
80
    line_number << setfill('0') << setw(6);
81
    line_number << x;
82
    to.append(line_number.str());
83
    to.append(": ", 2);
84
85
    for (y= 0; y < 16; y++)
86
    {
87
      if ((x + y) < from_length)
88
      {
89
        to.push_back(hex_map[(from[x+y]) >> 4]);
90
        to.push_back(hex_map[(from[x+y]) & 0xF]);
91
        to.push_back(' ');
92
      }
93
      else
94
        to.append("   ");
95
    }
96
    to.push_back(' ');
97
    for (y= 0; y < 16; y++)
98
    {
99
      if ((x + y) < from_length)
100
        to.push_back(isprint(from[x + y]) ? from[x + y] : '.');
101
    }
102
    to.push_back('\n');
103
    line_number.str("");
104
  }
105
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
106
107
} /* namespace drizzled */