decompiled_code:
__int64 __fastcall e3x_self_new(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
char *v3; // r12
__int64 v4; // rbx
unsigned __int8 v5; // r14
__int64 v6; // rax
__int64 (__fastcall *v7)(__int64, __int64); // rax
__int64 v8; // rax
__int64 result; // rax

v2 = a2;
if ( !a2 )
{
v2 = lob_linked();
if ( !v2 )
return 0LL;
}
v3 = (char *)calloc(0x20uLL, 1uLL);
if ( !v3 )
return 0LL;
v4 = 0LL;
v5 = 0;
do
{
v6 = e3x_cipher_sets[v4];
if ( v6 )
{
v7 = *(__int64 (__fastcall **)(__int64, __int64))(v6 + 32);
if ( v7 )
{
v8 = v7(v2, a1);
*(_QWORD *)&v3[v4 * 8 + 16] = v8;
if ( v8 )
{
++v5;
*(_QWORD *)&v3[v4 * 8] = lob_get_base32(v2, e3x_cipher_sets[v4] + 138LL);
}
}
}
++v4;
}
while ( v4 != 2 );
if ( v5 )
{
util_sys_log(7LL, "src/e3x/self.c", 35LL, "e3x_self_new", "self created with %d csids", v5);
result = (__int64)v3;
}
else
{
e3x_self_free(v3);
result = util_sys_log(
7LL,
"src/e3x/self.c",
32LL,
"e3x_self_new",
"self failed for %.*s",
*(_QWORD *)(v2 + 32),
*(_QWORD *)(v2 + 24));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
char *v3; // r12
__int64 v4; // rbx
unsigned __int8 v5; // r14
__int64 v6; // rax
__int64 (__fastcall *v7)(__int64, __int64); // rax
__int64 v8; // rax
__int64 result; // rax

v2 = a2;
if ( !a2 )
{
v2 = lob_linked();
if ( !v2 )
return 0LL;
}
v3 = (char *)calloc(0x20uLL, 1uLL);
if ( !v3 )
return 0LL;
v4 = 0LL;
v5 = 0;
do
{
v6 = e3x_cipher_sets[v4];
if ( v6 )
{
v7 = *(__int64 (__fastcall **)(__int64, __int64))(v6 + 32);
if ( v7 )
{
v8 = v7(v2, a1);
*(_QWORD *)&v3[v4 * 8 + 16] = v8;
if ( v8 )
{
++v5;
*(_QWORD *)&v3[v4 * 8] = lob_get_base32(v2, e3x_cipher_sets[v4] + 138LL);
}
}
}
++v4;
}
while ( v4 != 2 );
if ( v5 )
{
util_sys_log(7LL, "src/e3x/self.c", 35LL, "e3x_self_new", "self created with %d csids", v5);
result = (__int64)v3;
}
else
{
func0(v3);
result = util_sys_log(
7LL,
"src/e3x/self.c",
32LL,
"e3x_self_new",
"self failed for %.*s",
*(_QWORD *)(v2 + 32),
*(_QWORD *)(v2 + 24));
}
return result;
}

original_code:
e3x_self_t e3x_self_new(lob_t secrets, lob_t keys)
{
  uint8_t i, csids = 0;
  e3x_self_t self;
  if(!keys) keys = lob_linked(secrets);
  if(!keys) return ((void *)0);
  if(!(self = malloc(sizeof (struct e3x_self_struct)))) return ((void *)0);
  memset(self,0,sizeof (struct e3x_self_struct));
  for(i=0; i<2; i++)
  {
    if(!e3x_cipher_sets[i] || !e3x_cipher_sets[i]->local_new) continue;
    self->locals[i] = e3x_cipher_sets[i]->local_new(keys, secrets);
    if(!self->locals[i]) continue;
    self->keys[i] = lob_get_base32(keys, e3x_cipher_sets[i]->hex);
    csids++;
  }
  if(!csids)
  {
    e3x_self_free(self);
    return util_sys_log(7, "src/e3x/self.c", 32, __func__, "self failed for %.*s",keys->head_len,keys->head);
  }
  util_sys_log(7, "src/e3x/self.c", 35, __func__, "self created with %d csids",csids);
  return self;
}

canonical_original_code:
struct e3x_self_struct *e3x_self_new(struct lob_struct *secrets, struct lob_struct *keys)
{
  unsigned char i; unsigned char csids = 0;
  struct e3x_self_struct *self;
  if(!keys) keys = lob_linked(secrets);
  if(!keys) return ((void *)0);
  if(!(self = malloc(sizeof (struct e3x_self_struct)))) return ((void *)0);
  memset(self,0,sizeof (struct e3x_self_struct));
  for(i=0; i<2; i++)
  {
    if(!e3x_cipher_sets[i] || !e3x_cipher_sets[i]->local_new) continue;
    self->locals[i] = e3x_cipher_sets[i]->local_new(keys, secrets);
    if(!self->locals[i]) continue;
    self->keys[i] = lob_get_base32(keys, e3x_cipher_sets[i]->hex);
    csids++;
  }
  if(!csids)
  {
    e3x_self_free(self);
    return util_sys_log(7, "src/e3x/self.c", 32, __func__, "self failed for %.*s",keys->head_len,keys->head);
  }
  util_sys_log(7, "src/e3x/self.c", 35, __func__, "self created with %d csids",csids);
  return self;
}

variable_types:
secrets
struct lob_struct *

variable_types:
keys
struct lob_struct *

variable_types:
csids
unsigned char

return_type: struct e3x_self_struct { struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys[2]; void *locals[2]; } *


repo: telehash/telehash-c

decompiled_code:
__int64 __fastcall e3x_self_decrypt(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rax

if ( !a1 || !a2 )
return util_sys_log(7LL, "src/e3x/self.c", 61LL, "e3x_self_decrypt", "bad args");
if ( *(_QWORD *)(a2 + 32) != 1LL )
return util_sys_log(7LL, "src/e3x/self.c", 62LL, "e3x_self_decrypt", "invalid message");
v3 = e3x_cipher_set(**(unsigned __int8 **)(a2 + 24), 0LL);
if ( v3 )
result = (*(__int64 (__fastcall **)(_QWORD, __int64))(v3 + 48))(
*(_QWORD *)(a1 + 8LL * *(unsigned __int8 *)(v3 + 136) + 16),
a2);
else
result = util_sys_log(
7LL,
"src/e3x/self.c",
64LL,
"e3x_self_decrypt",
"no cipherset %2x",
**(unsigned __int8 **)(a2 + 24));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rax

if ( !a1 || !a2 )
return util_sys_log(7LL, "src/e3x/self.c", 61LL, "e3x_self_decrypt", "bad args");
if ( *(_QWORD *)(a2 + 32) != 1LL )
return util_sys_log(7LL, "src/e3x/self.c", 62LL, "e3x_self_decrypt", "invalid message");
v3 = e3x_cipher_set(**(unsigned __int8 **)(a2 + 24), 0LL);
if ( v3 )
result = (*(__int64 (__fastcall **)(_QWORD, __int64))(v3 + 48))(
*(_QWORD *)(a1 + 8LL * *(unsigned __int8 *)(v3 + 136) + 16),
a2);
else
result = util_sys_log(
7LL,
"src/e3x/self.c",
64LL,
"e3x_self_decrypt",
"no cipherset %2x",
**(unsigned __int8 **)(a2 + 24));
return result;
}

original_code:
lob_t e3x_self_decrypt(e3x_self_t self, lob_t message)
{
  e3x_cipher_t cs;
  if(!self || !message) return util_sys_log(7, "src/e3x/self.c", 61, __func__, "bad args");
  if(message->head_len != 1) return util_sys_log(7, "src/e3x/self.c", 62, __func__, "invalid message");
  cs = e3x_cipher_set(message->head[0],((void *)0));
  if(!cs) return util_sys_log(7, "src/e3x/self.c", 64, __func__, "no cipherset %2x",message->head[0]);
  return cs->local_decrypt(self->locals[cs->id],message);
}

canonical_original_code:
struct lob_struct *e3x_self_decrypt(struct e3x_self_struct *self, struct lob_struct *message)
{
  struct e3x_cipher_struct *cs;
  if(!self || !message) return util_sys_log(7, "src/e3x/self.c", 61, __func__, "bad args");
  if(message->head_len != 1) return util_sys_log(7, "src/e3x/self.c", 62, __func__, "invalid message");
  cs = e3x_cipher_set(message->head[0],((void *)0));
  if(!cs) return util_sys_log(7, "src/e3x/self.c", 64, __func__, "no cipherset %2x",message->head[0]);
  return cs->local_decrypt(self->locals[cs->id],message);
}

variable_types: struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *

variable_types:
message
struct lob_struct *

variable_types: struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *

user_defined_types: struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; }


