Determining the integeral value of a data.

Started by statik, January 28, 2006, 04:37:52 PM

Previous topic - Next topic

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">http://www.pulltheplug.org/wargames/vortex/level00.html



Thanks for the help guys.
-statik

nigelbrown

#1
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

#2
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

#3

#!/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

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



(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:



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

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



(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