00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <drizzled/util/convert.h>
00024 #include <string>
00025 #include <iomanip>
00026 #include <sstream>
00027
00028 using namespace std;
00029
00030 namespace drizzled {
00031
00032 uint64_t drizzled_string_to_hex(char *to, const char *from, uint64_t from_size)
00033 {
00034 static const char* hex_map= "0123456789ABCDEF";
00035
00036 for (const char *from_end= from + from_size; from != from_end; from++)
00037 {
00038 *to++= hex_map[((unsigned char) *from) >> 4];
00039 *to++= hex_map[((unsigned char) *from) & 0xF];
00040 }
00041
00042 *to= 0;
00043
00044 return from_size * 2;
00045 }
00046
00047 static inline char hex_char_value(char c)
00048 {
00049 if (c >= '0' && c <= '9')
00050 return c - '0';
00051 else if (c >= 'A' && c <= 'F')
00052 return c - 'A' + 10;
00053 else if (c >= 'a' && c <= 'f')
00054 return c - 'a' + 10;
00055 return 0;
00056 }
00057
00058 void drizzled_hex_to_string(char *to, const char *from, uint64_t from_size)
00059 {
00060 const char *from_end = from + from_size;
00061 while (from != from_end)
00062 {
00063 *to= hex_char_value(*from++) << 4;
00064 if (from != from_end)
00065 *to++|= hex_char_value(*from++);
00066 }
00067 }
00068
00069 void bytesToHexdumpFormat(string &to, const unsigned char *from, size_t from_length)
00070 {
00071 static const char* hex_map= "0123456789abcdef";
00072 ostringstream line_number;
00073
00074 for (size_t x= 0; x < from_length; x+= 16)
00075 {
00076 line_number << setfill('0') << setw(6) << x;
00077 to.append(line_number.str());
00078 to.append(": ", 2);
00079
00080 for (size_t y= 0; y < 16; y++)
00081 {
00082 if (x + y < from_length)
00083 {
00084 to.push_back(hex_map[(from[x+y]) >> 4]);
00085 to.push_back(hex_map[(from[x+y]) & 0xF]);
00086 to.push_back(' ');
00087 }
00088 else
00089 to.append(" ");
00090 }
00091 to.push_back(' ');
00092 for (size_t y= 0; y < 16; y++)
00093 {
00094 if (x + y < from_length)
00095 to.push_back(isprint(from[x + y]) ? from[x + y] : '.');
00096 }
00097 to.push_back('\n');
00098 line_number.str("");
00099 }
00100 }
00101
00102 }