Открытые статические члены

Класс nitro::Encoders

Класс для работы с алгоритмами кодирования/раскодирования. Подробнее...

#include <utilities/string_utilities.h>

Полный список членов класса

Открытые статические члены

static void XOREncode (char *OutputBuffer, std::size_t BufferSize, char *CodeWord)
static void XORDecode (char *InputBuffer, std::size_t BufferSize, char *CodeWord)
static void EncodeBase64 (const char *bf, std::size_t count, char *outbf)
template<class str_type >
static str_type EncodeBase64 (const char *bf, std::size_t count)
static void DecodeBase64 (const char *InBuf, char *OutBuf, std::size_t &Count)
template<class str_type >
static str_type DecodeBase64 (const char *bf)
static void md5 (const char *Data, std::size_t DataLength, char *Hash)
template<class str_type >
static void md5 (const char *Data, std::size_t DataLength)

Подробное описание

Класс для работы с алгоритмами кодирования/раскодирования.

Автор:
Додонов А.А.

См. определение в файле string_utilities.h строка 740


Методы

void nitro::Encoders::DecodeBase64 ( const char *  InBuf,
char *  OutBuf,
std::size_t &  Count 
) [static]

Функция для раcкодирования из base64.

Аргументы:
InBuf - Буфер, который будем раскодировать.
OutBuf - Буфер куда будет помещен результат раcкодирования.
Count - Размер выходного буффера
Исключения:
nitro::exception Кидает исключение с описанием ошибки.
Автор:
Додонов А.А.

См. определение в файле string_utilities.cpp строка 341

Перекрестные ссылки nitro::exception::code() и nitro::exception::what().

Используется в DecodeBase64().

        {
                try
                {
                        std::string             EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

                        char                    a , b , c , d;
                        char                    x , y , z;
                        int                             t = 0;

                        if( std::string( InBuf ) == "" )
                        {
                                Count = 0;
                                return;
                        }

                        if( strlen( InBuf ) % 4 != 0 )
                        {
                                throw( std::string( "Illegal input string" ) );
                        }

                        for( std::size_t i( 0 ) ; i < strlen( InBuf ) / 4 ; i++ )
                        {
                                std::size_t                     r;

                                r = EncodeTable.find( InBuf[ 4 * i + 0 ] , 0 );
                                a = ( r == std::string::npos ? -1 : ( char ) r );

                                r = EncodeTable.find( InBuf[ 4 * i + 1 ] , 0 );
                                b = ( r == std::string::npos ? -1 : ( char ) r );

                                r = EncodeTable.find( InBuf[ 4 * i + 2 ] , 0 );
                                c = ( r == std::string::npos ? -1 : ( char ) r );

                                r = EncodeTable.find( InBuf[ 4 * i + 3 ] , 0 );
                                d = ( r == std::string::npos ? -1 : ( char ) r );

                                if( ( a == -1 ) || ( b == -1 ) || ( c == -1 ) || ( d == -1 ) || ( a == 64 ) || ( b == 64 ) )
                                {
                                        throw( std::string( "An error occured while decoding" ) );
                                }
                                if( c == 64 )
                                {
                                        x = ( a << 2 ) | ( b >> 4 );
                                        OutBuf[ t++ ] = x;
                                }
                                else if( d == 64 )
                                {
                                        x = ( a << 2 ) | ( b >> 4 );
                                        y = ( b << 4 ) | ( c >> 2 );
                                        OutBuf[ t++ ] = x;
                                        OutBuf[ t++ ] = y;
                                }
                                else
                                {
                                        x = ( a << 2 ) | ( b >> 4 );
                                        y = ( b << 4 ) | ( c >> 2 );
                                        z = ( c << 6 ) | d;
                                        OutBuf[ t++ ] = x;
                                        OutBuf[ t++ ] = y;
                                        OutBuf[ t++ ] = z;
                                }
                        }

                        Count = t;
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * InBuf , char * OutBuf , std::size_t & Count )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * InBuf , char * OutBuf , std::size_t & Count )::An error occured while encoding string to base64" ) , 1 ) );
                }
        }

Граф вызовов:

template<class str_type >
str_type nitro::Encoders::DecodeBase64 ( const char *  bf  )  [static]

Функция для раcкодирования из base64.

Аргументы:
bf - Буфер, который будем раскодировать.
Исключения:
nitro::exception Кидает исключение с описанием ошибки.
Автор:
Додонов А.А.

См. определение в файле string_utilities.h строка 987

