2 * Copyright (c) 2007 S.Sam Arun Raj
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/types.h>
49 ELFTC_VCSID("$Id: strings.c 3571 2017-09-14 02:04:50Z emaste $");
61 ENCODING_16BIT_LITTLE,
66 #define PRINTABLE(c) \
67 ((c) >= 0 && (c) <= 255 && \
68 ((c) == '\t' || isprint((c)) || \
69 (encoding == ENCODING_8BIT && (c) > 127)))
71 static int encoding_size, entire_file, show_filename, show_loc;
72 static enum encoding_style encoding;
73 static enum radix_style radix;
74 static intmax_t min_len;
76 static struct option strings_longopts[] = {
77 { "all", no_argument, NULL, 'a'},
78 { "bytes", required_argument, NULL, 'n'},
79 { "encoding", required_argument, NULL, 'e'},
80 { "help", no_argument, NULL, 'h'},
81 { "print-file-name", no_argument, NULL, 'f'},
82 { "radix", required_argument, NULL, 't'},
83 { "version", no_argument, NULL, 'v'},
87 long getcharacter(void);
88 int handle_file(const char *);
89 int handle_elf(const char *, int);
90 int handle_binary(const char *, int);
91 int find_strings(const char *, off_t, off_t);
92 void show_version(void);
96 * strings(1) extracts text(contiguous printable characters)
97 * from elf and binary files.
100 main(int argc, char **argv)
107 if (elf_version(EV_CURRENT) == EV_NONE)
108 errx(EXIT_FAILURE, "ELF library initialization failed: %s",
111 while ((ch = getopt_long(argc, argv, "1234567890ae:fhn:ot:Vv",
112 strings_longopts, NULL)) != -1)
118 if (*optarg == 's') {
119 encoding = ENCODING_7BIT;
120 } else if (*optarg == 'S') {
121 encoding = ENCODING_8BIT;
122 } else if (*optarg == 'b') {
123 encoding = ENCODING_16BIT_BIG;
125 } else if (*optarg == 'B') {
126 encoding = ENCODING_32BIT_BIG;
128 } else if (*optarg == 'l') {
129 encoding = ENCODING_16BIT_LITTLE;
131 } else if (*optarg == 'L') {
132 encoding = ENCODING_32BIT_LITTLE;
142 min_len = strtoimax(optarg, (char**)NULL, 10);
144 errx(EX_USAGE, "option -n should specify a "
145 "positive decimal integer.");
154 radix = RADIX_DECIMAL;
155 else if (*optarg == 'o')
157 else if (*optarg == 'x')
192 rc = find_strings("{standard input}", 0, 0);
194 if (handle_file(*argv) != 0)
202 handle_file(const char *name)
208 if (freopen(name, "rb", stdin) == NULL) {
209 warnx("'%s': %s", name, strerror(errno));
216 rt = handle_elf(name, fd);
221 * Files not understood by handle_elf, will be passed off here and will
222 * treated as a binary file. This would include text file, core dumps ...
225 handle_binary(const char *name, int fd)
229 memset(&buf, 0, sizeof(struct stat));
230 (void) lseek(fd, (off_t)0, SEEK_SET);
231 if (!fstat(fd, &buf))
232 return (find_strings(name, (off_t)0, buf.st_size));
237 * Will analyse a file to see if it ELF, other files including ar(1),
238 * core dumps are passed off and treated as flat binary files. Unlike
239 * GNU size in FreeBSD this routine will not treat ELF object from
240 * different archs as flat binary files(has to overridden using -a).
243 handle_elf(const char *name, int fd)
252 /* If entire file is chosen, treat it as a binary file */
254 return (handle_binary(name, fd));
256 (void) lseek(fd, (off_t)0, SEEK_SET);
257 elf = elf_begin(fd, ELF_C_READ, NULL);
258 if (elf_kind(elf) != ELF_K_ELF) {
260 return (handle_binary(name, fd));
263 if (gelf_getehdr(elf, &elfhdr) == NULL) {
265 warnx("%s: ELF file could not be processed", name);
269 if (elfhdr.e_shnum == 0 && elfhdr.e_type == ET_CORE) {
271 return (handle_binary(name, fd));
274 while ((scn = elf_nextscn(elf, scn)) != NULL) {
275 if (gelf_getshdr(scn, &shdr) == NULL)
277 if (shdr.sh_type != SHT_NOBITS &&
278 (shdr.sh_flags & SHF_ALLOC) != 0) {
279 rc = find_strings(name, shdr.sh_offset,
289 * Retrieves a character from input stream based on the encoding
300 for(i = 0; i < encoding_size; i++) {
312 case ENCODING_16BIT_BIG:
313 rt = (buf[0] << 8) | buf[1];
315 case ENCODING_16BIT_LITTLE:
316 rt = buf[0] | (buf[1] << 8);
318 case ENCODING_32BIT_BIG:
319 rt = ((long) buf[0] << 24) | ((long) buf[1] << 16) |
320 ((long) buf[2] << 8) | buf[3];
322 case ENCODING_32BIT_LITTLE:
323 rt = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) |
324 ((long) buf[3] << 24);
331 * Input stream stdin is read until the end of file is reached or until
332 * the section size is reached in case of ELF files. Contiguous
333 * characters of >= min_size(default 4) will be displayed.
336 find_strings(const char *name, off_t offset, off_t size)
338 off_t cur_off, start_off;
343 if ((obuf = (char*)calloc(1, min_len + 1)) == NULL) {
344 (void) fprintf(stderr, "Unable to allocate memory: %s\n",
349 (void) fseeko(stdin, offset, SEEK_SET);
353 if ((offset + size) && (cur_off >= offset + size))
356 memset(obuf, 0, min_len+1);
357 for(i = 0; i < min_len; i++) {
359 if (c == EOF && feof(stdin))
364 cur_off += encoding_size;
366 if (encoding == ENCODING_8BIT &&
370 cur_off += encoding_size;
373 cur_off += encoding_size;
378 if (i >= min_len && ((cur_off <= offset + size) ||
381 printf ("%s: ", name);
385 (void) printf("%7ju ",
386 (uintmax_t)start_off);
389 (void) printf("%7jx ",
390 (uintmax_t)start_off);
393 (void) printf("%7jo ",
394 (uintmax_t)start_off);
401 if ((offset + size) &&
402 (cur_off >= offset + size))
405 cur_off += encoding_size;
406 if (encoding == ENCODING_8BIT &&
411 if (!PRINTABLE(c) || c == EOF)
423 #define USAGE_MESSAGE "\
424 Usage: %s [options] [file...]\n\
425 Print contiguous sequences of printable characters.\n\n\
427 -a | --all Scan the entire file for strings.\n\
428 -e ENC | --encoding=ENC Select the character encoding to use.\n\
429 -f | --print-file-name Print the file name before each string.\n\
430 -h | --help Print a help message and exit.\n\
431 -n N | --bytes=N | -N Print sequences with 'N' or more characters.\n\
432 -o Print offsets in octal.\n\
433 -t R | --radix=R Print offsets using the radix named by 'R'.\n\
434 -v | --version Print a version identifier and exit.\n"
439 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
446 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());