stream bytes

Discussion in 'C++' started by Christopher, Dec 5, 2011.

  1. Christopher

    Christopher Guest

    I am trying to debug some code that supposedly stores text as bytes
    and then back again.
    The first step is to examine the contents.

    Somewhere data was inserted that contained at least one character with
    the signed bit set.
    So, the supposed "text" isn't even valid text at all.

    I had orginally written this code, which threw if an invalid text
    cahr (or non-ascii) was found:

    std::wstring StringBufferList::GetBytesAsText() const
    {
    // This class should have been storing bytes as unsigned char
    rather than char
    // to begin with and needs to be changed later.
    //
    // I am just adding this method quickly for debugging purposes.
    //
    // Because of the lack of type safety currently in insertion of
    any type using a reinterpret_cast
    // this class made use of, we must check each byte for validity.
    //
    // It was assumed only ascii characters would be used, but that
    might not be the case

    std::wstringstream output;
    size_t numBytes = getSize();

    for( const_iterator itBuffer = begin(); itBuffer != end(); itBuffer
    ++ )
    {
    for( size_t byteIndex = 0; byteIndex < numBytes; ++byteIndex )
    {
    char & data = itBuffer->buffer->value_.get()[byteIndex];

    if( data < 0 )
    {
    // Error - Invalid byte value
    std::wstringstream msg;
    msg << L"Attempted to convert byte values to wide
    character hex text values and came across a negative signed
    character.";

    LOG4CXX_ERROR(logger, msg.str());
    throw InternalErrorException(__WFILE__, __LINE__) <<
    msg.str();
    }

    output << std::hex << std::setw(2) << std::setfill(L'0')
    << data;
    output << "' '";
    }
    }

    return output.str();
    }


    In debugging, my exception happened.
    Can I examine the values just by taking my if statement out? How do I
    recognize the signed values in hex?

    If this buffer supposedly held XML in ASCII, how would you go about
    looking at the contents to see where it became invalid?
    Christopher, Dec 5, 2011
    #1
    1. Advertising

  2. Christopher <> wrote:
    > In debugging, my exception happened.
    > Can I examine the values just by taking my if statement out? How do I
    > recognize the signed values in hex?


    I don't really understand what is it that you are asking. Or, more
    precisely, it sounds to me like you are asking how to add debug output
    to your code, which is such a trivial question that I must have
    misunderstood.
    Juha Nieminen, Dec 5, 2011
    #2
    1. Advertising

  3. Christopher

    Christopher Guest

    Ok, maybe I should reword things.

    Someone, sometime, made an erroneous buffer class. The intention the
    author had was to store UTF-8 encoded text and that text was expected
    to be XML. Instead, the author decided that all he had to do was
    reinterpret_cast<const char *> whatever parameter came in to be
    stored:

    template <typename T, size_t L>
    StringBufferList & operator << (const T (& value)[L])
    {
    append(reinterpret_cast<const char *>(&value), (L - 1) *
    sizeof(T));
    return *this;
    }

    Then when extracting he reinterpet_cast<const wchar_t *>
    The author obviously had no idea what he was doing.

    Later down the road, someone must have inserted something besides
    UTF-8 encoded text. My goal is to identify where that is occuring. My
    thought was to examine the contents of the buffer and see what parts
    are able to be converted to UTF-8 encoded text, look at it, and see if
    it is something recognizable, so as to give me a clue where the bad
    insertions are occuring.

    It isn't as easy as just setting a breakpoint and looking at the call
    stack, because there is not much of a call stack, since the author
    also decided to use boost::bind to trigger this via callbacks.

    In order to examine the contents, since there is no telling what they
    are, I thought I should dump it out to a file as a textual
    representation of the bytes.

    So my questions are
    How do I dump the contents out as a textual representation of the
    bytes? Would the code below do the trick?


    std::wstring StringBufferList::GetBytesAsText() const
    {
    // This class should have been storing bytes as unsigned char
    rather than char
    // to begin with and needs to be changed later.
    //
    // I am just adding this method quickly for debugging purposes.
    //
    // Because of the lack of type safety currently in insertion of
    any type using a reinterpret_cast
    // this class made use of, we must check each byte for validity.

    std::wstringstream output;
    size_t numBytes = getSize();

    for( const_iterator itBuffer = begin(); itBuffer != end(); itBuffer
    ++ )
    {
    for( size_t byteIndex = 0; byteIndex < numBytes; ++byteIndex )
    {
    int & byte = static_cast<int>(itBuffer->buffer-
    >value_.get()[byteIndex]);

    output << std::hex << std::setw(2) << std::setfill(L'0')
    << byte;
    output << "' '";
    }
    }

    return output.str();
    }



    I cannot simply examine things in the debugger, because
    1) The enitre contents aren't valid text in any encoding, due to the
    author's bug
    2) The contents are split up amongst several data structures in a link
    list
    Christopher, Dec 6, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Collins
    Replies:
    3
    Views:
    5,997
    Jason Collins
    Feb 18, 2004
  2. mrby

    4-bytes or 8-bytes alignment?

    mrby, Nov 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    405
    Mark McIntyre
    Nov 2, 2004
  3. Replies:
    5
    Views:
    520
    Flash Gordon
    Apr 9, 2006
  4. Yandos
    Replies:
    12
    Views:
    5,100
    Pete Becker
    Sep 15, 2005
  5. Replies:
    8
    Views:
    485
    Bob Hairgrove
    Apr 10, 2006
Loading...

Share This Page