basic_types.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // definition of the function prototypes in basic_types.h
  2. // checks if a pointer is valid for read/write
  3. owl_bool owl_check_ptr(void * ptr, owl_umax size)
  4. {
  5. return (ptr != NULL && size != 0);
  6. }
  7. // print the bits of a byte in memory
  8. owl_umax owl_print_byte_bin(owl_byte * mem, owl_umax mem_size)
  9. {
  10. // check params
  11. if (owl_check_ptr(mem, mem_size) == owl_false)
  12. return 0;
  13. owl_byte tmp = 0x80;
  14. printf("0b");
  15. for (owl_umax i = 0; i < OWL_BYTE_BITS; i++) {
  16. printf("%u", (*mem & tmp) >> (OWL_BYTE_BITS - i - 1));
  17. tmp = tmp >> 1;
  18. }
  19. return sizeof(owl_byte);
  20. }
  21. // print byte contents in decimal
  22. owl_umax owl_print_byte_dec(owl_byte * mem, owl_umax mem_size)
  23. {
  24. // check params
  25. if (owl_check_ptr(mem, mem_size) == owl_false)
  26. return 0;
  27. printf("%u", *mem);
  28. return sizeof(owl_byte);
  29. }
  30. // print byte contents in hexadecimal
  31. owl_umax owl_print_byte_hex(owl_byte * mem, owl_umax mem_size)
  32. {
  33. // check params
  34. if (owl_check_ptr(mem, mem_size) == owl_false)
  35. return 0;
  36. printf("0x%02X", *mem);
  37. return sizeof(owl_byte);
  38. }
  39. // print char contents in decimal (signed integer 8 bits)
  40. owl_umax owl_print_char_dec(owl_byte * mem, owl_umax mem_size)
  41. {
  42. // check params
  43. if (owl_check_ptr(mem, mem_size) == owl_false)
  44. return 0;
  45. printf("%d", *mem);
  46. return sizeof(owl_char);
  47. }
  48. // print char contents as ascii
  49. owl_umax owl_print_char_char(owl_byte * mem, owl_umax mem_size)
  50. {
  51. // check params
  52. if (owl_check_ptr(mem, mem_size) == owl_false)
  53. return 0;
  54. if (*mem >= 0 && *mem <= 127)
  55. printf("%c", *mem);
  56. else
  57. printf("UNDEF_CHAR");
  58. return sizeof(owl_char);
  59. }
  60. // print umax integer as decimal
  61. owl_umax owl_print_umax_dec(owl_byte * mem, owl_umax mem_size)
  62. {
  63. // check params
  64. if (owl_check_ptr(mem, mem_size) == owl_false || mem_size < sizeof(owl_umax))
  65. return 0;
  66. printf("%llu", ((owl_umax *) mem)[0]);
  67. return sizeof(owl_umax);
  68. }
  69. // print umax integer as hexadecimal
  70. owl_umax owl_print_umax_hex(owl_byte * mem, owl_umax mem_size)
  71. {
  72. // check params
  73. if (owl_check_ptr(mem, mem_size) == owl_false || mem_size < sizeof(owl_umax))
  74. return 0;
  75. printf("0x%llX", ((owl_umax *) mem)[0]);
  76. return sizeof(owl_umax);
  77. }
  78. // print smax integer as decimal
  79. owl_umax owl_print_smax_dec(owl_byte * mem, owl_umax mem_size)
  80. {
  81. // check params
  82. if (owl_check_ptr(mem, mem_size) == owl_false || mem_size < sizeof(owl_smax))
  83. return 0;
  84. printf("%lld", ((owl_smax *) mem)[0]);
  85. return sizeof(owl_smax);
  86. }
  87. // print fmax number
  88. owl_umax owl_print_fmax(owl_byte * mem, owl_umax mem_size)
  89. {
  90. // check params
  91. if (owl_check_ptr(mem, mem_size) == owl_false || mem_size < sizeof(owl_fmax))
  92. return 0;
  93. printf("%lf", ((owl_fmax *) mem)[0]);
  94. return sizeof(owl_fmax);
  95. }
  96. // print bool (number)
  97. owl_umax owl_print_bool(owl_byte * mem, owl_umax mem_size)
  98. {
  99. // check params
  100. if (owl_check_ptr(mem, mem_size) == owl_false || mem_size < sizeof(owl_bool))
  101. return 0;
  102. if (*mem == 0)
  103. printf("false");
  104. else
  105. printf("true");
  106. return sizeof(owl_bool);
  107. }
  108. // to compare bytes
  109. owl_char owl_comp_byte(owl_byte * byte1, owl_byte * byte2)
  110. {
  111. // check params
  112. if (byte1 == NULL || byte2 == NULL)
  113. return owl_comp_err;
  114. // do the comparison
  115. if (*byte1 < *byte2)
  116. return owl_comp_less;
  117. else if (*byte1 > *byte2)
  118. return owl_comp_greater;
  119. return owl_comp_equal;
  120. }
  121. // to compare chars
  122. owl_char owl_comp_char(owl_byte * char1, owl_byte * char2)
  123. {
  124. // check params
  125. if (char1 == NULL || char2 == NULL)
  126. return owl_comp_err;
  127. // do the comparison
  128. if (*char1 < *char2)
  129. return owl_comp_less;
  130. else if (*char1 > *char2)
  131. return owl_comp_greater;
  132. return owl_comp_equal;
  133. }
  134. // to compare umaxes
  135. owl_char owl_comp_umax(owl_byte * umax1, owl_byte * umax2)
  136. {
  137. // check params
  138. if (umax1 == NULL || umax2 == NULL)
  139. return owl_comp_err;
  140. // do the comparison
  141. owl_umax * tmp1 = (void *) umax1,
  142. * tmp2 = (void *) umax2;
  143. if (*tmp1 < *tmp2)
  144. return owl_comp_less;
  145. else if (*tmp1 > *tmp2)
  146. return owl_comp_greater;
  147. return owl_comp_equal;
  148. }
  149. // to compare smaxes
  150. owl_char owl_comp_smax(owl_byte * smax1, owl_byte * smax2)
  151. {
  152. // check params
  153. if (smax1 == NULL || smax2 == NULL)
  154. return owl_comp_err;
  155. // do the comparison
  156. owl_smax * tmp1 = (void *) smax1,
  157. * tmp2 = (void *) smax2;
  158. if (*tmp1 < *tmp2)
  159. return owl_comp_less;
  160. else if (*tmp1 > *tmp2)
  161. return owl_comp_greater;
  162. return owl_comp_equal;
  163. }
  164. // to compare fmaxes
  165. owl_char owl_comp_fmax(owl_byte * fmax1, owl_byte * fmax2)
  166. {
  167. // check params
  168. if (fmax1 == NULL || fmax2 == NULL)
  169. return owl_comp_err;
  170. // do the comparison
  171. owl_fmax * tmp1 = (void *) fmax1,
  172. * tmp2 = (void *) fmax2;
  173. if (*tmp1 < *tmp2)
  174. return owl_comp_less;
  175. else if (*tmp1 > *tmp2)
  176. return owl_comp_greater;
  177. return owl_comp_equal;
  178. }
  179. // to compare bools
  180. owl_char owl_comp_bool(owl_byte * bool1, owl_byte * bool2)
  181. {
  182. // reuse owl_comp_byte
  183. return owl_comp_byte(bool1, bool2);
  184. }