Skip to content

Commit

Permalink
Fix string enc/dec for Scala
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-bucev committed Jul 22, 2024
1 parent b7924dd commit 423619f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
16 changes: 8 additions & 8 deletions StgScala/acn_scala.stg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
group scala_acn;

getStringSize(p) ::= "ULong.fromRaw(<p>.indexOf(0x00.toRawUByte))"
getStringSize(p) ::= "ULong.fromRaw(<p>.indexOfOrLength(0x00.toRawUByte))"

getSizeableSize(p, sAcc, bIsUnsigned) ::= <<
<if(bIsUnsigned)>
Expand Down Expand Up @@ -450,11 +450,11 @@ val <p> = codec.dec_String_Ascii_FixSize(<nAsn1Max>)
>>

Acn_String_Ascii_Null_Terminated_encode(p, sErrCode, nAsn1Max, arruNullBytes) ::= <<
codec.enc_String_Ascii_Null_Terminated_mult(<nAsn1Max>, Array({<arruNullBytes; separator=", ">}), <arruNullBytes.Length>, <p>)
codec.enc_String_Ascii_Null_Terminated_multVec(<nAsn1Max>, Array({<arruNullBytes; separator=", ">}), <arruNullBytes.Length>, <p>)
>>

Acn_String_Ascii_Null_Terminated_decode(p, sErrCode, nAsn1Max, arruNullBytes) ::= <<
val <p> = codec.dec_String_Ascii_Null_Terminated_mult(<nAsn1Max>, Array({<arruNullBytes; separator=", ">}), <arruNullBytes.Length>)
val <p> = codec.dec_String_Ascii_Null_Terminated_multVec(<nAsn1Max>, Array({<arruNullBytes; separator=", ">}), <arruNullBytes.Length>)
>>

Acn_String_Ascii_External_Field_Determinant_encode(p, sErrCode, nAsn1Max, sExtFld) ::= "codec.enc_String_Ascii_External_Field_Determinant(<nAsn1Max>, <p>)"
Expand Down Expand Up @@ -625,7 +625,7 @@ val <p> =

oct_sqf_null_terminated_encode(p, sAcc, i, sInternalItem, noSizeMin, nSizeMax, arruNullBytes, nBitPatternLength, sErrCode, nIntItemMinSize, nIntItemMaxSize) ::= <<
<loopFixedItem(i=i, sInternalItem=sInternalItem, fixedSize=[p, sAcc, "nCount"])>
codec.base.bitStream.appendBitsMSBFirst(Array({<arruNullBytes; separator=", ">}), <nBitPatternLength>)
codec.base.bitStream.appendBitsMSBFirstVec(Array({<arruNullBytes; separator=", ">}), <nBitPatternLength>)
>>

oct_sqf_null_terminated_decode(p, sAcc, i, sInternalItem, noSizeMin, nSizeMax, arruNullBytes, nBitPatternLength, sErrCode, nIntItemMinSize, nIntItemMaxSize) ::= <<
Expand All @@ -648,7 +648,7 @@ else if checkBitPatternPresentResult.get then
>>

bit_string_external_field_encode(sTypeDefName, p, sErrCode, sAcc, noSizeMin, nSizeMax, sExtFld) ::= <<
codec.base.bitStream.appendBitsMSBFirst(<p><sAcc>arr, <p><sAcc>nCount)
codec.base.bitStream.appendBitsMSBFirstVec(<p><sAcc>arr, <p><sAcc>nCount)
>>

bit_string_external_field_decode(sTypeDefName, p, sErrCode, sAcc, noSizeMin, nSizeMax, sExtFld) ::= <<
Expand All @@ -659,7 +659,7 @@ val <p> =
>>

bit_string_external_field_fixed_size_encode(sTypeDefName, p, sErrCode, sAcc, noSizeMin, nSizeMax, sExtFld) ::= <<
codec.base.bitStream.appendBitsMSBFirst(<p><sAcc>arr, <nSizeMax>)
codec.base.bitStream.appendBitsMSBFirstVec(<p><sAcc>arr, <nSizeMax>)
>>

