Determining the integeral value of a data.

Q&A's, tips, howto's
Locked
statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Determining the integeral value of a data.

Post by statik »

I have an unsigned integer that is four bytes long that is sent to me via a network connection. When the server sends the integer, newlisp sees it as various characters ()&!@#*)}{":>?^`á–ó<Š¤,Ù¸í.!ƒ/,á–ó< so on and so forth.

What I need to do is take those four bytes and 'convert' them into an integer value.

Where do I start when doing something like this? I've looked at (format) and played with it a little, but was unable to get it to determine an unsigned int value.

I am at a loss :-(

If you're interested in what I'm doing, you can try the challenge found here: http://www.pulltheplug.org/wargames/vortex/level00.html

Thanks for the help guys.
-statik

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post by nigelbrown »

Try unpack (see manual)

Note at site says "little endian"
but just says "4 unsigned integers" so I don't know what they think the bytes per integer is. 4 bytes per integer would be 32 bit. 4 bytes for 4 integers would be 8 bit.

Nigel

statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Post by statik »

Thank you sir.

Yes, I'm almost positive that I'm looking at four 32 bit integers :-) I'm pretty surprised I haven't seen pack/unpack used before. I have no idea how I've missed it for so long.

Once again, thanks.
-statik

statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Post by statik »

Code: Select all

#!/usr/bin/newlisp
 
(context 'MAIN)
 
(setq host "69.55.233.82")
(setq port 5842)
 
(setq socket (net-connect host port))
 
(if socket
        (begin
                (println "Connected successfully!")
                (print "Receiving data: ")
                (setq final "")
                (while (and (net-select socket "r" 1000000) (> (net-peek socket) 0))
                        (net-receive socket 'buffer 4)
                        (push ((unpack "<lu" buffer) 0) pool -1)
                        (print buffer)
                )
                (println)
                (net-send socket (string (apply + pool)))
                (while (and (net-select socket "r" 1000000) (> (net-peek socket) 0))
                        (net-receive socket 'buffer 1024)
                        (println buffer)
                )
        )
)
 
(net-close socket)
(exit)
So there's my attempt at the challenge. If I understand everything correctly, my structure/logic (broad) is correct.

The above code should be doing the following:

1- Connecting to remote host
2- Reading four bytes (which should be our first unsigned integer) into a buffer
3- Unpacking that four byte buffer
4- Throwing the results of the unpack into a list
5- Repeat steps 2-4 until four integers have been collected
6- Calculating a sum of all the elements of our newly created list
7- Returning our sum to the remote host
8- Capturing results (did we fail or not?)

Now, my code is flawed somewhere in it's unpacking/calculating sum stages. I cannot figure out what I am doing wrong, but it appears that both (map) and (unpack) are being used correctly.

If the server is telling me that I don't have the right sum, then where am I going wrong?
-statik

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Have not analyzed your code much, but could crack the thing with following code:

Code: Select all

(set 'sock (net-connect "vortex.labs.pulltheplug.org" 5842))
(set 'sum 0)
(dotimes (i 4)
        (net-receive sock 'buff 4)
        (set 'val ((unpack "<lu" buff) 0))
        (println "received:" val)
        (push val pool)
)

(set 'sum (apply + pool))

(println "sending:" sum)

(set 'msg (pack "<lu" sum))

(net-send sock msg 4)

(net-receive sock 'info 1024)

(println info)
as a result you get:

Code: Select all

received:574136882
received:1177762125
received:939103820
received:630841909
sending:-973122560
Username: level1 Password: Gq#qu3bF3
Lutz

ps: note that newLISP's signed integer addition overflows the 2billion 10^31, but it doesn't matter when converstin "<lu" everything will be fine.

statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Post by statik »

What an embarassment haha. Well I think I tracked down the flaw/bug...

Code: Select all

(set 'msg (pack "<lu" sum)) 
I forgot to repack the data before sending it back. That would do it.

Thanks you guys for the help.
-statik

Locked