On Mon, 2003-08-25 at 15:27, Timo Sirainen wrote:
I'm actually still wondering a bit if this works. Have to ask from some people who really know. The problem is that if I write 123 over XXX, can a simultaneous read() return 1X3 in some situation? (1XX, 12X, XX3 and X23 are fine) That might break my plan..
That would probably work, but just to make sure here's an addition to the plan. Can anyone think of a situation where this would break?
While MD5 is probably good enough, it doesn't _guarantee_ the consistency. I just thought of a simple algorithm that I think would. I'll go and use that unless someone proves it wrong :)
Except of course if there's 256 writes and the last one is non-ordered and it all happens while a read() is executing.. Less than unlikely I'd say.
I don't think there's any other potential problems with read() than that it may not have all data up to date? Such as it would never temporarily show the data as zero?
static int verify(const unsigned char *buf, size_t size) { const unsigned char *checksum = buf + size; unsigned char xor; size_t i;
xor = buf[0] ^ checksum[0];
for (i = 1; i < size; i++) {
if ((buf[i] ^ xor) != checksum[i])
return 0;
}
return 1;
}
void write_data(const void *data, size_t size) { unsigned char *checksum = buf + size; unsigned char xor; size_t i;
memcpy(buf, data, size);
checksum[0]++; // always different checksum
xor = buf[0] ^ checksum[0];
for (i = 1; i < size; i++)
checksum[i] = buf[i] ^ xor;
}
void read_data(void *data, size_t size) { unsigned char copy[size*2];
do {
memcpy(copy, buf, size*2);
} while (!verify(copy, size));
memcpy(data, copy, size);
}