2014-01-15 08:43:20 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2007 S.Sam Arun Raj
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2019-04-04 16:32:27 +00:00
|
|
|
#include <sys/capsicum.h>
|
2019-01-10 14:18:11 +00:00
|
|
|
#include <sys/stat.h>
|
2014-01-15 08:43:20 +00:00
|
|
|
|
2019-04-04 16:32:27 +00:00
|
|
|
#include <capsicum_helpers.h>
|
2014-01-15 08:43:20 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-02-10 19:39:36 +00:00
|
|
|
#include <sysexits.h>
|
2014-01-15 08:43:20 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <libelf.h>
|
|
|
|
#include <libelftc.h>
|
|
|
|
#include <gelf.h>
|
|
|
|
|
2019-04-04 16:32:27 +00:00
|
|
|
#include <libcasper.h>
|
|
|
|
#include <casper/cap_fileargs.h>
|
|
|
|
|
2014-01-15 08:43:20 +00:00
|
|
|
#include "_elftc.h"
|
|
|
|
|
2019-01-10 14:18:11 +00:00
|
|
|
ELFTC_VCSID("$Id: strings.c 3648 2018-11-22 23:26:43Z emaste $");
|
2014-01-15 08:43:20 +00:00
|
|
|
|
|
|
|
enum radix_style {
|
|
|
|
RADIX_DECIMAL,
|
|
|
|
RADIX_HEX,
|
|
|
|
RADIX_OCTAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum encoding_style {
|
|
|
|
ENCODING_7BIT,
|
|
|
|
ENCODING_8BIT,
|
|
|
|
ENCODING_16BIT_BIG,
|
|
|
|
ENCODING_16BIT_LITTLE,
|
|
|
|
ENCODING_32BIT_BIG,
|
|
|
|
ENCODING_32BIT_LITTLE
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PRINTABLE(c) \
|
2019-01-10 14:18:11 +00:00
|
|
|
((c) >= 0 && (c) <= 255 && \
|
2014-01-15 08:43:20 +00:00
|
|
|
((c) == '\t' || isprint((c)) || \
|
|
|
|
(encoding == ENCODING_8BIT && (c) > 127)))
|
|
|
|
|
2016-02-10 19:39:36 +00:00
|
|
|
static int encoding_size, entire_file, show_filename, show_loc;
|
2014-12-22 20:32:23 +00:00
|
|
|
static enum encoding_style encoding;
|
|
|
|
static enum radix_style radix;
|
2016-02-10 19:39:36 +00:00
|
|
|
static intmax_t min_len;
|
2014-01-15 08:43:20 +00:00
|
|
|
|
|
|
|
static struct option strings_longopts[] = {
|
|
|
|
{ "all", no_argument, NULL, 'a'},
|
|
|
|
{ "bytes", required_argument, NULL, 'n'},
|
|
|
|
{ "encoding", required_argument, NULL, 'e'},
|
|
|
|
{ "help", no_argument, NULL, 'h'},
|
|
|
|
{ "print-file-name", no_argument, NULL, 'f'},
|
|
|
|
{ "radix", required_argument, NULL, 't'},
|
|
|
|
{ "version", no_argument, NULL, 'v'},
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2019-03-23 02:17:07 +00:00
|
|
|
int getcharacter(FILE *, long *);
|
2019-04-04 16:32:27 +00:00
|
|
|
int handle_file(fileargs_t *fa, const char *);
|
2019-03-21 05:00:24 +00:00
|
|
|
int handle_elf(const char *, FILE *);
|
|
|
|
int handle_binary(const char *, FILE *, size_t);
|
|
|
|
int find_strings(const char *, FILE *, off_t, off_t);
|
2014-01-15 08:43:20 +00:00
|
|
|
void show_version(void);
|
|
|
|
void usage(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* strings(1) extracts text(contiguous printable characters)
|
|
|
|
* from elf and binary files.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2019-04-04 16:32:27 +00:00
|
|
|
fileargs_t *fa;
|
|
|
|
cap_rights_t rights;
|
2014-01-15 08:43:20 +00:00
|
|
|
int ch, rc;
|
|
|
|
|
2017-04-17 23:14:26 +00:00
|
|
|
rc = 0;
|
2014-01-15 08:43:20 +00:00
|
|
|
min_len = 0;
|
|
|
|
encoding_size = 1;
|
|
|
|
if (elf_version(EV_CURRENT) == EV_NONE)
|
|
|
|
errx(EXIT_FAILURE, "ELF library initialization failed: %s",
|
|
|
|
elf_errmsg(-1));
|
|
|
|
|
|
|
|
while ((ch = getopt_long(argc, argv, "1234567890ae:fhn:ot:Vv",
|
2019-01-10 14:18:11 +00:00
|
|
|
strings_longopts, NULL)) != -1) {
|
|
|
|
switch ((char)ch) {
|
2014-01-15 08:43:20 +00:00
|
|
|
case 'a':
|
|
|
|
entire_file = 1;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (*optarg == 's') {
|
|
|
|
encoding = ENCODING_7BIT;
|
|
|
|
} else if (*optarg == 'S') {
|
|
|
|
encoding = ENCODING_8BIT;
|
|
|
|
} else if (*optarg == 'b') {
|
|
|
|
encoding = ENCODING_16BIT_BIG;
|
|
|
|
encoding_size = 2;
|
|
|
|
} else if (*optarg == 'B') {
|
|
|
|
encoding = ENCODING_32BIT_BIG;
|
|
|
|
encoding_size = 4;
|
|
|
|
} else if (*optarg == 'l') {
|
|
|
|
encoding = ENCODING_16BIT_LITTLE;
|
|
|
|
encoding_size = 2;
|
|
|
|
} else if (*optarg == 'L') {
|
|
|
|
encoding = ENCODING_32BIT_LITTLE;
|
|
|
|
encoding_size = 4;
|
|
|
|
} else
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
show_filename = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
2016-02-10 19:39:36 +00:00
|
|
|
min_len = strtoimax(optarg, (char**)NULL, 10);
|
|
|
|
if (min_len <= 0)
|
|
|
|
errx(EX_USAGE, "option -n should specify a "
|
|
|
|
"positive decimal integer.");
|
2014-01-15 08:43:20 +00:00
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
show_loc = 1;
|
|
|
|
radix = RADIX_OCTAL;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
show_loc = 1;
|
|
|
|
if (*optarg == 'd')
|
|
|
|
radix = RADIX_DECIMAL;
|
|
|
|
else if (*optarg == 'o')
|
|
|
|
radix = RADIX_OCTAL;
|
|
|
|
else if (*optarg == 'x')
|
|
|
|
radix = RADIX_HEX;
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
case 'V':
|
|
|
|
show_version();
|
|
|
|
/* NOTREACHED */
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
min_len *= 10;
|
|
|
|
min_len += ch - '0';
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2019-01-10 14:18:11 +00:00
|
|
|
}
|
2014-01-15 08:43:20 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2019-07-16 04:17:25 +00:00
|
|
|
cap_rights_init(&rights, CAP_READ, CAP_SEEK, CAP_FSTAT, CAP_FCNTL, CAP_MMAP_R);
|
2019-04-17 16:18:14 +00:00
|
|
|
fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN);
|
2019-04-04 16:32:27 +00:00
|
|
|
if (fa == NULL)
|
|
|
|
err(1, "Unable to initialize casper fileargs");
|
|
|
|
|
|
|
|
caph_cache_catpages();
|
2019-07-16 03:32:21 +00:00
|
|
|
if (caph_limit_stdio() < 0 || caph_enter_casper() < 0) {
|
2019-04-04 16:32:27 +00:00
|
|
|
fileargs_free(fa);
|
|
|
|
err(1, "Unable to enter capability mode");
|
|
|
|
}
|
|
|
|
|
2019-01-10 14:18:11 +00:00
|
|
|
if (min_len == 0)
|
2014-01-15 08:43:20 +00:00
|
|
|
min_len = 4;
|
2019-01-10 14:18:11 +00:00
|
|
|
if (*argv == NULL)
|
2019-03-21 05:00:24 +00:00
|
|
|
rc = find_strings("{standard input}", stdin, 0, 0);
|
2019-01-10 14:18:11 +00:00
|
|
|
else while (*argv != NULL) {
|
2019-04-04 16:32:27 +00:00
|
|
|
if (handle_file(fa, *argv) != 0)
|
2017-04-17 23:14:26 +00:00
|
|
|
rc = 1;
|
2014-01-15 08:43:20 +00:00
|
|
|
argv++;
|
|
|
|
}
|
2019-04-04 16:32:27 +00:00
|
|
|
|
|
|
|
fileargs_free(fa);
|
|
|
|
|
2014-01-15 08:43:20 +00:00
|
|
|
return (rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-04-04 16:32:27 +00:00
|
|
|
handle_file(fileargs_t *fa, const char *name)
|
2014-01-15 08:43:20 +00:00
|
|
|
{
|
2019-03-21 05:00:24 +00:00
|
|
|
FILE *pfile;
|
|
|
|
int rt;
|
2014-01-15 08:43:20 +00:00
|
|
|
|
|
|
|
if (name == NULL)
|
2017-04-17 23:14:26 +00:00
|
|
|
return (1);
|
2019-04-04 16:32:27 +00:00
|
|
|
pfile = fileargs_fopen(fa, name, "rb");
|
2019-03-21 05:00:24 +00:00
|
|
|
if (pfile == NULL) {
|
2018-04-27 12:02:11 +00:00
|
|
|
warnx("'%s': %s", name, strerror(errno));
|
|
|
|
return (1);
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 05:00:24 +00:00
|
|
|
rt = handle_elf(name, pfile);
|
|
|
|
fclose(pfile);
|
2014-01-15 08:43:20 +00:00
|
|
|
return (rt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Files not understood by handle_elf, will be passed off here and will
|
|
|
|
* treated as a binary file. This would include text file, core dumps ...
|
|
|
|
*/
|
|
|
|
int
|
2019-03-21 05:00:24 +00:00
|
|
|
handle_binary(const char *name, FILE *pfile, size_t size)
|
2014-01-15 08:43:20 +00:00
|
|
|
{
|
|
|
|
|
2019-03-21 05:00:24 +00:00
|
|
|
(void)fseeko(pfile, 0, SEEK_SET);
|
|
|
|
return (find_strings(name, pfile, 0, size));
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Will analyse a file to see if it ELF, other files including ar(1),
|
|
|
|
* core dumps are passed off and treated as flat binary files. Unlike
|
|
|
|
* GNU size in FreeBSD this routine will not treat ELF object from
|
|
|
|
* different archs as flat binary files(has to overridden using -a).
|
|
|
|
*/
|
|
|
|
int
|
2019-03-21 05:00:24 +00:00
|
|
|
handle_elf(const char *name, FILE *pfile)
|
2014-01-15 08:43:20 +00:00
|
|
|
{
|
2019-03-21 05:00:24 +00:00
|
|
|
struct stat buf;
|
2014-01-15 08:43:20 +00:00
|
|
|
GElf_Ehdr elfhdr;
|
|
|
|
GElf_Shdr shdr;
|
|
|
|
Elf *elf;
|
|
|
|
Elf_Scn *scn;
|
2019-03-21 05:00:24 +00:00
|
|
|
int rc, fd;
|
2014-01-15 08:43:20 +00:00
|
|
|
|
2017-04-17 23:14:26 +00:00
|
|
|
rc = 0;
|
2019-03-21 05:00:24 +00:00
|
|
|
fd = fileno(pfile);
|
|
|
|
if (fstat(fd, &buf) < 0)
|
|
|
|
return (1);
|
|
|
|
|
2016-05-19 20:06:56 +00:00
|
|
|
/* If entire file is chosen, treat it as a binary file */
|
2014-01-15 08:43:20 +00:00
|
|
|
if (entire_file)
|
2019-03-21 05:00:24 +00:00
|
|
|
return (handle_binary(name, pfile, buf.st_size));
|
2014-01-15 08:43:20 +00:00
|
|
|
|
2019-01-10 14:18:11 +00:00
|
|
|
(void)lseek(fd, 0, SEEK_SET);
|
2014-01-15 08:43:20 +00:00
|
|
|
elf = elf_begin(fd, ELF_C_READ, NULL);
|
|
|
|
if (elf_kind(elf) != ELF_K_ELF) {
|
2019-01-10 14:18:11 +00:00
|
|
|
(void)elf_end(elf);
|
2019-03-21 05:00:24 +00:00
|
|
|
return (handle_binary(name, pfile, buf.st_size));
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gelf_getehdr(elf, &elfhdr) == NULL) {
|
2019-01-10 14:18:11 +00:00
|
|
|
(void)elf_end(elf);
|
2014-01-15 08:43:20 +00:00
|
|
|
warnx("%s: ELF file could not be processed", name);
|
2017-04-17 23:14:26 +00:00
|
|
|
return (1);
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (elfhdr.e_shnum == 0 && elfhdr.e_type == ET_CORE) {
|
2019-01-10 14:18:11 +00:00
|
|
|
(void)elf_end(elf);
|
2019-03-21 05:00:24 +00:00
|
|
|
return (handle_binary(name, pfile, buf.st_size));
|
2014-01-15 08:43:20 +00:00
|
|
|
} else {
|
|
|
|
scn = NULL;
|
|
|
|
while ((scn = elf_nextscn(elf, scn)) != NULL) {
|
|
|
|
if (gelf_getshdr(scn, &shdr) == NULL)
|
|
|
|
continue;
|
|
|
|
if (shdr.sh_type != SHT_NOBITS &&
|
|
|
|
(shdr.sh_flags & SHF_ALLOC) != 0) {
|
2019-03-21 05:00:24 +00:00
|
|
|
rc = find_strings(name, pfile, shdr.sh_offset,
|
2014-01-15 08:43:20 +00:00
|
|
|
shdr.sh_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 14:18:11 +00:00
|
|
|
(void)elf_end(elf);
|
2014-01-15 08:43:20 +00:00
|
|
|
return (rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieves a character from input stream based on the encoding
|
|
|
|
* type requested.
|
|
|
|
*/
|
2019-03-23 02:17:07 +00:00
|
|
|
int
|
|
|
|
getcharacter(FILE *pfile, long *rt)
|
2014-01-15 08:43:20 +00:00
|
|
|
{
|
2019-03-21 06:20:33 +00:00
|
|
|
int i, c;
|
|
|
|
char buf[4];
|
2014-01-15 08:43:20 +00:00
|
|
|
|
|
|
|
for(i = 0; i < encoding_size; i++) {
|
2019-03-21 05:00:24 +00:00
|
|
|
c = getc(pfile);
|
2019-03-21 05:24:44 +00:00
|
|
|
if (c == EOF)
|
2019-03-23 02:17:07 +00:00
|
|
|
return (-1);
|
2014-01-15 08:43:20 +00:00
|
|
|
buf[i] = c;
|
|
|
|
}
|
|
|
|
|
2019-01-10 14:18:11 +00:00
|
|
|
switch (encoding) {
|
2014-01-15 08:43:20 +00:00
|
|
|
case ENCODING_7BIT:
|
|
|
|
case ENCODING_8BIT:
|
2019-03-23 02:17:07 +00:00
|
|
|
*rt = buf[0];
|
2014-01-15 08:43:20 +00:00
|
|
|
break;
|
|
|
|
case ENCODING_16BIT_BIG:
|
2019-03-23 02:17:07 +00:00
|
|
|
*rt = (buf[0] << 8) | buf[1];
|
2014-01-15 08:43:20 +00:00
|
|
|
break;
|
|
|
|
case ENCODING_16BIT_LITTLE:
|
2019-03-23 02:17:07 +00:00
|
|
|
*rt = buf[0] | (buf[1] << 8);
|
|
|
|
break;
|
2014-01-15 08:43:20 +00:00
|
|
|
case ENCODING_32BIT_BIG:
|
2019-03-23 02:17:07 +00:00
|
|
|
*rt = ((long) buf[0] << 24) | ((long) buf[1] << 16) |
|
2019-01-10 14:18:11 +00:00
|
|
|
((long) buf[2] << 8) | buf[3];
|
|
|
|
break;
|
2014-01-15 08:43:20 +00:00
|
|
|
case ENCODING_32BIT_LITTLE:
|
2019-03-23 02:17:07 +00:00
|
|
|
*rt = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) |
|
2019-01-10 14:18:11 +00:00
|
|
|
((long) buf[3] << 24);
|
|
|
|
break;
|
2019-03-23 02:17:07 +00:00
|
|
|
default:
|
|
|
|
return (-1);
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
2019-03-23 02:17:07 +00:00
|
|
|
|
|
|
|
return (0);
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-03-21 05:00:24 +00:00
|
|
|
* Input stream is read until the end of file is reached or until
|
2014-01-15 08:43:20 +00:00
|
|
|
* the section size is reached in case of ELF files. Contiguous
|
|
|
|
* characters of >= min_size(default 4) will be displayed.
|
|
|
|
*/
|
|
|
|
int
|
2019-03-21 05:00:24 +00:00
|
|
|
find_strings(const char *name, FILE *pfile, off_t offset, off_t size)
|
2014-01-15 08:43:20 +00:00
|
|
|
{
|
|
|
|
off_t cur_off, start_off;
|
|
|
|
char *obuf;
|
|
|
|
long c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((obuf = (char*)calloc(1, min_len + 1)) == NULL) {
|
2019-01-10 14:18:11 +00:00
|
|
|
fprintf(stderr, "Unable to allocate memory: %s\n",
|
|
|
|
strerror(errno));
|
2017-04-17 23:14:26 +00:00
|
|
|
return (1);
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 05:00:24 +00:00
|
|
|
(void)fseeko(pfile, offset, SEEK_SET);
|
2014-01-15 08:43:20 +00:00
|
|
|
cur_off = offset;
|
|
|
|
start_off = 0;
|
2019-01-10 14:18:11 +00:00
|
|
|
for (;;) {
|
2014-01-15 08:43:20 +00:00
|
|
|
if ((offset + size) && (cur_off >= offset + size))
|
|
|
|
break;
|
|
|
|
start_off = cur_off;
|
2019-01-10 14:18:11 +00:00
|
|
|
memset(obuf, 0, min_len + 1);
|
2014-01-15 08:43:20 +00:00
|
|
|
for(i = 0; i < min_len; i++) {
|
2019-03-23 02:17:07 +00:00
|
|
|
if (getcharacter(pfile, &c) < 0)
|
2014-01-15 08:43:20 +00:00
|
|
|
goto _exit1;
|
2019-01-10 14:18:11 +00:00
|
|
|
if (PRINTABLE(c)) {
|
|
|
|
obuf[i] = c;
|
|
|
|
obuf[i + 1] = 0;
|
|
|
|
cur_off += encoding_size;
|
|
|
|
} else {
|
2014-01-15 08:43:20 +00:00
|
|
|
if (encoding == ENCODING_8BIT &&
|
|
|
|
(uint8_t)c > 127) {
|
2019-01-10 14:18:11 +00:00
|
|
|
obuf[i] = c;
|
|
|
|
obuf[i + 1] = 0;
|
|
|
|
cur_off += encoding_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cur_off += encoding_size;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= min_len && ((cur_off <= offset + size) ||
|
|
|
|
!(offset + size))) {
|
|
|
|
if (show_filename)
|
2019-01-10 14:18:11 +00:00
|
|
|
printf("%s: ", name);
|
2014-01-15 08:43:20 +00:00
|
|
|
if (show_loc) {
|
2019-01-10 14:18:11 +00:00
|
|
|
switch (radix) {
|
2014-01-15 08:43:20 +00:00
|
|
|
case RADIX_DECIMAL:
|
2019-01-10 14:18:11 +00:00
|
|
|
printf("%7ju ", (uintmax_t)start_off);
|
2014-01-15 08:43:20 +00:00
|
|
|
break;
|
|
|
|
case RADIX_HEX:
|
2019-01-10 14:18:11 +00:00
|
|
|
printf("%7jx ", (uintmax_t)start_off);
|
2014-01-15 08:43:20 +00:00
|
|
|
break;
|
|
|
|
case RADIX_OCTAL:
|
2019-01-10 14:18:11 +00:00
|
|
|
printf("%7jo ", (uintmax_t)start_off);
|
2014-01-15 08:43:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%s", obuf);
|
|
|
|
|
2019-01-10 14:18:11 +00:00
|
|
|
for (;;) {
|
2014-01-15 08:43:20 +00:00
|
|
|
if ((offset + size) &&
|
|
|
|
(cur_off >= offset + size))
|
|
|
|
break;
|
2019-03-23 02:17:07 +00:00
|
|
|
if (getcharacter(pfile, &c) < 0)
|
2019-03-21 06:31:14 +00:00
|
|
|
break;
|
2019-03-23 02:17:07 +00:00
|
|
|
cur_off += encoding_size;
|
2014-01-15 08:43:20 +00:00
|
|
|
if (encoding == ENCODING_8BIT &&
|
|
|
|
(uint8_t)c > 127) {
|
2019-01-10 14:18:11 +00:00
|
|
|
putchar(c);
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-23 02:17:07 +00:00
|
|
|
if (!PRINTABLE(c))
|
|
|
|
break;
|
2014-01-15 08:43:20 +00:00
|
|
|
putchar(c);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_exit1:
|
|
|
|
free(obuf);
|
2017-04-17 23:14:26 +00:00
|
|
|
return (0);
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define USAGE_MESSAGE "\
|
|
|
|
Usage: %s [options] [file...]\n\
|
|
|
|
Print contiguous sequences of printable characters.\n\n\
|
|
|
|
Options:\n\
|
|
|
|
-a | --all Scan the entire file for strings.\n\
|
|
|
|
-e ENC | --encoding=ENC Select the character encoding to use.\n\
|
|
|
|
-f | --print-file-name Print the file name before each string.\n\
|
|
|
|
-h | --help Print a help message and exit.\n\
|
|
|
|
-n N | --bytes=N | -N Print sequences with 'N' or more characters.\n\
|
|
|
|
-o Print offsets in octal.\n\
|
|
|
|
-t R | --radix=R Print offsets using the radix named by 'R'.\n\
|
|
|
|
-v | --version Print a version identifier and exit.\n"
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
2019-01-10 14:18:11 +00:00
|
|
|
|
|
|
|
fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
|
2014-01-15 08:43:20 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
show_version(void)
|
|
|
|
{
|
2019-01-10 14:18:11 +00:00
|
|
|
|
|
|
|
printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
|
2014-01-15 08:43:20 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|