os/textandloc/charconvfw/charconv_fw/tools/WRITER.PM
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
#
sl@0
     2
# Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
# All rights reserved.
sl@0
     4
# This component and the accompanying materials are made available
sl@0
     5
# under the terms of "Eclipse Public License v1.0"
sl@0
     6
# which accompanies this distribution, and is available
sl@0
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
#
sl@0
     9
# Initial Contributors:
sl@0
    10
# Nokia Corporation - initial contribution.
sl@0
    11
#
sl@0
    12
# Contributors:
sl@0
    13
#
sl@0
    14
# Description: 
sl@0
    15
#
sl@0
    16
sl@0
    17
use strict;
sl@0
    18
use integer;
sl@0
    19
use FindBin;
sl@0
    20
sl@0
    21
package WRITER;
sl@0
    22
require Exporter;
sl@0
    23
@WRITER::ISA=qw(Exporter);
sl@0
    24
@WRITER::EXPORT=qw(write8 write16 write32 writePositiveIntegerCompacted15 writePositiveIntegerCompacted30 writeSignedIntegerCompacted29 writeString writeUids);
sl@0
    25
sl@0
    26
sub write8
sl@0
    27
	{
sl@0
    28
	my $fileHandle=shift;
sl@0
    29
	my $integer=shift;
sl@0
    30
	if ($integer&0xffffff00)
sl@0
    31
		{
sl@0
    32
		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 8 bits\n");
sl@0
    33
		}
sl@0
    34
	printf $fileHandle "%c", $integer;
sl@0
    35
	}
sl@0
    36
sl@0
    37
sub write16 # little-endian
sl@0
    38
	{
sl@0
    39
	my $fileHandle=shift;
sl@0
    40
	my $integer=shift;
sl@0
    41
	if ($integer&0xffff0000)
sl@0
    42
		{
sl@0
    43
		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 8 bits\n");
sl@0
    44
		}
sl@0
    45
	&write8($fileHandle, $integer&0x000000ff);
sl@0
    46
	&write8($fileHandle, ($integer>>8)&0x000000ff);
sl@0
    47
	}
sl@0
    48
sl@0
    49
sub write32 # little-endian
sl@0
    50
	{
sl@0
    51
	my $fileHandle=shift;
sl@0
    52
	my $integer=shift;
sl@0
    53
	&write8($fileHandle, $integer&0x000000ff);
sl@0
    54
	&write8($fileHandle, ($integer>>8)&0x000000ff);
sl@0
    55
	&write8($fileHandle, ($integer>>16)&0x000000ff);
sl@0
    56
	&write8($fileHandle, ($integer>>24)&0x000000ff);
sl@0
    57
	}
sl@0
    58
sl@0
    59
sub writePositiveIntegerCompacted15 # big-endian
sl@0
    60
	{
sl@0
    61
	my $fileHandle=shift;
sl@0
    62
	my $integer=shift;
sl@0
    63
	if ($integer<0)
sl@0
    64
		{
sl@0
    65
		die("Error: the integer ".sprintf("0x%08x", $integer)." is negative\n");
sl@0
    66
		}
sl@0
    67
	if ($integer<0x00000080)
sl@0
    68
		{
sl@0
    69
		&write8($fileHandle, $integer);
sl@0
    70
		}
sl@0
    71
	elsif ($integer<0x00008000)
sl@0
    72
		{
sl@0
    73
		&write8($fileHandle, 0x00000080|(($integer>>8)&0x0000007f));
sl@0
    74
		&write8($fileHandle, $integer&0x000000ff);
sl@0
    75
		}
sl@0
    76
	else
sl@0
    77
		{
sl@0
    78
		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 15 bits\n");
sl@0
    79
		}
sl@0
    80
	}
sl@0
    81
sl@0
    82
sub writePositiveIntegerCompacted30 # big-endian
sl@0
    83
	{
sl@0
    84
	my $fileHandle=shift;
sl@0
    85
	my $integer=shift;
sl@0
    86
	if ($integer<0)
sl@0
    87
		{
sl@0
    88
		die("Error: the integer ".sprintf("0x%08x", $integer)." is negative\n");
sl@0
    89
		}
sl@0
    90
	if ($integer<0x00000080)
sl@0
    91
		{
sl@0
    92
		&write8($fileHandle, $integer);
sl@0
    93
		}
sl@0
    94
	elsif ($integer<0x00004000)
sl@0
    95
		{
sl@0
    96
		&write8($fileHandle, 0x00000080|(($integer>>8)&0x0000003f));
sl@0
    97
		&write8($fileHandle, $integer&0x000000ff);
sl@0
    98
		}
sl@0
    99
	elsif ($integer<0x40000000)
sl@0
   100
		{
sl@0
   101
		&write8($fileHandle, 0x000000c0|(($integer>>24)&0x0000003f));
sl@0
   102
		&write8($fileHandle, ($integer>>16)&0x000000ff);
sl@0
   103
		&write8($fileHandle, ($integer>>8)&0x000000ff);
sl@0
   104
		&write8($fileHandle, $integer&0x000000ff);
sl@0
   105
		}
sl@0
   106
	else
sl@0
   107
		{
sl@0
   108
		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 30 bits\n");
sl@0
   109
		}
sl@0
   110
	}
