Naturally XOR can be used twice to get back the original value. What if the original value is part of the mask?
Encoding:
e[i] = c[i] ^ (c[i] + c[i-1])
Assuming: starting value c[-1] = 0, ^ means bitwise XOR
In imperative C form:
void encode(byte *p, int len)
{
byte prev = 0;
for (auto i = 0; i < len; i++)
{
auto byt = p[i];
p[i] = byt ^ (prev + byt);
prev = byt;
}
}
How do I create a decode step that reverses this from e => c?
I've simplified/clarified (read: changed) the question given what I've learned from your answers! Using similar steps to DanL, starting with the original equation:
e[i] = c[i] ^ (c[i] + c[i-1])
e[i] ^ c[i] = c[i] ^ (c[i] + c[i-1]) ^ c[i]
e[i] ^ c[i] = c[i] + c[i-1]
c[i] = e[i] ^ c[i] - c[i-1]
c[i] ^ c[i] = (e[i] ^ c[i] - c[i-1]) ^ c[i]
0 = e[i] ^ c[i] ^ c[i] - c[i-1] ^ c[i]
0 = e[i] - c[i-1] ^ c[i]
c[i-1] ^ c[i] = e[i]
c[i-1] ^ c[i] ^ c[i-1] = e[i] ^ c[i-1]
c[i] = e[i] ^ c[i-1]
???
Now, looking at the original encode - the first byte will always be zero (= c[i] ^ (c[i] + 0)). So yes, there must be a loss of one byte over the set.