Blame support/support_quote_blob.c

Packit 6c4009
/* Quote a blob so that it can be used in C literals.
Packit 6c4009
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/xmemstream.h>
Packit 6c4009
Packit 6c4009
char *
Packit 6c4009
support_quote_blob (const void *blob, size_t length)
Packit 6c4009
{
Packit 6c4009
  struct xmemstream out;
Packit 6c4009
  xopen_memstream (&out;;
Packit 6c4009
Packit 6c4009
  const unsigned char *p = blob;
Packit 6c4009
  for (size_t i = 0; i < length; ++i)
Packit 6c4009
    {
Packit 6c4009
      unsigned char ch = p[i];
Packit 6c4009
Packit 6c4009
      /* Use C backslash escapes for those control characters for
Packit 6c4009
         which they are defined.  */
Packit 6c4009
      switch (ch)
Packit 6c4009
        {
Packit 6c4009
          case '\a':
Packit 6c4009
            putc_unlocked ('\\', out.out);
Packit 6c4009
            putc_unlocked ('a', out.out);
Packit 6c4009
            break;
Packit 6c4009
          case '\b':
Packit 6c4009
            putc_unlocked ('\\', out.out);
Packit 6c4009
            putc_unlocked ('b', out.out);
Packit 6c4009
            break;
Packit 6c4009
          case '\f':
Packit 6c4009
            putc_unlocked ('\\', out.out);
Packit 6c4009
            putc_unlocked ('f', out.out);
Packit 6c4009
            break;
Packit 6c4009
          case '\n':
Packit 6c4009
            putc_unlocked ('\\', out.out);
Packit 6c4009
            putc_unlocked ('n', out.out);
Packit 6c4009
            break;
Packit 6c4009
          case '\r':
Packit 6c4009
            putc_unlocked ('\\', out.out);
Packit 6c4009
            putc_unlocked ('r', out.out);
Packit 6c4009
            break;
Packit 6c4009
          case '\t':
Packit 6c4009
            putc_unlocked ('\\', out.out);
Packit 6c4009
            putc_unlocked ('t', out.out);
Packit 6c4009
            break;
Packit 6c4009
          case '\v':
Packit 6c4009
            putc_unlocked ('\\', out.out);
Packit 6c4009
            putc_unlocked ('v', out.out);
Packit 6c4009
            break;
Packit 6c4009
          case '\\':
Packit 6c4009
          case '\'':
Packit 6c4009
          case '\"':
Packit 6c4009
            putc_unlocked ('\\', out.out);
Packit 6c4009
            putc_unlocked (ch, out.out);
Packit 6c4009
            break;
Packit 6c4009
        default:
Packit 6c4009
          if (ch < ' ' || ch > '~')
Packit 6c4009
            /* Use octal sequences because they are fixed width,
Packit 6c4009
               unlike hexadecimal sequences.  */
Packit 6c4009
            fprintf (out.out, "\\%03o", ch);
Packit 6c4009
          else
Packit 6c4009
            putc_unlocked (ch, out.out);
Packit 6c4009
        }
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  xfclose_memstream (&out;;
Packit 6c4009
  return out.buffer;
Packit 6c4009
}