OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [binutils/] [strings.c] - Diff between revs 816 and 818

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 816 Rev 818
/* strings -- print the strings of printable characters in files
/* strings -- print the strings of printable characters in files
   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
   2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
   2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
   any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */
   02110-1301, USA.  */


/* Usage: strings [options] file...
/* Usage: strings [options] file...
 
 
   Options:
   Options:
   --all
   --all
   -a
   -a
   -            Do not scan only the initialized data section of object files.
   -            Do not scan only the initialized data section of object files.
 
 
   --print-file-name
   --print-file-name
   -f           Print the name of the file before each string.
   -f           Print the name of the file before each string.
 
 
   --bytes=min-len
   --bytes=min-len
   -n min-len
   -n min-len
   -min-len     Print graphic char sequences, MIN-LEN or more bytes long,
   -min-len     Print graphic char sequences, MIN-LEN or more bytes long,
                that are followed by a NUL or a newline.  Default is 4.
                that are followed by a NUL or a newline.  Default is 4.
 
 
   --radix={o,x,d}
   --radix={o,x,d}
   -t {o,x,d}   Print the offset within the file before each string,
   -t {o,x,d}   Print the offset within the file before each string,
                in octal/hex/decimal.
                in octal/hex/decimal.
 
 
   -o           Like -to.  (Some other implementations have -o like -to,
   -o           Like -to.  (Some other implementations have -o like -to,
                others like -td.  We chose one arbitrarily.)
                others like -td.  We chose one arbitrarily.)
 
 
   --encoding={s,S,b,l,B,L}
   --encoding={s,S,b,l,B,L}
   -e {s,S,b,l,B,L}
   -e {s,S,b,l,B,L}
                Select character encoding: 7-bit-character, 8-bit-character,
                Select character encoding: 7-bit-character, 8-bit-character,
                bigendian 16-bit, littleendian 16-bit, bigendian 32-bit,
                bigendian 16-bit, littleendian 16-bit, bigendian 32-bit,
                littleendian 32-bit.
                littleendian 32-bit.
 
 
   --target=BFDNAME
   --target=BFDNAME
   -T {bfdname}
   -T {bfdname}
                Specify a non-default object file format.
                Specify a non-default object file format.
 
 
   --help
   --help
   -h           Print the usage message on the standard output.
   -h           Print the usage message on the standard output.
 
 
   --version
   --version
   -V
   -V
   -v           Print the program version number.
   -v           Print the program version number.
 
 
   Written by Richard Stallman <rms@gnu.ai.mit.edu>
   Written by Richard Stallman <rms@gnu.ai.mit.edu>
   and David MacKenzie <djm@gnu.ai.mit.edu>.  */
   and David MacKenzie <djm@gnu.ai.mit.edu>.  */
 
 
#include "sysdep.h"
#include "sysdep.h"
#include "bfd.h"
#include "bfd.h"
#include "getopt.h"
#include "getopt.h"
#include "libiberty.h"
#include "libiberty.h"
#include "safe-ctype.h"
#include "safe-ctype.h"
#include <sys/stat.h>
#include <sys/stat.h>
#include "bucomm.h"
#include "bucomm.h"
 
 
#define STRING_ISGRAPHIC(c) \
#define STRING_ISGRAPHIC(c) \
      (   (c) >= 0 \
      (   (c) >= 0 \
       && (c) <= 255 \
       && (c) <= 255 \
       && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127)))
       && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127)))
 
 
