Back to index

cell-binutils  2.17cvs20070401
Classes | Defines | Typedefs | Functions | Variables
strings.c File Reference
#include "bfd.h"
#include <stdio.h>
#include "getopt.h"
#include <errno.h>
#include "bucomm.h"
#include "libiberty.h"
#include "safe-ctype.h"
#include <sys/stat.h>

Go to the source code of this file.

Classes

struct  filename_and_size_t

Defines

#define STRING_ISGRAPHIC(c)
#define DATA_FLAGS   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
#define file_open(s, m)   fopen(s, m)
#define file_stat(f, s)   stat(f, s)

Typedefs

typedef off_t file_off
typedef struct stat

Functions

static void strings_a_section (bfd *, asection *, void *)
static bfd_boolean strings_object_file (const char *)
static bfd_boolean strings_file (char *file)
static int integer_arg (char *s)
static void print_strings (const char *, FILE *, file_off, int, int, char *)
static void usage (FILE *, int)
static long get_char (FILE *, file_off *, int *, char **)
int main (int, char **)

Variables

int errno
static int address_radix
static int string_min
static bfd_boolean print_addresses
static bfd_boolean print_filenames
static bfd_boolean datasection_only
static bfd_boolean got_a_section
static char * target
static char encoding
static int encoding_bytes
static struct option []

Class Documentation

struct filename_and_size_t

Definition at line 158 of file strings.c.

Class Members
const char * filename
bfd_size_type filesize

Define Documentation

Definition at line 100 of file strings.c.

#define file_open (   s,
 
)    fopen(s, m)

Definition at line 107 of file strings.c.

#define file_stat (   f,
  s 
)    stat(f, s)

Definition at line 114 of file strings.c.

#define STRING_ISGRAPHIC (   c)
Value:
(   (c) >= 0 \
       && (c) <= 255 \
       && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127)))

Definition at line 90 of file strings.c.


Typedef Documentation

typedef off_t file_off

Definition at line 106 of file strings.c.

typedef struct stat

Definition at line 113 of file strings.c.


Function Documentation

static long get_char ( FILE *  stream,
file_off address,
int magiccount,
char **  magic 
) [static]

Definition at line 486 of file strings.c.

{
  int c, i;
  long r = EOF;
  unsigned char buf[4];

  for (i = 0; i < encoding_bytes; i++)
    {
      if (*magiccount)
       {
         (*magiccount)--;
         c = *(*magic)++;
       }
      else
       {
         if (stream == NULL)
           return EOF;

         /* Only use getc_unlocked if we found a declaration for it.
            Otherwise, libc is not thread safe by default, and we
            should not use it.  */

#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
         c = getc_unlocked (stream);
#else
         c = getc (stream);
#endif
         if (c == EOF)
           return EOF;
       }

      (*address)++;
      buf[i] = c;
    }

  switch (encoding)
    {
    case 'S':
    case 's':
      r = buf[0];
      break;
    case 'b':
      r = (buf[0] << 8) | buf[1];
      break;
    case 'l':
      r = buf[0] | (buf[1] << 8);
      break;
    case 'B':
      r = ((long) buf[0] << 24) | ((long) buf[1] << 16) |
       ((long) buf[2] << 8) | buf[3];
      break;
    case 'L':
      r = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) |
       ((long) buf[3] << 24);
      break;
    }

  if (r == EOF)
    return 0;

  return r;
}

Here is the caller graph for this function:

static int integer_arg ( char *  s) [static]

Definition at line 665 of file strings.c.

{
  int value;
  int radix = 10;
  char *p = s;
  int c;

  if (*p != '0')
    radix = 10;
  else if (*++p == 'x')
    {
      radix = 16;
      p++;
    }
  else
    radix = 8;

  value = 0;
  while (((c = *p++) >= '0' && c <= '9')
        || (radix == 16 && (c & ~40) >= 'A' && (c & ~40) <= 'Z'))
    {
      value *= radix;
      if (c >= '0' && c <= '9')
       value += c - '0';
      else
       value += (c & ~40) - 'A';
    }

  if (c == 'b')
    value *= 512;
  else if (c == 'B')
    value *= 1024;
  else
    p--;

  if (*p)
    fatal (_("invalid integer argument %s"), s);

  return value;
}

