in reply to Re: Creating MIME entities with CRLF line endings
in thread Creating MIME entities with CRLF line endings

... locate the offsets/lengths of the substrings containing the headers.

That, and account for the fact that the substitution may change the length of a header, and thus the offsets to subsequent headers. It's often easiest to reverse() the list of header locations and work your way backwards.

Anno