Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect encoding literals -- escape them #302

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions jvm/src/test/scala/io/kaitai/struct/exprlang/ExpressionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,21 @@ class ExpressionsSpec extends AnyFunSpec {
Expressions.parse("foo.bar") should be (Attribute(Name(identifier("foo")),identifier("bar")))
}

describe("strings") {
it("single-quoted") {
// \" -> \"
// \\ -> \\
Expressions.parse(""" ' \" \\ ' """) should be(Str(" \\\" \\\\ "))
Expressions.parse(""" 'ASCII\\x' """) should be(Str("ASCII\\\\x"))
}
it("double-quoted") {
// \" -> "
// \\ -> \
Expressions.parse(""" " \" \\ " """) should be(Str(" \" \\ "))
Expressions.parse(""" "ASCII\\'x" """) should be(Str("ASCII\\'x"))
}
}

describe("f-strings") {
it("parses f-string with just a string") {
Expressions.parse("f\"abc\"") should be(InterpolatedStr(Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CSharpTranslator(provider: TypeProvider, importList: ImportList) extends B
override def intToStr(i: expr): String =
s"${translate(i, METHOD_PRECEDENCE)}.ToString()"
override def bytesToStr(bytesExpr: String, encoding: String): String =
s"""System.Text.Encoding.GetEncoding("$encoding").GetString($bytesExpr)"""
s"""System.Text.Encoding.GetEncoding(${doStringLiteral(encoding)}).GetString($bytesExpr)"""
override def strLength(s: expr): String =
s"${translate(s, METHOD_PRECEDENCE)}.Length"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import
}
}

def doRawStringLiteral(s: String): String = super.doStringLiteral(s)

/**
* Handles string literal for C++ by wrapping a C `const char*`-style string
* into a std::string constructor. Note that normally std::string
Expand Down Expand Up @@ -188,7 +190,7 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import
//s"std::to_string(${translate(i)})"
s"${CppCompiler.kstreamName}::to_string(${translate(i)})"
override def bytesToStr(bytesExpr: String, encoding: String): String =
s"""${CppCompiler.kstreamName}::bytes_to_str($bytesExpr, "$encoding")"""
s"""${CppCompiler.kstreamName}::bytes_to_str($bytesExpr, ${doRawStringLiteral(encoding)})"""
override def bytesLength(b: Ast.expr): String =
s"${translate(b, METHOD_PRECEDENCE)}.length()"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class JavaScriptTranslator(provider: TypeProvider, importList: ImportList) exten
s"(${translate(i)}).toString()"

override def bytesToStr(bytesExpr: String, encoding: String): String =
s"""${JavaScriptCompiler.kstreamName}.bytesToStr($bytesExpr, "$encoding")"""
s"""${JavaScriptCompiler.kstreamName}.bytesToStr($bytesExpr, ${doStringLiteral(encoding)})"""

override def strLength(s: expr): String =
s"${translate(s, METHOD_PRECEDENCE)}.length"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class JavaTranslator(provider: TypeProvider, importList: ImportList) extends Bas
s"StandardCharsets.${charsetConst}"
case None =>
importList.add("java.nio.charset.Charset")
s"""Charset.forName("$encoding")"""
s"""Charset.forName(${doStringLiteral(encoding)})"""
}
s"new String($bytesExpr, $charsetExpr)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
override def bytesToStr(bytesExpr: String, encoding: String): String = {
importList.add("local str_decode = require(\"string_decode\")")

s"""str_decode.decode($bytesExpr, "$encoding")"""
s"""str_decode.decode($bytesExpr, ${doStringLiteral(encoding)})"""
}
override def bytesSubscript(container: Ast.expr, idx: Ast.expr): String = {
s"string.byte(${translate(container)}, ${translate(idx)} + 1)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.kaitai.struct.languages.NimCompiler.{ksToNim, namespaced, camelCase}
class NimTranslator(provider: TypeProvider, importList: ImportList) extends BaseTranslator(provider) {
// Members declared in io.kaitai.struct.translators.BaseTranslator
override def bytesToStr(bytesExpr: String, encoding: String): String = {
s"""encode($bytesExpr, "$encoding")"""
s"""encode($bytesExpr, ${doStringLiteral(encoding)})"""
}
override def doEnumById(enumSpec: EnumSpec, id: String): String = s"${namespaced(enumSpec.name)}($id)"
// override def doEnumByLabel(enumSpec: EnumSpec, label: String): String = s"${namespaced(enumSpec.name)}($label)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class PHPTranslator(provider: TypeProvider, config: RuntimeConfig) extends BaseT
s"strval(${translate(i)})"

override def bytesToStr(bytesExpr: String, encoding: String): String =
s"""${PHPCompiler.kstreamName}::bytesToStr($bytesExpr, "$encoding")"""
s"""${PHPCompiler.kstreamName}::bytesToStr($bytesExpr, ${doStringLiteral(encoding)})"""

override def bytesLength(b: Ast.expr): String =
s"strlen(${translate(b)})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class PerlTranslator(provider: TypeProvider, importList: ImportList) extends Bas
s"sprintf('%d', ${translate(i)})"
override def bytesToStr(bytesExpr: String, encoding: String): String = {
importList.add("Encode")
s"""Encode::decode("$encoding", $bytesExpr)"""
s"""Encode::decode(${doStringLiteral(encoding)}, $bytesExpr)"""
}
override def bytesLength(b: Ast.expr): String =
strLength(b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class PythonTranslator(provider: TypeProvider, importList: ImportList, config: R
override def intToStr(i: Ast.expr): String =
s"str(${translate(i)})"
override def bytesToStr(bytesExpr: String, encoding: String): String =
s"""($bytesExpr).decode("$encoding")"""
s"""($bytesExpr).decode(${doStringLiteral(encoding)})"""

override def bytesLength(value: Ast.expr): String =
s"len(${translate(value)})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class RubyTranslator(provider: TypeProvider) extends BaseTranslator(provider)
override def bytesToStr(bytesExpr: String, encoding: String): String = {
// We can skip "encode to UTF8" if we're 100% sure that the string we're handling is already
// in UTF8.
s"""($bytesExpr).force_encoding("$encoding")""" + (if (encoding != "UTF-8") {
s"""($bytesExpr).force_encoding(${doStringLiteral(encoding)})""" + (if (encoding != "UTF-8") {
".encode('UTF-8')"
} else {
""
Expand Down
Loading