1/* Check two binary blobs for equality.
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <support/check.h>
23#include <support/support.h>
24#include <support/xmemstream.h>
25
26static void
27report_length (const char *what, unsigned long int length, const char *expr)
28{
29 printf (" %s %lu bytes (from %s)\n", what, length, expr);
30}
31
32static void
33report_blob (const char *what, const unsigned char *blob,
34 unsigned long int length, const char *expr)
35{
36 if (length > 0)
37 {
38 printf (" %s (evaluated from %s):\n", what, expr);
39 char *quoted = support_quote_blob (blob, length);
40 printf (" \"%s\"\n", quoted);
41 free (quoted);
42
43 fputs (" ", stdout);
44 for (unsigned long i = 0; i < length; ++i)
45 printf (" %02X", blob[i]);
46 putc ('\n', stdout);
47 }
48}
49
50void
51support_test_compare_blob (const void *left, unsigned long int left_length,
52 const void *right, unsigned long int right_length,
53 const char *file, int line,
54 const char *left_expr, const char *left_len_expr,
55 const char *right_expr, const char *right_len_expr)
56{
57 /* No differences are possible if both lengths are null. */
58 if (left_length == 0 && right_length == 0)
59 return;
60
61 if (left_length != right_length || left == NULL || right == NULL
62 || memcmp (left, right, left_length) != 0)
63 {
64 support_record_failure ();
65 printf ("%s:%d: error: blob comparison failed\n", file, line);
66 if (left_length == right_length)
67 printf (" blob length: %lu bytes\n", left_length);
68 else
69 {
70 report_length ("left length: ", left_length, left_len_expr);
71 report_length ("right length:", right_length, right_len_expr);
72 }
73 report_blob ("left", left, left_length, left_expr);
74 report_blob ("right", right, right_length, right_expr);
75 }
76}
77