First public contribution.
2 # Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
4 # This component and the accompanying materials are made available
5 # under the terms of "Eclipse Public License v1.0"
6 # which accompanies this distribution, and is available
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 # Initial Contributors:
10 # Nokia Corporation - initial contribution.
23 @WRITER::ISA=qw(Exporter);
24 @WRITER::EXPORT=qw(write8 write16 write32 writePositiveIntegerCompacted15 writePositiveIntegerCompacted30 writeSignedIntegerCompacted29 writeString writeUids);
30 if ($integer&0xffffff00)
32 die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 8 bits\n");
34 printf $fileHandle "%c", $integer;
37 sub write16 # little-endian
41 if ($integer&0xffff0000)
43 die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 8 bits\n");
45 &write8($fileHandle, $integer&0x000000ff);
46 &write8($fileHandle, ($integer>>8)&0x000000ff);
49 sub write32 # little-endian
53 &write8($fileHandle, $integer&0x000000ff);
54 &write8($fileHandle, ($integer>>8)&0x000000ff);
55 &write8($fileHandle, ($integer>>16)&0x000000ff);
56 &write8($fileHandle, ($integer>>24)&0x000000ff);
59 sub writePositiveIntegerCompacted15 # big-endian
65 die("Error: the integer ".sprintf("0x%08x", $integer)." is negative\n");
67 if ($integer<0x00000080)
69 &write8($fileHandle, $integer);
71 elsif ($integer<0x00008000)
73 &write8($fileHandle, 0x00000080|(($integer>>8)&0x0000007f));
74 &write8($fileHandle, $integer&0x000000ff);
78 die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 15 bits\n");
82 sub writePositiveIntegerCompacted30 # big-endian
88 die("Error: the integer ".sprintf("0x%08x", $integer)." is negative\n");
90 if ($integer<0x00000080)
92 &write8($fileHandle, $integer);
94 elsif ($integer<0x00004000)
96 &write8($fileHandle, 0x00000080|(($integer>>8)&0x0000003f));
97 &write8($fileHandle, $integer&0x000000ff);
99 elsif ($integer<0x40000000)
101 &write8($fileHandle, 0x000000c0|(($integer>>24)&0x0000003f));
102 &write8($fileHandle, ($integer>>16)&0x000000ff);
103 &write8($fileHandle, ($integer>>8)&0x000000ff);
104 &write8($fileHandle, $integer&0x000000ff);
108 die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 30 bits\n");
112 sub writeSignedIntegerCompacted29 # big-endian
114 my $fileHandle=shift;
116 my $negativeFlag=0x00000000;
119 $negativeFlag=0x00000080;
120 if (($integer^-$integer)==0)
122 die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 29 bits\n");
126 if ($integer<0x00000040)
128 &write8($fileHandle, $negativeFlag|$integer);
130 elsif ($integer<0x00002000)
132 &write8($fileHandle, $negativeFlag|0x00000040|(($integer>>8)&0x0000001f));
133 &write8($fileHandle, $integer&0x000000ff);
135 elsif ($integer<0x20000000)
137 &write8($fileHandle, $negativeFlag|0x00000060|(($integer>>24)&0x0000001f));
138 &write8($fileHandle, ($integer>>16)&0x000000ff);
139 &write8($fileHandle, ($integer>>8)&0x000000ff);
140 &write8($fileHandle, $integer&0x000000ff);
144 die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 30 bits\n");
150 my $fileHandle=shift;
152 my $lengthOfString=length($string);
154 for ($i=0; $i<$lengthOfString; ++$i)
157 ($byteAsNumber)=unpack("C", substr($string, $i, 1));
158 &write8($fileHandle, $byteAsNumber);
164 my $fileHandle=shift;
169 my $uidsin = sprintf("0x%08x 0x%08x 0x%08x ", $uid1, $uid2, $uid3);
171 # Locate uidcrc on the same path as this module, and if not found then run through environment
174 my $uidlocation = "$FindBin::Bin/uidcrc";
175 if(-e $uidlocation or -e $uidlocation.".exe") {
176 $uidsout = `$uidlocation $uidsin` or die('Error: could not find UIDCRC at '.$uidlocation ); }
178 $uidsout = `uidcrc $uidsin` or die('Error: could not find UIDCRC on PATH');
181 die ('Did not understand output of UIDCRC')
182 unless $uidsout =~ /^0x([0-9a-fA-F]{8}) 0x([0-9a-fA-F]{8}) 0x([0-9a-fA-F]{8}) 0x([0-9a-fA-F]{8})[ \n\r\t]*$/s;
183 my $uidCrcBinaryBuffer = pack 'V4', hex($1), hex($2), hex($3), hex($4);
184 &writeString($fileHandle, $uidCrcBinaryBuffer);