decompiled_code:
__int64 __fastcall e3x_self_sign(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v4; // r14
const char *v7; // rbx
__int64 v8; // rax
__int64 (__fastcall *v9)(__int64, __int64, __int64, __int64); // r8

v4 = a1;
v7 = (const char *)lob_get(a2, "alg");
if ( a4 == 0 || a3 == 0 || !v7 )
return util_sys_log(7LL, "src/e3x/self.c", 74LL, "e3x_self_sign", "bad args");
v8 = e3x_cipher_set(0LL, v7);
if ( !v8 )
return util_sys_log(7LL, "src/e3x/self.c", 76LL, "e3x_self_sign", "no signing support for %s", v7);
v9 = *(__int64 (__fastcall **)(__int64, __int64, __int64, __int64))(v8 + 56);
if ( !v9 )
return util_sys_log(7LL, "src/e3x/self.c", 76LL, "e3x_self_sign", "no signing support for %s", v7);
if ( a1 )
v4 = *(_QWORD *)(a1 + 8LL * *(unsigned __int8 *)(v8 + 136) + 16);
return v9(v4, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v4; // r14
const char *v7; // rbx
__int64 v8; // rax
__int64 (__fastcall *v9)(__int64, __int64, __int64, __int64); // r8

v4 = a1;
v7 = (const char *)lob_get(a2, "alg");
if ( a4 == 0 || a3 == 0 || !v7 )
return util_sys_log(7LL, "src/e3x/self.c", 74LL, "e3x_self_sign", "bad args");
v8 = e3x_cipher_set(0LL, v7);
if ( !v8 )
return util_sys_log(7LL, "src/e3x/self.c", 76LL, "e3x_self_sign", "no signing support for %s", v7);
v9 = *(__int64 (__fastcall **)(__int64, __int64, __int64, __int64))(v8 + 56);
if ( !v9 )
return util_sys_log(7LL, "src/e3x/self.c", 76LL, "e3x_self_sign", "no signing support for %s", v7);
if ( a1 )
v4 = *(_QWORD *)(a1 + 8LL * *(unsigned __int8 *)(v8 + 136) + 16);
return v9(v4, a2, a3, a4);
}

original_code:
lob_t e3x_self_sign(e3x_self_t self, lob_t args, uint8_t *data, size_t len)
{
  void* local = ((void *)0);
  e3x_cipher_t cs = ((void *)0);
  char *alg = lob_get(args,"alg");
  if(!data || !len || !alg) return util_sys_log(7, "src/e3x/self.c", 74, __func__, "bad args");
  cs = e3x_cipher_set(0,alg);
  if(!cs || !cs->local_sign) return util_sys_log(7, "src/e3x/self.c", 76, __func__, "no signing support for %s",alg);
  if(self) local = self->locals[cs->id];
  return cs->local_sign(local,args,data,len);
}

canonical_original_code:
struct lob_struct *e3x_self_sign(struct e3x_self_struct *self, struct lob_struct *args, unsigned char *data, unsigned long len)
{
  void *local = ((void *)0);
  struct e3x_cipher_struct *cs = ((void *)0);
  char *alg = lob_get(args,"alg");
  if(!data || !len || !alg) return util_sys_log(7, "src/e3x/self.c", 74, __func__, "bad args");
  cs = e3x_cipher_set(0,alg);
  if(!cs || !cs->local_sign) return util_sys_log(7, "src/e3x/self.c", 76, __func__, "no signing support for %s",alg);
  if(self) local = self->locals[cs->id];
  return cs->local_sign(local,args,data,len);
}

variable_types:
args
struct lob_struct *

variable_types:
data
unsigned char *

variable_types:
len
unsigned long

variable_types:
local
void *

variable_types:
alg
char *

return_type: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *

user_defined_types: struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; }


decompiled_code:
__int64 __fastcall net_send(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1;
}

original_code:
link_t net_send(link_t link, lob_t packet, void *arg)
{
  return link;
}

canonical_original_code:
struct link_struct *net_send(struct link_struct *link, struct lob_struct *packet, void *arg)
{
  return link;
}

variable_types:
link
struct link_struct *

variable_types:
packet
struct lob_struct *

variable_types:
arg
void *

return_type: struct link_struct { struct hashname_struct { unsigned char bin[32]; } *id; struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *x; struct mesh_struct { struct hashname_struct *id; struct lob_struct *keys; struct e3x_self_struct *self; void *on; unsigned int state; struct link_struct *links; } *mesh; struct lob_struct *key; struct chan_struct { struct link_struct *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 } state; } *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; } *

user_defined_types: struct link_struct { struct hashname_struct *id; struct e3x_exchange_struct *x; struct mesh_struct *mesh; struct lob_struct *key; struct chan_struct *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; }

user_defined_types: struct e3x_exchange_struct { struct e3x_cipher_struct *cs; struct e3x_self_struct *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; }

user_defined_types: struct chan_struct { struct link_struct *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states state; }


canonical_original_code:
struct link_struct *net_test(struct link_struct *link, struct lob_struct *path)
{
  do { if(!(path)) { fprintf(stderr, "----------------------------------------------\n" "%s:%d: test FAILED:\nFailed test: %s\n" "----------------------------------------------\n", "mesh_core.c", 11, "path"); ; exit(1); }else{ fprintf(stderr, "TEST   %s:%d: passed %d tests\n", "mesh_core.c", 11, ++unit_test_pass); } } while(0);
  return link_pipe(link, net_send, ((void *)0));
}

variable_types:
path
struct lob_struct *

user_defined_types: struct hashname_struct { unsigned char bin[32]; }

user_defined_types: struct mesh_struct { struct hashname_struct *id; struct lob_struct *keys; struct e3x_self_struct *self; void *on; unsigned int state; struct link_struct *links; }

user_defined_types: enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 }

user_defined_types: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
_QWORD *v4; // rbp
__int64 v5; // r12
__int64 v6; // r13
__int64 v7; // rax
__int64 v8; // r12
__int64 v9; // r12
const char *v10; // rax
__int64 v11; // rax
__int64 v12; // r13
unsigned int v13; // eax
__int64 v14; // rax
__int64 v15; // rax
__int64 v16; // r12
__int64 v17; // rax
const char *v18; // rax
__int64 v19; // rax
__int64 v20; // rax
const char *v21; // rax
__int64 v22; // rax
__int64 v23; // rax
const char *v24; // rax
const char *v26; // r8
__int64 v27; // rcx
const char *v28; // r8
__int64 v29; // rcx

v3 = mesh_new(argc, argv, envp);
if ( !v3 )
{
v26 = "mesh";
v27 = 18LL;
goto LABEL_19;
}
v4 = (_QWORD *)v3;
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 18LL, (unsigned int)++unit_test_pass);
v5 = mesh_generate(v4);
if ( !v5 )
{
v26 = "secrets";
v27 = 20LL;
LABEL_19:
fprintf(
stderr,
"----------------------------------------------\n"
"%s:%d: test FAILED:\n"
"Failed test: %s\n"
"----------------------------------------------\n",
"mesh_core.c",
v27,
v26);
exit(1);
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 20LL, (unsigned int)++unit_test_pass);
if ( !v4[4] )
{
v26 = "mesh->self";
v27 = 21LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 21LL, (unsigned int)++unit_test_pass);
if ( !*v4 )
{
v26 = "mesh->id";
v27 = 22LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 22LL, (unsigned int)++unit_test_pass);
lob_free(v5);
v6 = e3x_generate();
v7 = lob_linked(v6);
v8 = hashname_vkeys(v7);
if ( !v8 )
{
v26 = "hnB";
v27 = 27LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 27LL, (unsigned int)++unit_test_pass);
v9 = link_get(v4, v8);
if ( !v9 )
{
v26 = "link";
v27 = 29LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 29LL, (unsigned int)++unit_test_pass);
v10 = (const char *)hashname_char(*(_QWORD *)v9);
if ( strlen(v10) != 52 )
{
v26 = "strlen(hashname_char(link->id)) == 52";
v27 = 30LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 30LL, (unsigned int)++unit_test_pass);
if ( *(_BYTE *)(v9 + 80) != 1 )
{
v26 = "link->csid == 0x01";
v27 = 31LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 31LL, (unsigned int)++unit_test_pass);
v11 = lob_linked(v6);
if ( v9 != link_get_keys(v4, v11) )
{
v26 = "link_get_keys(mesh,lob_linked(idB)) == link";
v27 = 33LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 33LL, (unsigned int)++unit_test_pass);
if ( *(_BYTE *)(v9 + 80) <= 1u )
{
v26 = "link->csid > 0x01";
v27 = 34LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 34LL, (unsigned int)++unit_test_pass);
if ( !*(_QWORD *)(v9 + 8) )
{
v26 = "link->x";
v27 = 35LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 35LL, (unsigned int)++unit_test_pass);
lob_free(v6);
v12 = lob_new();
lob_set(v12, "type", "test");
v13 = e3x_exchange_cid(*(_QWORD *)(v9 + 8), 0LL);
lob_set_int(v12, "c", v13);
if ( !link_chan(v9, v12) )
{
v28 = "chan";
v29 = 42LL;
LABEL_21:
fprintf(
stderr,
"----------------------------------------------\n"
"%s:%d: test FAILED:\n"
"Failed test: %s\n"
"----------------------------------------------\n",
"mesh_core.c",
v29,
v28);
exit(1);
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 42LL, (unsigned int)++unit_test_pass);
lob_free(v12);
mesh_on_path(v4, "test", net_test);
v14 = lob_new();
v15 = lob_set(v14, "type", "test");
v16 = mesh_path(v4, v9, v15);
if ( !v16 )
{
v26 = "link";
v27 = 47LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 47LL, (unsigned int)++unit_test_pass);
v17 = mesh_json(v4);
v18 = (const char *)lob_json(v17);
if ( strlen(v18) <= 0xA )
{
v26 = "strlen(lob_json(mesh_json(mesh))) > 10";
v27 = 49LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 49LL, (unsigned int)++unit_test_pass);
v19 = mesh_links(v4);
v20 = lob_array(v19);
v21 = (const char *)lob_json(v20);
util_sys_log(7LL, "mesh_core.c", 50LL, "main", "json %s", v21);
v22 = mesh_links(v4);
v23 = lob_array(v22);
v24 = (const char *)lob_json(v23);
if ( strlen(v24) <= 0xA )
{
v26 = "strlen(lob_json(lob_array(mesh_links(mesh)))) > 10";
v27 = 51LL;
goto LABEL_19;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 51LL, (unsigned int)++unit_test_pass);
if ( !mesh_process(v4, 1LL) )
{
v28 = "mesh_process(mesh, 1)";
v29 = 53LL;
goto LABEL_21;
}
fprintf(stderr, "TEST %s:%d: passed %d tests\n", "mesh_core.c", 53LL, (unsigned int)++unit_test_pass);
link_free(v16);
mesh_free(v4);
return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct mesh_struct { struct hashname_struct { unsigned char bin[32]; } *id; struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *on; unsigned int state; struct link_struct { struct hashname_struct *id; struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *x; struct mesh_struct *mesh; struct lob_struct *key; struct chan_struct { struct link_struct *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 } state; } *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; } *links; } *

variable_types: struct link_struct { struct hashname_struct { unsigned char bin[32]; } *id; struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *x; struct mesh_struct { struct hashname_struct *id; struct lob_struct *keys; struct e3x_self_struct *self; void *on; unsigned int state; struct link_struct *links; } *mesh; struct lob_struct *key; struct chan_struct { struct link_struct *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 } state; } *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; } *

