Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using binary data in C++ string

I am working with binary data coming from a service provider. I am subscribing to this data through HTTP request, using C++. Every now and then, I get an HTML payload coming from this provider. The HTML payload is actually binary data, which looks like that:

! ¦ô¿Âˤ ÍÌL? Àÿ Àÿ¥ š™©@ Àÿ Àÿ or ! H·ô¿Âˤ ÍÌL? Àÿ333?¥ š™©@ Àÿff¦@

I would like to do simulation using the data I get. To do so I hard code the data in a string and launch my program that will use those strings.

std::string input = "! ¦ô¿Âˤ ÍÌL? Àÿ Àÿ¥ š™©@ Àÿ Àÿ"

The first issue I have is that I have to escape all characters like \n. But I don't know how to escape \0 for example. Also, I have this error message, which may be due to the fact that I don't escape the end of file character properly:

Error 3 fatal error C1004: unexpected end-of-file found

So the main question is: what does the end-of-file character looks like, and how can I escape it ?

Then, is there a linux command or a way to take binary data from a binary file, and escape all the special character with \ so then I just copy paste it in my C++ code.

Eventually, I would like to put all the different payloads I have got, in a binary file, so I can launch my simulation using this file. The issue is that I don't know how to separate different payloads, since just going to the next line won't do the trick, because it will be interpreted as a random character (and the payload don't have a fix size). I don't know what kind of separator to use.

like image 447
0x26res Avatar asked Feb 02 '11 10:02

0x26res


People also ask

How do you input a binary string?

If you need to read a binary number from a string, you would roll the reverse code to what you do to print it ( std::cin >> n assumes that the input is a base 10 number, so it reads a wrong number if the input is actually intended to be in base 2).

Can STD string hold binary data?

std::string can hold binary data. That is, a string with '\0' in any position. Note that it's not entirely trivial to create such a string.

What is binary string example?

Here are some examples: The empty string ε is a binary string. ε is in both Σ* and Σ**. 0 and 1 are finite binary strings, and are in both Σ* and Σ**, as are all finite binary strings.


1 Answers

You can write your payload in a file and read it using a std::ifstream. That would allow you to change the payload without having to recompile.

If you really want to store it as binary data, you can use an array of char, and initialize it like that :

const unsigned char raw_data[] = {
    0x21, 0x20, 0xc2, 0xa6, 0xc3, 0xb4, 0xc2, 0xbf,
    0xc3, 0x82, 0xc3, 0x8b, 0xc2, 0xa4, 0x20, 0xc3,
    0x8d, 0xc3, 0x8c, 0x4c, 0x3f, 0x20, 0x20, 0xc3,
    0x80, 0xc3, 0xbf, 0x20, 0x20, 0xc3, 0x80, 0xc3,
    0xbf, 0xc2, 0xa5, 0x20, 0xc5, 0xa1, 0xe2, 0x84,
    0xa2, 0xc2, 0xa9, 0x40, 0x20, 0x20, 0xc3, 0x80,
    0xc3, 0xbf, 0x20, 0x20, 0xc3, 0x80, 0xc3, 0xbf,
    0x60, 0x20, 0x6f, 0x72, 0x20, 0x60, 0x21, 0x20,
    0x48, 0xc2, 0xb7, 0xc3, 0xb4, 0xc2, 0xbf, 0xc3,
    0x82, 0xc3, 0x8b, 0xc2, 0xa4, 0x20, 0xc3, 0x8d,
    0xc3, 0x8c, 0x4c, 0x3f, 0x20, 0x20, 0xc3, 0x80,
    0xc3, 0xbf, 0x33, 0x33, 0x33, 0x3f, 0xc2, 0xa5,
    0x20, 0xc5, 0xa1, 0xe2, 0x84, 0xa2, 0xc2, 0xa9,
    0x40, 0x20, 0x20, 0xc3, 0x80, 0xc3, 0xbf, 0x66,
    0x66, 0xc2, 0xa6, 0x40, 0x0a,
};

std::string data(
    reinterpret_cast< const char* >(raw_data),
    reinterpret_cast< const char* >(raw_data) + sizeof(raw_data));

Oh, BTW, I converted your payload to a buffer using the following simple Python code:

#!/usr/bin/python

def convert_file(path, stream):
    data = open(path, 'rb').read()
    stream.write('const unsigned char raw_data[] = {')
    for i, char in enumerate(data):
        if i % 8 == 0:
            stream.write('\n   ')
        stream.write(' 0x%02x,' % (ord(char),))
    stream.write('\n};\n')

if __name__ == '__main__':
    import sys
    convert_file(sys.argv[1], sys.stdout)
like image 178
Sylvain Defresne Avatar answered Oct 09 '22 11:10

Sylvain Defresne