scanf注册新的转换说明符

时间:2019-05-10 17:51:27

标签: c gcc binary scanf glibc

我这周写了printf函数家族的扩展,以接受%b打印二进制文件。为此,我使用了功能register_printf_specifier()

现在我想知道我是否可以在scanf函数家族中做同样的事情来接受二进制输入并将其写入变量。

是否有任何扩展名可以允许我这样做?

1 个答案:

答案 0 :(得分:1)

TL; DR:否。使用glibc时至少没有。


我已经下载了glibc的最新版本:

% wget https://ftp.gnu.org/gnu/glibc/glibc-2.29.tar.gz
% tar -xzf glibc-2.29.tar.gz

然后grep编辑了find,寻找我想到的随机scanf家庭功能-在这种情况下,它是vfscanf

% find | grep "vfscanf"

根据我的经验,我知道真正的实现位于-internal中,但是我查看了输出:

./stdio-common/iovfscanf.c
./stdio-common/isoc99_vfscanf.c
./stdio-common/vfscanf-internal.c
./stdio-common/vfscanf.c
./sysdeps/ieee754/ldbl-opt/nldbl-iovfscanf.c
./sysdeps/ieee754/ldbl-opt/nldbl-isoc99_vfscanf.c
./sysdeps/ieee754/ldbl-opt/nldbl-vfscanf.c

并决定检查./stdio-common/vfscanf.c,实际上其中是否包含内部函数的存根:

% cat ./stdio-common/vfscanf.c

int
___vfscanf (FILE *s, const char *format, va_list argptr)
{
  return __vfscanf_internal (s, format, argptr, 0);
}

展望未来,我已经查看了文件,并到达了格式解析器:

% cat ./stdio-common/vfscanf-internal.c | head -n 1390 | tail -n 20
          }
          break;

        case L_('x'):   /* Hexadecimal integer.  */
        case L_('X'):   /* Ditto.  */
          base = 16;
          goto number;

        case L_('o'):   /* Octal integer.  */
          base = 8;
          goto number;

        case L_('u'):   /* Unsigned decimal integer.  */
          base = 10;
          goto number;

        case L_('d'):   /* Signed decimal integer.  */
          base = 10;
          flags |= NUMBER_SIGNED;
          goto number;

我查看了文件的末尾,并找到了一些整理用例的标签:

% cat ./stdio-common/vfscanf-internal.c | tail -n 60
                  ++done;
                }
            }
          break;

        case L_('p'):   /* Generic pointer.  */
          base = 16;
          /* A PTR must be the same size as a `long int'.  */
          flags &= ~(SHORT|LONGDBL);
          if (need_long)
            flags |= LONG;
          flags |= READ_POINTER;
          goto number;

        default:
          /* If this is an unknown format character punt.  */
          conv_error ();
        }
    }

  /* The last thing we saw int the format string was a white space.
     Consume the last white spaces.  */
  if (skip_space)
    {
      do
        c = inchar ();
      while (ISSPACE (c));
      ungetc (c, s);
    }

 errout:
  /* Unlock stream.  */
  UNLOCK_STREAM (s);

  scratch_buffer_free (&charbuf.scratch);

  if (__glibc_unlikely (done == EOF))
    {
      if (__glibc_unlikely (ptrs_to_free != NULL))
        {
          struct ptrs_to_free *p = ptrs_to_free;
          while (p != NULL)
            {
              for (size_t cnt = 0; cnt < p->count; ++cnt)
                {
                  free (*p->ptrs[cnt]);
                  *p->ptrs[cnt] = NULL;
                }
              p = p->next;
              ptrs_to_free = p;
            }
        }
    }
  else if (__glibc_unlikely (strptr != NULL))
    {
      free (*strptr);
      *strptr = NULL;
    }
  return done;
}

以及完成该功能的代码。这意味着,对于scanf系列功能之一,所有格式说明符都是常量,这意味着您不能在不弄乱glibc源代码中的大型clusterf..k的情况下注册新处理程序(当然,不会便携式)。