Перекрестные ссылки nitro::exception::code(), DecodeBase64() и nitro::exception::what().

        {
                try
                {
                        str_type                        out;
                        //Pointer< char >               OutBuffer( strlen( bf ) );
                        char *                          OutBuffer( new char[ strlen( bf ) ] );

                        memset( OutBuffer , 0 , strlen( bf ) );

                        std::size_t             Count;

                        DecodeBase64( bf , OutBuffer , Count );

                        out.AppendData( OutBuffer , Count );

                        delete [] OutBuffer;

                        return( out );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * bf )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * bf )::An error occured" ) , 0 ) );
                }
        }

Граф вызовов:

void nitro::Encoders::EncodeBase64 ( const char *  bf,
std::size_t  count,
char *  outbf 
) [static]

Функция для кодирования в base64.

Аргументы:
bf - Кодируемый буффер.
count - Размер кодируемого буффера.
outbf - Перекодированная строка.
Исключения:
nitro::exception Кидает исключение с описанием ошибки.
Автор:
Додонов А.А.

См. определение в файле string_utilities.cpp строка 278

Перекрестные ссылки nitro::exception::code() и nitro::exception::what().

Используется в EncodeBase64().

        {
                try
                {
                        std::string                     EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
                        std::string                     Sout = "";
                        unsigned char           a , b , c , i;
                        
                        for( std::size_t k( 0 ) ; k < count ; k++ )
                        {
                                if( count - k == 1 )
                                {
                                        a = bf[ k ];
                                        b = 0;
                                        i = a >> 2;
                                        Sout += EncodeTable.substr( i , 1 );
                                        i = ( ( a & 3 ) << 4 ) | ( b >> 4 );
                                        Sout += EncodeTable.substr( i , 1 );
                                        Sout += EncodeTable.substr( 64 , 1 );
                                        Sout += EncodeTable.substr( 64 , 1 );
                                }
                                else if( count - k == 2 )
                                {
                                        a = bf[ k ];
                                        b = bf[ ++k ];
                                        c = 0;
                                        i = a >> 2;
                                        Sout += EncodeTable.substr( i , 1 );
                                        i = ( ( a & 3 ) << 4 ) | ( b >> 4 );
                                        Sout += EncodeTable.substr( i , 1 );
                                        i = ( ( b & 15 ) << 2 ) | ( c >> 6 );
                                        Sout += EncodeTable.substr( i , 1 );
                                        Sout += EncodeTable.substr( 64 , 1 );
                                }
                                else
                                {
                                        a = bf[ k ];
                                        b = bf[ ++k ];
                                        c = bf[ ++k ];
                                
                                        i = a >> 2;
                                        Sout += EncodeTable.substr( i , 1 );
                                        i = ( ( a & 3 ) << 4 ) | ( b >> 4 );
                                        Sout += EncodeTable.substr( i , 1 );
                                        i = ( ( b & 15 ) << 2 ) | ( c >> 6 );
                                        Sout += EncodeTable.substr( i , 1 );
                                        i = c & 63;
                                        Sout += EncodeTable.substr( i , 1 );
                                }
                        }
                        
                        strcpy( outbf  , Sout.c_str() );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count , char * outbf )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count , char * outbf )::An error occured while encoding string to base64" ) , 1 ) );
                }
        }

Граф вызовов:

template<class str_type >
str_type nitro::Encoders::EncodeBase64 ( const char *  bf,
std::size_t  count 
) [static]

Функция для кодирования в base64.

Аргументы:
bf - Кодируемый буффер.
count - Размер кодируемого буффера.
Возвращает:
Перекодированная строка.
Исключения:
nitro::exception Кидает исключение с описанием ошибки.
Автор:
Додонов А.А.

См. определение в файле string_utilities.h строка 960

Перекрестные ссылки nitro::exception::code(), EncodeBase64() и nitro::exception::what().

        {
                try
                {
                        str_type                        out;
                        //Pointer< char >               OutBuffer( ( std::size_t )( count * 1.5 ) );
                        char *                                  OutBuffer( new char[ ( std::size_t )( count * 1.5 ) ] );
                        memset( OutBuffer , 0 , ( std::size_t )( count * 1.5 ) );

                        EncodeBase64( bf , count , OutBuffer );

                        out.AppendData( OutBuffer , ( std::size_t )( count * 1.5 ) );

                        delete [] OutBuffer;

                        return( out );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count )::An error occured" ) , 0 ) );
                }
        }

Граф вызовов:

template<class str_type >
void nitro::Encoders::md5 ( const char *  Data,
std::size_t  DataLength 
) [static]

Функция вычисления md5 хэша.

Аргументы:
Data - Данные для которых ищется хэш.
DataLength - Размер данных.
Возвращает:
Хэш (размер буффера должен быть как минимум 33 байта).
Исключения:
nitro::exception Кидает исключение с описанием ошибки.
Автор:
Додонов А.А.

