File: ABItype.cpp

package info (click to toggle)
gentle 1.9+cvs20100605+dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,224 kB
  • ctags: 6,214
  • sloc: cpp: 41,569; ansic: 3,978; sh: 1,420; makefile: 243
file content (259 lines) | stat: -rwxr-xr-x 6,875 bytes parent folder | download | duplicates (6)
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
/** \file
	\brief Contains the ABItype class, as well as its TFLAG helper class
*/
#include "ABItype.h"

/** \brief Constructor
*/
ABItype::ABItype ()
    {
    }
    
/** \brief Destructor, deletes flags and their associated data
*/
ABItype::~ABItype ()
    {
    for ( unsigned int a = 0 ; a < vf.size() ; a++ )
        if ( vf[a].data ) delete vf[a].data ;
    vf.clear () ;
    }

/** \brief Checks for a CMBF structure from the ABI data, returns its length
	\param t Pointer to the data to parse
	\param l Length of the data
*/
int ABItype::getCMBF ( unsigned char *t , int l )
    {
    unsigned char *s , *r = NULL ;
    for ( s = t + l - 8 ; s > t ; s-- )
       {
       if ( *s == 'C' )
          {
          if ( *(s+1) == 'M' &&
               *(s+2) == 'B' &&
               *(s+3) == 'F' )
             {
             r = s ;
             break ;
             }
          }
       }
    if ( !r ) return -1 ;
    return r - t ;
    }
    
/** \brief Parses an ABI format file
	\param filename The filename. Surprise!
	
	The "parsing" works by
	* - detecting and ignoring the Mac file header, if any
	* - finding the offset for the first CMBF structure
	* - iterating through the CMBF structures
*/
void ABItype::parse ( wxString filename )
    {
	wxFile f ( filename , wxFile::read ) ;
	long l = f.Length() ;
	unsigned char *t = new unsigned char [l+15] ;
	f.Read ( t , l ) ;
	f.Close() ;
	
	// Parsing "t"
	int macOffset = getMacOffset ( t ) ; // Trying to compensate for MAC header
	if ( macOffset == -1 ) return ; // No valid ABI file
	vf.clear () ;
    
    int pos = macOffset ;
    pos += 4 ; // ABIF
    pos += 2 ; // Unknown
    pos += 4 ; // tdir
    pos += 4 ; // Instance = 1
    pos += 2 ; // Datatype
    pos += 2 ; // Datasize
    int nrecords = getInt4 ( t , pos ) ;
    pos += 4 ; // # Bytes
    int cnt = getInt4 ( t , pos ) + macOffset ; // Pointer
    
    while ( nrecords > 0 )
	    {
        TFLAG flag = getFlag ( t , cnt ) ;
        if ( flag.nbytes > 0 )
           {
           if ( flag.nbytes > 4 )
              {
              flag.data = new unsigned char [ flag.nbytes+5 ] ;
              for ( int i = 0 ; i < flag.nbytes ; i++ )
                 flag.data[i] = t[macOffset+flag.value+i] ;
              }
           vf.push_back ( flag ) ;
           }
        nrecords-- ;
        }

	delete t ;
    }
    
/** \brief Deterimnes the position of the "ABIF" key
	\param t Pointer to the raw data
	
	This returns
 	* - 0 for a valid Windows file
 	* - 128 for a valid Mac file
 	* - -1 if the file is invalid
*/
int ABItype::getMacOffset ( unsigned char *t )
    {
    int r = 0 ;
    if ( t[r+0]=='A' && t[r+1]=='B' && t[r+2]=='I' && t[r+3]=='F' ) return r ;
    r = 128 ;
    if ( t[r+0]=='A' && t[r+1]=='B' && t[r+2]=='I' && t[r+3]=='F' ) return r ;
    return -1 ;
    }
    
