#ifdef __cplusplus
extern "C" {
#endif

#include "ulib/pack.h"

#ifdef __cplusplus
}
#endif

void uu_pack_v0(const struct_uu_t *in, uu_t out) {
  uu_pack_v1(in, out);
}

void uu_pack_v1(const struct_uu_t *in, uu_t out) {
  U32 tmp;

  tmp = in->v1.time_low;
  out[3] = (U8)tmp; tmp >>= 8;
  out[2] = (U8)tmp; tmp >>= 8;
  out[1] = (U8)tmp; tmp >>= 8;
  out[0] = (U8)tmp;

  tmp = in->v1.time_mid;
  out[5] = (U8)tmp; tmp >>= 8;
  out[4] = (U8)tmp;

  tmp = in->v1.time_high_and_version;
  out[7] = (U8)tmp; tmp >>= 8;
  out[6] = (U8)tmp;

  tmp = in->v1.clock_seq_and_variant;
  out[9] = (U8)tmp; tmp >>= 8;
  out[8] = (U8)tmp;

  memcpy(out+10, in->v1.node, 6);
}

void uu_pack_v3(const struct_uu_t *in, uu_t out) {
  U32 tmp;

  tmp = in->v1.time_low;
  out[3] = (U8)tmp; tmp >>= 8;
  out[2] = (U8)tmp; tmp >>= 8;
  out[1] = (U8)tmp; tmp >>= 8;
  out[0] = (U8)tmp;

  tmp = in->v1.time_mid;
  out[5] = (U8)tmp; tmp >>= 8;
  out[4] = (U8)tmp;

  tmp = in->v1.time_high_and_version;
  out[7] = (U8)tmp; tmp >>= 8;
  out[6] = (U8)tmp;

  tmp = in->v1.clock_seq_and_variant;
  out[9] = (U8)tmp; tmp >>= 8;
  out[8] = (U8)tmp;

  memcpy(out+10, in->v1.node, 6);
}

void uu_pack_v4(const struct_uu_t *in, uu_t out) {
  U32 tmp;

  tmp = in->v4.rand_a;
  out[ 3] = (U8)tmp; tmp >>= 8;
  out[ 2] = (U8)tmp; tmp >>= 8;
  out[ 1] = (U8)tmp; tmp >>= 8;
  out[ 0] = (U8)tmp;

  tmp = in->v4.rand_b_and_version;
  out[ 7] = (U8)tmp; tmp >>= 8;
  out[ 6] = (U8)tmp; tmp >>= 8;
  out[ 5] = (U8)tmp; tmp >>= 8;
  out[ 4] = (U8)tmp;

  tmp = in->v4.rand_c_and_variant;
  out[11] = (U8)tmp; tmp >>= 8;
  out[10] = (U8)tmp; tmp >>= 8;
  out[ 9] = (U8)tmp; tmp >>= 8;
  out[ 8] = (U8)tmp;

  tmp = in->v4.rand_d;
  out[15] = (U8)tmp; tmp >>= 8;
  out[14] = (U8)tmp; tmp >>= 8;
  out[13] = (U8)tmp; tmp >>= 8;
  out[12] = (U8)tmp;
}

void uu_pack_v5(const struct_uu_t *in, uu_t out) {
  U32 tmp;

  tmp = in->v1.time_low;
  out[3] = (U8)tmp; tmp >>= 8;
  out[2] = (U8)tmp; tmp >>= 8;
  out[1] = (U8)tmp; tmp >>= 8;
  out[0] = (U8)tmp;

  tmp = in->v1.time_mid;
  out[5] = (U8)tmp; tmp >>= 8;
  out[4] = (U8)tmp;

  tmp = in->v1.time_high_and_version;
  out[7] = (U8)tmp; tmp >>= 8;
  out[6] = (U8)tmp;

  tmp = in->v1.clock_seq_and_variant;
  out[9] = (U8)tmp; tmp >>= 8;
  out[8] = (U8)tmp;

  memcpy(out+10, in->v1.node, 6);
}

void uu_pack_v6(const struct_uu_t *in, uu_t out) {
  U32 tmp;

  tmp = in->v6.time_high;
  out[3] = (U8)tmp; tmp >>= 8;
  out[2] = (U8)tmp; tmp >>= 8;
  out[1] = (U8)tmp; tmp >>= 8;
  out[0] = (U8)tmp;

  tmp = in->v6.time_mid;
  out[5] = (U8)tmp; tmp >>= 8;
  out[4] = (U8)tmp;

  tmp = in->v6.time_low_and_version;
  out[7] = (U8)tmp; tmp >>= 8;
  out[6] = (U8)tmp;

  tmp = in->v6.clock_seq_and_variant;
  out[9] = (U8)tmp; tmp >>= 8;
  out[8] = (U8)tmp;

  memcpy(out+10, in->v6.node, 6);
}

void uu_pack_v7(const struct_uu_t *in, uu_t out) {
  U64 tmp;

  tmp = in->v7.time_high;
  out[3] = (U8)tmp; tmp >>= 8;
  out[2] = (U8)tmp; tmp >>= 8;
  out[1] = (U8)tmp; tmp >>= 8;
  out[0] = (U8)tmp;

  tmp = in->v7.time_low;
  out[5] = (U8)tmp; tmp >>= 8;
  out[4] = (U8)tmp;

  tmp = in->v7.rand_a_and_version;
  out[7] = (U8)tmp; tmp >>= 8;
  out[6] = (U8)tmp;

  tmp = in->v7.rand_b_and_variant;
  out[15] = (U8)tmp; tmp >>= 8;
  out[14] = (U8)tmp; tmp >>= 8;
  out[13] = (U8)tmp; tmp >>= 8;
  out[12] = (U8)tmp; tmp >>= 8;
  out[11] = (U8)tmp; tmp >>= 8;
  out[10] = (U8)tmp; tmp >>= 8;
  out[ 9] = (U8)tmp; tmp >>= 8;
  out[ 8] = (U8)tmp;
}

void uu_pack_unpack(const uu_t in, struct_uu_t *out) {
  const U8  *ptr = in;
  U32       tmp;

  tmp = *ptr++;
  tmp = (tmp << 8) | *ptr++;
  tmp = (tmp << 8) | *ptr++;
  tmp = (tmp << 8) | *ptr++;
  out->v1.time_low = tmp;

  tmp = *ptr++;
  tmp = (tmp << 8) | *ptr++;
  out->v1.time_mid = (U16)tmp;

  tmp = *ptr++;
  tmp = (tmp << 8) | *ptr++;
  out->v1.time_high_and_version = (U16)tmp;

  tmp = *ptr++;
  tmp = (tmp << 8) | *ptr++;
  out->v1.clock_seq_and_variant = (U16)tmp;

  out->v1.node[0] = *ptr++;
  out->v1.node[1] = *ptr++;
  out->v1.node[2] = *ptr++;
  out->v1.node[3] = *ptr++;
  out->v1.node[4] = *ptr++;
  out->v1.node[5] = *ptr;
}

/* ex:set ts=2 sw=2 itab=spaces: */
