Re: Comparing memory with a constant
- From: "galapogos" <goister@xxxxxxxxx>
- Date: 22 Nov 2006 17:35:45 -0800
Chris Torek wrote:
Ian Collins wrote:
const unsigned char ref[] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
In article <1164163513.385700.320670@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
galapogos <goister@xxxxxxxxx> wrote:
Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that
switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}
where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?
Indeed, you cannot do this at all.
If you dislike both:
if (memcmp(array, ref, sizeof ref) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
and:
if (memcmp(array, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09", 10) == 0)
... handle the case where the pattern is 0x00 0x01 0x02 ...
you can always write a program "P" to produce a C program (or program
fragment) to do the comparisons. You can then decide how fancy Program
P should be; it can even do things like:
/* program P has observed that these three bytes
combine to a value in [0..9180] that is unique for
all the "interesting" values, so that we only have to
verify that all ten values are correct */
switch (array[2] + (3 * array[3]) + (array[7] << 5)) {
static const unsigned char patterns[N][10] = {
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 },
... others ...
};
case DIGESTED_CONST_0:
if (memcmp(array, patterns[0], 10) != 0) goto Default;
... matched pattern #0 ...
break;
case DIGESTED_CONST_1:
if (memcmp(array, patterns[1], 10) != 0) goto Default;
... matched pattern #1 ...
break;
... other cases here ...
default:
Default:
... did not match any pattern ...
}
Look up "perfect hash" to find strategies for producing a formula
for computing a number to switch on, and case-labels.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Thanks! I don't think I'll do the perfect hash way though it is pretty
interested if a tad convoluted. The 2nd idea is great though. I've been
looking for a way to represent the pattern with a constant, even a
string, but for some reason I didn't think of that method. I just tried
it and it works. That'll save me the memory for declaring the patterns
as constant arrays. I can just put a couple of #defines with this :)
.
- Follow-Ups:
- Re: Comparing memory with a constant
- From: J. J. Farrell
- Re: Comparing memory with a constant
- References:
- Comparing memory with a constant
- From: galapogos
- Re: Comparing memory with a constant
- From: galapogos
- Re: Comparing memory with a constant
- From: Ian Collins
- Re: Comparing memory with a constant
- From: galapogos
- Re: Comparing memory with a constant
- From: Chris Torek
- Comparing memory with a constant
- Prev by Date: Re: %[^a] in scanf
- Next by Date: Re: Comparing memory with a constant
- Previous by thread: Re: Comparing memory with a constant
- Next by thread: Re: Comparing memory with a constant
- Index(es):
Relevant Pages
|
|