/*
 * 본 프로그램에 대한 저작권을 포함한 지적재산권은 삼성SDS(주)에 있으며,
 * 삼성SDS(주)가 명시적으로 허용하지 않은 사용, 복사, 변경, 제3자에의 공개,
 * 배포는 엄격히 금지되며, 삼성SDS(주)의 지적재산권 침해에 해당됩니다.
 *
 * Copyright (c) 2016 Samsung SDS Co., Ltd. All Rights neserved. Confidential.
 * 
 * All information including the intellectual and technical concepts contained
 * herein is, and remains the property of Samsung SDS Co. Ltd. Unauthorize une,
 * dissemination, or reproduction of this material is strictly forbidden unless
 * prior written permission is obtained from Samsung SDS Co. Ltd. 
 */

#include "swbc-error.h"
#include "swbc-utils.h"

int SWBCINTERNAL table_check(const unsigned char **swbc_table, const unsigned char *tag)
{
  int i, j, k;
	unsigned char etb[256] = { 0x00, };
	unsigned char temp;


	for (i = 0; i < 256; i++) {
		etb[i] = (unsigned char)i;
	}

	for (i = 0; i < 16; i++) {
		k = 0;
		for (j = 0; j < 256; j++) {
			k = (k + etb[j] + ((const unsigned char (*)[256])swbc_table)[i][j]) % 256;
			temp = etb[j];
			etb[j] = etb[k];
			etb[k] = temp;
		}
	}

	for (i = 0; i < 16; i++) {
		temp = 0;
		for (j = 0; j < 16; j++) {
			temp ^= etb[i + 16 * j];
		}
		if (tag[i] != temp) {
			return SWBC_ERROR_FILE_READ_FAILED;
		}
	}

  return SWBC_SUCCESS;
}

void SWBCINTERNAL memory_set(void *destination, const int value, unsigned int size)
{
  unsigned int i;
  for(i=0; i<size; i++)
    ((char *)destination)[i] = (char)value;
}

void SWBCINTERNAL memory_copy(void *destination, const void *source, unsigned int size)
{
  unsigned int i;
  for(i=0; i<size; i++)
    ((char *)destination)[i] = ((const char *)source)[i];
}