/** \brief Sets a TFLAG structure from the data
	\param t Pointer to the raw data
	\param from Offset; is changed after the structure is read
*/    
TFLAG ABItype::getFlag ( unsigned char *t , int &from )
    {
    TFLAG r ;
    r.data = NULL ;
    r.pos = from ;
    r.flag = getStr ( t , from , 4 ) ; from += 4 ;
    r.instance = getInt4 ( t , from ) ;
    r.datatype = getInt2 ( t , from ) ;
    r.datasize = getInt2 ( t , from ) ;
    r.nrecords = getInt4 ( t , from ) ;
    r.nbytes = getInt4 ( t , from ) ;
    r.value = getInt4 ( t , from ) ;
    r.spare = getInt4 ( t , from ) ;
    r.after = from ;
    return r ;
    }
    
wxString ABItype::getText ( unsigned char *t , int &from )
    {
    wxString r ;
    int l = t[from++] ;
    for ( int a = 0 ; a < l ; a++ ) r += t[from++] ;
    return r ;
    }

// This function does not appear to be used anywhere...
int ABItype::getInt10 ( unsigned char *t , int &from )
    {
    from += 10 ; // Essentially ignoring
	return 0 ;
    }
    
int ABItype::getInt1 ( unsigned char *t , int &from )
    {
    return (unsigned char) t[from++] ;
    }
    
int ABItype::getInt2 ( unsigned char *t , int &from )
    {
    int r = 0 ;
    int t1 = (unsigned char) t[from++] ;
    int t2 = (unsigned char) t[from++] ;
    r = t2 + ( t1 << 8 ) ;
    return r ;
    }
    
int ABItype::getInt4 ( unsigned char *t , int &from )
    {
    int r = 0 ;
    int t1 = getInt2 ( t , from ) ;
    int t2 = getInt2 ( t , from ) ;
    r = ( t1 << 16 ) + t2 ;
    return r ;
    }
    
wxString ABItype::getStr ( unsigned char *t , int from , int len )
    {
    wxString r ;
    for ( int a = 0 ; a < len ; a++ ) r += t[from+a] ;
    return r ;
    }

//--------------------------------------------


/** \brief Finds a specific record in the parsed data
	\param id The flag (type) of the record
	\param num The number of the record
*/    
int ABItype::getRecord ( wxString id , int num )
    {
    unsigned int a ;
    for ( a = 0 ; a < vf.size() && ( vf[a].flag != id || vf[a].instance != num ) ; a++ ) ;
    if ( a == vf.size() ) return -1 ; // Not found
    return a ;
    }

/** \brief Finds a specific sequence record
	\param num The number of the sequence record
*/    
wxString ABItype::getSequence ( int num )
    {
    wxString r ;
    int a = getRecord ( _T("PBAS") , num ) , b ;
    myass ( a > -1 , _T("ABItype::getSequence") ) ;
    for ( b = 0 ; b < vf[a].nbytes ; b++ ) r += vf[a].data[b] ;
    return r ;
    }
    
/** \brief Returns the string value of a specific record in the parsed data
	\param id The flag (type) of the record
	\param num The number of the record
*/    
wxString ABItype::getRecordPascalString ( wxString id , int num )
    {
    int i = getRecord ( id , num ) ;
    if ( i == -1 ) return _T("") ;
    return vf[i].getPascalString() ;
    }

/** \brief Returns the (integer) value of a specific record in the parsed data
	\param id The flag (type) of the record
	\param num The number of the record
*/    
int ABItype::getRecordValue ( wxString id , int num )
    {
    int i = getRecord ( id , num ) ;
    if ( i == -1 ) return 0 ;
    return vf[i].value ;
    }
    
    
//***************************************

/// Constructor
TFLAG::TFLAG ()
    {
    }

/// Destructor
TFLAG::~TFLAG ()
    {
    }    

/// \brief Reads a Pascal-like string from the data   
wxString TFLAG::getPascalString ()
    {
    if ( !data )
       {
       char t[5] ;
//       t[0] = ( value >> 24 ) & 255 ;
       t[1] = ( value >> 16 ) & 255 ;
       t[2] = ( value >>  8 ) & 255 ;
       t[3] = ( value >>  0 ) & 255 ;
       t[4] = 0 ;
       return wxString ( t+1 , wxConvUTF8 ) ;
       }
    wxString r ;
    int len = (unsigned char) data[0] ;
    for ( int a = 1 ; a <= len ; a++ ) r += data[a] ;
    return r ;
    }