Here is the call graph for this function:

int main ( int  argc,
char **  argv 
)
static void print_strings ( const char *  filename,
FILE *  stream,
file_off  address,
int  stop_point,
int  magiccount,
char *  magic 
) [static]

Definition at line 562 of file strings.c.

{
  char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));

  while (1)
    {
      file_off start;
      int i;
      long c;

      /* See if the next `string_min' chars are all graphic chars.  */
    tryline:
      if (stop_point && address >= stop_point)
       break;
      start = address;
      for (i = 0; i < string_min; i++)
       {
         c = get_char (stream, &address, &magiccount, &magic);
         if (c == EOF)
           return;
         if (! STRING_ISGRAPHIC (c))
           /* Found a non-graphic.  Try again starting with next char.  */
           goto tryline;
         buf[i] = c;
       }

      /* We found a run of `string_min' graphic characters.  Print up
        to the next non-graphic character.  */

      if (print_filenames)
       printf ("%s: ", filename);
      if (print_addresses)
       switch (address_radix)
         {
         case 8:
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
           if (sizeof (start) > sizeof (long))
             printf ("%7Lo ", (unsigned long long) start);
           else
#else
# if !BFD_HOST_64BIT_LONG
           if (start != (unsigned long) start)
             printf ("++%7lo ", (unsigned long) start);
           else
# endif
#endif
             printf ("%7lo ", (unsigned long) start);
           break;

         case 10:
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
           if (sizeof (start) > sizeof (long))
             printf ("%7Ld ", (unsigned long long) start);
           else
#else
# if !BFD_HOST_64BIT_LONG
           if (start != (unsigned long) start)
             printf ("++%7ld ", (unsigned long) start);
           else
# endif
#endif
             printf ("%7ld ", (long) start);
           break;

         case 16:
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
           if (sizeof (start) > sizeof (long))
             printf ("%7Lx ", (unsigned long long) start);
           else
#else
# if !BFD_HOST_64BIT_LONG
           if (start != (unsigned long) start)
             printf ("%lx%8.8lx ", (unsigned long) (start >> 32),
                    (unsigned long) (start & 0xffffffff));
           else
# endif
#endif
             printf ("%7lx ", (unsigned long) start);
           break;
         }

      buf[i] = '\0';
      fputs (buf, stdout);

      while (1)
       {
         c = get_char (stream, &address, &magiccount, &magic);
         if (c == EOF)
           break;
         if (! STRING_ISGRAPHIC (c))
           break;
         putchar (c);
       }

      putchar ('\n');
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void strings_a_section ( bfd abfd,
asection sect,
void *  arg 
) [static]

Definition at line 339 of file strings.c.

{
  filename_and_size_t * filename_and_sizep;
  bfd_size_type *filesizep;
  bfd_size_type sectsize;
  void *mem;
     
  if ((sect->flags & DATA_FLAGS) != DATA_FLAGS)
    return;

  sectsize = bfd_get_section_size (sect);
     
  if (sectsize <= 0)
    return;

  /* Get the size of the file.  This might have been cached for us.  */
  filename_and_sizep = (filename_and_size_t *) arg;
  filesizep = & filename_and_sizep->filesize;

  if (*filesizep == 0)
    {
      struct stat st;
      
      if (bfd_stat (abfd, &st))
       return;

      /* Cache the result so that we do not repeatedly stat this file.  */
      *filesizep = st.st_size;
    }

  /* Compare the size of the section against the size of the file.
     If the section is bigger then the file must be corrupt and
     we should not try dumping it.  */
  if (sectsize >= *filesizep)
    return;

  mem = xmalloc (sectsize);

  if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sectsize))
    {
      got_a_section = TRUE;

      print_strings (filename_and_sizep->filename, NULL, sect->filepos,
                   0, sectsize, mem);
    }

  free (mem);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bfd_boolean strings_file ( char *  file) [static]

