source: chevmsgr/trunk/bit.c@ 7

Last change on this file since 7 was 7, checked in by cheese, 9 years ago

암복호 알고리즘 추가

File size: 2.4 KB
Line 
1#include "bit.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5
6int
7CHEV_BIT_ShiftLeft (byte * in,
8 byte * out,
9 int size,
10 int offset)
11{
12 int iter = 0;
13
14 int quota = offset / 8;
15 int remain = offset % 8;
16 int byteSize = size / 8;
17 int loop = byteSize - quota;
18
19 int idx = 0;
20 int maskPos = 8 - remain;
21 byte mask = 0xff << (8 - remain);
22
23 out[0] = in[0 + quota] << remain;
24
25 for (iter = 1 ; iter < loop ; iter++)
26 {
27 idx = iter + quota;
28
29 out[iter ] = (in[idx] ) << remain;
30 out[iter - 1] |= (in[idx] & mask) >> maskPos;
31 }
32
33 return 0;
34}
35
36int
37CHEV_BIT_ShiftRight (byte * in,
38 byte * out,
39 int size,
40 int offset)
41{
42 int iter = 0;
43
44 int quota = offset / 8;
45 int remain = offset % 8;
46 int byteSize = size / 8;
47 int loop = quota - 1;
48
49 int idx = 0;
50 int maskPos = 8 - remain;
51 byte mask = 0xff >> (8 - remain);
52
53 out[byteSize - 1] = in[byteSize - 1 - quota] >> remain;
54
55 for (iter = byteSize - 2 ; iter > loop ; iter--)
56 {
57 idx = iter - quota;
58
59 out[iter ] = (in[idx] ) >> remain;
60 out[iter + 1] |= (in[idx] & mask) << maskPos;
61 }
62
63 return 0;
64}
65
66int
67CHEV_BIT_RotateLeft (byte * in,
68 byte * out,
69 int size,
70 int offset)
71{
72 int result = 0;
73 byte * buf1 = NULL;
74
75 buf1 = (byte *) calloc (size, 2);
76
77 if (!buf1)
78 return -1;
79
80 CHEV_BIT_ShiftLeft (in, buf1, size, offset);
81 CHEV_BIT_ShiftRight (in, buf1 + size, size, size - offset);
82
83 CHEV_BIT_OR (buf1, buf1 + size, size, out);
84
85 free (buf1);
86
87 return result;
88}
89
90int
91CHEV_BIT_RotateRight (byte * in,
92 byte * out,
93 int size,
94 int offset)
95{
96 int result = 0;
97 byte * buf1 = NULL;
98
99 buf1 = (byte *) calloc (size, 2);
100
101 if (!buf1)
102 return -1;
103
104 CHEV_BIT_ShiftRight (in, buf1, size, offset);
105 CHEV_BIT_ShiftLeft (in, buf1 + size, size, size - offset);
106
107 CHEV_BIT_OR (buf1, buf1 + size, size, out);
108
109 free (buf1);
110
111 return result;
112}
113
114int
115CHEV_BIT_AND (byte * in1,
116 byte * in2,
117 int size,
118 byte * out)
119{
120 int iter = 0;
121 int quota = size / 8;
122
123 for (iter = 0 ; iter < quota ; iter++)
124 out[iter] = in1[iter] & in2[iter];
125
126 return 0;
127}
128
129int
130CHEV_BIT_OR (byte * in1,
131 byte * in2,
132 int size,
133 byte * out)
134{
135 int iter = 0;
136 int quota = size / 8;
137
138 for (iter = 0 ; iter < quota ; iter++)
139 out[iter] = in1[iter] | in2[iter];
140
141 return 0;
142}
143
144int
145CHEV_BIT_XOR (byte * in1,
146 byte * in2,
147 int size,
148 byte * out)
149{
150 int iter = 0;
151 int quota = size / 8;
152
153 for (iter = 0 ; iter < quota ; iter++)
154 out[iter] = in1[iter] ^ in2[iter];
155
156 return 0;
157}
Note: See TracBrowser for help on using the repository browser.