summaryrefslogtreecommitdiff
path: root/src/tools/bin2cpp.cpp
blob: 65e48ed4a5651a192be12b43c855e56268eeceae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <bu/sio.h>
#include <bu/optparser.h>
#include <bu/file.h>
#include <bu/streamstack.h>
#include <bu/strfilter.h>
#include <bu/taf.h>
#include <bu/autoconfig.h>

#ifdef BU_HAS_DEFLATE
#include <bu/deflate.h>
#endif
#ifdef BU_HAS_BZIP2
#include <bu/bzip2.h>
#endif
#ifdef BU_HAS_LZMA
#include <bu/lzma.h>
#endif

#include <bu/base64.h>
#include <bu/hex.h>

Bu::String cescape( const Bu::String &sIn )
{
    Bu::String sOut;
    for( Bu::String::const_iterator i = sIn.begin(); i; i++ )
    {
        sOut += Bu::String("\\x%1").arg( (int)*i, Bu::Fmt(2).hex() );
    }
    return sOut;
}

using namespace Bu;

class Options : public OptParser
{
public:
    Options( int argc, char *argv[] ) :
        sClass("Datafiles")
    {
        addHelpBanner("bin2cpp - convert files into executable-embeddable C++ code.\n");
        addHelpBanner("Each file in the input is loaded, filtered according to your options, and written as stack allocated, static variables in a generated class.  You can then access the files as though they were on disk through that class.");
        addHelpBanner("\nUsage: bin2cpp [options] [input1] [input2] [...] [inputN]");
        addHelpBanner(  "   Or: bin2cpp -s <taf spec file>\n");
        addOption( sClass, 'c', "Class name [default=\"Datafiles\"]");
        addOption( sOutBase, 'o', "Output base filename [defaults to classname]");
        addOption( sOutDir, 'd', "Output directory [defaults to current dir]");
        addOption( slot(this, &Options::addFilter), 'f', "Add filter: "
#ifdef BU_HAS_DEFLATE
            "deflate, "
#endif
#ifdef BU_HAS_BZIP2
            "bzip2, "
#endif
#ifdef BU_HAS_LZMA
            "lzma, "
#endif
            "base64, hex");
        addOption( sSpecFile, 's', "Use the specified spec file instead of providing options on the command line.  If you use this option all others are ignored.");
        setNonOption( slot(this, &Options::addInput) );
        addHelpOption();

        parse( argc, argv );

        if( !sOutBase.isSet() )
            sOutBase = sClass.toLower();
        if( sOutDir.isSet() )
            sOutDir += "/";
    }

    virtual ~Options()
    {
    }

    int addFilter( Bu::StrArray aArgs )
    {
        slFilter.append( aArgs[1] );
        return 1;
    }

    int addInput( Bu::StrArray aArgs )
    {
        slInput.append( aArgs[0] );
        return 0;
    }

    Bu::String sClass;
    Bu::String sOutBase;
    Bu::String sOutDir;
    Bu::StringList slInput;
    Bu::StringList slFilter;
    Bu::String sSpecFile;
};

