サマリ:base64のソースコードリーディング3回目
base64を読もう③
まずはdo_decode
から。
struct base_decode_context ctx;
base_decode_ctx_init (&ctx);
base_decode_cx_init
はbase64_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_LENGTH
はBASE64_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_BLOCKSIZE
は1024*5
と定義されているため、BASE_LENGTH (DEC_BLOCKSIZE)
は6828となる。そのため、fread
は、1バイトを6828サイズ分入力から変数へコピーすることになる。
ignore_garbage
は、『デコード時に英数以外を無視』するオプションによって設定されている。
###理解できたこと ###宿題
BASE64_LENGTH
の式がイマイチわからない
今日はここまで。 今日も晴れるといいな。