<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#
# $Id: GSM0338.pm,v 2.10 2021/05/24 10:56:53 dankogai Exp $
#
package Encode::GSM0338;

use strict;
use warnings;
use Carp;

use vars qw($VERSION);
$VERSION = do { my @r = ( q$Revision: 2.10 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };

use Encode qw(:fallbacks);

use parent qw(Encode::Encoding);
__PACKAGE__-&gt;Define('gsm0338');

use utf8;

# Mapping table according to 3GPP TS 23.038 version 16.0.0 Release 16 and ETSI TS 123 038 V16.0.0 (2020-07)
# https://www.etsi.org/deliver/etsi_ts/123000_123099/123038/16.00.00_60/ts_123038v160000p.pdf (page 20 and 22)
our %UNI2GSM = (
    "\x{000A}" =&gt; "\x0A",        # LINE FEED
    "\x{000C}" =&gt; "\x1B\x0A",    # FORM FEED
    "\x{000D}" =&gt; "\x0D",        # CARRIAGE RETURN
    "\x{0020}" =&gt; "\x20",        # SPACE
    "\x{0021}" =&gt; "\x21",        # EXCLAMATION MARK
    "\x{0022}" =&gt; "\x22",        # QUOTATION MARK
    "\x{0023}" =&gt; "\x23",        # NUMBER SIGN
    "\x{0024}" =&gt; "\x02",        # DOLLAR SIGN
    "\x{0025}" =&gt; "\x25",        # PERCENT SIGN
    "\x{0026}" =&gt; "\x26",        # AMPERSAND
    "\x{0027}" =&gt; "\x27",        # APOSTROPHE
    "\x{0028}" =&gt; "\x28",        # LEFT PARENTHESIS
    "\x{0029}" =&gt; "\x29",        # RIGHT PARENTHESIS
    "\x{002A}" =&gt; "\x2A",        # ASTERISK
    "\x{002B}" =&gt; "\x2B",        # PLUS SIGN
    "\x{002C}" =&gt; "\x2C",        # COMMA
    "\x{002D}" =&gt; "\x2D",        # HYPHEN-MINUS
    "\x{002E}" =&gt; "\x2E",        # FULL STOP
    "\x{002F}" =&gt; "\x2F",        # SOLIDUS
    "\x{0030}" =&gt; "\x30",        # DIGIT ZERO
    "\x{0031}" =&gt; "\x31",        # DIGIT ONE
    "\x{0032}" =&gt; "\x32",        # DIGIT TWO
    "\x{0033}" =&gt; "\x33",        # DIGIT THREE
    "\x{0034}" =&gt; "\x34",        # DIGIT FOUR
    "\x{0035}" =&gt; "\x35",        # DIGIT FIVE
    "\x{0036}" =&gt; "\x36",        # DIGIT SIX
    "\x{0037}" =&gt; "\x37",        # DIGIT SEVEN
    "\x{0038}" =&gt; "\x38",        # DIGIT EIGHT
    "\x{0039}" =&gt; "\x39",        # DIGIT NINE
    "\x{003A}" =&gt; "\x3A",        # COLON
    "\x{003B}" =&gt; "\x3B",        # SEMICOLON
    "\x{003C}" =&gt; "\x3C",        # LESS-THAN SIGN
    "\x{003D}" =&gt; "\x3D",        # EQUALS SIGN
    "\x{003E}" =&gt; "\x3E",        # GREATER-THAN SIGN
    "\x{003F}" =&gt; "\x3F",        # QUESTION MARK
    "\x{0040}" =&gt; "\x00",        # COMMERCIAL AT
    "\x{0041}" =&gt; "\x41",        # LATIN CAPITAL LETTER A
    "\x{0042}" =&gt; "\x42",        # LATIN CAPITAL LETTER B
    "\x{0043}" =&gt; "\x43",        # LATIN CAPITAL LETTER C
    "\x{0044}" =&gt; "\x44",        # LATIN CAPITAL LETTER D
    "\x{0045}" =&gt; "\x45",        # LATIN CAPITAL LETTER E
    "\x{0046}" =&gt; "\x46",        # LATIN CAPITAL LETTER F
    "\x{0047}" =&gt; "\x47",        # LATIN CAPITAL LETTER G
    "\x{0048}" =&gt; "\x48",        # LATIN CAPITAL LETTER H
    "\x{0049}" =&gt; "\x49",        # LATIN CAPITAL LETTER I
    "\x{004A}" =&gt; "\x4A",        # LATIN CAPITAL LETTER J
    "\x{004B}" =&gt; "\x4B",        # LATIN CAPITAL LETTER K
    "\x{004C}" =&gt; "\x4C",        # LATIN CAPITAL LETTER L
    "\x{004D}" =&gt; "\x4D",        # LATIN CAPITAL LETTER M
    "\x{004E}" =&gt; "\x4E",        # LATIN CAPITAL LETTER N
    "\x{004F}" =&gt; "\x4F",        # LATIN CAPITAL LETTER O
    "\x{0050}" =&gt; "\x50",        # LATIN CAPITAL LETTER P
    "\x{0051}" =&gt; "\x51",        # LATIN CAPITAL LETTER Q
    "\x{0052}" =&gt; "\x52",        # LATIN CAPITAL LETTER R
    "\x{0053}" =&gt; "\x53",        # LATIN CAPITAL LETTER S
    "\x{0054}" =&gt; "\x54",        # LATIN CAPITAL LETTER T
    "\x{0055}" =&gt; "\x55",        # LATIN CAPITAL LETTER U
    "\x{0056}" =&gt; "\x56",        # LATIN CAPITAL LETTER V
    "\x{0057}" =&gt; "\x57",        # LATIN CAPITAL LETTER W
    "\x{0058}" =&gt; "\x58",        # LATIN CAPITAL LETTER X
    "\x{0059}" =&gt; "\x59",        # LATIN CAPITAL LETTER Y
    "\x{005A}" =&gt; "\x5A",        # LATIN CAPITAL LETTER Z
    "\x{005B}" =&gt; "\x1B\x3C",    # LEFT SQUARE BRACKET
    "\x{005C}" =&gt; "\x1B\x2F",    # REVERSE SOLIDUS
    "\x{005D}" =&gt; "\x1B\x3E",    # RIGHT SQUARE BRACKET
    "\x{005E}" =&gt; "\x1B\x14",    # CIRCUMFLEX ACCENT
    "\x{005F}" =&gt; "\x11",        # LOW LINE
    "\x{0061}" =&gt; "\x61",        # LATIN SMALL LETTER A
    "\x{0062}" =&gt; "\x62",        # LATIN SMALL LETTER B
    "\x{0063}" =&gt; "\x63",        # LATIN SMALL LETTER C
    "\x{0064}" =&gt; "\x64",        # LATIN SMALL LETTER D
    "\x{0065}" =&gt; "\x65",        # LATIN SMALL LETTER E
    "\x{0066}" =&gt; "\x66",        # LATIN SMALL LETTER F
    "\x{0067}" =&gt; "\x67",        # LATIN SMALL LETTER G
    "\x{0068}" =&gt; "\x68",        # LATIN SMALL LETTER H
    "\x{0069}" =&gt; "\x69",        # LATIN SMALL LETTER I
    "\x{006A}" =&gt; "\x6A",        # LATIN SMALL LETTER J
    "\x{006B}" =&gt; "\x6B",        # LATIN SMALL LETTER K
    "\x{006C}" =&gt; "\x6C",        # LATIN SMALL LETTER L
    "\x{006D}" =&gt; "\x6D",        # LATIN SMALL LETTER M
    "\x{006E}" =&gt; "\x6E",        # LATIN SMALL LETTER N
    "\x{006F}" =&gt; "\x6F",        # LATIN SMALL LETTER O
    "\x{0070}" =&gt; "\x70",        # LATIN SMALL LETTER P
    "\x{0071}" =&gt; "\x71",        # LATIN SMALL LETTER Q
    "\x{0072}" =&gt; "\x72",        # LATIN SMALL LETTER R
    "\x{0073}" =&gt; "\x73",        # LATIN SMALL LETTER S
    "\x{0074}" =&gt; "\x74",        # LATIN SMALL LETTER T
    "\x{0075}" =&gt; "\x75",        # LATIN SMALL LETTER U
    "\x{0076}" =&gt; "\x76",        # LATIN SMALL LETTER V
    "\x{0077}" =&gt; "\x77",        # LATIN SMALL LETTER W
    "\x{0078}" =&gt; "\x78",        # LATIN SMALL LETTER X
    "\x{0079}" =&gt; "\x79",        # LATIN SMALL LETTER Y
    "\x{007A}" =&gt; "\x7A",        # LATIN SMALL LETTER Z
    "\x{007B}" =&gt; "\x1B\x28",    # LEFT CURLY BRACKET
    "\x{007C}" =&gt; "\x1B\x40",    # VERTICAL LINE
    "\x{007D}" =&gt; "\x1B\x29",    # RIGHT CURLY BRACKET
    "\x{007E}" =&gt; "\x1B\x3D",    # TILDE
    "\x{00A1}" =&gt; "\x40",        # INVERTED EXCLAMATION MARK
    "\x{00A3}" =&gt; "\x01",        # POUND SIGN
    "\x{00A4}" =&gt; "\x24",        # CURRENCY SIGN
    "\x{00A5}" =&gt; "\x03",        # YEN SIGN
    "\x{00A7}" =&gt; "\x5F",        # SECTION SIGN
    "\x{00BF}" =&gt; "\x60",        # INVERTED QUESTION MARK
    "\x{00C4}" =&gt; "\x5B",        # LATIN CAPITAL LETTER A WITH DIAERESIS
    "\x{00C5}" =&gt; "\x0E",        # LATIN CAPITAL LETTER A WITH RING ABOVE
    "\x{00C6}" =&gt; "\x1C",        # LATIN CAPITAL LETTER AE
    "\x{00C7}" =&gt; "\x09",        # LATIN CAPITAL LETTER C WITH CEDILLA
    "\x{00C9}" =&gt; "\x1F",        # LATIN CAPITAL LETTER E WITH ACUTE
    "\x{00D1}" =&gt; "\x5D",        # LATIN CAPITAL LETTER N WITH TILDE
    "\x{00D6}" =&gt; "\x5C",        # LATIN CAPITAL LETTER O WITH DIAERESIS
    "\x{00D8}" =&gt; "\x0B",        # LATIN CAPITAL LETTER O WITH STROKE
    "\x{00DC}" =&gt; "\x5E",        # LATIN CAPITAL LETTER U WITH DIAERESIS
    "\x{00DF}" =&gt; "\x1E",        # LATIN SMALL LETTER SHARP S
    "\x{00E0}" =&gt; "\x7F",        # LATIN SMALL LETTER A WITH GRAVE
    "\x{00E4}" =&gt; "\x7B",        # LATIN SMALL LETTER A WITH DIAERESIS
    "\x{00E5}" =&gt; "\x0F",        # LATIN SMALL LETTER A WITH RING ABOVE
    "\x{00E6}" =&gt; "\x1D",        # LATIN SMALL LETTER AE
    "\x{00E8}" =&gt; "\x04",        # LATIN SMALL LETTER E WITH GRAVE
    "\x{00E9}" =&gt; "\x05",        # LATIN SMALL LETTER E WITH ACUTE
    "\x{00EC}" =&gt; "\x07",        # LATIN SMALL LETTER I WITH GRAVE
    "\x{00F1}" =&gt; "\x7D",        # LATIN SMALL LETTER N WITH TILDE
    "\x{00F2}" =&gt; "\x08",        # LATIN SMALL LETTER O WITH GRAVE
    "\x{00F6}" =&gt; "\x7C",        # LATIN SMALL LETTER O WITH DIAERESIS
    "\x{00F8}" =&gt; "\x0C",        # LATIN SMALL LETTER O WITH STROKE
    "\x{00F9}" =&gt; "\x06",        # LATIN SMALL LETTER U WITH GRAVE
    "\x{00FC}" =&gt; "\x7E",        # LATIN SMALL LETTER U WITH DIAERESIS
    "\x{0393}" =&gt; "\x13",        # GREEK CAPITAL LETTER GAMMA
    "\x{0394}" =&gt; "\x10",        # GREEK CAPITAL LETTER DELTA
    "\x{0398}" =&gt; "\x19",        # GREEK CAPITAL LETTER THETA
    "\x{039B}" =&gt; "\x14",        # GREEK CAPITAL LETTER LAMDA
    "\x{039E}" =&gt; "\x1A",        # GREEK CAPITAL LETTER XI
    "\x{03A0}" =&gt; "\x16",        # GREEK CAPITAL LETTER PI
    "\x{03A3}" =&gt; "\x18",        # GREEK CAPITAL LETTER SIGMA
    "\x{03A6}" =&gt; "\x12",        # GREEK CAPITAL LETTER PHI
    "\x{03A8}" =&gt; "\x17",        # GREEK CAPITAL LETTER PSI
    "\x{03A9}" =&gt; "\x15",        # GREEK CAPITAL LETTER OMEGA
    "\x{20AC}" =&gt; "\x1B\x65",    # EURO SIGN
);
our %GSM2UNI = reverse %UNI2GSM;
our $ESC     = "\x1b";

sub decode ($$;$) {
    my ( $obj, $bytes, $chk ) = @_;
    return undef unless defined $bytes;
    my $str = substr( $bytes, 0, 0 );    # to propagate taintedness;
    while ( length $bytes ) {
        my $seq = '';
        my $c;
        do {
            $c = substr( $bytes, 0, 1, '' );
            $seq .= $c;
        } while ( length $bytes and $c eq $ESC );
        my $u =
            exists $GSM2UNI{$seq}          ? $GSM2UNI{$seq}
          : ( $chk &amp;&amp; ref $chk eq 'CODE' ) ? $chk-&gt;( unpack 'C*', $seq )
          :                                  "\x{FFFD}";
        if ( not exists $GSM2UNI{$seq} and $chk and not ref $chk ) {
            if ( substr( $seq, 0, 1 ) eq $ESC
                and ( $chk &amp; Encode::STOP_AT_PARTIAL ) )
            {
                $bytes .= $seq;
                last;
            }
            croak join( '', map { sprintf "\\x%02X", $_ } unpack 'C*', $seq )
              . ' does not map to Unicode'
              if $chk &amp; Encode::DIE_ON_ERR;
            carp join( '', map { sprintf "\\x%02X", $_ } unpack 'C*', $seq )
              . ' does not map to Unicode'
              if $chk &amp; Encode::WARN_ON_ERR;
            if ( $chk &amp; Encode::RETURN_ON_ERR ) {
                $bytes .= $seq;
                last;
            }
        }
        $str .= $u;
    }
    $_[1] = $bytes if not ref $chk and $chk and !( $chk &amp; Encode::LEAVE_SRC );
    return $str;
}

sub encode($$;$) {
    my ( $obj, $str, $chk ) = @_;
    return undef unless defined $str;
    my $bytes = substr( $str, 0, 0 );    # to propagate taintedness
    while ( length $str ) {
        my $u = substr( $str, 0, 1, '' );
        my $c;
        my $seq =
            exists $UNI2GSM{$u}            ? $UNI2GSM{$u}
          : ( $chk &amp;&amp; ref $chk eq 'CODE' ) ? $chk-&gt;( ord($u) )
          :                                  $UNI2GSM{'?'};
        if ( not exists $UNI2GSM{$u} and $chk and not ref $chk ) {
            croak sprintf( "\\x{%04x} does not map to %s", ord($u), $obj-&gt;name )
              if $chk &amp; Encode::DIE_ON_ERR;
            carp sprintf( "\\x{%04x} does not map to %s", ord($u), $obj-&gt;name )
              if $chk &amp; Encode::WARN_ON_ERR;
            if ( $chk &amp; Encode::RETURN_ON_ERR ) {
                $str .= $u;
                last;
            }
        }
        $bytes .= $seq;
    }
    $_[1] = $str if not ref $chk and $chk and !( $chk &amp; Encode::LEAVE_SRC );
    return $bytes;
}

1;
__END__

=head1 NAME

Encode::GSM0338 -- ETSI GSM 03.38 Encoding

=head1 SYNOPSIS

  use Encode qw/encode decode/;
  $gsm0338 = encode("gsm0338", $unicode); # loads Encode::GSM0338 implicitly
  $unicode = decode("gsm0338", $gsm0338); # ditto

=head1 DESCRIPTION

GSM0338 is for GSM handsets. Though it shares alphanumerals with ASCII,
control character ranges and other parts are mapped very differently,
mainly to store Greek characters.  There are also escape sequences
(starting with 0x1B) to cover e.g. the Euro sign.

This was once handled by L&lt;Encode::Bytes&gt; but because of all those
unusual specifications, Encode 2.20 has relocated the support to
this module.

This module implements only I&lt;GSM 7 bit Default Alphabet&gt; and
I&lt;GSM 7 bit default alphabet extension table&gt; according to standard
3GPP TS 23.038 version 16. Therefore I&lt;National Language Single Shift&gt;
and I&lt;National Language Locking Shift&gt; are not implemented nor supported.

=head2 Septets

This modules operates with octets (like any other Encode module) and not
with packed septets (unlike other GSM standards). Therefore for processing
binary SMS or parts of GSM TPDU payload (3GPP TS 23.040) it is needed to do
conversion between octets and packed septets. For this purpose perl's C&lt;pack&gt;
and C&lt;unpack&gt; functions may be useful:

  $bytes = substr(pack('(b*)*', unpack '(A7)*', unpack 'b*', $septets), 0, $num_of_septets);
  $unicode = decode('GSM0338', $bytes);

  $bytes = encode('GSM0338', $unicode);
  $septets = pack 'b*', join '', map { substr $_, 0, 7 } unpack '(A8)*', unpack 'b*', $bytes;
  $num_of_septets = length $bytes;

Please note that for correct decoding of packed septets it is required to
know number of septets packed in binary buffer as binary buffer is always
padded with zero bits and 7 zero bits represents character C&lt;@&gt;. Number
of septets is also stored in TPDU payload when dealing with 3GPP TS 23.040.

=head1 BUGS

Encode::GSM0338 2.7 and older versions (part of Encode 3.06) incorrectly
handled zero bytes (character C&lt;@&gt;). This was fixed in Encode::GSM0338
version 2.8 (part of Encode 3.07).

=head1 SEE ALSO

L&lt;3GPP TS 23.038|https://www.3gpp.org/dynareport/23038.htm&gt;

L&lt;ETSI TS 123 038 V16.0.0 (2020-07)|https://www.etsi.org/deliver/etsi_ts/123000_123099/123038/16.00.00_60/ts_123038v160000p.pdf&gt;

L&lt;Encode&gt;

=cut
</pre></body></html>