/*
*  Naamari - an AES/SHA-based manual file crypto-synchronizer for cloud transfers
*  Copyright (c) 2015-2017 Sunkware
*  Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
*  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, either version 3 of the License, or
*  (at your option) any later version.
*
*  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, see <http://www.gnu.org/licenses/>.
*
*  ----------------------------------------------------------------
*  WWW:       sunkware.org
*  E-mail:    sunkware@gmail.com
*  ----------------------------------------------------------------
*/

#include "converter.h"

namespace cryptrav
{

namespace Converter
{


QString        hexstrFromBin(quint8* data, quint32 len)
{
 QString       Result = "";

 for (quint32 i = 0; i < len; i++)
  {
   Result.append(QString::number( (data[i] >> 4) & 0xF, 0x10 ));
   Result.append(QString::number( data[i] & 0xF, 0x10 ));
  }

 return Result;
}

inline quint8  binFromHexdigit(QChar chr)
{
 quint8        chrASCII = chr.toLatin1();

 if ( (chrASCII >= 0x30) && (chrASCII <= 0x39) ) // '0' -- '9'
  return (chrASCII - 0x30);
 else if ( (chrASCII >= 0x41) && (chrASCII <= 0x46) ) // 'A' -- 'F'
  return (chrASCII - 0x37);
 else if ( (chrASCII >= 0x61) && (chrASCII <= 0x66) ) // 'a' -- 'f'
  return (chrASCII - 0x57);

 return 0;
}

int            binFromHexstr(QString str, quint8* data, qint32 len)
{
 if (data == NULL)
  return CTERR_NULL_POINTER;


 if (len > str.length())
  len = str.length();

 for (qint32 i = 0; i < len; i++)
  data[i] = (binFromHexdigit(str.at(i << 1)) << 4) + binFromHexdigit(str.at((i << 1) + 1));


 return 0;
}


} // namespace "Converter"

} // namespace "cryptrav"
