summaryrefslogtreecommitdiff
path: root/plugins/sid/sidplay-libs/libsidplay/src/sidtune/InfoFile.cpp
blob: 0c03b23c0142efe9122872ac507c5f72b50e0bd1 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * /home/ms/files/source/libsidtune/RCS/InfoFile.cpp,v
 *
 * SIDPLAY INFOFILE format support.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"

#ifdef HAVE_EXCEPTIONS
#   include <new>
#endif
#include <iostream>
#include <iomanip>
#include <sstream>
#include <ctype.h>
#include <string.h>

#include "SidTune.h"
#include "SidTuneTools.h"
#include "sidendian.h"


const char text_format[] = "Raw plus SIDPLAY ASCII text file (SID)";

const char text_truncatedError[] = "ERROR: SID file is truncated";
const char text_noMemError[] = "ERROR: Not enough free memory";

const char keyword_id[] = "SIDPLAY INFOFILE";

const char keyword_name[] = "NAME=";            // No white-space characters 
const char keyword_author[] = "AUTHOR=";        // in these keywords, because
const char keyword_copyright[] = "COPYRIGHT=";  // we want to use a white-space
const char keyword_released[] = "RELEASED=";    // we want to use a white-space
const char keyword_address[] = "ADDRESS=";      // eating string stream to
const char keyword_songs[] = "SONGS=";          // parse most of the header.
const char keyword_speed[] = "SPEED=";
const char keyword_musPlayer[] = "SIDSONG=YES";
const char keyword_reloc[] = "RELOC=";
const char keyword_clock[] = "CLOCK=";
const char keyword_sidModel[] = "SIDMODEL=";
const char keyword_compatibility[] = "COMPATIBILITY=";

const uint_least16_t sidMinFileSize = 1+sizeof(keyword_id);  // Just to avoid a first segm.fault.
const uint_least16_t parseChunkLen = 80;                     // Enough for all keywords incl. their values.


