バージョン6 Unixでダンプをキャンセルする(8進または16進ダンプ)

バージョン6 Unixでダンプをキャンセルする(8進または16進ダンプ)

od(octaldump) コマンドは Unix バージョン 1 から実装されます。しかし、私はバージョン 6 Unix マニュアル(1975).現代的な方法odorを使用したxxdリバースエンジニアリングはsedUnix V6では機能しません。なぜなら、sedand はawkバージョン 7 でのみ表示されるからです (and uudecodexxdともbase64使用できません)。

問題は次のとおりです。

  • 1975年、人々はどのように8進数または16進数のリストを2進数に変換しましたか?
  • クリップボードのテキストのみを端末に貼り付けることができるPDP-11エミュレータでこれをどのように実行しますか? (つまり、そのために独自のプログラムを書く必要がありますか?)

od以下は、バイナリファイルに再変換したいバイナリファイルの16進ダンプの例です。

# od -h /bin/sum
0000000 0107 00ba 0000 0204 0000 0000 0000 0001
0000020 1583 0bd6 0ac3 0601 8901 0a37 00ae 15b7
0000040 0002 8905 0000 0000 8609 15c0 0001 8904
0000060 0026 0005 01ef 706f 6472 000a 1001 0a05
0000100 1040 8903 00be 0200 870b 0bc0 030e 0ab7
0000120 007a 15c2 00be 9484 6105 0b45 7e04 01f0
0000140 15c0 0001 8904 0080 0002 09f7 0026 15c0
0000160 0001 8904 0083 0001 1dc5 0050 09f7 0014
0000200 15c0 0001 8904 0082 0001 1040 8906 01c2
0000220 203f 200a 1026 1066 1141 09f7 0006 1581
0000240 1580 0087 0a00 7217 000a 1066 1001 0302
0000260 09f7 fff0 15b7 0012 65f7 0030 000c 15c0
0000300 0001 8904 00ba 0001 0087
0000312 

ベストアンサー1

Unix バージョン 6 には、バージョン 7 にしか存在していない一般的なツール ( などsed) が多く含まれていないようですawk。当時、Unixはまだ商用化されていないので、単にその仕事の需要が広がっていないか、またはKen(または他のプログラマー)が/usrBell Labsからもたらされたため、「逆16進ダンプ」が失われた可能性があります。非公式ディレクトリで利用可能です。誰が知っている。

しかし、ここではUnix V6で16進ダンプを元に戻す実装があります。ようにコンパイルすると、cc -s -O unhex.c結果の実行可能ファイルは1160バイトに過ぎず(予想どおり)ダンプよりも高速に実行されます。

当時、C言語にはまだBの複合割り当て構文(=+=*など)があり、プログラマはファイルI / O用に独自のバッファを提供する必要があることに注意してください。

/* reverse "od -h" operation on Unix V6 */
/* written in pre-K&R C */
/* derived from wc.c and cvopt.c */

int ibuf[259];
int obuf[259];

main(argc,argv)
char **argv;
{
    int token, bytecnt;
    register char *p1, *p2;         /* input buffer pointers */
    register int c;                 /* char or read count */
    char sp, b1, b2, lastc, lastb2, nfirst;

    obuf[0] = 1;                    /* standard output by default */
    if (argc>2) {
                                    /* create output file */
            if ((obuf[0] = creat(argv[2], 0666)) < 0) {
                    diag(argv[2]);
                    diag(": failed to create\n");
                    return;
            }
    }
    if (argc>1 && fopen(argv[1], ibuf)>=0) {
            p1 = 0;
            p2 = 0;
            sp = 0;
            token = 0;
            bytecnt = 0;
            nfirst = 0;
            for(;;) {
                    /* reading from file */
                    if (p1 >= p2) {
                            p1 = &ibuf[1];
                            c = read(ibuf[0], p1, 512);
                            if (c <= 0)
                                    break;
                            p2 = p1+c;
                    }
                    /* decoding loop */
                    c = 0;
                    c =| *p1++;
                    if (c==' ' || c=='\n') {
                            b1 = token;
                            b2 = token >> 8;
                            if (lastc!=' ' && lastc!='\n') {
                                    /* end of token */
                                    if (sp>0) {
                                            if (nfirst) putc(lastb2, obuf);
                                            putc(b1, obuf);
                                            lastb2 = b2;
                                            nfirst = 1;
                                    } else {
                                            /* first token in the line */
                                            bytecnt = token;
                                    }
                            }
                            if (c==' ') sp++;
                            else {
                                    /* new line */
                                    sp = 0;
                                    fflush(obuf);
                            }
                            token = 0;
                    } else {
                            /* actual hex and octal conversion */
                            token =* sp>0 ? 16 : 8;
                            token =+ c<='9' ? c-'0' : c-'W';
                    }
                    lastc = c;
            }
            if (!(bytecnt & 1)) {
                    putc(lastb2, obuf);
                    fflush(obuf);
            }
            close(ibuf[0]);
            close(obuf[0]);
    } else if (argc>1) {
            diag(argv[1]);
            diag(": cannot open\n");
    } else {
            diag("error: filename missing\n");
    }
}

diag(s)
char *s;
{
    while(*s)
            write(2,s++,1);
}

UPD。より高速で簡単なバージョンを公開しました。GitHub、ここで構文も強調表示されます。

おすすめ記事