You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module CodesetUtil
  20. def self.replace_invalid_utf8(str)
  21. return nil if str.nil?
  22. str = str.dup
  23. str.force_encoding('UTF-8')
  24. if ! str.valid_encoding?
  25. str = str.encode("UTF-16LE", :invalid => :replace,
  26. :undef => :replace, :replace => '?').encode("UTF-8")
  27. end
  28. str
  29. end
  30. def self.to_utf8(str, encoding)
  31. return if str.nil?
  32. str = str.b
  33. if str.empty?
  34. str.force_encoding("UTF-8")
  35. return str
  36. end
  37. enc = encoding.blank? ? "UTF-8" : encoding
  38. if enc.casecmp("UTF-8") != 0
  39. str.force_encoding(enc)
  40. str = str.encode("UTF-8", :invalid => :replace,
  41. :undef => :replace, :replace => '?')
  42. else
  43. str = replace_invalid_utf8(str)
  44. end
  45. str
  46. end
  47. def self.to_utf8_by_setting(str)
  48. return if str.nil?
  49. str = str.dup
  50. self.to_utf8_by_setting_internal(str).force_encoding('UTF-8')
  51. end
  52. def self.to_utf8_by_setting_internal(str)
  53. return if str.nil?
  54. str = str.b
  55. return str if str.empty?
  56. return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match?(str) # for us-ascii
  57. str.force_encoding('UTF-8')
  58. encodings = Setting.repositories_encodings.split(',').collect(&:strip)
  59. encodings.each do |encoding|
  60. begin
  61. str.force_encoding(encoding)
  62. utf8 = str.encode('UTF-8')
  63. return utf8 if utf8.valid_encoding?
  64. rescue
  65. # do nothing here and try the next encoding
  66. end
  67. end
  68. self.replace_invalid_utf8(str).force_encoding('UTF-8')
  69. end
  70. def self.from_utf8(str, encoding)
  71. return if str.nil?
  72. str = str.dup
  73. str ||= ''
  74. str.force_encoding('UTF-8')
  75. if encoding.casecmp('UTF-8') != 0
  76. str = str.encode(encoding, :invalid => :replace,
  77. :undef => :replace, :replace => '?')
  78. else
  79. str = self.replace_invalid_utf8(str)
  80. end
  81. end
  82. def self.guess_encoding(str)
  83. return if str.nil?
  84. str = str.dup
  85. encodings = Setting.repositories_encodings.split(',').collect(&:strip)
  86. encodings = encodings.presence || ['UTF-8']
  87. encodings.each do |encoding|
  88. begin
  89. str.force_encoding(encoding)
  90. rescue Encoding::ConverterNotFoundError
  91. # ignore if the encoding name is invalid
  92. end
  93. return encoding if str.valid_encoding?
  94. end
  95. nil
  96. end
  97. end
  98. end