1/* Check two binary blobs for equality.
2 Copyright (C) 2018-2022 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 <https://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 (format: " %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 (blob == NULL && length > 0)
37 printf (format: " %s (evaluated from %s): NULL\n", what, expr);
38 else if (length > 0)
39 {
40 printf (format: " %s (evaluated from %s):\n", what, expr);
41 char *quoted = support_quote_blob (blob, length);
42 printf (format: " \"%s\"\n", quoted);
43 free (ptr: quoted);
44
45 fputs (s: " ", stdout);
46 for (unsigned long i = 0; i < length; ++i)
47 printf (format: " %02X", blob[i]);
48 putc (c: '\n', stdout);
49 }
50}
51
52void
53support_test_compare_blob (const void *left, unsigned long int left_length,
54 const void *right, unsigned long int right_length,
55 const char *file, int line,
56 const char *left_expr, const char *left_len_expr,
57 const char *right_expr, const char *right_len_expr)
58{
59 /* No differences are possible if both lengths are null. */
60 if (left_length == 0 && right_length == 0)
61 return;
62
63 if (left_length != right_length || left == NULL || right == NULL
64 || memcmp (s1: left, s2: right, n: left_length) != 0)
65 {
66 support_record_failure ();
67 printf (format: "%s:%d: error: blob comparison failed\n", file, line);
68 if (left_length == right_length)
69 printf (format: " blob length: %lu bytes\n", left_length);
70 else
71 {
72 report_length (what: "left length: ", length: left_length, expr: left_len_expr);
73 report_length (what: "right length:", length: right_length, expr: right_len_expr);
74 }
75 report_blob (what: "left", blob: left, length: left_length, expr: left_expr);
76 report_blob (what: "right", blob: right, length: right_length, expr: right_expr);
77 }
78}
79

source code of glibc/support/support_test_compare_blob.c