#!/usr/bin/env php
<?php
#include 'utf8lib.php';
#include 'ucs4lib.php';

function ucs4ord($c)
{
    return unpack('N*', $c);
}

function ucs4ord_1char($c)
{
    $u = ucs4ord($c);
    return $u[1];
}

function ucs4chr($u)
{
    return pack('N*', $u);
}

function print_ucs2mb_one_char($ucs, $mb)
{
    $mblen = strlen($mb);

    printf("<U%04X> ", $ucs);
    for ($i = 0; $i < $mblen; $i++) {
        printf("\\x%02X", ord($mb[$i]));
    }
    print "\n";
}

function dump_ucs2mb_plane($codeset, $plane)
{

    $u = 0x9042;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);

    $u = 0x4eba;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);

    $u = 0x4ec1;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);

    $u = 0x4f38;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);


    // hankaku kana

    for ($u = 0xff61; $u < 0xff9f; $u++) {
      $ucs = ucs4chr($u);
      printf("%04X ", $u);
      printf("  %04X \n", $ucs);
      $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
      print_ucs2mb_one_char($u, $mb);
    }

    // IBM extentions, 89 ku - 92 ku 
    $u = 0x216f+1;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);
/*    $u = 0xfaa1;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);
    $u = 0xfba1;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);
    $u = 0xfca1;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);
    $u = 0xfcf1;
    $ucs = ucs4chr($u);
    printf("%04X ", $u);
    printf("  %04X \n", $ucs);
    $mb = mb_convert_encoding($ucs, $codeset, "UCS-4");
    print_ucs2mb_one_char($u, $mb);
*/

}

function dump_ucs2mb($codeset)
{
    dump_ucs2mb_plane($codeset, 0);
#    dump_ucs2mb_plane($codeset, 2);
}

if ($argc != 2) {
    fputs(STDERR, "Usage: php_mb2ucs.php codeset\n");
    exit(1);
}

dump_ucs2mb($argv[1]);
exit(0);

?>

