Click here to Skip to main content
Click here to Skip to main content

TEA Encryption/Decryption Made Simple

By , 20 Dec 2008
 
tea_proj.zip
TestDlg EncryptDecrypt
Debug
res
TestDlg.ico
TestDlg.sln.old
TestDlg.suo
TestDlg.suo.old
TestDlg.vcproj.8.00.old
TestDlg.vcproj.Michael-PC.Michael.user
TestDlg.vcproj.MMOPrivatPC.Michael.user
TestDlg.vcproj.MMPOCKETPC.Michael Mogensen.user
tea_source.zip
//////////////////////////////////////////////////////////////////////
//
//  strconv.h - Some useful classes for string conversion.
//
//  Copyright (c) 2004-2007 by Daniel Strigl.
//
//  This code may be used in compiled form in any way you desire.
//  This file may be redistributed unmodified by any means PROVIDING
//  it is not sold for profit without the authors written consent,
//  and providing that this notice and the authors name is included.
//  By the way, if the source code in this file is used in any
//  application I would appreciate and enjoy hearing about them.
//
//  This file is provided "as is" with no expressed or implied warranty.
//  The author accepts no liability for any damage, in any form, caused
//  by this code. Use it at your own risk and as with all code expect
//  bugs! It's been tested, but I'm not perfect ;-)
//
//  Please use the contact page of my blog to report any
//  bug, comment or suggestion.
//
//  Contact:
//  --------
//
//    Daniel Strigl [http://geekswithblogs.net/dastblog/]
//
//  History:
//  --------
//
//    21 Jul 2004 - Initial version.
//    20 Nov 2006 - Added class CAtoT, CWtoT, CAtoW and CWtoA.
//
//////////////////////////////////////////////////////////////////////////

#if !defined(_STRCONV_H_)
#define _STRCONV_H_

//////////////////////////////////////////////////////////////////////////
//
//  CTtoA
//
//      LPCTSTR to LPCSTR conversion.
//
class CTtoA
{
    LPSTR m_psz;

public:
    CTtoA(LPCTSTR ptsz) : m_psz(NULL)
    {
        ASSERT(ptsz != NULL);
#if defined(_UNICODE) // for UNICODE build
        const int size = WideCharToMultiByte(CP_ACP, 0, ptsz, -1,
            NULL, 0, NULL, NULL);
#ifdef _DEBUG
        if (size == 0) {
            ASSERT(!_T("Function \"WideCharToMultiByte\" failed."));
        }
#endif // _DEBUG
        ASSERT(size > 0);
        m_psz = (LPSTR) malloc(sizeof(CHAR)*size);
        ASSERT(m_psz != NULL);
#ifdef _DEBUG
        memset(m_psz, 0, sizeof(CHAR)*size);
#endif // _DEBUG
        VERIFY(WideCharToMultiByte(CP_ACP, 0, ptsz, -1,
						m_psz, size, NULL, NULL) != 0);
#else // for non-UNICODE build
        m_psz = _strdup(ptsz);
        ASSERT(m_psz != NULL);
#endif // defined(_UNICODE)
    }

    CTtoA(const CTtoA& rhs) : m_psz(_strdup(rhs.m_psz))
    {
        ASSERT(m_psz != NULL);
    }

    CTtoA& operator=(const CTtoA& rhs)
    {
        if (this != &rhs)
        {
            free(m_psz);
            m_psz = _strdup(rhs.m_psz);
            ASSERT(m_psz != NULL);
        }
        return *this;
    }

    ~CTtoA()
    {
        free(m_psz);
    }

    size_t GetLength() const { return strlen(m_psz); }

    operator LPCSTR() const { return m_psz; }
};