#ifndef errno
#ifndef errno
extern int errno;
extern int errno;
#endif
#endif
 
 
/* The BFD section flags that identify an initialized data section.  */
/* The BFD section flags that identify an initialized data section.  */
#define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
#define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
 
 
#ifdef HAVE_FOPEN64
#ifdef HAVE_FOPEN64
typedef off64_t file_off;
typedef off64_t file_off;
#define file_open(s,m) fopen64(s, m)
#define file_open(s,m) fopen64(s, m)
#else
#else
typedef off_t file_off;
typedef off_t file_off;
#define file_open(s,m) fopen(s, m)
#define file_open(s,m) fopen(s, m)
#endif
#endif
#ifdef HAVE_STAT64
#ifdef HAVE_STAT64
# ifndef __APPLE__
# ifndef __APPLE__
typedef struct stat64 statbuf;
typedef struct stat64 statbuf;
#define file_stat(f,s) stat64(f, s)
#define file_stat(f,s) stat64(f, s)
# else
# else
// On OS X was getting errors to do with stat64() being deprecated
// On OS X was getting errors to do with stat64() being deprecated
typedef struct stat statbuf;
typedef struct stat statbuf;
#define file_stat(f,s) stat(f, s)
#define file_stat(f,s) stat(f, s)
# endif
# endif
#else
#else
typedef struct stat statbuf;
typedef struct stat statbuf;
#define file_stat(f,s) stat(f, s)
#define file_stat(f,s) stat(f, s)
#endif
#endif
 
 
/* Radix for printing addresses (must be 8, 10 or 16).  */
/* Radix for printing addresses (must be 8, 10 or 16).  */
static int address_radix;
static int address_radix;
 
 
/* Minimum length of sequence of graphic chars to trigger output.  */
/* Minimum length of sequence of graphic chars to trigger output.  */
static int string_min;
static int string_min;
 
 
/* TRUE means print address within file for each string.  */
/* TRUE means print address within file for each string.  */
static bfd_boolean print_addresses;
static bfd_boolean print_addresses;
 
 
/* TRUE means print filename for each string.  */
/* TRUE means print filename for each string.  */
static bfd_boolean print_filenames;
static bfd_boolean print_filenames;
 
 
/* TRUE means for object files scan only the data section.  */
/* TRUE means for object files scan only the data section.  */
static bfd_boolean datasection_only;
static bfd_boolean datasection_only;
 
 
/* TRUE if we found an initialized data section in the current file.  */
/* TRUE if we found an initialized data section in the current file.  */
static bfd_boolean got_a_section;
static bfd_boolean got_a_section;
 
 
/* The BFD object file format.  */
/* The BFD object file format.  */
static char *target;
static char *target;
 
 
/* The character encoding format.  */
/* The character encoding format.  */
static char encoding;
static char encoding;
static int encoding_bytes;
static int encoding_bytes;
 
 
static struct option long_options[] =
static struct option long_options[] =
{
{
  {"all", no_argument, NULL, 'a'},
  {"all", no_argument, NULL, 'a'},
  {"print-file-name", no_argument, NULL, 'f'},
  {"print-file-name", no_argument, NULL, 'f'},
  {"bytes", required_argument, NULL, 'n'},
  {"bytes", required_argument, NULL, 'n'},
  {"radix", required_argument, NULL, 't'},
  {"radix", required_argument, NULL, 't'},
  {"encoding", required_argument, NULL, 'e'},
  {"encoding", required_argument, NULL, 'e'},
  {"target", required_argument, NULL, 'T'},
  {"target", required_argument, NULL, 'T'},
  {"help", no_argument, NULL, 'h'},
  {"help", no_argument, NULL, 'h'},
  {"version", no_argument, NULL, 'v'},
  {"version", no_argument, NULL, 'v'},
  {NULL, 0, NULL, 0}
  {NULL, 0, NULL, 0}
};
};
 
 
/* Records the size of a named file so that we
/* Records the size of a named file so that we
   do not repeatedly run bfd_stat() on it.  */
   do not repeatedly run bfd_stat() on it.  */
 
 
typedef struct
typedef struct
{
{
  const char *  filename;
  const char *  filename;
  bfd_size_type filesize;
  bfd_size_type filesize;
} filename_and_size_t;
} filename_and_size_t;
 
 
static void strings_a_section (bfd *, asection *, void *);
static void strings_a_section (bfd *, asection *, void *);
static bfd_boolean strings_object_file (const char *);
static bfd_boolean strings_object_file (const char *);
static bfd_boolean strings_file (char *file);
static bfd_boolean strings_file (char *file);
static void print_strings (const char *, FILE *, file_off, int, int, char *);
static void print_strings (const char *, FILE *, file_off, int, int, char *);
static void usage (FILE *, int);
static void usage (FILE *, int);
static long get_char (FILE *, file_off *, int *, char **);
static long get_char (FILE *, file_off *, int *, char **);


