Skip to content
Snippets Groups Projects
Commit 609502b0 authored by Simon Tatham's avatar Simon Tatham
Browse files

Add utility function 'memxor'.

parent 09fa3f0e
No related branches found
No related tags found
No related merge requests found
...@@ -430,4 +430,13 @@ LoadFileStatus lf_load(LoadedFile *lf, const Filename *filename); ...@@ -430,4 +430,13 @@ LoadFileStatus lf_load(LoadedFile *lf, const Filename *filename);
static inline ptrlen ptrlen_from_lf(LoadedFile *lf) static inline ptrlen ptrlen_from_lf(LoadedFile *lf)
{ return make_ptrlen(lf->data, lf->len); } { return make_ptrlen(lf->data, lf->len); }
/* Set the memory block of 'size' bytes at 'out' to the bitwise XOR of
* the two blocks of the same size at 'in1' and 'in2'.
*
* 'out' may point to exactly the same address as one of the inputs,
* but if the input and output blocks overlap in any other way, the
* result of this function is not guaranteed. No memmove-style effort
* is made to handle difficult overlap cases. */
void memxor(uint8_t *out, const uint8_t *in1, const uint8_t *in2, size_t size);
#endif #endif
...@@ -1073,3 +1073,29 @@ void write_c_string_literal(FILE *fp, ptrlen str) ...@@ -1073,3 +1073,29 @@ void write_c_string_literal(FILE *fp, ptrlen str)
fprintf(fp, "\\%03o", (unsigned char)c); fprintf(fp, "\\%03o", (unsigned char)c);
} }
} }
void memxor(uint8_t *out, const uint8_t *in1, const uint8_t *in2, size_t size)
{
switch (size & 15) {
case 0:
while (size >= 16) {
*out++ = *in1++ ^ *in2++;
case 15: *out++ = *in1++ ^ *in2++;
case 14: *out++ = *in1++ ^ *in2++;
case 13: *out++ = *in1++ ^ *in2++;
case 12: *out++ = *in1++ ^ *in2++;
case 11: *out++ = *in1++ ^ *in2++;
case 10: *out++ = *in1++ ^ *in2++;
case 9: *out++ = *in1++ ^ *in2++;
case 8: *out++ = *in1++ ^ *in2++;
case 7: *out++ = *in1++ ^ *in2++;
case 6: *out++ = *in1++ ^ *in2++;
case 5: *out++ = *in1++ ^ *in2++;
case 4: *out++ = *in1++ ^ *in2++;
case 3: *out++ = *in1++ ^ *in2++;
case 2: *out++ = *in1++ ^ *in2++;
case 1: *out++ = *in1++ ^ *in2++;
size -= 16;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment