akaros/user/parlib/hexdump.c
<<
>>
Prefs
   1/*
   2 * Copyright 2013 Google Inc.
   3 *
   4 * See file CREDITS for list of people who contributed to this
   5 * project.
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License as
   9 * published by the Free Software Foundation; either version 2 of
  10 * the License, or (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but without any warranty; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20 * MA 02111-1307 USA
  21 */
  22
  23
  24#include <stdlib.h>
  25#include <parlib/stdio.h>
  26#include <unistd.h>
  27
  28static int isprint(int c)
  29{
  30        return (c >= 32 && c <= 126);
  31}
  32
  33void hexdump(FILE *f, void *v, int length)
  34{
  35        int i;
  36        unsigned char *m = v;
  37        unsigned long memory = (unsigned long) v;
  38        int all_zero = 0;
  39
  40        for (i = 0; i < length; i += 16) {
  41                int j;
  42
  43                all_zero++;
  44                for (j = 0; (j < 16) && (i + j < length); j++) {
  45                        if (m[i + j] != 0) {
  46                                all_zero = 0;
  47                                break;
  48                        }
  49                }
  50
  51                if (all_zero < 2) {
  52                        fprintf(f,"%08lx:", memory + i);
  53                        for (j = 0; j < 16; j++)
  54                                fprintf(f," %02x", m[i + j]);
  55                        fprintf(f,"  ");
  56                        for (j = 0; j < 16; j++)
  57                                fprintf(f, "%c",
  58                                        isprint(m[i + j]) ? m[i + j] : '.');
  59                        fprintf(f,"\n");
  60                } else if (all_zero == 2) {
  61                        fprintf(f,"...\n");
  62                }
  63        }
  64}
  65
  66