int main (int, char **);
int main (int, char **);
 
 
int
int
main (int argc, char **argv)
main (int argc, char **argv)
{
{
  int optc;
  int optc;
  int exit_status = 0;
  int exit_status = 0;
  bfd_boolean files_given = FALSE;
  bfd_boolean files_given = FALSE;
  char *s;
  char *s;
  int numeric_opt = 0;
  int numeric_opt = 0;
 
 
#if defined (HAVE_SETLOCALE)
#if defined (HAVE_SETLOCALE)
  setlocale (LC_ALL, "");
  setlocale (LC_ALL, "");
#endif
#endif
  bindtextdomain (PACKAGE, LOCALEDIR);
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
  textdomain (PACKAGE);
 
 
  program_name = argv[0];
  program_name = argv[0];
  xmalloc_set_program_name (program_name);
  xmalloc_set_program_name (program_name);
 
 
  expandargv (&argc, &argv);
  expandargv (&argc, &argv);
 
 
  string_min = 4;
  string_min = 4;
  print_addresses = FALSE;
  print_addresses = FALSE;
  print_filenames = FALSE;
  print_filenames = FALSE;
  datasection_only = TRUE;
  datasection_only = TRUE;
  target = NULL;
  target = NULL;
  encoding = 's';
  encoding = 's';
 
 
  while ((optc = getopt_long (argc, argv, "afhHn:ot:e:T:Vv0123456789",
  while ((optc = getopt_long (argc, argv, "afhHn:ot:e:T:Vv0123456789",
                              long_options, (int *) 0)) != EOF)
                              long_options, (int *) 0)) != EOF)
    {
    {
      switch (optc)
      switch (optc)
        {
        {
        case 'a':
        case 'a':
          datasection_only = FALSE;
          datasection_only = FALSE;
          break;
          break;
 
 
        case 'f':
        case 'f':
          print_filenames = TRUE;
          print_filenames = TRUE;
          break;
          break;
 
 
        case 'H':
        case 'H':
        case 'h':
        case 'h':
          usage (stdout, 0);
          usage (stdout, 0);
 
 
        case 'n':
        case 'n':
          string_min = (int) strtoul (optarg, &s, 0);
          string_min = (int) strtoul (optarg, &s, 0);
          if (s != NULL && *s != 0)
          if (s != NULL && *s != 0)
            fatal (_("invalid integer argument %s"), optarg);
            fatal (_("invalid integer argument %s"), optarg);
          break;
          break;
 
 
        case 'o':
        case 'o':
          print_addresses = TRUE;
          print_addresses = TRUE;
          address_radix = 8;
          address_radix = 8;
          break;
          break;
 
 
        case 't':
        case 't':
          print_addresses = TRUE;
          print_addresses = TRUE;
          if (optarg[1] != '\0')
          if (optarg[1] != '\0')
            usage (stderr, 1);
            usage (stderr, 1);
          switch (optarg[0])
          switch (optarg[0])
            {
            {
            case 'o':
            case 'o':
              address_radix = 8;
              address_radix = 8;
              break;
              break;
 
 
            case 'd':
            case 'd':
              address_radix = 10;
              address_radix = 10;
              break;
              break;
 
 
            case 'x':
            case 'x':
              address_radix = 16;
              address_radix = 16;
              break;
              break;
 
 
            default:
            default:
              usage (stderr, 1);
              usage (stderr, 1);
            }
            }
          break;
          break;
 
 
        case 'T':
        case 'T':
          target = optarg;
          target = optarg;
          break;
          break;
 
 
        case 'e':
        case 'e':
          if (optarg[1] != '\0')
          if (optarg[1] != '\0')
            usage (stderr, 1);
            usage (stderr, 1);
          encoding = optarg[0];
          encoding = optarg[0];
          break;
          break;
 
 
        case 'V':
        case 'V':
        case 'v':
        case 'v':
          print_version ("strings");
          print_version ("strings");
          break;
          break;
 
 
        case '?':
        case '?':
          usage (stderr, 1);
          usage (stderr, 1);
 
 
        default:
        default:
          numeric_opt = optind;
          numeric_opt = optind;
          break;
          break;
        }
        }
    }
    }
 
 
  if (numeric_opt != 0)
  if (numeric_opt != 0)
    {
    {
      string_min = (int) strtoul (argv[numeric_opt - 1] + 1, &s, 0);
      string_min = (int) strtoul (argv[numeric_opt - 1] + 1, &s, 0);
      if (s != NULL && *s != 0)
      if (s != NULL && *s != 0)
        fatal (_("invalid integer argument %s"), argv[numeric_opt - 1] + 1);
        fatal (_("invalid integer argument %s"), argv[numeric_opt - 1] + 1);
    }
    }
  if (string_min < 1)
  if (string_min < 1)
    fatal (_("invalid minimum string length %d"), string_min);
    fatal (_("invalid minimum string length %d"), string_min);
 
 
  switch (encoding)
  switch (encoding)
    {
    {
    case 'S':
    case 'S':
    case 's':
    case 's':
      encoding_bytes = 1;
      encoding_bytes = 1;
      break;
      break;
    case 'b':
    case 'b':
    case 'l':
    case 'l':
      encoding_bytes = 2;
      encoding_bytes = 2;
      break;
      break;
    case 'B':
    case 'B':
    case 'L':
    case 'L':
      encoding_bytes = 4;
      encoding_bytes = 4;
      break;
      break;
    default:
    default:
      usage (stderr, 1);
      usage (stderr, 1);
    }
    }
 
 
  bfd_init ();
  bfd_init ();
  set_default_bfd_target ();
  set_default_bfd_target ();
 
 
  if (optind >= argc)
  if (optind >= argc)
    {
    {
      datasection_only = FALSE;
      datasection_only = FALSE;
      SET_BINARY (fileno (stdin));
      SET_BINARY (fileno (stdin));
      print_strings ("{standard input}", stdin, 0, 0, 0, (char *) NULL);
      print_strings ("{standard input}", stdin, 0, 0, 0, (char *) NULL);
      files_given = TRUE;
      files_given = TRUE;
    }
    }
  else
  else
    {
    {
      for (; optind < argc; ++optind)
      for (; optind < argc; ++optind)
        {
        {
          if (strcmp (argv[optind], "-") == 0)
          if (strcmp (argv[optind], "-") == 0)
            datasection_only = FALSE;
            datasection_only = FALSE;
          else
          else
            {
            {
              files_given = TRUE;
              files_given = TRUE;
              exit_status |= strings_file (argv[optind]) == FALSE;
              exit_status |= strings_file (argv[optind]) == FALSE;
            }
            }
        }
        }
    }
    }
 
 
  if (!files_given)
  if (!files_given)
    usage (stderr, 1);
    usage (stderr, 1);
 
 
  return (exit_status);
  return (exit_status);
}
}


