Including huge strings with line breaks as variables

P

pkirk25

I have a file with roughly 1000 lines that I cannot allow the user to
read but which I want to use in my project.

I thought I could copy it into one huge string variable but that won't
work because of the line breaks.

Is there a way to enclos ethis data in 1 huge variable that tehn gets
compiled into the exutable?
 
P

pkirk25

c++ error C2001: newline in constant

Examples of the text are:
string crafted_items = "alchemy,Minor Healing
Potion,1,Peacebloom,1,Silverleaf,1,Empty Vial
alchemy,Elixir of Lion's Strength,1,Earthroot,1,Silverleaf,1,Empty Vial
alchemy,Elixir of Minor Defense,2,Silverleaf,1,Empty Vial,,
alchemy,Weak Troll's Blood Potion,1,Peacebloom,2,Earthroot,1,Empty Vial
alchemy,Minor Mana Potion,1,Mageroyal,1,Silverleaf,1,Empty Vial
alchemy,Minor Rejuvenation Potion,2,Mageroyal,1,Peacebloom,1,Empty
Vial";

Its in CSV format.

I like Accelerated C++ btw - on my desk right now and used this morning
while working out medians.
 
P

pkirk25

Done by exporting each liem to a text file perceded by

item_list.push_back("

and followed by ");

Is this the best way to hide data?
 
P

Peter

pkirk25 said:
c++ error C2001: newline in constant

Examples of the text are:
string crafted_items = "alchemy,Minor Healing
Potion,1,Peacebloom,1,Silverleaf,1,Empty Vial
alchemy,Elixir of Lion's Strength,1,Earthroot,1,Silverleaf,1,Empty Vial
alchemy,Elixir of Minor Defense,2,Silverleaf,1,Empty Vial,,
alchemy,Weak Troll's Blood Potion,1,Peacebloom,2,Earthroot,1,Empty Vial
alchemy,Minor Mana Potion,1,Mageroyal,1,Silverleaf,1,Empty Vial
alchemy,Minor Rejuvenation Potion,2,Mageroyal,1,Peacebloom,1,Empty
Vial";

Its in CSV format.

I like Accelerated C++ btw - on my desk right now and used this morning
while working out medians.


you will have to terminate the end-of line with a backslash:

const char ad[] = "line1\n\
line2\n\
line3\n\
line4";

If you don't want to change this string there is no need to copy it
into a std::string but you can leave it in a const char array like
above.
 
M

Michael

c++ error C2001: newline in constant
Examples of the text are:
string crafted_items = "alchemy,Minor Healing
Potion,1,Peacebloom,1,Silverleaf,1,Empty Vial
alchemy,Elixir of Lion's Strength,1,Earthroot,1,Silverleaf,1,Empty Vial
alchemy,Elixir of Minor Defense,2,Silverleaf,1,Empty Vial,,
alchemy,Weak Troll's Blood Potion,1,Peacebloom,2,Earthroot,1,Empty Vial
alchemy,Minor Mana Potion,1,Mageroyal,1,Silverleaf,1,Empty Vial
alchemy,Minor Rejuvenation Potion,2,Mageroyal,1,Peacebloom,1,Empty
Vial";

Just put quotes around each line and add newlines:
string crafted_items = "alchemy,Minor Healing\n"
"Potion,1,Peacebloom,1,Silverleaf,1,Empty Vial\n"
"alchemy,Elixir of Lion's Strength,1,Earthroot,1,Silverleaf,1,Empty
Vial\n"
etc.

The compiler concatenates adjacent string literals into one.

I've been doing this a lot in some code I wrote recently, and it's just
a couple of commands in VI to change the original text to the final.
Or you could write a little C++ program. Or a script of some sort.

Michael
 
R

red floyd

Peter said:
you will have to terminate the end-of line with a backslash:

const char ad[] = "line1\n\
line2\n\
line3\n\
line4";

Why bother with the backslashes? Just rely on literal concatenation.
The compiler will concatenate adjacent string literals.
e.g.:

const char ad[] = "line1\n"
"line2\n"
"line3\n"
"line4";
 
K

Kai-Uwe Bux

pkirk25 said:
I have a file with roughly 1000 lines that I cannot allow the user to
read but which I want to use in my project.

I thought I could copy it into one huge string variable but that won't
work because of the line breaks.

Is there a way to enclos ethis data in 1 huge variable that tehn gets
compiled into the exutable?

Sure, there is:

#include <iostream>

char const multi_line_const [] =
"the first line,\n"
"the second line,\n"
"and the third line\n";

int main ( void ) {
std::cout << multi_line_const;
}

But that won't stop any but the most lazy attacker: the compiler will just
embed the string within the executable. Thus, anybody can still read it
using a text or a hex editor.

Also, any cracker will run 'strings' (or whatever equivalent is available on
the target platform) on the executable and get the whole text without
effort:

news_group> strings a.out
/lib/ld-linux.so.2
libstdc++.so.6
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
__gxx_personality_v0
_ZNSt8ios_base4InitC1Ev
_ZNSt8ios_base4InitD1Ev
_ZSt4cout
_Jv_RegisterClasses
__gmon_start__
libm.so.6
libgcc_s.so.1
libc.so.6
_IO_stdin_used
__libc_start_main
/added/pkg/gcc-4.1.1/usr/lib
GLIBC_2.0
CXXABI_1.3
GLIBCXX_3.4
PTRhq
[^_]
the first line,
the second line,
and the third line


This route of obfuscation is essentially useless.


Best

Kai-Uwe Bux
 
J

Jim Langston

pkirk25 said:
I have a file with roughly 1000 lines that I cannot allow the user to
read but which I want to use in my project.

I thought I could copy it into one huge string variable but that won't
work because of the line breaks.

Is there a way to enclos ethis data in 1 huge variable that tehn gets
compiled into the exutable?

Read the above replies to your exact question, but I don't think you want to
go this way anyway as it's very easy to look at an executable program and
read the strings in them.

A better way would be to go ahead and put it in a file, but encrypt it
somehow. You should be able to google for a fairly simple encryption you
could use (doesn't need to be military grade) and then all you have to keep
in your source .exe is the key for the encryption, which would look like
garbage anyway. And if you got creative you could build the key from a few
parts of different strings so it would look like even more garbage.
 
P

pkirk25

Jim Langston wrote:
[...]
A better way would be to go ahead and put it in a file, but encrypt it
somehow. You should be able to google for a fairly simple encryption you
could use (doesn't need to be military grade) and then all you have to keep
in your source .exe is the key for the encryption, which would look like
garbage anyway. And if you got creative you could build the key from a few
parts of different strings so it would look like even more garbage.

Interesting. I do need to do something because a freeware that helps
gamers but costs me legal fees would be a bad idea. Not something the
missus would understand...
 
B

BobR

pkirk25 wrote in message
Jim Langston wrote:
[...]
A better way would be to go ahead and put it in a file, but encrypt it
somehow. You should be able to google for a fairly simple encryption you
could use (doesn't need to be military grade) and then all you have to keep
in your source .exe is the key for the encryption, which would look like
garbage anyway. And if you got creative you could build the key from a few
parts of different strings so it would look like even more garbage.

Interesting. I do need to do something because a freeware that helps
gamers but costs me legal fees would be a bad idea. Not something the
missus would understand...

You could just store the string as numerical:

std::string HideThis(" don't want just "
"anybody to see this "
"hidden message! ");
// std::reverse( HideThis.begin(), HideThis.end() ); // reverse it

std::eek:fstream Fout("XFiles.txt");
if( not Fout ){ /* handle error */ }
for(size_t i(0); i < HideThis.size(); ++i){
Fout << int( HideThis.at( i ) ) << " ";
} // for(i)
Fout<<std::endl;
Fout.close();

// -- output --
32 100 111 110 39 116 32 119 97 110 116 32 106 117 115 116 32 97 110 121 98
111 100 121 32 116 111 32 115 101 101 32 116 104 105 115 32 104 105 100 100
101 110 32 109 101 115 115 97 103 101 33 32


Then reverse the procedure to read it into a std::string in your program
(using a '.push_back( char( number ) );' or?).
 
K

Kai-Uwe Bux

BobR said:
pkirk25 wrote in message
Jim Langston wrote:
[...]
A better way would be to go ahead and put it in a file, but encrypt it
somehow. You should be able to google for a fairly simple encryption
you could use (doesn't need to be military grade) and then all you have
to keep
in your source .exe is the key for the encryption, which would look like
garbage anyway. And if you got creative you could build the key from a few
parts of different strings so it would look like even more garbage.

Interesting. I do need to do something because a freeware that helps
gamers but costs me legal fees would be a bad idea. Not something the
missus would understand...

To the OP: That sounds like the cryptographic attacker would not be the
gamer but a company. In that case, the attacker may safely be assumed to be
highly motivated and resourcefull. Then you are facing a difficult problem,
namely that you have to deliver. That means you have to give away the
information within the file somehow. You may embed an encrypted version of
the file, but you also have to embed the key, and you have to embed the
decryption routine. In these three parts, you give away all the attacker
needs to reconstruct the file.

Also, if you are up against a company you should consider the non-technical
attacks they have: the way you mention legal fees can indicate that you
expect being sued. Then, their lawers will just ask the court to order you
to fork over all iterations of the source code and related data during
discovery (caveat: IANAL). When that happens, all encryption was in vain
anyway.

You could just store the string as numerical:

std::string HideThis(" don't want just "
"anybody to see this "
"hidden message! ");
// std::reverse( HideThis.begin(), HideThis.end() ); // reverse it

std::eek:fstream Fout("XFiles.txt");
if( not Fout ){ /* handle error */ }
for(size_t i(0); i < HideThis.size(); ++i){
Fout << int( HideThis.at( i ) ) << " ";
} // for(i)
Fout<<std::endl;
Fout.close();

// -- output --
32 100 111 110 39 116 32 119 97 110 116 32 106 117 115 116 32 97 110 121
98 111 100 121 32 116 111 32 115 101 101 32 116 104 105 115 32 104 105 100
100 101 110 32 109 101 115 115 97 103 101 33 32


Then reverse the procedure to read it into a std::string in your program
(using a '.push_back( char( number ) );' or?).

This little obfuscation scheme is a good example. If someone runs your
program in a debugger, the encryption scheme will show. This is obfuscation
at best and will not work against any attacker who is reasonably determined
and resourcefull. If you just need to deter the casual cheater, it might
suffice; however, it will notsuffice agains a seasoned cracker or against a
company suspecting your program of containing their IP and sending their
lawers after you.


Best

Kai-Uwe Bux
 
B

BobR

Kai-Uwe Bux wrote in message ...
You could just store the string as numerical:

std::string HideThis(" don't want just "
"anybody to see this "
"hidden message! ");
// std::reverse( HideThis.begin(), HideThis.end() ); // reverse it
std::eek:fstream Fout("XFiles.txt");
if( not Fout ){ /* handle error */ }
for(size_t i(0); i < HideThis.size(); ++i){
Fout << int( HideThis.at( i ) ) << " ";
} // for(i)
Fout<<std::endl;
Fout.close();
// -- output --
32 100 111 110 39 116 32 119 97 110 116 32 106 117 115 116 32 97 110 121
98 111 100 121 32 116 111 32 115 101 101 32 116 104 105 115 32 104 105 100
100 101 110 32 109 101 115 115 97 103 101 33 32

Then reverse the procedure to read it into a std::string in your program
(using a '.push_back( char( number ) );' or?).
/* "
This little obfuscation scheme is a good example. If someone runs your
program in a debugger, the encryption scheme will show. This is obfuscation
at best and will not work against any attacker who is reasonably determined
and resourcefull. If you just need to deter the casual cheater, it might
suffice; however, it will notsuffice agains a seasoned cracker or against a
company suspecting your program of containing their IP and sending their
lawers after you.
Best
Kai-Uwe Bux
" */

I figured the OP just wanted to hide the data from casual users. Our part is
NOT to help someone rip-off some company! <G> [ there are other NGs for
that! ]

Yeah, I remember using debug to cheat games on my Apple][+. Back in those
days, I would crack the protection and hand out copies to anyone who would
take them, but, if it was not protected, my answer was "BUY your own copy!!"
[1]. I cracked everything but "Choplifter" 1/4 track non-standard write
scheme on the 5.25 floppy (I came close, but the ones that did crack it used
a network (which I didn't have)). Ahhh, the memories!!

[1] - the "protection wars" in the early '80s.
 
P

pkirk25

I AM NOT A THIEF

My question was how to prevent data teing passed on for free. I have
no interest in stealing from anyone. But if I buy data and use it in
my app, I need to be able to hide it. Not from the publisher, but from
the casual user who would love to have it all in Excel.

As such, something that would take more than an hour with a Hex editor
will be more than enough for me.

The bigger picture is that I am learning C++ and ask questions in the
hope of learning more. Its unlikely I'll make something thats fit to
sell as shareware but having a goal helps.
 
B

BobR

pkirk25 wrote in message
I AM NOT A THIEF

If i thought you were, I would *never* have offered help!
[ my comment was directed to Mr. Box. ]
My question was how to prevent data teing passed on for free. I have
no interest in stealing from anyone. But if I buy data and use it in
my app, I need to be able to hide it. Not from the publisher, but from
the casual user who would love to have it all in Excel.

As such, something that would take more than an hour with a Hex editor
will be more than enough for me.

Notice in the code I offered, I commented-out a line that will 'reverse' the
string. That will slow down anybody trying to figure it out.
Another thing you could do is add one to the number before writing it to
file, and then subtract one from the number before putting it back in a
string in your main program.
There are millions of ways to encrypt data.
The bigger picture is that I am learning C++ and ask questions in the
hope of learning more. Its unlikely I'll make something thats fit to
sell as shareware but having a goal helps.

'goals' are GOOD! Just set reasonable goals. [Attempting to write the next
major network app your first year in (C++) programming in not reasonable.].
As you achieve goals, raise the bar a little, and go to it.

A person who posts a homework assignment and wants someone to code it for
them, well...you've seen those answers. A hobbyist (or student) who posts his
code attempt will get more than enough help from the experts ( and others,
like me (if I think I can help)) here.

If you want/need more help, just post it.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top