//////////////////////////////////////////////////////////////////////////
//
//  CTtoW
//
//      LPCTSTR to LPCWSTR conversion.
//
class CTtoW
{
    LPWSTR m_pwsz;

public:
    CTtoW(LPCTSTR ptsz) : m_pwsz(NULL)
    {
        ASSERT(ptsz != NULL);
#if defined(_UNICODE) // for UNICODE build
        m_pwsz = _wcsdup(ptsz);
        ASSERT(m_pwsz != NULL);
#else // for non-UNICODE build
        const int size = MultiByteToWideChar(CP_ACP, 0, ptsz, -1, NULL, 0);
#ifdef _DEBUG
        if (size == 0) {
            ASSERT(!_T("Function \"MultiByteToWideChar\" failed."));
        }
#endif // _DEBUG
        ASSERT(size > 0);
        m_pwsz = (LPWSTR) malloc(sizeof(WCHAR)*size);
        ASSERT(m_pwsz != NULL);
#ifdef _DEBUG
        memset(m_pwsz, 0, sizeof(WCHAR)*size);
#endif // _DEBUG
        VERIFY(MultiByteToWideChar(CP_ACP, 0, ptsz, -1, m_pwsz, size) != 0);
#endif // defined(_UNICODE)
    }

    CTtoW(const CTtoW& rhs) : m_pwsz(_wcsdup(rhs.m_pwsz))
    {
        ASSERT(m_pwsz != NULL);
    }

    CTtoW& operator=(const CTtoW& rhs)
    {
        if (this != &rhs)
        {
            free(m_pwsz);
            m_pwsz = _wcsdup(rhs.m_pwsz);
            ASSERT(m_pwsz != NULL);
        }
        return *this;
    }

    ~CTtoW()
    {
        free(m_pwsz);
    }

    size_t GetLength() const { return wcslen(m_pwsz); }

    operator LPCWSTR() const { return m_pwsz; }
};

//////////////////////////////////////////////////////////////////////////
//
//  CAtoT
//
//      LPCSTR to LPCTSTR conversion.
//
class CAtoT
{
    LPTSTR m_ptsz;

public:
    CAtoT(LPCSTR psz) : m_ptsz(NULL)
    {
        ASSERT(psz != NULL);
#if defined(_UNICODE) // for UNICODE build
        const int size = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
#ifdef _DEBUG
        if (size == 0) {
            ASSERT(!_T("Function \"MultiByteToWideChar\" failed."));
        }
#endif // _DEBUG
        ASSERT(size > 0);
        m_ptsz = (LPWSTR) malloc(sizeof(WCHAR)*size);
        ASSERT(m_ptsz != NULL);
#ifdef _DEBUG
        memset(m_ptsz, 0, sizeof(WCHAR)*size);
#endif // _DEBUG
        VERIFY(MultiByteToWideChar(CP_ACP, 0, psz, -1, m_ptsz, size) != 0);
#else // for non-UNICODE build
        m_ptsz = _strdup(psz);
        ASSERT(m_ptsz != NULL);
#endif // defined(_UNICODE)
    }

    CAtoT(const CAtoT& rhs) : m_ptsz(_tcsdup(rhs.m_ptsz))
    {
        ASSERT(m_ptsz != NULL);
    }

    CAtoT& operator=(const CAtoT& rhs)
    {
        if (this != &rhs)
        {
            free(m_ptsz);
            m_ptsz = _tcsdup(rhs.m_ptsz);
            ASSERT(m_ptsz != NULL);
        }
        return *this;
    }

    ~CAtoT()
    {
        free(m_ptsz);
    }

    size_t GetLength() const { return _tcslen(m_ptsz); }

    operator LPCTSTR() const { return m_ptsz; }
};

//////////////////////////////////////////////////////////////////////////
//
//  CWtoT
//
//      LPCWSTR to LPCTSTR conversion.
//
class CWtoT
{
    LPTSTR m_ptsz;

public:
    CWtoT(LPCWSTR pwsz) : m_ptsz(NULL)
    {
        ASSERT(pwsz != NULL);
#if defined(_UNICODE) // for UNICODE build
        m_ptsz = _wcsdup(pwsz);
        ASSERT(m_ptsz != NULL);
#else // for non-UNICODE build
        const int size = WideCharToMultiByte(CP_ACP, 0, pwsz, -1,
            NULL, 0, NULL, NULL);
#ifdef _DEBUG
        if (size == 0) {
            ASSERT(!_T("Function \"WideCharToMultiByte\" failed."));
        }
#endif // _DEBUG
        ASSERT(size > 0);
        m_ptsz = (LPSTR) malloc(sizeof(CHAR)*size);
        ASSERT(m_ptsz != NULL);
#ifdef _DEBUG
        memset(m_ptsz, 0, sizeof(CHAR)*size);
#endif // _DEBUG
        VERIFY(WideCharToMultiByte(CP_ACP, 0, pwsz, -1,
            m_ptsz, size, NULL, NULL) != 0);
#endif // defined(_UNICODE)
    }

