sl@0: # sl@0: # Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: # All rights reserved. sl@0: # This component and the accompanying materials are made available sl@0: # under the terms of "Eclipse Public License v1.0" sl@0: # which accompanies this distribution, and is available sl@0: # at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: # sl@0: # Initial Contributors: sl@0: # Nokia Corporation - initial contribution. sl@0: # sl@0: # Contributors: sl@0: # sl@0: # Description: sl@0: # sl@0: sl@0: use strict; sl@0: use integer; sl@0: use FindBin; sl@0: sl@0: package WRITER; sl@0: require Exporter; sl@0: @WRITER::ISA=qw(Exporter); sl@0: @WRITER::EXPORT=qw(write8 write16 write32 writePositiveIntegerCompacted15 writePositiveIntegerCompacted30 writeSignedIntegerCompacted29 writeString writeUids); sl@0: sl@0: sub write8 sl@0: { sl@0: my $fileHandle=shift; sl@0: my $integer=shift; sl@0: if ($integer&0xffffff00) sl@0: { sl@0: die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 8 bits\n"); sl@0: } sl@0: printf $fileHandle "%c", $integer; sl@0: } sl@0: sl@0: sub write16 # little-endian sl@0: { sl@0: my $fileHandle=shift; sl@0: my $integer=shift; sl@0: if ($integer&0xffff0000) sl@0: { sl@0: die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 8 bits\n"); sl@0: } sl@0: &write8($fileHandle, $integer&0x000000ff); sl@0: &write8($fileHandle, ($integer>>8)&0x000000ff); sl@0: } sl@0: sl@0: sub write32 # little-endian sl@0: { sl@0: my $fileHandle=shift; sl@0: my $integer=shift; sl@0: &write8($fileHandle, $integer&0x000000ff); sl@0: &write8($fileHandle, ($integer>>8)&0x000000ff); sl@0: &write8($fileHandle, ($integer>>16)&0x000000ff); sl@0: &write8($fileHandle, ($integer>>24)&0x000000ff); sl@0: } sl@0: sl@0: sub writePositiveIntegerCompacted15 # big-endian sl@0: { sl@0: my $fileHandle=shift; sl@0: my $integer=shift; sl@0: if ($integer<0) sl@0: { sl@0: die("Error: the integer ".sprintf("0x%08x", $integer)." is negative\n"); sl@0: } sl@0: if ($integer<0x00000080) sl@0: { sl@0: &write8($fileHandle, $integer); sl@0: } sl@0: elsif ($integer<0x00008000) sl@0: { sl@0: &write8($fileHandle, 0x00000080|(($integer>>8)&0x0000007f)); sl@0: &write8($fileHandle, $integer&0x000000ff); sl@0: } sl@0: else sl@0: { sl@0: die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 15 bits\n"); sl@0: } sl@0: } sl@0: sl@0: sub writePositiveIntegerCompacted30 # big-endian sl@0: { sl@0: my $fileHandle=shift; sl@0: my $integer=shift; sl@0: if ($integer<0) sl@0: { sl@0: die("Error: the integer ".sprintf("0x%08x", $integer)." is negative\n"); sl@0: } sl@0: if ($integer<0x00000080) sl@0: { sl@0: &write8($fileHandle, $integer); sl@0: } sl@0: elsif ($integer<0x00004000) sl@0: { sl@0: &write8($fileHandle, 0x00000080|(($integer>>8)&0x0000003f)); sl@0: &write8($fileHandle, $integer&0x000000ff); sl@0: } sl@0: elsif ($integer<0x40000000) sl@0: { sl@0: &write8($fileHandle, 0x000000c0|(($integer>>24)&0x0000003f)); sl@0: &write8($fileHandle, ($integer>>16)&0x000000ff); sl@0: &write8($fileHandle, ($integer>>8)&0x000000ff); sl@0: &write8($fileHandle, $integer&0x000000ff); sl@0: } sl@0: else sl@0: { sl@0: die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 30 bits\n"); sl@0: } sl@0: } sl@0: sl@0: sub writeSignedIntegerCompacted29 # big-endian sl@0: { sl@0: my $fileHandle=shift; sl@0: my $integer=shift; sl@0: my $negativeFlag=0x00000000; sl@0: if ($integer<0) sl@0: { sl@0: $negativeFlag=0x00000080; sl@0: if (($integer^-$integer)==0) sl@0: { sl@0: die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 29 bits\n"); sl@0: } sl@0: $integer=-$integer; sl@0: } sl@0: if ($integer<0x00000040) sl@0: { sl@0: &write8($fileHandle, $negativeFlag|$integer); sl@0: } sl@0: elsif ($integer<0x00002000) sl@0: { sl@0: &write8($fileHandle, $negativeFlag|0x00000040|(($integer>>8)&0x0000001f)); sl@0: &write8($fileHandle, $integer&0x000000ff); sl@0: } sl@0: elsif ($integer<0x20000000) sl@0: { sl@0: &write8($fileHandle, $negativeFlag|0x00000060|(($integer>>24)&0x0000001f)); sl@0: &write8($fileHandle, ($integer>>16)&0x000000ff); sl@0: &write8($fileHandle, ($integer>>8)&0x000000ff); sl@0: &write8($fileHandle, $integer&0x000000ff); sl@0: } sl@0: else sl@0: { sl@0: die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 30 bits\n"); sl@0: } sl@0: } sl@0: sl@0: sub writeString sl@0: { sl@0: my $fileHandle=shift; sl@0: my $string=shift; sl@0: my $lengthOfString=length($string); sl@0: my $i; sl@0: for ($i=0; $i<$lengthOfString; ++$i) sl@0: { sl@0: my $byteAsNumber; sl@0: ($byteAsNumber)=unpack("C", substr($string, $i, 1)); sl@0: &write8($fileHandle, $byteAsNumber); sl@0: } sl@0: } sl@0: sl@0: sub writeUids sl@0: { sl@0: my $fileHandle=shift; sl@0: my $uid1=shift; sl@0: my $uid2=shift; sl@0: my $uid3=shift; sl@0: use Cwd; sl@0: my $uidsin = sprintf("0x%08x 0x%08x 0x%08x ", $uid1, $uid2, $uid3); sl@0: sl@0: # Locate uidcrc on the same path as this module, and if not found then run through environment sl@0: sl@0: my $uidsout = ""; sl@0: my $uidlocation = "$FindBin::Bin/uidcrc"; sl@0: if(-e $uidlocation or -e $uidlocation.".exe") { sl@0: $uidsout = `$uidlocation $uidsin` or die('Error: could not find UIDCRC at '.$uidlocation ); } sl@0: else { sl@0: $uidsout = `uidcrc $uidsin` or die('Error: could not find UIDCRC on PATH'); sl@0: } sl@0: sl@0: die ('Did not understand output of UIDCRC') sl@0: 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; sl@0: my $uidCrcBinaryBuffer = pack 'V4', hex($1), hex($2), hex($3), hex($4); sl@0: &writeString($fileHandle, $uidCrcBinaryBuffer); sl@0: } sl@0: