From 2ada69fe971ecc8881d5eb01dfbf337996472c40 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 1 Jul 2008 16:45:28 +0200 Subject: Initial import --- translate.cpp | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100755 translate.cpp (limited to 'translate.cpp') diff --git a/translate.cpp b/translate.cpp new file mode 100755 index 0000000..36a2f63 --- /dev/null +++ b/translate.cpp @@ -0,0 +1,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 + * + * 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& dst, const vector& src) +{ + // just copy the base into the byte vector + dst = vector(src.begin(),src.end()); +} +void Translate::CharToByte(vector& 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& dst, const vector& 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& src) +{ + dst = ""; + for(unsigned int i = 0; i < src.size(); i++) { + dst += TranslationTables::byteToCharMap[src[i]]; + } +} + +void Translate::HexToByte(vector& dst, const vector& 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& 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 &dst, const vector& 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 &dst, const vector& 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& dst, const vector& 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&src) +{ + dst = ""; + for(unsigned int i = 0; i < src.size(); i++) { + dst += TranslationTables::byteToHexMap[src[i]]; + } +} + +void Translate::ByteToOctal(vector&dst, const vector&src) +{ + dst.erase(dst.begin(),dst.end()); + dst.reserve(src.size()*3); + + vector::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&src) +{ + dst = ""; + vector::const_iterator itr; + for(itr = src.begin(); itr != src.end(); ++itr) { + dst += TranslationTables::byteToOctalMap[*itr]; + } +} + +void Translate::ByteToBinary(vector&dst, const vector&src) +{ + dst.erase(dst.begin(),dst.end()); + dst.reserve(src.size()*8); + + vector::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&src) +{ + dst = ""; + + vector::const_iterator itr; + for(itr = src.begin(); itr != src.end(); ++itr) { + dst += TranslationTables::byteToBinaryMap[*itr]; + } +} + -- cgit v1.2.3-54-g00ecf