sl@0
   111
sl@0
   112
sub writeSignedIntegerCompacted29 # big-endian
sl@0
   113
	{
sl@0
   114
	my $fileHandle=shift;
sl@0
   115
	my $integer=shift;
sl@0
   116
	my $negativeFlag=0x00000000;
sl@0
   117
	if ($integer<0)
sl@0
   118
		{
sl@0
   119
		$negativeFlag=0x00000080;
sl@0
   120
		if (($integer^-$integer)==0)
sl@0
   121
			{
sl@0
   122
			die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 29 bits\n");
sl@0
   123
			}
sl@0
   124
		$integer=-$integer;
sl@0
   125
		}
sl@0
   126
	if ($integer<0x00000040)
sl@0
   127
		{
sl@0
   128
		&write8($fileHandle, $negativeFlag|$integer);
sl@0
   129
		}
sl@0
   130
	elsif ($integer<0x00002000)
sl@0
   131
		{
sl@0
   132
		&write8($fileHandle, $negativeFlag|0x00000040|(($integer>>8)&0x0000001f));
sl@0
   133
		&write8($fileHandle, $integer&0x000000ff);
sl@0
   134
		}
sl@0
   135
	elsif ($integer<0x20000000)
sl@0
   136
		{
sl@0
   137
		&write8($fileHandle, $negativeFlag|0x00000060|(($integer>>24)&0x0000001f));
sl@0
   138
		&write8($fileHandle, ($integer>>16)&0x000000ff);
sl@0
   139
		&write8($fileHandle, ($integer>>8)&0x000000ff);
sl@0
   140
		&write8($fileHandle, $integer&0x000000ff);
sl@0
   141
		}
sl@0
   142
	else
sl@0
   143
		{
sl@0
   144
		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 30 bits\n");
sl@0
   145
		}
sl@0
   146
	}
sl@0
   147
sl@0
   148
sub writeString
sl@0
   149
	{
sl@0
   150
	my $fileHandle=shift;
sl@0
   151
	my $string=shift;
sl@0
   152
	my $lengthOfString=length($string);
sl@0
   153
	my $i;
sl@0
   154
	for ($i=0; $i<$lengthOfString; ++$i)
sl@0
   155
		{
sl@0
   156
		my $byteAsNumber;
sl@0
   157
		($byteAsNumber)=unpack("C", substr($string, $i, 1));
sl@0
   158
		&write8($fileHandle, $byteAsNumber);
sl@0
   159
		}
sl@0
   160
	}
sl@0
   161
sl@0
   162
sub writeUids
sl@0
   163
	{
sl@0
   164
	my $fileHandle=shift;
sl@0
   165
	my $uid1=shift;
sl@0
   166
	my $uid2=shift;
sl@0
   167
	my $uid3=shift;
sl@0
   168
	use Cwd;
sl@0
   169
	my $uidsin = sprintf("0x%08x 0x%08x 0x%08x ", $uid1, $uid2, $uid3);
sl@0
   170
sl@0
   171
	# Locate uidcrc on the same path as this module, and if not found then run through environment
sl@0
   172
sl@0
   173
  	my $uidsout = "";
sl@0
   174
  	my $uidlocation = "$FindBin::Bin/uidcrc";
sl@0
   175
  	if(-e $uidlocation or -e $uidlocation.".exe") {
sl@0
   176
		$uidsout = `$uidlocation $uidsin` or die('Error: could not find UIDCRC at '.$uidlocation ); }
sl@0
   177
	else {
sl@0
   178
		$uidsout = `uidcrc $uidsin` or die('Error: could not find UIDCRC on PATH');
sl@0
   179
	}
sl@0
   180
sl@0
   181
	die ('Did not understand output of UIDCRC')
sl@0
   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;
sl@0
   183
	my $uidCrcBinaryBuffer = pack 'V4', hex($1), hex($2), hex($3), hex($4);
sl@0
   184
	&writeString($fileHandle, $uidCrcBinaryBuffer);
sl@0
   185
	}
sl@0
   186