aboutsummaryrefslogtreecommitdiff
path: root/tools/iDSK/src/Basic.cpp
blob: 6fe044da7c095c0f1fb31a25d72a0b839124752d (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
#include <iostream>
using namespace std;
#include <cmath>
#include <cstring>
#include <cstdio>
#include <ctype.h>

#include "MyType.h"
#include "Basic.h"


//static char ConvCpcFr[ 128 ] = " !\"#$%&'()*+,-./0123456789:;<=>?àABCDEFGHIJKLMNOPQRSTUVWXYZ[ç]^_`abcdefghijklmnopqrstuvwxyzéùè~";


//
// Tableau de décryptage d'un programme en basic protégé
//
static BYTE DproBasic[ 128 ] =
    {
    0xAB, 0x2C, 0xED, 0xEA, 0x6C, 0x37, 0x3F, 0xEC,
    0x9B, 0xDF, 0x7A, 0x0C, 0x3B, 0xD4, 0x6D, 0xF5,
    0x04, 0x44, 0x03, 0x11, 0xDF, 0x59, 0x8F, 0x21,
    0x73, 0x7A, 0xCC, 0x83, 0xDD, 0x30, 0x6A, 0x30,
    0xD3, 0x8F, 0x02, 0xF0, 0x60, 0x6B, 0x94, 0xE4,
    0xB7, 0xF3, 0x03, 0xA8, 0x60, 0x88, 0xF0, 0x43,
    0xE8, 0x8E, 0x43, 0xA0, 0xCA, 0x84, 0x31, 0x53,
    0xF3, 0x1F, 0xC9, 0xE8, 0xAD, 0xC0, 0xBA, 0x6D,
    0x93, 0x08, 0xD4, 0x6A, 0x2C, 0xB2, 0x07, 0x27,
    0xC0, 0x99, 0xEE, 0x89, 0xAF, 0xC3, 0x53, 0xAB,
    0x2B, 0x34, 0x5C, 0x2F, 0x13, 0xEE, 0xAA, 0x2C,
    0xD9, 0xF4, 0xBC, 0x12, 0xB3, 0xC5, 0x1C, 0x68,
    0x01, 0x20, 0x2C, 0xFA, 0x77, 0xA6, 0xB5, 0xA4,
    0xFC, 0x9B, 0xF1, 0x32, 0x5B, 0xC3, 0x70, 0x77,
    0x85, 0x36, 0xBE, 0x5B, 0x8C, 0xC8, 0xB5, 0xC2,
    0xF0, 0x0B, 0x98, 0x0F, 0x36, 0x9D, 0xD8, 0x96
    };


BYTE GetByte( BYTE * BufFile, int Pos, int Deprotect )
{
	//BYTE b = ( BYTE )( BufFile[ Pos ] ^ ( DproBasic[ Pos & 0x7F ] * Deprotect ) );
	//cout << "GetByte:"<<hex<<b<<endl;
    return( BYTE )( BufFile[ Pos ] ^ ( DproBasic[ Pos & 0x7F ] * Deprotect ) );
}


int GetWord( BYTE * BufFile, int Pos, int Deprotect )
{
    int Ret = BufFile[ Pos ] ^ ( DproBasic[ Pos & 0x7F ] * Deprotect );
    Pos++;
    Ret += ( ( BufFile[ Pos ] ^ ( DproBasic[ Pos & 0x7F ] * Deprotect ) ) << 8 );
    return( Ret );
}


//
// Ajoute un "mot" (nom d'une variable, RSX...) dans la chaine "Listing"
//
int AddWord( BYTE * BufFile, int Pos, char * Listing, int Deprotect )
{
    int LenVar = 0, l = strlen( Listing );
    BYTE b;

    do
        {
        b = GetByte( BufFile, Pos++, Deprotect );
        Listing[ l++ ] = ( char )( b & 0x7F );
        }
    while( ! ( b & 0x80 ) && LenVar++ < 0xFF );
    Listing[ l ] = 0;
    return( Pos );
}


//
// Convertir le buffer en listing basic
//
void Basic( BYTE * BufFile, char * Listing, bool IsBasic, bool CrLf )
{
    static char Tmp[ 32 ];
    int Pos = 0, Token = 0;
    int StartLigne = 0, EndLigne;
    char * p;
    double f;
    int exp;
	int Deprotect=0;
	//cout << BufFile <<endl;
    static const char * Nbre[ 11 ] =
        {
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
        };
    static const char * MotsClefs[ 0x80 ] =
        {
        "AFTER", "AUTO", "BORDER", "CALL", "CAT", "CHAIN", "CLEAR", "CLG",
        "CLOSEIN", "CLOSEOUT", "CLS", "CONT", "DATA", "DEF", "DEFINT",
        "DEFREAL", "DEFSTR", "DEG", "DELETE", "DIM", "DRAW", "DRAWR", "EDIT",
        "ELSE", "END", "ENT", "ENV", "ERASE", "ERROR", "EVERY", "FOR",
        "GOSUB", "GOTO", "IF", "INK", "INPUT", "KEY", "LET", "LINE", "LIST",
        "LOAD", "LOCATE", "MEMORY", "MERGE", "MID$", "MODE", "MOVE", "MOVER",
        "NEXT", "NEW", "ON", "ON BREAK", "ON ERROR GOTO", "SQ", "OPENIN",
        "OPENOUT", "ORIGIN", "OUT", "PAPER", "PEN", "PLOT", "PLOTR", "POKE",
        "PRINT", "'", "RAD", "RANDOMIZE", "READ", "RELEASE", "REM", "RENUM",
        "RESTORE", "RESUME", "RETURN", "RUN", "SAVE", "SOUND", "SPEED", "STOP",
        "SYMBOL", "TAG", "TAGOFF", "TROFF", "TRON", "WAIT", "WEND", "WHILE",
        "WIDTH", "WINDOW", "WRITE", "ZONE", "DI", "EI", "FILL", "GRAPHICS",
        "MASK", "FRAME", "CURSOR", "#E2", "ERL", "FN", "SPC", "STEP", "SWAP",
        "#E8", "#E9", "TAB", "THEN", "TO", "USING", ">", "=", ">=", "<", "<>",
        "<=", "+", "-", "*", "/", "^", "\\ ", "AND", "MOD", "OR", "XOR", "NOT",
        "#FF"
        };

    static const char * Fcts[ 0x80 ] =
        {
        "ABS", "ASC", "ATN", "CHR$", "CINT", "COS", "CREAL", "EXP", "FIX",
        "FRE", "INKEY", "INP", "INT", "JOY", "LEN", "LOG", "LOG10", "LOWER$",
        "PEEK", "REMAIN", "SGN", "SIN", "SPACE$", "SQ", "SQR", "STR$", "TAN",
        "UNT", "UPPER$", "VAL", "", "", "", "", "", "", "", "", "", "", "",
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
        "", "", "", "", "", "", "EOF", "ERR", "HIMEM", "INKEY$", "PI", "RND",
        "TIME", "XPOS", "YPOS", "DERR", "", "", "", "", "", "", "", "", "", "",
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
        "", "", "", "", "", "", "", "", "", "", "", "BIN$", "DEC$", "HEX$",
        "INSTR", "LEFT$", "MAX", "MIN", "POS", "RIGHT$", "ROUND", "STRING$",
        "TEST", "TESTR", "COPYCHR$", "VPOS"
        };


    * Listing = 0;
    Token = GetByte( BufFile, 0, Deprotect );
    for ( ;; )
        {
        	//cout << "Listing : " <<Listing << endl;
        if ( IsBasic )
            {
            int lg = GetWord( BufFile, Pos, Deprotect );
            Pos += 2;
            if ( ! lg )
                break;

            int NumLigne = GetWord( BufFile, Pos, Deprotect );
            Pos += 2;
            sprintf( Tmp, "%d ", NumLigne );
            strcat( Listing, Tmp );
            }
        else
            if ( ! Token || Token == 0x1A )
                break;

        int DansChaine = 0; // #### Hum, plus compliqué que ça je pense...
        do
            {
            //cout << "Tmp:"<<Tmp<<endl;
            Token = GetByte( BufFile, Pos++, Deprotect );
            if ( ! IsBasic && Token == 0x1A )
                break;

            if ( DansChaine || ! IsBasic )
                {
                Tmp[ 0 ] = ( char )Token;
                Tmp[ 1 ] = 0;
                strcat( Listing, Tmp );
                if ( Token == '"' )
                    DansChaine ^= 1;
                   // cout << " DansChaine Tmp:"<<Tmp<<endl;
                }
            else
                if ( Token > 0x7F && Token < 0xFF )
                    {
                    // #### Traitement particulier du ':' avant le ELSE
                    if (  Listing[ strlen( Listing ) - 1 ] == ':' 
                       && Token == 0x97
                       )
                        Listing[ strlen( Listing ) - 1 ] = 0;

                    strcat( Listing
                          , MotsClefs[ Token & 0x7F ]
                          );
                    }
                else
                    if ( Token >= 0x0E && Token <= 0x18 )
                        strcat( Listing
                              , Nbre[ Token - 0x0E ]
                              );
                    else
                        if ( Token >= 0x20 && Token < 0x7C )
                            {
                            Tmp[ 0 ] = ( char )Token;
                            Tmp[ 1 ] = 0;
                            strcat( Listing, Tmp );
                            if ( Token == '"' )
                                DansChaine ^= 1;
                            }
                        else
                            {
                            //cout << "Token:" << Token <<endl;
                            switch( Token )
                                {
                                case 0x01 :
                                    Tmp[ 0 ] = ':';
                                    Tmp[ 1 ] = 0;
                                    strcat( Listing, Tmp );
                                    break;

                                case 0x02 : // Variable entière (type %)
                                    Pos = AddWord( BufFile
                                                 , 2 + Pos
                                                 , Listing
                                                 , Deprotect
                                                 );
                                    strcat( Listing, "%" );
                                    break;


                                case 0x03 : // Variable chaine (type $)
                                    Pos = AddWord( BufFile
                                                 , 2 + Pos
                                                 , Listing
                                                 , Deprotect
                                                 );
                                    strcat( Listing, "$" );
                                    break;

                                case 0x04 : // Variable float (type !)
                                    Pos = AddWord( BufFile
                                                 , 2 + Pos
                                                 , Listing
                                                 , Deprotect
                                                 );
                                    strcat( Listing, "!" );
                                    break;

                                case 0x0B :
                                case 0x0C :
                                case 0x0D : // Variable "standard"
                                    Pos = AddWord( BufFile
                                                 , 2 + Pos
                                                 , Listing
                                                 , Deprotect
                                                 );
                                    break;

                                case 0x19 : // Constante entière 8 bits
									sprintf(Listing+strlen(Listing),"%d",(BYTE)GetByte( BufFile, Pos, Deprotect)); 
                                    Pos++;
                                    break;

                                case 0x1A :
                                case 0x1E : // Constante entière 16 bits
									sprintf(Listing+strlen(Listing),"%d",GetWord( BufFile, Pos, Deprotect));
                                    Pos += 2;
                                    break;

                                case 0x1B :
                                    sprintf( Tmp
                                           , "&X%X"
                                           , GetWord( BufFile, Pos, Deprotect )
                                           );
                                    strcat( Listing, Tmp );
                                    Pos += 2;
                                    break;

                                case 0x1C :
                                    sprintf( Tmp
                                           , "&%X"
                                           , GetWord( BufFile, Pos, Deprotect )
                                           );
                                    strcat( Listing, Tmp );
                                    Pos += 2;
                                    break;

                                case 0x1F : // Constante flottante
                                    f = ( GetByte( BufFile, Pos + 2, Deprotect ) << 16 )
                                      + ( GetByte( BufFile, Pos + 1, Deprotect ) << 8 )
                                      + GetByte( BufFile, Pos, Deprotect )
                                      + ( ( GetByte( BufFile, Pos + 3, Deprotect ) & 0x7F ) << 24 );
                                    f = 1 + ( f / 0x80000000 );

                                    if ( GetByte( BufFile, Pos + 3, Deprotect ) & 0x80 )
                                        f = -f;

                                    exp = GetByte( BufFile, Pos + 4, Deprotect ) - 129;
                                    Pos += 5;
                                    sprintf( Tmp, "%f", f * pow( (double) 2, exp ) );
                                    // Suppression des '0' inutiles
                                    p = &Tmp[ strlen( Tmp ) - 1 ];
                                    while( * p == '0' )
                                        * p-- = 0;

                                    if ( * p == '.' )
                                        * p = 0;

                                    strcat( Listing, Tmp );
                                    break;

                                case 0x7C :
                                    strcat( Listing, "|" );
                                    Pos = AddWord( BufFile
                                                 , 1 + Pos
                                                 , Listing
                                                 , Deprotect
                                                 );
                                    break;

                                case 0xFF :
                                    if ( GetByte( BufFile, Pos, Deprotect ) < 0x80 )
                                        strcat( Listing
                                              , Fcts[ GetByte( BufFile
                                                             , Pos++
                                                             , Deprotect
                                                             )
                                                    ]
                                              );
                                    else
                                        {
                                        Tmp[ 1 ] = 0;
                                        Tmp[ 0 ] = ( char )( GetByte( BufFile
                                                                    , Pos++
                                                                    , Deprotect
                                                                    ) & 0x7F
                                                           );
                                        strcat( Listing, Tmp );
                                        }
                                    break;

                                default :
                                    Token = Token;
                                }
                            }
            }
        while( Token );
        if ( CrLf )
            {
            //
            // Retour à la ligne si > 80 caractères
            //
            EndLigne = strlen( &Listing[ StartLigne ] );
            while( EndLigne > 80 )
                {
                memmove( &Listing[ StartLigne + 82 ]
                       , &Listing[ StartLigne + 80 ]
                       , EndLigne
                       );
                memcpy( &Listing[ StartLigne + 80 ], "\r\n", 2 );
                StartLigne += 82;
                EndLigne -= 80;
                }
            }
        strcat( Listing, "\r\n" );
        StartLigne = strlen( Listing );
        }
    // Conversion des caractères accentués si nécessaire

      for ( int i = strlen( Listing); i--; )
        {
        	//cout << i << " ";
        
            if ( ! isprint(Listing[ i ]) &&  Listing[ i ] != '\n' && Listing[ i ] != '\r'  ) Listing[ i ] = '?';  
        }
}