Commit 630bbb9e authored by juvancic's avatar juvancic
Browse files

Added ans1c from ITS

parent 89d815b3
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
#include <asn1/asn_application.h>

#include "asn1_recoder.hh"

#include <TTCN3.hh>

extern "C" {
static int asn1c_collect_encoded_data(const void *buffer, size_t size, void *application_specific_key) {
  TTCN_Buffer *tb = (TTCN_Buffer *)application_specific_key;
  tb->put_s(size, (unsigned char *)buffer);
  return 0;
}
}

int asn1_recoder::recode(const asn_TYPE_descriptor_s &td, int from, int to, TTCN_Buffer &buf) {
  int            rc  = -1;
  void *         ptr = NULL;
  asn_dec_rval_t rc_d;
  rc_d = asn_decode(NULL, (asn_transfer_syntax)from, (asn_TYPE_descriptor_s *)&td, &ptr, buf.get_data(), buf.get_len());
  if (rc_d.code == RC_OK) {
    // Encode as PER
    asn_enc_rval_t rc_e;
    buf.clear();
    rc_e = asn_encode(NULL, (asn_transfer_syntax)to, (asn_TYPE_descriptor_s *)&td, ptr, asn1c_collect_encoded_data, &buf);
    rc   = rc_e.encoded;
  }
  if (ptr) {
    ASN_STRUCT_FREE(td, ptr);
  }
  return rc;
}

int asn1_recoder::ber2per(const asn_TYPE_descriptor_s &td, TTCN_Buffer &buf) 
{
  //return recode(td, (int)ATS_BER, (int)ATS_UNALIGNED_CANONICAL_PER, buf);
  //return recode(td, (int)ATS_BER, (int)ATS_ALIGNED_CANONICAL_PER, buf);
  //return recode(td, (int)ATS_BER, (int)ATS_ALIGNED_BASIC_PER, buf);
  return recode(td, (int)ATS_DER, (int)ATS_UNALIGNED_BASIC_PER, buf);
}

int asn1_recoder::per2ber(const asn_TYPE_descriptor_s &td, TTCN_Buffer &buf) 
{
  //return recode(td, (int)ATS_ALIGNED_CANONICAL_PER, (int)ATS_BER, buf);
  //return recode(td, (int)ATS_ALIGNED_BASIC_PER, (int)ATS_BER, buf);
  return recode(td, (int)ATS_UNALIGNED_BASIC_PER, (int)ATS_DER, buf);
}

int asn1_recoder::ber2oer(const asn_TYPE_descriptor_s & td, TTCN_Buffer & buf)
{
  return recode(td, (int)ATS_BER, (int)ATS_CANONICAL_OER, buf);
}

int asn1_recoder::oer2ber(const asn_TYPE_descriptor_s & td, TTCN_Buffer & buf)
{
  return recode(td, (int)ATS_BASIC_OER, (int)ATS_BER, buf);
}
+29 −0
Original line number Diff line number Diff line
/*!
 * \file      asn1_recode_per.hh
 * \brief     Header file ASN.1 PER codec based on asn1c external tool.
 * \author    ETSI STF525
 * \copyright ETSI Copyright Notification
 *            No part may be reproduced except as authorized by written permission.
 *            The copyright and the foregoing restriction extend to reproduction in all media.
 *            All rights reserved.
 * \version   0.1
 */
#pragma once

class OCTETSTRING;           //! Forward declaration of TITAN class
class CHARSTRING;            //! Forward declaration of TITAN class
class BITSTRING;             //! Forward declaration of TITAN class
class TTCN_Buffer;           //! Forward declaration of TITAN class
class TTCN_EncDec;           //! Forward declaration of TITAN class
class TTCN_Typedescriptor_t; //! Forward declaration of TITAN class

struct asn_TYPE_descriptor_s; //! Forward declaration of asn1c class

class asn1_recoder {
protected:
  int ber2per(const asn_TYPE_descriptor_s &td, TTCN_Buffer &buf);
  int per2ber(const asn_TYPE_descriptor_s &td, TTCN_Buffer &buf);
  int ber2oer(const asn_TYPE_descriptor_s &td, TTCN_Buffer &buf);
  int oer2ber(const asn_TYPE_descriptor_s &td, TTCN_Buffer &buf);
  int recode(const asn_TYPE_descriptor_s &td, int from, int to, TTCN_Buffer &buf);
}; // End of class asn1_recode_per

ccsrc/Asn1c/module.mk

0 → 100644
+3 −0
Original line number Diff line number Diff line
sources := asn1_recoder.cc 
includes := .
+42 −0
Original line number Diff line number Diff line
#pragma once

#include "params.hh"
#include "asn1_recoder.hh"

class OCTETSTRING;
class CHARSTRING;
class BITSTRING;

