strings.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /* strings -- print the strings of printable characters in files
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  14. 02110-1301, USA. */
  15. /* Usage: strings [options] file...
  16. Options:
  17. --all
  18. -a
  19. - Scan each file in its entirety.
  20. --data
  21. -d Scan only the initialized data section(s) of object files.
  22. --print-file-name
  23. -f Print the name of the file before each string.
  24. --bytes=min-len
  25. -n min-len
  26. -min-len Print graphic char sequences, MIN-LEN or more bytes long,
  27. that are followed by a NUL or a non-displayable character.
  28. Default is 4.
  29. --radix={o,x,d}
  30. -t {o,x,d} Print the offset within the file before each string,
  31. in octal/hex/decimal.
  32. --include-all-whitespace
  33. -w By default tab and space are the only whitepace included in graphic
  34. char sequences. This option considers all of isspace() valid.
  35. -o Like -to. (Some other implementations have -o like -to,
  36. others like -td. We chose one arbitrarily.)
  37. --encoding={s,S,b,l,B,L}
  38. -e {s,S,b,l,B,L}
  39. Select character encoding: 7-bit-character, 8-bit-character,
  40. bigendian 16-bit, littleendian 16-bit, bigendian 32-bit,
  41. littleendian 32-bit.
  42. --target=BFDNAME
  43. -T {bfdname}
  44. Specify a non-default object file format.
  45. --unicode={default|locale|invalid|hex|escape|highlight}
  46. -U {d|l|i|x|e|h}
  47. Determine how to handle UTF-8 unicode characters. The default
  48. is no special treatment. All other versions of this option
  49. only apply if the encoding is valid and enabling the option
  50. implies --encoding=S.
  51. The 'locale' option displays the characters according to the
  52. current locale. The 'invalid' option treats them as
  53. non-string characters. The 'hex' option displays them as hex
  54. byte sequences. The 'escape' option displays them as escape
  55. sequences and the 'highlight' option displays them as
  56. coloured escape sequences.
  57. --output-separator=sep_string
  58. -s sep_string String used to separate parsed strings in output.
  59. Default is newline.
  60. --help
  61. -h Print the usage message on the standard output.
  62. --version
  63. -V
  64. -v Print the program version number.
  65. Written by Richard Stallman <rms@gnu.ai.mit.edu>
  66. and David MacKenzie <djm@gnu.ai.mit.edu>. */
  67. #include "sysdep.h"
  68. #include "bfd.h"
  69. #include "getopt.h"
  70. #include "libiberty.h"
  71. #include "safe-ctype.h"
  72. #include "bucomm.h"
  73. #ifndef streq
  74. #define streq(a,b) (strcmp ((a),(b)) == 0)
  75. #endif
  76. typedef enum unicode_display_type
  77. {
  78. unicode_default = 0,
  79. unicode_locale,
  80. unicode_escape,
  81. unicode_hex,
  82. unicode_highlight,
  83. unicode_invalid
  84. } unicode_display_type;
  85. static unicode_display_type unicode_display = unicode_default;
  86. #define STRING_ISGRAPHIC(c) \
  87. ( (c) >= 0 \
  88. && (c) <= 255 \
  89. && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127) \
  90. || (include_all_whitespace && ISSPACE (c))) \
  91. )
  92. #ifndef errno
  93. extern int errno;
  94. #endif
  95. /* The BFD section flags that identify an initialized data section. */
  96. #define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
  97. /* Radix for printing addresses (must be 8, 10 or 16). */
  98. static int address_radix;
  99. /* Minimum length of sequence of graphic chars to trigger output. */
  100. static unsigned int string_min;
  101. /* Whether or not we include all whitespace as a graphic char. */
  102. static bool include_all_whitespace;
  103. /* TRUE means print address within file for each string. */
  104. static bool print_addresses;
  105. /* TRUE means print filename for each string. */
  106. static bool print_filenames;
  107. /* TRUE means for object files scan only the data section. */
  108. static bool datasection_only;
  109. /* The BFD object file format. */
  110. static char *target;
  111. /* The character encoding format. */
  112. static char encoding;
  113. static int encoding_bytes;
  114. /* Output string used to separate parsed strings */
  115. static char *output_separator;
  116. static struct option long_options[] =
  117. {
  118. {"all", no_argument, NULL, 'a'},
  119. {"bytes", required_argument, NULL, 'n'},
  120. {"data", no_argument, NULL, 'd'},
  121. {"encoding", required_argument, NULL, 'e'},
  122. {"help", no_argument, NULL, 'h'},
  123. {"include-all-whitespace", no_argument, NULL, 'w'},
  124. {"output-separator", required_argument, NULL, 's'},
  125. {"print-file-name", no_argument, NULL, 'f'},
  126. {"radix", required_argument, NULL, 't'},
  127. {"target", required_argument, NULL, 'T'},
  128. {"unicode", required_argument, NULL, 'U'},
  129. {"version", no_argument, NULL, 'v'},
  130. {NULL, 0, NULL, 0}
  131. };
  132. static bool strings_file (char *);
  133. static void print_strings (const char *, FILE *, file_ptr, int, char *);
  134. static void usage (FILE *, int) ATTRIBUTE_NORETURN;
  135. int main (int, char **);
  136. int
  137. main (int argc, char **argv)
  138. {
  139. int optc;
  140. int exit_status = 0;
  141. bool files_given = false;
  142. char *s;
  143. int numeric_opt = 0;
  144. setlocale (LC_ALL, "");
  145. bindtextdomain (PACKAGE, LOCALEDIR);
  146. textdomain (PACKAGE);
  147. program_name = argv[0];
  148. xmalloc_set_program_name (program_name);
  149. bfd_set_error_program_name (program_name);
  150. expandargv (&argc, &argv);
  151. string_min = 4;
  152. include_all_whitespace = false;
  153. print_addresses = false;
  154. print_filenames = false;
  155. if (DEFAULT_STRINGS_ALL)
  156. datasection_only = false;
  157. else
  158. datasection_only = true;
  159. target = NULL;
  160. encoding = 's';
  161. output_separator = NULL;
  162. while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:U:Vv0123456789",
  163. long_options, (int *) 0)) != EOF)
  164. {
  165. switch (optc)
  166. {
  167. case 'a':
  168. datasection_only = false;
  169. break;
  170. case 'd':
  171. datasection_only = true;
  172. break;
  173. case 'f':
  174. print_filenames = true;
  175. break;
  176. case 'H':
  177. case 'h':
  178. usage (stdout, 0);
  179. case 'n':
  180. string_min = (int) strtoul (optarg, &s, 0);
  181. if (s != NULL && *s != 0)
  182. fatal (_("invalid integer argument %s"), optarg);
  183. break;
  184. case 'w':
  185. include_all_whitespace = true;
  186. break;
  187. case 'o':
  188. print_addresses = true;
  189. address_radix = 8;
  190. break;
  191. case 't':
  192. print_addresses = true;
  193. if (optarg[1] != '\0')
  194. usage (stderr, 1);
  195. switch (optarg[0])
  196. {
  197. case 'o':
  198. address_radix = 8;
  199. break;
  200. case 'd':
  201. address_radix = 10;
  202. break;
  203. case 'x':
  204. address_radix = 16;
  205. break;
  206. default:
  207. usage (stderr, 1);
  208. }
  209. break;
  210. case 'T':
  211. target = optarg;
  212. break;
  213. case 'e':
  214. if (optarg[1] != '\0')
  215. usage (stderr, 1);
  216. encoding = optarg[0];
  217. break;
  218. case 's':
  219. output_separator = optarg;
  220. break;
  221. case 'U':
  222. if (streq (optarg, "default") || streq (optarg, "d"))
  223. unicode_display = unicode_default;
  224. else if (streq (optarg, "locale") || streq (optarg, "l"))
  225. unicode_display = unicode_locale;
  226. else if (streq (optarg, "escape") || streq (optarg, "e"))
  227. unicode_display = unicode_escape;
  228. else if (streq (optarg, "invalid") || streq (optarg, "i"))
  229. unicode_display = unicode_invalid;
  230. else if (streq (optarg, "hex") || streq (optarg, "x"))
  231. unicode_display = unicode_hex;
  232. else if (streq (optarg, "highlight") || streq (optarg, "h"))
  233. unicode_display = unicode_highlight;
  234. else
  235. fatal (_("invalid argument to -U/--unicode: %s"), optarg);
  236. break;
  237. case 'V':
  238. case 'v':
  239. print_version ("strings");
  240. break;
  241. case '?':
  242. usage (stderr, 1);
  243. default:
  244. numeric_opt = optind;
  245. break;
  246. }
  247. }
  248. if (unicode_display != unicode_default)
  249. encoding = 'S';
  250. if (numeric_opt != 0)
  251. {
  252. string_min = (int) strtoul (argv[numeric_opt - 1] + 1, &s, 0);
  253. if (s != NULL && *s != 0)
  254. fatal (_("invalid integer argument %s"), argv[numeric_opt - 1] + 1);
  255. }
  256. if (string_min < 1)
  257. fatal (_("invalid minimum string length %d"), string_min);
  258. switch (encoding)
  259. {
  260. case 'S':
  261. case 's':
  262. encoding_bytes = 1;
  263. break;
  264. case 'b':
  265. case 'l':
  266. encoding_bytes = 2;
  267. break;
  268. case 'B':
  269. case 'L':
  270. encoding_bytes = 4;
  271. break;
  272. default:
  273. usage (stderr, 1);
  274. }
  275. if (bfd_init () != BFD_INIT_MAGIC)
  276. fatal (_("fatal error: libbfd ABI mismatch"));
  277. set_default_bfd_target ();
  278. if (optind >= argc)
  279. {
  280. datasection_only = false;
  281. SET_BINARY (fileno (stdin));
  282. print_strings ("{standard input}", stdin, 0, 0, (char *) NULL);
  283. files_given = true;
  284. }
  285. else
  286. {
  287. for (; optind < argc; ++optind)
  288. {
  289. if (streq (argv[optind], "-"))
  290. datasection_only = false;
  291. else
  292. {
  293. files_given = true;
  294. exit_status |= !strings_file (argv[optind]);
  295. }
  296. }
  297. }
  298. if (!files_given)
  299. usage (stderr, 1);
  300. return (exit_status);
  301. }
  302. /* Scan section SECT of the file ABFD, whose printable name is
  303. FILENAME. If it contains initialized data set GOT_A_SECTION and
  304. print the strings in it. */
  305. static void
  306. strings_a_section (bfd *abfd, asection *sect, const char *filename,
  307. bool *got_a_section)
  308. {
  309. bfd_size_type sectsize;
  310. bfd_byte *mem;
  311. if ((sect->flags & DATA_FLAGS) != DATA_FLAGS)
  312. return;
  313. sectsize = bfd_section_size (sect);
  314. if (sectsize == 0)
  315. return;
  316. if (!bfd_malloc_and_get_section (abfd, sect, &mem))
  317. {
  318. non_fatal (_("%s: Reading section %s failed: %s"),
  319. filename, sect->name, bfd_errmsg (bfd_get_error ()));
  320. return;
  321. }
  322. *got_a_section = true;
  323. print_strings (filename, NULL, sect->filepos, sectsize, (char *) mem);
  324. free (mem);
  325. }
  326. /* Scan all of the sections in FILE, and print the strings
  327. in the initialized data section(s).
  328. Return TRUE if successful,
  329. FALSE if not (such as if FILE is not an object file). */
  330. static bool
  331. strings_object_file (const char *file)
  332. {
  333. bfd *abfd;
  334. asection *s;
  335. bool got_a_section;
  336. abfd = bfd_openr (file, target);
  337. if (abfd == NULL)
  338. /* Treat the file as a non-object file. */
  339. return false;
  340. /* This call is mainly for its side effect of reading in the sections.
  341. We follow the traditional behavior of `strings' in that we don't
  342. complain if we don't recognize a file to be an object file. */
  343. if (!bfd_check_format (abfd, bfd_object))
  344. {
  345. bfd_close (abfd);
  346. return false;
  347. }
  348. got_a_section = false;
  349. for (s = abfd->sections; s != NULL; s = s->next)
  350. strings_a_section (abfd, s, file, &got_a_section);
  351. if (!bfd_close (abfd))
  352. {
  353. bfd_nonfatal (file);
  354. return false;
  355. }
  356. return got_a_section;
  357. }
  358. /* Print the strings in FILE. Return TRUE if ok, FALSE if an error occurs. */
  359. static bool
  360. strings_file (char *file)
  361. {
  362. struct stat st;
  363. /* get_file_size does not support non-S_ISREG files. */
  364. if (stat (file, &st) < 0)
  365. {
  366. if (errno == ENOENT)
  367. non_fatal (_("'%s': No such file"), file);
  368. else
  369. non_fatal (_("Warning: could not locate '%s'. reason: %s"),
  370. file, strerror (errno));
  371. return false;
  372. }
  373. else if (S_ISDIR (st.st_mode))
  374. {
  375. non_fatal (_("Warning: '%s' is a directory"), file);
  376. return false;
  377. }
  378. /* If we weren't told to scan the whole file,
  379. try to open it as an object file and only look at
  380. initialized data sections. If that fails, fall back to the
  381. whole file. */
  382. if (!datasection_only || !strings_object_file (file))
  383. {
  384. FILE *stream;
  385. stream = fopen (file, FOPEN_RB);
  386. if (stream == NULL)
  387. {
  388. fprintf (stderr, "%s: ", program_name);
  389. perror (file);
  390. return false;
  391. }
  392. print_strings (file, stream, (file_ptr) 0, 0, (char *) NULL);
  393. if (fclose (stream) == EOF)
  394. {
  395. fprintf (stderr, "%s: ", program_name);
  396. perror (file);
  397. return false;
  398. }
  399. }
  400. return true;
  401. }
  402. /* Read the next character, return EOF if none available.
  403. Assume that STREAM is positioned so that the next byte read
  404. is at address ADDRESS in the file.
  405. If STREAM is NULL, do not read from it.
  406. The caller can supply a buffer of characters
  407. to be processed before the data in STREAM.
  408. MAGIC is the address of the buffer and
  409. MAGICCOUNT is how many characters are in it. */
  410. static long
  411. get_char (FILE *stream, file_ptr *address, int *magiccount, char **magic)
  412. {
  413. int c, i;
  414. long r = 0;
  415. for (i = 0; i < encoding_bytes; i++)
  416. {
  417. if (*magiccount)
  418. {
  419. (*magiccount)--;
  420. c = *(*magic)++;
  421. }
  422. else
  423. {
  424. if (stream == NULL)
  425. return EOF;
  426. /* Only use getc_unlocked if we found a declaration for it.
  427. Otherwise, libc is not thread safe by default, and we
  428. should not use it. */
  429. #if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
  430. c = getc_unlocked (stream);
  431. #else
  432. c = getc (stream);
  433. #endif
  434. if (c == EOF)
  435. return EOF;
  436. }
  437. (*address)++;
  438. r = (r << 8) | (c & 0xff);
  439. }
  440. switch (encoding)
  441. {
  442. default:
  443. break;
  444. case 'l':
  445. r = ((r & 0xff) << 8) | ((r & 0xff00) >> 8);
  446. break;
  447. case 'L':
  448. r = (((r & 0xff) << 24) | ((r & 0xff00) << 8)
  449. | ((r & 0xff0000) >> 8) | ((r & 0xff000000) >> 24));
  450. break;
  451. }
  452. return r;
  453. }
  454. /* Throw away one byte of a (possibly) multi-byte char C, updating
  455. address and buffer to suit. */
  456. static void
  457. unget_part_char (long c, file_ptr *address, int *magiccount, char **magic)
  458. {
  459. static char tmp[4];
  460. if (encoding_bytes > 1)
  461. {
  462. *address -= encoding_bytes - 1;
  463. if (*magiccount == 0)
  464. {
  465. /* If no magic buffer exists, use temp buffer. */
  466. switch (encoding)
  467. {
  468. default:
  469. break;
  470. case 'b':
  471. tmp[0] = c & 0xff;
  472. *magiccount = 1;
  473. break;
  474. case 'l':
  475. tmp[0] = (c >> 8) & 0xff;
  476. *magiccount = 1;
  477. break;
  478. case 'B':
  479. tmp[0] = (c >> 16) & 0xff;
  480. tmp[1] = (c >> 8) & 0xff;
  481. tmp[2] = c & 0xff;
  482. *magiccount = 3;
  483. break;
  484. case 'L':
  485. tmp[0] = (c >> 8) & 0xff;
  486. tmp[1] = (c >> 16) & 0xff;
  487. tmp[2] = (c >> 24) & 0xff;
  488. *magiccount = 3;
  489. break;
  490. }
  491. *magic = tmp;
  492. }
  493. else
  494. {
  495. /* If magic buffer exists, rewind. */
  496. *magic -= encoding_bytes - 1;
  497. *magiccount += encoding_bytes - 1;
  498. }
  499. }
  500. }
  501. static void
  502. print_filename_and_address (const char * filename, file_ptr address)
  503. {
  504. if (print_filenames)
  505. printf ("%s: ", filename);
  506. if (! print_addresses)
  507. return;
  508. switch (address_radix)
  509. {
  510. case 8:
  511. if (sizeof (address) > sizeof (long))
  512. {
  513. #ifndef __MSVCRT__
  514. printf ("%7llo ", (unsigned long long) address);
  515. #else
  516. printf ("%7I64o ", (unsigned long long) address);
  517. #endif
  518. }
  519. else
  520. printf ("%7lo ", (unsigned long) address);
  521. break;
  522. case 10:
  523. if (sizeof (address) > sizeof (long))
  524. {
  525. #ifndef __MSVCRT__
  526. printf ("%7llu ", (unsigned long long) address);
  527. #else
  528. printf ("%7I64d ", (unsigned long long) address);
  529. #endif
  530. }
  531. else
  532. printf ("%7ld ", (long) address);
  533. break;
  534. case 16:
  535. if (sizeof (address) > sizeof (long))
  536. {
  537. #ifndef __MSVCRT__
  538. printf ("%7llx ", (unsigned long long) address);
  539. #else
  540. printf ("%7I64x ", (unsigned long long) address);
  541. #endif
  542. }
  543. else
  544. printf ("%7lx ", (unsigned long) address);
  545. break;
  546. }
  547. }
  548. /* Return non-zero if the bytes starting at BUFFER form a valid UTF-8 encoding.
  549. If the encoding is valid then returns the number of bytes it uses. */
  550. static unsigned int
  551. is_valid_utf8 (const unsigned char * buffer, unsigned long buflen)
  552. {
  553. if (buffer[0] < 0xc0)
  554. return 0;
  555. if (buflen < 2)
  556. return 0;
  557. if ((buffer[1] & 0xc0) != 0x80)
  558. return 0;
  559. if ((buffer[0] & 0x20) == 0)
  560. return 2;
  561. if (buflen < 3)
  562. return 0;
  563. if ((buffer[2] & 0xc0) != 0x80)
  564. return 0;
  565. if ((buffer[0] & 0x10) == 0)
  566. return 3;
  567. if (buflen < 4)
  568. return 0;
  569. if ((buffer[3] & 0xc0) != 0x80)
  570. return 0;
  571. return 4;
  572. }
  573. /* Display a UTF-8 encoded character in BUFFER according to the setting
  574. of unicode_display. The character is known to be valid.
  575. Returns the number of bytes consumed. */
  576. static unsigned int
  577. display_utf8_char (const unsigned char * buffer)
  578. {
  579. unsigned int j;
  580. unsigned int utf8_len;
  581. switch (buffer[0] & 0x30)
  582. {
  583. case 0x00:
  584. case 0x10:
  585. utf8_len = 2;
  586. break;
  587. case 0x20:
  588. utf8_len = 3;
  589. break;
  590. default:
  591. utf8_len = 4;
  592. }
  593. switch (unicode_display)
  594. {
  595. default:
  596. fprintf (stderr, "ICE: unexpected unicode display type\n");
  597. break;
  598. case unicode_escape:
  599. case unicode_highlight:
  600. if (unicode_display == unicode_highlight && isatty (1))
  601. printf ("\x1B[31;47m"); /* Red. */
  602. switch (utf8_len)
  603. {
  604. case 2:
  605. printf ("\\u%02x%02x",
  606. ((buffer[0] & 0x1c) >> 2),
  607. ((buffer[0] & 0x03) << 6) | (buffer[1] & 0x3f));
  608. break;
  609. case 3:
  610. printf ("\\u%02x%02x",
  611. ((buffer[0] & 0x0f) << 4) | ((buffer[1] & 0x3c) >> 2),
  612. ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3f)));
  613. break;
  614. case 4:
  615. printf ("\\u%02x%02x%02x",
  616. ((buffer[0] & 0x07) << 6) | ((buffer[1] & 0x3c) >> 2),
  617. ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3c) >> 2),
  618. ((buffer[2] & 0x03) << 6) | ((buffer[3] & 0x3f)));
  619. break;
  620. default:
  621. /* URG. */
  622. break;
  623. }
  624. if (unicode_display == unicode_highlight && isatty (1))
  625. printf ("\033[0m"); /* Default colour. */
  626. break;
  627. case unicode_hex:
  628. putchar ('<');
  629. printf ("0x");
  630. for (j = 0; j < utf8_len; j++)
  631. printf ("%02x", buffer [j]);
  632. putchar ('>');
  633. break;
  634. case unicode_locale:
  635. printf ("%.1s", buffer);
  636. break;
  637. }
  638. return utf8_len;
  639. }
  640. /* Display strings in BUFFER. Treat any UTF-8 encoded characters encountered
  641. according to the setting of the unicode_display variable. The buffer
  642. contains BUFLEN bytes.
  643. Display the characters as if they started at ADDRESS and are contained in
  644. FILENAME. */
  645. static void
  646. print_unicode_buffer (const char * filename,
  647. file_ptr address,
  648. const unsigned char * buffer,
  649. unsigned long buflen)
  650. {
  651. /* Paranoia checks... */
  652. if (filename == NULL
  653. || buffer == NULL
  654. || unicode_display == unicode_default
  655. || encoding != 'S'
  656. || encoding_bytes != 1)
  657. {
  658. fprintf (stderr, "ICE: bad arguments to print_unicode_buffer\n");
  659. return;
  660. }
  661. if (buflen == 0)
  662. return;
  663. /* We must only display strings that are at least string_min *characters*
  664. long. So we scan the buffer in two stages. First we locate the start
  665. of a potential string. Then we walk along it until we have found
  666. string_min characters. Then we go back to the start point and start
  667. displaying characters according to the unicode_display setting. */
  668. unsigned long start_point = 0;
  669. unsigned long i = 0;
  670. unsigned int char_len = 1;
  671. unsigned int num_found = 0;
  672. for (i = 0; i < buflen; i += char_len)
  673. {
  674. int c = buffer[i];
  675. char_len = 1;
  676. /* Find the first potential character of a string. */
  677. if (! STRING_ISGRAPHIC (c))
  678. {
  679. num_found = 0;
  680. continue;
  681. }
  682. if (c > 126)
  683. {
  684. if (c < 0xc0)
  685. {
  686. num_found = 0;
  687. continue;
  688. }
  689. if ((char_len = is_valid_utf8 (buffer + i, buflen - i)) == 0)
  690. {
  691. char_len = 1;
  692. num_found = 0;
  693. continue;
  694. }
  695. if (unicode_display == unicode_invalid)
  696. {
  697. /* We have found a valid UTF-8 character, but we treat it as non-graphic. */
  698. num_found = 0;
  699. continue;
  700. }
  701. }
  702. if (num_found == 0)
  703. /* We have found a potential starting point for a string. */
  704. start_point = i;
  705. ++ num_found;
  706. if (num_found >= string_min)
  707. break;
  708. }
  709. if (num_found < string_min)
  710. return;
  711. print_filename_and_address (filename, address + start_point);
  712. /* We have found string_min characters. Display them and any
  713. more that follow. */
  714. for (i = start_point; i < buflen; i += char_len)
  715. {
  716. int c = buffer[i];
  717. char_len = 1;
  718. if (! STRING_ISGRAPHIC (c))
  719. break;
  720. else if (c < 127)
  721. putchar (c);
  722. else if (! is_valid_utf8 (buffer + i, buflen - i))
  723. break;
  724. else if (unicode_display == unicode_invalid)
  725. break;
  726. else
  727. char_len = display_utf8_char (buffer + i);
  728. }
  729. if (output_separator)
  730. fputs (output_separator, stdout);
  731. else
  732. putchar ('\n');
  733. /* FIXME: Using tail recursion here is lazy programming... */
  734. print_unicode_buffer (filename, address + i, buffer + i, buflen - i);
  735. }
  736. static int
  737. get_unicode_byte (FILE * stream,
  738. unsigned char * putback,
  739. unsigned int * num_putback,
  740. unsigned int * num_read)
  741. {
  742. if (* num_putback > 0)
  743. {
  744. * num_putback = * num_putback - 1;
  745. return putback [* num_putback];
  746. }
  747. * num_read = * num_read + 1;
  748. #if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
  749. return getc_unlocked (stream);
  750. #else
  751. return getc (stream);
  752. #endif
  753. }
  754. /* Helper function for print_unicode_stream. */
  755. static void
  756. print_unicode_stream_body (const char * filename,
  757. file_ptr address,
  758. FILE * stream,
  759. unsigned char * putback_buf,
  760. unsigned int num_putback,
  761. unsigned char * print_buf)
  762. {
  763. /* It would be nice if we could just read the stream into a buffer
  764. and then process if with print_unicode_buffer. But the input
  765. might be huge or it might time-locked (eg stdin). So instead
  766. we go one byte at a time... */
  767. file_ptr start_point = 0;
  768. unsigned int num_read = 0;
  769. unsigned int num_chars = 0;
  770. unsigned int num_print = 0;
  771. int c = 0;
  772. /* Find a series of string_min characters. Put them into print_buf. */
  773. do
  774. {
  775. if (num_chars >= string_min)
  776. break;
  777. c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
  778. if (c == EOF)
  779. break;
  780. if (! STRING_ISGRAPHIC (c))
  781. {
  782. num_chars = num_print = 0;
  783. continue;
  784. }
  785. if (num_chars == 0)
  786. start_point = num_read - 1;
  787. if (c < 127)
  788. {
  789. print_buf[num_print] = c;
  790. num_chars ++;
  791. num_print ++;
  792. continue;
  793. }
  794. if (c < 0xc0)
  795. {
  796. num_chars = num_print = 0;
  797. continue;
  798. }
  799. /* We *might* have a UTF-8 sequence. Time to start peeking. */
  800. char utf8[4];
  801. utf8[0] = c;
  802. c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
  803. if (c == EOF)
  804. break;
  805. utf8[1] = c;
  806. if ((utf8[1] & 0xc0) != 0x80)
  807. {
  808. /* Invalid UTF-8. */
  809. putback_buf[num_putback++] = utf8[1];
  810. num_chars = num_print = 0;
  811. continue;
  812. }
  813. else if ((utf8[0] & 0x20) == 0)
  814. {
  815. /* A valid 2-byte UTF-8 encoding. */
  816. if (unicode_display == unicode_invalid)
  817. {
  818. putback_buf[num_putback++] = utf8[1];
  819. num_chars = num_print = 0;
  820. }
  821. else
  822. {
  823. print_buf[num_print ++] = utf8[0];
  824. print_buf[num_print ++] = utf8[1];
  825. num_chars ++;
  826. }
  827. continue;
  828. }
  829. c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
  830. if (c == EOF)
  831. break;
  832. utf8[2] = c;
  833. if ((utf8[2] & 0xc0) != 0x80)
  834. {
  835. /* Invalid UTF-8. */
  836. putback_buf[num_putback++] = utf8[2];
  837. putback_buf[num_putback++] = utf8[1];
  838. num_chars = num_print = 0;
  839. continue;
  840. }
  841. else if ((utf8[0] & 0x10) == 0)
  842. {
  843. /* A valid 3-byte UTF-8 encoding. */
  844. if (unicode_display == unicode_invalid)
  845. {
  846. putback_buf[num_putback++] = utf8[2];
  847. putback_buf[num_putback++] = utf8[1];
  848. num_chars = num_print = 0;
  849. }
  850. else
  851. {
  852. print_buf[num_print ++] = utf8[0];
  853. print_buf[num_print ++] = utf8[1];
  854. print_buf[num_print ++] = utf8[2];
  855. num_chars ++;
  856. }
  857. continue;
  858. }
  859. c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
  860. if (c == EOF)
  861. break;
  862. utf8[3] = c;
  863. if ((utf8[3] & 0xc0) != 0x80)
  864. {
  865. /* Invalid UTF-8. */
  866. putback_buf[num_putback++] = utf8[3];
  867. putback_buf[num_putback++] = utf8[2];
  868. putback_buf[num_putback++] = utf8[1];
  869. num_chars = num_print = 0;
  870. }
  871. /* We have a valid 4-byte UTF-8 encoding. */
  872. else if (unicode_display == unicode_invalid)
  873. {
  874. putback_buf[num_putback++] = utf8[3];
  875. putback_buf[num_putback++] = utf8[1];
  876. putback_buf[num_putback++] = utf8[2];
  877. num_chars = num_print = 0;
  878. }
  879. else
  880. {
  881. print_buf[num_print ++] = utf8[0];
  882. print_buf[num_print ++] = utf8[1];
  883. print_buf[num_print ++] = utf8[2];
  884. print_buf[num_print ++] = utf8[3];
  885. num_chars ++;
  886. }
  887. }
  888. while (1);
  889. if (num_chars >= string_min)
  890. {
  891. /* We know that we have string_min valid characters in print_buf,
  892. and there may be more to come in the stream. Start displaying
  893. them. */
  894. print_filename_and_address (filename, address + start_point);
  895. unsigned int i;
  896. for (i = 0; i < num_print;)
  897. {
  898. if (print_buf[i] < 127)
  899. putchar (print_buf[i++]);
  900. else
  901. i += display_utf8_char (print_buf + i);
  902. }
  903. /* OK so now we have to start read unchecked bytes. */
  904. /* Find a series of string_min characters. Put them into print_buf. */
  905. do
  906. {
  907. c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
  908. if (c == EOF)
  909. break;
  910. if (! STRING_ISGRAPHIC (c))
  911. break;
  912. if (c < 127)
  913. {
  914. putchar (c);
  915. continue;
  916. }
  917. if (c < 0xc0)
  918. break;
  919. /* We *might* have a UTF-8 sequence. Time to start peeking. */
  920. unsigned char utf8[4];
  921. utf8[0] = c;
  922. c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
  923. if (c == EOF)
  924. break;
  925. utf8[1] = c;
  926. if ((utf8[1] & 0xc0) != 0x80)
  927. {
  928. /* Invalid UTF-8. */
  929. putback_buf[num_putback++] = utf8[1];
  930. break;
  931. }
  932. else if ((utf8[0] & 0x20) == 0)
  933. {
  934. /* Valid 2-byte UTF-8. */
  935. if (unicode_display == unicode_invalid)
  936. {
  937. putback_buf[num_putback++] = utf8[1];
  938. break;
  939. }
  940. else
  941. {
  942. (void) display_utf8_char (utf8);
  943. continue;
  944. }
  945. }
  946. c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
  947. if (c == EOF)
  948. break;
  949. utf8[2] = c;
  950. if ((utf8[2] & 0xc0) != 0x80)
  951. {
  952. /* Invalid UTF-8. */
  953. putback_buf[num_putback++] = utf8[2];
  954. putback_buf[num_putback++] = utf8[1];
  955. break;
  956. }
  957. else if ((utf8[0] & 0x10) == 0)
  958. {
  959. /* Valid 3-byte UTF-8. */
  960. if (unicode_display == unicode_invalid)
  961. {
  962. putback_buf[num_putback++] = utf8[2];
  963. putback_buf[num_putback++] = utf8[1];
  964. break;
  965. }
  966. else
  967. {
  968. (void) display_utf8_char (utf8);
  969. continue;
  970. }
  971. }
  972. c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
  973. if (c == EOF)
  974. break;
  975. utf8[3] = c;
  976. if ((utf8[3] & 0xc0) != 0x80)
  977. {
  978. /* Invalid UTF-8. */
  979. putback_buf[num_putback++] = utf8[3];
  980. putback_buf[num_putback++] = utf8[2];
  981. putback_buf[num_putback++] = utf8[1];
  982. break;
  983. }
  984. else if (unicode_display == unicode_invalid)
  985. {
  986. putback_buf[num_putback++] = utf8[3];
  987. putback_buf[num_putback++] = utf8[2];
  988. putback_buf[num_putback++] = utf8[1];
  989. break;
  990. }
  991. else
  992. /* A valid 4-byte UTF-8 encoding. */
  993. (void) display_utf8_char (utf8);
  994. }
  995. while (1);
  996. if (output_separator)
  997. fputs (output_separator, stdout);
  998. else
  999. putchar ('\n');
  1000. }
  1001. if (c != EOF)
  1002. /* FIXME: Using tail recursion here is lazy, but it works. */
  1003. print_unicode_stream_body (filename, address + num_read, stream, putback_buf, num_putback, print_buf);
  1004. }
  1005. /* Display strings read in from STREAM. Treat any UTF-8 encoded characters
  1006. encountered according to the setting of the unicode_display variable.
  1007. The stream is positioned at ADDRESS and is attached to FILENAME. */
  1008. static void
  1009. print_unicode_stream (const char * filename,
  1010. file_ptr address,
  1011. FILE * stream)
  1012. {
  1013. /* Paranoia checks... */
  1014. if (filename == NULL
  1015. || stream == NULL
  1016. || unicode_display == unicode_default
  1017. || encoding != 'S'
  1018. || encoding_bytes != 1)
  1019. {
  1020. fprintf (stderr, "ICE: bad arguments to print_unicode_stream\n");
  1021. return;
  1022. }
  1023. /* Allocate space for string_min 4-byte utf-8 characters. */
  1024. unsigned char * print_buf = xmalloc ((4 * string_min) + 1);
  1025. /* We should never have to put back more than 4 bytes. */
  1026. unsigned char putback_buf[5];
  1027. unsigned int num_putback = 0;
  1028. print_unicode_stream_body (filename, address, stream, putback_buf, num_putback, print_buf);
  1029. free (print_buf);
  1030. }
  1031. /* Find the strings in file FILENAME, read from STREAM.
  1032. Assume that STREAM is positioned so that the next byte read
  1033. is at address ADDRESS in the file.
  1034. If STREAM is NULL, do not read from it.
  1035. The caller can supply a buffer of characters
  1036. to be processed before the data in STREAM.
  1037. MAGIC is the address of the buffer and
  1038. MAGICCOUNT is how many characters are in it.
  1039. Those characters come at address ADDRESS and the data in STREAM follow. */
  1040. static void
  1041. print_strings (const char *filename, FILE *stream, file_ptr address,
  1042. int magiccount, char *magic)
  1043. {
  1044. if (unicode_display != unicode_default)
  1045. {
  1046. if (magic != NULL)
  1047. print_unicode_buffer (filename, address,
  1048. (const unsigned char *) magic, magiccount);
  1049. if (stream != NULL)
  1050. print_unicode_stream (filename, address, stream);
  1051. return;
  1052. }
  1053. char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
  1054. while (1)
  1055. {
  1056. file_ptr start;
  1057. unsigned int i;
  1058. long c;
  1059. /* See if the next `string_min' chars are all graphic chars. */
  1060. tryline:
  1061. start = address;
  1062. for (i = 0; i < string_min; i++)
  1063. {
  1064. c = get_char (stream, &address, &magiccount, &magic);
  1065. if (c == EOF)
  1066. {
  1067. free (buf);
  1068. return;
  1069. }
  1070. if (! STRING_ISGRAPHIC (c))
  1071. {
  1072. /* Found a non-graphic. Try again starting with next byte. */
  1073. unget_part_char (c, &address, &magiccount, &magic);
  1074. goto tryline;
  1075. }
  1076. buf[i] = c;
  1077. }
  1078. /* We found a run of `string_min' graphic characters. Print up
  1079. to the next non-graphic character. */
  1080. print_filename_and_address (filename, start);
  1081. buf[i] = '\0';
  1082. fputs (buf, stdout);
  1083. while (1)
  1084. {
  1085. c = get_char (stream, &address, &magiccount, &magic);
  1086. if (c == EOF)
  1087. break;
  1088. if (! STRING_ISGRAPHIC (c))
  1089. {
  1090. unget_part_char (c, &address, &magiccount, &magic);
  1091. break;
  1092. }
  1093. putchar (c);
  1094. }
  1095. if (output_separator)
  1096. fputs (output_separator, stdout);
  1097. else
  1098. putchar ('\n');
  1099. }
  1100. free (buf);
  1101. }
  1102. static void
  1103. usage (FILE *stream, int status)
  1104. {
  1105. fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
  1106. fprintf (stream, _(" Display printable strings in [file(s)] (stdin by default)\n"));
  1107. fprintf (stream, _(" The options are:\n"));
  1108. if (DEFAULT_STRINGS_ALL)
  1109. fprintf (stream, _("\
  1110. -a - --all Scan the entire file, not just the data section [default]\n\
  1111. -d --data Only scan the data sections in the file\n"));
  1112. else
  1113. fprintf (stream, _("\
  1114. -a - --all Scan the entire file, not just the data section\n\
  1115. -d --data Only scan the data sections in the file [default]\n"));
  1116. fprintf (stream, _("\
  1117. -f --print-file-name Print the name of the file before each string\n\
  1118. -n <number> Locate & print any sequence of at least <number>\n\
  1119. --bytes=<number> displayable characters. (The default is 4).\n\
  1120. -t --radix={o,d,x} Print the location of the string in base 8, 10 or 16\n\
  1121. -w --include-all-whitespace Include all whitespace as valid string characters\n\
  1122. -o An alias for --radix=o\n\
  1123. -T --target=<BFDNAME> Specify the binary file format\n\
  1124. -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
  1125. s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
  1126. --unicode={default|show|invalid|hex|escape|highlight}\n\
  1127. -U {d|s|i|x|e|h} Specify how to treat UTF-8 encoded unicode characters\n\
  1128. -s --output-separator=<string> String used to separate strings in output.\n\
  1129. @<file> Read options from <file>\n\
  1130. -h --help Display this information\n\
  1131. -v -V --version Print the program's version number\n"));
  1132. list_supported_targets (program_name, stream);
  1133. if (REPORT_BUGS_TO[0] && status == 0)
  1134. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  1135. exit (status);
  1136. }