Is this code spinning it’s wheels

345 pts.
Tags:
C
Windows Server 2008
I have a piece of code which gets a transaction approval from a server which handles bank transactions. The code is supposed to be checking a returned base16 byte array to make sure all of the characters are valid. I do no believe the code does anything except check 1 character in the array. Please tell me I'm wrong. This is the code:
unsigned long CyoDecode::Base16Decode( void* dest, const void* src, unsigned long size )
{
    if ((dest == NULL) || (src == NULL))
        throw std::runtime_error( c_pszErrorInvalidParameter );

    if (size % BASE16_INPUT != 0)
        throw std::runtime_error( c_pszErrorNotMultipleOf2 );

    ///

    LPBYTE pSrc = (LPBYTE)src;
    LPBYTE pDest = (LPBYTE)dest;
    DWORD dwSrcSize = size;
    DWORD dwDestSize = 0;

    while (dwSrcSize >= 1)
    {
        // 2 inputs...
        BYTE in1 = *pSrc++;
        BYTE in2 = *pSrc++;
        dwSrcSize -= BASE16_INPUT; //2

        // Validate ascii...
        assert( 0 <= in1 && in1 <= 0x7f );
        assert( 0 <= in2 && in2 <= 0x7f );

        // Convert ascii to base16...
        in1 = BASE16_TABLE[ in1 ];
        in2 = BASE16_TABLE[ in2 ];

        // Validate base16...
        assert( in1 != 0xff );
        assert( 0 <= in1 && in1 <= 15 );
        assert( 0 <= in2 && in2 <= 15 );

        // 1 output...
        *pDest++ = ((in1 << 4) | in2);
        dwDestSize += BASE16_OUTPUT; //1
    }

    return dwDestSize;
}


Software/Hardware used:
Windows server 2008

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 8  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • CarlCioffi

    The application terminates at this line of code:

    assert( in1 != 0xff );

    345 pointsBadges:
    report
  • CarlCioffi

    This is the base16_table:

        DWORD BASE16_INPUT = 2;
        DWORD BASE16_OUTPUT = 1;
        BYTE BASE16_TABLE[ 0x80 ] = {
            /*00-07*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*08-0f*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*10-17*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*18-1f*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*20-27*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*28-2f*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*30-37*/ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, //8 = '0'-'7'
            /*38-3f*/ 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //2 = '8'-'9'
            /*40-47*/ 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xFF, //6 = 'A'-'F'
            /*48-4f*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*50-57*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*58-5f*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*60-67*/ 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xFF, //6 = 'a'-'f' (same as 'A'-'F')
            /*68-6f*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*70-77*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            /*78-7f*/ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
        };
    
     
    345 pointsBadges:
    report
  • carlosdl
    What makes you think it only validates 1 character?
    70,200 pointsBadges:
    report
  • CarlCioffi
    I'm not really a C++ programmer and the *pSrc++ seems a little odd at the beginning of a while loop.  Does that actually change the value of pSrc?  If not then it's always looking at the same character in the byte array.  If it does then that is fine but in that case it would never be looking at the first position in the byte array because it increments right off the bat.
    345 pointsBadges:
    report
  • carlosdl
    When you have the increment operator (++) placed at the end of the variable, it gives you the previous value and after that it increments its value.

    So, for example, if you have this code:

    a=5;
    b=a++;
    b will be assigned the value of 5, and after that line of code is executed a will have a value of 6.
    70,200 pointsBadges:
    report
  • CarlCioffi
    Ok I get it now.  I'm so glad I use C#, it makes stuff like this so much easier and a log easier to read and understand.  Especially when you have to look at code that somebody wrote with basically no source code documentation.
    345 pointsBadges:
    report
  • TomLiotta

    I’m so glad I use C#, it makes stuff like this so much easier and a log easier to read and understand.

    It's not clear why C# would make much difference. The postfix increment operator (i.e., "x++") works essentially the same way in C#.

    Tom

    125,585 pointsBadges:
    report
  • CarlCioffi
    I like C# because it seems C and C++ developers like to write code that's cryptic and not easily read.  They don't really seem to see value in documenting their code either.  Just my observation over the 30+ years I've been writing software.
    345 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following