decoder.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package xml // import "miniflux.app/v2/internal/reader/xml"
  4. import (
  5. "bytes"
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. "strings"
  10. "unicode/utf8"
  11. "miniflux.app/v2/internal/reader/encoding"
  12. )
  13. // NewXMLDecoder returns a XML decoder that filters illegal characters.
  14. func NewXMLDecoder(data io.ReadSeeker) *xml.Decoder {
  15. var decoder *xml.Decoder
  16. // This is way fasted than io.ReadAll(data) as the buffer can be allocated in one go instead of dynamically grown.
  17. buffer := &bytes.Buffer{}
  18. io.Copy(buffer, data)
  19. enc := getEncoding(buffer.Bytes())
  20. if enc == "" || strings.EqualFold(enc, "utf-8") {
  21. // filter invalid chars now, since decoder.CharsetReader not called for utf-8 content
  22. filteredBytes := filterValidXMLChars(buffer.Bytes())
  23. decoder = xml.NewDecoder(bytes.NewReader(filteredBytes))
  24. } else {
  25. // filter invalid chars later within decoder.CharsetReader
  26. data.Seek(0, io.SeekStart)
  27. decoder = xml.NewDecoder(data)
  28. }
  29. decoder.Entity = xml.HTMLEntity
  30. decoder.Strict = false
  31. decoder.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
  32. utf8Reader, err := encoding.CharsetReader(charset, input)
  33. if err != nil {
  34. return nil, err
  35. }
  36. rawData, err := io.ReadAll(utf8Reader)
  37. if err != nil {
  38. return nil, fmt.Errorf("encoding: unable to read data: %w", err)
  39. }
  40. filteredBytes := filterValidXMLChars(rawData)
  41. return bytes.NewReader(filteredBytes), nil
  42. }
  43. return decoder
  44. }
  45. // filterValidXMLChars filters inplace invalid XML characters.
  46. // This function is inspired from bytes.Map
  47. func filterValidXMLChars(s []byte) []byte {
  48. var i uint // declaring it as an uint removes a bound check in the loop.
  49. var j int
  50. for i = 0; i < uint(len(s)); {
  51. wid := 1
  52. r := rune(s[i])
  53. if r >= utf8.RuneSelf {
  54. r, wid = utf8.DecodeRune(s[i:])
  55. }
  56. if r != utf8.RuneError {
  57. if r = filterValidXMLChar(r); r >= 0 {
  58. utf8.EncodeRune(s[j:], r)
  59. j += wid
  60. }
  61. }
  62. i += uint(wid)
  63. }
  64. return s[:j]
  65. }
  66. // This function is copied from encoding/xml package,
  67. // and is used to check if all the characters are legal.
  68. func filterValidXMLChar(r rune) rune {
  69. if r == 0x09 ||
  70. r == 0x0A ||
  71. r == 0x0D ||
  72. r >= 0x20 && r <= 0xD7FF ||
  73. r >= 0xE000 && r <= 0xFFFD ||
  74. r >= 0x10000 && r <= 0x10FFFF {
  75. return r
  76. }
  77. return -1
  78. }
  79. // This function is copied from encoding/xml's procInst and adapted for []bytes instead of string
  80. func getEncoding(b []byte) string {
  81. // This parsing is somewhat lame and not exact.
  82. // It works for all actual cases, though.
  83. idx := bytes.Index(b, []byte("encoding="))
  84. if idx == -1 {
  85. return ""
  86. }
  87. v := b[idx+len("encoding="):]
  88. if len(v) == 0 {
  89. return ""
  90. }
  91. if v[0] != '\'' && v[0] != '"' {
  92. return ""
  93. }
  94. idx = bytes.IndexRune(v[1:], rune(v[0]))
  95. if idx == -1 {
  96. return ""
  97. }
  98. return string(v[1 : idx+1])
  99. }