Two more things ...
UTF-8 is self synchronising. If you find an error within a UTF-8 stream, all you have to do is skip all octets that follow the 10xxxxxx bit pattern, the following octet will again be the start of a UTF-8 multi-octet character code.
As for writing such a library portably across PIM and ISO, this will require separate implementations for lower level code that manipulates individual bits or blocks of bits because bit operations are not portable between PIM and ISO.