Link Search Menu Expand Document

サマリ:base64のソースコードリーディング10回目(最終回)

base64を読もう⑩〜base_decode_ctx編〜

 /* Restore OUT and OUTLEFT.  */
      out -= outleft_save - outleft;
      outleft = outleft_save;

outleft_saveoutleftに差が生まれるのはdecode_4 (in, inlen, &out, &outleft)で正常なコードが返ってこなかった場合で、outに一旦は代入した文字を戻す(ポインタの位置を調整する)。当然、出力文字の残りを示すoutleftについてもoutleft_saveから復帰させる。

      {
        char const *in_end = in + inlen;
        char const *non_nl;

        if (ignore_newlines)
          non_nl = get_4 (ctx, &in, in_end, &inlen);
        else
          non_nl = in;  /* Might have nl in this case. */

        /* If the input is empty or consists solely of newlines (0 non-newlines),
           then we're done.  Likewise if there are fewer than 4 bytes when not
           flushing context and not treating newlines as garbage.  */
        if (inlen == 0 || (inlen < 4 && !flush_ctx && ignore_newlines))
          {
            inlen = 0;
            break;
          }
        if (!decode_4 (non_nl, inlen, &out, &outleft))
          break;

        inlen = in_end - in;
      }

in_endにはinの末尾が、non_nlにはignore_newlinesの値によって異なる値が代入されます。なお、ignore_newlines==trueの場合にコールされるget_4ですが、コメントは以下のようになっています。

/* If CTX->i is 0 or 4, there are four or more bytes in [*IN..IN_END), and
   none of those four is a newline, then return *IN.  Otherwise, copy up to
   4 - CTX->i non-newline bytes from that range into CTX->buf, starting at
   index CTX->i and setting CTX->i to reflect the number of bytes copied,
   and return CTX->buf.  In either case, advance *IN to point to the byte
   after the last one processed, and set *N_NON_NEWLINE to the number of
   verified non-newline bytes accessible through the returned pointer.  */

CTX->iが0または4の場合、4バイトかそれ以上、*IN〜*IN_ENDに存在する。そして、それら4バイトは改行ではなく、*INが返される。言い換えれば、CTX->bufの*IN〜*IN_ENDの範囲から4 - CTX->iの非改行文字がコピーされる。CTX->iを開始インデックスとして、CTX->iをバイトコピーの数値を示す設定を行い、CTX->bufが返される。いずれにしても、*INは最終プロセスの後にポイントされる。そして、*N_NON_NEWLINEは非改行文字であると保証され、アクセス可能な文字数が設定され、ポインタとして返される。

理解したこと

宿題

  • get_4を読む

とりあえずここまで。

4月なのに少し寒いですね。もう少し暖かくなってくれれば良いのですが。 それはそうと、メガネが壊れました・・。早く直さないと・・。

感想

とりあえず、base64の基本的な流れは理解できるようになったので、ここまでとします。 吸収できることが多かったので、すごく楽しかったです。


Back to top

Copyright © 2021- Akira Otaka All rights reserved.