libexplain  1.4.D001
libexplain/buffer/hexdump.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 2011, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation; either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/ctype.h>
00020 
00021 #include <libexplain/buffer/hexdump.h>
00022 
00023 
00024 void
00025 explain_buffer_hexdump(explain_string_buffer_t *sb, const void *data,
00026     size_t data_size)
00027 {
00028     const unsigned char *cp;
00029 
00030     cp = data;
00031     while (data_size > 0)
00032     {
00033         explain_string_buffer_printf(sb, " %02X", *cp++);
00034         --data_size;
00035     }
00036 }
00037 
00038 
00039 void
00040 explain_buffer_hexdump_array(explain_string_buffer_t *sb, const void *data,
00041     size_t data_size)
00042 {
00043     const unsigned char *cp;
00044     size_t          j;
00045 
00046     explain_string_buffer_puts(sb, "{ ");
00047     cp = data;
00048     for (j = 0; j < data_size; ++j)
00049     {
00050         unsigned char   c;
00051 
00052         if (j)
00053             explain_string_buffer_puts(sb, ", ");
00054         c = cp[j];
00055         if (isprint(c) || isspace(c))
00056             explain_string_buffer_putc_quoted(sb, c);
00057         else
00058             explain_string_buffer_printf(sb, "0x%02X", *cp++);
00059     }
00060     explain_string_buffer_puts(sb, " }");
00061 }
00062 
00063 
00064 static int
00065 mostly_text(const char *data, size_t data_size)
00066 {
00067     size_t          bin;
00068     size_t          j;
00069 
00070     bin = 0;
00071     for (j = 0; j < data_size; ++j)
00072     {
00073         unsigned char c = data[j];
00074         if (!c)
00075             return 0;
00076         if (!isprint(c) && !isspace(c))
00077             ++bin;
00078     }
00079     return (bin * 4 < data_size);
00080 }
00081 
00082 
00083 void
00084 explain_buffer_mostly_text(explain_string_buffer_t *sb, const void *data,
00085     size_t data_size)
00086 {
00087     if (mostly_text(data, data_size))
00088         explain_string_buffer_puts_quoted_n(sb, data, data_size);
00089     else
00090         explain_buffer_hexdump_array(sb, data, data_size);
00091 }
00092 
00093 
00094 /* vim: set ts=8 sw=4 et : */