Home > database >  Why is flatbuffers output different from C in Python?
Why is flatbuffers output different from C in Python?

Time:05-23

I use the same protocol files, but I find that they have different output in Python and C . My protocol file:


namespace serial.proto.api.login;

table LoginReq {
    account:string; //账号
    passwd:string; //密码
    device:string;  //设备信息
    token:string;
}

table LoginRsp {
    account:string; //账号
    passwd:string;  //密码
    device:string;  //设备信息
    token:string;
}

table LogoutReq {
    account:string;
}

table LogoutRsp {
    account:string;
}

My python code:

builder = flatbuffers.Builder()

account = builder.CreateString('test')
paswd = builder.CreateString('test')
device = builder.CreateString('test')
token = builder.CreateString('test')
LoginReq.LoginReqStart(builder)
LoginReq.LoginReqAddPasswd(builder, paswd)
LoginReq.LoginReqAddToken(builder, token)
LoginReq.LoginReqAddDevice(builder, device)
LoginReq.LoginReqAddAccount(builder, account)
login = LoginReq.LoginReqEnd(builder)
builder.Finish(login)
buf = builder.Output()
print(buf)
with open("layer.bin1","wb") as f:
    f.write(buf)

enter image description here My C code:

flatbuffers::FlatBufferBuilder builder;
    auto account = builder.CreateString("test");
    auto device = builder.CreateString("test");
    auto passwd = builder.CreateString("test");
    auto token = builder.CreateString("test");
    auto l = CreateLoginReq(builder, account = account, passwd = passwd, device = device, token = token);
    builder.Finish(l);
    auto buf = builder.GetBufferPointer();
    flatbuffers::SaveFile("layer.bin", reinterpret_cast<char *>(buf), builder.GetSize(), true);

output:

md5 layer.bin
MD5 (layer.bin) = 496e5031dda0f754fb4462fadce9e975

CodePudding user response:

Flatbuffers generated by different implementations (i.e. generators) don't necessarily have the same binary layout, but can still be equivalent. It depends on how the implementation decide to write out the contents. So taking the hash of the binary is not going to tell you equivalence.

  • Related