#include <netinet/in.h>

#include "swissparse.h"
#include "swisscrypt.h"


inline void *swiss_mallocretrmsg(){
      return (void*)malloc(1+sizeof(uint32_t)+SWISS_PWDLEN);
}
inline void *swiss_mallocstoremsg(struct account *acc){
      return (void*)malloc(1+sizeof(uint32_t)+SWISS_PWDLEN+sizeof(uint32_t)+acc->dsize);
}

//mallocs acc->auth
char *sw_parse_cred(struct account *const acc, char* buf){
      acc->acc_id = (int)ntohl(*(uint32_t*)buf);
      buf += sizeof(uint32_t);
      acc->asize = SWISS_PWDLEN;
      acc->auth = (char*)malloc(acc->asize=SWISS_PWDLEN);
      memcpy(acc->auth, buf, SWISS_PWDLEN);
      return buf+SWISS_PWDLEN;
}


//buf has to be allocated before
char *sw_marsh_cred(struct account *const acc, char *buf){
      *(uint32_t*)buf = htonl(acc->acc_id);
      buf += sizeof(uint32_t);
      memcpy(buf, acc->auth, SWISS_PWDLEN);
      return (char*)(buf + SWISS_PWDLEN);
}

char *sw_parse_dsize(struct account *const acc, char *buf){
      acc->dsize = (int)ntohl(*(uint32_t*)buf);
      return buf + sizeof(uint32_t);
}

char *sw_parse_data(struct account *const acc, char *buf) {
      acc->auth = (char*)malloc(acc->dsize);
      memcpy(acc->data, buf, acc->dsize);
      return buf+acc->dsize;
}

char *sw_marsh_dsize_data(struct account *acc, char *buf){
      *(uint32_t*)buf = htonl(acc->dsize);
      (char*)buf += sizeof(uint32_t);
      memcpy(buf, acc->data, acc->dsize);
      return (char*)(buf + acc->dsize);
}
