1 | /**
|
---|
2 | * \file cf_bitwise.c
|
---|
3 | *
|
---|
4 | * \author myusgun <myusgun@gmail.com>
|
---|
5 | * \author chevalier <achagun@gmail.com>
|
---|
6 | *
|
---|
7 | * \brief 비트 연산자 구현
|
---|
8 | */
|
---|
9 | #include "cf_bitwise.h"
|
---|
10 | #include "cf_error.h"
|
---|
11 |
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <stdlib.h>
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * 비트열을 왼쪽으로 시프트
|
---|
17 | *
|
---|
18 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
19 | *
|
---|
20 | * \param in 시프트 할 비트열
|
---|
21 | * \param out 시프트 결과
|
---|
22 | * \param size 비트열 길이 (8의 배수)
|
---|
23 | * \param offset 시프트 할 수
|
---|
24 | */
|
---|
25 | int
|
---|
26 | CF_Bitwise_ShiftLeft (cf_byte * in,
|
---|
27 | cf_byte * out,
|
---|
28 | size_t size,
|
---|
29 | size_t offset)
|
---|
30 | {
|
---|
31 | size_t iter = 0;
|
---|
32 |
|
---|
33 | size_t quota = offset / 8;
|
---|
34 | size_t remain = offset % 8;
|
---|
35 | size_t byteSize = size / 8;
|
---|
36 |
|
---|
37 | size_t maskPos = 8 - remain;
|
---|
38 | cf_byte mask = 0;
|
---|
39 | cf_byte buf = 0;
|
---|
40 |
|
---|
41 | if (!in || !out)
|
---|
42 | return CF_ERROR_BITWISE_INVALID_ARGS;
|
---|
43 |
|
---|
44 | for (iter = 0 ; iter < remain ; iter++)
|
---|
45 | mask |= (cf_byte)(0x80 >> iter);
|
---|
46 |
|
---|
47 | out[0] = (cf_byte)(in[0 + quota] << remain);
|
---|
48 |
|
---|
49 | for (iter = 1 ; iter < byteSize - quota ; iter++)
|
---|
50 | {
|
---|
51 | buf = in[iter + quota] & mask;
|
---|
52 |
|
---|
53 | out[iter ] = (cf_byte)(in[iter + quota] << remain);
|
---|
54 | out[iter - 1] |= (cf_byte)(buf >> maskPos);
|
---|
55 | }
|
---|
56 |
|
---|
57 | return CF_OK;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * 비트열을 오른쪽으로 시프트
|
---|
62 | *
|
---|
63 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
64 | *
|
---|
65 | * \param in 시프트 할 비트열
|
---|
66 | * \param out 시프트 결과
|
---|
67 | * \param size 비트열 길이 (8의 배수)
|
---|
68 | * \param offset 시프트 할 수
|
---|
69 | */
|
---|
70 | int
|
---|
71 | CF_Bitwise_ShiftRight (cf_byte * in,
|
---|
72 | cf_byte * out,
|
---|
73 | size_t size,
|
---|
74 | size_t offset)
|
---|
75 | {
|
---|
76 | size_t iter = 0;
|
---|
77 |
|
---|
78 | size_t quota = offset / 8;
|
---|
79 | size_t remain = offset % 8;
|
---|
80 | size_t byteSize = size / 8;
|
---|
81 |
|
---|
82 | size_t maskPos = 8 - remain;
|
---|
83 | cf_byte mask = 0;
|
---|
84 | cf_byte buf = 0;
|
---|
85 |
|
---|
86 | if (!in || !out)
|
---|
87 | return CF_ERROR_BITWISE_INVALID_ARGS;
|
---|
88 |
|
---|
89 | for (iter = 0 ; iter < remain ; iter++)
|
---|
90 | mask |= (cf_byte)(0x01 << iter);
|
---|
91 |
|
---|
92 | out[byteSize - 1] = (cf_byte)(in[byteSize - 1 - quota] >> remain);
|
---|
93 |
|
---|
94 | for (iter = byteSize - 2 ; iter > quota - 1 ; iter--)
|
---|
95 | {
|
---|
96 | buf = in[iter - quota] & mask;
|
---|
97 |
|
---|
98 | out[iter ] = (cf_byte)(in[iter - quota] >> remain);
|
---|
99 | out[iter + 1] |= (cf_byte)(buf << maskPos);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return CF_OK;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * 비트열을 왼쪽으로 로테이션
|
---|
107 | *
|
---|
108 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
109 | *
|
---|
110 | * \param in 로테이션 할 비트열
|
---|
111 | * \param out 로테이션 결과
|
---|
112 | * \param size 비트열 길이 (8의 배수)
|
---|
113 | * \param offset 로테이션 할 수
|
---|
114 | */
|
---|
115 | int
|
---|
116 | CF_Bitwise_RotateLeft (cf_byte * in,
|
---|
117 | cf_byte * out,
|
---|
118 | size_t size,
|
---|
119 | size_t offset)
|
---|
120 | {
|
---|
121 | int result = 0;
|
---|
122 | cf_byte * buf1 = NULL;
|
---|
123 | cf_byte * buf2 = NULL;
|
---|
124 |
|
---|
125 | if (!in || !out)
|
---|
126 | return CF_ERROR_BITWISE_INVALID_ARGS;
|
---|
127 |
|
---|
128 | buf1 = (cf_byte *) calloc (size, 1);
|
---|
129 | buf2 = (cf_byte *) calloc (size, 1);
|
---|
130 |
|
---|
131 | if (!buf1 || !buf2)
|
---|
132 | result = CF_ERROR_BITWISE_ALLOCATE_BUFFER;
|
---|
133 | else
|
---|
134 | {
|
---|
135 | CF_Bitwise_ShiftLeft (in, buf1, size, offset);
|
---|
136 | CF_Bitwise_ShiftRight (in, buf2, size, size - offset);
|
---|
137 |
|
---|
138 | CF_Bitwise_OR (buf1, buf2, size, out);
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (buf1) free (buf1);
|
---|
142 | if (buf2) free (buf2);
|
---|
143 |
|
---|
144 | return result;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * 비트열을 오른쪽으로 로테이션
|
---|
149 | *
|
---|
150 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
151 | *
|
---|
152 | * \param in 로테이션 할 비트열
|
---|
153 | * \param out 로테이션 결과
|
---|
154 | * \param size 비트열 길이 (8의 배수)
|
---|
155 | * \param offset 로테이션 할 수
|
---|
156 | */
|
---|
157 | int
|
---|
158 | CF_Bitwise_RotateRight (cf_byte * in,
|
---|
159 | cf_byte * out,
|
---|
160 | size_t size,
|
---|
161 | size_t offset)
|
---|
162 | {
|
---|
163 | int result = 0;
|
---|
164 | cf_byte * buf1 = NULL;
|
---|
165 | cf_byte * buf2 = NULL;
|
---|
166 |
|
---|
167 | if (!in || !out)
|
---|
168 | return CF_ERROR_BITWISE_INVALID_ARGS;
|
---|
169 |
|
---|
170 | buf1 = (cf_byte *) calloc (size, 1);
|
---|
171 | buf2 = (cf_byte *) calloc (size, 1);
|
---|
172 |
|
---|
173 | if (!buf1 || !buf2)
|
---|
174 | result = CF_ERROR_BITWISE_ALLOCATE_BUFFER;
|
---|
175 | else
|
---|
176 | {
|
---|
177 | CF_Bitwise_ShiftRight (in, buf1, size, offset);
|
---|
178 | CF_Bitwise_ShiftLeft (in, buf2, size, size - offset);
|
---|
179 |
|
---|
180 | CF_Bitwise_OR (buf1, buf2, size, out);
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (buf1) free (buf1);
|
---|
184 | if (buf2) free (buf2);
|
---|
185 |
|
---|
186 | return result;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * 두 비트열의 AND
|
---|
191 | *
|
---|
192 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
193 | *
|
---|
194 | * \param in1 입력 비트열 1
|
---|
195 | * \param in2 입력 비트열 2
|
---|
196 | * \param size 비트열 길이 (8의 배수)
|
---|
197 | * \param out 두 비트열의 AND 결과
|
---|
198 | */
|
---|
199 | int
|
---|
200 | CF_Bitwise_AND (cf_byte * in1,
|
---|
201 | cf_byte * in2,
|
---|
202 | size_t size,
|
---|
203 | cf_byte * out)
|
---|
204 | {
|
---|
205 | size_t iter = 0;
|
---|
206 | size_t quota = size / 8;
|
---|
207 |
|
---|
208 | if (!in1 || !in2)
|
---|
209 | return CF_ERROR_BITWISE_INVALID_ARGS;
|
---|
210 |
|
---|
211 | for (iter = 0 ; iter < quota ; iter++)
|
---|
212 | out[iter] = in1[iter] & in2[iter];
|
---|
213 |
|
---|
214 | return 0;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * 두 비트열의 OR
|
---|
219 | *
|
---|
220 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
221 | *
|
---|
222 | * \param in1 입력 비트열 1
|
---|
223 | * \param in2 입력 비트열 2
|
---|
224 | * \param size 비트열 길이 (8의 배수)
|
---|
225 | * \param out 두 비트열의 OR 결과
|
---|
226 | */
|
---|
227 | int
|
---|
228 | CF_Bitwise_OR (cf_byte * in1,
|
---|
229 | cf_byte * in2,
|
---|
230 | size_t size,
|
---|
231 | cf_byte * out)
|
---|
232 | {
|
---|
233 | size_t iter = 0;
|
---|
234 | size_t quota = size / 8;
|
---|
235 |
|
---|
236 | if (!in1 || !in2)
|
---|
237 | return CF_ERROR_BITWISE_INVALID_ARGS;
|
---|
238 |
|
---|
239 | for (iter = 0 ; iter < quota ; iter++)
|
---|
240 | out[iter] = in1[iter] | in2[iter];
|
---|
241 |
|
---|
242 | return 0;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * 두 비트열의 XOR
|
---|
247 | *
|
---|
248 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
249 | *
|
---|
250 | * \param in1 입력 비트열 1
|
---|
251 | * \param in2 입력 비트열 2
|
---|
252 | * \param size 비트열 길이 (8의 배수)
|
---|
253 | * \param out 두 비트열의 XOR 결과
|
---|
254 | */
|
---|
255 | int
|
---|
256 | CF_Bitwise_XOR (cf_byte * in1,
|
---|
257 | cf_byte * in2,
|
---|
258 | size_t size,
|
---|
259 | cf_byte * out)
|
---|
260 | {
|
---|
261 | size_t iter = 0;
|
---|
262 | size_t quota = size / 8;
|
---|
263 |
|
---|
264 | if (!in1 || !in2)
|
---|
265 | return CF_ERROR_BITWISE_INVALID_ARGS;
|
---|
266 |
|
---|
267 | for (iter = 0 ; iter < quota ; iter++)
|
---|
268 | out[iter] = in1[iter] ^ in2[iter];
|
---|
269 |
|
---|
270 | return 0;
|
---|
271 | }
|
---|