int main( int argc, char *argv[] )
{
    Options opt( argc, argv );

    if( !opt.sSpecFile.isEmpty() )
    {
        Bu::File fTaf( opt.sSpecFile, Bu::File::Read );
        Bu::TafReader rTaf( fTaf );
        Bu::TafGroup *pRoot = rTaf.readGroup();

        if( pRoot == NULL || pRoot->getName() != "bin2cpp" )
        {
            sio << "Specfied spec file does not appear to be a bin2cpp taf "
                "specifications file." << sio.nl;
            return 5;
        }

        opt.sOutBase = opt.sClass = pRoot->getProperty("class");
        if( pRoot->hasProperty("output") )
            opt.sOutBase = pRoot->getProperty("output");
        opt.sOutDir = pRoot->getProperty("dir", ".") + "/";

        delete pRoot;
    }

    File fHdrOut( opt.sOutDir + opt.sOutBase + ".h", File::WriteNew );
    File fSrcOut( opt.sOutDir + opt.sOutBase + ".cpp", File::WriteNew );

    Bu::Hash<char, bool> hFilters;

    Formatter fHdr( fHdrOut );
    Formatter fSrc( fSrcOut );
    fHdr << "#ifndef BIN2CPP_" << opt.sClass.toUpper() << "_H" << fHdr.nl
        << "#define BIN2CPP_" << opt.sClass.toUpper() << "_H" << fHdr.nl << fHdr.nl
        << "#include <bu/string.h>" << fHdr.nl
        << "#include <bu/streamstack.h>" << fHdr.nl
        << fHdr.nl
        << "class " << opt.sClass << fHdr.nl
        << "{" << fHdr.nl
        << "public:" << fHdr.nl
        << "\tclass File { public: int iSize; const char *data; const char *flt; };" << fHdr.nl << fHdr.nl
        << "\tstatic const File &getFile( const Bu::String &sName );" << fHdr.nl
        << "\tstatic Bu::StreamStack *open( const Bu::String &sName );" << fHdr.nl
        << "\tstatic Bu::StreamStack *openRaw( const Bu::String &sName );" << fHdr.nl
        << "\tstatic Bu::String getString( const Bu::String &sName );" << fHdr.nl
        << "\tstatic Bu::String getStringRaw( const Bu::String &sName );" << fHdr.nl
        << fHdr.nl;
    fHdr << "public:" << fHdr.nl
        << "\tstatic const File aFile[];" << fHdr.nl
        << "};" << fHdr.nl << fHdr.nl;
    fHdr << "#endif";

    fSrc << "#include \"" << opt.sOutBase << ".h\"" << fSrc.nl;
#ifdef BU_HAS_DEFLATE
    fSrc << "#include <bu/deflate.h>" << fSrc.nl;
#endif
#ifdef BU_HAS_BZIP2
    fSrc << "#include <bu/bzip2.h>" << fSrc.nl;
#endif
#ifdef BU_HAS_LZMA
    fSrc << "#include <bu/lzma.h>" << fSrc.nl;
#endif
    fSrc << "#include <bu/base64.h>" << fSrc.nl;
    fSrc << "#include <bu/hex.h>" << fSrc.nl;
    fSrc << "#include <bu/strfilter.h>" << fSrc.nl;
    fSrc << "#include <bu/staticmembuf.h>" << fSrc.nl;
    fSrc << fSrc.nl;
    fSrc << "const " << opt.sClass << "::File " << opt.sClass << "::aFile[] = {" << fSrc.nl;

    for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ )
    {
        File fIn( *i, File::Read );
        Bu::String sDat;
        char buf[1024];
        while( !fIn.isEos() )
        {
            sDat.append( buf, fIn.read( buf, 1024 ) );
        }

        Bu::String sFltDesc;
        for( Bu::StringList::iterator f = opt.slFilter.begin(); f; f++ )
        {
            if( false ) { } // should optomize out
#ifdef BU_HAS_DEFLATE
            else if( *f == "deflate" )
            {
                sDat = encodeStr<Deflate>( sDat );
                sFltDesc.prepend("d");
                hFilters.insert('d', true );
            }
#endif
#ifdef BU_HAS_BZIP2
            else if( *f == "bzip2" )
            {
                sDat = encodeStr<BZip2>( sDat );
                sFltDesc.prepend("b");
                hFilters.insert('b', true );
            }
#endif
#ifdef BU_HAS_LZMA
            else if( *f == "lzma" )
            {
                sDat = encodeStr<Lzma>( sDat );
                sFltDesc.prepend("l");
                hFilters.insert('l', true );
            }
#endif
            else if( *f == "base64" )
            {
                sDat = encodeStr<Base64>( sDat );
                sFltDesc.prepend("6");
                hFilters.insert('6', true );
            }
            else if( *f == "hex" )
            {
                sDat = encodeStr<Hex>( sDat );
                sFltDesc.prepend("h");
                hFilters.insert('h', true );
            }
            else
            {
                sio << "No known filter named " << *f << sio.nl;
                return 1;
            }
        }
        
        fSrc << "  {" << sDat.getSize() << ", \"";

		for( Bu::String::iterator j = sDat.begin(); j; j++ )
		{
			fSrc << "\\x" << Fmt::hex() << (unsigned char)*j;
		}
		fSrc << "\", \"" << sFltDesc << "\"}," << fSrc.nl;
    }
    fSrc << "};" << fSrc.nl << fSrc.nl;

    fSrc << "const " << opt.sClass << "::File &" << opt.sClass << "::getFile( const Bu::String &sName )"
        << fSrc.nl
        << "{" << fSrc.nl
        << "\tswitch( Bu::__calcHashCode( sName ) )" << fSrc.nl
        << "\t{" << fSrc.nl;

    int idx = 0;
    {
        typedef Bu::List<Bu::StringList::iterator> IterList;
        typedef Bu::Hash<uint32_t, IterList> IterHashList;
        typedef Bu::Hash<Bu::String, int> IdxHash;

        IdxHash hIdx;
        IterHashList hCodes;
        for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ )
        {
            uint32_t uc = Bu::__calcHashCode( *i );
            if( !hCodes.has( uc ) )
            {
                hCodes.insert( uc, IterList() );
            }
            hCodes.get( uc ).append( i );
            hIdx.insert( *i, idx++ );
        }
        for( IterHashList::iterator r = hCodes.begin(); r; r++ )
        {
            fSrc << "\t\tcase " << r.getKey() << "UL:" << fSrc.nl;
            for( IterList::iterator i = r.getValue().begin(); i; i++ )
            {
                fSrc << "\t\t\tif( sName == \"" << cescape(*(*i)) << "\" ) // "
                    << *(*i) << fSrc.nl
                    << "\t\t\t\treturn aFile[" << hIdx.get(*(*i)) << "];" << fSrc.nl;
            }
            fSrc << "\t\t\tbreak;" << fSrc.nl;
        }
    }
    fSrc << "\t}" << fSrc.nl
        << "\tthrow Bu::ExceptionBase(\"No file matching \\\"%s\\\" found.\", sName.getStr() );" << fSrc.nl
        << "}" << fSrc.nl << fSrc.nl;
    
    fSrc << "Bu::StreamStack *" << opt.sClass << "::open( const Bu::String &sName )" << fSrc.nl
        << "{" << fSrc.nl
        << "\tconst File &f = getFile( sName );" << fSrc.nl
        << "\tBu::StreamStack *s = new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl;

    if( !hFilters.isEmpty() )
    {
        fSrc << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl
            << "\t{" << fSrc.nl
            << "\t\tswitch( *t )" << fSrc.nl
            << "\t\t{" << fSrc.nl;
        if( hFilters.has('d') )
            fSrc << "\t\t\tcase 'd': s->pushFilter<Bu::Deflate>(); break;" << fSrc.nl;
        if( hFilters.has('b') )
            fSrc << "\t\t\tcase 'b': s->pushFilter<Bu::BZip2>(); break;" << fSrc.nl;
        if( hFilters.has('l') )
            fSrc << "\t\t\tcase 'l': s->pushFilter<Bu::Lzma>(); break;" << fSrc.nl;
        if( hFilters.has('6') )
            fSrc << "\t\t\tcase '6': s->pushFilter<Bu::Base64>(); break;" << fSrc.nl;
        if( hFilters.has('h') )
            fSrc << "\t\t\tcase 'h': s->pushFilter<Bu::Hex>(); break;" << fSrc.nl;
        fSrc << "\t\t}" << fSrc.nl
            << "\t}" << fSrc.nl;
    }
    fSrc << "\treturn s;" << fSrc.nl
        << "}" << fSrc.nl << fSrc.nl;

    fSrc << "Bu::StreamStack *" << opt.sClass << "::openRaw( const Bu::String &sName )" << fSrc.nl
        << "{" << fSrc.nl
        << "\tconst File &f = getFile( sName );" << fSrc.nl
        << "\treturn new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl
        << "}" << fSrc.nl << fSrc.nl;

    fSrc << "Bu::String " << opt.sClass << "::getString( const Bu::String &sName )" << fSrc.nl
        << "{" << fSrc.nl
        << "\tBu::StreamStack *ss = open( sName );" << fSrc.nl
        << "\tBu::String s;" << fSrc.nl
        << "\tchar buf[1024];" << fSrc.nl
        << "\twhile( !ss->isEos() )" << fSrc.nl
        << "\t{" << fSrc.nl
        << "\t\ts.append( buf, ss->read( buf, 1024 ) );" << fSrc.nl
        << "\t}" << fSrc.nl
        << "\tdelete ss;" << fSrc.nl
        << "\treturn s;" << fSrc.nl
        << "}" << fSrc.nl << fSrc.nl;
    
    fSrc << "Bu::String " << opt.sClass << "::getStringRaw( const Bu::String &sName )" << fSrc.nl
        << "{" << fSrc.nl
        << "\tconst File &f = getFile( sName );" << fSrc.nl
        << "\treturn Bu::String( f.data, f.iSize );" << fSrc.nl
        << "}" << fSrc.nl << fSrc.nl;

    return 0;
}