/* strings -- print the strings of printable characters in files
- Copyright (C) 1993-2018 Free Software Foundation, Inc.
+ Copyright (C) 1993-2021 Free Software Foundation, Inc.
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
static int string_min;
/* Whether or not we include all whitespace as a graphic char. */
-static bfd_boolean include_all_whitespace;
+static bool include_all_whitespace;
/* TRUE means print address within file for each string. */
-static bfd_boolean print_addresses;
+static bool print_addresses;
/* TRUE means print filename for each string. */
-static bfd_boolean print_filenames;
+static bool print_filenames;
/* TRUE means for object files scan only the data section. */
-static bfd_boolean datasection_only;
+static bool datasection_only;
/* The BFD object file format. */
static char *target;
{NULL, 0, NULL, 0}
};
-static bfd_boolean strings_file (char *);
+static bool strings_file (char *);
static void print_strings (const char *, FILE *, file_ptr, int, int, char *);
static void usage (FILE *, int) ATTRIBUTE_NORETURN;
\f
{
int optc;
int exit_status = 0;
- bfd_boolean files_given = FALSE;
+ bool files_given = false;
char *s;
int numeric_opt = 0;
-#if defined (HAVE_SETLOCALE)
setlocale (LC_ALL, "");
-#endif
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
expandargv (&argc, &argv);
string_min = 4;
- include_all_whitespace = FALSE;
- print_addresses = FALSE;
- print_filenames = FALSE;
+ include_all_whitespace = false;
+ print_addresses = false;
+ print_filenames = false;
if (DEFAULT_STRINGS_ALL)
- datasection_only = FALSE;
+ datasection_only = false;
else
- datasection_only = TRUE;
+ datasection_only = true;
target = NULL;
encoding = 's';
output_separator = NULL;
switch (optc)
{
case 'a':
- datasection_only = FALSE;
+ datasection_only = false;
break;
case 'd':
- datasection_only = TRUE;
+ datasection_only = true;
break;
case 'f':
- print_filenames = TRUE;
+ print_filenames = true;
break;
case 'H':
break;
case 'w':
- include_all_whitespace = TRUE;
+ include_all_whitespace = true;
break;
case 'o':
- print_addresses = TRUE;
+ print_addresses = true;
address_radix = 8;
break;
case 't':
- print_addresses = TRUE;
+ print_addresses = true;
if (optarg[1] != '\0')
usage (stderr, 1);
switch (optarg[0])
usage (stderr, 1);
}
- bfd_init ();
+ if (bfd_init () != BFD_INIT_MAGIC)
+ fatal (_("fatal error: libbfd ABI mismatch"));
set_default_bfd_target ();
if (optind >= argc)
{
- datasection_only = FALSE;
+ datasection_only = false;
SET_BINARY (fileno (stdin));
print_strings ("{standard input}", stdin, 0, 0, 0, (char *) NULL);
- files_given = TRUE;
+ files_given = true;
}
else
{
for (; optind < argc; ++optind)
{
if (strcmp (argv[optind], "-") == 0)
- datasection_only = FALSE;
+ datasection_only = false;
else
{
- files_given = TRUE;
+ files_given = true;
exit_status |= !strings_file (argv[optind]);
}
}
static void
strings_a_section (bfd *abfd, asection *sect, const char *filename,
- bfd_boolean *got_a_section)
+ bool *got_a_section)
{
bfd_size_type sectsize;
bfd_byte *mem;
if ((sect->flags & DATA_FLAGS) != DATA_FLAGS)
return;
- sectsize = bfd_get_section_size (sect);
+ sectsize = bfd_section_size (sect);
if (sectsize == 0)
return;
return;
}
- *got_a_section = TRUE;
+ *got_a_section = true;
print_strings (filename, NULL, sect->filepos, 0, sectsize, (char *) mem);
free (mem);
}
Return TRUE if successful,
FALSE if not (such as if FILE is not an object file). */
-static bfd_boolean
+static bool
strings_object_file (const char *file)
{
bfd *abfd;
asection *s;
- bfd_boolean got_a_section;
+ bool got_a_section;
abfd = bfd_openr (file, target);
if (abfd == NULL)
/* 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.
We follow the traditional behavior of `strings' in that we don't
if (!bfd_check_format (abfd, bfd_object))
{
bfd_close (abfd);
- return FALSE;
+ return false;
}
- got_a_section = FALSE;
+ got_a_section = false;
for (s = abfd->sections; s != NULL; s = s->next)
strings_a_section (abfd, s, file, &got_a_section);
if (!bfd_close (abfd))
{
bfd_nonfatal (file);
- return FALSE;
+ return false;
}
return got_a_section;
/* Print the strings in FILE. Return TRUE if ok, FALSE if an error occurs. */
-static bfd_boolean
+static bool
strings_file (char *file)
{
struct stat st;
else
non_fatal (_("Warning: could not locate '%s'. reason: %s"),
file, strerror (errno));
- return FALSE;
+ return false;
}
else if (S_ISDIR (st.st_mode))
{
non_fatal (_("Warning: '%s' is a directory"), file);
- return FALSE;
+ return false;
}
/* If we weren't told to scan the whole file,
{
fprintf (stderr, "%s: ", program_name);
perror (file);
- return FALSE;
+ return false;
}
print_strings (file, stream, (file_ptr) 0, 0, 0, (char *) 0);
{
fprintf (stderr, "%s: ", program_name);
perror (file);
- return FALSE;
+ return false;
}
}
- return TRUE;
+ return true;
}
\f
/* Read the next character, return EOF if none available.
return r;
}
+
+/* Throw away one byte of a (possibly) multi-byte char C, updating
+ address and buffer to suit. */
+
+static void
+unget_part_char (long c, file_ptr *address, int *magiccount, char **magic)
+{
+ static char tmp[4];
+
+ if (encoding_bytes > 1)
+ {
+ *address -= encoding_bytes - 1;
+
+ if (*magiccount == 0)
+ {
+ /* If no magic buffer exists, use temp buffer. */
+ switch (encoding)
+ {
+ default:
+ break;
+ case 'b':
+ tmp[0] = c & 0xff;
+ *magiccount = 1;
+ break;
+ case 'l':
+ tmp[0] = (c >> 8) & 0xff;
+ *magiccount = 1;
+ break;
+ case 'B':
+ tmp[0] = (c >> 16) & 0xff;
+ tmp[1] = (c >> 8) & 0xff;
+ tmp[2] = c & 0xff;
+ *magiccount = 3;
+ break;
+ case 'L':
+ tmp[0] = (c >> 8) & 0xff;
+ tmp[1] = (c >> 16) & 0xff;
+ tmp[2] = (c >> 24) & 0xff;
+ *magiccount = 3;
+ break;
+ }
+ *magic = tmp;
+ }
+ else
+ {
+ /* If magic buffer exists, rewind. */
+ *magic -= encoding_bytes - 1;
+ *magiccount += encoding_bytes - 1;
+ }
+ }
+}
\f
/* Find the strings in file FILENAME, read from STREAM.
Assume that STREAM is positioned so that the next byte read
free (buf);
return;
}
+
if (! STRING_ISGRAPHIC (c))
- /* Found a non-graphic. Try again starting with next char. */
- goto tryline;
+ {
+ /* Found a non-graphic. Try again starting with next byte. */
+ unget_part_char (c, &address, &magiccount, &magic);
+ goto tryline;
+ }
buf[i] = c;
}
switch (address_radix)
{
case 8:
-#ifdef HAVE_LONG_LONG
if (sizeof (start) > sizeof (long))
{
-# ifndef __MSVCRT__
- printf ("%7llo ", (unsigned long long) start);
-# else
- printf ("%7I64o ", (unsigned long long) start);
-# endif
+#ifndef __MSVCRT__
+ printf ("%7llo ", (unsigned long long) start);
+#else
+ printf ("%7I64o ", (unsigned long long) start);
+#endif
}
else
-#elif !BFD_HOST_64BIT_LONG
- if (start != (unsigned long) start)
- printf ("++%7lo ", (unsigned long) start);
- else
-#endif
printf ("%7lo ", (unsigned long) start);
break;
case 10:
-#ifdef HAVE_LONG_LONG
if (sizeof (start) > sizeof (long))
{
-# ifndef __MSVCRT__
- printf ("%7lld ", (unsigned long long) start);
-# else
- printf ("%7I64d ", (unsigned long long) start);
-# endif
+#ifndef __MSVCRT__
+ printf ("%7llu ", (unsigned long long) start);
+#else
+ printf ("%7I64d ", (unsigned long long) start);
+#endif
}
else
-#elif !BFD_HOST_64BIT_LONG
- if (start != (unsigned long) start)
- printf ("++%7ld ", (unsigned long) start);
- else
-#endif
printf ("%7ld ", (long) start);
break;
case 16:
-#ifdef HAVE_LONG_LONG
if (sizeof (start) > sizeof (long))
{
-# ifndef __MSVCRT__
- printf ("%7llx ", (unsigned long long) start);
-# else
- printf ("%7I64x ", (unsigned long long) start);
-# endif
+#ifndef __MSVCRT__
+ printf ("%7llx ", (unsigned long long) start);
+#else
+ printf ("%7I64x ", (unsigned long long) start);
+#endif
}
else
-#elif !BFD_HOST_64BIT_LONG
- if (start != (unsigned long) start)
- printf ("%lx%8.8lx ", (unsigned long) (start >> 32),
- (unsigned long) (start & 0xffffffff));
- else
-#endif
printf ("%7lx ", (unsigned long) start);
break;
}
if (c == EOF)
break;
if (! STRING_ISGRAPHIC (c))
- break;
+ {
+ unget_part_char (c, &address, &magiccount, &magic);
+ break;
+ }
putchar (c);
}
if (output_separator)
- fputs (output_separator, stdout);
+ fputs (output_separator, stdout);
else
- putchar ('\n');
+ putchar ('\n');
}
free (buf);
}