bool SidTune::SID_fileSupport(const void* dataBuffer, uint_least32_t dataBufLen,
                              const void* sidBuffer, uint_least32_t sidBufLen)
{
    // Make sure SID buffer pointer is not zero.
    // Check for a minimum file size. If it is smaller, we will not proceed.
    if ((sidBuffer==0) || (sidBufLen<sidMinFileSize))
    {
        return false;
    }

    const char* pParseBuf = (const char*)sidBuffer;
    // First line has to contain the exact identification string.
    if ( SidTuneTools::myStrNcaseCmp( pParseBuf, keyword_id ) != 0 )
    {
        return false;
    }
    else
    {
        // At least the ID was found, so set a default error message.
        info.formatString = text_truncatedError;
        
        // Defaults.
        fileOffset = 0;                // no header in separate data file
        info.sidChipBase1 = 0xd400;
        info.sidChipBase2 = 0;
        info.musPlayer = false;
        info.numberOfInfoStrings = 0;
        uint_least32_t oldStyleSpeed = 0;

        // Flags for required entries.
        bool hasAddress = false,
            hasName = false,
            hasAuthor = false,
            hasReleased = false,
            hasSongs = false,
            hasSpeed = false;
    
        // Using a temporary instance of an input string chunk.
#ifdef HAVE_EXCEPTIONS
        char* pParseChunk = new(std::nothrow) char[parseChunkLen+1];
#else
        char* pParseChunk = new char[parseChunkLen+1];
#endif
        if ( pParseChunk == 0 )
        {
            info.formatString = text_noMemError;
            return false;
        }
        
        // Parse as long we have not collected all ``required'' entries.
        //while ( !hasAddress || !hasName || !hasAuthor || !hasCopyright
        //        || !hasSongs || !hasSpeed )

        // Above implementation is wrong, we need to get all known
        // fields and then check if all ``required'' ones were found.
        for (;;)
        {
            // Skip to next line. Leave loop, if none.
            if (( pParseBuf = SidTuneTools::returnNextLine( pParseBuf )) == 0 )
            {
                break;
            }
            // And get a second pointer to the following line.
            const char* pNextLine = SidTuneTools::returnNextLine( pParseBuf );
            uint_least32_t restLen;
            if ( pNextLine != 0 )
            {
                // Calculate number of chars between current pos and next line.
                restLen = (uint_least32_t)(pNextLine - pParseBuf);
            }
            else
            {
                // Calculate number of chars between current pos and end of buf.
                restLen = sidBufLen - (uint_least32_t)(pParseBuf - (char*)sidBuffer);
            }
            // Create whitespace eating (!) input string stream.

            std::string s (pParseBuf, restLen);

            std::istringstream parseStream (s);
            // A second one just for copying.
            std::istringstream parseCopyStream (s);
            if ( !parseStream || !parseCopyStream )
            {
                break;
            }
            // Now copy the next X characters except white-spaces.
            for ( uint_least16_t i = 0; i < parseChunkLen; i++ )
            {
                char c;
                parseCopyStream >> c;
                pParseChunk[i] = c;
            }
            pParseChunk[parseChunkLen]=0;
            // Now check for the possible keywords.
            // ADDRESS
            if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_address ) == 0 )
            {
                SidTuneTools::skipToEqu( parseStream );
                info.loadAddr = (uint_least16_t)SidTuneTools::readHex( parseStream );
                if ( !parseStream )
                    break;
                info.initAddr = (uint_least16_t)SidTuneTools::readHex( parseStream );
                if ( !parseStream )
                    break;
                info.playAddr = (uint_least16_t)SidTuneTools::readHex( parseStream );
                hasAddress = true;
            }
            // NAME
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_name ) == 0 )
            {
                SidTuneTools::copyStringValueToEOL(pParseBuf,&infoString[0][0],SIDTUNE_MAX_CREDIT_STRLEN);
                info.infoString[0] = &infoString[0][0];
                hasName = true;
            }
            // AUTHOR
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_author ) == 0 )
            {
                SidTuneTools::copyStringValueToEOL(pParseBuf,&infoString[1][0],SIDTUNE_MAX_CREDIT_STRLEN);
                info.infoString[1] = &infoString[1][0];
                hasAuthor = true;
            }
            // COPYRIGHT
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_copyright ) == 0 )
            {
                SidTuneTools::copyStringValueToEOL(pParseBuf,&infoString[2][0],SIDTUNE_MAX_CREDIT_STRLEN);
                info.infoString[2] = &infoString[2][0];
                hasReleased = true;
            }
            // RELEASED
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_released ) == 0 )
            {
                SidTuneTools::copyStringValueToEOL(pParseBuf,&infoString[2][0],SIDTUNE_MAX_CREDIT_STRLEN);
                info.infoString[2] = &infoString[2][0];
                hasReleased = true;
            }
            // SONGS
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_songs ) == 0 )
            {
                SidTuneTools::skipToEqu( parseStream );
                info.songs = (uint_least16_t)SidTuneTools::readDec( parseStream );
                info.startSong = (uint_least16_t)SidTuneTools::readDec( parseStream );
                hasSongs = true;
            }
            // SPEED
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_speed ) == 0 )
            {
                SidTuneTools::skipToEqu( parseStream );
                oldStyleSpeed = SidTuneTools::readHex(parseStream);
                hasSpeed = true;
            }
            // SIDSONG
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_musPlayer ) == 0 )
            {
                info.musPlayer = true;
            }
            // RELOC
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_reloc ) == 0 )
            {
                info.relocStartPage = (uint_least8_t)SidTuneTools::readHex( parseStream );
                if ( !parseStream )
                    break;
                info.relocPages = (uint_least8_t)SidTuneTools::readHex( parseStream );
            }
            // CLOCK
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_clock ) == 0 )
            {
                char clock[8];
                SidTuneTools::copyStringValueToEOL(pParseBuf,clock,sizeof(clock));
                if ( SidTuneTools::myStrNcaseCmp( clock, "UNKNOWN" ) == 0 )
                    info.clockSpeed = SIDTUNE_CLOCK_UNKNOWN;
                else if ( SidTuneTools::myStrNcaseCmp( clock, "PAL" ) == 0 )
                    info.clockSpeed = SIDTUNE_CLOCK_PAL;
                else if ( SidTuneTools::myStrNcaseCmp( clock, "NTSC" ) == 0 )
                    info.clockSpeed = SIDTUNE_CLOCK_NTSC;
                else if ( SidTuneTools::myStrNcaseCmp( clock, "ANY" ) == 0 )
                    info.clockSpeed = SIDTUNE_CLOCK_ANY;
            }
            // SIDMODEL
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_sidModel ) == 0 )
            {
                char model[8];
                SidTuneTools::copyStringValueToEOL(pParseBuf,model,sizeof(model));
                if ( SidTuneTools::myStrNcaseCmp( model, "UNKNOWN" ) == 0 )
                    info.sidModel = SIDTUNE_SIDMODEL_UNKNOWN;
                else if ( SidTuneTools::myStrNcaseCmp( model, "6581" ) == 0 )
                    info.sidModel = SIDTUNE_SIDMODEL_6581;
                else if ( SidTuneTools::myStrNcaseCmp( model, "8580" ) == 0 )
                    info.sidModel = SIDTUNE_SIDMODEL_8580;
                else if ( SidTuneTools::myStrNcaseCmp( model, "ANY" ) == 0 )
                    info.sidModel = SIDTUNE_SIDMODEL_ANY;
            }
            // COMPATIBILITY
            else if ( SidTuneTools::myStrNcaseCmp( pParseChunk, keyword_compatibility ) == 0 )
            {
                char comp[5];
                SidTuneTools::copyStringValueToEOL(pParseBuf,comp,sizeof(comp));
                if ( SidTuneTools::myStrNcaseCmp( comp, "C64" ) == 0 )
                    info.compatibility = SIDTUNE_COMPATIBILITY_C64;
                else if ( SidTuneTools::myStrNcaseCmp( comp, "PSID" ) == 0 )
                    info.compatibility = SIDTUNE_COMPATIBILITY_PSID;
                else if ( SidTuneTools::myStrNcaseCmp( comp, "R64" ) == 0 )
                    info.compatibility = SIDTUNE_COMPATIBILITY_R64;
            }
        };
        
        delete[] pParseChunk;
        
        // Again check for the ``required'' values.
        if ( hasAddress || hasName || hasAuthor || hasReleased || hasSongs || hasSpeed )
        {
            // Check reserved fields to force real c64 compliance
            if (info.compatibility == SIDTUNE_COMPATIBILITY_R64)
            {
                if (checkRealC64Info (oldStyleSpeed) == false)
                    return false;
            }
            // Create the speed/clock setting table.
            convertOldStyleSpeedToTables(oldStyleSpeed, info.clockSpeed);
            // loadAddr = 0 means, the address is stored in front of the C64 data.
            // We cannot verify whether the dataBuffer contains valid data.
            // All we want to know is whether the SID buffer is valid.
            // If data is present, we access it (here to get the C64 load address).
            if (info.loadAddr==0 && (dataBufLen>=(fileOffset+2)) && dataBuffer!=0)
            {
                const uint8_t* pDataBufCp = (const uint8_t*)dataBuffer + fileOffset;
                info.loadAddr = endian_16( *(pDataBufCp + 1), *pDataBufCp );
                fileOffset += 2;  // begin of data
            }
            // Keep compatibility to PSID/SID.
            if ( info.initAddr == 0 )
            {
                info.initAddr = info.loadAddr;
            }
            info.numberOfInfoStrings = 3;
            // We finally accept the input data.
            info.formatString = text_format;
            return true;
        }
        else
        {
            // Something is missing (or damaged ?).
            // Error string set above.
            return false;
        }
    }
}


