summaryrefslogtreecommitdiff
path: root/translate.cpp
blob: 36a2f63e5022cf0b4a952669c321549d8c776333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* $Id: translate.cpp,v 1.4 2006/11/05 04:42:43 ganzhorn Exp $
 * This file is part of lfhex.
 * Copyright (C) 2006 Salem Ganzhorn <eyekode@yahoo.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "translate.hpp"

// translation maps from byte->hex, hex->byte, and byte->char
// mappings  are static arrays for speed
#include "mappings.h"

void Translate::CharToByte(vector<uchar>& dst, const vector<uchar>& src) 
{
  // just copy the base into the byte vector
  dst = vector<uchar>(src.begin(),src.end());
}
void Translate::CharToByte(vector<uchar>& dst, const QString& src) 
{
  // just copy the base into the byte vector
  dst.resize( src.length() );
  for(int i =0 ; i < src.length(); ++i)
    dst[i] = src[i].toAscii();
}


void Translate::ByteToChar(vector<uchar>& dst, const vector<uchar>& src)
{
  dst.erase(dst.begin(),dst.end());
  dst.reserve(src.size());
  for(unsigned int i = 0; i < src.size(); i++) {
    dst.push_back(TranslationTables::byteToCharMap[src[i]]);
  }
}

void Translate::ByteToChar(QString& dst, const vector<uchar>& src)
{
  dst = "";
  for(unsigned int i = 0; i < src.size(); i++) {
    dst += TranslationTables::byteToCharMap[src[i]];
  }
}

void Translate::HexToByte(vector<uchar>& dst, const vector<uchar>& src) 
{
  dst.erase(dst.begin(),dst.end());
  int start;

  if( src.size() % 2 ) {
    // if odd
    dst.reserve(src.size()/2+1);
    dst.push_back(TranslationTables::hexToByteMap[src[0]]);
    start = 1;
  } else {
    start = 0;
    dst.reserve(src.size()/2);
  }
  for(unsigned int i = start; i < src.size(); i+=2) {
    dst.push_back(TranslationTables::hexToByteMap[src[i]]*16 + 
		  TranslationTables::hexToByteMap[src[i+1]]);
  }
}

void Translate::HexToByte(vector<uchar>& dst, const QString& src) 
{
  dst.erase(dst.begin(),dst.end());
  dst.reserve(src.length()/2);
  int i = 0;
  if( src.length() % 2 ) {
      i = 1;
  }
  for(; i < src.length(); i+=2) {
    uchar hi = src[i].toAscii();
    uchar lo = src[i+1].toAscii();
    dst.push_back( TranslationTables::hexToByteMap[hi]*16 +
		   TranslationTables::hexToByteMap[lo] );
  }
}

void Translate::OctalToByte(vector<uchar> &dst, const vector<uchar>& src)
{
  dst.erase(dst.begin(),dst.end());
  dst.reserve(src.size()/3);
  for(unsigned int i = 0; i+2 < src.size(); i+=3) {
    dst.push_back(TranslationTables::hexToByteMap[src[i]]*64+
		  TranslationTables::hexToByteMap[src[i+1]]*8+
		  TranslationTables::hexToByteMap[src[i+2]]);
  }
}

void Translate::BinaryToByte(vector<uchar> &dst, const vector<uchar>& src)
{
  dst.erase(dst.begin(),dst.end());
  dst.reserve(src.size()/8);
  // FIXME: would be faster if we compared each src[i] with '1' before mult
  for(unsigned int i = 0; i < src.size(); i+=8) {
    dst.push_back(TranslationTables::hexToByteMap[src[i]]*128+
		  TranslationTables::hexToByteMap[src[i+1]]*64+
		  TranslationTables::hexToByteMap[src[i+2]]*32+
		  TranslationTables::hexToByteMap[src[i+3]]*16+
		  TranslationTables::hexToByteMap[src[i+4]]*8+
		  TranslationTables::hexToByteMap[src[i+5]]*4+
		  TranslationTables::hexToByteMap[src[i+6]]*2+
		  TranslationTables::hexToByteMap[src[i+7]]);
  }
}

const char * Translate::ByteToHex(uchar b)
{
  return TranslationTables::byteToHexMap[b];
}

void Translate::ByteToHex(vector<uchar>& dst, const vector<uchar>& src)
{
  const char * str;
  dst.erase(dst.begin(),dst.end());
  dst.reserve(src.size()*2);
  for(unsigned int i = 0; i < src.size(); i++) {
    str = TranslationTables::byteToHexMap[src[i]];
    dst.push_back(*str);
    dst.push_back(*(str+1));
  }
}

void Translate::ByteToHex(QString& dst, const vector<uchar>&src)
{
  dst = "";
  for(unsigned int i = 0; i < src.size(); i++) {
    dst += TranslationTables::byteToHexMap[src[i]];
  }
}

void Translate::ByteToOctal(vector<uchar>&dst, const vector<uchar>&src)
{
  dst.erase(dst.begin(),dst.end());
  dst.reserve(src.size()*3);

  vector<uchar>::const_iterator itr;
  const char * str;
  for(itr = src.begin(); itr != src.end(); ++itr) {
    str = TranslationTables::byteToOctalMap[*itr];
    dst.insert( dst.end(), str, str+3 );
  }
}

void Translate::ByteToOctal(QString&dst, const vector<uchar>&src)
{
  dst = "";
  vector<uchar>::const_iterator itr;
  for(itr = src.begin(); itr != src.end(); ++itr) {
    dst += TranslationTables::byteToOctalMap[*itr];
  }
}

void Translate::ByteToBinary(vector<uchar>&dst, const vector<uchar>&src)
{
  dst.erase(dst.begin(),dst.end());
  dst.reserve(src.size()*8);

  vector<uchar>::const_iterator itr;
  const char * str;
  for(itr = src.begin(); itr != src.end(); ++itr) {
    str = TranslationTables::byteToBinaryMap[*itr];
    dst.insert(dst.end(), str, str+8);
  }
}

void Translate::ByteToBinary(QString&dst, const vector<uchar>&src)
{
  dst = "";

  vector<uchar>::const_iterator itr;
  for(itr = src.begin(); itr != src.end(); ++itr) {
    dst += TranslationTables::byteToBinaryMap[*itr];
  }
}