aboutsummaryrefslogtreecommitdiffstats
path: root/autofill-parser/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixListLoader.kt
blob: 8491030d14ddc7531eb25874f60282f6651bbd90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * SPDX-License-Identifier: (LGPL-3.0-only WITH LGPL-3.0-linking-exception) OR MPL-2.0
 */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.lib.publicsuffixlist

import android.content.Context
import java.io.BufferedInputStream
import java.io.IOException

private const val PUBLIC_SUFFIX_LIST_FILE = "publicsuffixes"

internal object PublicSuffixListLoader {

  fun load(inputStream: BufferedInputStream): PublicSuffixListData =
    inputStream.use { stream ->
      val publicSuffixSize = stream.readInt()
      val publicSuffixBytes = stream.readFully(publicSuffixSize)

      val exceptionSize = stream.readInt()
      val exceptionBytes = stream.readFully(exceptionSize)

      PublicSuffixListData(publicSuffixBytes, exceptionBytes)
    }

  fun load(context: Context): PublicSuffixListData =
    load(context.assets.open(PUBLIC_SUFFIX_LIST_FILE).buffered())
}

@Suppress("MagicNumber")
private fun BufferedInputStream.readInt(): Int {
  return (read() and
    0xff shl
    24 or
    (read() and 0xff shl 16) or
    (read() and 0xff shl 8) or
    (read() and 0xff))
}

private fun BufferedInputStream.readFully(size: Int): ByteArray {
  val bytes = ByteArray(size)

  var offset = 0
  while (offset < size) {
    val read = read(bytes, offset, size - offset)
    if (read == -1) {
      throw IOException("Unexpected end of stream")
    }
    offset += read
  }

  return bytes
}