adobe / svg-native-viewer

SVG Native viewer is a library that parses and renders SVG Native documents
Apache License 2.0
155 stars 37 forks source link

CreateSVGDocument takes a C string #164

Open bungeman opened 2 years ago

bungeman commented 2 years ago

CreateSVGDocument should take a pointer and a length for the SVG data, as well as document that the encoding of the data must be UTF-8. While the XML specification forbids the use of U+0000 so it could in theory be used as a stop, real UTF-8 is not terminated by U+0000 (U+0000 is a perfectly legal code point to have in the middle of a UTF-8 string). As a result, users may have the SVG document in UTF-8 in hand but without any trailing '\0', forcing a copy just to add this extra byte.

This is additionally interesting because the sane xml parsers (Expat, LibXML2) are taking this '\0' terminated string an immediately calling strlen on it. Note that LibXML2's xmlReadMemory should be used instead of xmlReadDoc. RapidXML is not sane and actually does require a '\0' terminated string, and even makes a note in its comments that it is unsuitable for parsing straight mmap'ed files. If RapidXML is to continue to be used, then there will need two ways of providing the SVG data, one as a '\0' terminated string and one as a pointer and length (and the RapidXML port will just need to make a copy) or there will need to be some way to indicate to the user which is better.

dirkschulze commented 2 years ago

Good point. We should add the length argument with additional information how to use the API in case of RapidJSON. Not sure why we didn't use xmlReadMemory earlier. Need to look into it.

HinTak commented 1 year ago

I found myself needing this too. The OT-SVG data coming out of freetype is not (necessarily) null-terminated, but does have an accompanying length description.