/* Scan section SECT of the file ABFD, whose printable name is in
/* Scan section SECT of the file ABFD, whose printable name is in
   ARG->filename and whose size might be in ARG->filesize.  If it
   ARG->filename and whose size might be in ARG->filesize.  If it
   contains initialized data set `got_a_section' and print the
   contains initialized data set `got_a_section' and print the
   strings in it.
   strings in it.
 
 
   FIXME: We ought to be able to return error codes/messages for
   FIXME: We ought to be able to return error codes/messages for
   certain conditions.  */
   certain conditions.  */
 
 
static void
static void
strings_a_section (bfd *abfd, asection *sect, void *arg)
strings_a_section (bfd *abfd, asection *sect, void *arg)
{
{
  filename_and_size_t * filename_and_sizep;
  filename_and_size_t * filename_and_sizep;
  bfd_size_type *filesizep;
  bfd_size_type *filesizep;
  bfd_size_type sectsize;
  bfd_size_type sectsize;
  void *mem;
  void *mem;
 
 
  if ((sect->flags & DATA_FLAGS) != DATA_FLAGS)
  if ((sect->flags & DATA_FLAGS) != DATA_FLAGS)
    return;
    return;
 
 
  sectsize = bfd_get_section_size (sect);
  sectsize = bfd_get_section_size (sect);
 
 
  if (sectsize <= 0)
  if (sectsize <= 0)
    return;
    return;
 
 
  /* Get the size of the file.  This might have been cached for us.  */
  /* Get the size of the file.  This might have been cached for us.  */
  filename_and_sizep = (filename_and_size_t *) arg;
  filename_and_sizep = (filename_and_size_t *) arg;
  filesizep = & filename_and_sizep->filesize;
  filesizep = & filename_and_sizep->filesize;
 
 
  if (*filesizep == 0)
  if (*filesizep == 0)
    {
    {
      struct stat st;
      struct stat st;
 
 
      if (bfd_stat (abfd, &st))
      if (bfd_stat (abfd, &st))
        return;
        return;
 
 
      /* Cache the result so that we do not repeatedly stat this file.  */
      /* Cache the result so that we do not repeatedly stat this file.  */
      *filesizep = st.st_size;
      *filesizep = st.st_size;
    }
    }
 
 
  /* Compare the size of the section against the size of the file.
  /* Compare the size of the section against the size of the file.
     If the section is bigger then the file must be corrupt and
     If the section is bigger then the file must be corrupt and
     we should not try dumping it.  */
     we should not try dumping it.  */
  if (sectsize >= *filesizep)
  if (sectsize >= *filesizep)
    return;
    return;
 
 
  mem = xmalloc (sectsize);
  mem = xmalloc (sectsize);
 
 
  if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sectsize))
  if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sectsize))
    {
    {
      got_a_section = TRUE;
      got_a_section = TRUE;
 
 
      print_strings (filename_and_sizep->filename, NULL, sect->filepos,
      print_strings (filename_and_sizep->filename, NULL, sect->filepos,
                     0, sectsize, (char *) mem);
                     0, sectsize, (char *) mem);
    }
    }
 
 
  free (mem);
  free (mem);
}
}
 
 
/* Scan all of the sections in FILE, and print the strings
/* Scan all of the sections in FILE, and print the strings
   in the initialized data section(s).
   in the initialized data section(s).
 
 
   Return TRUE if successful,
   Return TRUE if successful,
   FALSE if not (such as if FILE is not an object file).  */
   FALSE if not (such as if FILE is not an object file).  */
 
 
