Link Search Menu Expand Document

サマリ:base64のソースコードリーディング3回目

base64を読もう③

まずはdo_decodeから。

  struct base_decode_context ctx;

  base_decode_ctx_init (&ctx);

base_decode_cx_initbase64_decode_ctx_initの再定義で、../gnulib/lib/base64.cに実体がある。

/* Initialize decode-context buffer, CTX.  */
void
base64_decode_ctx_init (struct base64_decode_context *ctx)
{
  ctx->i = 0;
}

struct base64_decode_context../gnulib/lib/base64.hに実体がある。

struct base64_decode_context
{
  unsigned int i;
  char buf[4];
};

ただ、それぞれのメンバについての説明はない。ひとまず置いておいて、先に進む。 これから先はdo { ;; } while (!eof (in));となっており、入力が終了するまで継続される。

      sum = 0;
      do
        {
          n = fread (inbuf + sum, 1, BASE_LENGTH (DEC_BLOCKSIZE) - sum, in);

          if (ignore_garbage)
            {
              size_t i;
              for (i = 0; n > 0 && i < n;)
                if (isbase (inbuf[sum + i]) || inbuf[sum + i] == '=')
                  i++;
                else
                  memmove (inbuf + sum + i, inbuf + sum + i + 1, --n - i);
            }

          sum += n;

          if (ferror (in))
            error (EXIT_FAILURE, errno, _("read error"));
        }
      while (sum < BASE_LENGTH (DEC_BLOCKSIZE) && !feof (in));

BASE_LENGTHBASE64_LENGTHから再定義されており、実体は../gnulib/lib/base4.hにある。

/* This uses that the expression (n+(k-1))/k means the smallest
   integer >= n/k, i.e., the ceiling of n/k.  */
# define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)

DEC_BLOCKSIZE1024*5と定義されているため、BASE_LENGTH (DEC_BLOCKSIZE)は6828となる。そのため、freadは、1バイトを6828サイズ分入力から変数へコピーすることになる。

ignore_garbageは、『デコード時に英数以外を無視』するオプションによって設定されている。

###理解できたこと ###宿題

  • BASE64_LENGTHの式がイマイチわからない

今日はここまで。 今日も晴れるといいな。


Back to top

Copyright © 2021- Akira Otaka All rights reserved.