~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
2409.3.1 by Olaf van der Spek
Add do_sha1()
30
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
31
971.3.10 by Eric Day
Added new convert file for hex function.
32
uint64_t drizzled_string_to_hex(char *to, const char *from, uint64_t from_size)
33
{
2409.3.1 by Olaf van der Spek
Add do_sha1()
34
  static const char* hex_map= "0123456789ABCDEF";
35
  
36
  for (const char *from_end= from + from_size; from != from_end; from++)
971.3.10 by Eric Day
Added new convert file for hex function.
37
  {
38
    *to++= hex_map[((unsigned char) *from) >> 4];
39
    *to++= hex_map[((unsigned char) *from) & 0xF];
40
  }
41
42
  *to= 0;
43
44
  return from_size * 2;
45
}
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
46
1337.4.4 by Eric Day
Fixed authentication plugin checks, added required functionality for protocol/auth plugins to specify password type and context.
47
static inline char hex_char_value(char c)
48
{
49
  if (c >= '0' && c <= '9')
50
    return c - '0';
51
  else if (c >= 'A' && c <= 'F')
52
    return c - 'A' + 10;
53
  else if (c >= 'a' && c <= 'f')
54
    return c - 'a' + 10;
55
  return 0;
56
}
57
58
void drizzled_hex_to_string(char *to, const char *from, uint64_t from_size)
59
{
60
  const char *from_end = from + from_size;
61
  while (from != from_end)
62
  {
63
    *to= hex_char_value(*from++) << 4;
64
    if (from != from_end)
65
      *to++|= hex_char_value(*from++);
66
  }
67
}
68
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
69
void bytesToHexdumpFormat(string &to, const unsigned char *from, size_t from_length)
70
{
2409.3.1 by Olaf van der Spek
Add do_sha1()
71
  static const char* hex_map= "0123456789abcdef";
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
72
  ostringstream line_number;
73
2409.3.1 by Olaf van der Spek
Add do_sha1()
74
  for (size_t x= 0; x < from_length; x+= 16)
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
75
  {
2409.3.1 by Olaf van der Spek
Add do_sha1()
76
    line_number << setfill('0') << setw(6) << x;
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
77
    to.append(line_number.str());
78
    to.append(": ", 2);
79
2409.3.1 by Olaf van der Spek
Add do_sha1()
80
    for (size_t y= 0; y < 16; y++)
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
81
    {
2409.3.1 by Olaf van der Spek
Add do_sha1()
82
      if (x + y < from_length)
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
83
      {
84
        to.push_back(hex_map[(from[x+y]) >> 4]);
85
        to.push_back(hex_map[(from[x+y]) & 0xF]);
86
        to.push_back(' ');
87
      }
88
      else
89
        to.append("   ");
90
    }
91
    to.push_back(' ');
2409.3.1 by Olaf van der Spek
Add do_sha1()
92
    for (size_t y= 0; y < 16; y++)
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
93
    {
2409.3.1 by Olaf van der Spek
Add do_sha1()
94
      if (x + y < from_length)
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
95
        to.push_back(isprint(from[x + y]) ? from[x + y] : '.');
96
    }
97
    to.push_back('\n');
98
    line_number.str("");
99
  }
100
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
101
102
} /* namespace drizzled */