static bfd_boolean
static bfd_boolean
strings_object_file (const char *file)
strings_object_file (const char *file)
{
{
  filename_and_size_t filename_and_size;
  filename_and_size_t filename_and_size;
  bfd *abfd;
  bfd *abfd;
 
 
  abfd = bfd_openr (file, target);
  abfd = bfd_openr (file, target);
 
 
  if (abfd == NULL)
  if (abfd == NULL)
    /* Treat the file as a non-object file.  */
    /* Treat the file as a non-object file.  */
    return FALSE;
    return FALSE;
 
 
  /* This call is mainly for its side effect of reading in the sections.
  /* This call is mainly for its side effect of reading in the sections.
     We follow the traditional behavior of `strings' in that we don't
     We follow the traditional behavior of `strings' in that we don't
     complain if we don't recognize a file to be an object file.  */
     complain if we don't recognize a file to be an object file.  */
  if (!bfd_check_format (abfd, bfd_object))
  if (!bfd_check_format (abfd, bfd_object))
    {
    {
      bfd_close (abfd);
      bfd_close (abfd);
      return FALSE;
      return FALSE;
    }
    }
 
 
  got_a_section = FALSE;
  got_a_section = FALSE;
  filename_and_size.filename = file;
  filename_and_size.filename = file;
  filename_and_size.filesize = 0;
  filename_and_size.filesize = 0;
  bfd_map_over_sections (abfd, strings_a_section, & filename_and_size);
  bfd_map_over_sections (abfd, strings_a_section, & filename_and_size);
 
 
  if (!bfd_close (abfd))
  if (!bfd_close (abfd))
    {
    {
      bfd_nonfatal (file);
      bfd_nonfatal (file);
      return FALSE;
      return FALSE;
    }
    }
 
 
  return got_a_section;
  return got_a_section;
}
}
 
 
/* Print the strings in FILE.  Return TRUE if ok, FALSE if an error occurs.  */
/* Print the strings in FILE.  Return TRUE if ok, FALSE if an error occurs.  */
 
 
static bfd_boolean
static bfd_boolean
strings_file (char *file)
strings_file (char *file)
{
{
  statbuf st;
  statbuf st;
 
 
  if (file_stat (file, &st) < 0)
  if (file_stat (file, &st) < 0)
    {
    {
      if (errno == ENOENT)
      if (errno == ENOENT)
        non_fatal (_("'%s': No such file"), file);
        non_fatal (_("'%s': No such file"), file);
      else
      else
        non_fatal (_("Warning: could not locate '%s'.  reason: %s"),
        non_fatal (_("Warning: could not locate '%s'.  reason: %s"),
                   file, strerror (errno));
                   file, strerror (errno));
      return FALSE;
      return FALSE;
    }
    }
 
 
  /* If we weren't told to scan the whole file,
  /* If we weren't told to scan the whole file,
     try to open it as an object file and only look at
     try to open it as an object file and only look at
     initialized data sections.  If that fails, fall back to the
     initialized data sections.  If that fails, fall back to the
     whole file.  */
     whole file.  */
  if (!datasection_only || !strings_object_file (file))
  if (!datasection_only || !strings_object_file (file))
    {
    {
      FILE *stream;
      FILE *stream;
 
 
      stream = file_open (file, FOPEN_RB);
      stream = file_open (file, FOPEN_RB);
      if (stream == NULL)
      if (stream == NULL)
        {
        {
          fprintf (stderr, "%s: ", program_name);
          fprintf (stderr, "%s: ", program_name);
          perror (file);
          perror (file);
          return FALSE;
          return FALSE;
        }
        }
 
 
      print_strings (file, stream, (file_off) 0, 0, 0, (char *) 0);
      print_strings (file, stream, (file_off) 0, 0, 0, (char *) 0);
 
 
      if (fclose (stream) == EOF)
      if (fclose (stream) == EOF)
        {
        {
          fprintf (stderr, "%s: ", program_name);
          fprintf (stderr, "%s: ", program_name);
          perror (file);
          perror (file);
          return FALSE;
          return FALSE;
        }
        }
    }
    }
 
 
  return TRUE;
  return TRUE;
}
}


