1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/* utils.c --- Self test utilities.
* Copyright (C) 2002-2025 Simon Josefsson
*
* This file is part of GNU Libidn.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
int debug = 0;
int error_count = 0;
int break_on_error = 0;
void
fail (const char *format, ...)
{
va_list arg_ptr;
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
error_count++;
if (break_on_error)
exit (EXIT_FAILURE);
}
void
escapeprint (const char *str, size_t len)
{
size_t i;
printf (" (length %ld bytes):\n\t", (long) len);
for (i = 0; i < len; i++)
{
if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
|| (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
printf ("%c", (str[i] & 0xFF));
else
printf ("\\x%02X", (unsigned) (str[i] & 0xFF));
if ((i + 1) % 16 == 0 && (i + 1) < len)
printf ("'\n\t'");
}
printf ("\n");
}
void
hexprint (const char *str, size_t len)
{
size_t i;
printf ("\t;; ");
for (i = 0; i < len; i++)
{
printf ("%02x ", (unsigned) (str[i] & 0xFF));
if ((i + 1) % 8 == 0)
printf (" ");
if ((i + 1) % 16 == 0 && i + 1 < len)
printf ("\n\t;; ");
}
printf ("\n");
}
void
binprint (const char *str, size_t len)
{
size_t i;
printf ("\t;; ");
for (i = 0; i < len; i++)
{
printf ("%d%d%d%d%d%d%d%d ",
(str[i] & 0xFF) & 0x80 ? 1 : 0,
(str[i] & 0xFF) & 0x40 ? 1 : 0,
(str[i] & 0xFF) & 0x20 ? 1 : 0,
(str[i] & 0xFF) & 0x10 ? 1 : 0,
(str[i] & 0xFF) & 0x08 ? 1 : 0,
(str[i] & 0xFF) & 0x04 ? 1 : 0,
(str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
if ((i + 1) % 3 == 0)
printf (" ");
if ((i + 1) % 6 == 0 && i + 1 < len)
printf ("\n\t;; ");
}
printf ("\n");
}
void
ucs4print (const uint32_t *str, size_t len)
{
size_t i;
printf ("\t;; ");
for (i = 0; (len == (size_t) -1) ? str[i] : i < len; i++)
{
printf ("U+%04x ", str[i]);
if ((i + 1) % 4 == 0)
printf (" ");
if ((i + 1) % 8 == 0 && i + 1 < len)
printf ("\n\t;; ");
}
puts ("");
}
int
main (int argc, char *argv[])
{
do
if (strcmp (argv[argc - 1], "-v") == 0 ||
strcmp (argv[argc - 1], "--verbose") == 0)
debug = 1;
else if (strcmp (argv[argc - 1], "-b") == 0 ||
strcmp (argv[argc - 1], "--break-on-error") == 0)
break_on_error = 1;
else if (strcmp (argv[argc - 1], "-h") == 0 ||
strcmp (argv[argc - 1], "-?") == 0 ||
strcmp (argv[argc - 1], "--help") == 0)
{
printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
argv[0]);
return 1;
}
while (argc-- > 1);
doit ();
if (debug)
printf ("Self tests done with %d errors\n", error_count);
return error_count ? 1 : 0;
}
|