struct asn_TYPE_descriptor_s;

template<typename TPDU> class oer_codec : public asn1_recoder
{
public:
  virtual int encode(const TPDU& msg, BITSTRING& bits) = 0;
  virtual int decode(const BITSTRING& bits, TPDU& msg) = 0;
  
protected:
  inline int _decode (const TTCN_Typedescriptor_t& ttcn, const asn_TYPE_descriptor_s & td, const BITSTRING& p_data, TPDU& msg) {
    TTCN_Buffer buf(bit2oct(p_data));
    TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);
    int rc = oer2xer (td, buf);
    if (rc > 0) {
      msg.decode(ttcn, buf, TTCN_EncDec::CT_BER, BER_ACCEPT_ALL);
      rc = buf.get_len();
    }
    return rc;
  }
  inline int _encode (const TTCN_Typedescriptor_t& ttcn, const asn_TYPE_descriptor_s & td, const TPDU& msg, BITSTRING& p_data) {
    int rc = -1;
    TTCN_Buffer buf;
    TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);
    msg.encode(ttcn, buf, TTCN_EncDec::CT_BER, BER_ENCODE_DER);
    if (buf.get_len() > 0) {
      rc = xer2oer (td, buf);
      if (rc > 0) {
        p_data = oct2bit(OCTETSTRING(buf.get_len(), buf.get_data()));
      }
    }
    return rc;
  }
};
+81 −0
Original line number Diff line number Diff line
/*!
 * \file      per_codec.hh
 * \brief     Header file for TITAN message to ASN.1 PER message codec.
 * \author    ETSI STF525
 * \copyright ETSI Copyright Notification
 *            No part may be reproduced except as authorized by written permission.
 *            The copyright and the foregoing restriction extend to reproduction in all media.
 *            All rights reserved.
 * \version   0.1
 */
#pragma once

#include "params.hh"
#include "asn1_recoder.hh"

class BITSTRING;             //! Forward declaration of TITAN class
class TTCN_Typedescriptor_t; //! Forward declaration of TITAN class

struct asn_TYPE_descriptor_s; //! Declare asn1c class

/*!
 * \class per_codec
 * \brief  This class provides the interface for all ASN.1 PER codecs.
 * \remark This class uses asn1c external tool
 */
template<typename TPDU>
class per_codec : public asn1_recoder
{
public: //! \publicsection
  /*!
   * \fn int encode(const TPDU& p_message, BITSTRING& p_bitstring);
   * \brief Encode TITAN message into ASN.1 PER message
   * \param[in]  p_message   The PDU message to encode
   * \param[out] p_bitstring  The encoded PDU message in bit string format
   * \pure
   */
  virtual int encode(const TPDU& p_message, BITSTRING& p_bitstring) = 0;
  /*!
   * \fn int decode(const BITSTRING& p_bitstring, TPDU& p_message);
   * \brief Decode ASN.1 PER message into TITAN message
   * \param[in] p_bitstring  The network message in bit string format to decode
   * \param[out] p_message   The PDU message
   * \pure
   */
  virtual int decode(const BITSTRING& p_bitstring, TPDU& p_message) = 0;
  
protected: //! \protectedsection
  inline int _decode (const TTCN_Typedescriptor_t& ttcn, const asn_TYPE_descriptor_s & td, const BITSTRING& p_data, TPDU& msg);
  inline int _encode (const TTCN_Typedescriptor_t& ttcn, const asn_TYPE_descriptor_s & td, const TPDU& msg, BITSTRING& p_data);
}; // End of class per_codec

#include <TTCN3.hh>

template<class TPDU>
int per_codec<TPDU>::_decode (const TTCN_Typedescriptor_t& ttcn, const asn_TYPE_descriptor_s & td, const BITSTRING& p_data, TPDU& msg) {
  TTCN_Buffer buf(bit2oct(p_data));
  TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);
  //int rc = per2ber (td, buf);
  //if (rc > 0) {
    msg.decode(ttcn, buf, TTCN_EncDec::CT_BER, BER_ACCEPT_ALL);
    int rc = buf.get_len();
  //}
  return rc;
}

template<class TPDU>
int per_codec<TPDU>::_encode (const TTCN_Typedescriptor_t& ttcn, const asn_TYPE_descriptor_s & td, const TPDU& msg, BITSTRING& p_data) {
  int rc = 1;//-1;
  TTCN_Buffer buf;
  TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT/*WARNING*/);
  msg.encode(ttcn, buf, TTCN_EncDec::CT_BER, BER_ENCODE_DER);
  //if (buf.get_len() > 0) {
  //  rc = ber2per (td, buf);
  //  if (rc > 0) {
      p_data = oct2bit(OCTETSTRING(buf.get_len(), buf.get_data()));
      rc = buf.get_len();
  //  }
  //}
  return rc;
}