Jump to content
Moopler
Sign in to follow this  
Guest yuri

Snippet TSecType - Reorganized

Recommended Posts

Guest yuri

Example usage is same as original post benny made https://wecodez.com/threads/source-tsectype-class.604/

    TSecType<double> tsec_double;
    printf("TSecType<double>:\n");
    printf("setData(): %04X\n", tsec_double.setData(1.89));
    printf("getData(): %08f\n", tsec_double.getData());
    printf("\n");

Example usage of TSecType in Getting Item coordinates https://github.com/Waty/KamiPls/blob/master/KamiPls/TSecType.h

 

TSecType.cpp

/**
* \brief get and set data from a checksum 
* \author benny - original source https://wecodez.com/threads/source-tsectype-class.604/
* \author yuri  - reorganized
* \version 12/16/2016
*/

#include <cstdio>
#include <cstdlib>
#include "TSecType.h"

/**
 * \brief explicit instantiation for dynamic usage. 
 *		  Add or remove generics supported here
 */
template TSecType<int>;
template TSecType<signed int>;
template TSecType<unsigned int>;
template TSecType<long>;
template TSecType<signed long>;
template TSecType<unsigned long>;
template TSecType<signed char>;
template TSecType<unsigned char>;
template TSecType<short>;
template TSecType<signed short>;
template TSecType<unsigned short>;
template TSecType<char>;
template TSecType<signed char>;
template TSecType<unsigned char>;
template TSecType<double>;
template TSecType<float>;

/**
 * \brief private helper to get lower byte using upgraded 
 *        #define LOBYTE(x)  (*(unsigned char *) &(x))
 * \param x template x value
 * \return pointed type casted int to char 
 */
template <class T>
unsigned char LOBYTE(T x)
{
    return (*reinterpret_cast<unsigned char *>(&(x)));
}


/**
* \brief private helper to initialize constructor of object by using generic parameter
* \param data generic type object
*/
template <class T>
void TSecType<T>::init(T data)
{
    this->m_secdata = reinterpret_cast<TSecData<T>*>(malloc(12));
    this->FakePtr1 = static_cast<unsigned int>(reinterpret_cast<unsigned int>(&this[-0x00003FF8]) + rand());
    this->FakePtr2 = static_cast<unsigned int>(reinterpret_cast<unsigned int>(&this[-0x00003FF8]) + rand());
    this->m_secdata->FakePtr1 = static_cast<unsigned char>(LOBYTE(this->FakePtr1));
    this->m_secdata->FakePtr2 = static_cast<unsigned char>(LOBYTE(this->FakePtr2));
    this->setData(data);
}


/**
* \brief private helper method to get key at index
* \param index used to find key
* \return the key
*/
template <class T>
unsigned char TSecType<T>::setKey(unsigned char index)
{
    unsigned char key = 0;
    //key = (i == 0 ? this->m_secdata->bKey : reinterpret_cast<unsigned char*>(&this->m_secdata->data)[i - 1] + key + 42);
    if (index == 0)
        key = this->m_secdata->bKey;
    else
        key = reinterpret_cast<unsigned char*>(&this->m_secdata->data)[index - 1] + key + 42;

    return key;
}


/**
* \brief default constructor setting data to zero
*/
template <class T>
TSecType<T>::TSecType()
{
    init(0);
}


/**
* \brief default constructor setting data
* \param data custom data to add
*/
template <class T>
TSecType<T>::TSecType(T data)
{
    init(data);
}


/**
* \brief Default destructor, clears m_secdata
*/
template <class T>
TSecType<T>::~TSecType()
{
    if (this->m_secdata) // != null
        free(this->m_secdata);
    // TODO: else exception here
}


/**
* \brief gets data from the "encryped" checksum
* \return the decrypted data
*/
template <class T>
T TSecType<T>::getData()
{
    T decrypted_data = 0;
    unsigned short checksum = 0;
    for (unsigned char i = 0; i < (sizeof(T) + 1); i++)
    {
        unsigned char key = this->setKey(i);
        if (i > 0)
            if (i > 1) // bitwise or 
                checksum = ((8 * checksum) | (key + (checksum >> 13)));
            else
                checksum = ((key + 4) | 0xD328);
        if (i < sizeof(T))
        {
            if (!key) // no key
                key = 42;
            reinterpret_cast<unsigned char*>(&decrypted_data)[i] = reinterpret_cast<unsigned char*>(&this->m_secdata->data)[i] ^ key;
        }
    }
    // test to throw exception on error   checksum = 0;
    // final check on failure of checksum
    if ((this->m_secdata->wChecksum != checksum) ||
        (LOBYTE(this->FakePtr1) != this->m_secdata->FakePtr1) ||
        (LOBYTE(this->FakePtr2) != this->m_secdata->FakePtr2))
    {
        // TODO: throw exception on error. Needs to be handled.
        printf("GetData failed.\n"
               "wChecksum - TSecData(%04X) : GetData(%04X)\n"
               "FakePtr1 - TSecData(%02X) : TSecType(%02X)\n"
               "FakePtr2 - TSecData(%02X) : TSecType(%02X)\n",
            this->m_secdata->wChecksum, checksum,
            this->m_secdata->FakePtr1, static_cast<unsigned char>(LOBYTE(this->FakePtr1)),
            this->m_secdata->FakePtr2, static_cast<unsigned char>(LOBYTE(this->FakePtr2)));
        return 0;
    }

    return decrypted_data;
}


/**
* \brief sets data according to the checksum
* \param data to "encrypt" into checksum
* \return wChecksum of set Data
*/
template <class T>
unsigned short TSecType<T>::setData(T data)
{
    this->m_secdata->bKey = static_cast<unsigned char>(rand());
    this->m_secdata->wChecksum = static_cast<unsigned short>(39525);

    for (unsigned char i = 0; i < (sizeof(T) + 1); i++)
    {
        unsigned char key = this->setKey(i);
        if (i > 0)
            this->m_secdata->wChecksum = ((8 * this->m_secdata->wChecksum) | (key + (this->m_secdata->wChecksum >> 13)));

        if (i < sizeof(T))
        {
            if (!key)
                key = 42;
            reinterpret_cast<unsigned char*>(&this->m_secdata->data)[i] = reinterpret_cast<unsigned char*>(&data)[i] ^ key;
        }
    }
    return this->m_secdata->wChecksum;
}

 

TSecType.h

#pragma once

template <class T>
struct TSecData
{
    T data;
    unsigned char bKey;
    unsigned char FakePtr1;
    unsigned char FakePtr2;
    unsigned short wChecksum;
};

template <class T>
class TSecType
{
public:
    TSecType();
    TSecType(T data);
    ~TSecType();
    T getData();
    unsigned short setData(T data);
private:
    unsigned int FakePtr1;
    unsigned int FakePtr2;
    TSecData<T>* m_secdata;
    void init(T data);
    unsigned char setKey(unsigned char index);
};

 

Same as benny's original work, just organized differently and pleases my eyes. Feel free to add / remove. Planned to throw exceptions but didn't have time yet.

 

Edited by yuri

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×