-
Notifications
You must be signed in to change notification settings - Fork 11
/
byteutils.cpp
50 lines (42 loc) · 863 Bytes
/
byteutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* byteutils.cpp
* stackimport
*
* Created by Mr. Z. on 03/31/10.
* Copyright 2010 Mr Z. All rights reserved.
*
*/
#include "byteutils.h"
#include <string.h>
char * __hex(int x)
{
const char * hex = "0123456789ABCDEF";
char h[] = "ab";
static char buf[4] = { 0 };
h[0] = hex[(x/16) % 16];
h[1] = hex[x % 16];
strcpy( buf, h );
return buf;
}
// X-Ors the bytes in dest with those in src:
void xornstr(char * dest, const char * src, int n)
{
int i = 0;
for (i=0; i<n; i++)
{
dest[i] ^= src[i];
}
}
void shiftnstr(char * s, int n, int sh)
{
int i = 0;
int p = 1;
int x = 0;
for (i=0; i<sh; i++) { p += p; } // Bitshift p by sh bits?
for (i=0; i<n; i++)
{
x += ((unsigned char)s[i] * 65536) / p; // Bitshift by 2 bytes?
s[i] = x / 65536; // Store low byte?
x = (x % 65536) * 256; // Keep high byte?
}
}