Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The previous version has supported only files upto 4GiB. When working wi... #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions bgrep.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright 2009 Felix Domke <[email protected]>. All rights reserved.
// Copyright 2014 Jirka Hladky <[email protected]>.
// Added support for large files (previously 4GB, now 2^64 bytes, 16EiB Exbibytes)
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
Expand All @@ -24,6 +26,9 @@
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of the copyright holder.
//
/*
gcc -Wall -Wextra -O2 -o bgrep bgrep.c
*/

#include <stdio.h>
#include <stdlib.h>
Expand All @@ -33,8 +38,9 @@
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <inttypes.h>

#define BGREP_VERSION "0.2"
#define BGREP_VERSION "0.3"

int ascii2hex(char c)
{
Expand All @@ -56,7 +62,7 @@ int ascii2hex(char c)

void searchfile(const char *filename, int fd, const unsigned char *value, const unsigned char *mask, int len)
{
off_t offset = 0;
uint64_t offset = 0;
unsigned char buf[1024];

len--;
Expand All @@ -75,15 +81,16 @@ void searchfile(const char *filename, int fd, const unsigned char *value, const
} else if (!r)
return;

int o, i;
for (o = offset ? 0 : len; o < r; ++o)
int i;
uint64_t o;
for (o = offset ? 0 : len; o < (unsigned) r; ++o)
{
for (i = 0; i <= len; ++i)
if ((buf[o + i] & mask[i]) != value[i])
break;
if (i > len)
{
printf("%s: %08llx\n", filename, (unsigned long long)(offset + o - len));
printf("%s: %016" PRIx64 "\n", filename, (uint64_t)(offset + o - len));
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/bgrep-random.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_bgrep(datalen, searchlen):

bgrep_res = subprocess.Popen([BGREP, search.encode('hex'), filename], stdout=subprocess.PIPE).communicate()[0]

expected_res = ''.join(["%s: %08x\n" % (filename, i) for i in results])
expected_res = ''.join(["%s: %016x\n" % (filename, i) for i in results])

if bgrep_res != expected_res:
print "search: %s" % search.encode('hex')
Expand Down
4 changes: 2 additions & 2 deletions test/bgrep-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if [ $PREPARE -eq 1 ]; then
for index in `seq 0 1025`;do
fileIndex=`printf "%04i" ${index}`
perl -e 'print "A"x'"${index}"';print "BBBC";print "A"x20;' > /tmp/bgrepTest${fileIndex}.txt
printf "%08x\n" ${index} >> /tmp/bgrepExpectedResult.txt
printf "%016x\n" ${index} >> /tmp/bgrepExpectedResult.txt
done

#
Expand All @@ -28,7 +28,7 @@ if [ $PREPARE -eq 1 ]; then
#
for index in `seq 1020 1025`;do
perl -e 'print "A"x'"${index}"';print "BBB"' > /tmp/bgrepTestSpecial${index}.txt
printf "%08x\n" ${index} >> /tmp/bgrepExpectedResult.txt
printf "%016x\n" ${index} >> /tmp/bgrepExpectedResult.txt
done

fi
Expand Down