Help with building a string

I

Ian Gibbons

Firstly what type is %x as I've not encountered it before?
Now the problem: I'm trying to alter a host masking system for ircd so
that it masks all but the isp name and location (if .uk.us etc..).
However the crc32 stuff doesnt return as a string, and because the
number of hostname fields is not static I need to build a string which
contains each hostname field hashed and joined together.

if (parc > 4) /* There are isp's like *.isp.co.uk etc out
there */
{
for (i=2; i < (parc - 2); i++)
{
hash = ((crc32(parv, strlen(parv)) + KEY2) ^ KEY1) ^
KEY3;
hash <<= 2;
hash >>= 2;
/* what goes here to make buf complete? */
}
sprintf(mask, "%s%s.%s.%s", buf, parv[parc - 3], parv[parc - 2],
parv[parc - 1]);
}

hash[0] and [1] are always the first two hostname fields, and in other
conditions are sprintf'ed into mask as %x.
Ultimately what im aiming is to have mask.mask.mydomain.co.uk,
mask.mask.mask.otherdomain.co.uk, mask.mask.mask.domain3.com etc..

Hope this makes sense :)

Kind Regards,
Ian
 
J

Jack Klein

Firstly what type is %x as I've not encountered it before?
Now the problem: I'm trying to alter a host masking system for ircd so
that it masks all but the isp name and location (if .uk.us etc..).
However the crc32 stuff doesnt return as a string, and because the
number of hostname fields is not static I need to build a string which
contains each hostname field hashed and joined together.

if (parc > 4) /* There are isp's like *.isp.co.uk etc out
there */

What is parc?
{
for (i=2; i < (parc - 2); i++)
{
hash = ((crc32(parv, strlen(parv)) + KEY2) ^ KEY1) ^
KEY3;


What is hash? What is parv? What does crc32() return?
hash <<= 2;
hash >>= 2;
/* what goes here to make buf complete? */
}
sprintf(mask, "%s%s.%s.%s", buf, parv[parc - 3], parv[parc - 2],
parv[parc - 1]);
}

hash[0] and [1] are always the first two hostname fields, and in other
conditions are sprintf'ed into mask as %x.
Ultimately what im aiming is to have mask.mask.mydomain.co.uk,
mask.mask.mask.otherdomain.co.uk, mask.mask.mask.domain3.com etc..

Hope this makes sense :)

Kind Regards,
Ian


Your posted sample code containing quite a few variables and one
function without explaining what they are.

As for the question in your first sentence, %x and %X are conversion
specifier for the printf() family of functions that can be used
anywhere %d or %u can be used, including with a leading l for long
values. They specify that the integer type argument is to be
converted to hexadecimal text representation.

Example:

printf("%d, %x, %X, %04X\n", 63, 63, 63, 63);

....should output:

63, 3f, 3F, 003F

The lower case %x specifies that hex digits a...f are output as lower
case, %X displays A...F as upper case. All of the usual conversion
modifiers may be used, such a "%04X", which prints four characters
with leading '0' fill.
 
I

Ian Gibbons

Firstly what type is %x as I've not encountered it before?
Now the problem: I'm trying to alter a host masking system for ircd
so that it masks all but the isp name and location (if .uk.us etc..).
However the crc32 stuff doesnt return as a string, and because the
number of hostname fields is not static I need to build a string
which contains each hostname field hashed and joined together.

if (parc > 4) /* There are isp's like *.isp.co.uk etc out
there */

What is parc?
{
for (i=2; i < (parc - 2); i++)
{
hash = ((crc32(parv, strlen(parv)) + KEY2) ^ KEY1) ^
KEY3;


What is hash? What is parv? What does crc32() return?
hash <<= 2;
hash >>= 2;
/* what goes here to make buf complete? */
}
sprintf(mask, "%s%s.%s.%s", buf, parv[parc - 3], parv[parc - 2],
parv[parc - 1]);
}

hash[0] and [1] are always the first two hostname fields, and in
other conditions are sprintf'ed into mask as %x.
Ultimately what im aiming is to have mask.mask.mydomain.co.uk,
mask.mask.mask.otherdomain.co.uk, mask.mask.mask.domain3.com etc..

Hope this makes sense :)

Kind Regards,
Ian


Your posted sample code containing quite a few variables and one
function without explaining what they are.

As for the question in your first sentence, %x and %X are conversion
specifier for the printf() family of functions that can be used
anywhere %d or %u can be used, including with a leading l for long
values. They specify that the integer type argument is to be
converted to hexadecimal text representation.

Example:

printf("%d, %x, %X, %04X\n", 63, 63, 63, 63);

...should output:

63, 3f, 3F, 003F

The lower case %x specifies that hex digits a...f are output as lower
case, %X displays A...F as upper case. All of the usual conversion
modifiers may be used, such a "%04X", which prints four characters
with leading '0' fill.


Cheers for that, now its just working out converting those to build the
buffer needed.

The crc32() function uses a huge array of hex values which I presume is
a referance for the actual crc32 method of masking things.

/* Return a 32-bit CRC of the contents of the buffer. */
unsigned long crc32(const unsigned char *s, unsigned int len)
{
unsigned int i;
unsigned long crc32val;

crc32val = 0;
for (i = 0; i < len; i++)
{
crc32val = crc32_tab[(crc32val ^ s) & 0xff] ^ (crc32val >> 8);
}
return crc32val;
}

Where crc32_tab[] is the array of hex values. parc is the count of
hostname fields, determined by making an array of the full hostname
using strtok. parv is then filled with each hostname field.
hash is set as "unsigned long hash[8];". I will at some stage add code
to make sure the loop doesn't try and fill above [8], but for now im
more concerned on getting it working how I want it :)

