Debian Codegraph @

src/fcml_utils.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2020 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <fcml_types.h>
21 #include <fcml_errors.h>
22
23 #include "fcml_utils.h"
24 #include "fcml_coll.h"
25 #include "fcml_trace.h"
26 #include "fcml_messages.h"
27
28 fcml_ceh_error fcml_fn_utils_conv_int64_to_int(const fcml_uint64_t src,
29 fcml_bool is_src_signed, fcml_st_integer *dest,
30 fcml_en_utils_size_flags filter) {
31
32 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
33
34 if (is_src_signed) {
35 fcml_int64_t imm = (fcml_int64_t) src;
36 if ((filter & FCML_ENUSF_8) && imm <= FCML_INT8_MAX
37 && imm >= FCML_INT8_MIN) {
38 dest->int8 = (fcml_int8_t) imm;
39 dest->size = FCML_DS_8;
40 } else if ((filter & FCML_ENUSF_16) && imm <= FCML_INT16_MAX
41 && imm >= FCML_INT16_MIN) {
42 dest->int16 = (fcml_int16_t) imm;
43 dest->size = FCML_DS_16;
44 } else if ((filter & FCML_ENUSF_32) && imm <= FCML_INT32_MAX
45 && imm >= FCML_INT32_MIN) {
46 dest->int32 = (fcml_int32_t) imm;
47 dest->size = FCML_DS_32;
48 } else {
49 if (filter & FCML_ENUSF_64) {
50 dest->int64 = imm;
51 dest->size = FCML_DS_64;
52 } else {
53 error = FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
54 }
55 }
56 } else {
57 if ((filter & FCML_ENUSF_8) && (src <= FCML_UINT8_MAX)) {
58 dest->int8 = (fcml_uint8_t) src;
59 dest->size = FCML_DS_8;
60 } else if ((filter & FCML_ENUSF_16) && (src <= FCML_UINT16_MAX)) {
61 dest->int16 = (fcml_uint16_t) src;
62 dest->size = FCML_DS_16;
63 } else if ((filter & FCML_ENUSF_32) && (src <= FCML_UINT32_MAX)) {
64 dest->int32 = (fcml_uint32_t) src;
65 dest->size = FCML_DS_32;
66 } else if (filter & FCML_ENUSF_64) {
67 dest->int64 = src;
68 dest->size = FCML_DS_64;
69 } else {
70 error = FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
71 }
72 }
73
74 dest->is_signed = is_src_signed;
75
76 return error;
77 }
78
79 fcml_ceh_error fcml_fn_utils_conv_int_to_int(const fcml_st_integer *src,
80 fcml_st_integer *dest, fcml_usize expected_src_size,
81 fcml_usize dest_size) {
82
83 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
84
85 /* Convert given source immediate value to expected one.*/
86 fcml_st_integer expected = { 0 };
87
88 expected.size = expected_src_size;
89 expected.is_signed = src->is_signed;
90
91 if (src->size != expected_src_size) {
92 switch (expected_src_size) {
93 case FCML_DS_8:
94 error = fcml_fn_utils_conv_int_to_int8(src, &(expected.int8));
95 break;
96 case FCML_DS_16:
97 error = fcml_fn_utils_conv_int_to_int16(src, &(expected.int16));
98 break;
99 case FCML_DS_32:
100 error = fcml_fn_utils_conv_int_to_int32(src, &(expected.int32));
101 break;
102 case FCML_DS_64:
103 error = fcml_fn_utils_conv_int_to_int64(src, &(expected.int64));
104 break;
105 }
106 } else {
107 expected = *src;
108 }
109
110 if (!error) {
111 /* Convert expected value to destination one.*/
112 switch (dest_size) {
113 case FCML_DS_8:
114 error = fcml_fn_utils_conv_int_to_int8(&expected, &(dest->int8));
115 break;
116 case FCML_DS_16:
117 error = fcml_fn_utils_conv_int_to_int16(&expected, &(dest->int16));
118 break;
119 case FCML_DS_32:
120 error = fcml_fn_utils_conv_int_to_int32(&expected, &(dest->int32));
121 break;
122 case FCML_DS_64:
123 error = fcml_fn_utils_conv_int_to_int64(&expected, &(dest->int64));
124 break;
125 }
126 if (!error) {
127 dest->size = dest_size;
128 dest->is_signed = expected.is_signed;
129 }
130 }
131
132 return error;
133 }
134
135 fcml_ceh_error fcml_fn_utils_conv_int_to_uint8(const fcml_st_integer *src,
136 fcml_uint8_t *dest) {
137 switch (src->size) {
138 case FCML_DS_8:
139 /* In this case we doesn't care what is in IMM, because no conversion is needed.*/
140 *dest = src->int8;
141 break;
142 case FCML_DS_16:
143 if (src->is_signed) {
144 if (src->int16 > FCML_UINT8_MAX || src->int16 < 0) {
145 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
146 }
147 *dest = (fcml_uint8_t) (fcml_int16_t) src->int16;
148 } else {
149 if ((fcml_uint16_t) src->int16 > FCML_UINT8_MAX) {
150 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
151 }
152 *dest = (fcml_uint8_t) src->int16;
153 }
154 break;
155 case FCML_DS_32:
156 if (src->is_signed) {
157 if (src->int32 > FCML_UINT8_MAX || src->int32 < 0) {
158 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
159 }
160 *dest = (fcml_uint8_t) (fcml_int32_t) src->int32;
161 } else {
162 if ((fcml_uint32_t) src->int32 > FCML_UINT8_MAX) {
163 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
164 }
165 *dest = (fcml_uint8_t) src->int32;
166 }
167 break;
168 case FCML_DS_64:
169 if (src->is_signed) {
170 if (src->int64 > FCML_UINT8_MAX || src->int64 < 0) {
171 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
172 }
173 *dest = (fcml_uint8_t) (fcml_int64_t) src->int64;
174 } else {
175 if ((fcml_uint64_t) src->int64 > FCML_UINT8_MAX) {
176 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
177 }
178 *dest = (fcml_uint8_t) src->int64;
179 }
180 break;
181 default:
182 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
183 }
184 return FCML_CEH_GEC_NO_ERROR;
185 }
186
187 fcml_ceh_error fcml_fn_utils_conv_int_to_uint16(const fcml_st_integer *src,
188 fcml_uint16_t *dest) {
189 switch (src->size) {
190 case FCML_DS_8:
191 if (src->is_signed) {
192 *dest = (fcml_uint16_t) src->int8;
193 } else {
194 *dest = (fcml_uint16_t) (fcml_uint8_t) src->int8;
195 }
196 break;
197 case FCML_DS_16:
198 *dest = (fcml_uint16_t) src->int16;
199 break;
200 case FCML_DS_32:
201 if (src->is_signed) {
202 if (src->int32 > FCML_UINT16_MAX || src->int32 < 0) {
203 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
204 }
205 *dest = (fcml_uint16_t) src->int32;
206 } else {
207 if ((fcml_uint32_t) src->int32 > FCML_UINT16_MAX) {
208 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
209 }
210 *dest = (fcml_uint16_t) src->int32;
211 }
212 break;
213 case FCML_DS_64:
214 if (src->is_signed) {
215 if (src->int64 > FCML_UINT16_MAX || src->int64 < 0) {
216 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
217 }
218 *dest = (fcml_uint16_t) src->int64;
219 } else {
220 if ((fcml_uint64_t) src->int64 > FCML_UINT16_MAX) {
221 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
222 }
223 *dest = (fcml_uint16_t) src->int64;
224 }
225 break;
226 default:
227 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
228 }
229 return FCML_CEH_GEC_NO_ERROR;
230 }
231
232 fcml_ceh_error fcml_fn_utils_conv_int_to_uint32(const fcml_st_integer *src,
233 fcml_uint32_t *dest) {
234 switch (src->size) {
235 case FCML_DS_8:
236 if (src->is_signed) {
237 *dest = (fcml_uint32_t) src->int8;
238 } else {
239 *dest = (fcml_uint32_t) (fcml_uint8_t) src->int8;
240 }
241 break;
242 case FCML_DS_16:
243 if (src->is_signed) {
244 *dest = (fcml_uint32_t) src->int16;
245 } else {
246 *dest = (fcml_uint32_t) (fcml_uint16_t) src->int16;
247 }
248 break;
249 case FCML_DS_32:
250 *dest = (fcml_uint32_t) src->int32;
251 break;
252 case FCML_DS_64:
253 if (src->is_signed) {
254 if (src->int64 > FCML_UINT32_MAX || src->int64 < 0) {
255 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
256 }
257 *dest = (fcml_uint32_t) src->int16;
258 } else {
259 if ((fcml_uint64_t) src->int64 > FCML_UINT32_MAX) {
260 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
261 }
262 *dest = (fcml_uint32_t) src->int64;
263 }
264 break;
265 default:
266 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
267 }
268 return FCML_CEH_GEC_NO_ERROR;
269 }
270
271 fcml_ceh_error fcml_fn_utils_conv_int_to_uint64(const fcml_st_integer *src,
272 fcml_uint64_t *dest) {
273 switch (src->size) {
274 case FCML_DS_8:
275 if (src->is_signed) {
276 *dest = (fcml_uint64_t) src->int8;
277 } else {
278 *dest = (fcml_uint64_t) (fcml_uint8_t) src->int8;
279 }
280 break;
281 case FCML_DS_16:
282 if (src->is_signed) {
283 *dest = (fcml_uint64_t) src->int16;
284 } else {
285 *dest = (fcml_uint64_t) (fcml_uint16_t) src->int16;
286 }
287 break;
288 case FCML_DS_32:
289 if (src->is_signed) {
290 *dest = (fcml_uint64_t) src->int32;
291 } else {
292 *dest = (fcml_uint64_t) (fcml_uint32_t) src->int32;
293 }
294 break;
295 case FCML_DS_64:
296 *dest = (fcml_uint64_t) src->int64;
297 break;
298 default:
299 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
300 }
301 return FCML_CEH_GEC_NO_ERROR;
302 }
303
304 fcml_ceh_error fcml_fn_utils_conv_int_to_int8(const fcml_st_integer *src,
305 fcml_int8_t *dest) {
306 switch (src->size) {
307 case FCML_DS_8:
308 *dest = (fcml_int8_t) src->int8;
309 break;
310 case FCML_DS_16:
311 if (src->is_signed) {
312 if (src->int16 < FCML_INT8_MIN || src->int16 > FCML_INT8_MAX) {
313 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
314 }
315 *dest = (fcml_int8_t) src->int16;
316 } else {
317 if ((fcml_uint16_t) src->int16 > FCML_UINT8_MAX) {
318 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
319 }
320 *dest = (fcml_int8_t) (fcml_uint16_t) src->int16;
321 }
322 break;
323 case FCML_DS_32:
324 if (src->is_signed) {
325 if (src->int32 < FCML_INT8_MIN || src->int32 > FCML_INT8_MAX) {
326 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
327 }
328 *dest = (fcml_int8_t) src->int32;
329 } else {
330 if ((fcml_uint32_t) src->int32 > FCML_INT8_MAX) {
331 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
332 }
333 *dest = (fcml_int8_t) (fcml_uint32_t) src->int32;
334 }
335 break;
336 case FCML_DS_64:
337 if (src->is_signed) {
338 if ((fcml_int64_t) src->int64 < FCML_INT8_MIN
339 || (fcml_int64_t) src->int64 > FCML_INT8_MAX) {
340 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
341 }
342 *dest = (fcml_int8_t) src->int64;
343 } else {
344 if ((fcml_uint64_t) src->int64 > FCML_INT8_MAX) {
345 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
346 }
347 *dest = (fcml_int8_t) (fcml_uint64_t) src->int64;
348 }
349 break;
350 default:
351 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
352 }
353 return FCML_CEH_GEC_NO_ERROR;
354 }
355
356 fcml_ceh_error fcml_fn_utils_conv_int_to_int16(const fcml_st_integer *src,
357 fcml_int16_t *dest) {
358 switch (src->size) {
359 case FCML_DS_8:
360 *dest = src->is_signed ?
361 (fcml_int16_t) src->int8 :
362 (fcml_int16_t) (fcml_uint8_t) src->int8;
363 break;
364 case FCML_DS_16:
365 *dest = src->int16;
366 break;
367 case FCML_DS_32:
368 if (src->is_signed) {
369 if (src->int32 < FCML_INT16_MIN || src->int32 > FCML_INT16_MAX) {
370 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
371 }
372 *dest = (fcml_int16_t) src->int32;
373 } else {
374 if ((fcml_uint32_t) src->int32 > FCML_INT16_MAX) {
375 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
376 }
377 *dest = (fcml_int16_t) (fcml_uint32_t) src->int32;
378 }
379 break;
380 case FCML_DS_64:
381 if (src->is_signed) {
382 if (src->int64 < FCML_INT16_MIN || src->int64 > FCML_INT16_MAX) {
383 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
384 }
385 *dest = (fcml_int16_t) src->int64;
386 } else {
387 if ((fcml_uint64_t) src->int64 > FCML_INT16_MAX) {
388 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
389 }
390 *dest = (fcml_int16_t) (fcml_uint64_t) src->int64;
391 }
392 break;
393 default:
394 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
395 }
396 return FCML_CEH_GEC_NO_ERROR;
397 }
398
399 fcml_ceh_error fcml_fn_utils_conv_int_to_int32(const fcml_st_integer *src,
400 fcml_int32_t *dest) {
401 switch (src->size) {
402 case FCML_DS_8:
403 *dest = src->is_signed ?
404 (fcml_int32_t) src->int8 :
405 (fcml_int32_t) (fcml_uint8_t) src->int8;
406 break;
407 case FCML_DS_16:
408 *dest = src->is_signed ?
409 (fcml_int32_t) src->int16 :
410 (fcml_int32_t) (fcml_uint16_t) src->int16;
411 break;
412 case FCML_DS_32:
413 *dest = src->int32;
414 break;
415 case FCML_DS_64:
416 if (src->is_signed) {
417 if (src->int64 < FCML_INT32_MIN || src->int64 > FCML_INT32_MAX) {
418 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
419 }
420 *dest = (fcml_int32_t) src->int64;
421 } else {
422 if ((fcml_uint64_t) src->int64 > FCML_INT32_MAX) {
423 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
424 }
425 *dest = (fcml_int32_t) (fcml_uint64_t) src->int64;
426 }
427 break;
428 default:
429 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
430 }
431 return FCML_CEH_GEC_NO_ERROR;
432 }
433
434 fcml_ceh_error fcml_fn_utils_conv_int_to_int64(const fcml_st_integer *src,
435 fcml_int64_t *dest) {
436 switch (src->size) {
437 case FCML_DS_8:
438 *dest = src->is_signed ?
439 (fcml_int64_t) src->int8 :
440 (fcml_int64_t) (fcml_uint8_t) src->int8;
441 break;
442 case FCML_DS_16:
443 *dest = src->is_signed ?
444 (fcml_int64_t) src->int16 :
445 (fcml_int64_t) (fcml_uint16_t) src->int16;
446 break;
447 case FCML_DS_32:
448 *dest = src->is_signed ?
449 (fcml_int64_t) src->int32 :
450 (fcml_int64_t) (fcml_uint32_t) src->int32;
451 break;
452 case FCML_DS_64:
453 *dest = src->int64;
454 break;
455 default:
456 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
457 }
458 return FCML_CEH_GEC_NO_ERROR;
459 }
460
461 fcml_ceh_error fcml_fn_utils_decode_integer(fcml_st_memory_stream *stream,
462 fcml_st_integer *integer, fcml_usize size) {
463 fcml_bool result = FCML_FALSE;
464 switch (size) {
465 case FCML_DS_8:
466 integer->int8 = fcml_fn_stream_read(stream, &result);
467 integer->size = FCML_DS_8;
468 break;
469 case FCML_DS_16:
470 integer->int16 = fcml_fn_stream_read_word(stream, &result);
471 integer->size = FCML_DS_16;
472 break;
473 case FCML_DS_32:
474 integer->int32 = fcml_fn_stream_read_dword(stream, &result);
475 integer->size = FCML_DS_32;
476 break;
477 case FCML_DS_64:
478 integer->int64 = fcml_fn_stream_read_qword(stream, &result);
479 integer->size = FCML_DS_64;
480 break;
481 default:
482 return FCML_CEH_GEC_INVALID_INPUT;
483 }
484 if (!result) {
485 return FCML_CEH_GEC_EOF;
486 }
487 return FCML_CEH_GEC_NO_ERROR;
488 }
489
490 fcml_ceh_error fcml_fn_utils_encode_integer(fcml_st_memory_stream *stream,
491 const fcml_st_integer *integer) {
492 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
493 fcml_bool result = FCML_FALSE;
494 switch (integer->size) {
495 case FCML_DS_8:
496 result = fcml_fn_stream_write(stream, integer->int8);
497 break;
498 case FCML_DS_16:
499 result = fcml_fn_stream_write_word(stream, integer->int16);
500 break;
501 case FCML_DS_32:
502 result = fcml_fn_stream_write_dword(stream, integer->int32);
503 break;
504 case FCML_DS_64:
505 result = fcml_fn_stream_write_qword(stream, integer->int64);
506 break;
507 }
508 if (!result) {
509 /* Insufficient stream size.*/
510 error = FCML_CEH_GEC_INVALID_INPUT;
511 }
512 return error;
513 }
514
515 fcml_ceh_error fcml_fn_utils_offset_to_integer(const fcml_st_offset *offset,
516 fcml_st_integer *integer) {
517 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
518 integer->int64 = 0;
519 switch (offset->size) {
520 case FCML_DS_16:
521 integer->int16 = offset->off16;
522 integer->size = FCML_DS_16;
523 break;
524 case FCML_DS_32:
525 integer->int32 = offset->off32;
526 integer->size = FCML_DS_32;
527 break;
528 case FCML_DS_64:
529 integer->int64 = offset->off64;
530 integer->size = FCML_DS_64;
531 break;
532 default:
533 error = FCML_CEH_GEC_INVALID_INPUT;
534 break;
535 }
536 integer->is_signed = offset->is_signed;
537 return error;
538 }
539
540 fcml_ceh_error fcml_fn_utils_integer_to_offset(const fcml_st_integer *integer,
541 fcml_st_offset *offset) {
542 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
543 offset->off64 = 0;
544 switch (integer->size) {
545 case FCML_DS_8:
546 offset->off16 = (fcml_uint16_t) integer->int8;
547 offset->size = FCML_DS_16;
548 break;
549 case FCML_DS_16:
550 offset->off16 = (fcml_uint16_t) integer->int16;
551 offset->size = FCML_DS_16;
552 break;
553 case FCML_DS_32:
554 offset->off32 = (fcml_uint32_t) integer->int32;
555 offset->size = FCML_DS_32;
556 break;
557 case FCML_DS_64:
558 offset->off64 = (fcml_uint64_t) integer->int64;
559 offset->size = FCML_DS_64;
560 break;
561 default:
562 error = FCML_CEH_GEC_INVALID_INPUT;
563 break;
564 }
565 offset->is_signed = integer->is_signed;
566 return error;
567 }
568
569 fcml_ceh_error fcml_fn_utils_extend_int(fcml_st_integer *integer,
570 fcml_usize extension) {
571 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
572 if (!integer->is_signed) {
573 fcml_uint64_t temp_int = 0;
574 switch (integer->size) {
575 case 8:
576 temp_int = (fcml_uint8_t) integer->int8;
577 break;
578 case 16:
579 temp_int = (fcml_uint16_t) integer->int16;
580 break;
581 case 32:
582 temp_int = (fcml_uint32_t) integer->int32;
583 break;
584 case 64:
585 temp_int = (fcml_uint64_t) integer->int64;
586 break;
587 default:
588 error = FCML_CEH_GEC_INVALID_INPUT;
589 break;
590 }
591 if (!error) {
592 integer->int64 = 0;
593 switch (extension) {
594 case 8:
595 integer->int8 = (fcml_uint8_t) temp_int;
596 break;
597 case 16:
598 integer->int16 = (fcml_uint16_t) temp_int;
599 break;
600 case 32:
601 integer->int32 = (fcml_uint32_t) temp_int;
602 break;
603 case 64:
604 integer->int64 = (fcml_uint64_t) temp_int;
605 break;
606 }
607 integer->size = extension;
608 }
609 } else {
610 fcml_int64_t temp_int = 0;
611 switch (integer->size) {
612 case 8:
613 temp_int = integer->int8;
614 break;
615 case 16:
616 temp_int = integer->int16;
617 break;
618 case 32:
619 temp_int = integer->int32;
620 break;
621 case 64:
622 temp_int = integer->int64;
623 break;
624 default:
625 error = FCML_CEH_GEC_INVALID_INPUT;
626 break;
627 }
628 if (!error) {
629 integer->int64 = 0;
630 switch (extension) {
631 case 8:
632 integer->int8 = (fcml_int8_t) temp_int;
633 break;
634 case 16:
635 integer->int16 = (fcml_int16_t) temp_int;
636 break;
637 case 32:
638 integer->int32 = (fcml_int32_t) temp_int;
639 break;
640 case 64:
641 integer->int64 = (fcml_int64_t) temp_int;
642 break;
643 }
644 integer->size = extension;
645 }
646 return error;
647 }
648 return error;
649 }
650
651 fcml_ceh_error fcml_fn_utils_conv_map_error(fcml_int error) {
652 fcml_ceh_error ceh_error = FCML_CEH_GEC_NO_ERROR;
653 switch (error) {
654 case FCML_COLL_ERROR_NO_ERROR:
655 ceh_error = FCML_CEH_GEC_NO_ERROR;
656 break;
657 case FCML_COLL_ERROR_OUT_OF_MEMORY:
658 ceh_error = FCML_CEH_GEC_OUT_OF_MEMORY;
659 break;
660 case FCML_COLL_ERROR_BAD_ARGS:
661 case FCML_COLL_ERROR_MAP_FULL:
662 // Should never happened in case of FCML.
663 FCML_TRACE_MSG("Map reached its maximal size or something "
664 "different related to map processing happened.");
665 ceh_error = FCML_CEH_GEC_INTERNAL_ERROR;
666 break;
667 }
668 return ceh_error;
669 }
670
671 void fcml_fn_utils_conv_gec_to_error_info(fcml_bool enabled,
672 fcml_st_ceh_error_container *error_container, fcml_ceh_error code) {
673 if (enabled) {
674 /* Prepare standard description of the error code,
675 but only if there is no other error on the list. */
676 fcml_st_ceh_error_info *current = error_container->errors;
677 while (current) {
678 if (current->level == FCML_EN_CEH_EL_ERROR) {
679 return;
680 }
681 current = current->next_error;
682 }
683
684 if (!fcml_fn_ceh_add_error(error_container,
685 fcml_fn_msg_get_message((fcml_en_msg_message_code) code), code,
686 FCML_EN_CEH_EL_ERROR)) {
687 FCML_TRACE_MSG("Out of memory, can not allocate space for error info.");
688 }
689 }
690 }
691
692 fcml_ceh_error fcml_fn_utils_prepare_entry_point(
693 fcml_st_entry_point *entry_point) {
694
695 /* Mode has to be set. */
696 if (entry_point->op_mode != FCML_OM_16_BIT
697 && entry_point->op_mode != FCML_OM_32_BIT
698 && entry_point->op_mode != FCML_OM_64_BIT) {
699 return FCML_CEH_GEC_INVALID_OPERATING_MODE;
700 }
701
702 /* 16 bit address size attribute is not supported in 64bit mode. */
703 if (entry_point->op_mode == FCML_OM_64_BIT&&
704 entry_point->address_size_attribute == FCML_DS_16) {
705 return FCML_CEH_GEC_INVALID_ADDRESS_SIZE;
706 }
707
708 /* Check if attributes are valid and set them to default values. */
709 if (!entry_point->address_size_attribute) {
710 switch (entry_point->op_mode) {
711 case FCML_OM_16_BIT:
712 entry_point->address_size_attribute = FCML_DS_16;
713 break;
714 case FCML_OM_32_BIT:
715 entry_point->address_size_attribute = FCML_DS_32;
716 break;
717 case FCML_OM_64_BIT:
718 entry_point->address_size_attribute = FCML_DS_64;
719 break;
720 }
721 }
722
723 if (!entry_point->operand_size_attribute) {
724 switch (entry_point->op_mode) {
725 case FCML_OM_16_BIT:
726 entry_point->operand_size_attribute = FCML_DS_16;
727 break;
728 case FCML_OM_32_BIT:
729 entry_point->operand_size_attribute = FCML_DS_32;
730 break;
731 case FCML_OM_64_BIT:
732 entry_point->operand_size_attribute = FCML_DS_32;
733 break;
734 }
735 }
736
737 /* Check if ASA is value.*/
738 fcml_usize asa = entry_point->address_size_attribute;
739 if (asa != FCML_DS_16 && asa != FCML_DS_32 && asa != FCML_DS_64) {
740 return FCML_CEH_GEC_INVALID_ADDRESS_SIZE;
741 }
742
743 /* Check if OSA is value.*/
744 fcml_usize osa = entry_point->operand_size_attribute;
745 if (osa != FCML_DS_16 && osa != FCML_DS_32 && osa != FCML_DS_64) {
746 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
747 }
748
749 return FCML_CEH_GEC_NO_ERROR;
750 }
751
752 fcml_bool fcml_fn_utils_is_disp_neg(const fcml_st_integer *disp) {
753 if (!disp->is_signed) {
754 return FCML_FALSE;
755 }
756 switch (disp->size) {
757 case FCML_DS_8:
758 return disp->int8 < 0;
759 case FCML_DS_16:
760 return disp->int16 < 0;
761 case FCML_DS_32:
762 return disp->int32 < 0;
763 case FCML_DS_64:
764 return disp->int64 < 0;
765 default:
766 return FCML_FALSE;
767 }
768 }
769
770 fcml_bool fcml_fn_utils_validate_vector_len(fcml_usize vector_length) {
771 switch (vector_length) {
772 case FCML_DS_128:
773 case FCML_DS_256:
774 case FCML_DS_512:
775 return FCML_TRUE;
776 }
777 return FCML_FALSE;
778 }
779
780 fcml_int64_t fcml_fn_utils_divide_integer(const fcml_st_integer *dividend,
781 fcml_st_integer *quotient, fcml_int32_t divisor) {
782 fcml_int64_t remainder;
783 quotient->is_signed = dividend->is_signed;
784 quotient->size = dividend->size;
785 switch (dividend->size) {
786 case FCML_DS_8:
787 if (dividend->is_signed) {
788 quotient->int8 = dividend->int8 / divisor;
789 remainder = dividend->int8 % divisor;
790 } else {
791 quotient->int8 = ((fcml_uint8_t) dividend->int8) / divisor;
792 remainder = ((fcml_uint8_t) dividend->int8) % divisor;
793 }
794 break;
795 case FCML_DS_16:
796 if (dividend->is_signed) {
797 quotient->int16 = dividend->int16 / divisor;
798 remainder = dividend->int16 % divisor;
799 } else {
800 quotient->int16 = ((fcml_uint16_t) dividend->int16) / divisor;
801 remainder = ((fcml_uint16_t) dividend->int16) % divisor;
802 }
803 break;
804 case FCML_DS_32:
805 if (dividend->is_signed) {
806 quotient->int32 = dividend->int32 / divisor;
807 remainder = dividend->int32 % divisor;
808 } else {
809 quotient->int32 = ((fcml_uint32_t) dividend->int32) / divisor;
810 remainder = ((fcml_uint32_t) dividend->int32) % divisor;
811 }
812 break;
813 case FCML_DS_64:
814 if (dividend->is_signed) {
815 quotient->int64 = dividend->int64 / divisor;
816 remainder = dividend->int64 % divisor;
817 } else {
818 quotient->int64 = ((fcml_uint64_t) dividend->int64) / divisor;
819 remainder = ((fcml_uint64_t) dividend->int64) % divisor;
820 }
821 break;
822 }
823 return remainder;
824 }
825