summaryrefslogtreecommitdiffstats
path: root/src/build/tocgen/mkpnortoc.pl
blob: f4e5464f55e7ba1f9f1053c70c64ffb405ba3412 (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
#!/usr/bin/perl
# File mkPnorTOC.pl created by ADAM R. MUHLE at 14:39:27 on Mon Aug  1 2011.

#Limitations to address later
# number fields must be 4 or 8 bytes
# numbers cannot be over 32 bits

#@TODO - enable "use strict"
#@TODO - standardize variable names, i_blah for input, etc.

use XML::LibXML;
#use Data::Dumper;
use File::Basename;

my $programName = File::Basename::basename $0;
my $g_trace = 0; # >0 enable traces
$tocDataFile = "";
$tocOutFile = "";

if ($#ARGV < 0) {
    usage();
    exit;
}


#Parse the commandline args
for ($i=0; $i < $#ARGV + 1; $i++)
{
    if ($ARGV[$i] =~ /-h/) {
    usage();
    exit 0;
    }
    elsif($ARGV[$i] =~ /-v/) {
	$verbose = 1;
    }
    elsif($ARGV[$i] =~ /-i/) {
	$tocDataFile = $ARGV[++$i];
	trace(1, "Input Data File=$tocDataFile");
    }
    elsif($ARGV[$i] =~ /-o/) {
	$tocOutFile = $ARGV[++$i];
	trace(1, "Output Binary File=$tocOutFile");
    }
    else {
	#Error!!
    }
}


#open output file
open( BIN_TOC_FILE, ">:raw", $tocOutFile) or die "Can't open $tocOutFile file for writing";

#parse the input XML file
$parser = XML::LibXML->new();
$doc = $parser->parse_file($tocDataFile);

#add the SBE field to the TOC
$sbeNodes = $doc->getElementsByTagName("sbeLoc");
$onlySbeNode = $sbeNodes->pop();
writeElementToBinFile(BIN_TOC_FILE, $onlySbeNode);

#Add TOC version field to TOC
$tocVerNodes = $doc->getElementsByTagName("tocVersion");
$onlytocVerNode = $tocVerNodes->pop();
writeElementToBinFile(BIN_TOC_FILE, $onlytocVerNode);


#Add the individual TOC entries
$root = $doc->firstChild;
$curSibling = $root->firstChild;

do {
    #just in case the first childe is bad
    if(!$curSibling)
    {
	last;
    }
    elsif($curSibling->nodeName eq "tocEntry")
    {
	parseTocEntry(BIN_TOC_FILE, $curSibling);
    }
    elsif(($curSibling->nodeName eq "sbeLoc") ||
	  ($curSibling->nodeName eq "tocVersion"))
    {
	#skip these, already inserted above
    }

}while($curSibling = $curSibling->nextSibling);


close(BIN_TOC_FILE);
exit;

#################################################
# Get Length of a field
#  Searches XML element for sub-element called
#    length
#################################################
sub getLength
{
    $element = @_[0];

    $lenElements = $element->getElementsByLocalName("length");
    $lenEl = $lenElements->pop();

    $len = $lenEl->firstChild->data;
    chomp $len;
    return $len;

}

#################################################
# Get format of a field
#  Searches XML element for sub-element called
#    format
#################################################
sub getFormat
{
    $element = @_[0];

    $formatElements = $element->getElementsByLocalName("format");
    $formatEl = $formatElements->pop();

    $format = $formatEl->firstChild->data;
    chomp $format;
    return $format;

}


#################################################
# Get data value from provided XML element
#  
#################################################
sub getValue
{
    $i_element = @_[0];

    $value = $i_element->firstChild->data;

    $value =~s/\s+$//;
    chomp $value;

    return $value;
}
    
#################################################
# parse <tocEntry> node, write bin data to file
#  
#################################################
sub parseTocEntry
{
    ($i_FILEHANDLE, $i_element) = @_;
    $entryLength = 0;
    $sumFieldLen = 0;
    local $curSibling;
    

    $curSibling = $i_element->firstChild;
    do {
	#just in case the first childe is bad
	if(!$curSibling)
	{
	    last;
	}
	elsif(($curSibling->nodeName eq "text") ||
	      ($curSibling->nodeName eq "#text"))
	{
	    trace(1, "ignoring ".$curSibling->nodeName);
	}
	elsif($curSibling->nodeName eq "length")
	{
	    $entryLength = $curSibling->firstChild->data;

	}
	else
	{
	    trace(1, "Parsing ".$curSibling->nodeName);
	    $fieldLen = writeElementToBinFile($i_FILEHANDLE, $curSibling);
	    $sumFieldLen = $sumFieldLen + $fieldLen;

	}
#       ($nextSibling = $curSibling->nextSibling)

    }while($curSibling = $curSibling->nextSibling);

    if($sumFieldLen > $entryLength)
    {
	trace(0, "Fields in TOC Entry consumed more space (".$sumFieldLen.") then they were supposed to (".$entryLength.")\n Aborting");
	exit 1;
    }
    elsif($sumFieldLen < $entryLength)
    {
	trace(1, "Need to insert padding to fill up TOC entry space");
	$padBytes = $entryLength - $sumFieldLen;
	insertPadBytes($i_FILEHANDLE, $padBytes);
    }
    

}
#################################################
# write provided element to binary file
#  
#################################################
sub writeElementToBinFile
{
    ($i_FILEHANDLE, $i_element) = @_;

    $elValue = getValue($i_element);
    $len = getLength($i_element);
    $format = getFormat($i_element);

    trace(1, "Value=".$elValue." Length=".$len." Format=".$format);


    if($format =~ "string")
    {
	#print in ascii
	#pad on right side
	@charArray = unpack("C*", $elValue);
	foreach $char (@charArray)
	{
	    print $i_FILEHANDLE pack('C', $char);
	}

	$zeroPad = $len-length($elValue);
	insertPadBytes($i_FILEHANDLE, $zeroPad);
    }
    elsif ($format =~ "number")
    {
	if(($len != 4) &&
	   ($len != 8))
	{
	    trace(0, "field <length>=".$len.".  Currently only lengths of 4 or 8 are supported. \n Aborting program");
	    exit 1;
	}

	if($len == 8)
	{
	    print $i_FILEHANDLE pack('N', 0);
	}

	#check if we have a hex number
	if($elValue =~ "0x")
	{
	    $num = oct($elValue);
	}
	else
	{
	    $num = $elValue;
	}

	#verify number consumes less than 32 bits
	if($num > 0xFFFFFFFF)
	{
	    trace(0, "number=".$num.".  This is greater than 32 bits in hex and not currently supported!. \n Aborting program");
	    exit 1;
	}

	print $i_FILEHANDLE pack('N', $num);

    }
    else
    {
	print "ERROR: Unrecognized <format> type: ".$format."  Exiting!\n";
	exit 1;
    }

    return $len

}

#################################################
# Insert specifed number of pad bytes into file
#  
#################################################
sub insertPadBytes
{
    ($i_FILEHANDLE, $i_padBytes) = @_;
    for($i=0; $i<$i_padBytes; $i++)
    {
	print $i_FILEHANDLE pack('C', 0);
    }
}




################################################################################
# trace
################################################################################
sub trace
{
    ($i_traceLevel, $i_string) = @_;

    #traceLevel 0 is for errors
    if($i_traceLevel == 0)
    {
	print "ERROR: ".$i_string."\n";
    }
    elsif ($g_trace >= $i_traceLevel)
    {
	print "TRACE: ".$i_string."\n";
    }
}

################################################################################
# print usage instructions
################################################################################
sub usage
{
print <<"ENDUSAGE";
  $programName = Creates the PNOR Table of Contents (TOC) based on input XML file.

  Usage:
      $programName -i <XML File> -o <binary Output File>

  Current Limitations:
      --Hex/number fields must be 4 or 8 bytes in length
      --Numbers cannot be over 32 bits (in hex)
     The script will print and error and exit with a non-zero return code if these
     conditions are encountered.

ENDUSAGE
}
OpenPOWER on IntegriCloud