1 /* strings -- print the strings of printable characters in files
2 Copyright (C) 1993-2022 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19 /* Usage: strings [options] file...
24 - Scan each file in its entirety.
27 -d Scan only the initialized data section(s) of object files.
30 -f Print the name of the file before each string.
34 -min-len Print graphic char sequences, MIN-LEN or more bytes long,
35 that are followed by a NUL or a non-displayable character.
39 -t {o,x,d} Print the offset within the file before each string,
42 --include-all-whitespace
43 -w By default tab and space are the only whitepace included in graphic
44 char sequences. This option considers all of isspace() valid.
46 -o Like -to. (Some other implementations have -o like -to,
47 others like -td. We chose one arbitrarily.)
49 --encoding={s,S,b,l,B,L}
51 Select character encoding: 7-bit-character, 8-bit-character,
52 bigendian 16-bit, littleendian 16-bit, bigendian 32-bit,
57 Specify a non-default object file format.
59 --unicode={default|locale|invalid|hex|escape|highlight}
61 Determine how to handle UTF-8 unicode characters. The default
62 is no special treatment. All other versions of this option
63 only apply if the encoding is valid and enabling the option
65 The 'locale' option displays the characters according to the
66 current locale. The 'invalid' option treats them as
67 non-string characters. The 'hex' option displays them as hex
68 byte sequences. The 'escape' option displays them as escape
69 sequences and the 'highlight' option displays them as
70 coloured escape sequences.
72 --output-separator=sep_string
73 -s sep_string String used to separate parsed strings in output.
77 -h Print the usage message on the standard output.
81 -v Print the program version number.
83 Written by Richard Stallman <rms@gnu.ai.mit.edu>
84 and David MacKenzie <djm@gnu.ai.mit.edu>. */
89 #include "libiberty.h"
90 #include "safe-ctype.h"
94 #define streq(a,b) (strcmp ((a),(b)) == 0)
97 typedef enum unicode_display_type
105 } unicode_display_type
;
107 static unicode_display_type unicode_display
= unicode_default
;
109 #define STRING_ISGRAPHIC(c) \
112 && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127) \
113 || (include_all_whitespace && ISSPACE (c))) \
120 /* The BFD section flags that identify an initialized data section. */
121 #define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
123 /* Radix for printing addresses (must be 8, 10 or 16). */
124 static int address_radix
;
126 /* Minimum length of sequence of graphic chars to trigger output. */
127 static unsigned int string_min
;
129 /* Whether or not we include all whitespace as a graphic char. */
130 static bool include_all_whitespace
;
132 /* TRUE means print address within file for each string. */
133 static bool print_addresses
;
135 /* TRUE means print filename for each string. */
136 static bool print_filenames
;
138 /* TRUE means for object files scan only the data section. */
139 static bool datasection_only
;
141 /* The BFD object file format. */
144 /* The character encoding format. */
145 static char encoding
;
146 static int encoding_bytes
;
148 /* Output string used to separate parsed strings */
149 static char *output_separator
;
151 static struct option long_options
[] =
153 {"all", no_argument
, NULL
, 'a'},
154 {"bytes", required_argument
, NULL
, 'n'},
155 {"data", no_argument
, NULL
, 'd'},
156 {"encoding", required_argument
, NULL
, 'e'},
157 {"help", no_argument
, NULL
, 'h'},
158 {"include-all-whitespace", no_argument
, NULL
, 'w'},
159 {"output-separator", required_argument
, NULL
, 's'},
160 {"print-file-name", no_argument
, NULL
, 'f'},
161 {"radix", required_argument
, NULL
, 't'},
162 {"target", required_argument
, NULL
, 'T'},
163 {"unicode", required_argument
, NULL
, 'U'},
164 {"version", no_argument
, NULL
, 'v'},
168 static bool strings_file (char *);
169 static void print_strings (const char *, FILE *, file_ptr
, int, char *);
170 static void usage (FILE *, int) ATTRIBUTE_NORETURN
;
172 int main (int, char **);
175 main (int argc
, char **argv
)
179 bool files_given
= false;
183 setlocale (LC_ALL
, "");
184 bindtextdomain (PACKAGE
, LOCALEDIR
);
185 textdomain (PACKAGE
);
187 program_name
= argv
[0];
188 xmalloc_set_program_name (program_name
);
189 bfd_set_error_program_name (program_name
);
191 expandargv (&argc
, &argv
);
194 include_all_whitespace
= false;
195 print_addresses
= false;
196 print_filenames
= false;
197 if (DEFAULT_STRINGS_ALL
)
198 datasection_only
= false;
200 datasection_only
= true;
203 output_separator
= NULL
;
205 while ((optc
= getopt_long (argc
, argv
, "adfhHn:wot:e:T:s:U:Vv0123456789",
206 long_options
, (int *) 0)) != EOF
)
211 datasection_only
= false;
215 datasection_only
= true;
219 print_filenames
= true;
227 string_min
= (int) strtoul (optarg
, &s
, 0);
228 if (s
!= NULL
&& *s
!= 0)
229 fatal (_("invalid integer argument %s"), optarg
);
233 include_all_whitespace
= true;
237 print_addresses
= true;
242 print_addresses
= true;
243 if (optarg
[1] != '\0')
269 if (optarg
[1] != '\0')
271 encoding
= optarg
[0];
275 output_separator
= optarg
;
279 if (streq (optarg
, "default") || streq (optarg
, "d"))
280 unicode_display
= unicode_default
;
281 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
282 unicode_display
= unicode_locale
;
283 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
284 unicode_display
= unicode_escape
;
285 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
286 unicode_display
= unicode_invalid
;
287 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
288 unicode_display
= unicode_hex
;
289 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
290 unicode_display
= unicode_highlight
;
292 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
297 print_version ("strings");
304 numeric_opt
= optind
;
309 if (unicode_display
!= unicode_default
)
312 if (numeric_opt
!= 0)
314 string_min
= (int) strtoul (argv
[numeric_opt
- 1] + 1, &s
, 0);
315 if (s
!= NULL
&& *s
!= 0)
316 fatal (_("invalid integer argument %s"), argv
[numeric_opt
- 1] + 1);
319 fatal (_("invalid minimum string length %d"), string_min
);
339 if (bfd_init () != BFD_INIT_MAGIC
)
340 fatal (_("fatal error: libbfd ABI mismatch"));
341 set_default_bfd_target ();
345 datasection_only
= false;
346 SET_BINARY (fileno (stdin
));
347 print_strings ("{standard input}", stdin
, 0, 0, (char *) NULL
);
352 for (; optind
< argc
; ++optind
)
354 if (streq (argv
[optind
], "-"))
355 datasection_only
= false;
359 exit_status
|= !strings_file (argv
[optind
]);
367 return (exit_status
);
370 /* Scan section SECT of the file ABFD, whose printable name is
371 FILENAME. If it contains initialized data set GOT_A_SECTION and
372 print the strings in it. */
375 strings_a_section (bfd
*abfd
, asection
*sect
, const char *filename
,
378 bfd_size_type sectsize
;
381 if ((sect
->flags
& DATA_FLAGS
) != DATA_FLAGS
)
384 sectsize
= bfd_section_size (sect
);
388 if (!bfd_malloc_and_get_section (abfd
, sect
, &mem
))
390 non_fatal (_("%s: Reading section %s failed: %s"),
391 filename
, sect
->name
, bfd_errmsg (bfd_get_error ()));
395 *got_a_section
= true;
396 print_strings (filename
, NULL
, sect
->filepos
, sectsize
, (char *) mem
);
400 /* Scan all of the sections in FILE, and print the strings
401 in the initialized data section(s).
403 Return TRUE if successful,
404 FALSE if not (such as if FILE is not an object file). */
407 strings_object_file (const char *file
)
413 abfd
= bfd_openr (file
, target
);
416 /* Treat the file as a non-object file. */
419 /* This call is mainly for its side effect of reading in the sections.
420 We follow the traditional behavior of `strings' in that we don't
421 complain if we don't recognize a file to be an object file. */
422 if (!bfd_check_format (abfd
, bfd_object
))
428 got_a_section
= false;
429 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
430 strings_a_section (abfd
, s
, file
, &got_a_section
);
432 if (!bfd_close (abfd
))
438 return got_a_section
;
441 /* Print the strings in FILE. Return TRUE if ok, FALSE if an error occurs. */
444 strings_file (char *file
)
448 /* get_file_size does not support non-S_ISREG files. */
450 if (stat (file
, &st
) < 0)
453 non_fatal (_("'%s': No such file"), file
);
455 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
456 file
, strerror (errno
));
459 else if (S_ISDIR (st
.st_mode
))
461 non_fatal (_("Warning: '%s' is a directory"), file
);
465 /* If we weren't told to scan the whole file,
466 try to open it as an object file and only look at
467 initialized data sections. If that fails, fall back to the
469 if (!datasection_only
|| !strings_object_file (file
))
473 stream
= fopen (file
, FOPEN_RB
);
476 fprintf (stderr
, "%s: ", program_name
);
481 print_strings (file
, stream
, (file_ptr
) 0, 0, (char *) NULL
);
483 if (fclose (stream
) == EOF
)
485 fprintf (stderr
, "%s: ", program_name
);
494 /* Read the next character, return EOF if none available.
495 Assume that STREAM is positioned so that the next byte read
496 is at address ADDRESS in the file.
498 If STREAM is NULL, do not read from it.
499 The caller can supply a buffer of characters
500 to be processed before the data in STREAM.
501 MAGIC is the address of the buffer and
502 MAGICCOUNT is how many characters are in it. */
505 get_char (FILE *stream
, file_ptr
*address
, int *magiccount
, char **magic
)
510 for (i
= 0; i
< encoding_bytes
; i
++)
522 /* Only use getc_unlocked if we found a declaration for it.
523 Otherwise, libc is not thread safe by default, and we
524 should not use it. */
526 #if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
527 c
= getc_unlocked (stream
);
536 r
= (r
<< 8) | (c
& 0xff);
544 r
= ((r
& 0xff) << 8) | ((r
& 0xff00) >> 8);
547 r
= (((r
& 0xff) << 24) | ((r
& 0xff00) << 8)
548 | ((r
& 0xff0000) >> 8) | ((r
& 0xff000000) >> 24));
555 /* Throw away one byte of a (possibly) multi-byte char C, updating
556 address and buffer to suit. */
559 unget_part_char (long c
, file_ptr
*address
, int *magiccount
, char **magic
)
563 if (encoding_bytes
> 1)
565 *address
-= encoding_bytes
- 1;
567 if (*magiccount
== 0)
569 /* If no magic buffer exists, use temp buffer. */
579 tmp
[0] = (c
>> 8) & 0xff;
583 tmp
[0] = (c
>> 16) & 0xff;
584 tmp
[1] = (c
>> 8) & 0xff;
589 tmp
[0] = (c
>> 8) & 0xff;
590 tmp
[1] = (c
>> 16) & 0xff;
591 tmp
[2] = (c
>> 24) & 0xff;
599 /* If magic buffer exists, rewind. */
600 *magic
-= encoding_bytes
- 1;
601 *magiccount
+= encoding_bytes
- 1;
607 print_filename_and_address (const char * filename
, file_ptr address
)
610 printf ("%s: ", filename
);
612 if (! print_addresses
)
615 switch (address_radix
)
618 if (sizeof (address
) > sizeof (long))
621 printf ("%7llo ", (unsigned long long) address
);
623 printf ("%7I64o ", (unsigned long long) address
);
627 printf ("%7lo ", (unsigned long) address
);
631 if (sizeof (address
) > sizeof (long))
634 printf ("%7llu ", (unsigned long long) address
);
636 printf ("%7I64d ", (unsigned long long) address
);
640 printf ("%7ld ", (long) address
);
644 if (sizeof (address
) > sizeof (long))
647 printf ("%7llx ", (unsigned long long) address
);
649 printf ("%7I64x ", (unsigned long long) address
);
653 printf ("%7lx ", (unsigned long) address
);
658 /* Return non-zero if the bytes starting at BUFFER form a valid UTF-8 encoding.
659 If the encoding is valid then returns the number of bytes it uses. */
662 is_valid_utf8 (const unsigned char * buffer
, unsigned long buflen
)
664 if (buffer
[0] < 0xc0)
670 if ((buffer
[1] & 0xc0) != 0x80)
673 if ((buffer
[0] & 0x20) == 0)
679 if ((buffer
[2] & 0xc0) != 0x80)
682 if ((buffer
[0] & 0x10) == 0)
688 if ((buffer
[3] & 0xc0) != 0x80)
694 /* Display a UTF-8 encoded character in BUFFER according to the setting
695 of unicode_display. The character is known to be valid.
696 Returns the number of bytes consumed. */
699 display_utf8_char (const unsigned char * buffer
)
702 unsigned int utf8_len
;
704 switch (buffer
[0] & 0x30)
717 switch (unicode_display
)
720 fprintf (stderr
, "ICE: unexpected unicode display type\n");
724 case unicode_highlight
:
725 if (unicode_display
== unicode_highlight
&& isatty (1))
726 printf ("\x1B[31;47m"); /* Red. */
731 printf ("\\u%02x%02x",
732 ((buffer
[0] & 0x1c) >> 2),
733 ((buffer
[0] & 0x03) << 6) | (buffer
[1] & 0x3f));
737 printf ("\\u%02x%02x",
738 ((buffer
[0] & 0x0f) << 4) | ((buffer
[1] & 0x3c) >> 2),
739 ((buffer
[1] & 0x03) << 6) | ((buffer
[2] & 0x3f)));
743 printf ("\\u%02x%02x%02x",
744 ((buffer
[0] & 0x07) << 6) | ((buffer
[1] & 0x3c) >> 2),
745 ((buffer
[1] & 0x03) << 6) | ((buffer
[2] & 0x3c) >> 2),
746 ((buffer
[2] & 0x03) << 6) | ((buffer
[3] & 0x3f)));
753 if (unicode_display
== unicode_highlight
&& isatty (1))
754 printf ("\033[0m"); /* Default colour. */
760 for (j
= 0; j
< utf8_len
; j
++)
761 printf ("%02x", buffer
[j
]);
766 printf ("%.1s", buffer
);
773 /* Display strings in BUFFER. Treat any UTF-8 encoded characters encountered
774 according to the setting of the unicode_display variable. The buffer
775 contains BUFLEN bytes.
777 Display the characters as if they started at ADDRESS and are contained in
781 print_unicode_buffer (const char * filename
,
783 const unsigned char * buffer
,
784 unsigned long buflen
)
786 /* Paranoia checks... */
789 || unicode_display
== unicode_default
791 || encoding_bytes
!= 1)
793 fprintf (stderr
, "ICE: bad arguments to print_unicode_buffer\n");
800 /* We must only display strings that are at least string_min *characters*
801 long. So we scan the buffer in two stages. First we locate the start
802 of a potential string. Then we walk along it until we have found
803 string_min characters. Then we go back to the start point and start
804 displaying characters according to the unicode_display setting. */
806 unsigned long start_point
= 0;
808 unsigned int char_len
= 1;
809 unsigned int num_found
= 0;
811 for (i
= 0; i
< buflen
; i
+= char_len
)
817 /* Find the first potential character of a string. */
818 if (! STRING_ISGRAPHIC (c
))
832 if ((char_len
= is_valid_utf8 (buffer
+ i
, buflen
- i
)) == 0)
839 if (unicode_display
== unicode_invalid
)
841 /* We have found a valid UTF-8 character, but we treat it as non-graphic. */
848 /* We have found a potential starting point for a string. */
853 if (num_found
>= string_min
)
857 if (num_found
< string_min
)
860 print_filename_and_address (filename
, address
+ start_point
);
862 /* We have found string_min characters. Display them and any
864 for (i
= start_point
; i
< buflen
; i
+= char_len
)
870 if (! STRING_ISGRAPHIC (c
))
874 else if (! is_valid_utf8 (buffer
+ i
, buflen
- i
))
876 else if (unicode_display
== unicode_invalid
)
879 char_len
= display_utf8_char (buffer
+ i
);
882 if (output_separator
)
883 fputs (output_separator
, stdout
);
887 /* FIXME: Using tail recursion here is lazy programming... */
888 print_unicode_buffer (filename
, address
+ i
, buffer
+ i
, buflen
- i
);
892 get_unicode_byte (FILE * stream
,
893 unsigned char * putback
,
894 unsigned int * num_putback
,
895 unsigned int * num_read
)
897 if (* num_putback
> 0)
899 * num_putback
= * num_putback
- 1;
900 return putback
[* num_putback
];
903 * num_read
= * num_read
+ 1;
905 #if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
906 return getc_unlocked (stream
);
908 return getc (stream
);
912 /* Helper function for print_unicode_stream. */
915 print_unicode_stream_body (const char * filename
,
918 unsigned char * putback_buf
,
919 unsigned int num_putback
,
920 unsigned char * print_buf
)
922 /* It would be nice if we could just read the stream into a buffer
923 and then process if with print_unicode_buffer. But the input
924 might be huge or it might time-locked (eg stdin). So instead
925 we go one byte at a time... */
927 file_ptr start_point
= 0;
928 unsigned int num_read
= 0;
929 unsigned int num_chars
= 0;
930 unsigned int num_print
= 0;
933 /* Find a series of string_min characters. Put them into print_buf. */
936 if (num_chars
>= string_min
)
939 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
943 if (! STRING_ISGRAPHIC (c
))
945 num_chars
= num_print
= 0;
950 start_point
= num_read
- 1;
954 print_buf
[num_print
] = c
;
962 num_chars
= num_print
= 0;
966 /* We *might* have a UTF-8 sequence. Time to start peeking. */
970 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
975 if ((utf8
[1] & 0xc0) != 0x80)
978 putback_buf
[num_putback
++] = utf8
[1];
979 num_chars
= num_print
= 0;
982 else if ((utf8
[0] & 0x20) == 0)
984 /* A valid 2-byte UTF-8 encoding. */
985 if (unicode_display
== unicode_invalid
)
987 putback_buf
[num_putback
++] = utf8
[1];
988 num_chars
= num_print
= 0;
992 print_buf
[num_print
++] = utf8
[0];
993 print_buf
[num_print
++] = utf8
[1];
999 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1004 if ((utf8
[2] & 0xc0) != 0x80)
1006 /* Invalid UTF-8. */
1007 putback_buf
[num_putback
++] = utf8
[2];
1008 putback_buf
[num_putback
++] = utf8
[1];
1009 num_chars
= num_print
= 0;
1012 else if ((utf8
[0] & 0x10) == 0)
1014 /* A valid 3-byte UTF-8 encoding. */
1015 if (unicode_display
== unicode_invalid
)
1017 putback_buf
[num_putback
++] = utf8
[2];
1018 putback_buf
[num_putback
++] = utf8
[1];
1019 num_chars
= num_print
= 0;
1023 print_buf
[num_print
++] = utf8
[0];
1024 print_buf
[num_print
++] = utf8
[1];
1025 print_buf
[num_print
++] = utf8
[2];
1031 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1036 if ((utf8
[3] & 0xc0) != 0x80)
1038 /* Invalid UTF-8. */
1039 putback_buf
[num_putback
++] = utf8
[3];
1040 putback_buf
[num_putback
++] = utf8
[2];
1041 putback_buf
[num_putback
++] = utf8
[1];
1042 num_chars
= num_print
= 0;
1044 /* We have a valid 4-byte UTF-8 encoding. */
1045 else if (unicode_display
== unicode_invalid
)
1047 putback_buf
[num_putback
++] = utf8
[3];
1048 putback_buf
[num_putback
++] = utf8
[1];
1049 putback_buf
[num_putback
++] = utf8
[2];
1050 num_chars
= num_print
= 0;
1054 print_buf
[num_print
++] = utf8
[0];
1055 print_buf
[num_print
++] = utf8
[1];
1056 print_buf
[num_print
++] = utf8
[2];
1057 print_buf
[num_print
++] = utf8
[3];
1063 if (num_chars
>= string_min
)
1065 /* We know that we have string_min valid characters in print_buf,
1066 and there may be more to come in the stream. Start displaying
1069 print_filename_and_address (filename
, address
+ start_point
);
1072 for (i
= 0; i
< num_print
;)
1074 if (print_buf
[i
] < 127)
1075 putchar (print_buf
[i
++]);
1077 i
+= display_utf8_char (print_buf
+ i
);
1080 /* OK so now we have to start read unchecked bytes. */
1082 /* Find a series of string_min characters. Put them into print_buf. */
1085 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1089 if (! STRING_ISGRAPHIC (c
))
1101 /* We *might* have a UTF-8 sequence. Time to start peeking. */
1102 unsigned char utf8
[4];
1105 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1110 if ((utf8
[1] & 0xc0) != 0x80)
1112 /* Invalid UTF-8. */
1113 putback_buf
[num_putback
++] = utf8
[1];
1116 else if ((utf8
[0] & 0x20) == 0)
1118 /* Valid 2-byte UTF-8. */
1119 if (unicode_display
== unicode_invalid
)
1121 putback_buf
[num_putback
++] = utf8
[1];
1126 (void) display_utf8_char (utf8
);
1131 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1136 if ((utf8
[2] & 0xc0) != 0x80)
1138 /* Invalid UTF-8. */
1139 putback_buf
[num_putback
++] = utf8
[2];
1140 putback_buf
[num_putback
++] = utf8
[1];
1143 else if ((utf8
[0] & 0x10) == 0)
1145 /* Valid 3-byte UTF-8. */
1146 if (unicode_display
== unicode_invalid
)
1148 putback_buf
[num_putback
++] = utf8
[2];
1149 putback_buf
[num_putback
++] = utf8
[1];
1154 (void) display_utf8_char (utf8
);
1159 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1164 if ((utf8
[3] & 0xc0) != 0x80)
1166 /* Invalid UTF-8. */
1167 putback_buf
[num_putback
++] = utf8
[3];
1168 putback_buf
[num_putback
++] = utf8
[2];
1169 putback_buf
[num_putback
++] = utf8
[1];
1172 else if (unicode_display
== unicode_invalid
)
1174 putback_buf
[num_putback
++] = utf8
[3];
1175 putback_buf
[num_putback
++] = utf8
[2];
1176 putback_buf
[num_putback
++] = utf8
[1];
1180 /* A valid 4-byte UTF-8 encoding. */
1181 (void) display_utf8_char (utf8
);
1185 if (output_separator
)
1186 fputs (output_separator
, stdout
);
1192 /* FIXME: Using tail recursion here is lazy, but it works. */
1193 print_unicode_stream_body (filename
, address
+ num_read
, stream
, putback_buf
, num_putback
, print_buf
);
1196 /* Display strings read in from STREAM. Treat any UTF-8 encoded characters
1197 encountered according to the setting of the unicode_display variable.
1198 The stream is positioned at ADDRESS and is attached to FILENAME. */
1201 print_unicode_stream (const char * filename
,
1205 /* Paranoia checks... */
1206 if (filename
== NULL
1208 || unicode_display
== unicode_default
1210 || encoding_bytes
!= 1)
1212 fprintf (stderr
, "ICE: bad arguments to print_unicode_stream\n");
1216 /* Allocate space for string_min 4-byte utf-8 characters. */
1217 unsigned char * print_buf
= xmalloc ((4 * string_min
) + 1);
1218 /* We should never have to put back more than 4 bytes. */
1219 unsigned char putback_buf
[5];
1220 unsigned int num_putback
= 0;
1222 print_unicode_stream_body (filename
, address
, stream
, putback_buf
, num_putback
, print_buf
);
1226 /* Find the strings in file FILENAME, read from STREAM.
1227 Assume that STREAM is positioned so that the next byte read
1228 is at address ADDRESS in the file.
1230 If STREAM is NULL, do not read from it.
1231 The caller can supply a buffer of characters
1232 to be processed before the data in STREAM.
1233 MAGIC is the address of the buffer and
1234 MAGICCOUNT is how many characters are in it.
1235 Those characters come at address ADDRESS and the data in STREAM follow. */
1238 print_strings (const char *filename
, FILE *stream
, file_ptr address
,
1239 int magiccount
, char *magic
)
1241 if (unicode_display
!= unicode_default
)
1244 print_unicode_buffer (filename
, address
,
1245 (const unsigned char *) magic
, magiccount
);
1248 print_unicode_stream (filename
, address
, stream
);
1252 char *buf
= (char *) xmalloc (sizeof (char) * (string_min
+ 1));
1260 /* See if the next `string_min' chars are all graphic chars. */
1263 for (i
= 0; i
< string_min
; i
++)
1265 c
= get_char (stream
, &address
, &magiccount
, &magic
);
1272 if (! STRING_ISGRAPHIC (c
))
1274 /* Found a non-graphic. Try again starting with next byte. */
1275 unget_part_char (c
, &address
, &magiccount
, &magic
);
1281 /* We found a run of `string_min' graphic characters. Print up
1282 to the next non-graphic character. */
1283 print_filename_and_address (filename
, start
);
1286 fputs (buf
, stdout
);
1290 c
= get_char (stream
, &address
, &magiccount
, &magic
);
1293 if (! STRING_ISGRAPHIC (c
))
1295 unget_part_char (c
, &address
, &magiccount
, &magic
);
1301 if (output_separator
)
1302 fputs (output_separator
, stdout
);
1310 usage (FILE *stream
, int status
)
1312 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
1313 fprintf (stream
, _(" Display printable strings in [file(s)] (stdin by default)\n"));
1314 fprintf (stream
, _(" The options are:\n"));
1316 if (DEFAULT_STRINGS_ALL
)
1317 fprintf (stream
, _("\
1318 -a - --all Scan the entire file, not just the data section [default]\n\
1319 -d --data Only scan the data sections in the file\n"));
1321 fprintf (stream
, _("\
1322 -a - --all Scan the entire file, not just the data section\n\
1323 -d --data Only scan the data sections in the file [default]\n"));
1325 fprintf (stream
, _("\
1326 -f --print-file-name Print the name of the file before each string\n\
1327 -n <number> Locate & print any sequence of at least <number>\n\
1328 --bytes=<number> displayable characters. (The default is 4).\n\
1329 -t --radix={o,d,x} Print the location of the string in base 8, 10 or 16\n\
1330 -w --include-all-whitespace Include all whitespace as valid string characters\n\
1331 -o An alias for --radix=o\n\
1332 -T --target=<BFDNAME> Specify the binary file format\n\
1333 -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
1334 s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
1335 --unicode={default|show|invalid|hex|escape|highlight}\n\
1336 -U {d|s|i|x|e|h} Specify how to treat UTF-8 encoded unicode characters\n\
1337 -s --output-separator=<string> String used to separate strings in output.\n\
1338 @<file> Read options from <file>\n\
1339 -h --help Display this information\n\
1340 -v -V --version Print the program's version number\n"));
1341 list_supported_targets (program_name
, stream
);
1342 if (REPORT_BUGS_TO
[0] && status
== 0)
1343 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);