Definition at line 432 of file strings.c.

{
  statbuf st;

  if (file_stat (file, &st) < 0)
    {
      if (errno == ENOENT)
       non_fatal (_("'%s': No such file"), file);
      else
       non_fatal (_("Warning: could not locate '%s'.  reason: %s"),
                 file, strerror (errno));
      return FALSE;
    }

  /* If we weren't told to scan the whole file,
     try to open it as an object file and only look at
     initialized data sections.  If that fails, fall back to the
     whole file.  */
  if (!datasection_only || !strings_object_file (file))
    {
      FILE *stream;

      stream = file_open (file, FOPEN_RB);
      if (stream == NULL)
       {
         fprintf (stderr, "%s: ", program_name);
         perror (file);
         return FALSE;
       }

      print_strings (file, stream, (file_off) 0, 0, 0, (char *) 0);

      if (fclose (stream) == EOF)
       {
         fprintf (stderr, "%s: ", program_name);
         perror (file);
         return FALSE;
       }
    }

  return TRUE;
}

Here is the call graph for this function:

static bfd_boolean strings_object_file ( const char *  file) [static]

Definition at line 395 of file strings.c.

{
  filename_and_size_t filename_and_size;
  bfd *abfd;

  abfd = bfd_openr (file, target);

  if (abfd == NULL)
    /* Treat the file as a non-object file.  */
    return FALSE;

  /* 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
     complain if we don't recognize a file to be an object file.  */
  if (!bfd_check_format (abfd, bfd_object))
    {
      bfd_close (abfd);
      return FALSE;
    }

  got_a_section = FALSE;
  filename_and_size.filename = file;
  filename_and_size.filesize = 0;
  bfd_map_over_sections (abfd, strings_a_section, & filename_and_size);

  if (!bfd_close (abfd))
    {
      bfd_nonfatal (file);
      return FALSE;
    }

  return got_a_section;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void usage ( FILE *  stream,
int  status 
) [static]

Definition at line 707 of file strings.c.

{
  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, _(" The options are:\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\
  -n --bytes=[number]       Locate & print any NUL-terminated sequence of at\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\
  -o                        An alias for --radix=o\n\
  -T --target=<BFDNAME>     Specify the binary file format\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\
  @<file>                   Read options from <file>\n\
  -h --help                 Display this information\n\
  -v --version              Print the program's version number\n"));
  list_supported_targets (program_name, stream);
  if (REPORT_BUGS_TO[0] && status == 0)
    fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  exit (status);
}

Here is the call graph for this function:


Variable Documentation

int address_radix [static]

Definition at line 118 of file strings.c.

Definition at line 130 of file strings.c.

char encoding [static]

Definition at line 139 of file strings.c.

int encoding_bytes [static]

Definition at line 140 of file strings.c.

Definition at line 133 of file strings.c.

struct option[] [static]
Initial value:
{
  {"all", no_argument, NULL, 'a'},
  {"print-file-name", no_argument, NULL, 'f'},
  {"bytes", required_argument, NULL, 'n'},
  {"radix", required_argument, NULL, 't'},
  {"encoding", required_argument, NULL, 'e'},
  {"target", required_argument, NULL, 'T'},
  {"help", no_argument, NULL, 'h'},
  {"version", no_argument, NULL, 'v'},
  {NULL, 0, NULL, 0}
}

Definition at line 142 of file strings.c.

Definition at line 124 of file strings.c.

Definition at line 127 of file strings.c.

int string_min [static]

Definition at line 121 of file strings.c.

char* target [static]

Definition at line 136 of file strings.c.