Skip to content

Commit

Permalink
Safe EIP4 decimals parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskin1 committed Aug 17, 2022
1 parent 4b403a5 commit db86a42
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lazy val commonSettings = Seq(
scalacOptions ++= commonScalacOptions,
scalaVersion := "2.12.15",
organization := "org.ergoplatform",
version := "9.16.5",
version := "9.16.6",
resolvers += Resolver.sonatypeRepo("public"),
resolvers += Resolver.sonatypeRepo("snapshots"),
libraryDependencies ++= dependencies.Testing ++ dependencies.CompilerPlugins,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ object TokenPropsParser {
val nameRaw = r4 >>= parse
val nameOpt = nameRaw >>= (raw => toUtf8String(raw.toArray))
nameOpt.map { name =>
val descriptionRaw = r5 >>= parse
val decimalsRaw = r6 >>= parse
val descriptionOpt = descriptionRaw >>= (raw => toUtf8String(raw.toArray))
val decimalsOpt = decimalsRaw >>= (raw => toUtf8String(raw.toArray).map(_.toInt))
val descriptionRaw = r5 >>= parse
val decimalsRaw = r6 >>= parse
val descriptionOpt = descriptionRaw >>= (raw => toUtf8String(raw.toArray))
val decimalsOpt =
Try(decimalsRaw >>= (raw => toUtf8String(raw.toArray).map(_.toInt))).toOption.flatten
val (description, decimals) = (descriptionOpt.getOrElse(""), decimalsOpt.getOrElse(0))
TokenPropsEip4(name, description, decimals)
}
}
}

private def looksLikeUTF8(utf8: Array[Byte]): Boolean = {
val p = Pattern.compile("\\A(\n" +
val p = Pattern.compile(
"\\A(\n" +
" [\\x09\\x0A\\x0D\\x20-\\x7E] # ASCII\\n" +
"| [\\xC2-\\xDF][\\x80-\\xBF] # non-overlong 2-byte\n" +
"| \\xE0[\\xA0-\\xBF][\\x80-\\xBF] # excluding overlongs\n" +
Expand All @@ -50,14 +52,15 @@ object TokenPropsParser {
"| \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2} # planes 1-3\n" +
"| [\\xF1-\\xF3][\\x80-\\xBF]{3} # planes 4-15\n" +
"| \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2} # plane 16\n" +
")*\\z", Pattern.COMMENTS)
")*\\z",
Pattern.COMMENTS
)
val phonyString = new String(utf8, "ISO-8859-1")
p.matcher(phonyString).matches
}

private def toUtf8String(raw: Array[Byte]): Option[String] = {
private def toUtf8String(raw: Array[Byte]): Option[String] =
if (looksLikeUTF8(raw))
Try(new String(raw, Eip4StringCharset)).toOption
else None
}
}

0 comments on commit db86a42

Please sign in to comment.