/* Read the next character, return EOF if none available.
/* Read the next character, return EOF if none available.
   Assume that STREAM is positioned so that the next byte read
   Assume that STREAM is positioned so that the next byte read
   is at address ADDRESS in the file.
   is at address ADDRESS in the file.
 
 
   If STREAM is NULL, do not read from it.
   If STREAM is NULL, do not read from it.
   The caller can supply a buffer of characters
   The caller can supply a buffer of characters
   to be processed before the data in STREAM.
   to be processed before the data in STREAM.
   MAGIC is the address of the buffer and
   MAGIC is the address of the buffer and
   MAGICCOUNT is how many characters are in it.  */
   MAGICCOUNT is how many characters are in it.  */
 
 
static long
static long
get_char (FILE *stream, file_off *address, int *magiccount, char **magic)
get_char (FILE *stream, file_off *address, int *magiccount, char **magic)
{
{
  int c, i;
  int c, i;
  long r = EOF;
  long r = EOF;
  unsigned char buf[4];
  unsigned char buf[4];
 
 
  for (i = 0; i < encoding_bytes; i++)
  for (i = 0; i < encoding_bytes; i++)
    {
    {
      if (*magiccount)
      if (*magiccount)
        {
        {
          (*magiccount)--;
          (*magiccount)--;
          c = *(*magic)++;
          c = *(*magic)++;
        }
        }
      else
      else
        {
        {
          if (stream == NULL)
          if (stream == NULL)
            return EOF;
            return EOF;
 
 
          /* Only use getc_unlocked if we found a declaration for it.
          /* Only use getc_unlocked if we found a declaration for it.
             Otherwise, libc is not thread safe by default, and we
             Otherwise, libc is not thread safe by default, and we
             should not use it.  */
             should not use it.  */
 
 
#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
          c = getc_unlocked (stream);
          c = getc_unlocked (stream);
#else
#else
          c = getc (stream);
          c = getc (stream);
#endif
#endif
          if (c == EOF)
          if (c == EOF)
            return EOF;
            return EOF;
        }
        }
 
 
      (*address)++;
      (*address)++;
      buf[i] = c;
      buf[i] = c;
    }
    }
 
 
  switch (encoding)
  switch (encoding)
    {
    {
    case 'S':
    case 'S':
    case 's':
    case 's':
      r = buf[0];
      r = buf[0];
      break;
      break;
    case 'b':
    case 'b':
      r = (buf[0] << 8) | buf[1];
      r = (buf[0] << 8) | buf[1];
      break;
      break;
    case 'l':
    case 'l':
      r = buf[0] | (buf[1] << 8);
      r = buf[0] | (buf[1] << 8);
      break;
      break;
    case 'B':
    case 'B':
      r = ((long) buf[0] << 24) | ((long) buf[1] << 16) |
      r = ((long) buf[0] << 24) | ((long) buf[1] << 16) |
        ((long) buf[2] << 8) | buf[3];
        ((long) buf[2] << 8) | buf[3];
      break;
      break;
    case 'L':
    case 'L':
      r = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) |
      r = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) |
        ((long) buf[3] << 24);
        ((long) buf[3] << 24);
      break;
      break;
    }
    }
 
 
  if (r == EOF)
  if (r == EOF)
    return 0;
    return 0;
 
 
  return r;
  return r;
}
}


