[lib] better handling of encoding