variable_types: struct chan_struct { struct link_struct { struct hashname_struct { unsigned char bin[32]; } *id; struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *x; struct mesh_struct { struct hashname_struct *id; struct lob_struct *keys; struct e3x_self_struct *self; void *on; unsigned int state; struct link_struct *links; } *mesh; struct lob_struct *key; struct chan_struct *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; } *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 } state; } *

return_type: int


decompiled_code:
time_t util_sys_seconds()
{
return time(0LL);
}

canonical_decompiled_code:
time_t func2()
{
return time(0LL);
}

original_code:
at_t util_sys_seconds()
{
  return (at_t)time(0);
}

canonical_original_code:
unsigned int util_sys_seconds()
{
  return (unsigned int)time(0);
}

return_type: unsigned int


decompiled_code:
unsigned __int64 __fastcall util_sys_ms(__int64 a1)
{
unsigned __int64 result; // rax
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v2, 0LL);
result = 0LL;
if ( v2.tv_sec >= a1 )
result = 1000 * (v2.tv_sec - a1) + v2.tv_usec / 0x3E8uLL;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(__int64 a1)
{
unsigned __int64 result; // rax
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v2, 0LL);
result = 0LL;
if ( v2.tv_sec >= a1 )
result = 1000 * (v2.tv_sec - a1) + v2.tv_usec / 0x3E8uLL;
return result;
}

original_code:
unsigned long long util_sys_ms(long epoch)
{
  struct timeval tv;
  gettimeofday(&tv, ((void *)0));
  if(epoch > tv.tv_sec) return 0;
  return (unsigned long long)(tv.tv_sec - epoch) * 1000 + (unsigned long long)(tv.tv_usec) / 1000;
}

variable_types: epoch long

variable_types: struct timeval { long tv_sec; long tv_usec; }

return_type: unsigned long long


decompiled_code:
__int16 __fastcall util_sys_short(__int16 a1)
{
return _bswap_16(a1);
}

canonical_decompiled_code:
__int16 __fastcall func4(__int16 a1)
{
return func0(a1);
}

original_code:
unsigned short util_sys_short(unsigned short x)
{
  return ntohs(x);
}

variable_types:
x
unsigned short

return_type: unsigned short


