diff --git a/CMakeLists.txt b/CMakeLists.txt index f744169..13b51a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,9 @@ project(simple-protobuf) include(TestBigEndian) test_big_endian(isBigEndian) if (${isBigEndian}) - add_library(spb SHARED protobuf_be.c) -else() - add_library(spb SHARED protobuf_le.c) + add_definitions(-DWORDS_BIGENDIAN) endif() +add_library(spb SHARED protobuf.c) add_executable(test test.c) target_link_libraries(test spb) \ No newline at end of file diff --git a/protobuf_le.c b/protobuf.c similarity index 89% rename from protobuf_le.c rename to protobuf.c index c8e0b83..e7a5ec8 100644 --- a/protobuf_le.c +++ b/protobuf.c @@ -19,9 +19,14 @@ static uint64_t read_num(FILE* fp) { static int write_num(FILE* fp, uint64_t n) { char* c = (char*)(&n); int i = 0; - while(*c) { - int ch = *c & 0x7f; - if(c[1]) ch |= 0x80; + while(n > 0) { + #ifdef WORDS_BIGENDIAN + int ch = c[7] & 0x7f; + if(c[6]) ch |= 0x80; + #else + int ch = *c & 0x7f; + if(c[1]) ch |= 0x80; + #endif fputc(ch, fp); n >>= 7; i++;