См. определение в файле string_utilities.h строка 1017

Перекрестные ссылки nitro::exception::code(), md5() и nitro::exception::what().

        {
                try
                {
                        char                            Hash[ 33 ];
                        md5( Data , DataLength , Hash );
                        return( str_type( Hash ) );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength )::An error occured" ) , 0 ) );
                }
        }

Граф вызовов:

void nitro::Encoders::md5 ( const char *  Data,
std::size_t  DataLength,
char *  Hash 
) [static]

Функция вычисления md5 хэша.

Аргументы:
Data - Данные для которых ищется хэш.
DataLength - Размер данных.
Hash - Хэш (размер буффера должен быть как минимум 33 байта).
Исключения:
nitro::exception Кидает исключение с описанием ошибки.
Автор:
Додонов А.А.

См. определение в файле string_utilities.cpp строка 726

Перекрестные ссылки nitro::exception::code(), nitro::MDFinal(), nitro::MDInit(), nitro::MDUpdate() и nitro::exception::what().

Используется в md5().

        {
                try
                {
                        MD5_CTX         Context;
                        UC                      Digest[ 16 ];

                        MDInit( & Context );
                        MDUpdate( & Context , ( UC * )Data , DataLength );
                        MDFinal( Digest , & Context );

                        // Формируем возвращаемую строку
                        memset( Hash , 0 , 32 );

                        for( std::size_t i( 0 ) ; i < 16 ; i++ )
                        {
                                sprintf( & Hash[ i * 2 ] , "%02x" , Digest[ i ] );
                        }

                        Hash[ 32 ] = '\0';
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength , char * Hash )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength , char * Hash )::An error occured while encoding string to base64" ) , 1 ) );
                }
        }

Граф вызовов:

void nitro::Encoders::XORDecode ( char *  InputBuffer,
std::size_t  BufferSize,
char *  CodeWord 
) [static]

Функция декодирования ксором строки.

Аргументы:
InputBuffer - Буффер, который будет раскодирован.
BufferSize - Размер раскодируемого буффера.
CodeWord - Слово, которым будем раскодировать.
Исключения:
nitro::exception Кидает исключение с описанием ошибки.
Автор:
Додонов А.А. Function decodes string from XORed string.
Аргументы:
InputBuffer - Buffer to decode.
BufferSize - Size of buffer.
CodeWord - Password to decode.
Исключения:
nitro::exception Throws exception with error description.
Автор:
Dodonov A.A.

См. определение в файле string_utilities.cpp строка 254

Перекрестные ссылки nitro::exception::code() и nitro::exception::what().

        {
                try
                {
                        for( std::size_t i( 0 ) ; i < BufferSize ; i++ )
                        {
                                InputBuffer[ i ] ^= CodeWord[ i % strlen( CodeWord ) ];

                                for( int j( ( int )strlen( CodeWord ) - 1 ) ; j >= 0  ; j-- )
                                {
                                        InputBuffer[ i ] ^= CodeWord[ j ];
                                }
                        }
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "Encoders::XORDecode( char * InputBuffer , std::size_t BufferSize , char * CodeWord )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "Encoders::XORDecode( char * InputBuffer , std::size_t BufferSize , char * CodeWord )::An error occured" ) , 1 ) );
                }
        }

Граф вызовов:

void nitro::Encoders::XOREncode ( char *  OutputBuffer,
std::size_t  BufferSize,
char *  CodeWord 
) [static]

Функция кодирования ксором строки.

Аргументы:
OutputBuffer - Буффер, который будет закодирован.
BufferSize - Размер кодируемого буффера.
CodeWord - Слово, которым будем кодировать.
Исключения:
nitro::exception Кидает исключение с описанием ошибки.
Автор:
Додонов А.А.

См. определение в файле string_utilities.cpp строка 230

Перекрестные ссылки nitro::exception::code() и nitro::exception::what().

        {
                try
                {
                        for( std::size_t i( 0 ) ; i < BufferSize ; i++ )
                        {
                                for( std::size_t j( 0 ) ; j < strlen( CodeWord ) ; j++ )
                                {
                                        OutputBuffer[ i ] ^= CodeWord[ j ];
                                }

                                OutputBuffer[ i ] ^= CodeWord[ i % strlen( CodeWord ) ];
                        }
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "Encoders::XOREncode( char * OutputBuffer , std::size_t BufferSize , char * CodeWord )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "Encoders::XOREncode( char * OutputBuffer , std::size_t BufferSize , char * CodeWord )::An error occured" ) , 1 ) );
                }
        }

Граф вызовов:


Объявления и описания членов классов находятся в файлах:

Generated by  doxygen 1.6.1