Index: encoding_imp.e
===================================================================
--- encoding_imp.e	(revision 83696)
+++ encoding_imp.e	(working copy)
@@ -30,13 +30,11 @@
 			-- Convert `a_from_string' of `a_from_code_page' to a string of `a_to_code_page'.
 		local
 			l_managed_pointer: MANAGED_POINTER
-			l_count, l_size, i: INTEGER
+			l_count: INTEGER
 			l_pointer: POINTER
 			l_out_count: INTEGER
-			l_string: STRING_8
 			l_string_32: STRING_32
 			l_big_endian: BOOLEAN
-			l_no_endian: BOOLEAN
 			l_error: INTEGER
 			l_retried: BOOLEAN
 			l_converted: STRING_GENERAL
@@ -46,15 +44,19 @@
 				l_big_endian := is_big_endian_code_page (a_from_code_page) or else (not is_little_endian and not is_little_endian_code_page (a_from_code_page))
 				if is_four_byte_code_page (a_from_code_page) then
 					l_string_32 := a_from_string.twin
-					if not l_big_endian then
+					if (l_big_endian and not is_little_endian) or (not l_big_endian and is_little_endian) then
 						l_string_32.precede (byte_order_mark)
+					else
+						l_string_32.precede (byte_order_mark_32_reverse)
 					end
 					l_managed_pointer := string_32_to_pointer (l_string_32)
 					l_count := (l_string_32.count) * 4
 				elseif is_two_byte_code_page (a_from_code_page) then
 					l_string_32 := a_from_string.twin
-					if not l_big_endian then
+					if (l_big_endian and not is_little_endian) or (not l_big_endian and is_little_endian) then
 						l_string_32.precede (byte_order_mark)
+					else
+						l_string_32.precede (byte_order_mark_16_reverse)
 					end
 					l_managed_pointer := wide_string_to_pointer (l_string_32)
 					l_count := (l_string_32.count) * 2
@@ -70,18 +72,21 @@
 					last_conversion_successful := True
 				end
 				check l_pointer_set: l_pointer /= default_pointer end
-				l_no_endian := not is_big_endian_code_page (a_to_code_page) and not is_little_endian_code_page (a_to_code_page)
 				if is_four_byte_code_page (a_to_code_page) then
 					l_converted := pointer_to_string_32 (l_pointer, l_out_count)
 					if not l_converted.is_empty then
-						if bom_little_endian (l_converted.code (1)) then
+						if same_endian (l_converted.code (1)) then
 							l_converted := l_converted.substring (2, l_converted.count)
-							if l_no_endian and then not is_little_endian then
+							if (is_big_endian_code_page (a_to_code_page) and is_little_endian) or else
+								(is_little_endian_code_page (a_to_code_page) and not is_little_endian)
+							then
 								l_converted := string_32_switch_endian (l_converted)
 							end
-						elseif bom_big_endian (l_converted.code (1)) then
+						elseif reverse_endian (l_converted.code (1)) then
 							l_converted := l_converted.substring (2, l_converted.count)
-							if l_no_endian and then is_little_endian then
+							if (is_little_endian_code_page (a_to_code_page) and is_little_endian) or else
+								(is_big_endian_code_page (a_to_code_page) and not is_little_endian)
+							then
 								l_converted := string_32_switch_endian (l_converted)
 							end
 						end
@@ -89,14 +94,18 @@
 				elseif is_two_byte_code_page (a_to_code_page) then
 					l_converted := pointer_to_wide_string (l_pointer, l_out_count)
 					if not l_converted.is_empty then
-						if bom_little_endian (l_converted.code (1)) then
+						if same_endian (l_converted.code (1)) then
 							l_converted := l_converted.substring (2, l_converted.count)
-							if l_no_endian and then not is_little_endian then
+							if (is_big_endian_code_page (a_to_code_page) and is_little_endian) or else
+								(is_little_endian_code_page (a_to_code_page) and not is_little_endian)
+							then
 								l_converted := string_16_switch_endian (l_converted)
 							end
-						elseif bom_big_endian (l_converted.code (1)) then
+						elseif reverse_endian (l_converted.code (1)) then
 							l_converted := l_converted.substring (2, l_converted.count)
-							if l_no_endian and then is_little_endian then
+							if (is_little_endian_code_page (a_to_code_page) and is_little_endian) or else
+								(is_big_endian_code_page (a_to_code_page) and not is_little_endian)
+							then
 								l_converted := string_16_switch_endian (l_converted)
 							end
 						end
@@ -239,10 +248,8 @@
 			a_to_code_page_valid: is_code_page_valid (a_to_code_page)
 			code_page_convertable: is_code_page_convertable (a_from_code_page, a_to_code_page)
 		local
-			l_fp, l_tp: MANAGED_POINTER
 			l_key: STRING
 			l_cd: POINTER
-			l_succ: BOOLEAN
 		do
 			l_key := a_from_code_page + a_to_code_page
 			descriptor_cache.search (l_key)
@@ -282,14 +289,14 @@
 			Result := is_codeset_convertable (a_code_set, "utf-8", a_error)
 		end
 
-	bom_little_endian (code: NATURAL_32): BOOLEAN
-			-- Is `code' little endian BOM?
+	same_endian (code: NATURAL_32): BOOLEAN
+			-- The same endianness with the sys?
 		do
-			Result := code = 0xFEFF or code = 0xFEFF0000
+			Result := code = 0xFEFF
 		end
 
-	bom_big_endian (code: NATURAL_32): BOOLEAN
-			-- Is `code' big endian BOM?
+	reverse_endian (code: NATURAL_32): BOOLEAN
+			-- Reverse endianness with the sys?
 		do
 			Result := code = 0xFFFE or code = 0xFFFE0000
 		end
@@ -332,6 +339,18 @@
 			Result := (0xFEFF).to_character_32
 		end
 
+	byte_order_mark_32_reverse: CHARACTER_32
+			-- Byte order mark (BOM)
+		once
+			Result := (0xFFFE0000).to_character_32
+		end
+
+	byte_order_mark_16_reverse: CHARACTER_32
+			-- Byte order mark (BOM)
+		once
+			Result := (0xFFFE).to_character_32
+		end
+
 	conversion_exception (a_error:INTEGER): CONVERSION_FAILURE
 			-- Create exception by `a_error'
 		do
@@ -447,7 +466,7 @@
 
 note
 	library:   "Encoding: Library of reusable components for Eiffel."
-	copyright: "Copyright (c) 1984-2009, Eiffel Software and others"
+	copyright: "Copyright (c) 1984-2010, Eiffel Software and others"
 	license:   "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
 	source: "[
 			Eiffel Software
