Mailing List Archive

Converting a string to an int that's greater than 2^31-1
Hello All,

I'm tweaking my first plugin that I'm about ready to spam the list
with, but I'm having a situation where I'm given a string that
represents a long int. I.e., the decimal value is greater than
2147483647. How can I convert that string into an int without
losing any information?

Thanks!

Jon




__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
Re: Converting a string to an int that's greater than 2^31-1 [ In reply to ]
On Thu, Oct 06, 2005 at 09:32:06AM -0700, Jon Passki wrote:

> I'm tweaking my first plugin that I'm about ready to spam the list
> with, but I'm having a situation where I'm given a string that
> represents a long int. I.e., the decimal value is greater than
> 2147483647. How can I convert that string into an int without
> losing any information?

I don't think you can - NASL doesn't support long ints. Can you write
your code so it looks at the value as composed of multiple parts?

George
--
theall@tenablesecurity.com
_______________________________________________
Plugins-writers mailing list
Plugins-writers@list.nessus.org
http://mail.nessus.org/mailman/listinfo/plugins-writers
Re: Converting a string to an int that's greater than 2^31-1 [ In reply to ]
--- "George A. Theall" <theall@tenablesecurity.com> wrote:

> On Thu, Oct 06, 2005 at 09:32:06AM -0700, Jon Passki wrote:
>
> > I'm tweaking my first plugin that I'm about ready to spam the
> list
> > with, but I'm having a situation where I'm given a string that
> > represents a long int. I.e., the decimal value is greater than
> > 2147483647. How can I convert that string into an int without
> > losing any information?
>
> I don't think you can - NASL doesn't support long ints. Can you
> write
> your code so it looks at the value as composed of multiple parts?

Yeah, if I can break it down ;-) Cyclical answer, I know. The
string is an IP address, basically, in quad-octet form. The moment
any numeric routines are applied, it's converted to an int, rounded
to 2^31.

Jon






__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
_______________________________________________
Plugins-writers mailing list
Plugins-writers@list.nessus.org
http://mail.nessus.org/mailman/listinfo/plugins-writers
Re: Converting a string to an int that's greater than 2^31-1 [ In reply to ]
On Oct 6, 2005, at 19:46, Jon Passki wrote:
>
> Yeah, if I can break it down ;-) Cyclical answer, I know. The
> string is an IP address, basically, in quad-octet form. The moment
> any numeric routines are applied, it's converted to an int, rounded
> to 2^31.

No, it's converted to a signed int, so if you just perform bit-
shifting operations then things are working.

What do you want to do exactly ?


-- Renaud
_______________________________________________
Plugins-writers mailing list
Plugins-writers@list.nessus.org
http://mail.nessus.org/mailman/listinfo/plugins-writers
Re: Converting a string to an int that's greater than 2^31-1 [ In reply to ]
--- Renaud Deraison <deraison@nessus.org> wrote:

>
> On Oct 6, 2005, at 19:46, Jon Passki wrote:
> >
> > Yeah, if I can break it down ;-) Cyclical answer, I know. The
> > string is an IP address, basically, in quad-octet form. The
> moment
> > any numeric routines are applied, it's converted to an int,
> rounded
> > to 2^31.
>
> No, it's converted to a signed int, so if you just perform bit-
> shifting operations then things are working.
>
> What do you want to do exactly ?

I disagree (but would be happy to eat my words). If it was
converted to just a signed int, I should have a pretty big negative
value, since the sign bit is set to 1, with the remaining values,
two off IIRC. That is true if two defined ints less than 2^31-1
are added together, since they overflow. But the string to int
conversion leaves me with exactly 2^31 - 1.

2172748161 is 129.129.129.129 in quad-octet decimal format (or
0x81818181), which is the end goal. Converting from an int to quad
is easy (got the code already, just bitwise shifts by >> 8 & 255 as
you mentioned already).

Doing something w/ the string doesn't give the desired result:

stuff = "2172748161";
display ( stuff + "\n");
poo[0] = stuff & 255;
display ( poo[0] + "\n");

outputs:
2172748161
255

it should have outputed...
2172748161
129 [.or whatever the negative value & w/ 255 would be, which I
think is 2 off]

Did I miss something? Should I be using some other type of string
function? My input is an HTTP string, not a straight int. I think
this is the issue because I'm converting from an ASCII string to an
int. I think the conversion truncates everything down to 2^31-1.

Jon




__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
_______________________________________________
Plugins-writers mailing list
Plugins-writers@list.nessus.org
http://mail.nessus.org/mailman/listinfo/plugins-writers
Re: Converting a string to an int that's greater than 2^31-1 [ In reply to ]
--- "George A. Theall" <theall@tenablesecurity.com> wrote:

> On Thu, Oct 06, 2005 at 09:32:06AM -0700, Jon Passki wrote:
>
> > I'm tweaking my first plugin that I'm about ready to spam the
> list
> > with, but I'm having a situation where I'm given a string that
> > represents a long int. I.e., the decimal value is greater than
> > 2147483647. How can I convert that string into an int without
> > losing any information?
>
> I don't think you can - NASL doesn't support long ints. Can you
> write
> your code so it looks at the value as composed of multiple parts?

# Hack to convert a string that represents a value greater than
2^31 -1
# Look away!
if (strlen(pieces[1]) > 9) {
bottom = int( substr(pieces[1],5) );
top = int( substr(pieces[1],0,4) );
pieces[1] = top * 10**5 + bottom;
}


That's my hack for now. So, any number > 999999999 will be
converted. This unnecessarily converts strings representing
numbers < 2^31 - 1, but whatever :) It's straight-forward and
seems to work in my testing :D

Jon




__________________________________
Start your day with Yahoo! - Make it your home page!
http://www.yahoo.com/r/hs
_______________________________________________
Plugins-writers mailing list
Plugins-writers@list.nessus.org
http://mail.nessus.org/mailman/listinfo/plugins-writers