/* Find the strings in file FILENAME, read from STREAM.
/* Find the strings in file FILENAME, read from STREAM.
   Assume that STREAM is positioned so that the next byte read
   Assume that STREAM is positioned so that the next byte read
   is at address ADDRESS in the file.
   is at address ADDRESS in the file.
   Stop reading at address STOP_POINT in the file, if nonzero.
   Stop reading at address STOP_POINT in the file, if nonzero.
 
 
   If STREAM is NULL, do not read from it.
   If STREAM is NULL, do not read from it.
   The caller can supply a buffer of characters
   The caller can supply a buffer of characters
   to be processed before the data in STREAM.
   to be processed before the data in STREAM.
   MAGIC is the address of the buffer and
   MAGIC is the address of the buffer and
   MAGICCOUNT is how many characters are in it.
   MAGICCOUNT is how many characters are in it.
   Those characters come at address ADDRESS and the data in STREAM follow.  */
   Those characters come at address ADDRESS and the data in STREAM follow.  */
 
 
static void
static void
print_strings (const char *filename, FILE *stream, file_off address,
print_strings (const char *filename, FILE *stream, file_off address,
               int stop_point, int magiccount, char *magic)
               int stop_point, int magiccount, char *magic)
{
{
  char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
  char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
 
 
  while (1)
  while (1)
    {
    {
      file_off start;
      file_off start;
      int i;
      int i;
      long c;
      long c;
 
 
      /* See if the next `string_min' chars are all graphic chars.  */
      /* See if the next `string_min' chars are all graphic chars.  */
    tryline:
    tryline:
      if (stop_point && address >= stop_point)
      if (stop_point && address >= stop_point)
        break;
        break;
      start = address;
      start = address;
      for (i = 0; i < string_min; i++)
      for (i = 0; i < string_min; i++)
        {
        {
          c = get_char (stream, &address, &magiccount, &magic);
          c = get_char (stream, &address, &magiccount, &magic);
          if (c == EOF)
          if (c == EOF)
            return;
            return;
          if (! STRING_ISGRAPHIC (c))
          if (! STRING_ISGRAPHIC (c))
            /* Found a non-graphic.  Try again starting with next char.  */
            /* Found a non-graphic.  Try again starting with next char.  */
            goto tryline;
            goto tryline;
          buf[i] = c;
          buf[i] = c;
        }
        }
 
 
      /* We found a run of `string_min' graphic characters.  Print up
      /* We found a run of `string_min' graphic characters.  Print up
         to the next non-graphic character.  */
         to the next non-graphic character.  */
 
 
      if (print_filenames)
      if (print_filenames)
        printf ("%s: ", filename);
        printf ("%s: ", filename);
      if (print_addresses)
      if (print_addresses)
        switch (address_radix)
        switch (address_radix)
          {
          {
          case 8:
          case 8:
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
            if (sizeof (start) > sizeof (long))
            if (sizeof (start) > sizeof (long))
              {
              {
#ifndef __MSVCRT__
#ifndef __MSVCRT__
                printf ("%7llo ", (unsigned long long) start);
                printf ("%7llo ", (unsigned long long) start);
#else
#else
                printf ("%7I64o ", (unsigned long long) start);
                printf ("%7I64o ", (unsigned long long) start);
#endif
#endif
              }
              }
            else
            else
#elif !BFD_HOST_64BIT_LONG
#elif !BFD_HOST_64BIT_LONG
            if (start != (unsigned long) start)
            if (start != (unsigned long) start)
              printf ("++%7lo ", (unsigned long) start);
              printf ("++%7lo ", (unsigned long) start);
            else
            else
#endif
#endif
              printf ("%7lo ", (unsigned long) start);
              printf ("%7lo ", (unsigned long) start);
            break;
            break;
 
 
          case 10:
          case 10:
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
            if (sizeof (start) > sizeof (long))
            if (sizeof (start) > sizeof (long))
              {
              {
#ifndef __MSVCRT__
#ifndef __MSVCRT__
                printf ("%7lld ", (unsigned long long) start);
                printf ("%7lld ", (unsigned long long) start);
#else
#else
                printf ("%7I64d ", (unsigned long long) start);
                printf ("%7I64d ", (unsigned long long) start);
#endif
#endif
              }
              }
            else
            else
#elif !BFD_HOST_64BIT_LONG
#elif !BFD_HOST_64BIT_LONG
            if (start != (unsigned long) start)
            if (start != (unsigned long) start)
              printf ("++%7ld ", (unsigned long) start);
              printf ("++%7ld ", (unsigned long) start);
            else
            else
#endif
#endif
              printf ("%7ld ", (long) start);
              printf ("%7ld ", (long) start);
            break;
            break;
 
 
          case 16:
          case 16:
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
            if (sizeof (start) > sizeof (long))
            if (sizeof (start) > sizeof (long))
              {
              {
#ifndef __MSVCRT__
#ifndef __MSVCRT__
                printf ("%7llx ", (unsigned long long) start);
                printf ("%7llx ", (unsigned long long) start);
#else
#else
                printf ("%7I64x ", (unsigned long long) start);
                printf ("%7I64x ", (unsigned long long) start);
#endif
#endif
              }
              }
            else
            else
#elif !BFD_HOST_64BIT_LONG
#elif !BFD_HOST_64BIT_LONG
            if (start != (unsigned long) start)
            if (start != (unsigned long) start)
              printf ("%lx%8.8lx ", (unsigned long) (start >> 32),
              printf ("%lx%8.8lx ", (unsigned long) (start >> 32),
                      (unsigned long) (start & 0xffffffff));
                      (unsigned long) (start & 0xffffffff));
            else
            else
#endif
#endif
              printf ("%7lx ", (unsigned long) start);
              printf ("%7lx ", (unsigned long) start);
            break;
            break;
          }
          }
 
 
      buf[i] = '\0';
      buf[i] = '\0';
      fputs (buf, stdout);
      fputs (buf, stdout);
 
 
      while (1)
      while (1)
        {
        {
          c = get_char (stream, &address, &magiccount, &magic);
          c = get_char (stream, &address, &magiccount, &magic);
          if (c == EOF)
          if (c == EOF)
            break;
            break;
          if (! STRING_ISGRAPHIC (c))
          if (! STRING_ISGRAPHIC (c))
            break;
            break;
          putchar (c);
          putchar (c);
        }
        }
 
 
      putchar ('\n');
      putchar ('\n');
    }
    }
}
}


