diff --git a/CMakeLists.txt b/CMakeLists.txt index 13b51a7..745e01a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,5 +8,5 @@ if (${isBigEndian}) endif() add_library(spb SHARED protobuf.c) -add_executable(test test.c) -target_link_libraries(test spb) \ No newline at end of file +add_executable(t test.c) +target_link_libraries(t spb) \ No newline at end of file diff --git a/protobuf.c b/protobuf.c index 4b24046..3610c9f 100644 --- a/protobuf.c +++ b/protobuf.c @@ -79,4 +79,31 @@ int set_pb(FILE* fp, uint8_t* items_type, uint64_t struct_len, void* target) { fwrite(this, data_len, 1, fp); } return i; -} \ No newline at end of file +} + +uint8_t first_set(uint64_t n) { + uint8_t i = 0; + while(!(n & 1)) { + n >>= 1; + i++; + } + return i; +} + +void align_struct(uint8_t* items_type, uint64_t items_cnt, uint64_t struct_size) { + uint64_t sum = 0; + uint8_t min = 255; + for(uint64_t i = 0; i < items_cnt; i++) { + sum += 1u << items_type[i]; + if(min > items_type[i]) min = items_type[i]; + } + while(sum < struct_size) { + uint8_t new_min = 255; + sum = 0; + for(uint64_t i = 0; i < items_cnt; i++) { + if(items_type[i] == min) items_type[i]++; + if(new_min > items_type[i]) new_min = items_type[i]; + sum += 1u << items_type[i]; + } + } +} diff --git a/simple_protobuf.h b/simple_protobuf.h index 851eff9..bdac106 100644 --- a/simple_protobuf.h +++ b/simple_protobuf.h @@ -11,33 +11,7 @@ typedef struct SIMPLE_PB SIMPLE_PB; SIMPLE_PB* get_pb(FILE* fp); int set_pb(FILE* fp, uint8_t* items_type, uint64_t struct_len, void* target); - -uint8_t first_set(uint64_t n) { - uint8_t i = 0; - while(!(n & 1)) { - n >>= 1; - i++; - } - return i; -} - -void align_struct(uint8_t* items_type, uint64_t items_cnt, uint64_t struct_size) { - uint64_t sum = 0; - uint64_t min_cnt = 0; - uint8_t min = 255; - for(uint64_t i = 0; i < items_cnt; i++) { - sum += 1u << items_type[i]; - if(min > items_type[i]) min = items_type[i]; - } - while(sum < struct_size) { - uint8_t new_min = 255; - sum = 0; - for(uint64_t i = 0; i < items_cnt; i++) { - if(items_type[i] == min) items_type[i]++; - if(new_min > items_type[i]) new_min = items_type[i]; - sum += 1u << items_type[i]; - } - } -} +uint8_t first_set(uint64_t n); +void align_struct(uint8_t* items_type, uint64_t items_cnt, uint64_t struct_size); #endif \ No newline at end of file