    CWtoT(const CWtoT& rhs) : m_ptsz(_tcsdup(rhs.m_ptsz))
    {
        ASSERT(m_ptsz != NULL);
    }

    CWtoT& operator=(const CWtoT& rhs)
    {
        if (this != &rhs)
        {
            free(m_ptsz);
            m_ptsz = _tcsdup(rhs.m_ptsz);
            ASSERT(m_ptsz != NULL);
        }
        return *this;
    }

    ~CWtoT()
    {
        free(m_ptsz);
    }

    size_t GetLength() const { return _tcslen(m_ptsz); }

    operator LPCTSTR() const { return m_ptsz; }
};

//////////////////////////////////////////////////////////////////////////
//
//  CAtoW
//
//      LPCSTR to LPCWSTR conversion.
//
class CAtoW
{
    LPWSTR m_pwsz;

public:
    CAtoW(LPCSTR psz) : m_pwsz(NULL)
    {
        ASSERT(psz != NULL);
        const int size = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
#ifdef _DEBUG
        if (size == 0) {
            ASSERT(!_T("Function \"MultiByteToWideChar\" failed."));
        }
#endif // _DEBUG
        ASSERT(size > 0);
        m_pwsz = (LPWSTR) malloc(sizeof(WCHAR)*size);
        ASSERT(m_pwsz != NULL);
#ifdef _DEBUG
        memset(m_pwsz, 0, sizeof(WCHAR)*size);
#endif // _DEBUG
        VERIFY(MultiByteToWideChar(CP_ACP, 0, psz, -1, m_pwsz, size) != 0);
    }

    CAtoW(const CAtoW& rhs) : m_pwsz(_wcsdup(rhs.m_pwsz))
    {
        ASSERT(m_pwsz != NULL);
    }

    CAtoW& operator=(const CAtoW& rhs)
    {
        if (this != &rhs)
        {
            free(m_pwsz);
            m_pwsz = _wcsdup(rhs.m_pwsz);
            ASSERT(m_pwsz != NULL);
        }
        return *this;
    }

    ~CAtoW()
    {
        free(m_pwsz);
    }

    size_t GetLength() const { return wcslen(m_pwsz); }

    operator LPCWSTR() const { return m_pwsz; }
};

//////////////////////////////////////////////////////////////////////////
//
//  CWtoA
//
//      LPCWSTR to LPCSTR conversion.
//
class CWtoA
{
    LPSTR m_psz;

public:
    CWtoA(LPCWSTR pwsz) : m_psz(NULL)
    {
        ASSERT(pwsz != NULL);
        const int size = WideCharToMultiByte(CP_ACP, 0, pwsz, -1,
            NULL, 0, NULL, NULL);
#ifdef _DEBUG
        if (size == 0) {
            ASSERT(!_T("Function \"WideCharToMultiByte\" failed."));
        }
#endif // _DEBUG
        ASSERT(size > 0);
        m_psz = (LPSTR) malloc(sizeof(CHAR)*size);
        ASSERT(m_psz != NULL);
#ifdef _DEBUG
        memset(m_psz, 0, sizeof(CHAR)*size);
#endif // _DEBUG
        VERIFY(WideCharToMultiByte(CP_ACP, 0, pwsz, -1,
            m_psz, size, NULL, NULL) != 0);
    }

    CWtoA(const CWtoA& rhs) : m_psz(_strdup(rhs.m_psz))
    {
        ASSERT(m_psz != NULL);
    }

    CWtoA& operator=(const CWtoA& rhs)
    {
        if (this != &rhs)
        {
            free(m_psz);
            m_psz = _strdup(rhs.m_psz);
            ASSERT(m_psz != NULL);
        }
        return *this;
    }

    ~CWtoA()
    {
        free(m_psz);
    }

    size_t GetLength() const { return strlen(m_psz); }

    operator LPCSTR() const { return m_psz; }
};

//////////////////////////////////////////////////////////////////////////

#endif // !defined(_STRCONV_H_)

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Michael Pauli
Software Developer (Senior)
Denmark Denmark
Member
c/c++/c# -developer.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 20 Dec 2008
Article Copyright 2008 by Michael Pauli
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid