Add `rdbl-luks`, git textconv diff filter for LUKS headers
[sunny256-utils.git] / charconv
blobcf7ab4151d084698f6f798b6fe47a7e1dcef58d3
1 #!/usr/bin/env perl
3 #=======================================================================
4 # charconv
5 # File ID: 6e32b188-5d37-11df-98d6-90e6ba3022ac
6 # Konverterer fra cp865 til Unicode. Alternativ konverteringsfil
7 # spesifiseres med «-i»-parameteret, ellers brukes default filnavn. -p
8 # skriver ut en enkel HTML-header og -slutt.
9 # Lisens: GNU GPL v2 eller seinere.
10 #=======================================================================
12 use strict;
13 use warnings;
14 require 'getopts.pl';
16 my $map_file = "$ENV{HOME}/src/svn/div/trunk/ibm2ent/cp865.txt";
17 my %Table;
19 $| = 1;
21 Getopts('hi:pP');
22 defined($main::opt_i) && ($map_file = $main::opt_i);
24 for (my $a = 0; $a < 256; $a++) {
25 $Table{$a} = widechar($a);
28 open(MapFP, "<$map_file") || die("$map_file: Can’t open map file: $!");
30 while (<MapFP>) {
31 (/^#/ || /^\s*$/) && next;
32 if (/^0x(..)\s+0x(....)/) {
33 my ($Cp, $Uni) = (hex($1), hex($2));
34 if ($Cp != $Uni) {
35 $Table{$Cp} = widechar($Uni);
37 } else {
38 print STDERR "Line $.: Invalid line: \"$_\"\n";
42 $main::opt_P && print("<html><head></head><body><pre>\n") ||
43 $main::opt_p && print("<pre>\n");
45 while(<>) {
46 s/(.)/$Table{ord($1)}/g;
47 print;
50 $main::opt_P && print("</pre></body></html>\n") ||
51 $main::opt_p && print("</pre>\n");
53 # Henta fra h2u,v 1.5 (r386) 2002/11/20 00:09:40
54 sub widechar {
55 my $Val = shift;
56 my $allow_invalid = 0;
57 if ($Val < 0x80) {
58 return sprintf("%c", $Val);
59 } elsif ($Val < 0x800) {
60 return sprintf("%c%c", 0xC0 | ($Val >> 6),
61 0x80 | ($Val & 0x3F));
62 } elsif ($Val < 0x10000) {
63 unless ($allow_invalid) {
64 if (($Val >= 0xD800 && $Val <= 0xDFFF) || ($Val eq 0xFFFE) || ($Val eq 0xFFFF)) {
65 $Val = 0xFFFD;
68 return sprintf("%c%c%c", 0xE0 | ($Val >> 12),
69 0x80 | (($Val >> 6) & 0x3F),
70 0x80 | ($Val & 0x3F));
71 } elsif ($Val < 0x200000) {
72 return sprintf("%c%c%c%c", 0xF0 | ($Val >> 18),
73 0x80 | (($Val >> 12) & 0x3F),
74 0x80 | (($Val >> 6) & 0x3F),
75 0x80 | ($Val & 0x3F));
76 } elsif ($Val < 0x4000000) {
77 return sprintf("%c%c%c%c%c", 0xF8 | ($Val >> 24),
78 0x80 | (($Val >> 18) & 0x3F),
79 0x80 | (($Val >> 12) & 0x3F),
80 0x80 | (($Val >> 6) & 0x3F),
81 0x80 | ( $Val & 0x3F));
82 } elsif ($Val < 0x80000000) {
83 return sprintf("%c%c%c%c%c%c", 0xFC | ($Val >> 30),
84 0x80 | (($Val >> 24) & 0x3F),
85 0x80 | (($Val >> 18) & 0x3F),
86 0x80 | (($Val >> 12) & 0x3F),
87 0x80 | (($Val >> 6) & 0x3F),
88 0x80 | ( $Val & 0x3F));
89 } else {
90 return widechar(0xFFFD);
92 } # widechar()
94 __END__
96 #### End of file charconv ####