Skip to content

Commit

Permalink
removed encodeConstraintPosWholeNumber - no unsigned numbers on the JVM
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Schramka committed Jan 8, 2024
1 parent 2b9d74d commit 2b761f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
36 changes: 21 additions & 15 deletions asn1scala/src/main/scala/asn1scala/asn1jvm_Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ trait Codec {
require(min <= v && v <= max)

val range = max - min
stainlessAssert(range >= 0)
if range == 0 then
return
return;

// runtime only right now
if range < 0 then
writeToStdErr("Range is bigger than the biggest primitive on the JVM - unsafe!")

// get number of bits that get written
val nRangeBits: Int = GetNumberOfBitsForNonNegativeInteger(range)
Expand All @@ -226,20 +231,21 @@ trait Codec {
writeToStdErr("precondition for appendBitsLSBFirst not met")
}

def encodeConstraintPosWholeNumber(v: ULong, min: ULong, max: ULong): Unit = {
require(max >= 0 && max <= Long.MaxValue)
require(min >= 0 && min <= max)
require(min <= v && v <= max)

val range: ULong = (max - min)
if range == 0 then
return

val nRangeBits: Int = GetNumberOfBitsForNonNegativeInteger(range)
val nBits: Int = GetNumberOfBitsForNonNegativeInteger(v - min)
appendNBitZero(nRangeBits - nBits)
encodeNonNegativeInteger(v - min)
}
// TODO remove - does exactly the same as encodeConstrainedWholeNumber
// def encodeConstraintPosWholeNumber(v: ULong, min: ULong, max: ULong): Unit = {
// require(max >= 0 && max <= Long.MaxValue)
// require(min >= 0 && min <= max)
// require(min <= v && v <= max)
//
// val range: ULong = (max - min)
// if range == 0 then
// return
//
// val nRangeBits: Int = GetNumberOfBitsForNonNegativeInteger(range)
// val nBits: Int = GetNumberOfBitsForNonNegativeInteger(v - min)
// appendNBitZero(nRangeBits - nBits)
// encodeNonNegativeInteger(v - min)
// }

def decodeConstraintWholeNumber(min: Long, max: Long): Option[Long] = {
require(min <= max)
Expand Down
11 changes: 0 additions & 11 deletions asn1scala/src/main/scala/asn1scala/asn1jvm_Helper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,6 @@ def GetLengthForEncodingUnsigned(v: ULong): Int = {
max((GetNumberOfBitsForNonNegativeInteger(v) + NO_OF_BITS_IN_BYTE - 1) / NO_OF_BITS_IN_BYTE, 1) // even the number 0 needs 1 byte
}.ensuring(n => n > 0 && n <= NO_OF_BYTES_IN_JVM_LONG)

/**
* Get the amount of set bits in the given 64bit number v
*
* @param v input
* @return Amount of set bits in v
*/
@extern
private def popCountL(v: Long): Int = {
Integer.bitCount((v >>> 32).toUnsignedInt) + Integer.bitCount((v & 0xFF_FF_FF_FFL).toUnsignedInt)
}

/**
* Get number of bytes needed to encode the
* positive number v according to PER rules (8.3)
Expand Down

0 comments on commit 2b761f7

Please sign in to comment.