Re: Comparing memory with a constant




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

.



Relevant Pages

  • Re: Comparing memory with a constant
    ... so having multiple constants would be kinda unwieldy. ... switch (array) { ... case bitpattern1: ... ...
    (comp.lang.c)
  • Re: Comparing memory with a constant
    ... so having multiple constants would be kinda unwieldy. ... switch (array) { ... case bitpattern1: ... ...
    (comp.lang.c)
  • Re: Comparing memory with a constant
    ... I might as well save some space and compare each byte of ... the array with each byte of the constant. ... switch { ... case bitpattern1: ... ...
    (comp.lang.c)
  • Re: Bug in Strings split method???
    ... The limit parameter controls the number of times the pattern is ... applied and therefore affects the length of the resulting array. ... limit n is greater than zero then the pattern will be applied at most n ...
    (comp.lang.java.programmer)
  • Re: Surprise in array concatenation
    ... >> computational states for which A is considered be 1. ... >> memory dump and discover a bit pattern 000000001 at the address FF07712CA0 ... could you stop naming this ADT an array? ... > These are Ada arrays, rock solid low level stuff, based on preexisting ...
    (comp.lang.ada)