SayoriOS 0.3.3

endianess.c
1 #include "common.h"
2
3 uint8_t bit_flip_byte(uint8_t byte, int num_bits) {
4 uint8_t x = byte << (8 - num_bits);
5 return x | (byte >> num_bits);
6 }
7
8 uint16_t bit_flip_short(uint16_t short_int) {
9 uint32_t first_byte = *((uint8_t*)(&short_int));
10 uint32_t second_byte = *((uint8_t*)(&short_int) + 1);
11 return (first_byte << 8) | (second_byte);
12 }
13
14 uint32_t bit_flip_int(uint32_t long_int) {
15 uint32_t first_byte = *((uint8_t*)(&long_int));
16 uint32_t second_byte = *((uint8_t*)(&long_int) + 1);
17 uint32_t third_byte = *((uint8_t*)(&long_int) + 2);
18 uint32_t fourth_byte = *((uint8_t*)(&long_int) + 3);
19 return (first_byte << 24) | (second_byte << 16) | (third_byte << 8) | (fourth_byte);
20 }
21
22 uint8_t htonb(uint8_t byte, int num_bits) {
23 return bit_flip_byte(byte, num_bits);
24 }
25
26 uint8_t ntohb(uint8_t byte, int num_bits) {
27 return bit_flip_byte(byte, 8 - num_bits);
28 }
29
30 uint16_t htons(uint16_t hostshort) {
31 return bit_flip_short(hostshort);
32 }
33
34 uint32_t htonl(uint32_t hostlong) {
35 return bit_flip_int(hostlong);
36 }
37
38 uint16_t ntohs(uint16_t netshort) {
39 return bit_flip_short(netshort);
40 }
41
42 uint32_t ntohl(uint32_t netlong) {
43 return bit_flip_int(netlong);
44 }
common.h
Основные определения ядра