bit_string_external_field_fixed_size_decode(sTypeDefName, p, sErrCode, sAcc, noSizeMin, nSizeMax, sExtFld) ::= <<
Expand All @@ -670,7 +670,7 @@ val <p> =
>>

bit_string_null_terminated_encode(sTypeDefName, p, sErrCode, sAcc, i, noSizeMin, nSizeMax, arruNullBytes, nBitPatternLength) ::= <<
codec.base.bitStream.appendBitsMSBFirst(<p><sAcc>arr, <p><sAcc>arr.length*8) // TODO: re-introduce nCount? -> codec.base.bitStream.appendBitsMSBFirst(<p><sAcc>arr, <p><sAcc>nCount)
codec.base.bitStream.appendBitsMSBFirstVec(<p><sAcc>arr, <p><sAcc>arr.length*8) // TODO: re-introduce nCount? -> codec.base.bitStream.appendBitsMSBFirst(<p><sAcc>arr, <p><sAcc>nCount)
codec.base.bitStream.appendBitsMSBFirst(Array({<arruNullBytes; separator=", ">}), <nBitPatternLength>)
>>

Expand Down Expand Up @@ -1078,7 +1078,7 @@ octet_string_containing_ext_field_func_decode(p, sFuncName, sReqBytesForUperEnco
>>

bit_string_containing_ext_field_func_encode(p, sFuncName, sReqBytesForUperEncoding, sReqBitsForUperEncoding, sExtField, sErrCode) ::= <<
codec.base.bitStream.appendBitsMSBFirst(arr, <sExtField>.toInt)
codec.base.bitStream.appendBitsMSBFirstVec(arr, <sExtField>.toInt)
>>

bit_string_containing_ext_field_func_decode(p, sFuncName, sReqBytesForUperEncoding, sReqBitsForUperEncoding, sExtField, sErrCode) ::= <<
Expand Down
14 changes: 12 additions & 2 deletions StgScala/equal_scala.stg
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ def <sFuncName>(<sVarName1>: <sTypeDefName>, <sVarName2>: <sTypeDefName>): Boole

isEqual_Primitive(p1, p2) ::= "<p1> == <p2>"

isEqual_String(p1, p2) ::= "<p1>.toScala.sameElements(<p2>.toScala)"
isEqual_String(p1, p2) ::= <<
locally {
val zero = <p1>.indexOfOrLength(UByte.fromRaw(0))
<p1>.toScala.slice(0, zero).sameElements(<p2>.toScala.slice(0, zero))
}
>>


isEqual_Integer(p1, p2) /*nogen*/::= "ret = (<p1> == <p2>)"
Expand All @@ -90,7 +95,12 @@ isEqual_Boolean(p1, p2) /*nogen*/::= "ret = ( (<p1> && <p2>) || (!<p1> && !<p2>)

isEqual_Real(p1, p2) ::= "Asn1Real_Equal(<p1>, <p2>)"

isEqual_IA5String(p1, p2) /*nogen*/::= "ret = <p1>.toScala.sameElements(<p2>.toScala)"
isEqual_IA5String(p1, p2) /*nogen*/::= <<
locally {
val zero = <p1>.indexOfOrLength(UByte.fromRaw(0))
ret = <p1>.toScala.slice(0, zero).sameElements(<p2>.toScala.slice(0, zero))
}
>>

isEqual_NumericString(p1, p2) /*nogen*/::= "<isEqual_IA5String(p1=p1, p2=p2)>"
isEqual_NullType()/*nogen*/ ::= "ret = true"
Expand Down
8 changes: 4 additions & 4 deletions StgScala/init_scala.stg
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ val <sResVar> = Vector.fill(<nMaxSizePlusOne>)(UByte.fromRaw(0))
<else>
<if(bAlpha)>
val allowedCharSet: Array[UByte] = Array(<arrnAlphabetAsciiCodes:{ch|0x<ch;format="X2">.toRawUByte}; wrap, anchor, separator=",">)
val <sResVar>_tmp = scala.collection.immutable.Vector.tabulate(<nMaxSizePlusOne>)(<i> => if <i> == <nMaxSizePlusOne> - 1 then 0.toByte else allowedCharSet(<i> % <nAlphabetLength>))
val <sResVar> = Vector.fromScala(<sResVar>_tmp)
val <sResVar>_tmp = scala.collection.immutable.Vector.tabulate(<nSize>)(<i> => if <i> == <nMaxSizePlusOne> - 1 then UByte.fromRaw(0) else allowedCharSet(<i> % <nAlphabetLength>))
val <sResVar> = Vector.fromScala(<sResVar>_tmp :+ UByte.fromRaw(0))
<else>
val <sResVar>_tmp = scala.collection.immutable.Vector.tabulate(<nMaxSizePlusOne>)(<i> => UByte.fromRaw(if <i> == <nMaxSizePlusOne> - 1 then 0.toByte else if <i> % 128 == 0 then 'A'.toByte else (<i> % 128).toByte))
val <sResVar> = Vector.fromScala(<sResVar>_tmp)
val <sResVar>_tmp = scala.collection.immutable.Vector.tabulate(<nSize>)(<i> => UByte.fromRaw(if <i> == <nMaxSizePlusOne> - 1 then 0.toByte else if <i> % 128 == 0 then 'A'.toByte else (<i> % 128).toByte))
val <sResVar> = Vector.fromScala(<sResVar>_tmp :+ UByte.fromRaw(0))
<endif>
<endif>
>>
Expand Down
4 changes: 2 additions & 2 deletions StgScala/isvalid_scala.stg
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ StatementForLoop(p, sAcc, i, bIsFixedSize, nFixedSize, sInnerStatement) ::= <<


Print_AlphabetCheckFunc(sFuncName, arrsAlphaConBody) ::= <<
def <sFuncName>(str0: Array[UByte]): Boolean =
def <sFuncName>(str0: Vector[UByte]): Boolean =
{
val str = str0.toArrayRaws
val str = str0.toVectorRaws
var valid: Boolean = true
var i: Int = 0
(while (i \< str.length && (str(i) != CHAR_0000) && valid) {
Expand Down
2 changes: 1 addition & 1 deletion StgScala/uper_scala.stg
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ str_FixedSize_decode(p, sTasName, i, sInternalItem, nFixedSize, nIntItemMinSize,


str_VarSize_encode(p, sPIden, sTasName, i, sInternalItem, nSizeMin, nSizeMax, nSizeInBits, nIntItemMinSize, nIntItemMaxSize, nAlignSize, soInitExpr, soCallAux) ::= <<
nStringLength = <p>.indexOf(0x00.toRawUByte)
nStringLength = <p>.indexOfOrLength(0x00.toRawUByte)
/*ret = nStringLength >= <nSizeMin> && nStringLength \<= <nSizeMax>;*/
codec.base.encodeConstrainedWholeNumber(nStringLength, <nSizeMin>, <nSizeMax>)
val <sPIden>_nCount = nStringLength
Expand Down
8 changes: 8 additions & 0 deletions asn1scala/src/main/scala/asn1scala/asn1jvm_Codec_ACN.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,10 @@ case class ACN(base: Codec) {
i += 1
}

def enc_String_Ascii_Null_Terminated_multVec(max: Long, null_character: Array[Byte], null_character_size: Int, strVal: Vector[ASCIIChar]): Unit = {
enc_String_Ascii_Null_Terminated_mult(max, null_character, null_character_size, strVal.toScala.toArray)
}


def enc_String_Ascii_External_Field_Determinant(max: Long, strVal: Array[ASCIIChar]): Unit = {
enc_String_Ascii_private(max, strVal)
Expand Down Expand Up @@ -1690,6 +1694,10 @@ case class ACN(base: Codec) {
strVal
}

def dec_String_Ascii_Null_Terminated_multVec(max: Long, null_character: Array[ASCIIChar], null_character_size: Int): Vector[ASCIIChar] = {
val res = dec_String_Ascii_Null_Terminated_mult(max, null_character, null_character_size)
Vector.fromScala(res.toVector)
}

@opaque @inlineOnce
def dec_String_Ascii_External_Field_Determinant(max: Long, extSizeDeterminantFld: Long): Array[ASCIIChar] = {
Expand Down

0 comments on commit 423619f

Please sign in to comment.