Skip to content

Instantly share code, notes, and snippets.

@ia
Created January 23, 2013 20:51
Show Gist options
  • Select an option

  • Save ia/4613015 to your computer and use it in GitHub Desktop.

Select an option

Save ia/4613015 to your computer and use it in GitHub Desktop.
ip masking basic sample
#include <stdio.h>
#include <stdlib.h>
int membyte(unsigned char *b1, unsigned char *b2, unsigned char *b3, int len)
{
unsigned char *tmp = malloc(len);
printf("\n");
int i = 0;
for (i = 0; i < len; i++) {
printf("----\n");
printf("^: 0x%.02X\n", tmp[i] = b1[i] ^ b2[i]);
printf("|: 0x%.02X\n", tmp[i] = b1[i] | b2[i]);
printf("&: 0x%.02X\n", tmp[i] = b1[i] & b2[i]);
printf("----\n");
}
printf("result:\n");
for (i = 0; i < len; i++) {
printf("----\n");
printf("^: 0x%.02X\n", tmp[i] ^ b3[i]);
printf("&: 0x%.02X\n", tmp[i] & b3[i]);
printf("|: 0x%.02X\n", tmp[i] | b3[i]);
printf("----\n");
}
unsigned int m = memcmp(tmp, b3, len);
printf("cmp %.02X %d\n", m, m); // tmp[i] | b3[i]);
printf("\n");
}
int main(int argc, char const* argv[])
{
unsigned char buf1[4] = { 0xC0, 0xA8, 0x01, 0x42 };
unsigned char buf2[4] = { 0xFF, 0xFF, 0xFF, 0x00 };
unsigned char buf3[4] = { 0xC0, 0xA8, 0x01, 0x00 };
membyte(buf1, buf2, buf3, 4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment