function convert_dc_to_3_01a_output($data)
{
    //TODO
    //DEPENDS: DcMapSendSimple (provided by legacy function support)
    //THIS DOESN'T WORK!
    $Encoded = DcMapSendSimple($dc, '3_01a');
    $txt = '44434565020102fd' . $Encoded . 'fd03';
    return hex2bin($txt);
}
function dce_convert_1_43($data, $input_format, $output_format = "none", $output_log = false)
{
    if ($input_format == "legacy_cdce") {
        $strict = false;
    } else {
        if ($input_format == "cdce_lstrict") {
            $strict = true;
        }
    }
    $html_opening = '<html><head><title></title></head><body>';
    $html_closing = '</body></html>';
    $onestep = false;
    $final = '';
    //echo $output_format;
    /*  if ($output_format == 'utf32') {
        header('Content-type: text/plain; charset=utf-32');
        } else {
        header('Content-type: text/plain');
        } */
    //Ideally how this works: Convert the input into an array of Dc IDs, and then write that out using the selected translator.
    switch ($input_format) {
        case 'dc':
            $dc = $data;
            break;
        case 'utf8_base64':
            $dc = dce_convert(base64_decode($data), 'utf8', 'dc');
            break;
        case 'utf8_dc64':
            $dc = dce_convert(base64_3_01a_to_unicode($data), 'utf8', 'dc');
            break;
        case 'hex_dce':
            $dc = dce_convert(hex2bin($data), 'dce', 'dc');
            break;
        case 'hex_3_0a':
            $dc = dce_convert(hex2bin($data), '3_0a', 'dc');
            break;
        case 'hex_3_01a':
            $dc = dce_convert(hex2bin($data), '3_01a', 'dc');
            break;
        case 'dce2txt':
            return dce2txt($data);
            break;
        case 'dce2hex':
            return dce2hex($data);
            break;
        case 'hex2dce':
            return hex2dce($data);
            break;
        case '3_0a_old':
            //This next block is the old decoder.
            //######################################################
            //This is a DCE 3.0a file. It doesn't need to be updated. This case will though!
            global $dce3_0a_core;
            $counter = 14;
            $txt = '';
            while ($counter < strlen($hex)) {
                $txt = $txt . $dce3_0a_core[hexdec(substr($hex, $counter, 2))];
                if (substr($hex, $counter, 4) == 'fd03') {
                    break;
                }
                $counter = $counter + 2;
            }
            r:
            return $txt;
            $data = $data;
            break;
        case 'dce':
            $hex = strtolower(bin2hex($data));
            if (substr($hex, 0, 12) !== '444345650201') {
                return 'This document is not stored using a supported format.';
                break;
            }
            switch (get_dce_version_1_43($data)) {
                case '3_0a':
                    //######################################################
                    //This is a DCE 3.0a file.
                    global $DcMap_dce3_0a_Core;
                    $counter = 14;
                    $txt = '';
                    while ($counter < strlen($hex)) {
                        $txt = $txt . $DcMap_dce3_0a_Core[strtoupper(substr($hex, $counter, 2))] . ',';
                        //echo $txt;
                        //$txt=$txt;
                        if (substr($hex, $counter, 4) == 'fd03') {
                            break;
                        }
                        $counter = $counter + 2;
                    }
                    //echo $txt;
                    //echo '<br>';
                    //echo '<br>';
                    $txt = substr($txt, 3, strlen($txt) - 6);
                    //echo $txt;echo '<br>';echo '<br>';
                    // return $txt;
                    $dc = $txt;
                    break;
                case '3_01a':
                    //######################################################
                    //This is a DCE 3.01a file.
                    global $DcMap_dce3_01a_Core;
                    global $DcMap_dce3_01a_Variant_Selectors;
                    global $DcMap_dce3_01a_Semantic_Records;
                    global $DcMap_dce3_01a_Mathematics;
                    global $DcMap_dce3_01a_Whitespace_and_Punctuation;
                    global $Dc_to_Base64;
                    $counter = 14;
                    $txt = '';
                    $state = 'Core';
                    $varAppend = 'DcMap_dce3_01a_';
                    while ($counter < strlen($hex)) {
                        $action_last = $action;
                        log_add('<br><font color="red">' . (($counter - 14) / 2 + 1) . ': </font><br>State: ' . $state . '<br>Hex position: ' . substr($hex, $counter, 2) . '<br>Dc ID: ' . $DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))] . '<br>Appending: ');
                        //echo $state; echo '<br>';
                        //echo ''; echo '<br>';
                        $data_array_name = $varAppend . $state;
                        $data_array = ${$data_array_name};
                        switch ($state) {
                            case 'Core':
                                if (substr($DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))], 0, 1) == '>') {
                                    //Switch states
                                    $state = substr($DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))], 1, strlen($DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))]) - 1);
                                    $append = '';
                                    $action = 'State switch out of Core) <br>';
                                    //echo 'hit';
                                    break;
                                } else {
                                    ////////////////////////////////This is of no concern. Embedded Unicode should be preserved in its embedded state until being converted *out* of Dc. D'oh.
                                    /*  if ($DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2)) ] == '191') { //Unicode embedded
                                        while (substr($hex, $counter + 2, 2) != 'C4') {
                                        $counter = $counter + 2;
                                        // $unicode_decode = $unicode_decode . $Dc_to_Base64[$DcMap_dce3_01a_Core[substr($hex, $counter, 2) ]];
                                        }
                                        //  $unicode = base64_decode($unicode_decode);
                                        // $txt = $txt . '' . $unicode . '';
                                        } else */
                                    $append = $DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))] . ',';
                                    $action = 'Append from Core) <br>';
                                }
                                break;
                            case 'Variant_Selectors':
                                if (substr($hex, $counter, 2) == 'fd' || substr($hex, $counter, 2) == 'fe') {
                                    $state = 'Core';
                                    $append = '';
                                    $action = 'State switch out of Variant_Selectors) <br>';
                                } else {
                                    $append = $DcMap_dce3_01a_Variant_Selectors[strtoupper(substr($hex, $counter, 2))] . ',';
                                    $action = 'Append from Variant_Selectors) <br>';
                                }
                                break;
                            case 'Semantic_Records':
                                if (substr($hex, $counter, 2) == 'fd' || substr($hex, $counter, 2) == 'fe') {
                                    $state = 'Core';
                                    $append = '';
                                    $action = 'State switch out of Semantic_Records) <br>';
                                } else {
                                    $append = $DcMap_dce3_01a_Semantic_Records[strtoupper(substr($hex, $counter, 2))] . ',';
                                    $action = 'Append from Semantic_Records) <br>';
                                }
                                break;
                            case 'Mathematics':
                                if (substr($hex, $counter, 2) == 'fd' || substr($hex, $counter, 2) == 'fe') {
                                    $state = 'Core';
                                    $append = '';
                                    $action = 'State switch out of Mathematics) <br>';
                                } else {
                                    $append = $DcMap_dce3_01a_Mathematics[strtoupper(substr($hex, $counter, 2))] . ',';
                                    $action = 'Append from Mathematics) . <br>';
                                }
                                break;
                            case 'Whitespace_and_Punctuation':
                                if (substr($hex, $counter, 2) == 'fd' || substr($hex, $counter, 2) == 'fe') {
                                    $state = 'Core';
                                    $append = '';
                                    $action = 'State switch out of Whitespace_and_Punctuation) <br>';
                                } else {
                                    $append = $DcMap_dce3_01a_Whitespace_and_Punctuation[strtoupper(substr($hex, $counter, 2))] . ',';
                                    $action = 'Append from Whitespace_and_Punctuation) <br>';
                                }
                                break;
                        }
                        //echo $txt;
                        //$txt=$txt;
                        //echo '<br>' . substr($hex, $counter + 2, 4) . '<br>' . $txt . '<br>';
                        if (substr($hex, $counter + 2, 4) == 'fd03') {
                            $action = '<br>Halting. <br>';
                            break;
                        }
                        $txt = $txt . $append;
                        // echo '<br><font color="red">' . ((($counter - 14) / 2) + 1) . ': ' . substr($hex, $counter, 2) . '</font> → ' . $txt . ' (Action: ' . $action;
                        $counter = $counter + 2;
                        log_add(rtrim($append, ',') . '<br>Action: ' . substr($action, 0, strlen($action) - 6) . '<br><font color="green">→ ' . $txt . '</font><br><br>');
                        //echo $action;echo '<br>';
                    }
                    log_add(rtrim($append, ',') . '<br>Action: ' . substr($action, 4, strlen($action) - 5) . '<br><br><br><br>');
                    //echo $txt;
                    //echo '<br>';
                    //echo '<br>';
                    $txt = substr($txt, 3, strlen($txt) - 4);
                    //echo '<br>';echo $txt;echo '<br>';
                    //echo $txt;echo '<br>';echo '<br>';
                    // return $txt;
                    $dc = $txt;
                    break;
                default:
                    return 'This document is not stored using a supported version of DCE.';
                    break;
            }
            break;
        case 'utf32':
            $unicode = iconv('UTF-32BE', 'UTF-8', $data);
            return dce_convert($unicode, 'utf8', $output_format);
            break;
        case 'utf8':
            ini_set("display_errors", 0);
            $unicode = iconv('UTF-8', 'UTF-32BE', $data);
            $hex = bin2hex($unicode);
            //echo $hex;
            global $DcMap_Unicode_Lossy;
            $counter = 0;
            $txt = '';
            while ($counter < strlen($hex)) {
                // $byte=strtoupper(ltrim(substr($hex, $counter, 8),'0'));
                // echo'<br>';echo $byte;echo'<br>';
                //echo $txt;
                //echo '<br>';
                //echo $counter;
                if (strlen($DcMap_Unicode_Lossy[strtoupper(ltrim(substr($hex, $counter, 8), '0'))]) != 0) {
                    $append = $DcMap_Unicode_Lossy[strtoupper(ltrim(substr($hex, $counter, 8), '0'))] . ',';
                } else {
                    $append = '';
                }
                // $txt = $txt . $DcMap_Unicode_Lossy[strtoupper(ltrim(substr($hex, $counter, 8), '0')) ] . ',';
                $txt = $txt . $append;
                //echo $txt;
                //$txt=$txt;
                //if (substr($hex, $counter, 4) == 'fd03') {
                //  break;
                //   }
                $counter = $counter + 8;
            }
            //echo $txt;
            //echo '<br>';
            //echo '<br>';
            //$txt = substr($txt, 3, (strlen($txt) - 6));
            //echo $txt;echo '<br>';echo '<br>';
            // return $txt;
            $dc = $txt;
            break;
        case 'cdce':
            break;
        case 'legacy_cdce':
            // global $output_format;
            // global $onestep;
            // global $finished;
            //  global $html_opening;
            // global $html_closing;
            switch ($output_format) {
                case 'html_l':
                    //  global $onestep;
                    //  global $finished;
                    $onestep = 1;
                    $finished = $html_opening . legacy_cdce_to_html_snippet($data) . $html_closing;
                    //echo $onestep;
                    //echo $finished;
                    //echo $html_opening;
                    break;
                case 'html_snippet_l':
                    //   global $onestep;
                    //   global $finished;
                    //   global $html_opening;
                    //   global $html_closing;
                    $onestep = 1;
                    $finished = legacy_cdce_to_html_snippet($data);
                    break;
                default:
                    $dc = legacy_cdce_parse($data, $strict);
                    break;
            }
        case 'cdce_lstrict':
            // global $output_format;
            // global $onestep;
            // global $finished;
            //  global $html_opening;
            // global $html_closing;
            switch ($output_format) {
                case 'html_l':
                    //  global $onestep;
                    //  global $finished;
                    $onestep = 1;
                    $finished = $html_opening . legacy_cdce_to_html_snippet($data) . $html_closing;
                    return $finished;
                    //echo $onestep;
                    //echo $finished;
                    //echo $html_opening;
                    break;
                case 'html_snippet_l':
                    //   global $onestep;
                    //   global $finished;
                    //   global $html_opening;
                    //   global $html_closing;
                    $onestep = 1;
                    $finished = legacy_cdce_to_html_snippet($data);
                    return $finished;
                    break;
                default:
                    $dc = legacy_cdce_parse($data, $strict);
                    break;
            }
            break;
        default:
            $dc = '';
            //echo $dc;
            return 'Unknown input format: ' . $input_format . '. Available: dc, dce, 3_0a, 3_01a, legacy_cdce, cdce_lstrict, utf8, utf32';
            break;
    }
    //global $onestep;
    //$onestep="meuou";
    //echo $onestep;
    if ($onestep == "1") {
        return $finished;
        //echo 'test';
        //echo 'doom';
        //#####################################################################
        //OUTPUT TRANSLATORS BEGIN HERE
    } else {
        //  echo 'doom 2';
        $dc = preg_replace('/,\\Z/', '', $dc);
        $dc = str_replace(',,', ',0,', $dc);
        //      echo '<br>';
        //      echo $dc;
        //      echo '<br>';
        $dc_array = explode_escaped(',', $dc);
        //    print_r($dc_array);
        //     echo '<br>';
        $dc_size = count($dc_array);
        //   echo $dc_size;
        //    echo '<br>';
        switch ($output_format) {
            case 'none':
                break;
            case 'utf8_base64':
                return base64_encode(dce_convert($dc, 'dc', 'utf8'));
                break;
            case 'utf8_dc64':
                if ($input_format == 'utf8') {
                    return unicode_to_base64_3_01a($data);
                } else {
                    if ($input_format == 'ucs32') {
                        return iconv('UTF-32BE', 'UTF-8', unicode_to_base64_3_01a($data));
                    } else {
                        echo dce_convert($data), $input_format, 'utf8';
                        return unicode_to_base64_3_01a(dce_convert($data), $input_format, 'utf8');
                    }
                }
                break;
            case 'utf8_dc64_bin':
                $dc64 = dce_convert($data, $input_format, 'utf8_dc64');
                //Unless the layout of the base64 encapsulation characters changes, the following line doesn't need to be updated with new DCE versions, I don't think…
                //global $DcMapSend_dce3_01a_Core;
                //$dcarray = explode_escaped(',', $dc64);
                // print_r($dcarray);
                //$dcarray_size = count($dcarray);
                // echo $dcarray_size;
                //$counter = 0;
                //$dcb64 = '';
                //while ($counter < $dcarray_size) {
                //echo $Dc_to_Base64[$dcarray[$counter]];
                //$dcb64 = $dcb64 . $DcMapSend_dce3_01a_Core[$dcarray[$counter]];
                //$counter++;
                //}
                return DcMapSendSimple($dc64);
                break;
            case 'hex_dce':
                return bin2hex(dce_convert($dc, 'dc', 'dce'));
                break;
            case 'hex_3_0a':
                return bin2hex(dce_convert($dc, 'dc', '3_0a'));
                break;
            case 'hex_3_01a':
                return bin2hex(dce_convert($dc, 'dc', '3_01a'));
                break;
            case 'dc':
                return $dc;
                break;
            case 'dce':
                return dce_convert($dc, 'dc', '3_0a');
                break;
            case '3_0a':
                //echo $dc;
                global $DcMapSend_dce3_0a;
                $counter = 0;
                $txt = '44434565020101fd';
                //    echo '<br>';
                while ($counter < $dc_size) {
                    $txt = $txt . strtolower($DcMapSend_dce3_0a[$dc_array[$counter]]);
                    //    echo $counter;
                    //    echo ': ';
                    //    echo $txt;
                    //$txt=$txt;
                    //    echo '<br>';
                    $counter++;
                }
                $txt = $txt . 'fd03';
                //echo $txt;
                //$txt = substr($txt, 0, (strlen($txt) - 1));
                return hex2bin($txt);
                //    $dc = $txt;
                break;
            case '3_01a':
                /*   global $DcMapSend_dce3_01a;
                     $counter = 0;
                     $txt = '44434565020102fd';
                     //    echo '<br>';
                     while ($counter < $dc_size) {
                     $txt = $txt . strtolower($DcMapSend_dce3_01a[$dc_array[$counter]]);
                     //    echo $counter;
                     //    echo ': ';
                     //    echo $txt;
                     //$txt=$txt;
                     //    echo '<br>';
                     $counter++;
                     }
                     $txt = $txt . 'fd03';
                     //$txt = substr($txt, 0, (strlen($txt) - 1));
                     
                     //    $dc = $txt;*/
                //echo $dc;
                $Encoded = DcMapSendSimple($dc, '3_01a');
                $txt = '44434565020102fd' . $Encoded . 'fd03';
                return hex2bin($txt);
                break;
            case 'utf8':
                global $DcMapSend_Unicode;
                $counter = 0;
                $txt = '';
                // echo '<br>';
                while ($counter < $dc_size) {
                    $txt = $txt . str_pad(strtolower($DcMapSend_Unicode[$dc_array[$counter]]), 8, "0", STR_PAD_LEFT);
                    //echo '<br>';
                    //echo $dc_array[$counter] . ': ' . $txt;
                    //echo '<br>';
                    //    echo $counter;
                    //    echo ': ';
                    //    echo $txt;
                    //$txt=$txt;
                    //    echo '<br>';
                    $counter++;
                }
                // $txt = $txt . 'fd03';
                //$txt = substr($txt, 0, (strlen($txt) - 1));
                return iconv('UTF-32BE', 'utf8', hex2bin($txt));
                //    $dc = $txt;
                //  break;
                // return dce2txt($data);
                break;
            case 'utf32':
                $unicode = bin2hex(iconv('UTF-8', 'UTF-32BE', dce_convert($dc, 'dc', 'utf8')));
                //echo dce_convert($dc, 'dc', 'utf8');
                //echo $unicode;
                return hex2bin($unicode);
                break;
            case 'html_l':
                break;
            case 'cdce':
                break;
            case 'legacy_cdce':
                break;
                break;
            default:
                return 'Unknown output format: ' . $output_format . '. Available: dc, dce, 3_0a, 3_01a, utf8, html_l, html_snippet_l';
        }
    }
    if ($output_log) {
        global $log;
        echo $log;
    }
}
function convert_dc_to_3_03a_output($data)
{
    //TODO: may be incomplete
    //DEPENDS: DcMapSendSimple (provided by legacy function support)
    $Encoded = DcMapSendSimple($dc, '3_03a');
    $txt = '44434565020103fd' . $Encoded . 'fd03';
    return hex2bin($txt);
}