This only causes issues for values >= 0xfd that are represented using multiple bytes. I encountered it while serializing multisig transactions that has large scriptSig (it works fine with regular transactions, since they usually don't have >= 0xfd values).
I attempted to reverse it myself, but it didn't work well - seems like some parts of the code expects it to return big endian?