static void
static void
usage (FILE *stream, int status)
usage (FILE *stream, int status)
{
{
  fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
  fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
  fprintf (stream, _(" Display printable strings in [file(s)] (stdin by default)\n"));
  fprintf (stream, _(" Display printable strings in [file(s)] (stdin by default)\n"));
  fprintf (stream, _(" The options are:\n\
  fprintf (stream, _(" The options are:\n\
  -a - --all                Scan the entire file, not just the data section\n\
  -a - --all                Scan the entire file, not just the data section\n\
  -f --print-file-name      Print the name of the file before each string\n\
  -f --print-file-name      Print the name of the file before each string\n\
  -n --bytes=[number]       Locate & print any NUL-terminated sequence of at\n\
  -n --bytes=[number]       Locate & print any NUL-terminated sequence of at\n\
  -<number>                   least [number] characters (default 4).\n\
  -<number>                   least [number] characters (default 4).\n\
  -t --radix={o,d,x}        Print the location of the string in base 8, 10 or 16\n\
  -t --radix={o,d,x}        Print the location of the string in base 8, 10 or 16\n\
  -o                        An alias for --radix=o\n\
  -o                        An alias for --radix=o\n\
  -T --target=<BFDNAME>     Specify the binary file format\n\
  -T --target=<BFDNAME>     Specify the binary file format\n\
  -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
  -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
                            s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
                            s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
  @<file>                   Read options from <file>\n\
  @<file>                   Read options from <file>\n\
  -h --help                 Display this information\n\
  -h --help                 Display this information\n\
  -v -V --version           Print the program's version number\n"));
  -v -V --version           Print the program's version number\n"));
  list_supported_targets (program_name, stream);
  list_supported_targets (program_name, stream);
  if (REPORT_BUGS_TO[0] && status == 0)
  if (REPORT_BUGS_TO[0] && status == 0)
    fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
    fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  exit (status);
  exit (status);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.