(root)/
glibc-2.38/
support/
support_test_compare_blob.c
       1  /* Check two binary blobs for equality.
       2     Copyright (C) 2018-2023 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  
      26  static void
      27  report_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  
      32  static void
      33  report_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 ("  %s (evaluated from %s): NULL\n", what, expr);
      38    else if (length > 0)
      39      {
      40        printf ("  %s (evaluated from %s):\n", what, expr);
      41        char *quoted = support_quote_blob (blob, length);
      42        printf ("      \"%s\"\n", quoted);
      43        free (quoted);
      44  
      45        fputs ("     ", stdout);
      46        for (unsigned long i = 0; i < length; ++i)
      47          printf (" %02X", blob[i]);
      48        putc ('\n', stdout);
      49      }
      50  }
      51  
      52  void
      53  support_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 (left, right, left_length) != 0)
      65      {
      66        support_record_failure ();
      67        printf ("%s:%d: error: blob comparison failed\n", file, line);
      68        if (left_length == right_length)
      69          printf ("  blob length: %lu bytes\n", left_length);
      70        else
      71          {
      72            report_length ("left length: ", left_length, left_len_expr);
      73            report_length ("right length:", right_length, right_len_expr);
      74          }
      75        report_blob ("left", left, left_length, left_expr);
      76        report_blob ("right", right, right_length, right_expr);
      77      }
      78  }