// ---------------------------------------------------------------------------
// - Privsci.hpp                                                             -
// - afnix cryptography library - private functions used by the library      -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  is  distributed in  the hope  that it will be useful, but -
// - without  any  warranty;  without  even   the   implied    warranty   of -
// - merchantability or fitness for a particular purpose.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2005 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_CCNF_HPP
#include "ccnf.hpp"
#endif

#ifndef AFNIX_PRIVSCI_HPP
#define AFNIX_PRIVSCI_HPP

namespace afnix {

  // -------------------------------------------------------------------------
  // - generic bytes mapping functions                                       -
  // -------------------------------------------------------------------------

  // little endian byte to quad mapping function - the size is in bytes
  static inline void lebtoq (t_quad* dst, const t_byte* src, const long size) {
    for (long i = 0, j = 0; j < size; i++, j += 4)
      dst[i] =   ((t_quad) src[j])          | 
	        (((t_quad) src[j+1]) << 8)  |
	        (((t_quad) src[j+2]) << 16) | 
	        (((t_quad) src[j+3]) << 24);
  }

  // little endian quad to bytes mapping functions - the size is in quad
  static inline void leqtob (t_byte* dst, const t_quad* src, const long size) {
    for (long i = 0, j = 0; i < size; i++, j += 4) {
      dst[j]   = (t_byte)  (src[i]        & 0xff);
      dst[j+1] = (t_byte) ((src[i] >> 8)  & 0xff);
      dst[j+2] = (t_byte) ((src[i] >> 16) & 0xff);
      dst[j+3] = (t_byte) ((src[i] >> 24) & 0xff);
    }
  }

  // little endian octa to bytes mapping function - the size is in octa
  static inline void leotob (t_byte* dst, const t_octa* src, const long size) {
    for (long i = 0, j = 0; i < size; i++, j += 8) {
      dst[j]   = (t_byte)  (src[i]        & 0xff);
      dst[j+1] = (t_byte) ((src[i] >> 8)  & 0xff);
      dst[j+2] = (t_byte) ((src[i] >> 16) & 0xff);
      dst[j+3] = (t_byte) ((src[i] >> 24) & 0xff);
      dst[j+4] = (t_byte) ((src[i] >> 32) & 0xff);
      dst[j+5] = (t_byte) ((src[i] >> 40) & 0xff);
      dst[j+6] = (t_byte) ((src[i] >> 48) & 0xff);
      dst[j+7] = (t_byte) ((src[i] >> 56) & 0xff);
    }
  }

  // big endian byte to quad mapping function - the size is in bytes
  static inline void bebtoq (t_quad* dst, const t_byte* src, const long size) {
    for (long i = 0, j = 0; j < size; i++, j += 4)
      dst[i] =   ((t_quad) src[j+3])        | 
	        (((t_quad) src[j+2]) << 8)  |
	        (((t_quad) src[j+1]) << 16) | 
	        (((t_quad) src[j])   << 24);
  }

  // big endian quad to bytes mapping functions - the size is in quad
  static inline void beqtob (t_byte* dst, const t_quad* src, const long size) {
    for (long i = 0, j = 0; i < size; i++, j += 4) {
      dst[j+3] = (t_byte)  (src[i]        & 0xff);
      dst[j+2] = (t_byte) ((src[i] >> 8)  & 0xff);
      dst[j+1] = (t_byte) ((src[i] >> 16) & 0xff);
      dst[j]   = (t_byte) ((src[i] >> 24) & 0xff);
    }
  }

  // big endian byte to octa mapping function - the size is in bytes
  static inline void bebtoo (t_octa* dst, const t_byte* src, const long size) {
    for (long i = 0, j = 0; j < size; i++, j += 8)
      dst[i] =   ((t_octa) src[j+7])        | 
	        (((t_octa) src[j+6]) << 8)  |
	        (((t_octa) src[j+5]) << 16) | 
	        (((t_octa) src[j+4]) << 24) |
	        (((t_octa) src[j+3]) << 32) |
	        (((t_octa) src[j+2]) << 40) |
	        (((t_octa) src[j+1]) << 48) |
	        (((t_octa) src[j])   << 56);
  }

  // big endian octa to bytes mapping function - the size is in octa
  static inline void beotob (t_byte* dst, const t_octa* src, const long size) {
    for (long i = 0, j = 0; i < size; i++, j += 8) {
      dst[j+7] = (t_byte)  (src[i]        & 0xff);
      dst[j+6] = (t_byte) ((src[i] >> 8)  & 0xff);
      dst[j+5] = (t_byte) ((src[i] >> 16) & 0xff);
      dst[j+4] = (t_byte) ((src[i] >> 24) & 0xff);
      dst[j+3] = (t_byte) ((src[i] >> 32) & 0xff);
      dst[j+2] = (t_byte) ((src[i] >> 40) & 0xff);
      dst[j+1] = (t_byte) ((src[i] >> 48) & 0xff);
      dst[j]   = (t_byte) ((src[i] >> 56) & 0xff);
    }
  }

  // byte to character mapping function
  static inline char btoc (const char byte, const bool flag) {
    char data = flag ? byte & 0x0f : (byte >> 4) & 0x0f;
    if (data < 10) return '0' + data;
    return 'A' + (data - 10);
  }

  // byte to string mapping function - the size is in byte
  static inline String btos (const t_byte* src, const long size) {
    String result;
    for (long i = 0; i < size; i++) {
      result = result + btoc (src[i], false);
      result = result + btoc (src[i], true);
    }
    return result;
  }

  // -------------------------------------------------------------------------
  // - logical functions                                                     -
  // -------------------------------------------------------------------------

  // quad rotate left
  static inline t_quad qrotl (t_quad x, long n) {
    return ((x << n) | (x >> (32 - n)));
  }

  // quad rotate right
  static inline t_quad qrotr (t_quad x, long n) {
    return ((x >> n) | (x << (32 - n)));
  }

  // octa rotate left
  static inline t_octa orotl (t_octa x, long n) {
    return ((x << n) | (x >> (64 - n)));
  }

  // octa rotate right
  static inline t_octa orotr (t_octa x, long n) {
    return ((x >> n) | (x << (64 - n)));
  }
}

#endif