As a side note, the orignal code isn't mine and only hashed a fixed
number of hostname fields. I'm simply attempting to modify it to hash
all but the isp name.
Ie, with a host 5 or more fields long, to mask all but the last 3 fields
(domain.co.uk, dial.ntl.com etc..)

Also, if there's an obvious problem that would prevent this working I'd
be glad to have it explained why not.. I'm what could be called a
'learning' C coder, as the need to do mods for ircd is how I've learnt
what I have. I tend to learn best by practical samples rather than books
;p

Kind Regards,
Ian
 
I

Ian Gibbons

Firstly what type is %x as I've not encountered it before?
Now the problem: I'm trying to alter a host masking system for ircd
so that it masks all but the isp name and location (if .uk.us
etc..). However the crc32 stuff doesnt return as a string, and
because the number of hostname fields is not static I need to build
a string which contains each hostname field hashed and joined
together.

if (parc > 4) /* There are isp's like *.isp.co.uk etc out
there */

What is parc?
{
for (i=2; i < (parc - 2); i++)
{
hash = ((crc32(parv, strlen(parv)) + KEY2) ^ KEY1) ^
KEY3;


What is hash? What is parv? What does crc32() return?
hash <<= 2;
hash >>= 2;
/* what goes here to make buf complete? */
}
sprintf(mask, "%s%s.%s.%s", buf, parv[parc - 3], parv[parc -
2], parv[parc - 1]);
}

hash[0] and [1] are always the first two hostname fields, and in
other conditions are sprintf'ed into mask as %x.
Ultimately what im aiming is to have mask.mask.mydomain.co.uk,
mask.mask.mask.otherdomain.co.uk, mask.mask.mask.domain3.com etc..

Hope this makes sense :)

Kind Regards,
Ian


Your posted sample code containing quite a few variables and one
function without explaining what they are.

As for the question in your first sentence, %x and %X are conversion
specifier for the printf() family of functions that can be used
anywhere %d or %u can be used, including with a leading l for long
values. They specify that the integer type argument is to be
converted to hexadecimal text representation.

Example:

printf("%d, %x, %X, %04X\n", 63, 63, 63, 63);

...should output:

63, 3f, 3F, 003F

The lower case %x specifies that hex digits a...f are output as lower
case, %X displays A...F as upper case. All of the usual conversion
modifiers may be used, such a "%04X", which prints four characters
with leading '0' fill.


Cheers for that, now its just working out converting those to build
the buffer needed.

The crc32() function uses a huge array of hex values which I presume
is a referance for the actual crc32 method of masking things.

/* Return a 32-bit CRC of the contents of the buffer. */
unsigned long crc32(const unsigned char *s, unsigned int len)
{
unsigned int i;
unsigned long crc32val;

crc32val = 0;
for (i = 0; i < len; i++)
{
crc32val = crc32_tab[(crc32val ^ s) & 0xff] ^ (crc32val >>
8); }
return crc32val;
}

Where crc32_tab[] is the array of hex values. parc is the count of
hostname fields, determined by making an array of the full hostname
using strtok. parv is then filled with each hostname field.
hash is set as "unsigned long hash[8];". I will at some stage add code
to make sure the loop doesn't try and fill above [8], but for now im
more concerned on getting it working how I want it :)

As a side note, the orignal code isn't mine and only hashed a fixed
number of hostname fields. I'm simply attempting to modify it to hash
all but the isp name.
Ie, with a host 5 or more fields long, to mask all but the last 3
fields (domain.co.uk, dial.ntl.com etc..)

Also, if there's an obvious problem that would prevent this working
I'd be glad to have it explained why not.. I'm what could be called a
'learning' C coder, as the need to do mods for ircd is how I've learnt
what I have. I tend to learn best by practical samples rather than
books ;p

Kind Regards,
Ian


Think I have the solution, where the comment was for the unknown code I
now have:


sprintf(htob, "%x", hash);
strcat(buf, htob);
strcat(buf, ".");

htob is set exactly the same as mask, and a slight change of the i
condition check to <= seems to be working fine.
All I need is a friend to join tmw to test it works ;p

Tested now, it works. But....
Some hosts are now too long to fit in HOSTLEN :p
How annoying lol.

So I'm off to bed and then I'll think of a solution.

Regards,
Ian
 
P

Peter Nilsson

....
printf("%d, %x, %X, %04X\n", 63, 63, 63, 63);

...should output:

63, 3f, 3F, 003F

That's the Committee's intent, but...

printf("%d, %x, %X, %04X\n", 63, 63u, 63u, 63u);

....is still prefered by some. [Matching an expected unsigned int to each
%x/%X specifier.]
 
C

CBFalconer

Ian said:
.... snip ...

The crc32() function uses a huge array of hex values which I
presume is a referance for the actual crc32 method of masking
things.

I don't think Jack really cares. His actual point, which you seem
to have missed, is that you posted incomplete code and asked for
help with it. You should always reduce things to a minimum
compilable _standard C_ program before requesting help.
 
I

Ian Gibbons

I don't think Jack really cares. His actual point, which you seem
to have missed, is that you posted incomplete code and asked for
help with it. You should always reduce things to a minimum
compilable _standard C_ program before requesting help.

Noted..
This just seemed the place to ask while going through all the available
newsgroups :)

I came across a major problem with what I was attempting, so I had to
ditch that idea and stick with a fixed approach. Still, it meant I
learnt something I previously did not understand (the %x/%X types) and
that if doing such a loop to be careful not to exceed the length of your
complete string, which obviously didn't occur to me until I noticed the
chopped string when run.

Regards,
Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top