#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "xregex.h"
#include "jargrep.h"
#include "jartool.h"
#include "pushback.h"
#include "zipfile.h"
#include "zlib.h"
#include "compress.h"
#include <getopt.h>
void version(void);
void help(const char *name);
#define Usage "Usage: %s [-bcinsVw] [--version|--help] <-e PATTERN | PATTERN> FILE ...\n"
static int opt_valid(int options) {
int retflag;
if((options & JG_PRINT_COUNT) &&
(options & (JG_PRINT_BYTEOFFSET | JG_PRINT_LINE_NUMBER)))
{
retflag = FALSE;
}
else retflag = TRUE;
return retflag;
}
static regex_t *create_regexp(const char *regstr, int options) {
regex_t *exp;
int errcode;
int msgsize;
char *errmsg;
if((exp = (regex_t *) malloc(sizeof(regex_t))))
{
if((errcode = regcomp(exp, regstr, (options & JG_IGNORE_CASE) ? REG_ICASE : 0))) {
fprintf(stderr, "regcomp of regex failed,\n");
if((errmsg = (char *) malloc(msgsize = regerror(errcode, exp, NULL, 0) + 1))) {
regerror(errcode, exp, errmsg, msgsize);
fprintf(stderr, "Error: %s\n", errmsg);
free(exp);
free(errmsg);
exit(1);
}
else {
fprintf(stderr, "Malloc of errmsg failed.\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
free(exp);
exit(1);
}
}
}
else {
fprintf(stderr, "Malloc of regex failed,\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
exit(1);
}
return exp;
}
static int check_sig(ub1 *scratch, pb_file *pbfp) {
ub4 signature;
int retflag = 0;
signature = UNPACK_UB4(scratch, 0);
#ifdef DEBUG
printf("signature is %x\n", signature);
#endif
if(signature == 0x08074b50){
#ifdef DEBUG
printf("skipping data descriptor\n");
#endif
pb_read(pbfp, scratch, 12);
retflag = 2;
} else if(signature == 0x02014b50){
#ifdef DEBUG
printf("Central header reached.. we're all done!\n");
#endif
retflag = 1;
}else if(signature != 0x04034b50){
printf("Ick! %#x\n", signature);
retflag = 1;
}
return retflag;
}
static void decd_siz(ub4 *csize, ub4 *usize, ub2 *fnlen, ub2 *eflen, ub2 *flags, ub2 *method, ub1 *file_header) {
*csize = UNPACK_UB4(file_header, LOC_CSIZE);
#ifdef DEBUG
printf("Compressed size is %u\n", *csize);
#endif
*usize = UNPACK_UB4(file_header, LOC_USIZE);
#ifdef DEBUG
printf("Uncompressed size is %u\n", *usize);
#endif
*fnlen = UNPACK_UB2(file_header, LOC_FNLEN);
#ifdef DEBUG
printf("Filename length is %hu\n", *fnlen);
#endif
*eflen = UNPACK_UB2(file_header, LOC_EFLEN);
#ifdef DEBUG
printf("Extra field length is %hu\n", *eflen);
#endif
*flags = UNPACK_UB2(file_header, LOC_EXTRA);
#ifdef DEBUG
printf("Flags are %#hx\n", *flags);
#endif
*method = UNPACK_UB2(file_header, LOC_COMP);
#ifdef DEBUG
printf("Compression method is %#hx\n", *method);
#endif
}
static char *new_filename(pb_file *pbf, ub4 len) {
char *filename;
if(!(filename = (char *) malloc(len + 1))) {
fprintf(stderr, "Malloc failed of filename\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
}
pb_read(pbf, filename, len);
filename[len] = '\0';
#ifdef DEBUG
printf("filename is %s\n", filename);
#endif
return filename;
}
static char *read_string(pb_file *pbf, int size) {
char *page;
if((page = (char *) malloc(size + 1))) {
pb_read(pbf, page, size);
page[size] = '\0';
}
else {
fprintf(stderr, "Malloc of page buffer failed.\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
exit(1);
}
return page;
}
static char *extract_line(const char *stream, regoff_t begin, regoff_t end, int *b) {
int e;
int length;
char *retstr;
for(*b = begin; *b >= 0 && !iscntrl((unsigned char)stream[*b]); (*b)--);
(*b)++;
for(e = end; stream[e] == '\t' || !iscntrl((unsigned char)stream[e]); e++);
length = e - *b;
if((retstr = (char *) malloc(length + 1))) {
sprintf(retstr, "%d:", *b);
strncpy(retstr, &(stream[*b]), length);
retstr[length] = '\0';
}
else {
fprintf(stderr, "Malloc failed of output string.\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
exit(1);
}
return retstr;
}
static int chk_wrd(regex_t *exp, const char *str) {
int wrd_fnd = FALSE;
int regflag;
int frnt_ok;
int bck_ok;
const char *str2;
regmatch_t match;
str2 = str;
frnt_ok = bck_ok = FALSE;
while(!wrd_fnd && !(regflag = regexec(exp, str2, 1, &match, 0))) {
if(!match.rm_so && (str2 == str)) frnt_ok = TRUE;
else if(!isalnum((unsigned char)str2[match.rm_so - 1])
&& str2[match.rm_so - 1] != '_')
frnt_ok = TRUE;
else frnt_ok = FALSE;
if(frnt_ok) {
if(str2[match.rm_eo] == '\0') bck_ok = TRUE;
else if(!isalnum((unsigned char)str2[match.rm_eo])
&& str2[match.rm_eo] != '_')
bck_ok = TRUE;
else bck_ok = FALSE;
}
wrd_fnd = frnt_ok && bck_ok;
str2 = &(str2[match.rm_eo]);
}
return wrd_fnd;
}
static void prnt_mtchs(regex_t *exp, const char *filename, const char *stream, regmatch_t *pmatch, regmatch_t *nl_offset, int num, int lines, int options) {
int i;
int j = 0;
int ln_cnt;
int begin;
int o_begin;
char *str;
o_begin = -1;
ln_cnt = 0;
for(i = 0; i < num; i++) {
str = extract_line(stream, pmatch[i].rm_so, pmatch[i].rm_eo, &begin);
if(begin > o_begin) {
if(!(options & JG_WORD_EXPRESSIONS) || chk_wrd(exp, str)) {
ln_cnt++;
if(!(options & JG_PRINT_COUNT)) {
printf("%s:", filename);
if(options & JG_PRINT_LINE_NUMBER) {
for(; j < lines && nl_offset[j].rm_so < begin; j++);
printf("%d:", j + 1);
}
if(options & JG_PRINT_BYTEOFFSET) printf("%d:", begin);
printf("%s\n", str);
}
}
}
o_begin = begin;
free(str);
}
if(options & JG_PRINT_COUNT) printf("%s:%d\n", filename, ln_cnt);
}
static void check_crc(pb_file *pbf, const char *stream, ub4 usize) {
ub4 crc=0;
ub4 lcrc;
ub1 scratch[16];
crc = crc32(crc, NULL, 0);
crc = crc32(crc, (const unsigned char *)stream, usize);
if(pb_read(pbf, scratch, 16) != 16) {
perror("read");
exit(1);
}
if(UNPACK_UB4(scratch, 0) != 0x08074b50) {
fprintf(stderr, "Error! Missing data descriptor!\n");
exit(1);
}
lcrc = UNPACK_UB4(scratch, 4);
if(crc != lcrc){
fprintf(stderr, "Error! CRCs do not match! Got %x, expected %x\n",
crc, lcrc);
exit(1);
}
}
static void mk_ascii(char *stream, size_t usize) {
size_t i;
for(i = 0; i < usize; i++)
if(stream[i] != '\t'
&& (iscntrl((unsigned char)stream[i])
|| (unsigned char) stream[i] >= 128))
stream[i] = '\n';
}
static regmatch_t *fnd_match(regex_t *exp, const char *str_stream, int *i) {
int regflag;
regmatch_t match;
regmatch_t *match_array;
regmatch_t *tmp;
match_array = NULL;
for(*i = 0, regflag = regexec(exp, str_stream, 1, &match, 0); !regflag;
regflag = regexec(exp, &(str_stream[match.rm_eo]), 1, &match, 0), (*i)++)
{
if((tmp = (regmatch_t *)
realloc(match_array, sizeof(regmatch_t) * ((*i) + 1))))
{
match_array = tmp;
if(*i) {
match.rm_so += match_array[(*i) - 1].rm_eo;
match.rm_eo += match_array[(*i) - 1].rm_eo;
}
match_array[*i] = match;
}
else {
fprintf(stderr, "Realloc of match_array failed.\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
exit(1);
}
}
return match_array;
}
static int cont_grep(regex_t *exp, regex_t *nl_exp, int fd, pb_file *pbf, int options) {
int retflag = TRUE;
int i;
int j;
ub4 csize;
ub4 usize;
ub2 fnlen;
ub2 eflen;
ub2 flags;
ub2 method;
ub1 file_header[30];
char *filename;
char *str_stream;
regmatch_t *match_array;
regmatch_t *nl_offsets=0;
if(pb_read(pbf, (file_header + 4), 26) != 26) {
perror("read");
retflag = FALSE;
}
else {
decd_siz(&csize, &usize, &fnlen, &eflen, &flags, &method, file_header);
filename = new_filename(pbf, fnlen);
lseek(fd, eflen, SEEK_CUR);
if(filename[fnlen - 1] != '/') {
str_stream = (method == 8 || (flags & 0x0008)) ?
(char *) inflate_string(pbf, &csize, &usize) :
read_string(pbf, csize);
if(flags & 0x008) check_crc(pbf, str_stream, usize);
mk_ascii(str_stream, usize);
match_array = fnd_match(exp, str_stream, &i);
if((options & JG_PRINT_LINE_NUMBER) && i)
nl_offsets = fnd_match(nl_exp, str_stream, &j);
prnt_mtchs(exp, filename, str_stream, match_array, nl_offsets, i, j, options);
if(match_array) free(match_array);
free(str_stream);
}
free(filename);
retflag = TRUE;
}
return retflag;
}
static void jargrep(regex_t *exp, regex_t *nl_exp, const char *jarfile, int options){
int fd;
int floop = TRUE;
pb_file pbf;
ub1 scratch[16];
if((fd = open(jarfile, O_RDONLY)) == -1) {
if(!(options & JG_SUPRESS_ERROR))
fprintf(stderr, "Error reading file '%s': %s\n", jarfile, strerror(errno));
}
else {
pb_init(&pbf, fd);
do {
if(pb_read(&pbf, scratch, 4) != 4) {
perror("read");
floop = FALSE;
}
else {
switch (check_sig(scratch, &pbf)) {
case 0:
floop = cont_grep(exp, nl_exp, fd, &pbf, options);
break;
case 1:
floop = FALSE;
break;
case 2:
;
}
}
} while(floop);
}
}
#define LONG_OPT(Num) ((Num) + 128)
#define OPT_HELP LONG_OPT (0)
static const struct option option_vec[] =
{
{ "help", no_argument, NULL, OPT_HELP },
{ "version", no_argument, NULL, 'V' },
{ NULL, no_argument, NULL, 0 }
};
int main(int argc, char **argv) {
int c;
int retval = 0;
int fileindex;
int options = 0;
regex_t *regexp;
regex_t *nl_exp = NULL;
char *regexpstr = NULL;
while((c = getopt_long(argc, argv, "bce:insVw",
option_vec, NULL)) != -1) {
switch(c) {
case 'b':
options |= JG_PRINT_BYTEOFFSET;
break;
case 'c':
options |= JG_PRINT_COUNT;
break;
case 'e':
if(!(regexpstr = (char *) malloc(strlen(optarg) + 1))) {
fprintf(stderr, "Malloc failure.\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
exit(1);
}
strcpy(regexpstr, optarg);
break;
case 'i':
options |= JG_IGNORE_CASE;
break;
case 'n':
options |= JG_PRINT_LINE_NUMBER;
break;
case 's':
options |= JG_SUPRESS_ERROR;
break;
case 'v':
options |= JG_INVERT;
break;
case 'V':
version ();
break;
case 'w':
options |= JG_WORD_EXPRESSIONS;
break;
case OPT_HELP:
help(argv[0]);
break;
default:
fprintf(stderr, Usage, argv[0]);
exit(1);
}
}
if(!regexpstr){
if(((argc - optind) >= 2)) {
regexpstr = argv[optind];
fileindex = optind + 1;
}
else {
fprintf(stderr, "Invalid arguments.\n");
fprintf(stderr, Usage, argv[0]);
exit(1);
}
}
else if((argc - optind) == 1) {
fileindex = optind;
}
else {
fprintf(stderr, "Invalid arguments.\n");
fprintf(stderr, Usage, argv[0]);
exit(1);
}
if(opt_valid(options)) {
regexp = create_regexp(regexpstr, options);
if(options & JG_PRINT_LINE_NUMBER) nl_exp = create_regexp("\n", 0);
init_inflation();
for(; fileindex < argc; fileindex++)
jargrep(regexp, nl_exp, argv[fileindex], options);
regfree(regexp);
if(options & JG_PRINT_LINE_NUMBER) regfree(nl_exp);
}
else {
retval = 1;
fprintf(stderr, "Error: Invalid combination of options.\n");
}
return retval;
}
void help(const char *filename)
{
printf (Usage, filename);
printf ("\
\n\
Search files in a jar file for a pattern.\n\
\n\
-b print byte offset of match\n\
-c print number of matches\n\
-i compare case-insensitively\n\
-n print line number of each match\n\
-s suppress error messages\n\
-w force PATTERN to match only whole words\n\
-e PATTERN use PATTERN as regular expression\n\
-V|--version print version number and exit\n\
--help print help\n\
");
exit (0);
}
void version ()
{
printf("grepjar (%s) %s\n\n", PACKAGE, VERSION);
printf("Copyright 1999, 2000, 2001 Bryan Burns\n");
printf("Copyright 2000 Cory Hollingsworth\n");
printf("Copyright 2002 Free Software Foundation\n");
printf("\
This is free software; see the source for copying conditions. There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
exit (0);
}