1 /* strings -- print the strings of printable characters in files
2 Copyright (C) 1993-2021 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 newline. Default is 4.
38 -t {o,x,d} Print the offset within the file before each string,
41 --include-all-whitespace
42 -w By default tab and space are the only whitepace included in graphic
43 char sequences. This option considers all of isspace() valid.
45 -o Like -to. (Some other implementations have -o like -to,
46 others like -td. We chose one arbitrarily.)
48 --encoding={s,S,b,l,B,L}
50 Select character encoding: 7-bit-character, 8-bit-character,
51 bigendian 16-bit, littleendian 16-bit, bigendian 32-bit,
56 Specify a non-default object file format.
58 --unicode={default|locale|invalid|hex|escape|highlight}
60 Determine how to handle UTF-8 unicode characters. The default
61 is no special treatment. All other versions of this option
62 only apply if the encoding is valid and enabling the option
64 The 'locale' option displays the characters according to the
65 current locale. The 'invalid' option treats them as
66 non-string characters. The 'hex' option displays them as hex
67 byte sequences. The 'escape' option displays them as escape
68 sequences and the 'highlight' option displays them as
69 coloured escape sequences.
71 --output-separator=sep_string
72 -s sep_string String used to separate parsed strings in output.
76 -h Print the usage message on the standard output.
80 -v Print the program version number.
82 Written by Richard Stallman <rms@gnu.ai.mit.edu>
83 and David MacKenzie <djm@gnu.ai.mit.edu>. */
88 #include "libiberty.h"
89 #include "safe-ctype.h"
93 #define streq(a,b) (strcmp ((a),(b)) == 0)
96 typedef enum unicode_display_type
104 } unicode_display_type
;
106 static unicode_display_type unicode_display
= unicode_default
;
108 #define STRING_ISGRAPHIC(c) \
111 && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127) \
112 || (include_all_whitespace && ISSPACE (c))) \
119 /* The BFD section flags that identify an initialized data section. */
120 #define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
122 /* Radix for printing addresses (must be 8, 10 or 16). */
123 static int address_radix
;
125 /* Minimum length of sequence of graphic chars to trigger output. */
126 static unsigned int string_min
;
128 /* Whether or not we include all whitespace as a graphic char. */
129 static bool include_all_whitespace
;
131 /* TRUE means print address within file for each string. */
132 static bool print_addresses
;
134 /* TRUE means print filename for each string. */
135 static bool print_filenames
;
137 /* TRUE means for object files scan only the data section. */
138 static bool datasection_only
;
140 /* The BFD object file format. */
143 /* The character encoding format. */
144 static char encoding
;
145 static int encoding_bytes
;
147 /* Output string used to separate parsed strings */
148 static char *output_separator
;
150 static struct option long_options
[] =
152 {"all", no_argument
, NULL
, 'a'},
153 {"bytes", required_argument
, NULL
, 'n'},
154 {"data", no_argument
, NULL
, 'd'},
155 {"encoding", required_argument
, NULL
, 'e'},
156 {"help", no_argument
, NULL
, 'h'},
157 {"include-all-whitespace", no_argument
, NULL
, 'w'},
158 {"output-separator", required_argument
, NULL
, 's'},
159 {"print-file-name", no_argument
, NULL
, 'f'},
160 {"radix", required_argument
, NULL
, 't'},
161 {"target", required_argument
, NULL
, 'T'},
162 {"unicode", required_argument
, NULL
, 'U'},
163 {"version", no_argument
, NULL
, 'v'},
167 static bool strings_file (char *);
168 static void print_strings (const char *, FILE *, file_ptr
, int, char *);
169 static void usage (FILE *, int) ATTRIBUTE_NORETURN
;
171 int main (int, char **);
174 main (int argc
, char **argv
)
178 bool files_given
= false;
182 setlocale (LC_ALL
, "");
183 bindtextdomain (PACKAGE
, LOCALEDIR
);
184 textdomain (PACKAGE
);
186 program_name
= argv
[0];
187 xmalloc_set_program_name (program_name
);
188 bfd_set_error_program_name (program_name
);
190 expandargv (&argc
, &argv
);
193 include_all_whitespace
= false;
194 print_addresses
= false;
195 print_filenames
= false;
196 if (DEFAULT_STRINGS_ALL
)
197 datasection_only
= false;
199 datasection_only
= true;
202 output_separator
= NULL
;
204 while ((optc
= getopt_long (argc
, argv
, "adfhHn:wot:e:T:s:U:Vv0123456789",
205 long_options
, (int *) 0)) != EOF
)
210 datasection_only
= false;
214 datasection_only
= true;
218 print_filenames
= true;
226 string_min
= (int) strtoul (optarg
, &s
, 0);
227 if (s
!= NULL
&& *s
!= 0)
228 fatal (_("invalid integer argument %s"), optarg
);
232 include_all_whitespace
= true;
236 print_addresses
= true;
241 print_addresses
= true;
242 if (optarg
[1] != '\0')
268 if (optarg
[1] != '\0')
270 encoding
= optarg
[0];
274 output_separator
= optarg
;
278 if (streq (optarg
, "default") || streq (optarg
, "d"))
279 unicode_display
= unicode_default
;
280 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
281 unicode_display
= unicode_locale
;
282 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
283 unicode_display
= unicode_escape
;
284 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
285 unicode_display
= unicode_invalid
;
286 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
287 unicode_display
= unicode_hex
;
288 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
289 unicode_display
= unicode_highlight
;
291 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
296 print_version ("strings");
303 numeric_opt
= optind
;
308 if (unicode_display
!= unicode_default
)
311 if (numeric_opt
!= 0)
313 string_min
= (int) strtoul (argv
[numeric_opt
- 1] + 1, &s
, 0);
314 if (s
!= NULL
&& *s
!= 0)
315 fatal (_("invalid integer argument %s"), argv
[numeric_opt
- 1] + 1);
318 fatal (_("invalid minimum string length %d"), string_min
);
338 if (bfd_init () != BFD_INIT_MAGIC
)
339 fatal (_("fatal error: libbfd ABI mismatch"));
340 set_default_bfd_target ();
344 datasection_only
= false;
345 SET_BINARY (fileno (stdin
));
346 print_strings ("{standard input}", stdin
, 0, 0, (char *) NULL
);
351 for (; optind
< argc
; ++optind
)
353 if (streq (argv
[optind
], "-"))
354 datasection_only
= false;
358 exit_status
|= !strings_file (argv
[optind
]);
366 return (exit_status
);
369 /* Scan section SECT of the file ABFD, whose printable name is
370 FILENAME. If it contains initialized data set GOT_A_SECTION and
371 print the strings in it. */
374 strings_a_section (bfd
*abfd
, asection
*sect
, const char *filename
,
377 bfd_size_type sectsize
;
380 if ((sect
->flags
& DATA_FLAGS
) != DATA_FLAGS
)
383 sectsize
= bfd_section_size (sect
);
387 if (!bfd_malloc_and_get_section (abfd
, sect
, &mem
))
389 non_fatal (_("%s: Reading section %s failed: %s"),
390 filename
, sect
->name
, bfd_errmsg (bfd_get_error ()));
394 *got_a_section
= true;
395 print_strings (filename
, NULL
, sect
->filepos
, sectsize
, (char *) mem
);
399 /* Scan all of the sections in FILE, and print the strings
400 in the initialized data section(s).
402 Return TRUE if successful,
403 FALSE if not (such as if FILE is not an object file). */
406 strings_object_file (const char *file
)
412 abfd
= bfd_openr (file
, target
);
415 /* Treat the file as a non-object file. */
418 /* This call is mainly for its side effect of reading in the sections.
419 We follow the traditional behavior of `strings' in that we don't
420 complain if we don't recognize a file to be an object file. */
421 if (!bfd_check_format (abfd
, bfd_object
))
427 got_a_section
= false;
428 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
429 strings_a_section (abfd
, s
, file
, &got_a_section
);
431 if (!bfd_close (abfd
))
437 return got_a_section
;
440 /* Print the strings in FILE. Return TRUE if ok, FALSE if an error occurs. */
443 strings_file (char *file
)
447 /* get_file_size does not support non-S_ISREG files. */
449 if (stat (file
, &st
) < 0)
452 non_fatal (_("'%s': No such file"), file
);
454 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
455 file
, strerror (errno
));
458 else if (S_ISDIR (st
.st_mode
))
460 non_fatal (_("Warning: '%s' is a directory"), file
);
464 /* If we weren't told to scan the whole file,
465 try to open it as an object file and only look at
466 initialized data sections. If that fails, fall back to the
468 if (!datasection_only
|| !strings_object_file (file
))
472 stream
= fopen (file
, FOPEN_RB
);
475 fprintf (stderr
, "%s: ", program_name
);
480 print_strings (file
, stream
, (file_ptr
) 0, 0, (char *) NULL
);
482 if (fclose (stream
) == EOF
)
484 fprintf (stderr
, "%s: ", program_name
);
493 /* Read the next character, return EOF if none available.
494 Assume that STREAM is positioned so that the next byte read
495 is at address ADDRESS in the file.
497 If STREAM is NULL, do not read from it.
498 The caller can supply a buffer of characters
499 to be processed before the data in STREAM.
500 MAGIC is the address of the buffer and
501 MAGICCOUNT is how many characters are in it. */
504 get_char (FILE *stream
, file_ptr
*address
, int *magiccount
, char **magic
)
509 for (i
= 0; i
< encoding_bytes
; i
++)
521 /* Only use getc_unlocked if we found a declaration for it.
522 Otherwise, libc is not thread safe by default, and we
523 should not use it. */
525 #if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
526 c
= getc_unlocked (stream
);
535 r
= (r
<< 8) | (c
& 0xff);
543 r
= ((r
& 0xff) << 8) | ((r
& 0xff00) >> 8);
546 r
= (((r
& 0xff) << 24) | ((r
& 0xff00) << 8)
547 | ((r
& 0xff0000) >> 8) | ((r
& 0xff000000) >> 24));
554 /* Throw away one byte of a (possibly) multi-byte char C, updating
555 address and buffer to suit. */
558 unget_part_char (long c
, file_ptr
*address
, int *magiccount
, char **magic
)
562 if (encoding_bytes
> 1)
564 *address
-= encoding_bytes
- 1;
566 if (*magiccount
== 0)
568 /* If no magic buffer exists, use temp buffer. */
578 tmp
[0] = (c
>> 8) & 0xff;
582 tmp
[0] = (c
>> 16) & 0xff;
583 tmp
[1] = (c
>> 8) & 0xff;
588 tmp
[0] = (c
>> 8) & 0xff;
589 tmp
[1] = (c
>> 16) & 0xff;
590 tmp
[2] = (c
>> 24) & 0xff;
598 /* If magic buffer exists, rewind. */
599 *magic
-= encoding_bytes
- 1;
600 *magiccount
+= encoding_bytes
- 1;
606 print_filename_and_address (const char * filename
, file_ptr address
)
609 printf ("%s: ", filename
);
611 if (! print_addresses
)
614 switch (address_radix
)
617 if (sizeof (address
) > sizeof (long))
620 printf ("%7llo ", (unsigned long long) address
);
622 printf ("%7I64o ", (unsigned long long) address
);
626 printf ("%7lo ", (unsigned long) address
);
630 if (sizeof (address
) > sizeof (long))
633 printf ("%7llu ", (unsigned long long) address
);
635 printf ("%7I64d ", (unsigned long long) address
);
639 printf ("%7ld ", (long) address
);
643 if (sizeof (address
) > sizeof (long))
646 printf ("%7llx ", (unsigned long long) address
);
648 printf ("%7I64x ", (unsigned long long) address
);
652 printf ("%7lx ", (unsigned long) address
);
657 /* Return non-zero if the bytes starting at BUFFER form a valid UTF-8 encoding.
658 If the encoding is valid then returns the number of bytes it uses. */
661 is_valid_utf8 (const unsigned char * buffer
, unsigned long buflen
)
663 if (buffer
[0] < 0xc0)
669 if ((buffer
[1] & 0xc0) != 0x80)
672 if ((buffer
[0] & 0x20) == 0)
678 if ((buffer
[2] & 0xc0) != 0x80)
681 if ((buffer
[0] & 0x10) == 0)
687 if ((buffer
[3] & 0xc0) != 0x80)
693 /* Display a UTF-8 encoded character in BUFFER according to the setting
694 of unicode_display. The character is known to be valid.
695 Returns the number of bytes consumed. */
698 display_utf8_char (const unsigned char * buffer
)
701 unsigned int utf8_len
;
703 switch (buffer
[0] & 0x30)
716 switch (unicode_display
)
719 fprintf (stderr
, "ICE: unexpected unicode display type\n");
723 case unicode_highlight
:
724 if (unicode_display
== unicode_highlight
&& isatty (1))
725 printf ("\x1B[31;47m"); /* Red. */
730 printf ("\\u%02x%02x",
731 ((buffer
[0] & 0x1c) >> 2),
732 ((buffer
[0] & 0x03) << 6) | (buffer
[1] & 0x3f));
736 printf ("\\u%02x%02x",
737 ((buffer
[0] & 0x0f) << 4) | ((buffer
[1] & 0x3c) >> 2),
738 ((buffer
[1] & 0x03) << 6) | ((buffer
[2] & 0x3f)));
742 printf ("\\u%02x%02x%02x",
743 ((buffer
[0] & 0x07) << 6) | ((buffer
[1] & 0x3c) >> 2),
744 ((buffer
[1] & 0x03) << 6) | ((buffer
[2] & 0x3c) >> 2),
745 ((buffer
[2] & 0x03) << 6) | ((buffer
[3] & 0x3f)));
752 if (unicode_display
== unicode_highlight
&& isatty (1))
753 printf ("\033[0m"); /* Default colour. */
759 for (j
= 0; j
< utf8_len
; j
++)
760 printf ("%02x", buffer
[j
]);
765 printf ("%.1s", buffer
);
772 /* Display strings in BUFFER. Treat any UTF-8 encoded characters encountered
773 according to the setting of the unicode_display variable. The buffer
774 contains BUFLEN bytes.
776 Display the characters as if they started at ADDRESS and are contained in
780 print_unicode_buffer (const char * filename
,
782 const unsigned char * buffer
,
783 unsigned long buflen
)
785 /* Paranoia checks... */
788 || unicode_display
== unicode_default
790 || encoding_bytes
!= 1)
792 fprintf (stderr
, "ICE: bad arguments to print_unicode_buffer\n");
799 /* We must only display strings that are at least string_min *characters*
800 long. So we scan the buffer in two stages. First we locate the start
801 of a potential string. Then we walk along it until we have found
802 string_min characters. Then we go back to the start point and start
803 displaying characters according to the unicode_display setting. */
805 unsigned long start_point
= 0;
807 unsigned int char_len
= 1;
808 unsigned int num_found
= 0;
810 for (i
= 0; i
< buflen
; i
+= char_len
)
816 /* Find the first potential character of a string. */
817 if (! STRING_ISGRAPHIC (c
))
831 if ((char_len
= is_valid_utf8 (buffer
+ i
, buflen
- i
)) == 0)
838 if (unicode_display
== unicode_invalid
)
840 /* We have found a valid UTF-8 character, but we treat it as non-graphic. */
847 /* We have found a potential starting point for a string. */
852 if (num_found
>= string_min
)
856 if (num_found
< string_min
)
859 print_filename_and_address (filename
, address
+ start_point
);
861 /* We have found string_min characters. Display them and any
863 for (i
= start_point
; i
< buflen
; i
+= char_len
)
869 if (! STRING_ISGRAPHIC (c
))
873 else if (! is_valid_utf8 (buffer
+ i
, buflen
- i
))
875 else if (unicode_display
== unicode_invalid
)
878 char_len
= display_utf8_char (buffer
+ i
);
881 if (output_separator
)
882 fputs (output_separator
, stdout
);
886 /* FIXME: Using tail recursion here is lazy programming... */
887 print_unicode_buffer (filename
, address
+ i
, buffer
+ i
, buflen
- i
);
891 get_unicode_byte (FILE * stream
,
892 unsigned char * putback
,
893 unsigned int * num_putback
,
894 unsigned int * num_read
)
896 if (* num_putback
> 0)
898 * num_putback
= * num_putback
- 1;
899 return putback
[* num_putback
];
902 * num_read
= * num_read
+ 1;
904 #if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
905 return getc_unlocked (stream
);
907 return getc (stream
);
911 /* Helper function for print_unicode_stream. */
914 print_unicode_stream_body (const char * filename
,
917 unsigned char * putback_buf
,
918 unsigned int num_putback
,
919 unsigned char * print_buf
)
921 /* It would be nice if we could just read the stream into a buffer
922 and then process if with print_unicode_buffer. But the input
923 might be huge or it might time-locked (eg stdin). So instead
924 we go one byte at a time... */
926 file_ptr start_point
= 0;
927 unsigned int num_read
= 0;
928 unsigned int num_chars
= 0;
929 unsigned int num_print
= 0;
932 /* Find a series of string_min characters. Put them into print_buf. */
935 if (num_chars
>= string_min
)
938 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
942 if (! STRING_ISGRAPHIC (c
))
944 num_chars
= num_print
= 0;
949 start_point
= num_read
- 1;
953 print_buf
[num_print
] = c
;
961 num_chars
= num_print
= 0;
965 /* We *might* have a UTF-8 sequence. Time to start peeking. */
969 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
974 if ((utf8
[1] & 0xc0) != 0x80)
977 putback_buf
[num_putback
++] = utf8
[1];
978 num_chars
= num_print
= 0;
981 else if ((utf8
[0] & 0x20) == 0)
983 /* A valid 2-byte UTF-8 encoding. */
984 if (unicode_display
== unicode_invalid
)
986 putback_buf
[num_putback
++] = utf8
[1];
987 num_chars
= num_print
= 0;
991 print_buf
[num_print
++] = utf8
[0];
992 print_buf
[num_print
++] = utf8
[1];
998 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1003 if ((utf8
[2] & 0xc0) != 0x80)
1005 /* Invalid UTF-8. */
1006 putback_buf
[num_putback
++] = utf8
[2];
1007 putback_buf
[num_putback
++] = utf8
[1];
1008 num_chars
= num_print
= 0;
1011 else if ((utf8
[0] & 0x10) == 0)
1013 /* A valid 3-byte UTF-8 encoding. */
1014 if (unicode_display
== unicode_invalid
)
1016 putback_buf
[num_putback
++] = utf8
[2];
1017 putback_buf
[num_putback
++] = utf8
[1];
1018 num_chars
= num_print
= 0;
1022 print_buf
[num_print
++] = utf8
[0];
1023 print_buf
[num_print
++] = utf8
[1];
1024 print_buf
[num_print
++] = utf8
[2];
1030 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1035 if ((utf8
[3] & 0xc0) != 0x80)
1037 /* Invalid UTF-8. */
1038 putback_buf
[num_putback
++] = utf8
[3];
1039 putback_buf
[num_putback
++] = utf8
[2];
1040 putback_buf
[num_putback
++] = utf8
[1];
1041 num_chars
= num_print
= 0;
1043 /* We have a valid 4-byte UTF-8 encoding. */
1044 else if (unicode_display
== unicode_invalid
)
1046 putback_buf
[num_putback
++] = utf8
[3];
1047 putback_buf
[num_putback
++] = utf8
[1];
1048 putback_buf
[num_putback
++] = utf8
[2];
1049 num_chars
= num_print
= 0;
1053 print_buf
[num_print
++] = utf8
[0];
1054 print_buf
[num_print
++] = utf8
[1];
1055 print_buf
[num_print
++] = utf8
[2];
1056 print_buf
[num_print
++] = utf8
[3];
1062 if (num_chars
>= string_min
)
1064 /* We know that we have string_min valid characters in print_buf,
1065 and there may be more to come in the stream. Start displaying
1068 print_filename_and_address (filename
, address
+ start_point
);
1071 for (i
= 0; i
< num_print
;)
1073 if (print_buf
[i
] < 127)
1074 putchar (print_buf
[i
++]);
1076 i
+= display_utf8_char (print_buf
+ i
);
1079 /* OK so now we have to start read unchecked bytes. */
1081 /* Find a series of string_min characters. Put them into print_buf. */
1084 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1088 if (! STRING_ISGRAPHIC (c
))
1100 /* We *might* have a UTF-8 sequence. Time to start peeking. */
1101 unsigned char utf8
[4];
1104 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1109 if ((utf8
[1] & 0xc0) != 0x80)
1111 /* Invalid UTF-8. */
1112 putback_buf
[num_putback
++] = utf8
[1];
1115 else if ((utf8
[0] & 0x20) == 0)
1117 /* Valid 2-byte UTF-8. */
1118 if (unicode_display
== unicode_invalid
)
1120 putback_buf
[num_putback
++] = utf8
[1];
1125 (void) display_utf8_char (utf8
);
1130 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1135 if ((utf8
[2] & 0xc0) != 0x80)
1137 /* Invalid UTF-8. */
1138 putback_buf
[num_putback
++] = utf8
[2];
1139 putback_buf
[num_putback
++] = utf8
[1];
1142 else if ((utf8
[0] & 0x10) == 0)
1144 /* Valid 3-byte UTF-8. */
1145 if (unicode_display
== unicode_invalid
)
1147 putback_buf
[num_putback
++] = utf8
[2];
1148 putback_buf
[num_putback
++] = utf8
[1];
1153 (void) display_utf8_char (utf8
);
1158 c
= get_unicode_byte (stream
, putback_buf
, & num_putback
, & num_read
);
1163 if ((utf8
[3] & 0xc0) != 0x80)
1165 /* Invalid UTF-8. */
1166 putback_buf
[num_putback
++] = utf8
[3];
1167 putback_buf
[num_putback
++] = utf8
[2];
1168 putback_buf
[num_putback
++] = utf8
[1];
1171 else if (unicode_display
== unicode_invalid
)
1173 putback_buf
[num_putback
++] = utf8
[3];
1174 putback_buf
[num_putback
++] = utf8
[2];
1175 putback_buf
[num_putback
++] = utf8
[1];
1179 /* A valid 4-byte UTF-8 encoding. */
1180 (void) display_utf8_char (utf8
);
1184 if (output_separator
)
1185 fputs (output_separator
, stdout
);
1191 /* FIXME: Using tail recursion here is lazy, but it works. */
1192 print_unicode_stream_body (filename
, address
+ num_read
, stream
, putback_buf
, num_putback
, print_buf
);
1195 /* Display strings read in from STREAM. Treat any UTF-8 encoded characters
1196 encountered according to the setting of the unicode_display variable.
1197 The stream is positioned at ADDRESS and is attached to FILENAME. */
1200 print_unicode_stream (const char * filename
,
1204 /* Paranoia checks... */
1205 if (filename
== NULL
1207 || unicode_display
== unicode_default
1209 || encoding_bytes
!= 1)
1211 fprintf (stderr
, "ICE: bad arguments to print_unicode_stream\n");
1215 /* Allocate space for string_min 4-byte utf-8 characters. */
1216 unsigned char * print_buf
= xmalloc ((4 * string_min
) + 1);
1217 /* We should never have to put back more than 4 bytes. */
1218 unsigned char putback_buf
[5];
1219 unsigned int num_putback
= 0;
1221 print_unicode_stream_body (filename
, address
, stream
, putback_buf
, num_putback
, print_buf
);
1225 /* Find the strings in file FILENAME, read from STREAM.
1226 Assume that STREAM is positioned so that the next byte read
1227 is at address ADDRESS in the file.
1229 If STREAM is NULL, do not read from it.
1230 The caller can supply a buffer of characters
1231 to be processed before the data in STREAM.
1232 MAGIC is the address of the buffer and
1233 MAGICCOUNT is how many characters are in it.
1234 Those characters come at address ADDRESS and the data in STREAM follow. */
1237 print_strings (const char *filename
, FILE *stream
, file_ptr address
,
1238 int magiccount
, char *magic
)
1240 if (unicode_display
!= unicode_default
)
1243 print_unicode_buffer (filename
, address
,
1244 (const unsigned char *) magic
, magiccount
);
1247 print_unicode_stream (filename
, address
, stream
);
1251 char *buf
= (char *) xmalloc (sizeof (char) * (string_min
+ 1));
1259 /* See if the next `string_min' chars are all graphic chars. */
1262 for (i
= 0; i
< string_min
; i
++)
1264 c
= get_char (stream
, &address
, &magiccount
, &magic
);
1271 if (! STRING_ISGRAPHIC (c
))
1273 /* Found a non-graphic. Try again starting with next byte. */
1274 unget_part_char (c
, &address
, &magiccount
, &magic
);
1280 /* We found a run of `string_min' graphic characters. Print up
1281 to the next non-graphic character. */
1282 print_filename_and_address (filename
, start
);
1285 fputs (buf
, stdout
);
1289 c
= get_char (stream
, &address
, &magiccount
, &magic
);
1292 if (! STRING_ISGRAPHIC (c
))
1294 unget_part_char (c
, &address
, &magiccount
, &magic
);
1300 if (output_separator
)
1301 fputs (output_separator
, stdout
);
1309 usage (FILE *stream
, int status
)
1311 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
1312 fprintf (stream
, _(" Display printable strings in [file(s)] (stdin by default)\n"));
1313 fprintf (stream
, _(" The options are:\n"));
1315 if (DEFAULT_STRINGS_ALL
)
1316 fprintf (stream
, _("\
1317 -a - --all Scan the entire file, not just the data section [default]\n\
1318 -d --data Only scan the data sections in the file\n"));
1320 fprintf (stream
, _("\
1321 -a - --all Scan the entire file, not just the data section\n\
1322 -d --data Only scan the data sections in the file [default]\n"));
1324 fprintf (stream
, _("\
1325 -f --print-file-name Print the name of the file before each string\n\
1326 -n --bytes=[number] Locate & print any NUL-terminated sequence of at\n\
1327 -<number> least [number] characters (default 4).\n\
1328 -t --radix={o,d,x} Print the location of the string in base 8, 10 or 16\n\
1329 -w --include-all-whitespace Include all whitespace as valid string characters\n\
1330 -o An alias for --radix=o\n\
1331 -T --target=<BFDNAME> Specify the binary file format\n\
1332 -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
1333 s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
1334 --unicode={default|show|invalid|hex|escape|highlight}\n\
1335 -u {d|s|i|x|e|h} Specify how to treat UTF-8 encoded unicode characters\n\
1336 -s --output-separator=<string> String used to separate strings in output.\n\
1337 @<file> Read options from <file>\n\
1338 -h --help Display this information\n\
1339 -v -V --version Print the program's version number\n"));
1340 list_supported_targets (program_name
, stream
);
1341 if (REPORT_BUGS_TO
[0] && status
== 0)
1342 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);