Backport encoding fixes

This allows using ruby 2.0 with rb-readline-0.4.2