bool SidTune::SID_fileSupportSave( std::ofstream& toFile )
{
    toFile << keyword_id << std::endl
        << keyword_address << std::hex << std::setw(4)
        << std::setfill('0') << 0 << ','
        << std::hex << std::setw(4) << info.initAddr << ","
        << std::hex << std::setw(4) << info.playAddr << std::endl
        << keyword_songs << std::dec << (int)info.songs << ","
        << (int)info.startSong << std::endl;

    uint_least32_t oldStyleSpeed = 0;
    int maxBugSongs = ((info.songs <= 32) ? info.songs : 32);
    for (int s = 0; s < maxBugSongs; s++)
    {
        if (songSpeed[s] == SIDTUNE_SPEED_CIA_1A)
        {
            oldStyleSpeed |= (1<<s);
        }
    }

    toFile
        << keyword_speed << std::hex << std::setw(8)
        << oldStyleSpeed << std::endl
        << keyword_name << info.infoString[0] << std::endl
        << keyword_author << info.infoString[1] << std::endl
        << keyword_released << info.infoString[2] << std::endl;
    if ( info.musPlayer )
    {
        toFile << keyword_musPlayer << std::endl;
    }
    if ( info.relocStartPage )
    {
        toFile
            << keyword_reloc << std::setfill('0')
            << std::hex << std::setw(2) << (int) info.relocStartPage << ","
            << std::hex << std::setw(2) << (int) info.relocPages << std::endl;
    }
    if ( info.clockSpeed != SIDTUNE_CLOCK_UNKNOWN )
    {
        toFile << keyword_clock;
        switch (info.clockSpeed)
        {
        case SIDTUNE_CLOCK_PAL:
            toFile << "PAL";
            break;
        case SIDTUNE_CLOCK_NTSC:
            toFile << "NTSC";
            break;
        case SIDTUNE_CLOCK_ANY:
            toFile << "ANY";
            break;
        }
        toFile << std::endl;
    }
    if ( info.sidModel != SIDTUNE_SIDMODEL_UNKNOWN )
    {
        toFile << keyword_sidModel;
        switch (info.sidModel)
        {
        case SIDTUNE_SIDMODEL_6581:
            toFile << "6581";
            break;
        case SIDTUNE_SIDMODEL_8580:
            toFile << "8580";
            break;
        case SIDTUNE_SIDMODEL_ANY:
            toFile << "ANY";
            break;
        }
        toFile << std::endl;
    }
    if ( info.compatibility == SIDTUNE_COMPATIBILITY_PSID )
    {
        toFile << keyword_compatibility << "C64" << std::endl;
    }
    else if ( info.compatibility == SIDTUNE_COMPATIBILITY_R64 )
    {
        toFile << keyword_compatibility << "R64" << std::endl;
    }

    if ( !toFile )
    {
        return false;
    }
    else
    {
        return true;
    }
}