/* Copyright (C) 1994 - 2001 by Telelogic AB.
Copyright (C) 1991, 1992, 1993, 1994 by Telelogic Malmoe AB.
Copyright (C) 1990, 1991, 1999 by Telesoft Europe AB.
This Program is owned by Telelogic and is protected by national
copyright laws and international copyright treaties. Telelogic
grants you the right to use this Program on one computer or in
one local computer network at any one time.
Under this License you may only modify the source code for the purpose
of adapting it to your environment. You must reproduce and include
any copyright and trademark notices on all copies of the source code.
You may not use, copy, merge, modify or transfer the Program except as
provided in this License.
Telelogic does not warrant that the Program will meet your
requirements or that the operation of the Program will be
uninterrupted and error free. You are solely responsible that the
selection of the Program and the modification of the source code
will achieve your intended result and that the results are actually
obtained. */

#include "cucf.h"

/**************************************************
 * 
 * NAME: ERSetRule
 *
 *
 */
void
ERSetRule(tCoder* Coder, unsigned long Rule)
{
#ifdef CODER_DEBUG_BMS
  fprintf(stdout, "BaseBufSetRule\n");
#endif
  
  switch (Rule)
    {
#if !defined(CODER_REMOVE_BER)
    case er_BER:
      Coder->ER.Rule = (er_BER | CUCF_BER_LENGTH);
      break;
    case (er_BER | er_Definite):
    case (er_BER | er_Indefinite):
      Coder->ER.Rule = Rule;
      break;
#endif

#if !defined(CODER_REMOVE_PER)
    case er_PER:
      Coder->ER.Rule = (er_PER | CUCF_PER_VARIANT);
      break;
    case (er_PER | er_Aligned):
    case (er_PER | er_Unaligned):
    case (er_PER | er_NoEndPad):
#endif

#if defined(CODER_USE_UER)
    case er_UER:
#endif
    case er_NoRule:
      Coder->ER.Rule = Rule;
      break;
    }
}

/**************************************************
 * 
 * NAME: ERGetRule
 *
 *
 */
unsigned long ERGetRule(tCoder* Coder)
{
#ifdef CODER_ENV_ER
  const char* EnvRule = getenv(CODER_ENV_ER);
       if (EnvRule == NULL) Coder->ER.Rule = er_NoRule;
  else if (strcmp(EnvRule, "dber") == 0) Coder->ER.Rule = er_BER | er_Definite;
  else if (strcmp(EnvRule, "iber") == 0) Coder->ER.Rule = er_BER | er_Indefinite;
  else if (strcmp(EnvRule, "aper") == 0) Coder->ER.Rule = er_PER | er_Aligned;
  else if (strcmp(EnvRule, "uper") == 0) Coder->ER.Rule = er_PER | er_Unaligned;
  else if (strcmp(EnvRule, "nper") == 0) Coder->ER.Rule = er_PER | er_NoEndPad;
  else if (strcmp(EnvRule, "uer")  == 0) Coder->ER.Rule = er_UER;
  else if (strcmp(EnvRule, "none") == 0) Coder->ER.Rule = er_NoRule;
#endif /* CODER_ENV_ER */
  return Coder->ER.Rule;
}

/**************************************************
 * 
 * NAME: ASN1Encode
 *
 *
 */
int
ASN1Encode(tCoder* Coder, tASN1TypeInfo* Type, void* Val)
{
  switch (ERGetRule(Coder) & 0xF00)
    {

#if !defined(CODER_REMOVE_BER)
    case er_BER:
      return BEREncode(Coder, Type, Val);
#endif

#if !defined(CODER_REMOVE_PER)
    case er_PER:
      return PEREncode(Coder, Type, Val);
#endif

#if defined(CODER_USE_UER)
    case er_UER:
      return UER_ENCODE(Coder, Type, Val);
#endif

    default: break;
    }
  return ec_SUCCESS;
}

/**************************************************
 * 
 * NAME: ASN1Decode
 *
 *
 */
int
ASN1Decode(tCoder* Coder, tASN1TypeInfo* Type, void* Val)
{
  switch (ERGetRule(Coder) & 0xF00)
    {

#if !defined(CODER_REMOVE_BER)
    case er_BER:
      return BERDecode(Coder, Type, Val);
#endif

#if !defined(CODER_REMOVE_PER)
    case er_PER:
      return PERDecode(Coder, Type, Val);
#endif

#if defined(CODER_USE_UER)
    case er_UER:
      return UER_DECODE(Coder, Type, Val);
#endif

    default: break;
    }

  return ec_SUCCESS;
}