decompiled_code:
__int64 __fastcall util_sys_long(unsigned int a1)
{
return (unsigned int)_bswap_32(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
return (unsigned int)func1(a1);
}

original_code:
unsigned long util_sys_long(unsigned long x)
{
  return ntohl(x);
}

variable_types:
x
unsigned long

return_type: unsigned long


decompiled_code:
void util_sys_random_init()
{
__pid_t v0; // eax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
v0 = getpid();
srandom((v0 << 16) ^ LODWORD(v1.tv_sec) ^ LODWORD(v1.tv_usec));
}

canonical_decompiled_code:
void func6()
{
__pid_t v0; // eax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
v0 = getpid();
srandom((v0 << 16) ^ LODWORD(v1.tv_sec) ^ LODWORD(v1.tv_usec));
}

original_code:
void util_sys_random_init(void)
{
  struct timeval tv;
  unsigned int seed;
  gettimeofday(&tv, ((void *)0));
  seed = ((unsigned int)getpid() << 16) ^ (unsigned int)tv.tv_sec ^ (unsigned int)tv.tv_usec;
  srandom(seed);
}

canonical_original_code:
void util_sys_random_init()
{
  struct timeval tv;
  unsigned int seed;
  gettimeofday(&tv, ((void *)0));
  seed = ((unsigned int)getpid() << 16) ^ (unsigned int)tv.tv_sec ^ (unsigned int)tv.tv_usec;
  srandom(seed);
}

variable_types:
seed
unsigned int

return_type: void

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
// attributes: thunk
__int64 util_sys_random(void)
{
return random();
}

canonical_decompiled_code:
__int64 func7(void)
{
return random();
}

original_code:
long util_sys_random(void)
{
  return random();
}

canonical_original_code:
long util_sys_random()
{
  return random();
}

return_type: long


decompiled_code:
__int64 util_sys_log(unsigned __int8 a1, const char *a2, unsigned int a3, const char *a4, const char *a5, ...)
{
const char *v7; // r14
gcc_va_list arg; // [rsp+8h] [rbp-1F0h] BYREF
char s[256]; // [rsp+20h] [rbp-1D8h] BYREF

if ( logging )
{
v7 = "?????? ";
if ( a1 <= 8u )
v7 = CSWTCH_13[a1];
va_start(arg, a5);
vsnprintf(s, 0x100uLL, a5, arg);
fprintf(stderr, "%s%s:%d %s() %s\n", v7, a2, a3, a4, s);
fflush(stderr);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func8(unsigned __int8 a1, const char *a2, unsigned int a3, const char *a4, const char *a5, ...)
{
const char *v7; // r14
gcc_va_list arg; // [rsp+8h] [rbp-1F0h] BYREF
char s[256]; // [rsp+20h] [rbp-1D8h] BYREF

if ( logging )
{
v7 = "?????? ";
if ( a1 <= 8u )
v7 = CSWTCH_13[a1];
va_start(arg, a5);
vsnprintf(s, 0x100uLL, a5, arg);
fprintf(stderr, "%s%s:%d %s() %s\n", v7, a2, a3, a4, s);
fflush(stderr);
}
return 0LL;
}

original_code:
void *util_sys_log(uint8_t level, const char *file, int line, const char *function, const char * format, ...)
{
  char buffer[256];
  va_list args;
  if(!_logging) return ((void *)0);
  char *lstr = ((void *)0);
  switch(level)
  {
    case 0: lstr = "EMERG  "; break;
    case 1: lstr = "ALERT  "; break;
    case 2: lstr = "CRIT   "; break;
    case 3: lstr = "ERROR  "; break;
    case 4: lstr = "WARN   "; break;
    case 5: lstr = "NOTICE "; break;
    case 6: lstr = "INFO   "; break;
    case 7: lstr = "DEBUG  "; break;
    case 8: lstr = "CRAZY  "; break;
    default: lstr = "?????? "; break;
  }
  __builtin_va_start(args,format);
  vsnprintf (buffer, 256, format, args);
  fprintf(stderr,"%s%s:%d %s() %s\n",lstr,file, line, function, buffer);
  fflush(stderr);
  __builtin_va_end(args);
  return ((void *)0);
}

canonical_original_code:
void *util_sys_log(unsigned char level, char *file, int line, char *function, char *format, ...)
{
  char buffer[256];
  __builtin_va_list args;
  if(!_logging) return ((void *)0);
  char *lstr = ((void *)0);
  switch(level)
  {
    case 0: lstr = "EMERG  "; break;
    case 1: lstr = "ALERT  "; break;
    case 2: lstr = "CRIT   "; break;
    case 3: lstr = "ERROR  "; break;
    case 4: lstr = "WARN   "; break;
    case 5: lstr = "NOTICE "; break;
    case 6: lstr = "INFO   "; break;
    case 7: lstr = "DEBUG  "; break;
    case 8: lstr = "CRAZY  "; break;
    default: lstr = "?????? "; break;
  }
  __builtin_va_start(args,format);
  vsnprintf (buffer, 256, format, args);
  fprintf(stderr,"%s%s:%d %s() %s\n",lstr,file, line, function, buffer);
  fflush(stderr);
  __builtin_va_end(args);
  return ((void *)0);
}

variable_types:
level
unsigned char

variable_types:
file
char *

variable_types: line int

variable_types:
function
char *

variable_types:
format
char *

variable_types:
buffer
char [256]

variable_types:
lstr
char *

return_type: void *


decompiled_code:
__int64 __fastcall util_sys_logging(int a1)
{
if ( a1 < 0 )
a1 = logging ^ 1;
logging = a1;
return util_sys_log(7u, "src/unix/util_sys.c", 0x43u, "util_sys_logging", "log output enabled");
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
if ( a1 < 0 )
a1 = logging ^ 1;
logging = a1;
return func8(7u, "src/unix/util_sys.c", 0x43u, "util_sys_logging", "log output enabled");
}

original_code:
void util_sys_logging(int enabled)
{
  if(enabled < 0)
  {
    _logging ^= 1;
  }else{
    _logging = enabled;
  }
  util_sys_log(7, "src/unix/util_sys.c", 67, __func__, "log output enabled");
}

variable_types: enabled int


decompiled_code:
__int64 __fastcall e3x_init(__int64 a1)
{
__int64 result; // rax

result = 0LL;
if ( !initialized )
{
util_sys_random_init();
result = e3x_cipher_init(a1);
if ( !(_BYTE)result )
initialized = 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax

result = 0LL;
if ( !initialized )
{
util_sys_random_init();
result = e3x_cipher_init(a1);
if ( !(_BYTE)result )
initialized = 1;
}
return result;
}

original_code:
uint8_t e3x_init(lob_t options)
{
  uint8_t err;
  if(_initialized) return 0;
  util_sys_random_init();
  err = e3x_cipher_init(options);
  if(err) return err;
  _initialized = 1;
  return 0;
}

canonical_original_code:
unsigned char e3x_init(struct lob_struct *options)
{
  unsigned char err;
  if(_initialized) return 0;
  util_sys_random_init();
  err = e3x_cipher_init(options);
  if(err) return err;
  _initialized = 1;
  return 0;
}

variable_types: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *

variable_types:
err
unsigned char

return_type: unsigned char


original_code:
uint8_t *e3x_err(void)
{
  uint8_t i;
  uint8_t *err = ((void *)0);
  for(i=0; i<2; i++)
  {
    if(e3x_cipher_sets[i] && e3x_cipher_sets[i]->err) err = e3x_cipher_sets[i]->err();
    if(err) return err;
  }
  return err;
}

canonical_original_code:
unsigned char *e3x_err()
{
  unsigned char i;
  unsigned char *err = ((void *)0);
  for(i=0; i<2; i++)
  {
    if(e3x_cipher_sets[i] && e3x_cipher_sets[i]->err) err = e3x_cipher_sets[i]->err();
    if(err) return err;
  }
  return err;
}

variable_types:
i
unsigned char

variable_types:
err
unsigned char *

return_type: unsigned char *


decompiled_code:
__int64 e3x_generate()
{
__int64 v0; // rbx
__int64 v1; // rax
char v2; // dl
__int64 v3; // r12
__int64 v4; // rax
__int64 v5; // rdx
char v6; // bp
__int64 (__fastcall *v7)(__int64, __int64); // rdx
__int64 result; // rax

v0 = lob_new();
v1 = lob_chain(v0);
v2 = 0;
v3 = v1;
v4 = 0LL;
do
{
if ( !v2 )
{
while ( 1 )
{
v5 = e3x_cipher_sets[v4];
v6 = v4;
if ( v5 )
{
v7 = *(__int64 (__fastcall **)(__int64, __int64))(v5 + 24);
if ( v7 )
{
v2 = v7(v0, v3);
goto LABEL_6;
}
if ( (_BYTE)v4 == 1 )
goto LABEL_8;
}
else if ( (_BYTE)v4 == 1 )
{
goto LABEL_8;
}
++v4;
}
}
v6 = v4;
LABEL_6:
v4 = 1LL;
}
while ( v6 != 1 );
if ( v2 )
result = lob_free(v3);
else
LABEL_8:
result = v3;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rbx
__int64 v1; // rax
char v2; // dl
__int64 v3; // r12
__int64 v4; // rax
__int64 v5; // rdx
char v6; // bp
__int64 (__fastcall *v7)(__int64, __int64); // rdx
__int64 result; // rax

v0 = lob_new();
v1 = lob_chain(v0);
v2 = 0;
v3 = v1;
v4 = 0LL;
do
{
if ( !v2 )
{
while ( 1 )
{
v5 = e3x_cipher_sets[v4];
v6 = v4;
if ( v5 )
{
v7 = *(__int64 (__fastcall **)(__int64, __int64))(v5 + 24);
if ( v7 )
{
v2 = v7(v0, v3);
goto LABEL_6;
}
if ( (_BYTE)v4 == 1 )
goto LABEL_8;
}
else if ( (_BYTE)v4 == 1 )
{
goto LABEL_8;
}
++v4;
}
}
v6 = v4;
LABEL_6:
v4 = 1LL;
}
while ( v6 != 1 );
if ( v2 )
result = lob_free(v3);
else
LABEL_8:
result = v3;
return result;
}

original_code:
lob_t e3x_generate(void)
{
  uint8_t i, err = 0;
  lob_t keys, secrets;
  keys = lob_new();
  secrets = lob_chain(keys);
  for(err=i=0; i<2; i++)
  {
    if(err || !e3x_cipher_sets[i] || !e3x_cipher_sets[i]->generate) continue;
    err = e3x_cipher_sets[i]->generate(keys, secrets);
  }
  if(err) return lob_free(secrets);
  return secrets;
}

canonical_original_code:
struct lob_struct *e3x_generate()
{
  unsigned char i; unsigned char err = 0;
  struct lob_struct *keys; struct lob_struct *secrets;
  keys = lob_new();
  secrets = lob_chain(keys);
  for(err=i=0; i<2; i++)
  {
    if(err || !e3x_cipher_sets[i] || !e3x_cipher_sets[i]->generate) continue;
    err = e3x_cipher_sets[i]->generate(keys, secrets);
  }
  if(err) return lob_free(secrets);
  return secrets;
}


decompiled_code:
void __fastcall e3x_random(void *a1)
{
frandom = a1;
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
frandom = a1;
}

original_code:
void e3x_random(uint8_t (*frand)(void))
{
  frandom = frand;
}

canonical_original_code:
void e3x_random(unsigned char (*frand)())
{
  frandom = frand;
}

variable_types:
frand
unsigned char (*)()


decompiled_code:
__int64 __fastcall e3x_rand(_BYTE *a1, __int64 a2)
{
_BYTE *v3; // rbx

if ( a1 && a2 )
{
if ( e3x_cipher_default && *e3x_cipher_default )
return ((__int64 (*)(void))*e3x_cipher_default)();
v3 = a1;
do
*v3++ = frandom();
while ( v3 != &a1[a2] );
}
return (__int64)a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1, __int64 a2)
{
_BYTE *v3; // rbx

if ( a1 && a2 )
{
if ( e3x_cipher_default && *e3x_cipher_default )
return ((__int64 (*)(void))*e3x_cipher_default)();
v3 = a1;
do
*v3++ = frandom();
while ( v3 != &a1[a2] );
}
return (__int64)a1;
}

original_code:
uint8_t *e3x_rand(uint8_t *bytes, size_t len)
{
  uint8_t *x = bytes;
  if(!bytes || !len) return bytes;
  if(e3x_cipher_default && e3x_cipher_default->rand) return e3x_cipher_default->rand(bytes, len);
  while(len-- > 0)
  {
    *x = frandom();
    x++;
  }
  return bytes;
}

canonical_original_code:
unsigned char *e3x_rand(unsigned char *bytes, unsigned long len)
{
  unsigned char *x = bytes;
  if(!bytes || !len) return bytes;
  if(e3x_cipher_default && e3x_cipher_default->rand) return e3x_cipher_default->rand(bytes, len);
  while(len-- > 0)
  {
    *x = frandom();
    x++;
  }
  return bytes;
}

variable_types:
bytes
unsigned char *

variable_types:
len
unsigned long

variable_types:
x
unsigned char *


decompiled_code:
__int64 __fastcall e3x_hash(__int64 a1, __int64 a2, _OWORD *a3)
{
if ( a1 && a3 )
{
if ( e3x_cipher_default )
return (*(__int64 (**)(void))(e3x_cipher_default + 8LL))();
util_sys_log(7LL, "src/e3x/e3x.c", 80LL, "e3x_hash", "e3x not initialized, no cipher_set");
*a3 = 0LL;
a3[1] = 0LL;
}
return (__int64)a3;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, _OWORD *a3)
{
if ( a1 && a3 )
{
if ( e3x_cipher_default )
return (*(__int64 (**)(void))(e3x_cipher_default + 8LL))();
util_sys_log(7LL, "src/e3x/e3x.c", 80LL, "e3x_hash", "e3x not initialized, no cipher_set");
*a3 = 0LL;
a3[1] = 0LL;
}
return (__int64)a3;
}

original_code:
uint8_t *e3x_hash(uint8_t *in, size_t len, uint8_t *out32)
{
  if(!in || !out32) return out32;
  if(!e3x_cipher_default)
  {
    util_sys_log(7, "src/e3x/e3x.c", 80, __func__, "e3x not initialized, no cipher_set");
    memset(out32,0,32);
    return out32;
  }
  return e3x_cipher_default->hash(in, len, out32);
}

canonical_original_code:
unsigned char *e3x_hash(unsigned char *in, unsigned long len, unsigned char *out32)
{
  if(!in || !out32) return out32;
  if(!e3x_cipher_default)
  {
    util_sys_log(7, "src/e3x/e3x.c", 80, __func__, "e3x not initialized, no cipher_set");
    memset(out32,0,32);
    return out32;
  }
  return e3x_cipher_default->hash(in, len, out32);
}

variable_types:
in
unsigned char *


decompiled_code:
__int64 __fastcall util_links(__int64 a1, const char *a2)
{
__int64 result; // rax

result = util_fjson(a2);
if ( result )
{
lob_free(result);
result = a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
__int64 result; // rax

result = func0(a2);
if ( result )
{
lob_free(result);
result = a1;
}
return result;
}

original_code:
mesh_t util_links(mesh_t mesh, char *file)
{
  lob_t links = util_fjson(file);
  if(!links) return ((void *)0);
  lob_free(links);
  return mesh;
}

canonical_original_code:
struct mesh_struct *util_links(struct mesh_struct *mesh, char *file)
{
  struct lob_struct *links = util_fjson(file);
  if(!links) return ((void *)0);
  lob_free(links);
  return mesh;
}

variable_types:
mesh
struct mesh_struct *

return_type: struct mesh_struct { struct hashname_struct { unsigned char bin[32]; } *id; struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *on; unsigned int state; struct link_struct { struct hashname_struct *id; struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *x; struct mesh_struct *mesh; struct lob_struct *key; struct chan_struct { struct link_struct *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 } state; } *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; } *links; } *


decompiled_code:
__int64 __fastcall util_sock_timeout(int a1, unsigned int a2)
{
unsigned int v2; // er12
int *v4; // rax
char *v5; // rax
__int64 v6[3]; // [rsp+0h] [rbp-18h] BYREF

v2 = a1;
v6[0] = a2 / 0x3E8;
v6[1] = 1000 * (a2 % 0x3E8);
if ( setsockopt(a1, 1, 20, v6, 0x10u) < 0 )
{
v4 = _errno_location();
v2 = -1;
v5 = strerror(*v4);
util_sys_log(7LL, "src/unix/util.c", 71LL, "util_sock_timeout", "timeout setsockopt error %s", v5);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, unsigned int a2)
{
unsigned int v2; // er12
int *v4; // rax
char *v5; // rax
__int64 v6[3]; // [rsp+0h] [rbp-18h] BYREF

v2 = a1;
v6[0] = a2 / 0x3E8;
v6[1] = 1000 * (a2 % 0x3E8);
if ( setsockopt(a1, 1, 20, v6, 0x10u) < 0 )
{
v4 = _errno_location();
v2 = -1;
v5 = strerror(*v4);
util_sys_log(7LL, "src/unix/util.c", 71LL, "util_sock_timeout", "timeout setsockopt error %s", v5);
}
return v2;
}

original_code:
int util_sock_timeout(int sock, uint32_t ms)
{
  struct timeval tv;
  tv.tv_sec = ms/1000;
  tv.tv_usec = (ms%1000)*1000;
  if(setsockopt(sock, 1, 20, &tv, sizeof(tv)) < 0)
  {
    util_sys_log(7, "src/unix/util.c", 71, __func__, "timeout setsockopt error %s",strerror((*__errno_location ())));
    return -1;
  }
  return sock;
}

canonical_original_code:
int util_sock_timeout(int sock, unsigned int ms)
{
  struct timeval tv;
  tv.tv_sec = ms/1000;
  tv.tv_usec = (ms%1000)*1000;
  if(setsockopt(sock, 1, 20, &tv, sizeof(tv)) < 0)
  {
    util_sys_log(7, "src/unix/util.c", 71, __func__, "timeout setsockopt error %s",strerror((*__errno_location ())));
    return -1;
  }
  return sock;
}

variable_types: sock int

variable_types:
ms
unsigned int

variable_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
void *__fastcall util_reallocf(void *ptr, size_t a2)
{
void *result; // rax
void *v3; // [rsp+8h] [rbp-10h]

if ( a2 )
{
result = realloc(ptr, a2);
if ( result )
return result;
LABEL_5:
v3 = result;
free(ptr);
return v3;
}
result = 0LL;
if ( ptr )
goto LABEL_5;
return result;
}

canonical_decompiled_code:
void *__fastcall func4(void *ptr, size_t a2)
{
void *result; // rax
void *v3; // [rsp+8h] [rbp-10h]

if ( a2 )
{
result = realloc(ptr, a2);
if ( result )
return result;
LABEL_5:
v3 = result;
free(ptr);
return v3;
}
result = 0LL;
if ( ptr )
goto LABEL_5;
return result;
}

original_code:
void *util_reallocf(void *ptr, size_t size)
{
  void *ra;
  if(!size)
  {
    if(ptr) free(ptr);
    return ((void *)0);
  }
  ra = realloc(ptr,size);
  if(ra) return ra;
  free(ptr);
  return ((void *)0);
}

canonical_original_code:
void *util_reallocf(void *ptr, unsigned long size)
{
  void *ra;
  if(!size)
  {
    if(ptr) free(ptr);
    return ((void *)0);
  }
  ra = realloc(ptr,size);
  if(ra) return ra;
  free(ptr);
  return ((void *)0);
}

variable_types:
ptr
void *

variable_types:
size
unsigned long

variable_types:
ra
void *


decompiled_code:
__int64 util_at()
{
__int64 v1; // [rsp+8h] [rbp-10h]

LODWORD(v1) = util_sys_seconds();
HIDWORD(v1) = util_sys_ms((unsigned int)v1);
return v1;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v1; // [rsp+8h] [rbp-10h]

LODWORD(v1) = util_sys_seconds();
HIDWORD(v1) = util_sys_ms((unsigned int)v1);
return v1;
}

original_code:
uint64_t util_at(void)
{
  uint64_t at;
  uint32_t *half = (uint32_t*)(&at);
  half[0] = util_sys_seconds();
  half[1] = (uint32_t)util_sys_ms(half[0]);
  return at;
}

canonical_original_code:
unsigned long util_at()
{
  unsigned long at;
  unsigned int *half = (uint32_t*)(&at);
  half[0] = util_sys_seconds();
  half[1] = (unsigned int)util_sys_ms(half[0]);
  return at;
}

variable_types:
at
unsigned long

variable_types:
half
unsigned int *


decompiled_code:
__int64 __fastcall util_since(__int64 a1)
{
return (unsigned int)util_sys_ms((unsigned int)a1) - HIDWORD(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return (unsigned int)util_sys_ms((unsigned int)a1) - HIDWORD(a1);
}

original_code:
uint32_t util_since(uint64_t at)
{
  uint32_t *half = (uint32_t*)(&at);
  return ((uint32_t)util_sys_ms(half[0]) - half[1]);
}

canonical_original_code:
unsigned int util_since(unsigned long at)
{
  unsigned int *half = (uint32_t*)(&at);
  return ((unsigned int)util_sys_ms(half[0]) - half[1]);
}


decompiled_code:
_BOOL8 __fastcall util_ct_memcmp(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v5; // rdx
int v6; // ecx
unsigned __int8 v7; // al

if ( !a3 )
return 0LL;
v5 = 0LL;
v6 = 0;
do
{
v7 = *(_BYTE *)(a2 + v5) ^ *(_BYTE *)(a1 + v5);
++v5;
v6 |= v7;
}
while ( a3 != v5 );
return v6 != 0;
}

original_code:
int util_ct_memcmp(const void* s1, const void* s2, size_t n)
{
    const unsigned char *p1 = s1, *p2 = s2;
    int x = 0;
    while (n--)
    {
        x |= (*p1 ^ *p2);
        p1++;
        p2++;
    }
    if (x)
        x = 1;
    return x;
}

canonical_original_code:
int util_ct_memcmp(void *s1, void *s2, unsigned long n)
{
    unsigned char *p1 = s1; unsigned char *p2 = s2;
    int x = 0;
    while (n--)
    {
        x |= (*p1 ^ *p2);
        p1++;
        p2++;
    }
    if (x)
        x = 1;
    return x;
}

variable_types:
n
unsigned long

variable_types: x int


decompiled_code:
__int64 __fastcall util_cmp(const char *a1, const char *a2)
{
__int64 v2; // r13

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
if ( a1 != a2 )
{
v2 = strlen(a1);
if ( v2 == strlen(a2) )
return util_ct_memcmp((__int64)a1, (__int64)a2, v2);
return 0xFFFFFFFFLL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, const char *a2)
{
__int64 v2; // r13

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
if ( a1 != a2 )
{
v2 = strlen(a1);
if ( v2 == strlen(a2) )
return func7((__int64)a1, (__int64)a2, v2);
return 0xFFFFFFFFLL;
}
return 0LL;
}

original_code:
int util_cmp(char *a, char *b)
{
  if(!a || !b) return -1;
  if(a == b) return 0;
  if(strlen(a) != strlen(b)) return -1;
  return util_ct_memcmp(a,b,strlen(a));
}

variable_types:
a
char *

variable_types:
b
char *


decompiled_code:
void __fastcall e3x_exchange_free(void *ptr)
{
if ( ptr )
{
(*(void (__fastcall **)(_QWORD))(*(_QWORD *)ptr + 72LL))(*((_QWORD *)ptr + 2));
(*(void (__fastcall **)(_QWORD))(*(_QWORD *)ptr + 112LL))(*((_QWORD *)ptr + 3));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
if ( ptr )
{
(*(void (__fastcall **)(_QWORD))(*(_QWORD *)ptr + 72LL))(*((_QWORD *)ptr + 2));
(*(void (__fastcall **)(_QWORD))(*(_QWORD *)ptr + 112LL))(*((_QWORD *)ptr + 3));
free(ptr);
}
}

original_code:
void e3x_exchange_free(e3x_exchange_t x)
{
  if(!x) return;
  x->cs->remote_free(x->remote);
  x->cs->ephemeral_free(x->ephem);
  free(x);
}

canonical_original_code:
void e3x_exchange_free(struct e3x_exchange_struct *x)
{
  if(!x) return;
  x->cs->remote_free(x->remote);
  x->cs->ephemeral_free(x->ephem);
  free(x);
}

variable_types: struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *


decompiled_code:
__int64 __fastcall e3x_exchange_message(_QWORD *a1, __int64 a2)
{
__int64 result; // rax

if ( a1 && a2 )
result = (*(__int64 (__fastcall **)(_QWORD, _QWORD, __int64))(*a1 + 88LL))(
a1[2],
*(_QWORD *)(a1[1] + 8LL * *(unsigned __int8 *)(*a1 + 136LL) + 16),
a2);
else
result = util_sys_log(7LL, "src/e3x/exchange.c", 66LL, "e3x_exchange_message", "bad args");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, __int64 a2)
{
__int64 result; // rax

if ( a1 && a2 )
result = (*(__int64 (__fastcall **)(_QWORD, _QWORD, __int64))(*a1 + 88LL))(
a1[2],
*(_QWORD *)(a1[1] + 8LL * *(unsigned __int8 *)(*a1 + 136LL) + 16),
a2);
else
result = util_sys_log(7LL, "src/e3x/exchange.c", 66LL, "e3x_exchange_message", "bad args");
return result;
}

original_code:
lob_t e3x_exchange_message(e3x_exchange_t x, lob_t inner)
{
  if(!x || !inner) return util_sys_log(7, "src/e3x/exchange.c", 66, __func__, "bad args");
  return x->cs->remote_encrypt(x->remote,x->self->locals[x->cs->id],inner);
}

canonical_original_code:
struct lob_struct *e3x_exchange_message(struct e3x_exchange_struct *x, struct lob_struct *inner)
{
  if(!x || !inner) return util_sys_log(7, "src/e3x/exchange.c", 66, __func__, "bad args");
  return x->cs->remote_encrypt(x->remote,x->self->locals[x->cs->id],inner);
}

variable_types: struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *

variable_types:
inner
struct lob_struct *


decompiled_code:
__int64 __fastcall e3x_exchange_verify(_QWORD *a1, __int64 a2)
{
__int64 result; // rax

if ( a1 && a2 )
result = (*(__int64 (__fastcall **)(_QWORD, _QWORD, __int64))(*a1 + 80LL))(
a1[2],
*(_QWORD *)(a1[1] + 8LL * *(unsigned __int8 *)(*a1 + 136LL) + 16),
a2);
else
result = 1LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, __int64 a2)
{
__int64 result; // rax

if ( a1 && a2 )
result = (*(__int64 (__fastcall **)(_QWORD, _QWORD, __int64))(*a1 + 80LL))(
a1[2],
*(_QWORD *)(a1[1] + 8LL * *(unsigned __int8 *)(*a1 + 136LL) + 16),
a2);
else
result = 1LL;
return result;
}

original_code:
uint8_t e3x_exchange_verify(e3x_exchange_t x, lob_t outer)
{
  if(!x || !outer) return 1;
  return x->cs->remote_verify(x->remote,x->self->locals[x->cs->id],outer);
}

canonical_original_code:
unsigned char e3x_exchange_verify(struct e3x_exchange_struct *x, struct lob_struct *outer)
{
  if(!x || !outer) return 1;
  return x->cs->remote_verify(x->remote,x->self->locals[x->cs->id],outer);
}

variable_types:
outer
struct lob_struct *


decompiled_code:
__int64 __fastcall e3x_exchange_validate(_QWORD *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
_QWORD *v6; // r14
const char *v9; // r13
__int64 v10; // rax
__int64 (__fastcall *v11)(_QWORD *, __int64, __int64, __int64, __int64); // r9

v6 = a1;
v9 = (const char *)lob_get(a2, "alg");
if ( a5 == 0 || a4 == 0 || !v9 )
return 1LL;
v10 = e3x_cipher_set(0LL, v9);
if ( !v10 || (v11 = *(__int64 (__fastcall **)(_QWORD *, __int64, __int64, __int64, __int64))(v10 + 96)) == 0LL )
{
util_sys_log(7LL, "src/e3x/exchange.c", 86LL, "e3x_exchange_validate", "no validate support for %s", v9);
return 1LL;
}
if ( a1 )
{
if ( *a1 == v10 )
v6 = (_QWORD *)a1[2];
else
v6 = 0LL;
}
return v11(v6, a2, a3, a4, a5);
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
_QWORD *v6; // r14
const char *v9; // r13
__int64 v10; // rax
__int64 (__fastcall *v11)(_QWORD *, __int64, __int64, __int64, __int64); // r9

v6 = a1;
v9 = (const char *)lob_get(a2, "alg");
if ( a5 == 0 || a4 == 0 || !v9 )
return 1LL;
v10 = e3x_cipher_set(0LL, v9);
if ( !v10 || (v11 = *(__int64 (__fastcall **)(_QWORD *, __int64, __int64, __int64, __int64))(v10 + 96)) == 0LL )
{
util_sys_log(7LL, "src/e3x/exchange.c", 86LL, "e3x_exchange_validate", "no validate support for %s", v9);
return 1LL;
}
if ( a1 )
{
if ( *a1 == v10 )
v6 = (_QWORD *)a1[2];
else
v6 = 0LL;
}
return v11(v6, a2, a3, a4, a5);
}

original_code:
uint8_t e3x_exchange_validate(e3x_exchange_t x, lob_t args, lob_t sig, uint8_t *data, size_t len)
{
  void* remote = ((void *)0);
  e3x_cipher_t cs = ((void *)0);
  char *alg = lob_get(args,"alg");
  if(!data || !len || !alg) return 1;
  cs = e3x_cipher_set(0,alg);
  if(!cs || !cs->remote_validate)
  {
    util_sys_log(7, "src/e3x/exchange.c", 86, __func__, "no validate support for %s",alg);
    return 1;
  }
  if(x && x->cs == cs) remote = x->remote;
  return cs->remote_validate(remote,args,sig,data,len);
}

canonical_original_code:
unsigned char e3x_exchange_validate(struct e3x_exchange_struct *x, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len)
{
  void *remote = ((void *)0);
  struct e3x_cipher_struct *cs = ((void *)0);
  char *alg = lob_get(args,"alg");
  if(!data || !len || !alg) return 1;
  cs = e3x_cipher_set(0,alg);
  if(!cs || !cs->remote_validate)
  {
    util_sys_log(7, "src/e3x/exchange.c", 86, __func__, "no validate support for %s",alg);
    return 1;
  }
  if(x && x->cs == cs) remote = x->remote;
  return cs->remote_validate(remote,args,sig,data,len);
}

variable_types:
args
struct lob_struct *

variable_types:
sig
struct lob_struct *

variable_types:
data
unsigned char *

variable_types:
len
unsigned long

variable_types:
remote
void *

variable_types: struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *

variable_types:
alg
char *


decompiled_code:
__int64 __fastcall e3x_exchange_out(__int64 a1, unsigned int a2)
{
__int64 result; // rax

if ( !a1 )
return 0LL;
result = *(unsigned int *)(a1 + 36);
if ( a2 && a2 > (unsigned int)result )
{
*(_DWORD *)(a1 + 36) = a2;
if ( *(_BYTE *)(a1 + 81) != 2 )
{
if ( (a2 & 1) != 0 )
return a2;
goto LABEL_10;
}
result = a2;
if ( (a2 & 1) != 0 )
{
LABEL_10:
result = a2 + 1;
*(_DWORD *)(a1 + 36) = result;
return result;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned int a2)
{
__int64 result; // rax

if ( !a1 )
return 0LL;
result = *(unsigned int *)(a1 + 36);
if ( a2 && a2 > (unsigned int)result )
{
*(_DWORD *)(a1 + 36) = a2;
if ( *(_BYTE *)(a1 + 81) != 2 )
{
if ( (a2 & 1) != 0 )
return a2;
goto LABEL_10;
}
result = a2;
if ( (a2 & 1) != 0 )
{
LABEL_10:
result = a2 + 1;
*(_DWORD *)(a1 + 36) = result;
return result;
}
}
return result;
}

original_code:
uint32_t e3x_exchange_out(e3x_exchange_t x, uint32_t at)
{
  if(!x) return 0;
  if(at && at > x->out)
  {
    x->out = at;
    if(x->order == 2)
    {
      if(x->out % 2 != 0) x->out++;
    }else{
      if(x->out % 2 == 0) x->out++;
    }
  }
  return x->out;
}

canonical_original_code:
unsigned int e3x_exchange_out(struct e3x_exchange_struct *x, unsigned int at)
{
  if(!x) return 0;
  if(at && at > x->out)
  {
    x->out = at;
    if(x->order == 2)
    {
      if(x->out % 2 != 0) x->out++;
    }else{
      if(x->out % 2 == 0) x->out++;
    }
  }
  return x->out;
}

variable_types:
at
unsigned int


decompiled_code:
__int64 __fastcall e3x_exchange_in(__int64 a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // edx

if ( !a1 )
return 0LL;
result = *(unsigned int *)(a1 + 32);
if ( a2 )
{
if ( a2 > (unsigned int)result )
{
v3 = *(_DWORD *)(a1 + 36);
if ( v3 <= a2 && (2 - ((a2 & 1) == 0) == *(unsigned __int8 *)(a1 + 81) || v3 == a2) )
{
*(_DWORD *)(a1 + 32) = a2;
result = a2;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // edx

if ( !a1 )
return 0LL;
result = *(unsigned int *)(a1 + 32);
if ( a2 )
{
if ( a2 > (unsigned int)result )
{
v3 = *(_DWORD *)(a1 + 36);
if ( v3 <= a2 && (2 - ((a2 & 1) == 0) == *(unsigned __int8 *)(a1 + 81) || v3 == a2) )
{
*(_DWORD *)(a1 + 32) = a2;
result = a2;
}
}
}
return result;
}

original_code:
uint32_t e3x_exchange_in(e3x_exchange_t x, uint32_t at)
{
  if(!x) return 0;
  if(at && at > x->in && at >= x->out && (((at % 2)+1) == x->order || at == x->out)) x->in = at;
  return x->in;
}

canonical_original_code:
unsigned int e3x_exchange_in(struct e3x_exchange_struct *x, unsigned int at)
{
  if(!x) return 0;
  if(at && at > x->in && at >= x->out && (((at % 2)+1) == x->order || at == x->out)) x->in = at;
  return x->in;
}


original_code:
e3x_exchange_t e3x_exchange_down(e3x_exchange_t x)
{
  if(!x) return ((void *)0);
  x->out = 0;
  if(x->ephem)
  {
    x->cs->ephemeral_free(x->ephem);
    x->ephem = ((void *)0);
    memset(x->eid,0,16);
    x->last = 0;
  }
  return x;
}

canonical_original_code:
struct e3x_exchange_struct *e3x_exchange_down(struct e3x_exchange_struct *x)
{
  if(!x) return ((void *)0);
  x->out = 0;
  if(x->ephem)
  {
    x->cs->ephemeral_free(x->ephem);
    x->ephem = ((void *)0);
    memset(x->eid,0,16);
    x->last = 0;
  }
  return x;
}

variable_types:
x
struct e3x_exchange_struct *

return_type: struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *


original_code:
e3x_exchange_t e3x_exchange_sync(e3x_exchange_t x, lob_t outer)
{
  void* ephem;
  if(!x || !outer) return util_sys_log(7, "src/e3x/exchange.c", 144, __func__, "bad args");
  if(outer->body_len < 16) return util_sys_log(7, "src/e3x/exchange.c", 145, __func__, "outer too small");
  if(x->in > x->out) x->out = x->in;
  if(util_ct_memcmp(outer->body,x->eid,16) != 0)
  {
    ephem = x->cs->ephemeral_new(x->remote,outer);
    if(!ephem) return util_sys_log(7, "src/e3x/exchange.c", 153, __func__, "ephemeral creation failed %s",x->cs->err());
    x->cs->ephemeral_free(x->ephem);
    x->ephem = ephem;
    memcpy(x->eid,outer->body,16);
    x->last = 0;
  }
  return x;
}

canonical_original_code:
struct e3x_exchange_struct *e3x_exchange_sync(struct e3x_exchange_struct *x, struct lob_struct *outer)
{
  void *ephem;
  if(!x || !outer) return util_sys_log(7, "src/e3x/exchange.c", 144, __func__, "bad args");
  if(outer->body_len < 16) return util_sys_log(7, "src/e3x/exchange.c", 145, __func__, "outer too small");
  if(x->in > x->out) x->out = x->in;
  if(util_ct_memcmp(outer->body,x->eid,16) != 0)
  {
    ephem = x->cs->ephemeral_new(x->remote,outer);
    if(!ephem) return util_sys_log(7, "src/e3x/exchange.c", 153, __func__, "ephemeral creation failed %s",x->cs->err());
    x->cs->ephemeral_free(x->ephem);
    x->ephem = ephem;
    memcpy(x->eid,outer->body,16);
    x->last = 0;
  }
  return x;
}

variable_types:
ephem
void *


decompiled_code:
__int64 __fastcall e3x_exchange_handshake(__int64 a1, __int64 a2)
{
__int64 v3; // rdx
__int64 v4; // r12
__int64 result; // rax
__int64 v6; // r13
__int64 v7; // rax
__int64 v8; // rsi
__int64 v9; // rdi
__int64 v10; // rax
_BYTE v11[56]; // [rsp-38h] [rbp-38h] BYREF

if ( !a1 )
return util_sys_log(7LL, "src/e3x/exchange.c", 170LL, "e3x_exchange_handshake", "invalid args");
v3 = *(unsigned int *)(a1 + 36);
if ( !(_DWORD)v3 )
return util_sys_log(7LL, "src/e3x/exchange.c", 171LL, "e3x_exchange_handshake", "no out set");
v4 = a2;
if ( a2 )
{
lob_set_uint(a2, "at", v3);
result = e3x_exchange_message((_QWORD *)a1, a2);
}
else
{
v6 = lob_new();
lob_set(v6, "type", "key");
while ( 1 )
{
v7 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4);
if ( v7 )
{
v8 = *(_QWORD *)(v7 + 16);
v9 = *(_QWORD *)(v7 + 8);
if ( e3x_cipher_sets[v4] == *(_QWORD *)a1 )
{
lob_body(v6, v9, v8);
}
else
{
e3x_hash(v9, v8, v11);
lob_set_base32(v6, e3x_cipher_sets[v4] + 138LL, v11, 32LL);
}
}
if ( v4 == 1 )
break;
v4 = 1LL;
}
lob_set_uint(v6, "at", *(unsigned int *)(a1 + 36));
v10 = e3x_exchange_message((_QWORD *)a1, v6);
result = lob_link(v10, v6);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
__int64 v3; // rdx
__int64 v4; // r12
__int64 result; // rax
__int64 v6; // r13
__int64 v7; // rax
__int64 v8; // rsi
__int64 v9; // rdi
__int64 v10; // rax
_BYTE v11[56]; // [rsp-38h] [rbp-38h] BYREF

if ( !a1 )
return util_sys_log(7LL, "src/e3x/exchange.c", 170LL, "e3x_exchange_handshake", "invalid args");
v3 = *(unsigned int *)(a1 + 36);
if ( !(_DWORD)v3 )
return util_sys_log(7LL, "src/e3x/exchange.c", 171LL, "e3x_exchange_handshake", "no out set");
v4 = a2;
if ( a2 )
{
lob_set_uint(a2, "at", v3);
result = func2((_QWORD *)a1, a2);
}
else
{
v6 = lob_new();
lob_set(v6, "type", "key");
while ( 1 )
{
v7 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4);
if ( v7 )
{
v8 = *(_QWORD *)(v7 + 16);
v9 = *(_QWORD *)(v7 + 8);
if ( e3x_cipher_sets[v4] == *(_QWORD *)a1 )
{
lob_body(v6, v9, v8);
}
else
{
e3x_hash(v9, v8, v11);
lob_set_base32(v6, e3x_cipher_sets[v4] + 138LL, v11, 32LL);
}
}
if ( v4 == 1 )
break;
v4 = 1LL;
}
lob_set_uint(v6, "at", *(unsigned int *)(a1 + 36));
v10 = func2((_QWORD *)a1, v6);
result = lob_link(v10, v6);
}
return result;
}

original_code:
lob_t e3x_exchange_handshake(e3x_exchange_t x, lob_t inner)
{
  lob_t tmp;
  uint8_t i;
  uint8_t local = 0;
  if(!x) return util_sys_log(7, "src/e3x/exchange.c", 170, __func__, "invalid args");
  if(!x->out) return util_sys_log(7, "src/e3x/exchange.c", 171, __func__, "no out set");
  if(!inner)
  {
    local = 1;
    inner = lob_new();
    lob_set(inner, "type", "key");
    for(i=0; i<2; i++)
    {
      if(!(tmp = x->self->keys[i])) continue;
      if(e3x_cipher_sets[i] == x->cs)
      {
        lob_body(inner,tmp->body,tmp->body_len);
      }else{
        uint8_t hash[32];
        e3x_hash(tmp->body,tmp->body_len,hash);
        lob_set_base32(inner,e3x_cipher_sets[i]->hex,hash,32);
      }
    }
  }
  lob_set_uint(inner,"at",x->out);
  tmp = e3x_exchange_message(x, inner);
  if(!local) return tmp;
  return lob_link(tmp, inner);
}

canonical_original_code:
struct lob_struct *e3x_exchange_handshake(struct e3x_exchange_struct *x, struct lob_struct *inner)
{
  struct lob_struct *tmp;
  unsigned char i;
  unsigned char local = 0;
  if(!x) return util_sys_log(7, "src/e3x/exchange.c", 170, __func__, "invalid args");
  if(!x->out) return util_sys_log(7, "src/e3x/exchange.c", 171, __func__, "no out set");
  if(!inner)
  {
    local = 1;
    inner = lob_new();
    lob_set(inner, "type", "key");
    for(i=0; i<2; i++)
    {
      if(!(tmp = x->self->keys[i])) continue;
      if(e3x_cipher_sets[i] == x->cs)
      {
        lob_body(inner,tmp->body,tmp->body_len);
      }else{
        unsigned char hash[32];
        e3x_hash(tmp->body,tmp->body_len,hash);
        lob_set_base32(inner,e3x_cipher_sets[i]->hex,hash,32);
      }
    }
  }
  lob_set_uint(inner,"at",x->out);
  tmp = e3x_exchange_message(x, inner);
  if(!local) return tmp;
  return lob_link(tmp, inner);
}

variable_types: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *

variable_types:
i
unsigned char

variable_types:
local
unsigned char

variable_types:
hash
unsigned char [32]


decompiled_code:
__int64 __fastcall e3x_exchange_receive(_QWORD *a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // r12
__int64 result; // rax
const char *v5; // rax

if ( !a1 || !a2 )
return util_sys_log(7LL, "src/e3x/exchange.c", 207LL, "e3x_exchange_receive", "invalid args");
if ( !a1[3] )
return util_sys_log(7LL, "src/e3x/exchange.c", 208LL, "e3x_exchange_receive", "no handshake");
v2 = (*(__int64 (**)(void))(*a1 + 128LL))();
v3 = v2;
if ( v2 )
{
util_sys_log(
7LL,
"src/e3x/exchange.c",
211LL,
"e3x_exchange_receive",
"decrypted head %d body %d",
*(_QWORD *)(v2 + 32),
*(_QWORD *)(v2 + 16));
result = v3;
}
else
{
v5 = (const char *)(*(__int64 (**)(void))(*a1 + 16LL))();
result = util_sys_log(7LL, "src/e3x/exchange.c", 210LL, "e3x_exchange_receive", "decryption failed %s", v5);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // r12
__int64 result; // rax
const char *v5; // rax

if ( !a1 || !a2 )
return util_sys_log(7LL, "src/e3x/exchange.c", 207LL, "e3x_exchange_receive", "invalid args");
if ( !a1[3] )
return util_sys_log(7LL, "src/e3x/exchange.c", 208LL, "e3x_exchange_receive", "no handshake");
v2 = (*(__int64 (**)(void))(*a1 + 128LL))();
v3 = v2;
if ( v2 )
{
util_sys_log(
7LL,
"src/e3x/exchange.c",
211LL,
"e3x_exchange_receive",
"decrypted head %d body %d",
*(_QWORD *)(v2 + 32),
*(_QWORD *)(v2 + 16));
result = v3;
}
else
{
v5 = (const char *)(*(__int64 (**)(void))(*a1 + 16LL))();
result = util_sys_log(7LL, "src/e3x/exchange.c", 210LL, "e3x_exchange_receive", "decryption failed %s", v5);
}
return result;
}

original_code:
lob_t e3x_exchange_receive(e3x_exchange_t x, lob_t outer)
{
  lob_t inner;
  if(!x || !outer) return util_sys_log(7, "src/e3x/exchange.c", 207, __func__, "invalid args");
  if(!x->ephem) return util_sys_log(7, "src/e3x/exchange.c", 208, __func__, "no handshake");
  inner = x->cs->ephemeral_decrypt(x->ephem,outer);
  if(!inner) return util_sys_log(7, "src/e3x/exchange.c", 210, __func__, "decryption failed %s",x->cs->err());
  util_sys_log(7, "src/e3x/exchange.c", 211, __func__, "decrypted head %d body %d",inner->head_len,inner->body_len);
  return inner;
}

canonical_original_code:
struct lob_struct *e3x_exchange_receive(struct e3x_exchange_struct *x, struct lob_struct *outer)
{
  struct lob_struct *inner;
  if(!x || !outer) return util_sys_log(7, "src/e3x/exchange.c", 207, __func__, "invalid args");
  if(!x->ephem) return util_sys_log(7, "src/e3x/exchange.c", 208, __func__, "no handshake");
  inner = x->cs->ephemeral_decrypt(x->ephem,outer);
  if(!inner) return util_sys_log(7, "src/e3x/exchange.c", 210, __func__, "decryption failed %s",x->cs->err());
  util_sys_log(7, "src/e3x/exchange.c", 211, __func__, "decrypted head %d body %d",inner->head_len,inner->body_len);
  return inner;
}

variable_types: struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *


decompiled_code:
__int64 __fastcall e3x_exchange_send(_QWORD *a1, __int64 a2)
{
__int64 v3; // rdi
__int64 result; // rax
const char *v5; // rax
__int64 v6; // [rsp-10h] [rbp-28h]

if ( !a1 || !a2 )
return util_sys_log(7LL, "src/e3x/exchange.c", 219LL, "e3x_exchange_send", "invalid args");
if ( !a1[3] )
return util_sys_log(7LL, "src/e3x/exchange.c", 220LL, "e3x_exchange_send", "no handshake");
util_sys_log(
7LL,
"src/e3x/exchange.c",
221LL,
"e3x_exchange_send",
"encrypting head %d body %d",
*(_QWORD *)(a2 + 32),
*(_QWORD *)(a2 + 16));
v3 = a1[3];
result = (*(__int64 (__fastcall **)(__int64, __int64))(*a1 + 120LL))(v3, a2);
if ( !result )
{
v5 = (const char *)(*(__int64 (__fastcall **)(__int64, __int64, __int64))(*a1 + 16LL))(v3, a2, v6);
result = util_sys_log(7LL, "src/e3x/exchange.c", 223LL, "e3x_exchange_send", "encryption failed %s", v5);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(_QWORD *a1, __int64 a2)
{
__int64 v3; // rdi
__int64 result; // rax
const char *v5; // rax
__int64 v6; // [rsp-10h] [rbp-28h]

if ( !a1 || !a2 )
return util_sys_log(7LL, "src/e3x/exchange.c", 219LL, "e3x_exchange_send", "invalid args");
if ( !a1[3] )
return util_sys_log(7LL, "src/e3x/exchange.c", 220LL, "e3x_exchange_send", "no handshake");
util_sys_log(
7LL,
"src/e3x/exchange.c",
221LL,
"e3x_exchange_send",
"encrypting head %d body %d",
*(_QWORD *)(a2 + 32),
*(_QWORD *)(a2 + 16));
v3 = a1[3];
result = (*(__int64 (__fastcall **)(__int64, __int64))(*a1 + 120LL))(v3, a2);
if ( !result )
{
v5 = (const char *)(*(__int64 (__fastcall **)(__int64, __int64, __int64))(*a1 + 16LL))(v3, a2, v6);
result = util_sys_log(7LL, "src/e3x/exchange.c", 223LL, "e3x_exchange_send", "encryption failed %s", v5);
}
return result;
}

original_code:
lob_t e3x_exchange_send(e3x_exchange_t x, lob_t inner)
{
  lob_t outer;
  if(!x || !inner) return util_sys_log(7, "src/e3x/exchange.c", 219, __func__, "invalid args");
  if(!x->ephem) return util_sys_log(7, "src/e3x/exchange.c", 220, __func__, "no handshake");
  util_sys_log(7, "src/e3x/exchange.c", 221, __func__, "encrypting head %d body %d",inner->head_len,inner->body_len);
  outer = x->cs->ephemeral_encrypt(x->ephem,inner);
  if(!outer) return util_sys_log(7, "src/e3x/exchange.c", 223, __func__, "encryption failed %s",x->cs->err());
  return outer;
}

canonical_original_code:
struct lob_struct *e3x_exchange_send(struct e3x_exchange_struct *x, struct lob_struct *inner)
{
  struct lob_struct *outer;
  if(!x || !inner) return util_sys_log(7, "src/e3x/exchange.c", 219, __func__, "invalid args");
  if(!x->ephem) return util_sys_log(7, "src/e3x/exchange.c", 220, __func__, "no handshake");
  util_sys_log(7, "src/e3x/exchange.c", 221, __func__, "encrypting head %d body %d",inner->head_len,inner->body_len);
  outer = x->cs->ephemeral_encrypt(x->ephem,inner);
  if(!outer) return util_sys_log(7, "src/e3x/exchange.c", 223, __func__, "encryption failed %s",x->cs->err());
  return outer;
}

variable_types:
inner
struct lob_struct *


decompiled_code:
__int64 __fastcall e3x_exchange_cid(__int64 a1, __int64 a2)
{
__int64 result; // rax

if ( !a1 )
return 0LL;
if ( a2 )
{
result = lob_get_uint(a2, "c");
if ( (_DWORD)result
&& *(_DWORD *)(a1 + 44) < (unsigned int)result
&& (((unsigned __int8)result ^ (unsigned __int8)*(_DWORD *)(a1 + 40)) & 1) != 0 )
{
*(_DWORD *)(a1 + 44) = result;
return result;
}
return 0LL;
}
result = *(unsigned int *)(a1 + 40);
*(_DWORD *)(a1 + 40) = result + 2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2)
{
__int64 result; // rax

if ( !a1 )
return 0LL;
if ( a2 )
{
result = lob_get_uint(a2, "c");
if ( (_DWORD)result
&& *(_DWORD *)(a1 + 44) < (unsigned int)result
&& (((unsigned __int8)result ^ (unsigned __int8)*(_DWORD *)(a1 + 40)) & 1) != 0 )
{
*(_DWORD *)(a1 + 44) = result;
return result;
}
return 0LL;
}
result = *(unsigned int *)(a1 + 40);
*(_DWORD *)(a1 + 40) = result + 2;
return result;
}

original_code:
uint32_t e3x_exchange_cid(e3x_exchange_t x, lob_t incoming)
{
  uint32_t cid;
  if(!x) return 0;
  if(!incoming)
  {
    cid = x->cid;
    x->cid += 2;
    return cid;
  }
  if(!(cid = lob_get_uint(incoming,"c"))) return 0;
  if(cid <= x->last) return 0;
  if((cid % 2) == (x->cid % 2)) return 0;
  x->last = cid;
  return cid;
}

canonical_original_code:
unsigned int e3x_exchange_cid(struct e3x_exchange_struct *x, struct lob_struct *incoming)
{
  unsigned int cid;
  if(!x) return 0;
  if(!incoming)
  {
    cid = x->cid;
    x->cid += 2;
    return cid;
  }
  if(!(cid = lob_get_uint(incoming,"c"))) return 0;
  if(cid <= x->last) return 0;
  if((cid % 2) == (x->cid % 2)) return 0;
  x->last = cid;
  return cid;
}

variable_types: struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *

variable_types:
incoming
struct lob_struct *

variable_types:
cid
unsigned int


decompiled_code:
__int64 __fastcall e3x_exchange_token(__int64 a1)
{
__int64 result; // rax

result = a1 + 48;
if ( !a1 )
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
__int64 result; // rax

result = a1 + 48;
if ( !a1 )
result = 0LL;
return result;
}

original_code:
uint8_t *e3x_exchange_token(e3x_exchange_t x)
{
  if(!x) return ((void *)0);
  return x->token;
}

canonical_original_code:
unsigned char *e3x_exchange_token(struct e3x_exchange_struct *x)
{
  if(!x) return ((void *)0);
  return x->token;
}


decompiled_code:
__int64 __fastcall bulk_handler(__int64 a1)
{
__int64 v1; // rax
__int64 result; // rax
__int64 v3; // rbp

while ( 1 )
{
result = chan_receiving(a1);
v3 = result;
if ( !result )
break;
v1 = chan_packet(a1);
chan_send(a1, v1);
lob_free(v3);
++bulked;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rax
__int64 result; // rax
__int64 v3; // rbp

while ( 1 )
{
result = chan_receiving(a1);
v3 = result;
if ( !result )
break;
v1 = chan_packet(a1);
chan_send(a1, v1);
lob_free(v3);
++bulked;
}
return result;
}

original_code:
void bulk_handler(chan_t chan, void *arg)
{
  lob_t packet;
  while((packet = chan_receiving(chan)))
  {
    chan_send(chan,chan_packet(chan));
    lob_free(packet);
    bulked++;
  }
}

canonical_original_code:
void bulk_handler(struct chan_struct *chan, void *arg)
{
  struct lob_struct *packet;
  while((packet = chan_receiving(chan)))
  {
    chan_send(chan,chan_packet(chan));
    lob_free(packet);
    bulked++;
  }
}

variable_types: struct chan_struct { struct link_struct { struct hashname_struct { unsigned char bin[32]; } *id; struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *x; struct mesh_struct { struct hashname_struct *id; struct lob_struct *keys; struct e3x_self_struct *self; void *on; unsigned int state; struct link_struct *links; } *mesh; struct lob_struct *key; struct chan_struct *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; } *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 } state; } *

variable_types:
arg
void *


decompiled_code:
__int64 __fastcall bulk_on_open(__int64 a1, __int64 a2)
{
__int64 v3; // r13
__int64 v4; // rax
unsigned int v5; // eax

if ( (unsigned int)lob_get_cmp(a2, "type", "bulk") )
return a2;
util_sys_log(7LL, "net_bulk.c", 24LL, "bulk_on_open", "incoming bulk start");
chan = link_chan(a1, a2);
chan_handle(chan, bulk_handler, 0LL);
v3 = lob_new();
v4 = lob_get(a2, "c");
lob_set(v3, "c", v4);
chan_receive(chan, a2);
v5 = chan_size(chan);
printf("Done setting up channel: %d\n", v5);
link_direct(a1, v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
__int64 v3; // r13
__int64 v4; // rax
unsigned int v5; // eax

if ( (unsigned int)lob_get_cmp(a2, "type", "bulk") )
return a2;
util_sys_log(7LL, "net_bulk.c", 24LL, "bulk_on_open", "incoming bulk start");
chan = link_chan(a1, a2);
chan_handle(chan, bulk_handler, 0LL);
v3 = lob_new();
v4 = lob_get(a2, "c");
lob_set(v3, "c", v4);
chan_receive(chan, a2);
v5 = chan_size(chan);
printf("Done setting up channel: %d\n", v5);
link_direct(a1, v3);
return 0LL;
}

original_code:
lob_t bulk_on_open(link_t link, lob_t open)
{
  if(lob_get_cmp(open,"type","bulk")) return open;
  util_sys_log(7, "net_bulk.c", 24, __func__, "incoming bulk start");
  chan = link_chan(link, open);
  chan_handle(chan,bulk_handler,((void *)0));
  lob_t reply = lob_new();
  lob_set(reply,"c",lob_get(open,"c"));
  chan_receive(chan,open);
  printf("Done setting up channel: %d\n", chan_size(chan));
  link_direct(link,reply);
  return ((void *)0);
}

canonical_original_code:
struct lob_struct *bulk_on_open(struct link_struct *link, struct lob_struct *open)
{
  if(lob_get_cmp(open,"type","bulk")) return open;
  util_sys_log(7, "net_bulk.c", 24, __func__, "incoming bulk start");
  chan = link_chan(link, open);
  chan_handle(chan,bulk_handler,((void *)0));
  struct lob_struct *reply = lob_new();
  lob_set(reply,"c",lob_get(open,"c"));
  chan_receive(chan,open);
  printf("Done setting up channel: %d\n", chan_size(chan));
  link_direct(link,reply);
  return ((void *)0);
}

variable_types: struct link_struct { struct hashname_struct { unsigned char bin[32]; } *id; struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *x; struct mesh_struct { struct hashname_struct *id; struct lob_struct *keys; struct e3x_self_struct *self; void *on; unsigned int state; struct link_struct *links; } *mesh; struct lob_struct *key; struct chan_struct { struct link_struct *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 } state; } *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; } *

variable_types:
open
struct lob_struct *