Skip to content

Commit

Permalink
fix UTF-32 BOM (#8407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Endeavour233 committed May 13, 2024
1 parent 2f618f7 commit 62031cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions okhttp/src/main/kotlin/okhttp3/internal/-UtilCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ internal val UNICODE_BOMS =
"efbbbf".decodeHex(),
// UTF-16BE.
"feff".decodeHex(),
// UTF-32LE.
"fffe0000".decodeHex(),
// UTF-16LE.
"fffe".decodeHex(),
// UTF-32BE.
"0000ffff".decodeHex(),
// UTF-32LE.
"ffff0000".decodeHex(),
"0000feff".decodeHex(),
)

/**
Expand Down
10 changes: 7 additions & 3 deletions okhttp/src/main/kotlin/okhttp3/internal/-UtilJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,18 @@ internal fun format(
return String.format(Locale.US, format, *args)
}

/**
* will also strip BOM from the source
*/
@Throws(IOException::class)
internal fun BufferedSource.readBomAsCharset(default: Charset): Charset {
return when (select(UNICODE_BOMS)) {
// a mapping from the index of encoding methods in UNICODE_BOMS to its corresponding encoding method
0 -> UTF_8
1 -> UTF_16BE
2 -> UTF_16LE
3 -> UTF_32BE
4 -> UTF_32LE
2 -> UTF_32LE
3 -> UTF_16LE
4 -> UTF_32BE
-1 -> default
else -> throw AssertionError()
}
Expand Down
10 changes: 5 additions & 5 deletions okhttp/src/test/java/okhttp3/ResponseBodyJvmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ResponseBodyJvmTest {

@Test
fun stringBomOverridesExplicitCharset() {
val body = body("0000ffff00000068000000650000006c0000006c0000006f", "utf-8")
val body = body("0000feff00000068000000650000006c0000006c0000006f", "utf-8")
assertThat(body.string()).isEqualTo("hello")
}

Expand All @@ -86,13 +86,13 @@ class ResponseBodyJvmTest {

@Test
fun stringBomUtf32Be() {
val body = body("0000ffff00000068000000650000006c0000006c0000006f")
val body = body("0000feff00000068000000650000006c0000006c0000006f")
assertThat(body.string()).isEqualTo("hello")
}

@Test
fun stringBomUtf32Le() {
val body = body("ffff000068000000650000006c0000006c0000006f000000")
val body = body("fffe000068000000650000006c0000006c0000006f000000")
assertThat(body.string()).isEqualTo("hello")
}

Expand Down Expand Up @@ -168,13 +168,13 @@ class ResponseBodyJvmTest {

@Test
fun readerBomUtf32Be() {
val body = body("0000ffff00000068000000650000006c0000006c0000006f")
val body = body("0000feff00000068000000650000006c0000006c0000006f")
assertThat(exhaust(body.charStream())).isEqualTo("hello")
}

@Test
fun readerBomUtf32Le() {
val body = body("ffff000068000000650000006c0000006c0000006f000000")
val body = body("fffe000068000000650000006c0000006c0000006f000000")
assertThat(exhaust(body.charStream())).isEqualTo("hello")
}

Expand Down

0 comments on commit 62031cd

Please sign in to comment.