CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
string matching help !

 



mungo
New User

Apr 1, 2010, 3:39 AM

Post #1 of 7 (732 views)
string matching help ! Can't Post

here is a piece of fuzzing code, that I have acquired from a site . I need to adapt the code to work on my protocol. But the problem is that I am new to perl and I don't understand the working of few lines as given below :

$fuzz =~ s/<FUZZER>/$brute/g;

($counter) = $fuzz =~ m/<COUNT>([^\]]*)<COUNT>/g;

$fuzz =~ s/<SIZE>/length($counter)/egi;

$fuzz =~ s/<COUNT>//egi;

$log =~ s/(.|\n)/sprintf("%02lx", ord $1)/eg;


I have tried googling different operators but couldn't make sense of the code above. Can someone comment these lines and help a newbie ?


Nila
User


Apr 1, 2010, 4:27 AM

Post #2 of 7 (727 views)
Re: [mungo] string matching help ! [In reply to] Can't Post

 

Code
 
# It takes the input from $fuzz. Substitute the "<FUZZER>
# to "$brute" value. 'g' modifier used to global matching.

$fuzz =~ s/<FUZZER>/$brute/g;

# 'm' used to pattern matching not substitution.It matches
# the <COUNT> followed by other than '\' then <COUNT>.
# If it matched the input string will be strong in to $counter.
# If the $counter is scalar the return status will be stored
# in to the $counter variable.

($counter) = $fuzz =~ m/<COUNT>([^\]]*)<COUNT>/g;

# Here also same substitution. 'e' quantifier used to evaluate
# the expression inside the regular expression. 'i' quantifier
# used match the 'ignore' case.

$fuzz =~ s/<SIZE>/length($counter)/egi;

# It also same as above. But it substitute the "<COUNT>"
# to "nothing".
$fuzz =~ s/<COUNT>//egi;

# It matches any character or newline from the string then
# substitute with the sprintf function() value. ord() used to
# convert the first character of the expression to numeric
# value(ASCII value).
$log =~ s/(.|\n)/sprintf("%02lx", ord $1)/eg;


For better understanding go through the following link,
-- http://www.cs.tut.fi/~jkorpela/perl/regexp.html
-- http://www.sdsc.edu/~moreland/courses/IntroPerl/docs/manual/pod/perlre.html


(This post was edited by Nila on Apr 1, 2010, 4:30 AM)


mungo
New User

Apr 1, 2010, 12:46 PM

Post #3 of 7 (715 views)
Re: [Nila] string matching help ! [In reply to] Can't Post

Thanks Nila ! that was a great help indeed. I have got another problem regarding filing in Perl. I'm simply reading a hex file (containing hex stream) and printing it to another file. But the problem I face doing so is that , I get an extra ascii character (0x0a = Line feed) during the process... how can I do away with it ?


7stud
Enthusiast

Apr 1, 2010, 1:28 PM

Post #4 of 7 (713 views)
Re: [mungo] string matching help ! [In reply to] Can't Post

Are you trying to transfer exactly what's in one file to the other file? If so, see perldoc -f binmode.


If you actually want to remove a character from the input, then see the substitution operator, s//, in perlop in the section titled: Regexp Quote Like Operators.


(This post was edited by 7stud on Apr 1, 2010, 1:29 PM)


mungo
New User

Apr 2, 2010, 12:39 AM

Post #5 of 7 (699 views)
Re: [7stud] string matching help ! [In reply to] Can't Post

I've tried debugging and have cut down to the problematic line :

$template_fuzz =~ s/\\x(..)/pack("C",hex($1))/egi;

here $template_fuzz is the data read from a file, with many hex values(\x41\x1b\x21...\x1f etc.) in it. When I put a print $template_fuzz before the aforementioned line, I get exactly what it was supposed to read from a file. But after this command executes, and I do a print ascii_to_hex $template_fuzz, I see a line feed (0a) at the end of the hex string... Any comments ?


7stud
Enthusiast

Apr 2, 2010, 10:50 AM

Post #6 of 7 (689 views)
Re: [mungo] string matching help ! [In reply to] Can't Post

Yes. perl does automatic newline conversions on data read from a file, and how the conversion is done depends on what operating system the perl program is running on. If you are on windows, which uses \r\n for newlines, then perl looks for \r\n in the file to signal the end of a line. Then perl converts the actual newline found in the file to \n. Therefore a line in the file that looked like this:

hello world\r\n

is given to your program like this:

hello world\n

On the other hand, if you read that same file on unix, which uses \n for newlines, then perl looks for a \n in the file to signal the end of the line. And, most importantly, perl does no conversion on the \n. Therefore, a line that looks like this in your file:

hello world\r\n

is given to your program like this:

hello world\r\n

Note that the when perl reads the file on unix, the \r is not a signal for anything--it is just another character. Then when perl sees the \n, that is the signal that the line has ended.

Also, by default chomp() returns an OS dependent result. On unix, chomp() removes only a \n. On windows, chomp() removes \r\n.

Therefore, if your file was created on windows, it will have a \r\n at the end of every line. And if your program is running on unix, then reading a line from the windows file and then calling chomp() on the line will leave a \r at the end of every line.

perl's global variable $/, the input record separator, is actually what perl looks for when reading files line by line and chomp()'ing lines. You can change that to whatever you want.

Calling binmode() on a filehandle turns off perl's newline conversion feature.

perl's newline conversion feature is on when both reading and writing. So if you write a \n to a file, and you are on windows, perl will convert that to \r\n before writing the newline to the file.

And you didn't answer this question:


Quote
Are you trying to transfer exactly what's in one file to the other file?


Without knowing that, we can't know what your goal is. Sometimes instead of saying,

"This is what I am trying to do in my code..."

it's better to say,

"This is my goal..."

and then we might be able to offer better ways to achieve your goal.


(This post was edited by 7stud on Apr 2, 2010, 11:01 AM)


mungo
New User

Apr 2, 2010, 11:55 AM

Post #7 of 7 (677 views)
Re: [7stud] string matching help ! [In reply to] Can't Post

Thanks 7stud ! chomp was what I really needed...
And I appologise for missing the answer to whether I wanted to copy the entire file exactly or not. Regarding that, I wasn't copying the exact file but was reading from one file, modifying the text read (i.e removing "\x" perpended to each hex) and then printing the remaining text (in hexadecimal form ) to another file.

Anyways thanks a lot ! :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives