2017-12-12 31 views
1

私のプログラマーは、バイナリをベース64にエンコードすることになっています。 すべてはEOFまでうまく動作します。私は出力文字列の最後に '='を追加するのに苦労します。Nasm Linux x64-86 |正しいbase64エンコーディングのためにファイルの最後にビットを追加してください

これは、最後のバイトが読み取られている場合にのみ発生します。それは空の空間を埋めるはずです。ここでは、1つまたは2つの '='を追加する必要があるたびに、私のコードを検出します。私の中のSO

Read: 
     mov eax,3    ; Specify sys_read call 
     mov ebx,0    ; Specify File Descriptor 0: Standard Input 
     mov ecx,Bytes   ; Pass offset of the buffer to read to 
     mov edx,BYTESLEN  ; Pass number of bytes to read at one pass 
     int 80h     ; Call sys_read to fill the buffer 
     mov ebp,eax    ; Save # of bytes read from file for later 
     cmp rax,1    ; If EAX=0, sys_read reached EOF on stdin 
     je MissingTwoByte ; Jump If Equal (to 1, from compare) 
     cmp rax,2    ; If EAX=0, sys_read reached EOF on stdin 
     je MissingOneByte ; Jump If Equal (to 2, from compare) 
     cmp eax,0    ; If EAX=0, sys_read reached EOF on stdin 
     je Done   ; Jump If Equal (to 0, from compare) 

:MissingOneByteと:MissingTwoByte機能、私は右、私の '=' バイトに追加する必要がありますか?それをどうすれば実現できますか?

+0

あなたの問題は何か不明です。確かにあなたは 'mov [Bytes + 1]、 '=''などを探していませんか? – Jester

+0

いいえ、私は説明する方法を本当に知らない、私の英語はそれほど良くない。 – Arkarr

+0

これは32ビットですか、64ビットですか? –

答えて

3

私の前の答えでは、そのコードは3バイトを常に食べ、ゼロで埋め尽くされ、後で結果を修正/パッチすることになっていました!

I.e.単一入力バイト0x44の場合、Bytes44 00 00に設定する必要があります(最初の44sys_readで、他の2つはコードでクリアする必要があります)。間違った変換結果RAAAが表示され、次にそれを正しいRA==に修正する必要があります。

I.e.

SECTION .bss 
BYTESLEN equ  3   ; 3 bytes of real buffer are needed 
Bytes:  resb BYTESLEN + 5; real buffer +5 padding (total 8B) 
B64output: resb 4+4   ; 4 bytes are real output buffer 
           ; +4 bytes are padding (total 8B) 

SECTION .text 

     ;... 
Read: 
     mov  eax,3   ; Specify sys_read call 
     xor  ebx,ebx   ; Specify File Descriptor 0: Standard Input 
     mov  ecx,Bytes  ; Pass offset of the buffer to read to 
     mov  edx,BYTESLEN ; Pass number of bytes to read at one pass 
     int  80h    ; Call sys_read to fill the buffer 
     test eax,eax 
     jl  ReadingError ; OS has problem, system "errno" is set 
     mov  ebp,eax   ; Save # of bytes read from file for later 
     jz  Done   ; 0 bytes read, no more input 
     ; eax = 1, 2, 3 
     mov  [ecx + eax],ebx ; clear padding bytes 
      ; ^^ this is a bit nasty EBX reuse, works only for STDIN (0) 
      ; for any file handle use fixed zero: mov word [ecx+eax],0 
     call ConvertBytesToB64Output  ; convert to Base64 output 
     ; overwrite last two/one/none characters based on how many input 
     ; bytes were read (B64output+3+1 = B64output+4 => beyond 4 chars) 
     mov  word [B64output + ebp + 1], '==' 
     ;TODO store B64output where you wish 
     cmp  ebp,3 
     je  Read   ; if 3 bytes were read, loop again 
     ; 1 or 2 bytes will continue with "Done:" 
Done: 
     ; ... 

ReadingError: 
     ; ... 

ConvertBytesToB64Output: 
     ; ... 
     ret 

短くてシンプルに書かれ、パフォーマンスはあまり気にしません。

説明を簡単にするのは、バッファの終わりに十分なパディングがあることです。バッファを越えてメモリを上書きする心配がないので、各出力の後に2つの文字を書くことができます。 2つの最後の文字を上書きするか、最後の文字を1つ上書きするか、または完全に出力の外側にパディング領域に書き込む)。

if (length == 1/2/3) {...} else {...}が多すぎると、メモリへの書き込みを保護し、出力バッファのみを上書きすることはありません。

私は何をし、どのように動作するのかを理解し、自分のバッファに十分なパディングを追加してください。

また...!免責事項!=がbase64出力の最後にどれくらいあるべきかわかりません。いつ...それは基本64の定義を研究するためのOPです。私はちょうど0に埋め込まれた短い入力を取る3B→4B変換の間違った出力を修正する方法を示しています。うーん、online BASE64 generatorによると、実際には私のコードとして働く...(入力%3)=> 0は=、1は2つの=、2は単一の=を持っています。

+0

はい、それは '='を追加するのに適しています。ありがとうございました ! – Arkarr

関連する問題