spc476 / CBOR

The most comprehensive CBOR module in the Lua universe.
GNU Lesser General Public License v3.0
22 stars 3 forks source link

how to encode binary data that is incidentally valid UTF-8? #9

Open fferri opened 2 years ago

fferri commented 2 years ago
cbor.encode{a='\x66\xc3\xb9'}

encodes as:

A1           # map(1)
   61        # text(1)
      61     # "a"
   63        # text(3)
      66C3B9 # "f\xC3\xB9"

however the 3 bytes '\x66\xc3\xb9' are meant as bytes(3) (0x43), not text(3) (0x63).

Is there a way to force bytes for a particular field? I mean without resorting to a low-level encoding such as:

enc = cbor.TYPE.MAP(3)
      .. cbor.encode"a" .. cbor.TYPE.BIN('\x66\xc3\xb9')

Also, I can imagine if the library has to guess wether a Lua string is text or bytes, it has to scan the string, so telling what it is upfront could also save some CPU time.